Compare commits
57 Commits
moxxmpp-v0
...
a8d80eaddf
| Author | SHA1 | Date | |
|---|---|---|---|
| a8d80eaddf | |||
| 9baf1ed73c | |||
| ce3ea656ad | |||
| ed49212f5a | |||
| ad1242c47d | |||
| 890fcfb506 | |||
| d7723615fe | |||
| 6517065a1a | |||
| 9223a7d403 | |||
| 7ce6703c5b | |||
| 37261cddbb | |||
| d8c2ef6f3b | |||
| 98e5324409 | |||
| a69c2a23f2 | |||
| d8de093e4d | |||
| 678564dbb3 | |||
| 09d2601e85 | |||
| 41560682a1 | |||
| 473f8e4bb6 | |||
| 67446285c1 | |||
| e12f4688d3 | |||
| 2581bbe203 | |||
| 995f2e0248 | |||
| e2c8f79429 | |||
| 763c93857d | |||
| 55d2ef9c25 | |||
| f37cbd1616 | |||
| 2a3449d0f2 | |||
| 596693c206 | |||
| 22aa07c4ba | |||
| 62001c1e29 | |||
| ca85c94fe5 | |||
| 637e1e25a6 | |||
| 09696c1c4d | |||
| 298a8342b8 | |||
| d64220426b | |||
| 88efdc361c | |||
| cc1b371198 | |||
| d9e4a3c1d4 | |||
| 0ae13acca0 | |||
| d383fa31ae | |||
| d1de394cd9 | |||
| 14c48bcc64 | |||
| 138edffb0a | |||
| eb8f6ba17a | |||
| beff05765b | |||
| 3b7ded3b96 | |||
| edc86a10b3 | |||
| 39e9c55fae | |||
| 1b2c567787 | |||
| d3955479f7 | |||
| 300a52f9fe | |||
| 2e3472d88f | |||
| 6b106fe365 | |||
| bfd28c281e | |||
| c307567025 | |||
| 5dd96f518b |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ko_fi: papatutuwawa
|
||||||
12
README.md
12
README.md
@@ -3,13 +3,13 @@
|
|||||||
moxxmpp is a XMPP library written purely in Dart for usage in Moxxy.
|
moxxmpp is a XMPP library written purely in Dart for usage in Moxxy.
|
||||||
|
|
||||||
## Packages
|
## Packages
|
||||||
### moxxmpp
|
### [moxxmpp](./packages/moxxmpp)
|
||||||
|
|
||||||
This package contains the actual XMPP code that is platform-independent.
|
This package contains the actual XMPP code that is platform-independent.
|
||||||
|
|
||||||
### moxxmpp_socket
|
### [moxxmpp_socket_tcp](./packages/moxxmpp_socket_tcp)
|
||||||
|
|
||||||
`moxxmpp_socket` contains the implementation of the `BaseSocketWrapper` class that
|
`moxxmpp_socket_tcp` contains the implementation of the `BaseSocketWrapper` class that
|
||||||
implements the RFC6120 connection algorithm and XEP-0368 direct TLS connections,
|
implements the RFC6120 connection algorithm and XEP-0368 direct TLS connections,
|
||||||
if a DNS implementation is given, and supports StartTLS.
|
if a DNS implementation is given, and supports StartTLS.
|
||||||
|
|
||||||
@@ -25,3 +25,9 @@ the development shell provided by the NixOS Flake, ensure that `ANDROID_HOME` an
|
|||||||
## License
|
## License
|
||||||
|
|
||||||
See `./LICENSE`.
|
See `./LICENSE`.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
If you like what I do and you want to support me, feel free to donate to me on Ko-Fi.
|
||||||
|
|
||||||
|
[<img src="https://codeberg.org/moxxy/moxxyv2/raw/branch/master/assets/repo/kofi.png" height="36" style="height: 36px; border: 0px;"></img>](https://ko-fi.com/papatutuwawa)
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
CSINegotiator(),
|
CSINegotiator(),
|
||||||
RosterFeatureNegotiator(),
|
RosterFeatureNegotiator(),
|
||||||
SaslPlainNegotiator(),
|
SaslPlainNegotiator(),
|
||||||
|
SaslScramNegotiator(10, '', '', ScramHashType.sha512),
|
||||||
SaslScramNegotiator(9, '', '', ScramHashType.sha256),
|
SaslScramNegotiator(9, '', '', ScramHashType.sha256),
|
||||||
SaslScramNegotiator(8, '', '', ScramHashType.sha1),
|
SaslScramNegotiator(8, '', '', ScramHashType.sha1),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ dependencies:
|
|||||||
version: 0.1.4+1
|
version: 0.1.4+1
|
||||||
moxxmpp:
|
moxxmpp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: 0.1.2+2
|
version: 0.1.6+1
|
||||||
moxxmpp_socket_tcp:
|
moxxmpp_socket_tcp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: 0.1.2+2
|
version: 0.1.2+9
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
@@ -1,3 +1,34 @@
|
|||||||
|
## 0.1.6+1
|
||||||
|
|
||||||
|
- **FIX**: Fix LMC not working.
|
||||||
|
|
||||||
|
## 0.1.6
|
||||||
|
|
||||||
|
- **FEAT**: Implement XEP-0308.
|
||||||
|
|
||||||
|
## 0.1.5
|
||||||
|
|
||||||
|
- **FEAT**: Message events now contain the stanza error, if available.
|
||||||
|
|
||||||
|
## 0.1.4
|
||||||
|
|
||||||
|
- **FIX**: Only stanza-id required 'sid:0' support.
|
||||||
|
- **FEAT**: Implement parsing and sending of retractions.
|
||||||
|
|
||||||
|
## 0.1.3+1
|
||||||
|
|
||||||
|
- **FIX**: Expose the error classes.
|
||||||
|
|
||||||
|
## 0.1.3
|
||||||
|
|
||||||
|
- **REFACTOR**: Replace MayFail by Result.
|
||||||
|
- **FIX**: Remove the old Results API.
|
||||||
|
- **FEAT**: Rework how the negotiator system works.
|
||||||
|
|
||||||
|
## 0.1.2+3
|
||||||
|
|
||||||
|
- **FIX**: SASL SCRAM-SHA-{256,512} should now work.
|
||||||
|
|
||||||
## 0.1.2+2
|
## 0.1.2+2
|
||||||
|
|
||||||
- **FIX**: Fix reconnections when the connection is awaited.
|
- **FIX**: Fix reconnections when the connection is awaited.
|
||||||
|
|||||||
@@ -2,6 +2,22 @@
|
|||||||
|
|
||||||
A pure-Dart XMPP library written for Moxxy.
|
A pure-Dart XMPP library written for Moxxy.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Include the following as a dependency in your pubspec file:
|
||||||
|
|
||||||
|
```
|
||||||
|
moxxmpp:
|
||||||
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
version: 0.1.6+1
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
See `./LICENSE`.
|
See `./LICENSE`.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
If you like what I do and you want to support me, feel free to donate to me on Ko-Fi.
|
||||||
|
|
||||||
|
[<img src="https://codeberg.org/moxxy/moxxyv2/raw/branch/master/assets/repo/kofi.png" height="36" style="height: 36px; border: 0px;"></img>](https://ko-fi.com/papatutuwawa)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ void main() {
|
|||||||
});
|
});
|
||||||
final log = Logger('FailureReconnectionTest');
|
final log = Logger('FailureReconnectionTest');
|
||||||
|
|
||||||
test('Failing an awaited connection', () async {
|
test('Failing an awaited connection with TestingSleepReconnectionPolicy', () async {
|
||||||
var errors = 0;
|
var errors = 0;
|
||||||
final connection = XmppConnection(
|
final connection = XmppConnection(
|
||||||
TestingSleepReconnectionPolicy(10),
|
TestingSleepReconnectionPolicy(10),
|
||||||
@@ -52,4 +52,47 @@ void main() {
|
|||||||
await Future.delayed(const Duration(seconds: 20));
|
await Future.delayed(const Duration(seconds: 20));
|
||||||
expect(errors, 1);
|
expect(errors, 1);
|
||||||
}, timeout: Timeout.factor(2));
|
}, timeout: Timeout.factor(2));
|
||||||
|
|
||||||
|
test('Failing an awaited connection with ExponentialBackoffReconnectionPolicy', () async {
|
||||||
|
var errors = 0;
|
||||||
|
final connection = XmppConnection(
|
||||||
|
ExponentialBackoffReconnectionPolicy(1),
|
||||||
|
TCPSocketWrapper(false),
|
||||||
|
);
|
||||||
|
connection.registerFeatureNegotiators([
|
||||||
|
StartTlsNegotiator(),
|
||||||
|
]);
|
||||||
|
connection.registerManagers([
|
||||||
|
DiscoManager(),
|
||||||
|
RosterManager(),
|
||||||
|
PingManager(),
|
||||||
|
MessageManager(),
|
||||||
|
PresenceManager('http://moxxmpp.example'),
|
||||||
|
]);
|
||||||
|
connection.asBroadcastStream().listen((event) {
|
||||||
|
if (event is ConnectionStateChangedEvent) {
|
||||||
|
if (event.state == XmppConnectionState.error) {
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.setConnectionSettings(
|
||||||
|
ConnectionSettings(
|
||||||
|
jid: JID.fromString('testuser@no-sasl.badxmpp.eu'),
|
||||||
|
password: 'abc123',
|
||||||
|
useDirectTLS: true,
|
||||||
|
allowPlainAuth: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = await connection.connectAwaitable();
|
||||||
|
log.info('Connection failed as expected');
|
||||||
|
expect(result.success, false);
|
||||||
|
expect(errors, 1);
|
||||||
|
|
||||||
|
log.info('Waiting 20 seconds for unexpected reconnections');
|
||||||
|
await Future.delayed(const Duration(seconds: 20));
|
||||||
|
expect(errors, 1);
|
||||||
|
}, timeout: Timeout.factor(2));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
library moxxmpp;
|
library moxxmpp;
|
||||||
|
|
||||||
export 'package:moxxmpp/src/connection.dart';
|
export 'package:moxxmpp/src/connection.dart';
|
||||||
|
export 'package:moxxmpp/src/errors.dart';
|
||||||
export 'package:moxxmpp/src/events.dart';
|
export 'package:moxxmpp/src/events.dart';
|
||||||
export 'package:moxxmpp/src/iq.dart';
|
export 'package:moxxmpp/src/iq.dart';
|
||||||
export 'package:moxxmpp/src/jid.dart';
|
export 'package:moxxmpp/src/jid.dart';
|
||||||
@@ -16,6 +17,7 @@ export 'package:moxxmpp/src/negotiators/manager.dart';
|
|||||||
export 'package:moxxmpp/src/negotiators/namespaces.dart';
|
export 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/negotiator.dart';
|
export 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/resource_binding.dart';
|
export 'package:moxxmpp/src/negotiators/resource_binding.dart';
|
||||||
|
export 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/plain.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/plain.dart';
|
||||||
export 'package:moxxmpp/src/negotiators/sasl/scram.dart';
|
export 'package:moxxmpp/src/negotiators/sasl/scram.dart';
|
||||||
@@ -25,13 +27,14 @@ export 'package:moxxmpp/src/presence.dart';
|
|||||||
export 'package:moxxmpp/src/reconnect.dart';
|
export 'package:moxxmpp/src/reconnect.dart';
|
||||||
export 'package:moxxmpp/src/rfcs/rfc_2782.dart';
|
export 'package:moxxmpp/src/rfcs/rfc_2782.dart';
|
||||||
export 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
export 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
||||||
export 'package:moxxmpp/src/roster.dart';
|
export 'package:moxxmpp/src/roster/errors.dart';
|
||||||
|
export 'package:moxxmpp/src/roster/roster.dart';
|
||||||
|
export 'package:moxxmpp/src/roster/state.dart';
|
||||||
export 'package:moxxmpp/src/settings.dart';
|
export 'package:moxxmpp/src/settings.dart';
|
||||||
export 'package:moxxmpp/src/socket.dart';
|
export 'package:moxxmpp/src/socket.dart';
|
||||||
export 'package:moxxmpp/src/stanza.dart';
|
export 'package:moxxmpp/src/stanza.dart';
|
||||||
export 'package:moxxmpp/src/stringxml.dart';
|
export 'package:moxxmpp/src/stringxml.dart';
|
||||||
export 'package:moxxmpp/src/types/error.dart';
|
export 'package:moxxmpp/src/types/result.dart';
|
||||||
export 'package:moxxmpp/src/types/resultv2.dart';
|
|
||||||
export 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
export 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
||||||
export 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
|
export 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0004.dart';
|
export 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
@@ -57,11 +60,13 @@ export 'package:moxxmpp/src/xeps/xep_0203.dart';
|
|||||||
export 'package:moxxmpp/src/xeps/xep_0280.dart';
|
export 'package:moxxmpp/src/xeps/xep_0280.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0297.dart';
|
export 'package:moxxmpp/src/xeps/xep_0297.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0300.dart';
|
export 'package:moxxmpp/src/xeps/xep_0300.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0308.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0333.dart';
|
export 'package:moxxmpp/src/xeps/xep_0333.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0334.dart';
|
export 'package:moxxmpp/src/xeps/xep_0334.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0352.dart';
|
export 'package:moxxmpp/src/xeps/xep_0352.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0359.dart';
|
export 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0363.dart';
|
export 'package:moxxmpp/src/xeps/xep_0363/errors.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0363/xep_0363.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0380.dart';
|
export 'package:moxxmpp/src/xeps/xep_0380.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0384/errors.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/errors.dart';
|
||||||
@@ -70,7 +75,10 @@ export 'package:moxxmpp/src/xeps/xep_0384/types.dart';
|
|||||||
export 'package:moxxmpp/src/xeps/xep_0384/xep_0384.dart';
|
export 'package:moxxmpp/src/xeps/xep_0384/xep_0384.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0385.dart';
|
export 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0414.dart';
|
export 'package:moxxmpp/src/xeps/xep_0414.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0444.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0446.dart';
|
export 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0447.dart';
|
export 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0448.dart';
|
export 'package:moxxmpp/src/xeps/xep_0448.dart';
|
||||||
|
export 'package:moxxmpp/src/xeps/xep_0449.dart';
|
||||||
export 'package:moxxmpp/src/xeps/xep_0461.dart';
|
export 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moxlib/moxlib.dart';
|
||||||
import 'package:moxxmpp/src/buffer.dart';
|
import 'package:moxxmpp/src/buffer.dart';
|
||||||
|
import 'package:moxxmpp/src/errors.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/iq.dart';
|
import 'package:moxxmpp/src/iq.dart';
|
||||||
import 'package:moxxmpp/src/managers/attributes.dart';
|
import 'package:moxxmpp/src/managers/attributes.dart';
|
||||||
@@ -14,7 +16,7 @@ import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/presence.dart';
|
import 'package:moxxmpp/src/presence.dart';
|
||||||
import 'package:moxxmpp/src/reconnect.dart';
|
import 'package:moxxmpp/src/reconnect.dart';
|
||||||
import 'package:moxxmpp/src/roster.dart';
|
import 'package:moxxmpp/src/roster/roster.dart';
|
||||||
import 'package:moxxmpp/src/routing.dart';
|
import 'package:moxxmpp/src/routing.dart';
|
||||||
import 'package:moxxmpp/src/settings.dart';
|
import 'package:moxxmpp/src/settings.dart';
|
||||||
import 'package:moxxmpp/src/socket.dart';
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
@@ -27,22 +29,35 @@ import 'package:moxxmpp/src/xeps/xep_0352.dart';
|
|||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
/// The states the XmppConnection can be in
|
||||||
enum XmppConnectionState {
|
enum XmppConnectionState {
|
||||||
|
/// The XmppConnection instance is not connected to the server. This is either the
|
||||||
|
/// case before connecting or after disconnecting.
|
||||||
notConnected,
|
notConnected,
|
||||||
|
|
||||||
|
/// We are currently trying to connect to the server.
|
||||||
connecting,
|
connecting,
|
||||||
|
|
||||||
|
/// We are currently connected to the server.
|
||||||
connected,
|
connected,
|
||||||
|
|
||||||
|
/// We have received an unrecoverable error and the server killed the connection
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Metadata for [XmppConnection.sendStanza].
|
||||||
enum StanzaFromType {
|
enum StanzaFromType {
|
||||||
// Add the full JID to the stanza as the from attribute
|
/// Add the full JID to the stanza as the from attribute
|
||||||
full,
|
full,
|
||||||
// Add the bare JID to the stanza as the from attribute
|
|
||||||
|
/// Add the bare JID to the stanza as the from attribute
|
||||||
bare,
|
bare,
|
||||||
// Add no JID as the from attribute
|
|
||||||
none
|
/// Add no JID as the from attribute
|
||||||
|
none,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Nonza describing the XMPP stream header.
|
||||||
class StreamHeaderNonza extends XMLNode {
|
class StreamHeaderNonza extends XMLNode {
|
||||||
StreamHeaderNonza(String serverDomain) : super(
|
StreamHeaderNonza(String serverDomain) : super(
|
||||||
tag: 'stream:stream',
|
tag: 'stream:stream',
|
||||||
@@ -57,24 +72,48 @@ class StreamHeaderNonza extends XMLNode {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result of an awaited connection.
|
||||||
class XmppConnectionResult {
|
class XmppConnectionResult {
|
||||||
const XmppConnectionResult(
|
const XmppConnectionResult(
|
||||||
this.success,
|
this.success,
|
||||||
{
|
{
|
||||||
this.reason,
|
this.error,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// True if the connection was successful. False if it failed for any reason.
|
||||||
final bool success;
|
final bool success;
|
||||||
// NOTE: [reason] is not human-readable, but the type of SASL error.
|
|
||||||
// See sasl/errors.dart
|
// If a connection attempt fails, i.e. success is false, then this indicates the
|
||||||
final String? reason;
|
// reason the connection failed.
|
||||||
|
final XmppError? error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A surrogate key for awaiting stanzas.
|
||||||
|
@immutable
|
||||||
|
class _StanzaAwaitableData {
|
||||||
|
const _StanzaAwaitableData(this.sentTo, this.id);
|
||||||
|
|
||||||
|
/// The JID the original stanza was sent to. We expect the result to come from the
|
||||||
|
/// same JID.
|
||||||
|
final String sentTo;
|
||||||
|
|
||||||
|
/// The ID of the original stanza. We expect the result to have the same ID.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => sentTo.hashCode ^ id.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator==(Object other) {
|
||||||
|
return other is _StanzaAwaitableData &&
|
||||||
|
other.sentTo == sentTo &&
|
||||||
|
other.id == id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This class is a connection to the server.
|
||||||
class XmppConnection {
|
class XmppConnection {
|
||||||
/// [_socket] is for debugging purposes.
|
|
||||||
/// [connectionPingDuration] is the duration after which a ping will be sent to keep
|
|
||||||
/// the connection open. Defaults to 15 minutes.
|
|
||||||
XmppConnection(
|
XmppConnection(
|
||||||
ReconnectionPolicy reconnectionPolicy,
|
ReconnectionPolicy reconnectionPolicy,
|
||||||
this._socket,
|
this._socket,
|
||||||
@@ -82,25 +121,7 @@ class XmppConnection {
|
|||||||
this.connectionPingDuration = const Duration(minutes: 3),
|
this.connectionPingDuration = const Duration(minutes: 3),
|
||||||
this.connectingTimeout = const Duration(minutes: 2),
|
this.connectingTimeout = const Duration(minutes: 2),
|
||||||
}
|
}
|
||||||
) :
|
) : _reconnectionPolicy = reconnectionPolicy {
|
||||||
_connectionState = XmppConnectionState.notConnected,
|
|
||||||
_routingState = RoutingState.preConnection,
|
|
||||||
_eventStreamController = StreamController.broadcast(),
|
|
||||||
_resource = '',
|
|
||||||
_streamBuffer = XmlStreamBuffer(),
|
|
||||||
_uuid = const Uuid(),
|
|
||||||
_awaitingResponse = {},
|
|
||||||
_awaitingResponseLock = Lock(),
|
|
||||||
_xmppManagers = {},
|
|
||||||
_incomingStanzaHandlers = List.empty(growable: true),
|
|
||||||
_outgoingPreStanzaHandlers = List.empty(growable: true),
|
|
||||||
_outgoingPostStanzaHandlers = List.empty(growable: true),
|
|
||||||
_reconnectionPolicy = reconnectionPolicy,
|
|
||||||
_featureNegotiators = {},
|
|
||||||
_streamFeatures = List.empty(growable: true),
|
|
||||||
_negotiationLock = Lock(),
|
|
||||||
_isAuthenticated = false,
|
|
||||||
_log = Logger('XmppConnection') {
|
|
||||||
// Allow the reconnection policy to perform reconnections by itself
|
// Allow the reconnection policy to perform reconnections by itself
|
||||||
_reconnectionPolicy.register(
|
_reconnectionPolicy.register(
|
||||||
_attemptReconnection,
|
_attemptReconnection,
|
||||||
@@ -114,69 +135,101 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Connection properties
|
/// The state that the connection is currently in
|
||||||
///
|
XmppConnectionState _connectionState = XmppConnectionState.notConnected;
|
||||||
/// The state that the connection currently is in
|
|
||||||
XmppConnectionState _connectionState;
|
|
||||||
/// The socket that we are using for the connection and its data stream
|
/// The socket that we are using for the connection and its data stream
|
||||||
final BaseSocketWrapper _socket;
|
final BaseSocketWrapper _socket;
|
||||||
|
|
||||||
|
/// The data stream of the socket
|
||||||
late final Stream<String> _socketStream;
|
late final Stream<String> _socketStream;
|
||||||
/// Account settings
|
|
||||||
|
|
||||||
|
/// Connection settings
|
||||||
late ConnectionSettings _connectionSettings;
|
late ConnectionSettings _connectionSettings;
|
||||||
|
|
||||||
/// A policy on how to reconnect
|
/// A policy on how to reconnect
|
||||||
final ReconnectionPolicy _reconnectionPolicy;
|
final ReconnectionPolicy _reconnectionPolicy;
|
||||||
/// A list of stanzas we are tracking with its corresponding critical section
|
|
||||||
final Map<String, Completer<XMLNode>> _awaitingResponse;
|
/// A list of stanzas we are tracking with its corresponding critical section lock
|
||||||
final Lock _awaitingResponseLock;
|
final Map<_StanzaAwaitableData, Completer<XMLNode>> _awaitingResponse = {};
|
||||||
|
final Lock _awaitingResponseLock = Lock();
|
||||||
|
|
||||||
/// Helpers
|
/// Sorted list of handlers that we call or incoming and outgoing stanzas
|
||||||
///
|
final List<StanzaHandler> _incomingStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _incomingStanzaHandlers;
|
final List<StanzaHandler> _incomingPreStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _outgoingPreStanzaHandlers;
|
final List<StanzaHandler> _outgoingPreStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _outgoingPostStanzaHandlers;
|
final List<StanzaHandler> _outgoingPostStanzaHandlers = List.empty(growable: true);
|
||||||
final StreamController<XmppEvent> _eventStreamController;
|
final StreamController<XmppEvent> _eventStreamController = StreamController.broadcast();
|
||||||
final Map<String, XmppManagerBase> _xmppManagers;
|
final Map<String, XmppManagerBase> _xmppManagers = {};
|
||||||
|
|
||||||
/// Stream properties
|
|
||||||
///
|
|
||||||
/// Disco info we got after binding a resource (xmlns)
|
/// Disco info we got after binding a resource (xmlns)
|
||||||
final List<String> _serverFeatures = List.empty(growable: true);
|
final List<String> _serverFeatures = List.empty(growable: true);
|
||||||
|
|
||||||
/// The buffer object to keep split up stanzas together
|
/// The buffer object to keep split up stanzas together
|
||||||
final XmlStreamBuffer _streamBuffer;
|
final XmlStreamBuffer _streamBuffer = XmlStreamBuffer();
|
||||||
|
|
||||||
/// UUID object to generate stanza and origin IDs
|
/// UUID object to generate stanza and origin IDs
|
||||||
final Uuid _uuid;
|
final Uuid _uuid = const Uuid();
|
||||||
|
|
||||||
/// The time between sending a ping to keep the connection open
|
/// The time between sending a ping to keep the connection open
|
||||||
// TODO(Unknown): Only start the timer if we did not send a stanza after n seconds
|
// TODO(Unknown): Only start the timer if we did not send a stanza after n seconds
|
||||||
final Duration connectionPingDuration;
|
final Duration connectionPingDuration;
|
||||||
|
|
||||||
/// The time that we may spent in the "connecting" state
|
/// The time that we may spent in the "connecting" state
|
||||||
final Duration connectingTimeout;
|
final Duration connectingTimeout;
|
||||||
|
|
||||||
/// The current state of the connection handling state machine.
|
/// The current state of the connection handling state machine.
|
||||||
RoutingState _routingState;
|
RoutingState _routingState = RoutingState.preConnection;
|
||||||
|
|
||||||
/// The currently bound resource or '' if none has been bound yet.
|
/// The currently bound resource or '' if none has been bound yet.
|
||||||
String _resource;
|
String _resource = '';
|
||||||
|
|
||||||
/// True if we are authenticated. False if not.
|
/// True if we are authenticated. False if not.
|
||||||
bool _isAuthenticated;
|
bool _isAuthenticated = false;
|
||||||
|
|
||||||
/// Timer for the keep-alive ping.
|
/// Timer for the keep-alive ping.
|
||||||
Timer? _connectionPingTimer;
|
Timer? _connectionPingTimer;
|
||||||
|
|
||||||
/// Timer for the connecting timeout
|
/// Timer for the connecting timeout
|
||||||
Timer? _connectingTimeoutTimer;
|
Timer? _connectingTimeoutTimer;
|
||||||
|
|
||||||
/// Completers for certain actions
|
/// Completers for certain actions
|
||||||
// ignore: use_late_for_private_fields_and_variables
|
// ignore: use_late_for_private_fields_and_variables
|
||||||
Completer<XmppConnectionResult>? _connectionCompleter;
|
Completer<XmppConnectionResult>? _connectionCompleter;
|
||||||
/// Controls whether an XmppSocketClosureEvent triggers a reconnection.
|
|
||||||
bool _socketClosureTriggersReconnect = true;
|
|
||||||
|
|
||||||
/// Negotiators
|
/// Negotiators
|
||||||
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators;
|
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators = {};
|
||||||
XmppFeatureNegotiatorBase? _currentNegotiator;
|
XmppFeatureNegotiatorBase? _currentNegotiator;
|
||||||
final List<XMLNode> _streamFeatures;
|
final List<XMLNode> _streamFeatures = List.empty(growable: true);
|
||||||
/// Prevent data from being passed to _currentNegotiator.negotiator while the negotiator
|
/// Prevent data from being passed to _currentNegotiator.negotiator while the negotiator
|
||||||
/// is still running.
|
/// is still running.
|
||||||
final Lock _negotiationLock;
|
final Lock _negotiationLock = Lock();
|
||||||
|
|
||||||
/// Misc
|
/// The logger for the class
|
||||||
final Logger _log;
|
final Logger _log = Logger('XmppConnection');
|
||||||
|
|
||||||
|
/// A value indicating whether a connection attempt is currently running or not
|
||||||
|
bool _isConnectionRunning = false;
|
||||||
|
final Lock _connectionRunningLock = Lock();
|
||||||
|
|
||||||
|
/// Enters the critical section for accessing [XmppConnection._isConnectionRunning]
|
||||||
|
/// and does the following:
|
||||||
|
/// - if _isConnectionRunning is false, set it to true and return false.
|
||||||
|
/// - if _isConnectionRunning is true, return true.
|
||||||
|
Future<bool> _testAndSetIsConnectionRunning() async => _connectionRunningLock.synchronized(() {
|
||||||
|
if (!_isConnectionRunning) {
|
||||||
|
_isConnectionRunning = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Enters the critical section for accessing [XmppConnection._isConnectionRunning]
|
||||||
|
/// and sets it to false.
|
||||||
|
Future<void> _resetIsConnectionRunning() async => _connectionRunningLock.synchronized(() => _isConnectionRunning = false);
|
||||||
|
|
||||||
ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy;
|
ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy;
|
||||||
|
|
||||||
List<String> get serverFeatures => _serverFeatures;
|
List<String> get serverFeatures => _serverFeatures;
|
||||||
@@ -222,11 +275,13 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers());
|
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers());
|
||||||
|
_incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers());
|
||||||
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers());
|
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers());
|
||||||
_outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers());
|
_outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers());
|
||||||
|
|
||||||
if (sortHandlers) {
|
if (sortHandlers) {
|
||||||
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
|
_incomingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
}
|
}
|
||||||
@@ -330,6 +385,11 @@ class XmppConnection {
|
|||||||
|
|
||||||
/// Attempts to reconnect to the server by following an exponential backoff.
|
/// Attempts to reconnect to the server by following an exponential backoff.
|
||||||
Future<void> _attemptReconnection() async {
|
Future<void> _attemptReconnection() async {
|
||||||
|
if (await _testAndSetIsConnectionRunning()) {
|
||||||
|
_log.warning('_attemptReconnection is called but connection attempt is already running. Ignoring...');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_log.finest('_attemptReconnection: Setting state to notConnected');
|
_log.finest('_attemptReconnection: Setting state to notConnected');
|
||||||
await _setConnectionState(XmppConnectionState.notConnected);
|
await _setConnectionState(XmppConnectionState.notConnected);
|
||||||
_log.finest('_attemptReconnection: Done');
|
_log.finest('_attemptReconnection: Done');
|
||||||
@@ -345,12 +405,8 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Called when a stream ending error has occurred
|
/// Called when a stream ending error has occurred
|
||||||
Future<void> handleError(Object? error) async {
|
Future<void> handleError(XmppError error) async {
|
||||||
if (error != null) {
|
_log.severe('handleError called with ${error.toString()}');
|
||||||
_log.severe('handleError: $error');
|
|
||||||
} else {
|
|
||||||
_log.severe('handleError: Called with null');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Whenever we encounter an error that would trigger a reconnection attempt while
|
// Whenever we encounter an error that would trigger a reconnection attempt while
|
||||||
// the connection result is being awaited, don't attempt a reconnection but instead
|
// the connection result is being awaited, don't attempt a reconnection but instead
|
||||||
@@ -358,7 +414,12 @@ class XmppConnection {
|
|||||||
if (_connectionCompleter != null) {
|
if (_connectionCompleter != null) {
|
||||||
_log.info('Not triggering reconnection since connection result is being awaited');
|
_log.info('Not triggering reconnection since connection result is being awaited');
|
||||||
await _disconnect(triggeredByUser: false, state: XmppConnectionState.error);
|
await _disconnect(triggeredByUser: false, state: XmppConnectionState.error);
|
||||||
_connectionCompleter?.complete(const XmppConnectionResult(false));
|
_connectionCompleter?.complete(
|
||||||
|
XmppConnectionResult(
|
||||||
|
false,
|
||||||
|
error: error,
|
||||||
|
),
|
||||||
|
);
|
||||||
_connectionCompleter = null;
|
_connectionCompleter = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -370,11 +431,11 @@ class XmppConnection {
|
|||||||
/// Called whenever the socket creates an event
|
/// Called whenever the socket creates an event
|
||||||
Future<void> _handleSocketEvent(XmppSocketEvent event) async {
|
Future<void> _handleSocketEvent(XmppSocketEvent event) async {
|
||||||
if (event is XmppSocketErrorEvent) {
|
if (event is XmppSocketErrorEvent) {
|
||||||
await handleError(event.error);
|
await handleError(SocketError(event));
|
||||||
} else if (event is XmppSocketClosureEvent) {
|
} else if (event is XmppSocketClosureEvent) {
|
||||||
if (_socketClosureTriggersReconnect) {
|
if (!event.expected) {
|
||||||
_log.fine('Received XmppSocketClosureEvent. Reconnecting...');
|
_log.fine('Received unexpected XmppSocketClosureEvent. Reconnecting...');
|
||||||
await _reconnectionPolicy.onFailure();
|
await handleError(SocketError(XmppSocketErrorEvent(event)));
|
||||||
} else {
|
} else {
|
||||||
_log.fine('Received XmppSocketClosureEvent. No reconnection attempt since _socketClosureTriggersReconnect is false...');
|
_log.fine('Received XmppSocketClosureEvent. No reconnection attempt since _socketClosureTriggersReconnect is false...');
|
||||||
}
|
}
|
||||||
@@ -422,9 +483,10 @@ class XmppConnection {
|
|||||||
/// none.
|
/// none.
|
||||||
// TODO(Unknown): if addId = false, the function crashes.
|
// TODO(Unknown): if addId = false, the function crashes.
|
||||||
Future<XMLNode> sendStanza(Stanza stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false }) async {
|
Future<XMLNode> sendStanza(Stanza stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false }) async {
|
||||||
var stanza_ = stanza;
|
assert(implies(addId == false && stanza.id == null, !awaitable), 'Cannot await a stanza with no id');
|
||||||
|
|
||||||
// Add extra data in case it was not set
|
// Add extra data in case it was not set
|
||||||
|
var stanza_ = stanza;
|
||||||
if (addId && (stanza_.id == null || stanza_.id == '')) {
|
if (addId && (stanza_.id == null || stanza_.id == '')) {
|
||||||
stanza_ = stanza.copyWith(id: generateId());
|
stanza_ = stanza.copyWith(id: generateId());
|
||||||
}
|
}
|
||||||
@@ -442,8 +504,6 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final id = stanza_.id!;
|
|
||||||
|
|
||||||
_log.fine('Running pre stanza handlers..');
|
_log.fine('Running pre stanza handlers..');
|
||||||
final data = await _runOutgoingPreStanzaHandlers(
|
final data = await _runOutgoingPreStanzaHandlers(
|
||||||
stanza_,
|
stanza_,
|
||||||
@@ -481,42 +541,45 @@ class XmppConnection {
|
|||||||
final stanzaString = data.stanza.toXml();
|
final stanzaString = data.stanza.toXml();
|
||||||
|
|
||||||
// ignore: cascade_invocations
|
// ignore: cascade_invocations
|
||||||
_log.fine('Attempting to acquire lock for $id...');
|
_log.fine('Attempting to acquire lock for ${data.stanza.id}...');
|
||||||
// TODO(PapaTutuWawa): Handle this much more graceful
|
// TODO(PapaTutuWawa): Handle this much more graceful
|
||||||
var future = Future.value(XMLNode(tag: 'not-used'));
|
var future = Future.value(XMLNode(tag: 'not-used'));
|
||||||
await _awaitingResponseLock.synchronized(() async {
|
await _awaitingResponseLock.synchronized(() async {
|
||||||
_log.fine('Lock acquired for $id');
|
_log.fine('Lock acquired for ${data.stanza.id}');
|
||||||
if (awaitable) {
|
|
||||||
_awaitingResponse[id] = Completer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This uses the StreamManager to behave like a send queue
|
_StanzaAwaitableData? key;
|
||||||
if (await _canSendData()) {
|
if (awaitable) {
|
||||||
_socket.write(stanzaString);
|
key = _StanzaAwaitableData(data.stanza.to!, data.stanza.id!);
|
||||||
|
_awaitingResponse[key] = Completer();
|
||||||
|
}
|
||||||
|
|
||||||
// Try to ack every stanza
|
// This uses the StreamManager to behave like a send queue
|
||||||
// NOTE: Here we have send an Ack request nonza. This is now done by StreamManagementManager when receiving the StanzaSentEvent
|
if (await _canSendData()) {
|
||||||
} else {
|
_socket.write(stanzaString);
|
||||||
_log.fine('_canSendData() returned false.');
|
|
||||||
}
|
|
||||||
|
|
||||||
_log.fine('Running post stanza handlers..');
|
// Try to ack every stanza
|
||||||
await _runOutgoingPostStanzaHandlers(
|
// NOTE: Here we have send an Ack request nonza. This is now done by StreamManagementManager when receiving the StanzaSentEvent
|
||||||
|
} else {
|
||||||
|
_log.fine('_canSendData() returned false.');
|
||||||
|
}
|
||||||
|
|
||||||
|
_log.fine('Running post stanza handlers..');
|
||||||
|
await _runOutgoingPostStanzaHandlers(
|
||||||
|
stanza_,
|
||||||
|
initial: StanzaHandlerData(
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
stanza_,
|
stanza_,
|
||||||
initial: StanzaHandlerData(
|
),
|
||||||
false,
|
);
|
||||||
false,
|
_log.fine('Done');
|
||||||
null,
|
|
||||||
stanza_,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_log.fine('Done');
|
|
||||||
|
|
||||||
if (awaitable) {
|
if (awaitable) {
|
||||||
future = _awaitingResponse[id]!.future;
|
future = _awaitingResponse[key]!.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Releasing lock for $id');
|
_log.fine('Releasing lock for ${data.stanza.id}');
|
||||||
});
|
});
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
@@ -525,7 +588,7 @@ class XmppConnection {
|
|||||||
/// Called when we timeout during connecting
|
/// Called when we timeout during connecting
|
||||||
Future<void> _onConnectingTimeout() async {
|
Future<void> _onConnectingTimeout() async {
|
||||||
_log.severe('Connection stuck in "connecting". Causing a reconnection...');
|
_log.severe('Connection stuck in "connecting". Causing a reconnection...');
|
||||||
await handleError('Connecting timeout');
|
await handleError(TimeoutError());
|
||||||
}
|
}
|
||||||
|
|
||||||
void _destroyConnectingTimer() {
|
void _destroyConnectingTimer() {
|
||||||
@@ -628,8 +691,12 @@ class XmppConnection {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<StanzaHandlerData> _runIncomingStanzaHandlers(Stanza stanza) async {
|
Future<StanzaHandlerData> _runIncomingStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
|
||||||
return _runStanzaHandlers(_incomingStanzaHandlers, stanza);
|
return _runStanzaHandlers(_incomingStanzaHandlers, stanza, initial: initial);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _runIncomingPreStanzaHandlers(Stanza stanza) async {
|
||||||
|
return _runStanzaHandlers(_incomingPreStanzaHandlers, stanza);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<StanzaHandlerData> _runOutgoingPreStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
|
Future<StanzaHandlerData> _runOutgoingPreStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
|
||||||
@@ -671,20 +738,24 @@ class XmppConnection {
|
|||||||
|
|
||||||
// Run the incoming stanza handlers and bounce with an error if no manager handled
|
// Run the incoming stanza handlers and bounce with an error if no manager handled
|
||||||
// it.
|
// it.
|
||||||
final incomingHandlers = await _runIncomingStanzaHandlers(stanza);
|
final incomingPreHandlers = await _runIncomingPreStanzaHandlers(stanza);
|
||||||
final prefix = incomingHandlers.encrypted ?
|
final prefix = incomingPreHandlers.encrypted && incomingPreHandlers.other['encryption_error'] == null ?
|
||||||
'(Encrypted) ' :
|
'(Encrypted) ' :
|
||||||
'';
|
'';
|
||||||
_log.finest('<== $prefix${incomingHandlers.stanza.toXml()}');
|
_log.finest('<== $prefix${incomingPreHandlers.stanza.toXml()}');
|
||||||
|
|
||||||
// See if we are waiting for this stanza
|
// See if we are waiting for this stanza
|
||||||
final id = stanza.attributes['id'] as String?;
|
final id = incomingPreHandlers.stanza.attributes['id'] as String?;
|
||||||
var awaited = false;
|
var awaited = false;
|
||||||
await _awaitingResponseLock.synchronized(() async {
|
await _awaitingResponseLock.synchronized(() async {
|
||||||
if (id != null && _awaitingResponse.containsKey(id)) {
|
if (id != null && incomingPreHandlers.stanza.from != null) {
|
||||||
_awaitingResponse[id]!.complete(incomingHandlers.stanza);
|
final key = _StanzaAwaitableData(incomingPreHandlers.stanza.from!, id);
|
||||||
_awaitingResponse.remove(id);
|
final comp = _awaitingResponse[key];
|
||||||
awaited = true;
|
if (comp != null) {
|
||||||
|
comp.complete(incomingPreHandlers.stanza);
|
||||||
|
_awaitingResponse.remove(key);
|
||||||
|
awaited = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -693,8 +764,19 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Only bounce if the stanza has neither been awaited, nor handled.
|
// Only bounce if the stanza has neither been awaited, nor handled.
|
||||||
|
final incomingHandlers = await _runIncomingStanzaHandlers(
|
||||||
|
incomingPreHandlers.stanza,
|
||||||
|
initial: StanzaHandlerData(
|
||||||
|
false,
|
||||||
|
incomingPreHandlers.cancel,
|
||||||
|
incomingPreHandlers.cancelReason,
|
||||||
|
incomingPreHandlers.stanza,
|
||||||
|
encrypted: incomingPreHandlers.encrypted,
|
||||||
|
other: incomingPreHandlers.other,
|
||||||
|
),
|
||||||
|
);
|
||||||
if (!incomingHandlers.done) {
|
if (!incomingHandlers.done) {
|
||||||
handleUnhandledStanza(this, stanza);
|
handleUnhandledStanza(this, incomingPreHandlers.stanza);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -740,6 +822,7 @@ class XmppConnection {
|
|||||||
/// a disco sweep among other things.
|
/// a disco sweep among other things.
|
||||||
Future<void> _onNegotiationsDone() async {
|
Future<void> _onNegotiationsDone() async {
|
||||||
// Set the connection state
|
// Set the connection state
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await _setConnectionState(XmppConnectionState.connected);
|
await _setConnectionState(XmppConnectionState.connected);
|
||||||
|
|
||||||
// Resolve the connection completion future
|
// Resolve the connection completion future
|
||||||
@@ -749,20 +832,35 @@ class XmppConnection {
|
|||||||
// Send out initial presence
|
// Send out initial presence
|
||||||
await getPresenceManager().sendInitialPresence();
|
await getPresenceManager().sendInitialPresence();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To be called after _currentNegotiator!.negotiate(..) has been called. Checks the
|
|
||||||
/// state of the negotiator and picks the next negotiatior, ends negotiation or
|
|
||||||
/// waits, depending on what the negotiator did.
|
|
||||||
Future<void> _checkCurrentNegotiator() async {
|
|
||||||
if (_currentNegotiator!.state == NegotiatorState.done) {
|
|
||||||
_log.finest('Negotiator ${_currentNegotiator!.id} done');
|
|
||||||
|
|
||||||
|
Future<void> _executeCurrentNegotiator(XMLNode nonza) async {
|
||||||
|
// If we don't have a negotiator get one
|
||||||
|
_currentNegotiator ??= getNextNegotiator(_streamFeatures);
|
||||||
|
if (_currentNegotiator == null && _isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
|
_log.finest('Negotiations done!');
|
||||||
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _onNegotiationsDone();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = await _currentNegotiator!.negotiate(nonza);
|
||||||
|
if (result.isType<NegotiatorError>()) {
|
||||||
|
_log.severe('Negotiator returned an error');
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
|
await handleError(result.get<NegotiatorError>());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final state = result.get<NegotiatorState>();
|
||||||
|
_currentNegotiator!.state = state;
|
||||||
|
switch (state) {
|
||||||
|
case NegotiatorState.ready: return;
|
||||||
|
case NegotiatorState.done:
|
||||||
if (_currentNegotiator!.sendStreamHeaderWhenDone) {
|
if (_currentNegotiator!.sendStreamHeaderWhenDone) {
|
||||||
_currentNegotiator = null;
|
_currentNegotiator = null;
|
||||||
_streamFeatures.clear();
|
_streamFeatures.clear();
|
||||||
_sendStreamHeader();
|
_sendStreamHeader();
|
||||||
} else {
|
} else {
|
||||||
// Track what features we still have
|
|
||||||
_streamFeatures
|
_streamFeatures
|
||||||
.removeWhere((node) {
|
.removeWhere((node) {
|
||||||
return node.attributes['xmlns'] == _currentNegotiator!.negotiatingXmlns;
|
return node.attributes['xmlns'] == _currentNegotiator!.negotiatingXmlns;
|
||||||
@@ -772,7 +870,8 @@ class XmppConnection {
|
|||||||
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
_log.finest('Negotiations done!');
|
_log.finest('Negotiations done!');
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _reconnectionPolicy.onSuccess();
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else {
|
} else {
|
||||||
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
||||||
@@ -782,19 +881,22 @@ class XmppConnection {
|
|||||||
tag: 'stream:features',
|
tag: 'stream:features',
|
||||||
children: _streamFeatures,
|
children: _streamFeatures,
|
||||||
);
|
);
|
||||||
await _currentNegotiator!.negotiate(fakeStanza);
|
|
||||||
await _checkCurrentNegotiator();
|
await _executeCurrentNegotiator(fakeStanza);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.retryLater) {
|
break;
|
||||||
|
case NegotiatorState.retryLater:
|
||||||
_log.finest('Negotiator wants to continue later. Picking new one...');
|
_log.finest('Negotiator wants to continue later. Picking new one...');
|
||||||
|
|
||||||
_currentNegotiator!.state = NegotiatorState.ready;
|
_currentNegotiator!.state = NegotiatorState.ready;
|
||||||
|
|
||||||
|
|
||||||
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
|
||||||
_log.finest('Negotiations done!');
|
_log.finest('Negotiations done!');
|
||||||
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _reconnectionPolicy.onSuccess();
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else {
|
} else {
|
||||||
_log.finest('Picking new negotiator...');
|
_log.finest('Picking new negotiator...');
|
||||||
@@ -804,24 +906,20 @@ class XmppConnection {
|
|||||||
tag: 'stream:features',
|
tag: 'stream:features',
|
||||||
children: _streamFeatures,
|
children: _streamFeatures,
|
||||||
);
|
);
|
||||||
await _currentNegotiator!.negotiate(fakeStanza);
|
await _executeCurrentNegotiator(fakeStanza);
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
}
|
}
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.skipRest) {
|
break;
|
||||||
|
case NegotiatorState.skipRest:
|
||||||
_log.finest('Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!');
|
_log.finest('Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!');
|
||||||
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _reconnectionPolicy.onSuccess();
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else if (_currentNegotiator!.state == NegotiatorState.error) {
|
break;
|
||||||
_log.severe('Negotiator returned an error');
|
|
||||||
await handleError(null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _closeSocket() {
|
|
||||||
_socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Called whenever we receive data that has been parsed as XML.
|
/// Called whenever we receive data that has been parsed as XML.
|
||||||
Future<void> handleXmlStream(XMLNode node) async {
|
Future<void> handleXmlStream(XMLNode node) async {
|
||||||
// Check if we received a stream error
|
// Check if we received a stream error
|
||||||
@@ -829,7 +927,7 @@ class XmppConnection {
|
|||||||
_log
|
_log
|
||||||
..finest('<== ${node.toXml()}')
|
..finest('<== ${node.toXml()}')
|
||||||
..severe('Received a stream error! Attempting reconnection');
|
..severe('Received a stream error! Attempting reconnection');
|
||||||
await handleError('Stream error');
|
await handleError(StreamError());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -849,53 +947,14 @@ class XmppConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_currentNegotiator != null) {
|
if (node.tag == 'stream:features') {
|
||||||
// If we already have a negotiator, just let it do its thing
|
// Store the received stream features
|
||||||
_log.finest('Negotiator currently active...');
|
|
||||||
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
} else {
|
|
||||||
_streamFeatures
|
_streamFeatures
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(node.children);
|
..addAll(node.children);
|
||||||
|
|
||||||
// We need to pick a new one
|
|
||||||
if (_isMandatoryNegotiationDone(node.children)) {
|
|
||||||
// Mandatory features are done but can we still negotiate more?
|
|
||||||
if (_isNegotiationPossible(node.children)) {// We can still negotiate features, so do that.
|
|
||||||
_log.finest('All required stream features done! Continuing negotiation');
|
|
||||||
_currentNegotiator = getNextNegotiator(node.children);
|
|
||||||
_log.finest('Chose $_currentNegotiator as next negotiator');
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
} else {
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// There still are mandatory features
|
|
||||||
if (!_isNegotiationPossible(node.children)) {
|
|
||||||
_log.severe('Mandatory negotiations not done but continuation not possible');
|
|
||||||
_updateRoutingState(RoutingState.error);
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
|
||||||
|
|
||||||
// Resolve the connection completion future
|
|
||||||
_connectionCompleter?.complete(
|
|
||||||
const XmppConnectionResult(
|
|
||||||
false,
|
|
||||||
reason: 'Could not complete connection negotiations',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_connectionCompleter = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_currentNegotiator = getNextNegotiator(node.children);
|
|
||||||
_log.finest('Chose $_currentNegotiator as next negotiator');
|
|
||||||
await _currentNegotiator!.negotiate(node);
|
|
||||||
await _checkCurrentNegotiator();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await _executeCurrentNegotiator(node);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case RoutingState.handleStanzas:
|
case RoutingState.handleStanzas:
|
||||||
@@ -927,20 +986,6 @@ class XmppConnection {
|
|||||||
} else if (event is AuthenticationSuccessEvent) {
|
} else if (event is AuthenticationSuccessEvent) {
|
||||||
_log.finest('Received AuthenticationSuccessEvent. Setting _isAuthenticated to true');
|
_log.finest('Received AuthenticationSuccessEvent. Setting _isAuthenticated to true');
|
||||||
_isAuthenticated = true;
|
_isAuthenticated = true;
|
||||||
} else if (event is AuthenticationFailedEvent) {
|
|
||||||
_log.finest('Failed authentication');
|
|
||||||
_updateRoutingState(RoutingState.error);
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
|
||||||
|
|
||||||
// Resolve the connection completion future
|
|
||||||
_connectionCompleter?.complete(
|
|
||||||
XmppConnectionResult(
|
|
||||||
false,
|
|
||||||
reason: 'Authentication failed: ${event.saslError}',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_connectionCompleter = null;
|
|
||||||
_closeSocket();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final manager in _xmppManagers.values) {
|
for (final manager in _xmppManagers.values) {
|
||||||
@@ -968,9 +1013,10 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// To be called when we lost the network connection.
|
/// To be called when we lost the network connection.
|
||||||
void _onNetworkConnectionLost() {
|
Future<void> _onNetworkConnectionLost() async {
|
||||||
_socket.close();
|
_socket.close();
|
||||||
_setConnectionState(XmppConnectionState.notConnected);
|
await _resetIsConnectionRunning();
|
||||||
|
await _setConnectionState(XmppConnectionState.notConnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to gracefully close the session
|
/// Attempt to gracefully close the session
|
||||||
@@ -980,7 +1026,6 @@ class XmppConnection {
|
|||||||
|
|
||||||
Future<void> _disconnect({required XmppConnectionState state, bool triggeredByUser = true}) async {
|
Future<void> _disconnect({required XmppConnectionState state, bool triggeredByUser = true}) async {
|
||||||
_reconnectionPolicy.setShouldReconnect(false);
|
_reconnectionPolicy.setShouldReconnect(false);
|
||||||
_socketClosureTriggersReconnect = false;
|
|
||||||
|
|
||||||
if (triggeredByUser) {
|
if (triggeredByUser) {
|
||||||
getPresenceManager().sendUnavailablePresence();
|
getPresenceManager().sendUnavailablePresence();
|
||||||
@@ -1011,11 +1056,12 @@ class XmppConnection {
|
|||||||
|
|
||||||
/// Like [connect] but the Future resolves when the resource binding is either done or
|
/// Like [connect] but the Future resolves when the resource binding is either done or
|
||||||
/// SASL has failed.
|
/// SASL has failed.
|
||||||
Future<XmppConnectionResult> connectAwaitable({ String? lastResource }) {
|
Future<XmppConnectionResult> connectAwaitable({ String? lastResource }) async {
|
||||||
_runPreConnectionAssertions();
|
_runPreConnectionAssertions();
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
_connectionCompleter = Completer();
|
_connectionCompleter = Completer();
|
||||||
_log.finest('Calling connect() from connectAwaitable');
|
_log.finest('Calling connect() from connectAwaitable');
|
||||||
connect(lastResource: lastResource);
|
await connect(lastResource: lastResource);
|
||||||
return _connectionCompleter!.future;
|
return _connectionCompleter!.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1027,6 +1073,7 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_runPreConnectionAssertions();
|
_runPreConnectionAssertions();
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
_reconnectionPolicy.setShouldReconnect(true);
|
_reconnectionPolicy.setShouldReconnect(true);
|
||||||
|
|
||||||
if (lastResource != null) {
|
if (lastResource != null) {
|
||||||
@@ -1034,7 +1081,6 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _reconnectionPolicy.reset();
|
await _reconnectionPolicy.reset();
|
||||||
_socketClosureTriggersReconnect = true;
|
|
||||||
await _sendEvent(ConnectingEvent());
|
await _sendEvent(ConnectingEvent());
|
||||||
|
|
||||||
final smManager = getStreamManagementManager();
|
final smManager = getStreamManagementManager();
|
||||||
@@ -1053,7 +1099,7 @@ class XmppConnection {
|
|||||||
port: port,
|
port: port,
|
||||||
);
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
await handleError(null);
|
await handleError(NoConnectionError());
|
||||||
} else {
|
} else {
|
||||||
await _reconnectionPolicy.onSuccess();
|
await _reconnectionPolicy.onSuccess();
|
||||||
_log.fine('Preparing the internal state for a connection attempt');
|
_log.fine('Preparing the internal state for a connection attempt');
|
||||||
|
|||||||
20
packages/moxxmpp/lib/src/errors.dart
Normal file
20
packages/moxxmpp/lib/src/errors.dart
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
|
|
||||||
|
/// An internal error class
|
||||||
|
abstract class XmppError {}
|
||||||
|
|
||||||
|
/// Returned if we could not establish a TCP connection
|
||||||
|
/// to the server.
|
||||||
|
class NoConnectionError extends XmppError {}
|
||||||
|
|
||||||
|
/// Returned if a socket error occured
|
||||||
|
class SocketError extends XmppError {
|
||||||
|
SocketError(this.event);
|
||||||
|
final XmppSocketErrorEvent event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returned if we time out
|
||||||
|
class TimeoutError extends XmppError {}
|
||||||
|
|
||||||
|
/// Returned if we received a stream error
|
||||||
|
class StreamError extends XmppError {}
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
import 'package:moxxmpp/src/connection.dart';
|
import 'package:moxxmpp/src/connection.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
import 'package:moxxmpp/src/managers/data.dart';
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/roster/roster.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0066.dart';
|
import 'package:moxxmpp/src/xeps/xep_0066.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0334.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0444.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
@@ -50,6 +54,22 @@ class StreamResumedEvent extends XmppEvent {
|
|||||||
/// Triggered when stream resumption failed
|
/// Triggered when stream resumption failed
|
||||||
class StreamResumeFailedEvent extends XmppEvent {}
|
class StreamResumeFailedEvent extends XmppEvent {}
|
||||||
|
|
||||||
|
/// Triggered when the roster has been modified
|
||||||
|
class RosterUpdatedEvent extends XmppEvent {
|
||||||
|
RosterUpdatedEvent(this.removed, this.modified, this.added);
|
||||||
|
|
||||||
|
/// A list of bare JIDs that are removed from the roster
|
||||||
|
final List<String> removed;
|
||||||
|
|
||||||
|
/// A list of XmppRosterItems that are modified. Can be correlated with one's cache
|
||||||
|
/// using the jid attribute.
|
||||||
|
final List<XmppRosterItem> modified;
|
||||||
|
|
||||||
|
/// A list of XmppRosterItems that are added to the roster.
|
||||||
|
final List<XmppRosterItem> added;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Triggered when a message is received
|
||||||
class MessageEvent extends XmppEvent {
|
class MessageEvent extends XmppEvent {
|
||||||
MessageEvent({
|
MessageEvent({
|
||||||
required this.body,
|
required this.body,
|
||||||
@@ -62,6 +82,7 @@ class MessageEvent extends XmppEvent {
|
|||||||
required this.isMarkable,
|
required this.isMarkable,
|
||||||
required this.encrypted,
|
required this.encrypted,
|
||||||
required this.other,
|
required this.other,
|
||||||
|
this.error,
|
||||||
this.type,
|
this.type,
|
||||||
this.oob,
|
this.oob,
|
||||||
this.sfs,
|
this.sfs,
|
||||||
@@ -71,7 +92,13 @@ class MessageEvent extends XmppEvent {
|
|||||||
this.fun,
|
this.fun,
|
||||||
this.funReplacement,
|
this.funReplacement,
|
||||||
this.funCancellation,
|
this.funCancellation,
|
||||||
|
this.messageRetraction,
|
||||||
|
this.messageCorrectionId,
|
||||||
|
this.messageReactions,
|
||||||
|
this.messageProcessingHints,
|
||||||
|
this.stickerPackId,
|
||||||
});
|
});
|
||||||
|
final StanzaError? error;
|
||||||
final String body;
|
final String body;
|
||||||
final JID fromJid;
|
final JID fromJid;
|
||||||
final JID toJid;
|
final JID toJid;
|
||||||
@@ -90,6 +117,11 @@ class MessageEvent extends XmppEvent {
|
|||||||
final String? funReplacement;
|
final String? funReplacement;
|
||||||
final String? funCancellation;
|
final String? funCancellation;
|
||||||
final bool encrypted;
|
final bool encrypted;
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
|
final String? messageCorrectionId;
|
||||||
|
final MessageReactions? messageReactions;
|
||||||
|
final List<MessageProcessingHint>? messageProcessingHints;
|
||||||
|
final String? stickerPackId;
|
||||||
final Map<String, dynamic> other;
|
final Map<String, dynamic> other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +1,72 @@
|
|||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
/// Represents a Jabber ID in parsed form.
|
||||||
@immutable
|
@immutable
|
||||||
class JID {
|
class JID {
|
||||||
|
|
||||||
const JID(this.local, this.domain, this.resource);
|
const JID(this.local, this.domain, this.resource);
|
||||||
|
|
||||||
|
/// Parses the string [jid] into a JID instance.
|
||||||
factory JID.fromString(String jid) {
|
factory JID.fromString(String jid) {
|
||||||
// 0: Parsing either the local or domain part
|
// Algorithm taken from here: https://blog.samwhited.com/2021/02/xmpp-addresses/
|
||||||
// 1: Parsing the domain part
|
var localPart = '';
|
||||||
// 2: Parsing the resource
|
var domainPart = '';
|
||||||
var state = 0;
|
var resourcePart = '';
|
||||||
var buffer = '';
|
|
||||||
var local_ = '';
|
|
||||||
var domain_ = '';
|
|
||||||
var resource_ = '';
|
|
||||||
|
|
||||||
for (var i = 0; i < jid.length; i++) {
|
|
||||||
final c = jid[i];
|
|
||||||
final eol = i == jid.length - 1;
|
|
||||||
|
|
||||||
switch (state) {
|
|
||||||
case 0: {
|
|
||||||
if (c == '@') {
|
|
||||||
local_ = buffer;
|
|
||||||
buffer = '';
|
|
||||||
state = 1;
|
|
||||||
} else if (c == '/') {
|
|
||||||
domain_ = buffer;
|
|
||||||
buffer = '';
|
|
||||||
state = 2;
|
|
||||||
} else if (eol) {
|
|
||||||
domain_ = buffer + c;
|
|
||||||
} else {
|
|
||||||
buffer += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1: {
|
|
||||||
if (c == '/') {
|
|
||||||
domain_ = buffer;
|
|
||||||
buffer = '';
|
|
||||||
state = 2;
|
|
||||||
} else if (eol) {
|
|
||||||
domain_ = buffer;
|
|
||||||
|
|
||||||
if (c != ' ') {
|
final slashParts = jid.split('/');
|
||||||
domain_ = domain_ + c;
|
if (slashParts.length == 1) {
|
||||||
}
|
resourcePart = '';
|
||||||
} else if (c != ' ') {
|
} else {
|
||||||
buffer += c;
|
resourcePart = slashParts.sublist(1).join('/');
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2: {
|
|
||||||
if (eol) {
|
|
||||||
resource_ = buffer;
|
|
||||||
|
|
||||||
if (c != ' ') {
|
assert(resourcePart.isNotEmpty, 'Resource part cannot be there and empty');
|
||||||
resource_ = resource_ + c;
|
|
||||||
}
|
|
||||||
} else if (c != ''){
|
|
||||||
buffer += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return JID(local_, domain_, resource_);
|
final atParts = slashParts.first.split('@');
|
||||||
|
if (atParts.length == 1) {
|
||||||
|
localPart = '';
|
||||||
|
domainPart = atParts.first;
|
||||||
|
} else {
|
||||||
|
localPart = atParts.first;
|
||||||
|
domainPart = atParts.sublist(1).join('@');
|
||||||
|
|
||||||
|
assert(localPart.isNotEmpty, 'Local part cannot be there and empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
return JID(
|
||||||
|
localPart,
|
||||||
|
domainPart.endsWith('.') ?
|
||||||
|
domainPart.substring(0, domainPart.length - 1) :
|
||||||
|
domainPart,
|
||||||
|
resourcePart,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
final String local;
|
final String local;
|
||||||
final String domain;
|
final String domain;
|
||||||
final String resource;
|
final String resource;
|
||||||
|
|
||||||
|
/// Returns true if the JID is bare.
|
||||||
bool isBare() => resource.isEmpty;
|
bool isBare() => resource.isEmpty;
|
||||||
|
|
||||||
|
/// Returns true if the JID is full.
|
||||||
bool isFull() => resource.isNotEmpty;
|
bool isFull() => resource.isNotEmpty;
|
||||||
|
|
||||||
|
/// Converts the JID into a bare JID.
|
||||||
JID toBare() => JID(local, domain, '');
|
JID toBare() => JID(local, domain, '');
|
||||||
|
|
||||||
|
/// Converts the JID into one with a resource part of [resource].
|
||||||
JID withResource(String resource) => JID(local, domain, resource);
|
JID withResource(String resource) => JID(local, domain, resource);
|
||||||
|
|
||||||
|
/// Compares the JID with [other]. This function assumes that JID and [other]
|
||||||
|
/// are bare, i.e. only the domain- and localparts are compared. If [ensureBare]
|
||||||
|
/// is optionally set to true, then [other] MUST be bare. Otherwise, false is returned.
|
||||||
|
bool bareCompare(JID other, { bool ensureBare = false }) {
|
||||||
|
if (ensureBare && !other.isBare()) return false;
|
||||||
|
|
||||||
|
return local == other.local && domain == other.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts to JID instance into its string representation of
|
||||||
|
/// localpart@domainpart/resource.
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
var result = '';
|
var result = '';
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:moxxmpp/src/connection.dart';
|
import 'package:moxxmpp/src/connection.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
@@ -11,7 +10,6 @@ import 'package:moxxmpp/src/stanza.dart';
|
|||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
class XmppManagerAttributes {
|
class XmppManagerAttributes {
|
||||||
|
|
||||||
XmppManagerAttributes({
|
XmppManagerAttributes({
|
||||||
required this.sendStanza,
|
required this.sendStanza,
|
||||||
required this.sendNonza,
|
required this.sendNonza,
|
||||||
|
|||||||
@@ -21,18 +21,28 @@ abstract class XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
||||||
/// send. These are run before the stanza is sent.
|
/// send. These are run before the stanza is sent. The higher the value of the
|
||||||
|
/// handler's priority, the earlier it is run.
|
||||||
List<StanzaHandler> getOutgoingPreStanzaHandlers() => [];
|
List<StanzaHandler> getOutgoingPreStanzaHandlers() => [];
|
||||||
|
|
||||||
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
||||||
/// send. These are run after the stanza is sent.
|
/// send. These are run after the stanza is sent. The higher the value of the
|
||||||
|
/// handler's priority, the earlier it is run.
|
||||||
List<StanzaHandler> getOutgoingPostStanzaHandlers() => [];
|
List<StanzaHandler> getOutgoingPostStanzaHandlers() => [];
|
||||||
|
|
||||||
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
/// Return the StanzaHandlers associated with this manager that deal with stanzas we
|
||||||
/// receive.
|
/// receive. The higher the value of the
|
||||||
|
/// handler's priority, the earlier it is run.
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [];
|
List<StanzaHandler> getIncomingStanzaHandlers() => [];
|
||||||
|
|
||||||
|
/// Return the StanzaHandlers associated with this manager that deal with stanza handlers
|
||||||
|
/// that have to run before the main ones run. This is useful, for example, for OMEMO
|
||||||
|
/// as we have to decrypt the stanza before we do anything else. The higher the value
|
||||||
|
/// of the handler's priority, the earlier it is run.
|
||||||
|
List<StanzaHandler> getIncomingPreStanzaHandlers() => [];
|
||||||
|
|
||||||
/// Return the NonzaHandlers associated with this manager.
|
/// Return the NonzaHandlers associated with this manager. The higher the value of the
|
||||||
|
/// handler's priority, the earlier it is run.
|
||||||
List<NonzaHandler> getNonzaHandlers() => [];
|
List<NonzaHandler> getNonzaHandlers() => [];
|
||||||
|
|
||||||
/// Return a list of features that should be included in a disco response.
|
/// Return a list of features that should be included in a disco response.
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import 'package:moxxmpp/src/xeps/xep_0203.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
import 'package:moxxmpp/src/xeps/xep_0385.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0444.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
@@ -55,6 +57,15 @@ class StanzaHandlerData with _$StanzaHandlerData {
|
|||||||
// This is for stanza handlers that are not part of the XMPP library but still need
|
// This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
@Default(<String, dynamic>{}) Map<String, dynamic> other,
|
@Default(<String, dynamic>{}) Map<String, dynamic> other,
|
||||||
|
// If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? messageRetraction,
|
||||||
|
// If non-null, then the message is a correction for the specified stanza Id
|
||||||
|
String? lastMessageCorrectionSid,
|
||||||
|
// Reactions data
|
||||||
|
MessageReactions? messageReactions,
|
||||||
|
// The Id of the sticker pack this sticker belongs to
|
||||||
|
String? stickerPackId,
|
||||||
}
|
}
|
||||||
) = _StanzaHandlerData;
|
) = _StanzaHandlerData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,16 @@ mixin _$StanzaHandlerData {
|
|||||||
DelayedDelivery? get delayedDelivery =>
|
DelayedDelivery? get delayedDelivery =>
|
||||||
throw _privateConstructorUsedError; // This is for stanza handlers that are not part of the XMPP library but still need
|
throw _privateConstructorUsedError; // This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
Map<String, dynamic> get other => throw _privateConstructorUsedError;
|
Map<String, dynamic> get other =>
|
||||||
|
throw _privateConstructorUsedError; // If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? get messageRetraction =>
|
||||||
|
throw _privateConstructorUsedError; // If non-null, then the message is a correction for the specified stanza Id
|
||||||
|
String? get lastMessageCorrectionSid =>
|
||||||
|
throw _privateConstructorUsedError; // Reactions data
|
||||||
|
MessageReactions? get messageReactions =>
|
||||||
|
throw _privateConstructorUsedError; // The Id of the sticker pack this sticker belongs to
|
||||||
|
String? get stickerPackId => throw _privateConstructorUsedError;
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
$StanzaHandlerDataCopyWith<StanzaHandlerData> get copyWith =>
|
$StanzaHandlerDataCopyWith<StanzaHandlerData> get copyWith =>
|
||||||
@@ -87,7 +96,11 @@ abstract class $StanzaHandlerDataCopyWith<$Res> {
|
|||||||
bool encrypted,
|
bool encrypted,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other});
|
Map<String, dynamic> other,
|
||||||
|
MessageRetractionData? messageRetraction,
|
||||||
|
String? lastMessageCorrectionSid,
|
||||||
|
MessageReactions? messageReactions,
|
||||||
|
String? stickerPackId});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @nodoc
|
/// @nodoc
|
||||||
@@ -122,6 +135,10 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
|
Object? messageRetraction = freezed,
|
||||||
|
Object? lastMessageCorrectionSid = freezed,
|
||||||
|
Object? messageReactions = freezed,
|
||||||
|
Object? stickerPackId = freezed,
|
||||||
}) {
|
}) {
|
||||||
return _then(_value.copyWith(
|
return _then(_value.copyWith(
|
||||||
done: done == freezed
|
done: done == freezed
|
||||||
@@ -208,6 +225,22 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value.other
|
? _value.other
|
||||||
: other // ignore: cast_nullable_to_non_nullable
|
: other // ignore: cast_nullable_to_non_nullable
|
||||||
as Map<String, dynamic>,
|
as Map<String, dynamic>,
|
||||||
|
messageRetraction: messageRetraction == freezed
|
||||||
|
? _value.messageRetraction
|
||||||
|
: messageRetraction // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageRetractionData?,
|
||||||
|
lastMessageCorrectionSid: lastMessageCorrectionSid == freezed
|
||||||
|
? _value.lastMessageCorrectionSid
|
||||||
|
: lastMessageCorrectionSid // ignore: cast_nullable_to_non_nullable
|
||||||
|
as String?,
|
||||||
|
messageReactions: messageReactions == freezed
|
||||||
|
? _value.messageReactions
|
||||||
|
: messageReactions // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageReactions?,
|
||||||
|
stickerPackId: stickerPackId == freezed
|
||||||
|
? _value.stickerPackId
|
||||||
|
: stickerPackId // ignore: cast_nullable_to_non_nullable
|
||||||
|
as String?,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,7 +273,11 @@ abstract class _$$_StanzaHandlerDataCopyWith<$Res>
|
|||||||
bool encrypted,
|
bool encrypted,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other});
|
Map<String, dynamic> other,
|
||||||
|
MessageRetractionData? messageRetraction,
|
||||||
|
String? lastMessageCorrectionSid,
|
||||||
|
MessageReactions? messageReactions,
|
||||||
|
String? stickerPackId});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @nodoc
|
/// @nodoc
|
||||||
@@ -277,6 +314,10 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
|
Object? messageRetraction = freezed,
|
||||||
|
Object? lastMessageCorrectionSid = freezed,
|
||||||
|
Object? messageReactions = freezed,
|
||||||
|
Object? stickerPackId = freezed,
|
||||||
}) {
|
}) {
|
||||||
return _then(_$_StanzaHandlerData(
|
return _then(_$_StanzaHandlerData(
|
||||||
done == freezed
|
done == freezed
|
||||||
@@ -363,6 +404,22 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value._other
|
? _value._other
|
||||||
: other // ignore: cast_nullable_to_non_nullable
|
: other // ignore: cast_nullable_to_non_nullable
|
||||||
as Map<String, dynamic>,
|
as Map<String, dynamic>,
|
||||||
|
messageRetraction: messageRetraction == freezed
|
||||||
|
? _value.messageRetraction
|
||||||
|
: messageRetraction // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageRetractionData?,
|
||||||
|
lastMessageCorrectionSid: lastMessageCorrectionSid == freezed
|
||||||
|
? _value.lastMessageCorrectionSid
|
||||||
|
: lastMessageCorrectionSid // ignore: cast_nullable_to_non_nullable
|
||||||
|
as String?,
|
||||||
|
messageReactions: messageReactions == freezed
|
||||||
|
? _value.messageReactions
|
||||||
|
: messageReactions // ignore: cast_nullable_to_non_nullable
|
||||||
|
as MessageReactions?,
|
||||||
|
stickerPackId: stickerPackId == freezed
|
||||||
|
? _value.stickerPackId
|
||||||
|
: stickerPackId // ignore: cast_nullable_to_non_nullable
|
||||||
|
as String?,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -387,7 +444,11 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
this.encrypted = false,
|
this.encrypted = false,
|
||||||
this.encryptionType,
|
this.encryptionType,
|
||||||
this.delayedDelivery,
|
this.delayedDelivery,
|
||||||
final Map<String, dynamic> other = const <String, dynamic>{}})
|
final Map<String, dynamic> other = const <String, dynamic>{},
|
||||||
|
this.messageRetraction,
|
||||||
|
this.lastMessageCorrectionSid,
|
||||||
|
this.messageReactions,
|
||||||
|
this.stickerPackId})
|
||||||
: _other = other;
|
: _other = other;
|
||||||
|
|
||||||
// Indicates to the runner that processing is now done. This means that all
|
// Indicates to the runner that processing is now done. This means that all
|
||||||
@@ -463,9 +524,23 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
return EqualUnmodifiableMapView(_other);
|
return EqualUnmodifiableMapView(_other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
@override
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
|
// If non-null, then the message is a correction for the specified stanza Id
|
||||||
|
@override
|
||||||
|
final String? lastMessageCorrectionSid;
|
||||||
|
// Reactions data
|
||||||
|
@override
|
||||||
|
final MessageReactions? messageReactions;
|
||||||
|
// The Id of the sticker pack this sticker belongs to
|
||||||
|
@override
|
||||||
|
final String? stickerPackId;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return 'StanzaHandlerData(done: $done, cancel: $cancel, cancelReason: $cancelReason, stanza: $stanza, retransmitted: $retransmitted, sims: $sims, sfs: $sfs, oob: $oob, stableId: $stableId, reply: $reply, chatState: $chatState, isCarbon: $isCarbon, deliveryReceiptRequested: $deliveryReceiptRequested, isMarkable: $isMarkable, fun: $fun, funReplacement: $funReplacement, funCancellation: $funCancellation, encrypted: $encrypted, encryptionType: $encryptionType, delayedDelivery: $delayedDelivery, other: $other)';
|
return 'StanzaHandlerData(done: $done, cancel: $cancel, cancelReason: $cancelReason, stanza: $stanza, retransmitted: $retransmitted, sims: $sims, sfs: $sfs, oob: $oob, stableId: $stableId, reply: $reply, chatState: $chatState, isCarbon: $isCarbon, deliveryReceiptRequested: $deliveryReceiptRequested, isMarkable: $isMarkable, fun: $fun, funReplacement: $funReplacement, funCancellation: $funCancellation, encrypted: $encrypted, encryptionType: $encryptionType, delayedDelivery: $delayedDelivery, other: $other, messageRetraction: $messageRetraction, lastMessageCorrectionSid: $lastMessageCorrectionSid, messageReactions: $messageReactions, stickerPackId: $stickerPackId)';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -501,7 +576,15 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
.equals(other.encryptionType, encryptionType) &&
|
.equals(other.encryptionType, encryptionType) &&
|
||||||
const DeepCollectionEquality()
|
const DeepCollectionEquality()
|
||||||
.equals(other.delayedDelivery, delayedDelivery) &&
|
.equals(other.delayedDelivery, delayedDelivery) &&
|
||||||
const DeepCollectionEquality().equals(other._other, this._other));
|
const DeepCollectionEquality().equals(other._other, this._other) &&
|
||||||
|
const DeepCollectionEquality()
|
||||||
|
.equals(other.messageRetraction, messageRetraction) &&
|
||||||
|
const DeepCollectionEquality().equals(
|
||||||
|
other.lastMessageCorrectionSid, lastMessageCorrectionSid) &&
|
||||||
|
const DeepCollectionEquality()
|
||||||
|
.equals(other.messageReactions, messageReactions) &&
|
||||||
|
const DeepCollectionEquality()
|
||||||
|
.equals(other.stickerPackId, stickerPackId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -527,7 +610,11 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
const DeepCollectionEquality().hash(encrypted),
|
const DeepCollectionEquality().hash(encrypted),
|
||||||
const DeepCollectionEquality().hash(encryptionType),
|
const DeepCollectionEquality().hash(encryptionType),
|
||||||
const DeepCollectionEquality().hash(delayedDelivery),
|
const DeepCollectionEquality().hash(delayedDelivery),
|
||||||
const DeepCollectionEquality().hash(_other)
|
const DeepCollectionEquality().hash(_other),
|
||||||
|
const DeepCollectionEquality().hash(messageRetraction),
|
||||||
|
const DeepCollectionEquality().hash(lastMessageCorrectionSid),
|
||||||
|
const DeepCollectionEquality().hash(messageReactions),
|
||||||
|
const DeepCollectionEquality().hash(stickerPackId)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
@@ -556,7 +643,11 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
final bool encrypted,
|
final bool encrypted,
|
||||||
final ExplicitEncryptionType? encryptionType,
|
final ExplicitEncryptionType? encryptionType,
|
||||||
final DelayedDelivery? delayedDelivery,
|
final DelayedDelivery? delayedDelivery,
|
||||||
final Map<String, dynamic> other}) = _$_StanzaHandlerData;
|
final Map<String, dynamic> other,
|
||||||
|
final MessageRetractionData? messageRetraction,
|
||||||
|
final String? lastMessageCorrectionSid,
|
||||||
|
final MessageReactions? messageReactions,
|
||||||
|
final String? stickerPackId}) = _$_StanzaHandlerData;
|
||||||
|
|
||||||
@override // Indicates to the runner that processing is now done. This means that all
|
@override // Indicates to the runner that processing is now done. This means that all
|
||||||
// pre-processing is done and no other handlers should be consulted.
|
// pre-processing is done and no other handlers should be consulted.
|
||||||
@@ -606,6 +697,15 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
@override // This is for stanza handlers that are not part of the XMPP library but still need
|
@override // This is for stanza handlers that are not part of the XMPP library but still need
|
||||||
// pass data around.
|
// pass data around.
|
||||||
Map<String, dynamic> get other;
|
Map<String, dynamic> get other;
|
||||||
|
@override // If non-null, then it indicates the origin Id of the message that should be
|
||||||
|
// retracted
|
||||||
|
MessageRetractionData? get messageRetraction;
|
||||||
|
@override // If non-null, then the message is a correction for the specified stanza Id
|
||||||
|
String? get lastMessageCorrectionSid;
|
||||||
|
@override // Reactions data
|
||||||
|
MessageReactions? get messageReactions;
|
||||||
|
@override // The Id of the sticker pack this sticker belongs to
|
||||||
|
String? get stickerPackId;
|
||||||
@override
|
@override
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
_$$_StanzaHandlerDataCopyWith<_$_StanzaHandlerData> get copyWith =>
|
_$$_StanzaHandlerDataCopyWith<_$_StanzaHandlerData> get copyWith =>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import 'package:moxxmpp/src/stanza.dart';
|
|||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
abstract class Handler {
|
abstract class Handler {
|
||||||
|
|
||||||
const Handler(this.matchStanzas, { this.nonzaTag, this.nonzaXmlns });
|
const Handler(this.matchStanzas, { this.nonzaTag, this.nonzaXmlns });
|
||||||
final String? nonzaTag;
|
final String? nonzaTag;
|
||||||
final String? nonzaXmlns;
|
final String? nonzaXmlns;
|
||||||
@@ -32,7 +31,6 @@ abstract class Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class NonzaHandler extends Handler {
|
class NonzaHandler extends Handler {
|
||||||
|
|
||||||
NonzaHandler({
|
NonzaHandler({
|
||||||
required this.callback,
|
required this.callback,
|
||||||
String? nonzaTag,
|
String? nonzaTag,
|
||||||
@@ -46,7 +44,6 @@ class NonzaHandler extends Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class StanzaHandler extends Handler {
|
class StanzaHandler extends Handler {
|
||||||
|
|
||||||
StanzaHandler({
|
StanzaHandler({
|
||||||
required this.callback,
|
required this.callback,
|
||||||
this.tagXmlns,
|
this.tagXmlns,
|
||||||
|
|||||||
@@ -24,3 +24,7 @@ const omemoManager = 'org.moxxmpp.omemomanager';
|
|||||||
const emeManager = 'org.moxxmpp.ememanager';
|
const emeManager = 'org.moxxmpp.ememanager';
|
||||||
const cryptographicHashManager = 'org.moxxmpp.cryptographichashmanager';
|
const cryptographicHashManager = 'org.moxxmpp.cryptographichashmanager';
|
||||||
const delayedDeliveryManager = 'org.moxxmpp.delayeddeliverymanager';
|
const delayedDeliveryManager = 'org.moxxmpp.delayeddeliverymanager';
|
||||||
|
const messageRetractionManager = 'org.moxxmpp.messageretractionmanager';
|
||||||
|
const lastMessageCorrectionManager = 'org.moxxmpp.lastmessagecorrectionmanager';
|
||||||
|
const messageReactionsManager = 'org.moxxmpp.messagereactionsmanager';
|
||||||
|
const stickersManager = 'org.moxxmpp.stickersmanager';
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:moxlib/moxlib.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
@@ -11,14 +12,23 @@ import 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0066.dart';
|
import 'package:moxxmpp/src/xeps/xep_0066.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
import 'package:moxxmpp/src/xeps/xep_0085.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0184.dart';
|
import 'package:moxxmpp/src/xeps/xep_0184.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0308.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0333.dart';
|
import 'package:moxxmpp/src/xeps/xep_0333.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0334.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
import 'package:moxxmpp/src/xeps/xep_0359.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0424.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0444.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0448.dart';
|
import 'package:moxxmpp/src/xeps/xep_0448.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0461.dart';
|
||||||
|
|
||||||
|
/// Data used to build a message stanza.
|
||||||
|
///
|
||||||
|
/// [setOOBFallbackBody] indicates, when using SFS, whether a OOB fallback should be
|
||||||
|
/// added. This is recommended when sharing files but may cause issues when the message
|
||||||
|
/// stanza should include a SFS element without any fallbacks.
|
||||||
class MessageDetails {
|
class MessageDetails {
|
||||||
|
|
||||||
const MessageDetails({
|
const MessageDetails({
|
||||||
required this.to,
|
required this.to,
|
||||||
this.body,
|
this.body,
|
||||||
@@ -35,6 +45,12 @@ class MessageDetails {
|
|||||||
this.funReplacement,
|
this.funReplacement,
|
||||||
this.funCancellation,
|
this.funCancellation,
|
||||||
this.shouldEncrypt = false,
|
this.shouldEncrypt = false,
|
||||||
|
this.messageRetraction,
|
||||||
|
this.lastMessageCorrectionId,
|
||||||
|
this.messageReactions,
|
||||||
|
this.messageProcessingHints,
|
||||||
|
this.stickerPackId,
|
||||||
|
this.setOOBFallbackBody = true,
|
||||||
});
|
});
|
||||||
final String to;
|
final String to;
|
||||||
final String? body;
|
final String? body;
|
||||||
@@ -51,6 +67,12 @@ class MessageDetails {
|
|||||||
final String? funReplacement;
|
final String? funReplacement;
|
||||||
final String? funCancellation;
|
final String? funCancellation;
|
||||||
final bool shouldEncrypt;
|
final bool shouldEncrypt;
|
||||||
|
final MessageRetractionData? messageRetraction;
|
||||||
|
final String? lastMessageCorrectionId;
|
||||||
|
final MessageReactions? messageReactions;
|
||||||
|
final String? stickerPackId;
|
||||||
|
final List<MessageProcessingHint>? messageProcessingHints;
|
||||||
|
final bool setOOBFallbackBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
class MessageManager extends XmppManagerBase {
|
class MessageManager extends XmppManagerBase {
|
||||||
@@ -76,6 +98,11 @@ class MessageManager extends XmppManagerBase {
|
|||||||
final message = state.stanza;
|
final message = state.stanza;
|
||||||
final body = message.firstTag('body');
|
final body = message.firstTag('body');
|
||||||
|
|
||||||
|
final hints = List<MessageProcessingHint>.empty(growable: true);
|
||||||
|
for (final element in message.findTagsByXmlns(messageProcessingHintsXmlns)) {
|
||||||
|
hints.add(messageProcessingHintFromXml(element));
|
||||||
|
}
|
||||||
|
|
||||||
getAttributes().sendEvent(MessageEvent(
|
getAttributes().sendEvent(MessageEvent(
|
||||||
body: body != null ? body.innerText() : '',
|
body: body != null ? body.innerText() : '',
|
||||||
fromJid: JID.fromString(message.attributes['from']! as String),
|
fromJid: JID.fromString(message.attributes['from']! as String),
|
||||||
@@ -95,7 +122,15 @@ class MessageManager extends XmppManagerBase {
|
|||||||
funReplacement: state.funReplacement,
|
funReplacement: state.funReplacement,
|
||||||
funCancellation: state.funCancellation,
|
funCancellation: state.funCancellation,
|
||||||
encrypted: state.encrypted,
|
encrypted: state.encrypted,
|
||||||
|
messageRetraction: state.messageRetraction,
|
||||||
|
messageCorrectionId: state.lastMessageCorrectionSid,
|
||||||
|
messageReactions: state.messageReactions,
|
||||||
|
messageProcessingHints: hints.isEmpty ?
|
||||||
|
null :
|
||||||
|
hints,
|
||||||
|
stickerPackId: state.stickerPackId,
|
||||||
other: state.other,
|
other: state.other,
|
||||||
|
error: StanzaError.fromStanza(message),
|
||||||
),);
|
),);
|
||||||
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
@@ -107,6 +142,11 @@ class MessageManager extends XmppManagerBase {
|
|||||||
/// element to this id. If originId is non-null, then it will create an "origin-id"
|
/// element to this id. If originId is non-null, then it will create an "origin-id"
|
||||||
/// child in the message stanza and set its id to originId.
|
/// child in the message stanza and set its id to originId.
|
||||||
void sendMessage(MessageDetails details) {
|
void sendMessage(MessageDetails details) {
|
||||||
|
assert(
|
||||||
|
implies(details.quoteBody != null, details.quoteFrom != null && details.quoteId != null),
|
||||||
|
'When quoting a message, then quoteFrom and quoteId must also be non-null',
|
||||||
|
);
|
||||||
|
|
||||||
final stanza = Stanza.message(
|
final stanza = Stanza.message(
|
||||||
to: details.to,
|
to: details.to,
|
||||||
type: 'chat',
|
type: 'chat',
|
||||||
@@ -115,11 +155,11 @@ class MessageManager extends XmppManagerBase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (details.quoteBody != null) {
|
if (details.quoteBody != null) {
|
||||||
final fallback = '> ${details.quoteBody!}';
|
final quote = QuoteData.fromBodies(details.quoteBody!, details.body!);
|
||||||
|
|
||||||
stanza
|
stanza
|
||||||
..addChild(
|
..addChild(
|
||||||
XMLNode(tag: 'body', text: '$fallback\n${details.body}'),
|
XMLNode(tag: 'body', text: quote.body),
|
||||||
)
|
)
|
||||||
..addChild(
|
..addChild(
|
||||||
XMLNode.xmlns(
|
XMLNode.xmlns(
|
||||||
@@ -143,7 +183,7 @@ class MessageManager extends XmppManagerBase {
|
|||||||
tag: 'body',
|
tag: 'body',
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
'start': '0',
|
'start': '0',
|
||||||
'end': '${fallback.length}'
|
'end': '${quote.fallbackLength}',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
@@ -151,7 +191,7 @@ class MessageManager extends XmppManagerBase {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
var body = details.body;
|
var body = details.body;
|
||||||
if (details.sfs != null) {
|
if (details.sfs != null && details.setOOBFallbackBody) {
|
||||||
// TODO(Unknown): Maybe find a better solution
|
// TODO(Unknown): Maybe find a better solution
|
||||||
final firstSource = details.sfs!.sources.first;
|
final firstSource = details.sfs!.sources.first;
|
||||||
if (firstSource is StatelessFileSharingUrlSource) {
|
if (firstSource is StatelessFileSharingUrlSource) {
|
||||||
@@ -159,11 +199,15 @@ class MessageManager extends XmppManagerBase {
|
|||||||
} else if (firstSource is StatelessFileSharingEncryptedSource) {
|
} else if (firstSource is StatelessFileSharingEncryptedSource) {
|
||||||
body = firstSource.source.url;
|
body = firstSource.source.url;
|
||||||
}
|
}
|
||||||
|
} else if (details.messageRetraction?.fallback != null) {
|
||||||
|
body = details.messageRetraction!.fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
stanza.addChild(
|
if (body != null) {
|
||||||
XMLNode(tag: 'body', text: body),
|
stanza.addChild(
|
||||||
);
|
XMLNode(tag: 'body', text: body),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (details.requestDeliveryReceipt) {
|
if (details.requestDeliveryReceipt) {
|
||||||
@@ -180,7 +224,7 @@ class MessageManager extends XmppManagerBase {
|
|||||||
stanza.addChild(details.sfs!.toXML());
|
stanza.addChild(details.sfs!.toXML());
|
||||||
|
|
||||||
final source = details.sfs!.sources.first;
|
final source = details.sfs!.sources.first;
|
||||||
if (source is StatelessFileSharingUrlSource) {
|
if (source is StatelessFileSharingUrlSource && details.setOOBFallbackBody) {
|
||||||
// SFS recommends OOB as a fallback
|
// SFS recommends OOB as a fallback
|
||||||
stanza.addChild(constructOOBNode(OOBData(url: source.url)));
|
stanza.addChild(constructOOBNode(OOBData(url: source.url)));
|
||||||
}
|
}
|
||||||
@@ -216,6 +260,63 @@ class MessageManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (details.messageRetraction != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'apply-to',
|
||||||
|
xmlns: fasteningXmlns,
|
||||||
|
attributes: <String, String>{
|
||||||
|
'id': details.messageRetraction!.id,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'retract',
|
||||||
|
xmlns: messageRetractionXmlns,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (details.messageRetraction!.fallback != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'fallback',
|
||||||
|
xmlns: fallbackIndicationXmlns,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.lastMessageCorrectionId != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
makeLastMessageCorrectionEdit(
|
||||||
|
details.lastMessageCorrectionId!,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.messageReactions != null) {
|
||||||
|
stanza.addChild(details.messageReactions!.toXml());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.messageProcessingHints != null) {
|
||||||
|
for (final hint in details.messageProcessingHints!) {
|
||||||
|
stanza.addChild(hint.toXml());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.stickerPackId != null) {
|
||||||
|
stanza.addChild(
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'sticker',
|
||||||
|
xmlns: stickersXmlns,
|
||||||
|
attributes: {
|
||||||
|
'pack': details.stickerPackId!,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
getAttributes().sendStanza(stanza, awaitable: false);
|
getAttributes().sendStanza(stanza, awaitable: false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,9 @@ const hashSha3512 = 'sha3-512';
|
|||||||
const hashBlake2b256 = 'blake2b-256';
|
const hashBlake2b256 = 'blake2b-256';
|
||||||
const hashBlake2b512 = 'blake2b-512';
|
const hashBlake2b512 = 'blake2b-512';
|
||||||
|
|
||||||
|
// XEP-0308
|
||||||
|
const lmcXmlns = 'urn:xmpp:message-correct:0';
|
||||||
|
|
||||||
// XEP-0333
|
// XEP-0333
|
||||||
const chatMarkersXmlns = 'urn:xmpp:chat-markers:0';
|
const chatMarkersXmlns = 'urn:xmpp:chat-markers:0';
|
||||||
|
|
||||||
@@ -114,6 +117,18 @@ const simsXmlns = 'urn:xmpp:sims:1';
|
|||||||
// XEP-0420
|
// XEP-0420
|
||||||
const sceXmlns = 'urn:xmpp:sce:1';
|
const sceXmlns = 'urn:xmpp:sce:1';
|
||||||
|
|
||||||
|
// XEP-0422
|
||||||
|
const fasteningXmlns = 'urn:xmpp:fasten:0';
|
||||||
|
|
||||||
|
// XEP-0424
|
||||||
|
const messageRetractionXmlns = 'urn:xmpp:message-retract:0';
|
||||||
|
|
||||||
|
// XEP-0428
|
||||||
|
const fallbackIndicationXmlns = 'urn:xmpp:fallback:0';
|
||||||
|
|
||||||
|
// XEP-0444
|
||||||
|
const messageReactionsXmlns = 'urn:xmpp:reactions:0';
|
||||||
|
|
||||||
// XEP-0446
|
// XEP-0446
|
||||||
const fileMetadataXmlns = 'urn:xmpp:file:metadata:0';
|
const fileMetadataXmlns = 'urn:xmpp:file:metadata:0';
|
||||||
|
|
||||||
@@ -126,6 +141,9 @@ const sfsEncryptionAes128GcmNoPaddingXmlns = 'urn:xmpp:ciphers:aes-128-gcm-nopad
|
|||||||
const sfsEncryptionAes256GcmNoPaddingXmlns = 'urn:xmpp:ciphers:aes-256-gcm-nopadding:0';
|
const sfsEncryptionAes256GcmNoPaddingXmlns = 'urn:xmpp:ciphers:aes-256-gcm-nopadding:0';
|
||||||
const sfsEncryptionAes256CbcPkcs7Xmlns = 'urn:xmpp:ciphers:aes-256-cbc-pkcs7:0';
|
const sfsEncryptionAes256CbcPkcs7Xmlns = 'urn:xmpp:ciphers:aes-256-cbc-pkcs7:0';
|
||||||
|
|
||||||
|
// XEP-0449
|
||||||
|
const stickersXmlns = 'urn:xmpp:stickers:0';
|
||||||
|
|
||||||
// XEP-0461
|
// XEP-0461
|
||||||
const replyXmlns = 'urn:xmpp:reply:0';
|
const replyXmlns = 'urn:xmpp:reply:0';
|
||||||
const fallbackXmlns = 'urn:xmpp:feature-fallback:0';
|
const fallbackXmlns = 'urn:xmpp:feature-fallback:0';
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import 'package:moxlib/moxlib.dart';
|
import 'package:moxlib/moxlib.dart';
|
||||||
|
import 'package:moxxmpp/src/errors.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
import 'package:moxxmpp/src/settings.dart';
|
import 'package:moxxmpp/src/settings.dart';
|
||||||
import 'package:moxxmpp/src/socket.dart';
|
import 'package:moxxmpp/src/socket.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
/// The state a negotiator is currently in
|
/// The state a negotiator is currently in
|
||||||
enum NegotiatorState {
|
enum NegotiatorState {
|
||||||
@@ -14,15 +16,15 @@ enum NegotiatorState {
|
|||||||
done,
|
done,
|
||||||
// Cancel the current attempt but we are not done
|
// Cancel the current attempt but we are not done
|
||||||
retryLater,
|
retryLater,
|
||||||
// The negotiator is in an error state
|
|
||||||
error,
|
|
||||||
// Skip the rest of the negotiation and assume the stream ready. Only use this when
|
// Skip the rest of the negotiation and assume the stream ready. Only use this when
|
||||||
// using stream restoration XEPs, like Stream Management.
|
// using stream restoration XEPs, like Stream Management.
|
||||||
skipRest,
|
skipRest,
|
||||||
}
|
}
|
||||||
|
|
||||||
class NegotiatorAttributes {
|
/// A base class for all errors that may occur during feature negotiation
|
||||||
|
abstract class NegotiatorError extends XmppError {}
|
||||||
|
|
||||||
|
class NegotiatorAttributes {
|
||||||
const NegotiatorAttributes(
|
const NegotiatorAttributes(
|
||||||
this.sendNonza,
|
this.sendNonza,
|
||||||
this.getConnectionSettings,
|
this.getConnectionSettings,
|
||||||
@@ -97,7 +99,7 @@ abstract class XmppFeatureNegotiatorBase {
|
|||||||
/// must switch some internal state to prevent getting matched immediately again.
|
/// must switch some internal state to prevent getting matched immediately again.
|
||||||
/// If ready is returned, then the negotiator indicates that it is not done with
|
/// If ready is returned, then the negotiator indicates that it is not done with
|
||||||
/// negotiation.
|
/// negotiation.
|
||||||
Future<void> negotiate(XMLNode nonza);
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza);
|
||||||
|
|
||||||
/// Reset the negotiator to a state that negotation can happen again.
|
/// Reset the negotiator to a state that negotation can happen again.
|
||||||
void reset() {
|
void reset() {
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
class ResourceBindingFailedError extends NegotiatorError {}
|
||||||
|
|
||||||
class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
||||||
|
|
||||||
ResourceBindingNegotiator() : _requestSent = false, super(0, false, bindXmlns, resourceBindingNegotiator);
|
ResourceBindingNegotiator() : _requestSent = false, super(0, false, bindXmlns, resourceBindingNegotiator);
|
||||||
@@ -23,7 +26,7 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
if (!_requestSent) {
|
if (!_requestSent) {
|
||||||
final stanza = XMLNode.xmlns(
|
final stanza = XMLNode.xmlns(
|
||||||
tag: 'iq',
|
tag: 'iq',
|
||||||
@@ -42,10 +45,10 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
_requestSent = true;
|
_requestSent = true;
|
||||||
attributes.sendNonza(stanza);
|
attributes.sendNonza(stanza);
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
} else {
|
} else {
|
||||||
if (nonza.tag != 'iq' || nonza.attributes['type'] != 'result') {
|
if (nonza.tag != 'iq' || nonza.attributes['type'] != 'result') {
|
||||||
state = NegotiatorState.error;
|
return Result(ResourceBindingFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final bind = nonza.firstTag('bind')!;
|
final bind = nonza.firstTag('bind')!;
|
||||||
@@ -53,7 +56,7 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
final resource = jid.innerText().split('/')[1];
|
final resource = jid.innerText().split('/')[1];
|
||||||
|
|
||||||
await attributes.sendEvent(ResourceBindingSuccessEvent(resource: resource));
|
await attributes.sendEvent(ResourceBindingSuccessEvent(resource: resource));
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
packages/moxxmpp/lib/src/negotiators/sasl/errors.dart
Normal file
3
packages/moxxmpp/lib/src/negotiators/sasl/errors.dart
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
|
||||||
|
class SaslFailedError extends NegotiatorError {}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
class SaslPlainAuthNonza extends SaslAuthNonza {
|
class SaslPlainAuthNonza extends SaslAuthNonza {
|
||||||
SaslPlainAuthNonza(String username, String password) : super(
|
SaslPlainAuthNonza(String username, String password) : super(
|
||||||
@@ -15,7 +16,6 @@ class SaslPlainAuthNonza extends SaslAuthNonza {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SaslPlainNegotiator extends SaslNegotiator {
|
class SaslPlainNegotiator extends SaslNegotiator {
|
||||||
|
|
||||||
SaslPlainNegotiator()
|
SaslPlainNegotiator()
|
||||||
: _authSent = false,
|
: _authSent = false,
|
||||||
_log = Logger('SaslPlainNegotiator'),
|
_log = Logger('SaslPlainNegotiator'),
|
||||||
@@ -41,7 +41,7 @@ class SaslPlainNegotiator extends SaslNegotiator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
if (!_authSent) {
|
if (!_authSent) {
|
||||||
final settings = attributes.getConnectionSettings();
|
final settings = attributes.getConnectionSettings();
|
||||||
attributes.sendNonza(
|
attributes.sendNonza(
|
||||||
@@ -49,17 +49,17 @@ class SaslPlainNegotiator extends SaslNegotiator {
|
|||||||
redact: SaslPlainAuthNonza('******', '******').toXml(),
|
redact: SaslPlainAuthNonza('******', '******').toXml(),
|
||||||
);
|
);
|
||||||
_authSent = true;
|
_authSent = true;
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
} else {
|
} else {
|
||||||
final tag = nonza.tag;
|
final tag = nonza.tag;
|
||||||
if (tag == 'success') {
|
if (tag == 'success') {
|
||||||
await attributes.sendEvent(AuthenticationSuccessEvent());
|
await attributes.sendEvent(AuthenticationSuccessEvent());
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
} else {
|
} else {
|
||||||
// We assume it's a <failure/>
|
// We assume it's a <failure/>
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
|
return Result(SaslFailedError());
|
||||||
state = NegotiatorState.error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math' show Random;
|
import 'dart:math' show Random;
|
||||||
|
|
||||||
import 'package:cryptography/cryptography.dart';
|
import 'package:cryptography/cryptography.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/negotiators/sasl/errors.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
import 'package:moxxmpp/src/negotiators/sasl/nonza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:random_string/random_string.dart';
|
import 'package:random_string/random_string.dart';
|
||||||
import 'package:saslprep/saslprep.dart';
|
import 'package:saslprep/saslprep.dart';
|
||||||
|
|
||||||
@@ -30,6 +31,17 @@ HashAlgorithm hashFromType(ScramHashType type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int pbkdfBitsFromHash(ScramHashType type) {
|
||||||
|
switch (type) {
|
||||||
|
// NOTE: SHA1 is 20 octets long => 20 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha1: return 160;
|
||||||
|
// NOTE: SHA256 is 32 octets long => 32 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha256: return 256;
|
||||||
|
// NOTE: SHA512 is 64 octets long => 64 octets * 8 bits/octet
|
||||||
|
case ScramHashType.sha512: return 512;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const scramSha1Mechanism = 'SCRAM-SHA-1';
|
const scramSha1Mechanism = 'SCRAM-SHA-1';
|
||||||
const scramSha256Mechanism = 'SCRAM-SHA-256';
|
const scramSha256Mechanism = 'SCRAM-SHA-256';
|
||||||
const scramSha512Mechanism = 'SCRAM-SHA-512';
|
const scramSha512Mechanism = 'SCRAM-SHA-512';
|
||||||
@@ -78,7 +90,6 @@ enum ScramState {
|
|||||||
const gs2Header = 'n,,';
|
const gs2Header = 'n,,';
|
||||||
|
|
||||||
class SaslScramNegotiator extends SaslNegotiator {
|
class SaslScramNegotiator extends SaslNegotiator {
|
||||||
|
|
||||||
// NOTE: NEVER, and I mean, NEVER set clientNonce or initalMessageNoGS2. They are just there for testing
|
// NOTE: NEVER, and I mean, NEVER set clientNonce or initalMessageNoGS2. They are just there for testing
|
||||||
SaslScramNegotiator(
|
SaslScramNegotiator(
|
||||||
int priority,
|
int priority,
|
||||||
@@ -106,7 +117,7 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
final pbkdf2 = Pbkdf2(
|
final pbkdf2 = Pbkdf2(
|
||||||
macAlgorithm: Hmac(_hash),
|
macAlgorithm: Hmac(_hash),
|
||||||
iterations: iterations,
|
iterations: iterations,
|
||||||
bits: 160, // NOTE: RFC says 20 octets => 20 octets * 8 bits/octet
|
bits: pbkdfBitsFromHash(hashType),
|
||||||
);
|
);
|
||||||
|
|
||||||
final saltedPasswordRaw = await pbkdf2.deriveKey(
|
final saltedPasswordRaw = await pbkdf2.deriveKey(
|
||||||
@@ -186,7 +197,7 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
switch (_scramState) {
|
switch (_scramState) {
|
||||||
case ScramState.preSent:
|
case ScramState.preSent:
|
||||||
if (clientNonce == null || clientNonce == '') {
|
if (clientNonce == null || clientNonce == '') {
|
||||||
@@ -200,15 +211,14 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
SaslScramAuthNonza(body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)), type: hashType),
|
SaslScramAuthNonza(body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)), type: hashType),
|
||||||
redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(),
|
redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(),
|
||||||
);
|
);
|
||||||
break;
|
return const Result(NegotiatorState.ready);
|
||||||
case ScramState.initialMessageSent:
|
case ScramState.initialMessageSent:
|
||||||
if (nonza.tag != 'challenge') {
|
if (nonza.tag != 'challenge') {
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
|
|
||||||
state = NegotiatorState.error;
|
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
return;
|
return Result(SaslFailedError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final challengeBase64 = nonza.innerText();
|
final challengeBase64 = nonza.innerText();
|
||||||
@@ -219,15 +229,14 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
SaslScramResponseNonza(body: responseBase64),
|
SaslScramResponseNonza(body: responseBase64),
|
||||||
redact: SaslScramResponseNonza(body: '******').toXml(),
|
redact: SaslScramResponseNonza(body: '******').toXml(),
|
||||||
);
|
);
|
||||||
return;
|
return const Result(NegotiatorState.ready);
|
||||||
case ScramState.challengeResponseSent:
|
case ScramState.challengeResponseSent:
|
||||||
if (nonza.tag != 'success') {
|
if (nonza.tag != 'success') {
|
||||||
// We assume it's a <failure />
|
// We assume it's a <failure />
|
||||||
final error = nonza.children.first.tag;
|
final error = nonza.children.first.tag;
|
||||||
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
await attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This assumes that the string is always "v=..." and contains no other parameters
|
// NOTE: This assumes that the string is always "v=..." and contains no other parameters
|
||||||
@@ -237,16 +246,13 @@ class SaslScramNegotiator extends SaslNegotiator {
|
|||||||
//final error = nonza.children.first.tag;
|
//final error = nonza.children.first.tag;
|
||||||
//attributes.sendEvent(AuthenticationFailedEvent(error));
|
//attributes.sendEvent(AuthenticationFailedEvent(error));
|
||||||
_scramState = ScramState.error;
|
_scramState = ScramState.error;
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await attributes.sendEvent(AuthenticationSuccessEvent());
|
await attributes.sendEvent(AuthenticationSuccessEvent());
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
return;
|
|
||||||
case ScramState.error:
|
case ScramState.error:
|
||||||
state = NegotiatorState.error;
|
return Result(SaslFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
enum _StartTlsState {
|
enum _StartTlsState {
|
||||||
ready,
|
ready,
|
||||||
requested
|
requested
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StartTLSFailedError extends NegotiatorError {}
|
||||||
|
|
||||||
class StartTLSNonza extends XMLNode {
|
class StartTLSNonza extends XMLNode {
|
||||||
StartTLSNonza() : super.xmlns(
|
StartTLSNonza() : super.xmlns(
|
||||||
tag: 'starttls',
|
tag: 'starttls',
|
||||||
@@ -27,18 +30,17 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
final Logger _log;
|
final Logger _log;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
switch (_state) {
|
switch (_state) {
|
||||||
case _StartTlsState.ready:
|
case _StartTlsState.ready:
|
||||||
_log.fine('StartTLS is available. Performing StartTLS upgrade...');
|
_log.fine('StartTLS is available. Performing StartTLS upgrade...');
|
||||||
_state = _StartTlsState.requested;
|
_state = _StartTlsState.requested;
|
||||||
attributes.sendNonza(StartTLSNonza());
|
attributes.sendNonza(StartTLSNonza());
|
||||||
break;
|
return const Result(NegotiatorState.ready);
|
||||||
case _StartTlsState.requested:
|
case _StartTlsState.requested:
|
||||||
if (nonza.tag != 'proceed' || nonza.attributes['xmlns'] != startTlsXmlns) {
|
if (nonza.tag != 'proceed' || nonza.attributes['xmlns'] != startTlsXmlns) {
|
||||||
_log.severe('Failed to perform StartTLS negotiation');
|
_log.severe('Failed to perform StartTLS negotiation');
|
||||||
state = NegotiatorState.error;
|
return Result(StartTLSFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Securing socket');
|
_log.fine('Securing socket');
|
||||||
@@ -46,13 +48,11 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
.secure(attributes.getConnectionSettings().jid.domain);
|
.secure(attributes.getConnectionSettings().jid.domain);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
_log.severe('Failed to secure stream');
|
_log.severe('Failed to secure stream');
|
||||||
state = NegotiatorState.error;
|
return Result(StartTLSFailedError());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Stream is now TLS secured');
|
_log.fine('Stream is now TLS secured');
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,27 +4,33 @@ import 'package:logging/logging.dart';
|
|||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
abstract class ReconnectionPolicy {
|
/// A callback function to be called when the connection to the server has been lost.
|
||||||
|
typedef ConnectionLostCallback = Future<void> Function();
|
||||||
|
|
||||||
ReconnectionPolicy()
|
/// A function that, when called, causes the XmppConnection to connect to the server, if
|
||||||
: _shouldAttemptReconnection = false,
|
/// another reconnection is not already running.
|
||||||
_isReconnecting = false,
|
typedef PerformReconnectFunction = Future<void> Function();
|
||||||
_isReconnectingLock = Lock();
|
|
||||||
|
abstract class ReconnectionPolicy {
|
||||||
/// Function provided by XmppConnection that allows the policy
|
/// Function provided by XmppConnection that allows the policy
|
||||||
/// to perform a reconnection.
|
/// to perform a reconnection.
|
||||||
Future<void> Function()? performReconnect;
|
PerformReconnectFunction? performReconnect;
|
||||||
|
|
||||||
/// Function provided by XmppConnection that allows the policy
|
/// Function provided by XmppConnection that allows the policy
|
||||||
/// to say that we lost the connection.
|
/// to say that we lost the connection.
|
||||||
void Function()? triggerConnectionLost;
|
ConnectionLostCallback? triggerConnectionLost;
|
||||||
|
|
||||||
/// Indicate if should try to reconnect.
|
/// Indicate if should try to reconnect.
|
||||||
bool _shouldAttemptReconnection;
|
bool _shouldAttemptReconnection = false;
|
||||||
|
|
||||||
/// Indicate if a reconnection attempt is currently running.
|
/// Indicate if a reconnection attempt is currently running.
|
||||||
bool _isReconnecting;
|
bool _isReconnecting = false;
|
||||||
|
|
||||||
/// And the corresponding lock
|
/// And the corresponding lock
|
||||||
final Lock _isReconnectingLock;
|
final Lock _isReconnectingLock = Lock();
|
||||||
|
|
||||||
/// Called by XmppConnection to register the policy.
|
/// Called by XmppConnection to register the policy.
|
||||||
void register(Future<void> Function() performReconnect, void Function() triggerConnectionLost) {
|
void register(PerformReconnectFunction performReconnect, ConnectionLostCallback triggerConnectionLost) {
|
||||||
this.performReconnect = performReconnect;
|
this.performReconnect = performReconnect;
|
||||||
this.triggerConnectionLost = triggerConnectionLost;
|
this.triggerConnectionLost = triggerConnectionLost;
|
||||||
|
|
||||||
@@ -80,10 +86,11 @@ abstract class ReconnectionPolicy {
|
|||||||
/// for every failed attempt.
|
/// for every failed attempt.
|
||||||
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
||||||
|
|
||||||
ExponentialBackoffReconnectionPolicy()
|
ExponentialBackoffReconnectionPolicy(this._maxBackoffTime)
|
||||||
: _counter = 0,
|
: _counter = 0,
|
||||||
_log = Logger('ExponentialBackoffReconnectionPolicy'),
|
_log = Logger('ExponentialBackoffReconnectionPolicy'),
|
||||||
super();
|
super();
|
||||||
|
final int _maxBackoffTime;
|
||||||
int _counter;
|
int _counter;
|
||||||
Timer? _timer;
|
Timer? _timer;
|
||||||
final Logger _log;
|
final Logger _log;
|
||||||
@@ -124,7 +131,7 @@ class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait at max 80 seconds.
|
// Wait at max 80 seconds.
|
||||||
final seconds = min(pow(2, _counter).toInt(), 80);
|
final seconds = min(min(pow(2, _counter).toInt(), 80), _maxBackoffTime);
|
||||||
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,3 +24,28 @@ int ioctetSortComparator(String a, String b) {
|
|||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ioctetSortComparatorRaw(List<int> a, List<int> b) {
|
||||||
|
if (a.isEmpty && b.isEmpty) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.isEmpty && b.isNotEmpty) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.isNotEmpty && b.isEmpty) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a[0] == b[0]) {
|
||||||
|
return ioctetSortComparatorRaw(a.sublist(1), b.sublist(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(Unknown): Is this correct?
|
||||||
|
if (a[0] < b[0]) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|||||||
7
packages/moxxmpp/lib/src/roster/errors.dart
Normal file
7
packages/moxxmpp/lib/src/roster/errors.dart
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
abstract class RosterError {}
|
||||||
|
|
||||||
|
/// Returned when the server's response did not contain a <query /> element
|
||||||
|
class NoQueryError extends RosterError {}
|
||||||
|
|
||||||
|
/// Unspecified error
|
||||||
|
class UnknownError extends RosterError {}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
import 'package:moxxmpp/src/events.dart';
|
import 'dart:async';
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/attributes.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
import 'package:moxxmpp/src/managers/data.dart';
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
import 'package:moxxmpp/src/managers/handlers.dart';
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
@@ -7,21 +10,43 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
|
import 'package:moxxmpp/src/roster/errors.dart';
|
||||||
|
import 'package:moxxmpp/src/roster/state.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/error.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
const rosterErrorNoQuery = 1;
|
|
||||||
const rosterErrorNonResult = 2;
|
|
||||||
|
|
||||||
|
@immutable
|
||||||
class XmppRosterItem {
|
class XmppRosterItem {
|
||||||
|
const XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
|
||||||
XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
|
|
||||||
final String jid;
|
final String jid;
|
||||||
final String? name;
|
final String? name;
|
||||||
final String subscription;
|
final String subscription;
|
||||||
final String? ask;
|
final String? ask;
|
||||||
final List<String> groups;
|
final List<String> groups;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator==(Object other) {
|
||||||
|
return other is XmppRosterItem &&
|
||||||
|
other.jid == jid &&
|
||||||
|
other.name == name &&
|
||||||
|
other.subscription == subscription &&
|
||||||
|
other.ask == ask &&
|
||||||
|
const ListEquality<String>().equals(other.groups, groups);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => jid.hashCode ^ name.hashCode ^ subscription.hashCode ^ ask.hashCode ^ groups.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'XmppRosterItem('
|
||||||
|
'jid: $jid, '
|
||||||
|
'name: $name, '
|
||||||
|
'subscription: $subscription, '
|
||||||
|
'ask: $ask, '
|
||||||
|
'groups: $groups)';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum RosterRemovalResult {
|
enum RosterRemovalResult {
|
||||||
@@ -31,15 +56,13 @@ enum RosterRemovalResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RosterRequestResult {
|
class RosterRequestResult {
|
||||||
|
RosterRequestResult(this.items, this.ver);
|
||||||
RosterRequestResult({ required this.items, this.ver });
|
|
||||||
List<XmppRosterItem> items;
|
List<XmppRosterItem> items;
|
||||||
String? ver;
|
String? ver;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RosterPushEvent extends XmppEvent {
|
class RosterPushResult {
|
||||||
|
RosterPushResult(this.item, this.ver);
|
||||||
RosterPushEvent({ required this.item, this.ver });
|
|
||||||
final XmppRosterItem item;
|
final XmppRosterItem item;
|
||||||
final String? ver;
|
final String? ver;
|
||||||
}
|
}
|
||||||
@@ -53,11 +76,11 @@ class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
bool get isSupported => _supported;
|
bool get isSupported => _supported;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when the negotiator matched, meaning the server
|
// negotiate is only called when the negotiator matched, meaning the server
|
||||||
// advertises roster versioning.
|
// advertises roster versioning.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -70,9 +93,16 @@ class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
/// This manager requires a RosterFeatureNegotiator to be registered.
|
/// This manager requires a RosterFeatureNegotiator to be registered.
|
||||||
class RosterManager extends XmppManagerBase {
|
class RosterManager extends XmppManagerBase {
|
||||||
|
RosterManager(this._stateManager) : super();
|
||||||
|
|
||||||
RosterManager() : _rosterVersion = null, super();
|
/// The class managing the entire roster state.
|
||||||
String? _rosterVersion;
|
final BaseRosterStateManager _stateManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void register(XmppManagerAttributes attributes) {
|
||||||
|
super.register(attributes);
|
||||||
|
_stateManager.register(attributes.sendEvent);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => rosterManager;
|
String getId() => rosterManager;
|
||||||
@@ -92,17 +122,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
/// Override-able functions
|
|
||||||
Future<void> commitLastRosterVersion(String version) async {}
|
|
||||||
Future<void> loadLastRosterVersion() async {}
|
|
||||||
|
|
||||||
void setRosterVersion(String ver) {
|
|
||||||
assert(_rosterVersion == null, 'A roster version must not be empty');
|
|
||||||
|
|
||||||
_rosterVersion = ver;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<StanzaHandlerData> _onRosterPush(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onRosterPush(Stanza stanza, StanzaHandlerData state) async {
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
final from = stanza.attributes['from'] as String?;
|
final from = stanza.attributes['from'] as String?;
|
||||||
@@ -119,6 +139,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final query = stanza.firstTag('query', xmlns: rosterXmlns)!;
|
final query = stanza.firstTag('query', xmlns: rosterXmlns)!;
|
||||||
|
logger.fine('Roster push: ${query.toXml()}');
|
||||||
final item = query.firstTag('item');
|
final item = query.firstTag('item');
|
||||||
|
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
@@ -126,21 +147,20 @@ class RosterManager extends XmppManagerBase {
|
|||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.attributes['ver'] != null) {
|
unawaited(
|
||||||
final ver = query.attributes['ver']! as String;
|
_stateManager.handleRosterPush(
|
||||||
await commitLastRosterVersion(ver);
|
RosterPushResult(
|
||||||
_rosterVersion = ver;
|
XmppRosterItem(
|
||||||
}
|
jid: item.attributes['jid']! as String,
|
||||||
|
subscription: item.attributes['subscription']! as String,
|
||||||
attrs.sendEvent(RosterPushEvent(
|
ask: item.attributes['ask'] as String?,
|
||||||
item: XmppRosterItem(
|
name: item.attributes['name'] as String?,
|
||||||
jid: item.attributes['jid']! as String,
|
),
|
||||||
subscription: item.attributes['subscription']! as String,
|
query.attributes['ver'] as String?,
|
||||||
ask: item.attributes['ask'] as String?,
|
),
|
||||||
name: item.attributes['name'] as String?,
|
|
||||||
),
|
),
|
||||||
ver: query.attributes['ver'] as String?,
|
);
|
||||||
),);
|
|
||||||
await attrs.sendStanza(stanza.reply());
|
await attrs.sendStanza(stanza.reply());
|
||||||
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
@@ -148,73 +168,71 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
/// Shared code between requesting rosters without and with roster versioning, if
|
/// Shared code between requesting rosters without and with roster versioning, if
|
||||||
/// the server deems a regular roster response more efficient than n roster pushes.
|
/// the server deems a regular roster response more efficient than n roster pushes.
|
||||||
Future<MayFail<RosterRequestResult>> _handleRosterResponse(XMLNode? query) async {
|
Future<Result<RosterRequestResult, RosterError>> _handleRosterResponse(XMLNode? query) async {
|
||||||
final List<XmppRosterItem> items;
|
final List<XmppRosterItem> items;
|
||||||
|
String? rosterVersion;
|
||||||
if (query != null) {
|
if (query != null) {
|
||||||
items = query.children.map((item) => XmppRosterItem(
|
items = query.children.map(
|
||||||
name: item.attributes['name'] as String?,
|
(item) => XmppRosterItem(
|
||||||
jid: item.attributes['jid']! as String,
|
name: item.attributes['name'] as String?,
|
||||||
subscription: item.attributes['subscription']! as String,
|
jid: item.attributes['jid']! as String,
|
||||||
ask: item.attributes['ask'] as String?,
|
subscription: item.attributes['subscription']! as String,
|
||||||
groups: item.findTags('group').map((groupNode) => groupNode.innerText()).toList(),
|
ask: item.attributes['ask'] as String?,
|
||||||
),).toList();
|
groups: item.findTags('group').map((groupNode) => groupNode.innerText()).toList(),
|
||||||
|
),
|
||||||
|
).toList();
|
||||||
|
|
||||||
if (query.attributes['ver'] != null) {
|
rosterVersion = query.attributes['ver'] as String?;
|
||||||
final ver_ = query.attributes['ver']! as String;
|
|
||||||
await commitLastRosterVersion(ver_);
|
|
||||||
_rosterVersion = ver_;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
logger.warning('Server response to roster request without roster versioning does not contain a <query /> element, while the type is not error. This violates RFC6121');
|
logger.warning('Server response to roster request without roster versioning does not contain a <query /> element, while the type is not error. This violates RFC6121');
|
||||||
return MayFail.failure(rosterErrorNoQuery);
|
return Result(NoQueryError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final ver = query.attributes['ver'] as String?;
|
final result = RosterRequestResult(
|
||||||
if (ver != null) {
|
items,
|
||||||
_rosterVersion = ver;
|
rosterVersion,
|
||||||
await commitLastRosterVersion(ver);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MayFail.success(
|
|
||||||
RosterRequestResult(
|
|
||||||
items: items,
|
|
||||||
ver: ver,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
_stateManager.handleRosterFetch(result),
|
||||||
|
);
|
||||||
|
|
||||||
|
return Result(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Requests the roster following RFC 6121 without using roster versioning.
|
/// Requests the roster following RFC 6121.
|
||||||
Future<MayFail<RosterRequestResult>> requestRoster() async {
|
Future<Result<RosterRequestResult, RosterError>> requestRoster() async {
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
|
final query = XMLNode.xmlns(
|
||||||
|
tag: 'query',
|
||||||
|
xmlns: rosterXmlns,
|
||||||
|
);
|
||||||
|
final rosterVersion = await _stateManager.getRosterVersion();
|
||||||
|
if (rosterVersion != null && rosterVersioningAvailable()) {
|
||||||
|
query.attributes['ver'] = rosterVersion;
|
||||||
|
}
|
||||||
|
|
||||||
final response = await attrs.sendStanza(
|
final response = await attrs.sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
type: 'get',
|
type: 'get',
|
||||||
children: [
|
children: [
|
||||||
XMLNode.xmlns(
|
query,
|
||||||
tag: 'query',
|
|
||||||
xmlns: rosterXmlns,
|
|
||||||
)
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.attributes['type'] != 'result') {
|
if (response.attributes['type'] != 'result') {
|
||||||
logger.warning('Error requesting roster without roster versioning: ${response.toXml()}');
|
logger.warning('Error requesting roster: ${response.toXml()}');
|
||||||
return MayFail.failure(rosterErrorNonResult);
|
return Result(UnknownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final query = response.firstTag('query', xmlns: rosterXmlns);
|
final responseQuery = response.firstTag('query', xmlns: rosterXmlns);
|
||||||
return _handleRosterResponse(query);
|
return _handleRosterResponse(responseQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Requests a series of roster pushes according to RFC6121. Requires that the server
|
/// Requests a series of roster pushes according to RFC6121. Requires that the server
|
||||||
/// advertises urn:xmpp:features:rosterver in the stream features.
|
/// advertises urn:xmpp:features:rosterver in the stream features.
|
||||||
Future<MayFail<RosterRequestResult?>> requestRosterPushes() async {
|
Future<Result<RosterRequestResult?, RosterError>> requestRosterPushes() async {
|
||||||
if (_rosterVersion == null) {
|
|
||||||
await loadLastRosterVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
final result = await attrs.sendStanza(
|
final result = await attrs.sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
@@ -224,7 +242,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
tag: 'query',
|
tag: 'query',
|
||||||
xmlns: rosterXmlns,
|
xmlns: rosterXmlns,
|
||||||
attributes: {
|
attributes: {
|
||||||
'ver': _rosterVersion ?? ''
|
'ver': await _stateManager.getRosterVersion() ?? '',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
@@ -233,7 +251,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
|
|
||||||
if (result.attributes['type'] != 'result') {
|
if (result.attributes['type'] != 'result') {
|
||||||
logger.warning('Requesting roster pushes failed: ${result.toXml()}');
|
logger.warning('Requesting roster pushes failed: ${result.toXml()}');
|
||||||
return MayFail.failure(rosterErrorNonResult);
|
return Result(UnknownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final query = result.firstTag('query', xmlns: rosterXmlns);
|
final query = result.firstTag('query', xmlns: rosterXmlns);
|
||||||
220
packages/moxxmpp/lib/src/roster/state.dart
Normal file
220
packages/moxxmpp/lib/src/roster/state.dart
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moxxmpp/src/events.dart';
|
||||||
|
import 'package:moxxmpp/src/roster/roster.dart';
|
||||||
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
|
class _RosterProcessTriple {
|
||||||
|
const _RosterProcessTriple(this.removed, this.modified, this.added);
|
||||||
|
final String? removed;
|
||||||
|
final XmppRosterItem? modified;
|
||||||
|
final XmppRosterItem? added;
|
||||||
|
}
|
||||||
|
|
||||||
|
class RosterCacheLoadResult {
|
||||||
|
const RosterCacheLoadResult(this.version, this.roster);
|
||||||
|
final String? version;
|
||||||
|
final List<XmppRosterItem> roster;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This class manages the roster state in order to correctly process and persist
|
||||||
|
/// roster pushes and facilitate roster versioning requests.
|
||||||
|
abstract class BaseRosterStateManager {
|
||||||
|
/// The cached version of the roster. If null, then it has not been loaded yet.
|
||||||
|
List<XmppRosterItem>? _currentRoster;
|
||||||
|
|
||||||
|
/// The cached version of the roster version.
|
||||||
|
String? _currentVersion;
|
||||||
|
|
||||||
|
/// A critical section locking both _currentRoster and _currentVersion.
|
||||||
|
final Lock _lock = Lock();
|
||||||
|
|
||||||
|
/// A function to send an XmppEvent to moxxmpp's main event bus
|
||||||
|
late void Function(XmppEvent) _sendEvent;
|
||||||
|
|
||||||
|
/// Overrideable function
|
||||||
|
/// Loads the old cached version of the roster and optionally that roster version
|
||||||
|
/// from persistent storage into a RosterCacheLoadResult object.
|
||||||
|
Future<RosterCacheLoadResult> loadRosterCache();
|
||||||
|
|
||||||
|
/// Overrideable function
|
||||||
|
/// Commits the roster data to persistent storage.
|
||||||
|
///
|
||||||
|
/// [version] is the roster version string. If none was provided, then this value
|
||||||
|
/// is null.
|
||||||
|
///
|
||||||
|
/// [removed] is a (possibly empty) list of bare JIDs that are removed from the
|
||||||
|
/// roster.
|
||||||
|
///
|
||||||
|
/// [modified] is a (possibly empty) list of XmppRosterItems that are modified. Correlation with
|
||||||
|
/// the cache is done using its jid attribute.
|
||||||
|
///
|
||||||
|
/// [added] is a (possibly empty) list of XmppRosterItems that are added by the
|
||||||
|
/// roster push or roster fetch request.
|
||||||
|
Future<void> commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added);
|
||||||
|
|
||||||
|
/// Internal function. Registers functions from the RosterManger against this
|
||||||
|
/// instance.
|
||||||
|
void register(void Function(XmppEvent) sendEvent) {
|
||||||
|
_sendEvent = sendEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load and cache or return the cached roster version.
|
||||||
|
Future<String?> getRosterVersion() async {
|
||||||
|
return _lock.synchronized(() async {
|
||||||
|
await _loadRosterCache();
|
||||||
|
|
||||||
|
return _currentVersion;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper around _commitRoster that also sends an event to moxxmpp's event
|
||||||
|
/// bus.
|
||||||
|
Future<void> _commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added) async {
|
||||||
|
_sendEvent(
|
||||||
|
RosterUpdatedEvent(
|
||||||
|
removed,
|
||||||
|
modified,
|
||||||
|
added,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await commitRoster(version, removed, modified, added);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads the cached roster data into memory, if that has not already happened.
|
||||||
|
/// NOTE: Must be called from within the _lock critical section.
|
||||||
|
Future<void> _loadRosterCache() async {
|
||||||
|
if (_currentRoster == null) {
|
||||||
|
final result = await loadRosterCache();
|
||||||
|
|
||||||
|
_currentRoster = result.roster;
|
||||||
|
_currentVersion = result.version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Processes only single XmppRosterItem [item].
|
||||||
|
/// NOTE: Requires to be called from within the _lock critical section.
|
||||||
|
_RosterProcessTriple _handleRosterItem(XmppRosterItem item) {
|
||||||
|
if (item.subscription == 'remove') {
|
||||||
|
// The item has been removed
|
||||||
|
_currentRoster!.removeWhere((i) => i.jid == item.jid);
|
||||||
|
return _RosterProcessTriple(
|
||||||
|
item.jid,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final index = _currentRoster!.indexWhere((i) => i.jid == item.jid);
|
||||||
|
if (index == -1) {
|
||||||
|
// The item does not exist
|
||||||
|
_currentRoster!.add(item);
|
||||||
|
return _RosterProcessTriple(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
item,
|
||||||
|
);
|
||||||
|
} else if (_currentRoster![index] != item) {
|
||||||
|
// The item is updated
|
||||||
|
_currentRoster![index] = item;
|
||||||
|
return _RosterProcessTriple(
|
||||||
|
null,
|
||||||
|
item,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Item has not been modified or added
|
||||||
|
return const _RosterProcessTriple(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles a roster push from the RosterManager.
|
||||||
|
Future<void> handleRosterPush(RosterPushResult event) async {
|
||||||
|
await _lock.synchronized(() async {
|
||||||
|
await _loadRosterCache();
|
||||||
|
|
||||||
|
_currentVersion = event.ver;
|
||||||
|
final result = _handleRosterItem(event.item);
|
||||||
|
|
||||||
|
if (result.removed != null) {
|
||||||
|
return _commitRoster(
|
||||||
|
_currentVersion,
|
||||||
|
[result.removed!],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
} else if (result.modified != null) {
|
||||||
|
return _commitRoster(
|
||||||
|
_currentVersion,
|
||||||
|
[],
|
||||||
|
[result.modified!],
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
} else if (result.added != null) {
|
||||||
|
return _commitRoster(
|
||||||
|
_currentVersion,
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
[result.added!],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles the result from a roster fetch.
|
||||||
|
Future<void> handleRosterFetch(RosterRequestResult result) async {
|
||||||
|
await _lock.synchronized(() async {
|
||||||
|
final removed = List<String>.empty(growable: true);
|
||||||
|
final modified = List<XmppRosterItem>.empty(growable: true);
|
||||||
|
final added = List<XmppRosterItem>.empty(growable: true);
|
||||||
|
|
||||||
|
await _loadRosterCache();
|
||||||
|
|
||||||
|
_currentVersion = result.ver;
|
||||||
|
for (final item in result.items) {
|
||||||
|
final result = _handleRosterItem(item);
|
||||||
|
|
||||||
|
if (result.removed != null) removed.add(result.removed!);
|
||||||
|
if (result.modified != null) modified.add(result.modified!);
|
||||||
|
if (result.added != null) added.add(result.added!);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _commitRoster(
|
||||||
|
_currentVersion,
|
||||||
|
removed,
|
||||||
|
modified,
|
||||||
|
added,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
List<XmppRosterItem> getRosterItems() => _currentRoster!;
|
||||||
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
class TestingRosterStateManager extends BaseRosterStateManager {
|
||||||
|
TestingRosterStateManager(
|
||||||
|
this.initialRosterVersion,
|
||||||
|
this.initialRoster,
|
||||||
|
);
|
||||||
|
final String? initialRosterVersion;
|
||||||
|
final List<XmppRosterItem> initialRoster;
|
||||||
|
int loadCount = 0;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<RosterCacheLoadResult> loadRosterCache() async {
|
||||||
|
loadCount++;
|
||||||
|
return RosterCacheLoadResult(
|
||||||
|
initialRosterVersion,
|
||||||
|
initialRoster,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added) async {}
|
||||||
|
}
|
||||||
@@ -5,13 +5,17 @@ abstract class XmppSocketEvent {}
|
|||||||
|
|
||||||
/// Triggered by the socket when an error occurs.
|
/// Triggered by the socket when an error occurs.
|
||||||
class XmppSocketErrorEvent extends XmppSocketEvent {
|
class XmppSocketErrorEvent extends XmppSocketEvent {
|
||||||
|
|
||||||
XmppSocketErrorEvent(this.error);
|
XmppSocketErrorEvent(this.error);
|
||||||
final Object error;
|
final Object error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Triggered when the socket is closed
|
/// Triggered when the socket is closed
|
||||||
class XmppSocketClosureEvent extends XmppSocketEvent {}
|
class XmppSocketClosureEvent extends XmppSocketEvent {
|
||||||
|
XmppSocketClosureEvent(this.expected);
|
||||||
|
|
||||||
|
/// Indicate that the socket did not close unexpectedly.
|
||||||
|
final bool expected;
|
||||||
|
}
|
||||||
|
|
||||||
/// This class is the base for a socket that XmppConnection can use.
|
/// This class is the base for a socket that XmppConnection can use.
|
||||||
abstract class BaseSocketWrapper {
|
abstract class BaseSocketWrapper {
|
||||||
|
|||||||
@@ -1,6 +1,28 @@
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
|
/// A simple description of the <error /> element that may be inside a stanza
|
||||||
|
class StanzaError {
|
||||||
|
StanzaError(this.type, this.error);
|
||||||
|
String type;
|
||||||
|
String error;
|
||||||
|
|
||||||
|
/// Returns a StanzaError if [stanza] contains a <error /> element. If not, returns
|
||||||
|
/// null.
|
||||||
|
static StanzaError? fromStanza(Stanza stanza) {
|
||||||
|
final error = stanza.firstTag('error');
|
||||||
|
if (error == null) return null;
|
||||||
|
|
||||||
|
final stanzaError = error.firstTagByXmlns(fullStanzaXmlns);
|
||||||
|
if (stanzaError == null) return null;
|
||||||
|
|
||||||
|
return StanzaError(
|
||||||
|
error.attributes['type']! as String,
|
||||||
|
stanzaError.tag,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Stanza extends XMLNode {
|
class Stanza extends XMLNode {
|
||||||
// ignore: use_super_parameters
|
// ignore: use_super_parameters
|
||||||
Stanza({ this.to, this.from, this.type, this.id, List<XMLNode> children = const [], required String tag, Map<String, String> attributes = const {} }) : super(
|
Stanza({ this.to, this.from, this.type, this.id, List<XMLNode> children = const [], required String tag, Map<String, String> attributes = const {} }) : super(
|
||||||
|
|||||||
@@ -129,6 +129,12 @@ class XMLNode {
|
|||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<XMLNode> findTagsByXmlns(String xmlns) {
|
||||||
|
return children
|
||||||
|
.where((element) => element.attributes['xmlns'] == xmlns)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the inner text of the node. If none is set, returns the "".
|
/// Returns the inner text of the node. If none is set, returns the "".
|
||||||
String innerText() {
|
String innerText() {
|
||||||
return text ?? '';
|
return text ?? '';
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
/// A wrapper class that can be used to indicate that a function may return a valid
|
|
||||||
/// instance of [T] but may also fail.
|
|
||||||
/// The way [MayFail] is intended to be used to to have function specific - or application
|
|
||||||
/// specific - error codes that can be either handled by code or be translated into a
|
|
||||||
/// localised error message for the user.
|
|
||||||
class MayFail<T> {
|
|
||||||
|
|
||||||
MayFail({ this.result, this.errorCode });
|
|
||||||
MayFail.success(this.result);
|
|
||||||
MayFail.failure(this.errorCode);
|
|
||||||
T? result;
|
|
||||||
int? errorCode;
|
|
||||||
|
|
||||||
bool isError() => result == null && errorCode != null;
|
|
||||||
|
|
||||||
T getValue() => result!;
|
|
||||||
|
|
||||||
int getErrorCode() => errorCode!;
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
/// Class that is supposed to by used with a state type S and a value type V.
|
class Result<T, V> {
|
||||||
/// The state indicates if an action was successful or not, while the value
|
|
||||||
/// type indicates the return value, i.e. a result in a computation or the
|
|
||||||
/// actual error description.
|
|
||||||
class Result<S, V> {
|
|
||||||
|
|
||||||
Result(S state, V value) : _state = state, _value = value;
|
const Result(this._data) : assert(_data is T || _data is V, 'Invalid data type: Must be either $T or $V');
|
||||||
final S _state;
|
final dynamic _data;
|
||||||
final V _value;
|
|
||||||
|
|
||||||
S getState() => _state;
|
bool isType<S>() => _data is S;
|
||||||
V getValue() => _value;
|
|
||||||
|
S get<S>() {
|
||||||
|
assert(_data is S, 'Data is not $S');
|
||||||
|
|
||||||
|
return _data as S;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
class Result<T, V> {
|
|
||||||
|
|
||||||
const Result(this._data) : assert(_data is T || _data is V, 'Invalid data type: Must be either $T or $V');
|
|
||||||
final dynamic _data;
|
|
||||||
|
|
||||||
bool isType<S>() => _data is S;
|
|
||||||
|
|
||||||
S get<S>() {
|
|
||||||
assert(_data is S, 'Data is not $S');
|
|
||||||
|
|
||||||
return _data as S;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/presence.dart';
|
import 'package:moxxmpp/src/presence.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
|
||||||
@@ -289,7 +289,7 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a disco info query to the (full) jid [entity], optionally with node=[node].
|
/// Sends a disco info query to the (full) jid [entity], optionally with node=[node].
|
||||||
Future<Result<DiscoError, DiscoInfo>> discoInfoQuery(String entity, { String? node}) async {
|
Future<Result<DiscoError, DiscoInfo>> discoInfoQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
|
||||||
final cacheKey = DiscoCacheKey(entity, node);
|
final cacheKey = DiscoCacheKey(entity, node);
|
||||||
DiscoInfo? info;
|
DiscoInfo? info;
|
||||||
Completer<Result<DiscoError, DiscoInfo>>? completer;
|
Completer<Result<DiscoError, DiscoInfo>>? completer;
|
||||||
@@ -316,6 +316,7 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
|
|
||||||
final stanza = await getAttributes().sendStanza(
|
final stanza = await getAttributes().sendStanza(
|
||||||
buildDiscoInfoQueryStanza(entity, node),
|
buildDiscoInfoQueryStanza(entity, node),
|
||||||
|
encrypted: !shouldEncrypt,
|
||||||
);
|
);
|
||||||
final query = stanza.firstTag('query');
|
final query = stanza.firstTag('query');
|
||||||
if (query == null) {
|
if (query == null) {
|
||||||
@@ -359,9 +360,12 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a disco items query to the (full) jid [entity], optionally with node=[node].
|
/// Sends a disco items query to the (full) jid [entity], optionally with node=[node].
|
||||||
Future<Result<DiscoError, List<DiscoItem>>> discoItemsQuery(String entity, { String? node }) async {
|
Future<Result<DiscoError, List<DiscoItem>>> discoItemsQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
|
||||||
final stanza = await getAttributes()
|
final stanza = await getAttributes()
|
||||||
.sendStanza(buildDiscoItemsQueryStanza(entity, node: node)) as Stanza;
|
.sendStanza(
|
||||||
|
buildDiscoItemsQueryStanza(entity, node: node),
|
||||||
|
encrypted: !shouldEncrypt,
|
||||||
|
) as Stanza;
|
||||||
|
|
||||||
final query = stanza.firstTag('query');
|
final query = stanza.firstTag('query');
|
||||||
if (query == null) return Result(InvalidResponseDiscoError());
|
if (query == null) return Result(InvalidResponseDiscoError());
|
||||||
|
|||||||
@@ -7,15 +7,20 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
|
abstract class VCardError {}
|
||||||
|
|
||||||
|
class UnknownVCardError extends VCardError {}
|
||||||
|
|
||||||
|
class InvalidVCardError extends VCardError {}
|
||||||
|
|
||||||
class VCardPhoto {
|
class VCardPhoto {
|
||||||
|
|
||||||
const VCardPhoto({ this.binval });
|
const VCardPhoto({ this.binval });
|
||||||
final String? binval;
|
final String? binval;
|
||||||
}
|
}
|
||||||
|
|
||||||
class VCard {
|
class VCard {
|
||||||
|
|
||||||
const VCard({ this.nickname, this.url, this.photo });
|
const VCard({ this.nickname, this.url, this.photo });
|
||||||
final String? nickname;
|
final String? nickname;
|
||||||
final String? url;
|
final String? url;
|
||||||
@@ -23,7 +28,6 @@ class VCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class VCardManager extends XmppManagerBase {
|
class VCardManager extends XmppManagerBase {
|
||||||
|
|
||||||
VCardManager() : _lastHash = {}, super();
|
VCardManager() : _lastHash = {}, super();
|
||||||
final Map<String, String> _lastHash;
|
final Map<String, String> _lastHash;
|
||||||
|
|
||||||
@@ -59,12 +63,18 @@ class VCardManager extends XmppManagerBase {
|
|||||||
final lastHash = _lastHash[from];
|
final lastHash = _lastHash[from];
|
||||||
if (lastHash != hash) {
|
if (lastHash != hash) {
|
||||||
_lastHash[from] = hash;
|
_lastHash[from] = hash;
|
||||||
final vcard = await requestVCard(from);
|
final vcardResult = await requestVCard(from);
|
||||||
|
|
||||||
if (vcard != null) {
|
if (vcardResult.isType<VCard>()) {
|
||||||
final binval = vcard.photo?.binval;
|
final binval = vcardResult.get<VCard>().photo?.binval;
|
||||||
if (binval != null) {
|
if (binval != null) {
|
||||||
getAttributes().sendEvent(AvatarUpdatedEvent(jid: from, base64: binval, hash: hash));
|
getAttributes().sendEvent(
|
||||||
|
AvatarUpdatedEvent(
|
||||||
|
jid: from,
|
||||||
|
base64: binval,
|
||||||
|
hash: hash,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
logger.warning('No avatar data found');
|
logger.warning('No avatar data found');
|
||||||
}
|
}
|
||||||
@@ -95,7 +105,7 @@ class VCardManager extends XmppManagerBase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<VCard?> requestVCard(String jid) async {
|
Future<Result<VCardError, VCard>> requestVCard(String jid) async {
|
||||||
final result = await getAttributes().sendStanza(
|
final result = await getAttributes().sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
to: jid,
|
to: jid,
|
||||||
@@ -107,12 +117,13 @@ class VCardManager extends XmppManagerBase {
|
|||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
encrypted: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.attributes['type'] != 'result') return null;
|
if (result.attributes['type'] != 'result') return Result(UnknownVCardError());
|
||||||
final vcard = result.firstTag('vCard', xmlns: vCardTempXmlns);
|
final vcard = result.firstTag('vCard', xmlns: vCardTempXmlns);
|
||||||
if (vcard == null) return null;
|
if (vcard == null) return Result(UnknownVCardError());
|
||||||
|
|
||||||
return _parseVCard(vcard);
|
return Result(_parseVCard(vcard));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
@@ -16,7 +16,6 @@ import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0060/helpers.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/helpers.dart';
|
||||||
|
|
||||||
class PubSubPublishOptions {
|
class PubSubPublishOptions {
|
||||||
|
|
||||||
const PubSubPublishOptions({
|
const PubSubPublishOptions({
|
||||||
this.accessModel,
|
this.accessModel,
|
||||||
this.maxItems,
|
this.maxItems,
|
||||||
@@ -60,7 +59,6 @@ class PubSubPublishOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PubSubItem {
|
class PubSubItem {
|
||||||
|
|
||||||
const PubSubItem({ required this.id, required this.node, required this.payload });
|
const PubSubItem({ required this.id, required this.node, required this.payload });
|
||||||
final String id;
|
final String id;
|
||||||
final String node;
|
final String node;
|
||||||
|
|||||||
@@ -3,21 +3,24 @@ import 'package:moxxmpp/src/managers/base.dart';
|
|||||||
import 'package:moxxmpp/src/managers/namespaces.dart';
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
||||||
|
|
||||||
class UserAvatar {
|
abstract class AvatarError {}
|
||||||
|
|
||||||
|
class UnknownAvatarError extends AvatarError {}
|
||||||
|
|
||||||
|
class UserAvatar {
|
||||||
const UserAvatar({ required this.base64, required this.hash });
|
const UserAvatar({ required this.base64, required this.hash });
|
||||||
final String base64;
|
final String base64;
|
||||||
final String hash;
|
final String hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
class UserAvatarMetadata {
|
class UserAvatarMetadata {
|
||||||
|
|
||||||
const UserAvatarMetadata(
|
const UserAvatarMetadata(
|
||||||
this.id,
|
this.id,
|
||||||
this.length,
|
this.length,
|
||||||
@@ -49,6 +52,14 @@ class UserAvatarManager extends XmppManagerBase {
|
|||||||
@override
|
@override
|
||||||
Future<void> onXmppEvent(XmppEvent event) async {
|
Future<void> onXmppEvent(XmppEvent event) async {
|
||||||
if (event is PubSubNotificationEvent) {
|
if (event is PubSubNotificationEvent) {
|
||||||
|
if (event.item.node != userAvatarDataXmlns) return;
|
||||||
|
|
||||||
|
if (event.item.payload.tag != 'data' ||
|
||||||
|
event.item.payload.attributes['xmlns'] != userAvatarDataXmlns) {
|
||||||
|
logger.warning('Received avatar update from ${event.from} but the payload is invalid. Ignoring...');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
getAttributes().sendEvent(
|
getAttributes().sendEvent(
|
||||||
AvatarUpdatedEvent(
|
AvatarUpdatedEvent(
|
||||||
jid: event.from,
|
jid: event.from,
|
||||||
@@ -62,30 +73,30 @@ class UserAvatarManager extends XmppManagerBase {
|
|||||||
// TODO(PapaTutuWawa): Check for PEP support
|
// TODO(PapaTutuWawa): Check for PEP support
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
/// Requests the avatar from [jid]. Returns the avatar data if the request was
|
/// Requests the avatar from [jid]. Returns the avatar data if the request was
|
||||||
/// successful. Null otherwise
|
/// successful. Null otherwise
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, UserAvatar>> getUserAvatar(String jid) async {
|
||||||
Future<UserAvatar?> getUserAvatar(String jid) async {
|
|
||||||
final pubsub = _getPubSubManager();
|
final pubsub = _getPubSubManager();
|
||||||
final resultsRaw = await pubsub.getItems(jid, userAvatarDataXmlns);
|
final resultsRaw = await pubsub.getItems(jid, userAvatarDataXmlns);
|
||||||
if (resultsRaw.isType<PubSubError>()) return null;
|
if (resultsRaw.isType<PubSubError>()) return Result(UnknownAvatarError());
|
||||||
|
|
||||||
final results = resultsRaw.get<List<PubSubItem>>();
|
final results = resultsRaw.get<List<PubSubItem>>();
|
||||||
if (results.isEmpty) return null;
|
if (results.isEmpty) return Result(UnknownAvatarError());
|
||||||
|
|
||||||
final item = results[0];
|
final item = results[0];
|
||||||
return UserAvatar(
|
return Result(
|
||||||
base64: item.payload.innerText(),
|
UserAvatar(
|
||||||
hash: item.id,
|
base64: item.payload.innerText(),
|
||||||
|
hash: item.id,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publish the avatar data, [base64], on the pubsub node using [hash] as
|
/// Publish the avatar data, [base64], on the pubsub node using [hash] as
|
||||||
/// the item id. [hash] must be the SHA-1 hash of the image data, while
|
/// the item id. [hash] must be the SHA-1 hash of the image data, while
|
||||||
/// [base64] must be the base64-encoded version of the image data.
|
/// [base64] must be the base64-encoded version of the image data.
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, bool>> publishUserAvatar(String base64, String hash, bool public) async {
|
||||||
Future<bool> publishUserAvatar(String base64, String hash, bool public) async {
|
|
||||||
final pubsub = _getPubSubManager();
|
final pubsub = _getPubSubManager();
|
||||||
final result = await pubsub.publish(
|
final result = await pubsub.publish(
|
||||||
getAttributes().getFullJID().toBare().toString(),
|
getAttributes().getFullJID().toBare().toString(),
|
||||||
@@ -101,14 +112,15 @@ class UserAvatarManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return !result.isType<PubSubError>();
|
if (result.isType<PubSubError>()) return Result(UnknownAvatarError());
|
||||||
|
|
||||||
|
return const Result(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publish avatar metadata [metadata] to the User Avatar's metadata node. If [public]
|
/// Publish avatar metadata [metadata] to the User Avatar's metadata node. If [public]
|
||||||
/// is true, then the node will be set to an 'open' access model. If [public] is false,
|
/// is true, then the node will be set to an 'open' access model. If [public] is false,
|
||||||
/// then the node will be set to an 'roster' access model.
|
/// then the node will be set to an 'roster' access model.
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, bool>> publishUserAvatarMetadata(UserAvatarMetadata metadata, bool public) async {
|
||||||
Future<bool> publishUserAvatarMetadata(UserAvatarMetadata metadata, bool public) async {
|
|
||||||
final pubsub = _getPubSubManager();
|
final pubsub = _getPubSubManager();
|
||||||
final result = await pubsub.publish(
|
final result = await pubsub.publish(
|
||||||
getAttributes().getFullJID().toBare().toString(),
|
getAttributes().getFullJID().toBare().toString(),
|
||||||
@@ -135,39 +147,37 @@ class UserAvatarManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return result.isType<PubSubError>();
|
if (result.isType<PubSubError>()) return Result(UnknownAvatarError());
|
||||||
|
return const Result(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subscribe the data and metadata node of [jid].
|
/// Subscribe the data and metadata node of [jid].
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, bool>> subscribe(String jid) async {
|
||||||
Future<bool> subscribe(String jid) async {
|
|
||||||
await _getPubSubManager().subscribe(jid, userAvatarDataXmlns);
|
await _getPubSubManager().subscribe(jid, userAvatarDataXmlns);
|
||||||
await _getPubSubManager().subscribe(jid, userAvatarMetadataXmlns);
|
await _getPubSubManager().subscribe(jid, userAvatarMetadataXmlns);
|
||||||
|
|
||||||
return true;
|
return const Result(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unsubscribe the data and metadata node of [jid].
|
/// Unsubscribe the data and metadata node of [jid].
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, bool>> unsubscribe(String jid) async {
|
||||||
Future<bool> unsubscribe(String jid) async {
|
|
||||||
await _getPubSubManager().unsubscribe(jid, userAvatarDataXmlns);
|
await _getPubSubManager().unsubscribe(jid, userAvatarDataXmlns);
|
||||||
await _getPubSubManager().subscribe(jid, userAvatarMetadataXmlns);
|
await _getPubSubManager().subscribe(jid, userAvatarMetadataXmlns);
|
||||||
|
|
||||||
return true;
|
return const Result(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the PubSub Id of an avatar after doing a disco#items query.
|
/// Returns the PubSub Id of an avatar after doing a disco#items query.
|
||||||
/// Note that this assumes that there is only one (1) item published on
|
/// Note that this assumes that there is only one (1) item published on
|
||||||
/// the node.
|
/// the node.
|
||||||
// TODO(Unknown): Migrate to Resultsv2
|
Future<Result<AvatarError, String>> getAvatarId(String jid) async {
|
||||||
Future<String?> getAvatarId(String jid) async {
|
|
||||||
final disco = getAttributes().getManagerById(discoManager)! as DiscoManager;
|
final disco = getAttributes().getManagerById(discoManager)! as DiscoManager;
|
||||||
final response = await disco.discoItemsQuery(jid, node: userAvatarDataXmlns);
|
final response = await disco.discoItemsQuery(jid, node: userAvatarDataXmlns, shouldEncrypt: false);
|
||||||
if (response.isType<DiscoError>()) return null;
|
if (response.isType<DiscoError>()) return Result(UnknownAvatarError());
|
||||||
|
|
||||||
final items = response.get<List<DiscoItem>>();
|
final items = response.get<List<DiscoItem>>();
|
||||||
if (items.isEmpty) return null;
|
if (items.isEmpty) return Result(UnknownAvatarError());
|
||||||
|
|
||||||
return items.first.name;
|
return Result(items.first.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/nonzas.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/nonzas.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/state.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/state.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
||||||
@@ -23,7 +24,6 @@ enum _StreamManagementNegotiatorState {
|
|||||||
/// StreamManagementManager at least once before connecting, if stream resumption
|
/// StreamManagementManager at least once before connecting, if stream resumption
|
||||||
/// is wanted.
|
/// is wanted.
|
||||||
class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
||||||
|
|
||||||
StreamManagementNegotiator()
|
StreamManagementNegotiator()
|
||||||
: _state = _StreamManagementNegotiatorState.ready,
|
: _state = _StreamManagementNegotiatorState.ready,
|
||||||
_supported = false,
|
_supported = false,
|
||||||
@@ -59,7 +59,7 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when we matched the stream feature, so we know
|
// negotiate is only called when we matched the stream feature, so we know
|
||||||
// that the server advertises it.
|
// that the server advertises it.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
@@ -80,7 +80,8 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
_state = _StreamManagementNegotiatorState.enableRequested;
|
_state = _StreamManagementNegotiatorState.enableRequested;
|
||||||
attributes.sendNonza(StreamManagementEnableNonza());
|
attributes.sendNonza(StreamManagementEnableNonza());
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
return const Result(NegotiatorState.ready);
|
||||||
case _StreamManagementNegotiatorState.resumeRequested:
|
case _StreamManagementNegotiatorState.resumeRequested:
|
||||||
if (nonza.tag == 'resumed') {
|
if (nonza.tag == 'resumed') {
|
||||||
_log.finest('Stream Management resumption successful');
|
_log.finest('Stream Management resumption successful');
|
||||||
@@ -97,7 +98,7 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
_resumeFailed = false;
|
_resumeFailed = false;
|
||||||
_isResumed = true;
|
_isResumed = true;
|
||||||
state = NegotiatorState.skipRest;
|
return const Result(NegotiatorState.skipRest);
|
||||||
} else {
|
} else {
|
||||||
// We assume it is <failed />
|
// We assume it is <failed />
|
||||||
_log.info('Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...');
|
_log.info('Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...');
|
||||||
@@ -113,9 +114,8 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
_resumeFailed = true;
|
_resumeFailed = true;
|
||||||
_isResumed = false;
|
_isResumed = false;
|
||||||
_state = _StreamManagementNegotiatorState.ready;
|
_state = _StreamManagementNegotiatorState.ready;
|
||||||
state = NegotiatorState.retryLater;
|
return const Result(NegotiatorState.retryLater);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case _StreamManagementNegotiatorState.enableRequested:
|
case _StreamManagementNegotiatorState.enableRequested:
|
||||||
if (nonza.tag == 'enabled') {
|
if (nonza.tag == 'enabled') {
|
||||||
_log.finest('Stream Management enabled');
|
_log.finest('Stream Management enabled');
|
||||||
@@ -133,14 +133,12 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
} else {
|
} else {
|
||||||
// We assume a <failed />
|
// We assume a <failed />
|
||||||
_log.warning('Stream Management enablement failed');
|
_log.warning('Stream Management enablement failed');
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ class StreamManagementManager extends XmppManagerBase {
|
|||||||
];
|
];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingPreStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
callback: _onServerStanzaReceived,
|
callback: _onServerStanzaReceived,
|
||||||
priority: 9999,
|
priority: 9999,
|
||||||
@@ -185,9 +185,18 @@ class StreamManagementManager extends XmppManagerBase {
|
|||||||
_disableStreamManagement();
|
_disableStreamManagement();
|
||||||
_streamResumed = false;
|
_streamResumed = false;
|
||||||
} else if (event is ConnectionStateChangedEvent) {
|
} else if (event is ConnectionStateChangedEvent) {
|
||||||
if (event.state == XmppConnectionState.connected) {
|
switch (event.state) {
|
||||||
|
case XmppConnectionState.connected:
|
||||||
// Push out all pending stanzas
|
// Push out all pending stanzas
|
||||||
await onStreamResumed(0);
|
await onStreamResumed(0);
|
||||||
|
break;
|
||||||
|
case XmppConnectionState.error:
|
||||||
|
case XmppConnectionState.notConnected:
|
||||||
|
_stopAckTimer();
|
||||||
|
break;
|
||||||
|
case XmppConnectionState.connecting:
|
||||||
|
// NOOP
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,12 @@ import 'package:moxxmpp/src/stanza.dart';
|
|||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class DelayedDelivery {
|
class DelayedDelivery {
|
||||||
|
|
||||||
const DelayedDelivery(this.from, this.timestamp);
|
const DelayedDelivery(this.from, this.timestamp);
|
||||||
final DateTime timestamp;
|
final DateTime timestamp;
|
||||||
final String from;
|
final String from;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DelayedDeliveryManager extends XmppManagerBase {
|
class DelayedDeliveryManager extends XmppManagerBase {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => delayedDeliveryManager;
|
String getId() => delayedDeliveryManager;
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,18 @@ import 'package:moxxmpp/src/stringxml.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0297.dart';
|
import 'package:moxxmpp/src/xeps/xep_0297.dart';
|
||||||
|
|
||||||
|
/// This manager class implements support for XEP-0280.
|
||||||
class CarbonsManager extends XmppManagerBase {
|
class CarbonsManager extends XmppManagerBase {
|
||||||
|
CarbonsManager() : super();
|
||||||
|
|
||||||
CarbonsManager() : _isEnabled = false, _supported = false, _gotSupported = false, super();
|
/// Indicates that message carbons are enabled.
|
||||||
bool _isEnabled;
|
bool _isEnabled = false;
|
||||||
bool _supported;
|
|
||||||
bool _gotSupported;
|
/// Indicates that the server supports message carbons.
|
||||||
|
bool _supported = false;
|
||||||
|
|
||||||
|
/// Indicates that we know that [CarbonsManager._supported] is accurate.
|
||||||
|
bool _gotSupported = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => carbonsManager;
|
String getId() => carbonsManager;
|
||||||
@@ -26,13 +32,12 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
String getName() => 'CarbonsManager';
|
String getName() => 'CarbonsManager';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingPreStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
stanzaTag: 'message',
|
stanzaTag: 'message',
|
||||||
tagName: 'received',
|
tagName: 'received',
|
||||||
tagXmlns: carbonsXmlns,
|
tagXmlns: carbonsXmlns,
|
||||||
callback: _onMessageReceived,
|
callback: _onMessageReceived,
|
||||||
// Before all managers the message manager depends on
|
|
||||||
priority: -98,
|
priority: -98,
|
||||||
),
|
),
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
@@ -40,7 +45,6 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
tagName: 'sent',
|
tagName: 'sent',
|
||||||
tagXmlns: carbonsXmlns,
|
tagXmlns: carbonsXmlns,
|
||||||
callback: _onMessageSent,
|
callback: _onMessageSent,
|
||||||
// Before all managers the message manager depends on
|
|
||||||
priority: -98,
|
priority: -98,
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
@@ -104,10 +108,15 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
stanza: carbon,
|
stanza: carbon,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a request to the server, asking it to enable Message Carbons.
|
||||||
|
///
|
||||||
|
/// Returns true if carbons were enabled. False, if not.
|
||||||
Future<bool> enableCarbons() async {
|
Future<bool> enableCarbons() async {
|
||||||
final result = await getAttributes().sendStanza(
|
final attrs = getAttributes();
|
||||||
|
final result = await attrs.sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
|
to: attrs.getFullJID().toBare().toString(),
|
||||||
type: 'set',
|
type: 'set',
|
||||||
children: [
|
children: [
|
||||||
XMLNode.xmlns(
|
XMLNode.xmlns(
|
||||||
@@ -132,6 +141,9 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a request to the server, asking it to disable Message Carbons.
|
||||||
|
///
|
||||||
|
/// Returns true if carbons were disabled. False, if not.
|
||||||
Future<bool> disableCarbons() async {
|
Future<bool> disableCarbons() async {
|
||||||
final result = await getAttributes().sendStanza(
|
final result = await getAttributes().sendStanza(
|
||||||
Stanza.iq(
|
Stanza.iq(
|
||||||
@@ -159,12 +171,22 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// True if Message Carbons are enabled. False, if not.
|
||||||
|
bool get isEnabled => _isEnabled;
|
||||||
|
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
void forceEnable() {
|
void forceEnable() {
|
||||||
_isEnabled = true;
|
_isEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if a carbon sent by [senderJid] is valid to prevent vulnerabilities like
|
||||||
|
/// the ones listed at https://xmpp.org/extensions/xep-0280.html#security.
|
||||||
|
///
|
||||||
|
/// Returns true if the carbon is valid. Returns false if not.
|
||||||
bool isCarbonValid(JID senderJid) {
|
bool isCarbonValid(JID senderJid) {
|
||||||
return _isEnabled && senderJid == getAttributes().getConnectionSettings().jid.toBare();
|
return _isEnabled && getAttributes().getFullJID().bareCompare(
|
||||||
|
senderJid,
|
||||||
|
ensureBare: true,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
packages/moxxmpp/lib/src/xeps/xep_0308.dart
Normal file
50
packages/moxxmpp/lib/src/xeps/xep_0308.dart
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
|
XMLNode makeLastMessageCorrectionEdit(String id) {
|
||||||
|
return XMLNode.xmlns(
|
||||||
|
tag: 'replace',
|
||||||
|
xmlns: lmcXmlns,
|
||||||
|
attributes: <String, String>{
|
||||||
|
'id': id,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class LastMessageCorrectionManager extends XmppManagerBase {
|
||||||
|
@override
|
||||||
|
String getName() => 'LastMessageCorrectionManager';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getId() => lastMessageCorrectionManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<String> getDiscoFeatures() => [ lmcXmlns ];
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
StanzaHandler(
|
||||||
|
stanzaTag: 'message',
|
||||||
|
tagName: 'replace',
|
||||||
|
tagXmlns: lmcXmlns,
|
||||||
|
callback: _onMessage,
|
||||||
|
// Before the message handler
|
||||||
|
priority: -99,
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _onMessage(Stanza stanza, StanzaHandlerData state) async {
|
||||||
|
final edit = stanza.firstTag('replace', xmlns: lmcXmlns)!;
|
||||||
|
return state.copyWith(
|
||||||
|
lastMessageCorrectionSid: edit.attributes['id']! as String,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,8 +8,18 @@ enum MessageProcessingHint {
|
|||||||
store,
|
store,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// NOTE: We do not define a function for turning a Message Processing Hint element into
|
MessageProcessingHint messageProcessingHintFromXml(XMLNode element) {
|
||||||
/// an enum value since the elements do not concern us as a client.
|
switch (element.tag) {
|
||||||
|
case 'no-permanent-store': return MessageProcessingHint.noPermanentStore;
|
||||||
|
case 'no-store': return MessageProcessingHint.noStore;
|
||||||
|
case 'no-copy': return MessageProcessingHint.noCopies;
|
||||||
|
case 'store': return MessageProcessingHint.store;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(false, 'Invalid Message Processing Hint: ${element.tag}');
|
||||||
|
return MessageProcessingHint.noStore;
|
||||||
|
}
|
||||||
|
|
||||||
extension XmlExtension on MessageProcessingHint {
|
extension XmlExtension on MessageProcessingHint {
|
||||||
XMLNode toXml() {
|
XMLNode toXml() {
|
||||||
String tag;
|
String tag;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
import 'package:moxxmpp/src/negotiators/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
import 'package:moxxmpp/src/negotiators/negotiator.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
class CSIActiveNonza extends XMLNode {
|
class CSIActiveNonza extends XMLNode {
|
||||||
CSIActiveNonza() : super(
|
CSIActiveNonza() : super(
|
||||||
@@ -32,11 +33,11 @@ class CSINegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
bool get isSupported => _supported;
|
bool get isSupported => _supported;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
// negotiate is only called when the negotiator matched, meaning the server
|
// negotiate is only called when the negotiator matched, meaning the server
|
||||||
// advertises CSI.
|
// advertises CSI.
|
||||||
_supported = true;
|
_supported = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -58,8 +58,16 @@ class StableIdManager extends XmppManagerBase {
|
|||||||
String? stanzaIdBy;
|
String? stanzaIdBy;
|
||||||
final originIdTag = message.firstTag('origin-id', xmlns: stableIdXmlns);
|
final originIdTag = message.firstTag('origin-id', xmlns: stableIdXmlns);
|
||||||
final stanzaIdTag = message.firstTag('stanza-id', xmlns: stableIdXmlns);
|
final stanzaIdTag = message.firstTag('stanza-id', xmlns: stableIdXmlns);
|
||||||
if (originIdTag != null || stanzaIdTag != null) {
|
|
||||||
logger.finest('Found Unique and Stable Stanza Id tag');
|
// Process the origin id
|
||||||
|
if (originIdTag != null) {
|
||||||
|
logger.finest('Found origin Id tag');
|
||||||
|
originId = originIdTag.attributes['id']! as String;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the stanza id tag
|
||||||
|
if (stanzaIdTag != null) {
|
||||||
|
logger.finest('Found stanza Id tag');
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
final disco = attrs.getManagerById<DiscoManager>(discoManager)!;
|
final disco = attrs.getManagerById<DiscoManager>(discoManager)!;
|
||||||
final result = await disco.discoInfoQuery(from.toString());
|
final result = await disco.discoInfoQuery(from.toString());
|
||||||
@@ -68,17 +76,10 @@ class StableIdManager extends XmppManagerBase {
|
|||||||
logger.finest('Got info for ${from.toString()}');
|
logger.finest('Got info for ${from.toString()}');
|
||||||
if (info.features.contains(stableIdXmlns)) {
|
if (info.features.contains(stableIdXmlns)) {
|
||||||
logger.finest('${from.toString()} supports $stableIdXmlns.');
|
logger.finest('${from.toString()} supports $stableIdXmlns.');
|
||||||
|
stanzaId = stanzaIdTag.attributes['id']! as String;
|
||||||
if (originIdTag != null) {
|
stanzaIdBy = stanzaIdTag.attributes['by']! as String;
|
||||||
originId = originIdTag.attributes['id']! as String;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stanzaIdTag != null) {
|
|
||||||
stanzaId = stanzaIdTag.attributes['id']! as String;
|
|
||||||
stanzaIdBy = stanzaIdTag.attributes['by']! as String;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
logger.finest('${from.toString()} does not support $stableIdXmlns. Ignoring... ');
|
logger.finest('${from.toString()} does not support $stableIdXmlns. Ignoring stanza id... ');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.finest('Failed to find out if ${from.toString()} supports $stableIdXmlns. Ignoring... ');
|
logger.finest('Failed to find out if ${from.toString()} supports $stableIdXmlns. Ignoring... ');
|
||||||
|
|||||||
10
packages/moxxmpp/lib/src/xeps/xep_0363/errors.dart
Normal file
10
packages/moxxmpp/lib/src/xeps/xep_0363/errors.dart
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
abstract class HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Returned when we don't know what JID to ask for an upload slot
|
||||||
|
class NoEntityKnownError extends HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Returned when the file we want to upload is too big
|
||||||
|
class FileTooBigError extends HttpFileUploadError {}
|
||||||
|
|
||||||
|
/// Unspecified errors
|
||||||
|
class UnknownHttpFileUploadError extends HttpFileUploadError {}
|
||||||
@@ -7,19 +7,15 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/error.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0363/errors.dart';
|
||||||
const errorNoUploadServer = 1;
|
|
||||||
const errorFileTooBig = 2;
|
|
||||||
const errorGeneric = 3;
|
|
||||||
|
|
||||||
const allowedHTTPHeaders = [ 'authorization', 'cookie', 'expires' ];
|
const allowedHTTPHeaders = [ 'authorization', 'cookie', 'expires' ];
|
||||||
|
|
||||||
class HttpFileUploadSlot {
|
class HttpFileUploadSlot {
|
||||||
|
|
||||||
const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers);
|
const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers);
|
||||||
final String putUrl;
|
final String putUrl;
|
||||||
final String getUrl;
|
final String getUrl;
|
||||||
@@ -45,7 +41,6 @@ Map<String, String> prepareHeaders(Map<String, String> headers) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HttpFileUploadManager extends XmppManagerBase {
|
class HttpFileUploadManager extends XmppManagerBase {
|
||||||
|
|
||||||
HttpFileUploadManager() : _gotSupported = false, _supported = false, super();
|
HttpFileUploadManager() : _gotSupported = false, _supported = false, super();
|
||||||
JID? _entityJid;
|
JID? _entityJid;
|
||||||
int? _maxUploadSize;
|
int? _maxUploadSize;
|
||||||
@@ -119,17 +114,17 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
/// the file's size in octets. [contentType] is optional and refers to the file's
|
/// the file's size in octets. [contentType] is optional and refers to the file's
|
||||||
/// Mime type.
|
/// Mime type.
|
||||||
/// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise.
|
/// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise.
|
||||||
Future<MayFail<HttpFileUploadSlot>> requestUploadSlot(String filename, int filesize, { String? contentType }) async {
|
Future<Result<HttpFileUploadSlot, HttpFileUploadError>> requestUploadSlot(String filename, int filesize, { String? contentType }) async {
|
||||||
if (!(await isSupported())) return MayFail.failure(errorNoUploadServer);
|
if (!(await isSupported())) return Result(NoEntityKnownError());
|
||||||
|
|
||||||
if (_entityJid == null) {
|
if (_entityJid == null) {
|
||||||
logger.warning('Attempted to request HTTP File Upload slot but no entity is known to send this request to.');
|
logger.warning('Attempted to request HTTP File Upload slot but no entity is known to send this request to.');
|
||||||
return MayFail.failure(errorNoUploadServer);
|
return Result(NoEntityKnownError());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_maxUploadSize != null && filesize > _maxUploadSize!) {
|
if (_maxUploadSize != null && filesize > _maxUploadSize!) {
|
||||||
logger.warning('Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit');
|
logger.warning('Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit');
|
||||||
return MayFail.failure(errorFileTooBig);
|
return Result(FileTooBigError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
@@ -154,7 +149,7 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
if (response.attributes['type']! != 'result') {
|
if (response.attributes['type']! != 'result') {
|
||||||
logger.severe('Failed to request HTTP File Upload slot.');
|
logger.severe('Failed to request HTTP File Upload slot.');
|
||||||
// TODO(Unknown): Be more precise
|
// TODO(Unknown): Be more precise
|
||||||
return MayFail.failure(errorGeneric);
|
return Result(UnknownHttpFileUploadError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final slot = response.firstTag('slot', xmlns: httpFileUploadXmlns)!;
|
final slot = response.firstTag('slot', xmlns: httpFileUploadXmlns)!;
|
||||||
@@ -169,7 +164,7 @@ class HttpFileUploadManager extends XmppManagerBase {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return MayFail.success(
|
return Result(
|
||||||
HttpFileUploadSlot(
|
HttpFileUploadSlot(
|
||||||
putUrl,
|
putUrl,
|
||||||
getUrl,
|
getUrl,
|
||||||
@@ -7,3 +7,5 @@ class InvalidAffixElementsException with Exception {}
|
|||||||
class OmemoNotSupportedForContactException extends OmemoError {}
|
class OmemoNotSupportedForContactException extends OmemoError {}
|
||||||
|
|
||||||
class EncryptionFailedException with Exception {}
|
class EncryptionFailedException with Exception {}
|
||||||
|
|
||||||
|
class InvalidEnvelopePayloadException with Exception {}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:collection';
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxlib/moxlib.dart';
|
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
@@ -12,12 +10,13 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/types/resultv2.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0280.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0334.dart';
|
import 'package:moxxmpp/src/xeps/xep_0334.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
import 'package:moxxmpp/src/xeps/xep_0380.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
import 'package:moxxmpp/src/xeps/xep_0384/crypto.dart';
|
||||||
@@ -25,7 +24,7 @@ import 'package:moxxmpp/src/xeps/xep_0384/errors.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0384/helpers.dart';
|
import 'package:moxxmpp/src/xeps/xep_0384/helpers.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0384/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0384/types.dart';
|
||||||
import 'package:omemo_dart/omemo_dart.dart';
|
import 'package:omemo_dart/omemo_dart.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:xml/xml.dart';
|
||||||
|
|
||||||
const _doNotEncryptList = [
|
const _doNotEncryptList = [
|
||||||
// XEP-0033
|
// XEP-0033
|
||||||
@@ -43,18 +42,7 @@ const _doNotEncryptList = [
|
|||||||
DoNotEncrypt('stanza-id', stableIdXmlns),
|
DoNotEncrypt('stanza-id', stableIdXmlns),
|
||||||
];
|
];
|
||||||
|
|
||||||
abstract class OmemoManager extends XmppManagerBase {
|
abstract class BaseOmemoManager extends XmppManagerBase {
|
||||||
|
|
||||||
OmemoManager() : _handlerLock = Lock(), _handlerFutures = {}, super();
|
|
||||||
|
|
||||||
final Lock _handlerLock;
|
|
||||||
final Map<JID, Queue<Completer<void>>> _handlerFutures;
|
|
||||||
|
|
||||||
final Map<JID, List<int>> _deviceMap = {};
|
|
||||||
|
|
||||||
// Mapping whether we already tried to subscribe to the JID's devices node
|
|
||||||
final Map<JID, bool> _subscriptionMap = {};
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => omemoManager;
|
String getId() => omemoManager;
|
||||||
|
|
||||||
@@ -66,27 +54,24 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingPreStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
stanzaTag: 'iq',
|
stanzaTag: 'iq',
|
||||||
tagXmlns: omemoXmlns,
|
tagXmlns: omemoXmlns,
|
||||||
tagName: 'encrypted',
|
tagName: 'encrypted',
|
||||||
callback: _onIncomingStanza,
|
callback: _onIncomingStanza,
|
||||||
priority: 9999,
|
|
||||||
),
|
),
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
stanzaTag: 'presence',
|
stanzaTag: 'presence',
|
||||||
tagXmlns: omemoXmlns,
|
tagXmlns: omemoXmlns,
|
||||||
tagName: 'encrypted',
|
tagName: 'encrypted',
|
||||||
callback: _onIncomingStanza,
|
callback: _onIncomingStanza,
|
||||||
priority: 9999,
|
|
||||||
),
|
),
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
stanzaTag: 'message',
|
stanzaTag: 'message',
|
||||||
tagXmlns: omemoXmlns,
|
tagXmlns: omemoXmlns,
|
||||||
tagName: 'encrypted',
|
tagName: 'encrypted',
|
||||||
callback: _onIncomingStanza,
|
callback: _onIncomingStanza,
|
||||||
priority: -98,
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -128,60 +113,31 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
} else {
|
} else {
|
||||||
// Someone published to their device list node
|
// Someone published to their device list node
|
||||||
logger.finest('Got devices $ids');
|
logger.finest('Got devices $ids');
|
||||||
_deviceMap[jid] = ids;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tell the OmemoManager
|
||||||
|
(await getOmemoManager())
|
||||||
|
.onDeviceListUpdate(jid.toString(), ids);
|
||||||
|
|
||||||
// Generate an event
|
// Generate an event
|
||||||
getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids));
|
getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@visibleForOverriding
|
@visibleForOverriding
|
||||||
Future<OmemoSessionManager> getSessionManager();
|
Future<OmemoManager> getOmemoManager();
|
||||||
|
|
||||||
/// Wrapper around using getSessionManager and then calling encryptToJids on it.
|
|
||||||
Future<EncryptionResult> _encryptToJids(List<String> jids, String? plaintext, { List<OmemoBundle>? newSessions }) async {
|
|
||||||
final session = await getSessionManager();
|
|
||||||
return session.encryptToJids(jids, plaintext, newSessions: newSessions);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrapper around using getSessionManager and then calling encryptToJids on it.
|
|
||||||
Future<String?> _decryptMessage(List<int>? ciphertext, String senderJid, int senderDeviceId, List<EncryptedKey> keys, int sendTimestamp) async {
|
|
||||||
final session = await getSessionManager();
|
|
||||||
return session.decryptMessage(
|
|
||||||
ciphertext,
|
|
||||||
senderJid,
|
|
||||||
senderDeviceId,
|
|
||||||
keys,
|
|
||||||
sendTimestamp,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrapper around using getSessionManager and then calling getDeviceId on it.
|
/// Wrapper around using getSessionManager and then calling getDeviceId on it.
|
||||||
Future<int> _getDeviceId() async {
|
Future<int> _getDeviceId() async => (await getOmemoManager()).getDeviceId();
|
||||||
final session = await getSessionManager();
|
|
||||||
return session.getDeviceId();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrapper around using getSessionManager and then calling getDeviceId on it.
|
/// Wrapper around using getSessionManager and then calling getDeviceId on it.
|
||||||
Future<OmemoBundle> _getDeviceBundle() async {
|
Future<OmemoBundle> _getDeviceBundle() async {
|
||||||
final session = await getSessionManager();
|
final om = await getOmemoManager();
|
||||||
return session.getDeviceBundle();
|
final device = await om.getDevice();
|
||||||
|
return device.toBundle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper around using getSessionManager and then calling isRatchetAcknowledged on it.
|
|
||||||
Future<bool> _isRatchetAcknowledged(String jid, int deviceId) async {
|
|
||||||
final session = await getSessionManager();
|
|
||||||
return session.isRatchetAcknowledged(jid, deviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrapper around checking if [jid] appears in the session manager's device map.
|
|
||||||
Future<bool> _hasSessionWith(String jid) async {
|
|
||||||
final session = await getSessionManager();
|
|
||||||
final deviceMap = await session.getDeviceMap();
|
|
||||||
return deviceMap.containsKey(jid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Determines what child elements of a stanza should be encrypted. If shouldEncrypt
|
/// Determines what child elements of a stanza should be encrypted. If shouldEncrypt
|
||||||
/// returns true for [element], then [element] will be encrypted. If shouldEncrypt
|
/// returns true for [element], then [element] will be encrypted. If shouldEncrypt
|
||||||
/// returns false, then [element] won't be encrypted.
|
/// returns false, then [element] won't be encrypted.
|
||||||
@@ -206,56 +162,51 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
/// an attached payload, if [children] is not null, or an empty OMEMO message if
|
/// an attached payload, if [children] is not null, or an empty OMEMO message if
|
||||||
/// [children] is null. This function takes care of creating the affix elements as
|
/// [children] is null. This function takes care of creating the affix elements as
|
||||||
/// specified by both XEP-0420 and XEP-0384.
|
/// specified by both XEP-0420 and XEP-0384.
|
||||||
/// [jids] is the list of JIDs the payload should be encrypted for.
|
/// [toJid] is the list of JIDs the payload should be encrypted for.
|
||||||
Future<XMLNode> _encryptChildren(List<XMLNode>? children, List<String> jids, String toJid, List<OmemoBundle> newSessions) async {
|
String _buildEnvelope(List<XMLNode> children, String toJid) {
|
||||||
XMLNode? payload;
|
final payload = XMLNode.xmlns(
|
||||||
if (children != null) {
|
tag: 'envelope',
|
||||||
payload = XMLNode.xmlns(
|
xmlns: sceXmlns,
|
||||||
tag: 'envelope',
|
children: [
|
||||||
xmlns: sceXmlns,
|
XMLNode(
|
||||||
children: [
|
tag: 'content',
|
||||||
XMLNode(
|
children: children,
|
||||||
tag: 'content',
|
),
|
||||||
children: children,
|
|
||||||
),
|
|
||||||
|
|
||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'rpad',
|
tag: 'rpad',
|
||||||
text: generateRpad(),
|
text: generateRpad(),
|
||||||
),
|
),
|
||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'to',
|
tag: 'to',
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
'jid': toJid,
|
'jid': toJid,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'from',
|
tag: 'from',
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
'jid': getAttributes().getFullJID().toString(),
|
'jid': getAttributes().getFullJID().toString(),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
/*
|
/*
|
||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'time',
|
tag: 'time',
|
||||||
// TODO(Unknown): Implement
|
// TODO(Unknown): Implement
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
'stamp': '',
|
'stamp': '',
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
*/
|
*/
|
||||||
],
|
],
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
final encryptedEnvelope = await _encryptToJids(
|
|
||||||
jids,
|
|
||||||
payload?.toXml(),
|
|
||||||
newSessions: newSessions,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return payload.toXml();
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLNode _buildEncryptedElement(EncryptionResult result, String recipientJid, int deviceId) {
|
||||||
final keyElements = <String, List<XMLNode>>{};
|
final keyElements = <String, List<XMLNode>>{};
|
||||||
for (final key in encryptedEnvelope.encryptedKeys) {
|
for (final key in result.encryptedKeys) {
|
||||||
final keyElement = XMLNode(
|
final keyElement = XMLNode(
|
||||||
tag: 'key',
|
tag: 'key',
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
@@ -283,11 +234,11 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
var payloadElement = <XMLNode>[];
|
var payloadElement = <XMLNode>[];
|
||||||
if (payload != null) {
|
if (result.ciphertext != null) {
|
||||||
payloadElement = [
|
payloadElement = [
|
||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'payload',
|
tag: 'payload',
|
||||||
text: base64.encode(encryptedEnvelope.ciphertext!),
|
text: base64.encode(result.ciphertext!),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -300,7 +251,7 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
XMLNode(
|
XMLNode(
|
||||||
tag: 'header',
|
tag: 'header',
|
||||||
attributes: <String, String>{
|
attributes: <String, String>{
|
||||||
'sid': (await _getDeviceId()).toString(),
|
'sid': deviceId.toString(),
|
||||||
},
|
},
|
||||||
children: keysElements,
|
children: keysElements,
|
||||||
),
|
),
|
||||||
@@ -308,136 +259,18 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A logging wrapper around acking the ratchet with [jid] with identifier [deviceId].
|
/// For usage with omemo_dart's OmemoManager.
|
||||||
Future<void> _ackRatchet(String jid, int deviceId) async {
|
Future<void> sendEmptyMessageImpl(EncryptionResult result, String toJid) async {
|
||||||
logger.finest('Acking ratchet $jid:$deviceId');
|
|
||||||
final session = await getSessionManager();
|
|
||||||
await session.ratchetAcknowledged(jid, deviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Figure out if new sessions need to be built. [toJid] is the JID of the entity we
|
|
||||||
/// want to send a message to. [children] refers to the unencrypted children of the
|
|
||||||
/// message. They are required to be passed because shouldIgnoreUnackedRatchets is
|
|
||||||
/// called here.
|
|
||||||
///
|
|
||||||
/// Either returns a list of bundles we "need" to build a session with or an OmemoError.
|
|
||||||
Future<Result<OmemoError, List<OmemoBundle>>> _findNewSessions(JID toJid, List<XMLNode> children) async {
|
|
||||||
final ownJid = getAttributes().getFullJID().toBare();
|
|
||||||
final session = await getSessionManager();
|
|
||||||
final ownId = await session.getDeviceId();
|
|
||||||
|
|
||||||
// Ignore our own device if it is the only published device on our devices node
|
|
||||||
if (toJid.toBare() == ownJid) {
|
|
||||||
final deviceList = await getDeviceList(ownJid);
|
|
||||||
if (deviceList.isType<List<int>>()) {
|
|
||||||
final devices = deviceList.get<List<int>>();
|
|
||||||
if (devices.length == 1 && devices.first == ownId) {
|
|
||||||
return const Result(<OmemoBundle>[]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final newSessions = List<OmemoBundle>.empty(growable: true);
|
|
||||||
final sessionAvailable = await _hasSessionWith(toJid.toString());
|
|
||||||
if (!sessionAvailable) {
|
|
||||||
logger.finest('No session for $toJid. Retrieving bundles to build a new session.');
|
|
||||||
final result = await retrieveDeviceBundles(toJid);
|
|
||||||
if (result.isType<List<OmemoBundle>>()) {
|
|
||||||
final bundles = result.get<List<OmemoBundle>>();
|
|
||||||
|
|
||||||
if (ownJid == toJid) {
|
|
||||||
logger.finest('Requesting bundles for own JID. Ignoring current device');
|
|
||||||
newSessions.addAll(bundles.where((bundle) => bundle.id != ownId));
|
|
||||||
} else {
|
|
||||||
newSessions.addAll(bundles);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.warning('Failed to retrieve device bundles for $toJid');
|
|
||||||
return Result(OmemoNotSupportedForContactException());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_subscriptionMap.containsKey(toJid)) {
|
|
||||||
await subscribeToDeviceList(toJid);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
final toBare = toJid.toBare();
|
|
||||||
final ratchetSessions = (await session.getDeviceMap())[toBare.toString()]!;
|
|
||||||
final deviceMapRaw = await getDeviceList(toBare);
|
|
||||||
if (!_subscriptionMap.containsKey(toBare)) {
|
|
||||||
unawaited(subscribeToDeviceList(toBare));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deviceMapRaw.isType<OmemoError>()) {
|
|
||||||
logger.warning('Failed to get device list');
|
|
||||||
return Result(UnknownOmemoError());
|
|
||||||
}
|
|
||||||
|
|
||||||
final deviceList = deviceMapRaw.get<List<int>>();
|
|
||||||
for (final id in deviceList) {
|
|
||||||
// We already have a session with that device
|
|
||||||
if (ratchetSessions.contains(id)) continue;
|
|
||||||
|
|
||||||
// Ignore requests for our own device.
|
|
||||||
if (toJid == ownJid && id == ownId) {
|
|
||||||
logger.finest('Attempted to request bundle for our own device $id, which is the current device. Skipping request...');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.finest('Retrieving bundle for $toJid:$id');
|
|
||||||
final bundle = await retrieveDeviceBundle(toJid, id);
|
|
||||||
if (bundle.isType<OmemoBundle>()) {
|
|
||||||
newSessions.add(bundle.get<OmemoBundle>());
|
|
||||||
} else {
|
|
||||||
logger.warning('Failed to retrieve bundle for $toJid:$id');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result(newSessions);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sends an empty Omemo message to [toJid].
|
|
||||||
///
|
|
||||||
/// If [findNewSessions] is true, then
|
|
||||||
/// new devices will be looked for first before sending the message. This means that
|
|
||||||
/// the new sessions will be included in the empty Omemo message. If false, then no
|
|
||||||
/// new sessions will be looked for before encrypting.
|
|
||||||
///
|
|
||||||
/// [calledFromCriticalSection] MUST NOT be used from outside the manager. If true, then
|
|
||||||
/// sendEmptyMessage will not attempt to enter the critical section guarding the
|
|
||||||
/// encryption and decryption. If false, then the critical section will be entered before
|
|
||||||
/// encryption and left after sending the message.
|
|
||||||
Future<void> sendEmptyMessage(JID toJid, {
|
|
||||||
bool findNewSessions = false,
|
|
||||||
@protected
|
|
||||||
bool calledFromCriticalSection = false,
|
|
||||||
}) async {
|
|
||||||
if (!calledFromCriticalSection) {
|
|
||||||
final completer = await _handlerEntry(toJid);
|
|
||||||
if (completer != null) {
|
|
||||||
await completer.future;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var newSessions = <OmemoBundle>[];
|
|
||||||
if (findNewSessions) {
|
|
||||||
final result = await _findNewSessions(toJid, <XMLNode>[]);
|
|
||||||
if (!result.isType<OmemoError>()) newSessions = result.get<List<OmemoBundle>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
final empty = await _encryptChildren(
|
|
||||||
null,
|
|
||||||
[toJid.toString()],
|
|
||||||
toJid.toString(),
|
|
||||||
newSessions,
|
|
||||||
);
|
|
||||||
|
|
||||||
await getAttributes().sendStanza(
|
await getAttributes().sendStanza(
|
||||||
Stanza.message(
|
Stanza.message(
|
||||||
to: toJid.toString(),
|
to: toJid,
|
||||||
type: 'chat',
|
type: 'chat',
|
||||||
children: [
|
children: [
|
||||||
empty,
|
_buildEncryptedElement(
|
||||||
|
result,
|
||||||
|
toJid,
|
||||||
|
await _getDeviceId(),
|
||||||
|
),
|
||||||
|
|
||||||
// Add a storage hint in case this is a message
|
// Add a storage hint in case this is a message
|
||||||
// Taken from the example at
|
// Taken from the example at
|
||||||
@@ -448,10 +281,28 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
awaitable: false,
|
awaitable: false,
|
||||||
encrypted: true,
|
encrypted: true,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!calledFromCriticalSection) {
|
/// Send a heartbeat message to [jid].
|
||||||
await _handlerExit(toJid);
|
Future<void> sendOmemoHeartbeat(String jid) async {
|
||||||
}
|
final om = await getOmemoManager();
|
||||||
|
await om.sendOmemoHeartbeat(jid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// For usage with omemo_dart's OmemoManager
|
||||||
|
Future<List<int>?> fetchDeviceList(String jid) async {
|
||||||
|
final result = await getDeviceList(JID.fromString(jid));
|
||||||
|
if (result.isType<OmemoError>()) return null;
|
||||||
|
|
||||||
|
return result.get<List<int>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// For usage with omemo_dart's OmemoManager
|
||||||
|
Future<OmemoBundle?> fetchDeviceBundle(String jid, int id) async {
|
||||||
|
final result = await retrieveDeviceBundle(JID.fromString(jid), id);
|
||||||
|
if (result.isType<OmemoError>()) return null;
|
||||||
|
|
||||||
|
return result.get<OmemoBundle>();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<StanzaHandlerData> _onOutgoingStanza(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onOutgoingStanza(Stanza stanza, StanzaHandlerData state) async {
|
||||||
@@ -462,6 +313,7 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
|
|
||||||
if (stanza.to == null) {
|
if (stanza.to == null) {
|
||||||
// We cannot encrypt in this case.
|
// We cannot encrypt in this case.
|
||||||
|
logger.finest('Not encrypting since stanza.to is null');
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -472,34 +324,7 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
} else {
|
} else {
|
||||||
logger.finest('shouldEncryptStanza returned true for message to $toJid.');
|
logger.finest('shouldEncryptStanza returned true for message to $toJid.');
|
||||||
}
|
}
|
||||||
|
|
||||||
final completer = await _handlerEntry(toJid);
|
|
||||||
if (completer != null) {
|
|
||||||
await completer.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
final newSessions = List<OmemoBundle>.empty(growable: true);
|
|
||||||
// Try to find new sessions for [toJid].
|
|
||||||
final resultToJid = await _findNewSessions(toJid, stanza.children);
|
|
||||||
if (resultToJid.isType<List<OmemoBundle>>()) {
|
|
||||||
newSessions.addAll(resultToJid.get<List<OmemoBundle>>());
|
|
||||||
} else {
|
|
||||||
if (resultToJid.isType<OmemoNotSupportedForContactException>()) {
|
|
||||||
await _handlerExit(toJid);
|
|
||||||
return state.copyWith(
|
|
||||||
cancel: true,
|
|
||||||
cancelReason: resultToJid.get<OmemoNotSupportedForContactException>(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to find new sessions for our own Jid.
|
|
||||||
final ownJid = getAttributes().getFullJID().toBare();
|
|
||||||
final resultOwnJid = await _findNewSessions(ownJid, stanza.children);
|
|
||||||
if (resultOwnJid.isType<List<OmemoBundle>>()) {
|
|
||||||
newSessions.addAll(resultOwnJid.get<List<OmemoBundle>>());
|
|
||||||
}
|
|
||||||
|
|
||||||
final toEncrypt = List<XMLNode>.empty(growable: true);
|
final toEncrypt = List<XMLNode>.empty(growable: true);
|
||||||
final children = List<XMLNode>.empty(growable: true);
|
final children = List<XMLNode>.empty(growable: true);
|
||||||
for (final child in stanza.children) {
|
for (final child in stanza.children) {
|
||||||
@@ -509,76 +334,56 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
toEncrypt.add(child);
|
toEncrypt.add(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.finest('Beginning encryption');
|
||||||
|
final carbonsEnabled = getAttributes()
|
||||||
|
.getManagerById<CarbonsManager>(carbonsManager)?.isEnabled ?? false;
|
||||||
|
final om = await getOmemoManager();
|
||||||
|
final result = await om.onOutgoingStanza(
|
||||||
|
OmemoOutgoingStanza(
|
||||||
|
[
|
||||||
|
toJid.toString(),
|
||||||
|
if (carbonsEnabled)
|
||||||
|
getAttributes().getFullJID().toBare().toString(),
|
||||||
|
],
|
||||||
|
_buildEnvelope(toEncrypt, toJid.toString()),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
logger.finest('Encryption done');
|
||||||
|
|
||||||
final jidsToEncryptFor = <String>[JID.fromString(stanza.to!).toBare().toString()];
|
if (!result.isSuccess(2)) {
|
||||||
// Prevent encrypting to self if there is only one device (ours).
|
final other = Map<String, dynamic>.from(state.other);
|
||||||
if (await _hasSessionWith(ownJid.toString())) {
|
other['encryption_error_jids'] = result.jidEncryptionErrors;
|
||||||
jidsToEncryptFor.add(ownJid.toString());
|
other['encryption_error_devices'] = result.deviceEncryptionErrors;
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
logger.finest('Encrypting stanza');
|
|
||||||
final encrypted = await _encryptChildren(
|
|
||||||
toEncrypt,
|
|
||||||
jidsToEncryptFor,
|
|
||||||
stanza.to!,
|
|
||||||
newSessions,
|
|
||||||
);
|
|
||||||
logger.finest('Encryption done');
|
|
||||||
|
|
||||||
children.add(encrypted);
|
|
||||||
|
|
||||||
// Only add EME when sending a message
|
|
||||||
if (stanza.tag == 'message') {
|
|
||||||
children.add(buildEmeElement(ExplicitEncryptionType.omemo2));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a storage hint in case this is a message
|
|
||||||
// Taken from the example at
|
|
||||||
// https://xmpp.org/extensions/xep-0384.html#message-structure-description.
|
|
||||||
if (stanza.tag == 'message') {
|
|
||||||
children.add(MessageProcessingHint.store.toXml());
|
|
||||||
}
|
|
||||||
|
|
||||||
await _handlerExit(toJid);
|
|
||||||
return state.copyWith(
|
|
||||||
stanza: state.stanza.copyWith(
|
|
||||||
children: children,
|
|
||||||
),
|
|
||||||
encrypted: true,
|
|
||||||
);
|
|
||||||
} catch (ex) {
|
|
||||||
logger.severe('Encryption failed! $ex');
|
|
||||||
await _handlerExit(toJid);
|
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
|
other: other,
|
||||||
cancel: true,
|
cancel: true,
|
||||||
cancelReason: EncryptionFailedException(),
|
|
||||||
other: {
|
|
||||||
...state.other,
|
|
||||||
'encryption_error': ex,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
final encrypted = _buildEncryptedElement(
|
||||||
/// This function returns true if the encryption scheme should ignore unacked ratchets
|
result,
|
||||||
/// and don't try to build a new ratchet even though there are unacked ones.
|
toJid.toString(),
|
||||||
/// The current logic is that chat states with no body ignore the "ack" state of the
|
await _getDeviceId(),
|
||||||
/// ratchets.
|
);
|
||||||
///
|
children.add(encrypted);
|
||||||
/// This function may be overriden. By default, the ack status of the ratchet is ignored
|
|
||||||
/// if we're sending a message containing chatstates or chat markers and the message does
|
// Only add message specific metadata when actually sending a message
|
||||||
/// not contain a <body /> element.
|
if (stanza.tag == 'message') {
|
||||||
@visibleForOverriding
|
children
|
||||||
bool shouldIgnoreUnackedRatchets(List<XMLNode> children) {
|
// Add EME data
|
||||||
return listContains(
|
..add(buildEmeElement(ExplicitEncryptionType.omemo2))
|
||||||
children,
|
// Add a storage hint in case this is a message
|
||||||
(XMLNode child) {
|
// Taken from the example at
|
||||||
return child.attributes['xmlns'] == chatStateXmlns || child.attributes['xmlns'] == chatMarkersXmlns;
|
// https://xmpp.org/extensions/xep-0384.html#message-structure-description.
|
||||||
},
|
..add(MessageProcessingHint.store.toXml());
|
||||||
) && !listContains(
|
}
|
||||||
children,
|
|
||||||
(XMLNode child) => child.tag == 'body',
|
return state.copyWith(
|
||||||
|
stanza: state.stanza.copyWith(
|
||||||
|
children: children,
|
||||||
|
),
|
||||||
|
encrypted: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,48 +393,12 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
@visibleForOverriding
|
@visibleForOverriding
|
||||||
Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza);
|
Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza);
|
||||||
|
|
||||||
/// Wrapper function that attempts to enter the encryption/decryption critical section.
|
|
||||||
/// In case the critical section could be entered, null is returned. If not, then a
|
|
||||||
/// Completer is returned whose future will resolve once the critical section can be
|
|
||||||
/// entered.
|
|
||||||
Future<Completer<void>?> _handlerEntry(JID fromJid) async {
|
|
||||||
return _handlerLock.synchronized(() {
|
|
||||||
if (_handlerFutures.containsKey(fromJid)) {
|
|
||||||
final c = Completer<void>();
|
|
||||||
_handlerFutures[fromJid]!.addLast(c);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
_handlerFutures[fromJid] = Queue();
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Wrapper function that exits the critical section.
|
|
||||||
Future<void> _handlerExit(JID fromJid) async {
|
|
||||||
await _handlerLock.synchronized(() {
|
|
||||||
if (_handlerFutures.containsKey(fromJid)) {
|
|
||||||
if (_handlerFutures[fromJid]!.isEmpty) {
|
|
||||||
_handlerFutures.remove(fromJid);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_handlerFutures[fromJid]!.removeFirst().complete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<StanzaHandlerData> _onIncomingStanza(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onIncomingStanza(Stanza stanza, StanzaHandlerData state) async {
|
||||||
final encrypted = stanza.firstTag('encrypted', xmlns: omemoXmlns);
|
final encrypted = stanza.firstTag('encrypted', xmlns: omemoXmlns);
|
||||||
if (encrypted == null) return state;
|
if (encrypted == null) return state;
|
||||||
if (stanza.from == null) return state;
|
if (stanza.from == null) return state;
|
||||||
|
|
||||||
final fromJid = JID.fromString(stanza.from!).toBare();
|
final fromJid = JID.fromString(stanza.from!).toBare();
|
||||||
final completer = await _handlerEntry(fromJid);
|
|
||||||
if (completer != null) {
|
|
||||||
await completer.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
final header = encrypted.firstTag('header')!;
|
final header = encrypted.firstTag('header')!;
|
||||||
final payloadElement = encrypted.firstTag('payload');
|
final payloadElement = encrypted.firstTag('payload');
|
||||||
final keys = List<EncryptedKey>.empty(growable: true);
|
final keys = List<EncryptedKey>.empty(growable: true);
|
||||||
@@ -650,118 +419,62 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
final ourJid = getAttributes().getFullJID();
|
final ourJid = getAttributes().getFullJID();
|
||||||
final sid = int.parse(header.attributes['sid']! as String);
|
final sid = int.parse(header.attributes['sid']! as String);
|
||||||
|
|
||||||
// Ensure that if we receive a message from a device that we don't know about, we
|
final om = await getOmemoManager();
|
||||||
// ensure that _deviceMap is up-to-date.
|
final result = await om.onIncomingStanza(
|
||||||
final devices = _deviceMap[fromJid] ?? <int>[];
|
OmemoIncomingStanza(
|
||||||
if (!devices.contains(sid)) {
|
|
||||||
await getDeviceList(fromJid);
|
|
||||||
}
|
|
||||||
|
|
||||||
String? decrypted;
|
|
||||||
try {
|
|
||||||
decrypted = await _decryptMessage(
|
|
||||||
payloadElement != null ? base64.decode(payloadElement.innerText()) : null,
|
|
||||||
fromJid.toString(),
|
fromJid.toString(),
|
||||||
sid,
|
sid,
|
||||||
keys,
|
|
||||||
state.delayedDelivery?.timestamp.millisecondsSinceEpoch ?? DateTime.now().millisecondsSinceEpoch,
|
state.delayedDelivery?.timestamp.millisecondsSinceEpoch ?? DateTime.now().millisecondsSinceEpoch,
|
||||||
);
|
keys,
|
||||||
} catch (ex) {
|
payloadElement?.innerText(),
|
||||||
logger.warning('Error occurred during message decryption: $ex');
|
),
|
||||||
|
);
|
||||||
|
|
||||||
await _handlerExit(fromJid);
|
final other = Map<String, dynamic>.from(state.other);
|
||||||
return state.copyWith(
|
var children = stanza.children;
|
||||||
other: {
|
if (result.error != null) {
|
||||||
...state.other,
|
other['encryption_error'] = result.error;
|
||||||
'encryption_error': ex,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
final isAcked = await _isRatchetAcknowledged(fromJid.toString(), sid);
|
|
||||||
if (!isAcked) {
|
|
||||||
// Unacked ratchet decrypted this message
|
|
||||||
if (decrypted != null) {
|
|
||||||
// The message is not empty, i.e. contains content
|
|
||||||
logger.finest('Received non-empty OMEMO encrypted message for unacked ratchet. Acking with empty OMEMO message.');
|
|
||||||
|
|
||||||
await _ackRatchet(fromJid.toString(), sid);
|
|
||||||
await sendEmptyMessage(fromJid, calledFromCriticalSection: true);
|
|
||||||
|
|
||||||
final envelope = XMLNode.fromString(decrypted);
|
|
||||||
final children = stanza.children.where(
|
|
||||||
(child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
|
|
||||||
).toList()
|
|
||||||
..addAll(envelope.firstTag('content')!.children);
|
|
||||||
|
|
||||||
final other = Map<String, dynamic>.from(state.other);
|
|
||||||
if (!checkAffixElements(envelope, stanza.from!, ourJid)) {
|
|
||||||
other['encryption_error'] = InvalidAffixElementsException();
|
|
||||||
}
|
|
||||||
|
|
||||||
await _handlerExit(fromJid);
|
|
||||||
return state.copyWith(
|
|
||||||
encrypted: true,
|
|
||||||
stanza: Stanza(
|
|
||||||
to: stanza.to,
|
|
||||||
from: stanza.from,
|
|
||||||
id: stanza.id,
|
|
||||||
type: stanza.type,
|
|
||||||
children: children,
|
|
||||||
tag: stanza.tag,
|
|
||||||
attributes: Map<String, String>.from(stanza.attributes),
|
|
||||||
),
|
|
||||||
other: other,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
logger.info('Received empty OMEMO message for unacked ratchet. Marking $fromJid:$sid as acked');
|
|
||||||
await _ackRatchet(fromJid.toString(), sid);
|
|
||||||
|
|
||||||
final ownId = await (await getSessionManager()).getDeviceId();
|
|
||||||
final kex = keys.any((key) => key.kex && key.rid == ownId);
|
|
||||||
if (kex) {
|
|
||||||
logger.info('Empty OMEMO message contained a kex. Answering.');
|
|
||||||
await sendEmptyMessage(fromJid, calledFromCriticalSection: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
await _handlerExit(fromJid);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// The ratchet that decrypted the message was acked
|
children = stanza.children.where(
|
||||||
if (decrypted != null) {
|
(child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
|
||||||
final envelope = XMLNode.fromString(decrypted);
|
).toList();
|
||||||
|
}
|
||||||
|
|
||||||
final children = stanza.children.where(
|
if (result.payload != null) {
|
||||||
(child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
|
XMLNode envelope;
|
||||||
).toList()
|
try {
|
||||||
..addAll(envelope.firstTag('content')!.children);
|
envelope = XMLNode.fromString(result.payload!);
|
||||||
|
} on XmlParserException catch (_) {
|
||||||
final other = Map<String, dynamic>.from(state.other);
|
logger.warning('Failed to parse envelope payload: ${result.payload!}');
|
||||||
if (!checkAffixElements(envelope, stanza.from!, ourJid)) {
|
other['encryption_error'] = InvalidEnvelopePayloadException();
|
||||||
other['encryption_error'] = InvalidAffixElementsException();
|
|
||||||
}
|
|
||||||
|
|
||||||
await _handlerExit(fromJid);
|
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
encrypted: true,
|
encrypted: true,
|
||||||
stanza: Stanza(
|
|
||||||
to: stanza.to,
|
|
||||||
from: stanza.from,
|
|
||||||
id: stanza.id,
|
|
||||||
type: stanza.type,
|
|
||||||
children: children,
|
|
||||||
tag: stanza.tag,
|
|
||||||
attributes: Map<String, String>.from(stanza.attributes),
|
|
||||||
),
|
|
||||||
other: other,
|
other: other,
|
||||||
);
|
);
|
||||||
} else {
|
}
|
||||||
logger.info('Received empty OMEMO message on acked ratchet. Doing nothing');
|
|
||||||
await _handlerExit(fromJid);
|
children.addAll(
|
||||||
return state;
|
envelope.firstTag('content')!.children,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!checkAffixElements(envelope, stanza.from!, ourJid)) {
|
||||||
|
other['encryption_error'] = InvalidAffixElementsException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return state.copyWith(
|
||||||
|
encrypted: true,
|
||||||
|
stanza: Stanza(
|
||||||
|
to: stanza.to,
|
||||||
|
from: stanza.from,
|
||||||
|
id: stanza.id,
|
||||||
|
type: stanza.type,
|
||||||
|
children: children,
|
||||||
|
tag: stanza.tag,
|
||||||
|
attributes: Map<String, String>.from(stanza.attributes),
|
||||||
|
),
|
||||||
|
other: other,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function that attempts to retrieve the raw XML payload from the
|
/// Convenience function that attempts to retrieve the raw XML payload from the
|
||||||
@@ -777,15 +490,12 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
|
|
||||||
/// Retrieves the OMEMO device list from [jid].
|
/// Retrieves the OMEMO device list from [jid].
|
||||||
Future<Result<OmemoError, List<int>>> getDeviceList(JID jid) async {
|
Future<Result<OmemoError, List<int>>> getDeviceList(JID jid) async {
|
||||||
if (_deviceMap.containsKey(jid)) return Result(_deviceMap[jid]);
|
|
||||||
|
|
||||||
final itemsRaw = await _retrieveDeviceListPayload(jid);
|
final itemsRaw = await _retrieveDeviceListPayload(jid);
|
||||||
if (itemsRaw.isType<OmemoError>()) return Result(UnknownOmemoError());
|
if (itemsRaw.isType<OmemoError>()) return Result(UnknownOmemoError());
|
||||||
|
|
||||||
final ids = itemsRaw.get<XMLNode>().children
|
final ids = itemsRaw.get<XMLNode>().children
|
||||||
.map((child) => int.parse(child.attributes['id']! as String))
|
.map((child) => int.parse(child.attributes['id']! as String))
|
||||||
.toList();
|
.toList();
|
||||||
_deviceMap[jid] = ids;
|
|
||||||
return Result(ids);
|
return Result(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -883,13 +593,9 @@ abstract class OmemoManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Subscribes to the device list PubSub node of [jid].
|
/// Subscribes to the device list PubSub node of [jid].
|
||||||
Future<void> subscribeToDeviceList(JID jid) async {
|
Future<void> subscribeToDeviceListImpl(String jid) async {
|
||||||
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
|
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
|
||||||
final result = await pm.subscribe(jid.toString(), omemoDevicesXmlns);
|
await pm.subscribe(jid, omemoDevicesXmlns);
|
||||||
|
|
||||||
if (!result.isType<PubSubError>()) {
|
|
||||||
_subscriptionMap[jid] = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to find out if [jid] supports omemo:2.
|
/// Attempts to find out if [jid] supports omemo:2.
|
||||||
|
|||||||
59
packages/moxxmpp/lib/src/xeps/xep_0424.dart
Normal file
59
packages/moxxmpp/lib/src/xeps/xep_0424.dart
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
|
||||||
|
class MessageRetractionData {
|
||||||
|
MessageRetractionData(this.id, this.fallback);
|
||||||
|
final String? fallback;
|
||||||
|
final String id;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageRetractionManager extends XmppManagerBase {
|
||||||
|
@override
|
||||||
|
String getName() => 'MessageRetractionManager';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getId() => messageRetractionManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<String> getDiscoFeatures() => [ messageRetractionXmlns ];
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
StanzaHandler(
|
||||||
|
stanzaTag: 'message',
|
||||||
|
callback: _onMessage,
|
||||||
|
// Before the MessageManager
|
||||||
|
priority: -99,
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
|
||||||
|
final applyTo = message.firstTag('apply-to', xmlns: fasteningXmlns);
|
||||||
|
if (applyTo == null) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
final retract = applyTo.firstTag('retract', xmlns: messageRetractionXmlns);
|
||||||
|
if (retract == null) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
final isFallbackBody = message.firstTag('fallback', xmlns: fallbackIndicationXmlns) != null;
|
||||||
|
|
||||||
|
return state.copyWith(
|
||||||
|
messageRetraction: MessageRetractionData(
|
||||||
|
applyTo.attributes['id']! as String,
|
||||||
|
isFallbackBody ?
|
||||||
|
message.firstTag('body')?.innerText() :
|
||||||
|
null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
68
packages/moxxmpp/lib/src/xeps/xep_0444.dart
Normal file
68
packages/moxxmpp/lib/src/xeps/xep_0444.dart
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
|
class MessageReactions {
|
||||||
|
const MessageReactions(this.messageId, this.emojis);
|
||||||
|
final String messageId;
|
||||||
|
final List<String> emojis;
|
||||||
|
|
||||||
|
XMLNode toXml() {
|
||||||
|
return XMLNode.xmlns(
|
||||||
|
tag: 'reactions',
|
||||||
|
xmlns: messageReactionsXmlns,
|
||||||
|
attributes: <String, String>{
|
||||||
|
'id': messageId,
|
||||||
|
},
|
||||||
|
children: emojis.map((emoji) {
|
||||||
|
return XMLNode(
|
||||||
|
tag: 'reaction',
|
||||||
|
text: emoji,
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageReactionsManager extends XmppManagerBase {
|
||||||
|
@override
|
||||||
|
List<String> getDiscoFeatures() => [ messageReactionsXmlns ];
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getName() => 'MessageReactionsManager';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getId() => messageReactionsManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
StanzaHandler(
|
||||||
|
stanzaTag: 'message',
|
||||||
|
tagName: 'reactions',
|
||||||
|
tagXmlns: messageReactionsXmlns,
|
||||||
|
callback: _onReactionsReceived,
|
||||||
|
// Before the message handler
|
||||||
|
priority: -99,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _onReactionsReceived(Stanza message, StanzaHandlerData state) async {
|
||||||
|
final reactionsElement = message.firstTag('reactions', xmlns: messageReactionsXmlns)!;
|
||||||
|
return state.copyWith(
|
||||||
|
messageReactions: MessageReactions(
|
||||||
|
reactionsElement.attributes['id']! as String,
|
||||||
|
reactionsElement.children
|
||||||
|
.where((c) => c.tag == 'reaction')
|
||||||
|
.map((c) => c.innerText())
|
||||||
|
.toList(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0300.dart';
|
import 'package:moxxmpp/src/xeps/xep_0300.dart';
|
||||||
|
|
||||||
class FileMetadataData {
|
class FileMetadataData {
|
||||||
|
|
||||||
const FileMetadataData({
|
const FileMetadataData({
|
||||||
this.mediaType,
|
this.mediaType,
|
||||||
this.width,
|
this.width,
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ abstract class StatelessFileSharingSource {
|
|||||||
|
|
||||||
/// Implementation for url-data source elements.
|
/// Implementation for url-data source elements.
|
||||||
class StatelessFileSharingUrlSource extends StatelessFileSharingSource {
|
class StatelessFileSharingUrlSource extends StatelessFileSharingSource {
|
||||||
|
|
||||||
StatelessFileSharingUrlSource(this.url);
|
StatelessFileSharingUrlSource(this.url);
|
||||||
|
|
||||||
factory StatelessFileSharingUrlSource.fromXml(XMLNode element) {
|
factory StatelessFileSharingUrlSource.fromXml(XMLNode element) {
|
||||||
@@ -41,8 +40,29 @@ class StatelessFileSharingUrlSource extends StatelessFileSharingSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StatelessFileSharingData {
|
/// Finds the <sources/> element in [node] and returns the list of
|
||||||
|
/// StatelessFileSharingSources contained with it.
|
||||||
|
/// If [checkXmlns] is true, then the sources element must also have an xmlns attribute
|
||||||
|
/// of "urn:xmpp:sfs:0".
|
||||||
|
List<StatelessFileSharingSource> processStatelessFileSharingSources(XMLNode node, { bool checkXmlns = true }) {
|
||||||
|
final sources = List<StatelessFileSharingSource>.empty(growable: true);
|
||||||
|
|
||||||
|
final sourcesElement = node.firstTag(
|
||||||
|
'sources',
|
||||||
|
xmlns: checkXmlns ? sfsXmlns : null,
|
||||||
|
)!;
|
||||||
|
for (final source in sourcesElement.children) {
|
||||||
|
if (source.attributes['xmlns'] == urlDataXmlns) {
|
||||||
|
sources.add(StatelessFileSharingUrlSource.fromXml(source));
|
||||||
|
} else if (source.attributes['xmlns'] == sfsEncryptionXmlns) {
|
||||||
|
sources.add(StatelessFileSharingEncryptedSource.fromXml(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sources;
|
||||||
|
}
|
||||||
|
|
||||||
|
class StatelessFileSharingData {
|
||||||
const StatelessFileSharingData(this.metadata, this.sources);
|
const StatelessFileSharingData(this.metadata, this.sources);
|
||||||
|
|
||||||
/// Parse [node] as a StatelessFileSharingData element.
|
/// Parse [node] as a StatelessFileSharingData element.
|
||||||
@@ -50,20 +70,10 @@ class StatelessFileSharingData {
|
|||||||
assert(node.attributes['xmlns'] == sfsXmlns, 'Invalid element xmlns');
|
assert(node.attributes['xmlns'] == sfsXmlns, 'Invalid element xmlns');
|
||||||
assert(node.tag == 'file-sharing', 'Invalid element name');
|
assert(node.tag == 'file-sharing', 'Invalid element name');
|
||||||
|
|
||||||
final sources = List<StatelessFileSharingSource>.empty(growable: true);
|
|
||||||
|
|
||||||
final sourcesElement = node.firstTag('sources')!;
|
|
||||||
for (final source in sourcesElement.children) {
|
|
||||||
if (source.attributes['xmlns'] == urlDataXmlns) {
|
|
||||||
sources.add(StatelessFileSharingUrlSource.fromXml(source));
|
|
||||||
} else if (source.attributes['xmlns'] == sfsEncryptionXmlns) {
|
|
||||||
sources.add(StatelessFileSharingEncryptedSource.fromXml(source));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return StatelessFileSharingData(
|
return StatelessFileSharingData(
|
||||||
FileMetadataData.fromXML(node.firstTag('file')!),
|
FileMetadataData.fromXML(node.firstTag('file')!),
|
||||||
sources,
|
// TODO(PapaTutuWawa): This is a work around for Stickers where the source element has a XMLNS but SFS does not have one.
|
||||||
|
processStatelessFileSharingSources(node, checkXmlns: false),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +130,7 @@ class SFSManager extends XmppManagerBase {
|
|||||||
final sfs = message.firstTag('file-sharing', xmlns: sfsXmlns)!;
|
final sfs = message.firstTag('file-sharing', xmlns: sfsXmlns)!;
|
||||||
|
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
sfs: StatelessFileSharingData.fromXML(sfs),
|
sfs: StatelessFileSharingData.fromXML(sfs, ),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
310
packages/moxxmpp/lib/src/xeps/xep_0449.dart
Normal file
310
packages/moxxmpp/lib/src/xeps/xep_0449.dart
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
||||||
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0300.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0447.dart';
|
||||||
|
|
||||||
|
class Sticker {
|
||||||
|
const Sticker(this.metadata, this.sources, this.suggests);
|
||||||
|
|
||||||
|
factory Sticker.fromXML(XMLNode node) {
|
||||||
|
assert(node.tag == 'item', 'sticker has wrong tag');
|
||||||
|
|
||||||
|
return Sticker(
|
||||||
|
FileMetadataData.fromXML(node.firstTag('file', xmlns: fileMetadataXmlns)!),
|
||||||
|
processStatelessFileSharingSources(node, checkXmlns: false),
|
||||||
|
{},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final FileMetadataData metadata;
|
||||||
|
final List<StatelessFileSharingSource> sources;
|
||||||
|
// Language -> suggestion
|
||||||
|
final Map<String, String> suggests;
|
||||||
|
|
||||||
|
XMLNode toPubSubXML() {
|
||||||
|
final suggestsElements = suggests.keys.map((suggest) {
|
||||||
|
Map<String, String> attrs;
|
||||||
|
if (suggest.isEmpty) {
|
||||||
|
attrs = {};
|
||||||
|
} else {
|
||||||
|
attrs = {
|
||||||
|
'xml:lang': suggest,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return XMLNode(
|
||||||
|
tag: 'suggest',
|
||||||
|
attributes: attrs,
|
||||||
|
text: suggests[suggest],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return XMLNode(
|
||||||
|
tag: 'item',
|
||||||
|
children: [
|
||||||
|
metadata.toXML(),
|
||||||
|
...sources.map((source) => source.toXml()),
|
||||||
|
...suggestsElements,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StickerPack {
|
||||||
|
const StickerPack(
|
||||||
|
this.id,
|
||||||
|
this.name,
|
||||||
|
this.summary,
|
||||||
|
this.hashAlgorithm,
|
||||||
|
this.hashValue,
|
||||||
|
this.stickers,
|
||||||
|
this.restricted,
|
||||||
|
);
|
||||||
|
|
||||||
|
factory StickerPack.fromXML(String id, XMLNode node, { bool hashAvailable = true }) {
|
||||||
|
assert(node.tag == 'pack', 'node has wrong tag');
|
||||||
|
assert(node.attributes['xmlns'] == stickersXmlns, 'node has wrong XMLNS');
|
||||||
|
|
||||||
|
var hashAlgorithm = HashFunction.sha256;
|
||||||
|
var hashValue = '';
|
||||||
|
if (hashAvailable) {
|
||||||
|
final hash = node.firstTag('hash', xmlns: hashXmlns)!;
|
||||||
|
hashAlgorithm = hashFunctionFromName(hash.attributes['algo']! as String);
|
||||||
|
hashValue = hash.innerText();
|
||||||
|
}
|
||||||
|
|
||||||
|
return StickerPack(
|
||||||
|
id,
|
||||||
|
node.firstTag('name')!.innerText(),
|
||||||
|
node.firstTag('summary')!.innerText(),
|
||||||
|
hashAlgorithm,
|
||||||
|
hashValue,
|
||||||
|
node.children
|
||||||
|
.where((e) => e.tag == 'item')
|
||||||
|
.map<Sticker>(Sticker.fromXML)
|
||||||
|
.toList(),
|
||||||
|
node.firstTag('restricted') != null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final String id;
|
||||||
|
// TODO(PapaTutuWawa): Turn name and summary into a Map as it may contain a xml:lang
|
||||||
|
final String name;
|
||||||
|
final String summary;
|
||||||
|
final HashFunction hashAlgorithm;
|
||||||
|
final String hashValue;
|
||||||
|
final List<Sticker> stickers;
|
||||||
|
final bool restricted;
|
||||||
|
|
||||||
|
/// When using the fromXML factory to parse a description of a sticker pack with a
|
||||||
|
/// yet unknown hash, then this function can be used in order to apply the freshly
|
||||||
|
/// calculated hash to the object.
|
||||||
|
StickerPack copyWithId(HashFunction newHashFunction, String newId) {
|
||||||
|
return StickerPack(
|
||||||
|
newId,
|
||||||
|
name,
|
||||||
|
summary,
|
||||||
|
newHashFunction,
|
||||||
|
newId,
|
||||||
|
stickers,
|
||||||
|
restricted,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
XMLNode toXML() {
|
||||||
|
return XMLNode.xmlns(
|
||||||
|
tag: 'pack',
|
||||||
|
xmlns: stickersXmlns,
|
||||||
|
children: [
|
||||||
|
// Pack metadata
|
||||||
|
XMLNode(
|
||||||
|
tag: 'name',
|
||||||
|
text: name,
|
||||||
|
),
|
||||||
|
XMLNode(
|
||||||
|
tag: 'summary',
|
||||||
|
text: summary,
|
||||||
|
),
|
||||||
|
constructHashElement(
|
||||||
|
hashAlgorithm.toName(),
|
||||||
|
hashValue,
|
||||||
|
),
|
||||||
|
|
||||||
|
...restricted ?
|
||||||
|
[XMLNode(tag: 'restricted')] :
|
||||||
|
[],
|
||||||
|
|
||||||
|
// Stickers
|
||||||
|
...stickers
|
||||||
|
.map((sticker) => sticker.toPubSubXML()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculates the sticker pack's hash as specified by XEP-0449.
|
||||||
|
Future<String> getHash(HashFunction hashFunction) async {
|
||||||
|
// Build the meta string
|
||||||
|
final metaTmp = [
|
||||||
|
<int>[
|
||||||
|
...utf8.encode('name'),
|
||||||
|
0x1f,
|
||||||
|
0x1f,
|
||||||
|
...utf8.encode(name),
|
||||||
|
0x1f,
|
||||||
|
0x1e,
|
||||||
|
],
|
||||||
|
<int>[
|
||||||
|
...utf8.encode('summary'),
|
||||||
|
0x1f,
|
||||||
|
0x1f,
|
||||||
|
...utf8.encode(summary),
|
||||||
|
0x1f,
|
||||||
|
0x1e,
|
||||||
|
],
|
||||||
|
]..sort(ioctetSortComparatorRaw);
|
||||||
|
final metaString = List<int>.empty(growable: true);
|
||||||
|
for (final m in metaTmp) {
|
||||||
|
metaString.addAll(m);
|
||||||
|
}
|
||||||
|
metaString.add(0x1c);
|
||||||
|
|
||||||
|
// Build item hashes
|
||||||
|
final items = List<List<int>>.empty(growable: true);
|
||||||
|
for (final sticker in stickers) {
|
||||||
|
final tmp = List<int>.empty(growable: true)
|
||||||
|
..addAll(utf8.encode(sticker.metadata.desc!))
|
||||||
|
..add(0x1e);
|
||||||
|
|
||||||
|
final hashes = List<List<int>>.empty(growable: true);
|
||||||
|
for (final hash in sticker.metadata.hashes.keys) {
|
||||||
|
hashes.add([
|
||||||
|
...utf8.encode(hash),
|
||||||
|
0x1f,
|
||||||
|
...utf8.encode(sticker.metadata.hashes[hash]!),
|
||||||
|
0x1f,
|
||||||
|
0x1e,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
hashes.sort(ioctetSortComparatorRaw);
|
||||||
|
|
||||||
|
for (final hash in hashes) {
|
||||||
|
tmp.addAll(hash);
|
||||||
|
}
|
||||||
|
tmp.add(0x1d);
|
||||||
|
items.add(tmp);
|
||||||
|
}
|
||||||
|
items.sort(ioctetSortComparatorRaw);
|
||||||
|
final stickersString = List<int>.empty(growable: true);
|
||||||
|
for (final item in items) {
|
||||||
|
stickersString.addAll(item);
|
||||||
|
}
|
||||||
|
stickersString.add(0x1c);
|
||||||
|
|
||||||
|
// Calculate the hash
|
||||||
|
final rawHash = await CryptographicHashManager.hashFromData(
|
||||||
|
[
|
||||||
|
...metaString,
|
||||||
|
...stickersString,
|
||||||
|
],
|
||||||
|
hashFunction,
|
||||||
|
);
|
||||||
|
return base64.encode(rawHash).substring(0, 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StickersManager extends XmppManagerBase {
|
||||||
|
@override
|
||||||
|
String getId() => stickersManager;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getName() => 'StickersManager';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
StanzaHandler(
|
||||||
|
stanzaTag: 'message',
|
||||||
|
tagXmlns: stickersXmlns,
|
||||||
|
tagName: 'sticker',
|
||||||
|
callback: _onIncomingMessage,
|
||||||
|
priority: -99,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
Future<StanzaHandlerData> _onIncomingMessage(Stanza stanza, StanzaHandlerData state) async {
|
||||||
|
final sticker = stanza.firstTag('sticker', xmlns: stickersXmlns)!;
|
||||||
|
return state.copyWith(
|
||||||
|
stickerPackId: sticker.attributes['pack']! as String,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Publishes the StickerPack [pack] to the PubSub node of [jid]. If specified, then
|
||||||
|
/// [accessModel] will be used as the PubSub node's access model.
|
||||||
|
///
|
||||||
|
/// On success, returns true. On failure, returns a PubSubError.
|
||||||
|
Future<Result<PubSubError, bool>> publishStickerPack(JID jid, StickerPack pack, { String? accessModel }) async {
|
||||||
|
assert(pack.id != '', 'The sticker pack must have an id');
|
||||||
|
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
|
||||||
|
|
||||||
|
return pm.publish(
|
||||||
|
jid.toBare().toString(),
|
||||||
|
stickersXmlns,
|
||||||
|
pack.toXML(),
|
||||||
|
id: pack.id,
|
||||||
|
options: PubSubPublishOptions(
|
||||||
|
maxItems: 'max',
|
||||||
|
accessModel: accessModel,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes the sticker pack with id [id] from the PubSub node of [jid].
|
||||||
|
///
|
||||||
|
/// On success, returns the true. On failure, returns a PubSubError.
|
||||||
|
Future<Result<PubSubError, bool>> retractStickerPack(JID jid, String id) async {
|
||||||
|
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
|
||||||
|
|
||||||
|
return pm.retract(
|
||||||
|
jid,
|
||||||
|
stickersXmlns,
|
||||||
|
id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetches the sticker pack with id [id] from [jid].
|
||||||
|
///
|
||||||
|
/// On success, returns the StickerPack. On failure, returns a PubSubError.
|
||||||
|
Future<Result<PubSubError, StickerPack>> fetchStickerPack(JID jid, String id) async {
|
||||||
|
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
|
||||||
|
final stickerPackDataRaw = await pm.getItem(
|
||||||
|
jid.toBare().toString(),
|
||||||
|
stickersXmlns,
|
||||||
|
id,
|
||||||
|
);
|
||||||
|
if (stickerPackDataRaw.isType<PubSubError>()) {
|
||||||
|
return Result(stickerPackDataRaw.get<PubSubError>());
|
||||||
|
}
|
||||||
|
|
||||||
|
final stickerPackData = stickerPackDataRaw.get<PubSubItem>();
|
||||||
|
final stickerPack = StickerPack.fromXML(
|
||||||
|
stickerPackData.id,
|
||||||
|
stickerPackData.payload,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Result(stickerPack);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
import 'package:moxxmpp/src/managers/data.dart';
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
import 'package:moxxmpp/src/managers/handlers.dart';
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
@@ -5,20 +6,65 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/namespaces.dart';
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
|
||||||
|
/// Data summarizing the XEP-0461 data.
|
||||||
class ReplyData {
|
class ReplyData {
|
||||||
|
|
||||||
const ReplyData({
|
const ReplyData({
|
||||||
required this.to,
|
required this.to,
|
||||||
required this.id,
|
required this.id,
|
||||||
this.start,
|
this.start,
|
||||||
this.end,
|
this.end,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/// The bare JID to whom the reply applies to
|
||||||
final String to;
|
final String to;
|
||||||
|
|
||||||
|
/// The stanza ID of the message that is replied to
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
|
/// The start of the fallback body (inclusive)
|
||||||
final int? start;
|
final int? start;
|
||||||
|
|
||||||
|
/// The end of the fallback body (exclusive)
|
||||||
final int? end;
|
final int? end;
|
||||||
|
|
||||||
|
/// Applies the metadata to the received body [body] in order to remove the fallback.
|
||||||
|
/// If either [ReplyData.start] or [ReplyData.end] are null, then body is returned as
|
||||||
|
/// is.
|
||||||
|
String removeFallback(String body) {
|
||||||
|
if (start == null || end == null) return body;
|
||||||
|
|
||||||
|
return body.replaceRange(start!, end, '');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Internal class describing how to build a message with a quote fallback body.
|
||||||
|
@visibleForTesting
|
||||||
|
class QuoteData {
|
||||||
|
const QuoteData(this.body, this.fallbackLength);
|
||||||
|
|
||||||
|
/// Takes the body of the message we want to quote [quoteBody] and the content of
|
||||||
|
/// the reply [body] and computes the fallback body and its length.
|
||||||
|
factory QuoteData.fromBodies(String quoteBody, String body) {
|
||||||
|
final fallback = quoteBody
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => '> $line\n')
|
||||||
|
.join();
|
||||||
|
|
||||||
|
return QuoteData(
|
||||||
|
'$fallback$body',
|
||||||
|
fallback.length,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The new body with fallback data at the beginning
|
||||||
|
final String body;
|
||||||
|
|
||||||
|
/// The length of the fallback data
|
||||||
|
final int fallbackLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A manager implementing support for parsing XEP-0461 metadata. The
|
||||||
|
/// MessageRepliesManager itself does not modify the body of the message.
|
||||||
class MessageRepliesManager extends XmppManagerBase {
|
class MessageRepliesManager extends XmppManagerBase {
|
||||||
@override
|
@override
|
||||||
String getName() => 'MessageRepliesManager';
|
String getName() => 'MessageRepliesManager';
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: moxxmpp
|
name: moxxmpp
|
||||||
description: A pure-Dart XMPP library
|
description: A pure-Dart XMPP library
|
||||||
version: 0.1.2+2
|
version: 0.1.6+1
|
||||||
homepage: https://codeberg.org/moxxy/moxxmpp
|
homepage: https://codeberg.org/moxxy/moxxmpp
|
||||||
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ environment:
|
|||||||
sdk: '>=2.17.5 <3.0.0'
|
sdk: '>=2.17.5 <3.0.0'
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
|
collection: ^1.16.0
|
||||||
cryptography: ^2.0.5
|
cryptography: ^2.0.5
|
||||||
freezed: ^2.1.0+1
|
freezed: ^2.1.0+1
|
||||||
freezed_annotation: ^2.1.0
|
freezed_annotation: ^2.1.0
|
||||||
@@ -19,8 +20,8 @@ dependencies:
|
|||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: ^0.1.5
|
version: ^0.1.5
|
||||||
omemo_dart:
|
omemo_dart:
|
||||||
hosted: https://git.polynom.me/api/packages/PapaTutuWawa/pub
|
hosted: https://git.polynom.me/api/packages/PapaTutuWawa/pub
|
||||||
version: ^0.3.2
|
version: ^0.4.2
|
||||||
random_string: ^2.3.1
|
random_string: ^2.3.1
|
||||||
saslprep: ^1.0.2
|
saslprep: ^1.0.2
|
||||||
synchronized: ^3.0.0+2
|
synchronized: ^3.0.0+2
|
||||||
@@ -31,6 +32,6 @@ dev_dependencies:
|
|||||||
build_runner: ^2.1.11
|
build_runner: ^2.1.11
|
||||||
moxxmpp_socket_tcp:
|
moxxmpp_socket_tcp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: ^0.1.2+2
|
version: ^0.1.2+9
|
||||||
test: ^1.16.0
|
test: ^1.16.0
|
||||||
very_good_analysis: ^3.0.1
|
very_good_analysis: ^3.0.1
|
||||||
|
|||||||
@@ -1,41 +1,55 @@
|
|||||||
import 'package:moxxmpp/moxxmpp.dart';
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('Parse a full JID', () {
|
test('Parse a full JID', () {
|
||||||
final jid = JID.fromString('test@server/abc');
|
final jid = JID.fromString('test@server/abc');
|
||||||
|
|
||||||
expect(jid.local, 'test');
|
expect(jid.local, 'test');
|
||||||
expect(jid.domain, 'server');
|
expect(jid.domain, 'server');
|
||||||
expect(jid.resource, 'abc');
|
expect(jid.resource, 'abc');
|
||||||
expect(jid.toString(), 'test@server/abc');
|
expect(jid.toString(), 'test@server/abc');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Parse a bare JID', () {
|
test('Parse a bare JID', () {
|
||||||
final jid = JID.fromString('test@server');
|
final jid = JID.fromString('test@server');
|
||||||
|
|
||||||
expect(jid.local, 'test');
|
expect(jid.local, 'test');
|
||||||
expect(jid.domain, 'server');
|
expect(jid.domain, 'server');
|
||||||
expect(jid.resource, '');
|
expect(jid.resource, '');
|
||||||
expect(jid.toString(), 'test@server');
|
expect(jid.toString(), 'test@server');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Parse a JID with no local part', () {
|
test('Parse a JID with no local part', () {
|
||||||
final jid = JID.fromString('server/abc');
|
final jid = JID.fromString('server/abc');
|
||||||
|
|
||||||
expect(jid.local, '');
|
expect(jid.local, '');
|
||||||
expect(jid.domain, 'server');
|
expect(jid.domain, 'server');
|
||||||
expect(jid.resource, 'abc');
|
expect(jid.resource, 'abc');
|
||||||
expect(jid.toString(), 'server/abc');
|
expect(jid.toString(), 'server/abc');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Equality', () {
|
test('Equality', () {
|
||||||
expect(JID.fromString('hallo@welt/abc') == JID('hallo', 'welt', 'abc'), true);
|
expect(JID.fromString('hallo@welt/abc') == JID('hallo', 'welt', 'abc'), true);
|
||||||
expect(JID.fromString('hallo@welt') == JID('hallo', 'welt', 'a'), false);
|
expect(JID.fromString('hallo@welt') == JID('hallo', 'welt', 'a'), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Whitespaces', () {
|
test('Dot suffix at domain part', () {
|
||||||
expect(JID.fromString('hallo@welt ') == JID('hallo', 'welt', ''), true);
|
expect(JID.fromString('hallo@welt.example.') == JID('hallo', 'welt.example', ''), true);
|
||||||
expect(JID.fromString('hallo@welt/abc ') == JID('hallo', 'welt', 'abc'), true);
|
expect(JID.fromString('hallo@welt.example./test') == JID('hallo', 'welt.example', 'test'), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Parse resource with a slash', () {
|
||||||
|
expect(JID.fromString('hallo@welt.example./test/welt') == JID('hallo', 'welt.example', 'test/welt'), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('bareCompare', () {
|
||||||
|
final jid1 = JID('hallo', 'welt', 'lol');
|
||||||
|
final jid2 = JID('hallo', 'welt', '');
|
||||||
|
final jid3 = JID('hallo', 'earth', 'true');
|
||||||
|
|
||||||
|
expect(jid1.bareCompare(jid2), true);
|
||||||
|
expect(jid2.bareCompare(jid1), true);
|
||||||
|
expect(jid2.bareCompare(jid1, ensureBare: true), false);
|
||||||
|
expect(jid2.bareCompare(jid3), false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import 'package:test/test.dart';
|
|||||||
import 'helpers/logging.dart';
|
import 'helpers/logging.dart';
|
||||||
import 'helpers/xmpp.dart';
|
import 'helpers/xmpp.dart';
|
||||||
|
|
||||||
const exampleXmlns1 = 'im:moxxy:example1';
|
const exampleXmlns1 = 'im:moxxmpp:example1';
|
||||||
const exampleNamespace1 = 'im.moxxy.test.example1';
|
const exampleNamespace1 = 'im.moxxmpp.test.example1';
|
||||||
const exampleXmlns2 = 'im:moxxy:example2';
|
const exampleXmlns2 = 'im:moxxmpp:example2';
|
||||||
const exampleNamespace2 = 'im.moxxy.test.example2';
|
const exampleNamespace2 = 'im.moxxmpp.test.example2';
|
||||||
|
|
||||||
class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
||||||
StubNegotiator1() : called = false, super(1, false, exampleXmlns1, exampleNamespace1);
|
StubNegotiator1() : called = false, super(1, false, exampleXmlns1, exampleNamespace1);
|
||||||
@@ -14,9 +14,9 @@ class StubNegotiator1 extends XmppFeatureNegotiatorBase {
|
|||||||
bool called;
|
bool called;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
called = true;
|
called = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,9 +26,9 @@ class StubNegotiator2 extends XmppFeatureNegotiatorBase {
|
|||||||
bool called;
|
bool called;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> negotiate(XMLNode nonza) async {
|
Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
|
||||||
called = true;
|
called = true;
|
||||||
state = NegotiatorState.done;
|
return const Result(NegotiatorState.done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,8 +47,8 @@ void main() {
|
|||||||
from="test.server"
|
from="test.server"
|
||||||
xml:lang="en">
|
xml:lang="en">
|
||||||
<stream:features xmlns="http://etherx.jabber.org/streams">
|
<stream:features xmlns="http://etherx.jabber.org/streams">
|
||||||
<example1 xmlns="im:moxxy:example1" />
|
<example1 xmlns="im:moxxmpp:example1" />
|
||||||
<example2 xmlns="im:moxxy:example2" />
|
<example2 xmlns="im:moxxmpp:example2" />
|
||||||
</stream:features>''',
|
</stream:features>''',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -61,7 +61,7 @@ void main() {
|
|||||||
])
|
])
|
||||||
..registerManagers([
|
..registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
])
|
])
|
||||||
|
|||||||
153
packages/moxxmpp/test/roster_state_test.dart
Normal file
153
packages/moxxmpp/test/roster_state_test.dart
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test receiving a roster push', () async {
|
||||||
|
final rs = TestingRosterStateManager(null, []);
|
||||||
|
rs.register((_) {});
|
||||||
|
|
||||||
|
await rs.handleRosterPush(
|
||||||
|
RosterPushResult(
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser@server.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != -1,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(rs.loadCount, 1);
|
||||||
|
expect(rs.getRosterItems().length, 1);
|
||||||
|
|
||||||
|
// Receive another roster push
|
||||||
|
await rs.handleRosterPush(
|
||||||
|
RosterPushResult(
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser2@server2.example',
|
||||||
|
subscription: 'to',
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') != -1,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(rs.loadCount, 1);
|
||||||
|
expect(rs.getRosterItems().length, 2);
|
||||||
|
|
||||||
|
// Remove one of the items
|
||||||
|
await rs.handleRosterPush(
|
||||||
|
RosterPushResult(
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser2@server2.example',
|
||||||
|
subscription: 'remove',
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') == -1,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != 1,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(rs.loadCount, 1);
|
||||||
|
expect(rs.getRosterItems().length, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a roster fetch', () async {
|
||||||
|
final rs = TestingRosterStateManager(null, []);
|
||||||
|
rs.register((_) {});
|
||||||
|
|
||||||
|
// Fetch the roster
|
||||||
|
await rs.handleRosterFetch(
|
||||||
|
RosterRequestResult(
|
||||||
|
[
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser@server.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser2@server2.example',
|
||||||
|
subscription: 'to',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser3@server3.example',
|
||||||
|
subscription: 'from',
|
||||||
|
),
|
||||||
|
],
|
||||||
|
'aaaaaaaa',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(rs.loadCount, 1);
|
||||||
|
expect(rs.getRosterItems().length, 3);
|
||||||
|
expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser@server.example') != -1, true);
|
||||||
|
expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser2@server2.example') != -1, true);
|
||||||
|
expect(rs.getRosterItems().indexWhere((item) => item.jid == 'testuser3@server3.example') != -1, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a roster fetch if we already have a roster', () async {
|
||||||
|
XmppEvent? event;
|
||||||
|
final rs = TestingRosterStateManager('aaaaa', [
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser@server.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser2@server2.example',
|
||||||
|
subscription: 'to',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser3@server3.example',
|
||||||
|
subscription: 'from',
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
rs.register((_event) {
|
||||||
|
event = _event;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch the roster
|
||||||
|
await rs.handleRosterFetch(
|
||||||
|
RosterRequestResult(
|
||||||
|
[
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser@server.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser2@server2.example',
|
||||||
|
subscription: 'to',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser3@server3.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
XmppRosterItem(
|
||||||
|
jid: 'testuser4@server4.example',
|
||||||
|
subscription: 'both',
|
||||||
|
),
|
||||||
|
],
|
||||||
|
'bbbbb',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(event is RosterUpdatedEvent, true);
|
||||||
|
final updateEvent = event as RosterUpdatedEvent;
|
||||||
|
|
||||||
|
expect(updateEvent.added.length, 1);
|
||||||
|
expect(updateEvent.added.first.jid, 'testuser4@server4.example');
|
||||||
|
expect(updateEvent.modified.length, 1);
|
||||||
|
expect(updateEvent.modified.first.jid, 'testuser3@server3.example');
|
||||||
|
expect(updateEvent.removed.isEmpty, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,322 +0,0 @@
|
|||||||
import 'package:moxxmpp/moxxmpp.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
// TODO(PapaTutuWawa): Fix tests
|
|
||||||
|
|
||||||
typedef AddRosterItemFunction = Future<RosterItem> Function(
|
|
||||||
String avatarUrl,
|
|
||||||
String avatarHash,
|
|
||||||
String jid,
|
|
||||||
String title,
|
|
||||||
String subscription,
|
|
||||||
String ask,
|
|
||||||
{
|
|
||||||
List<String> groups,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
typedef UpdateRosterItemFunction = Future<RosterItem> Function(
|
|
||||||
int id, {
|
|
||||||
String? avatarUrl,
|
|
||||||
String? avatarHash,
|
|
||||||
String? title,
|
|
||||||
String? subscription,
|
|
||||||
String? ask,
|
|
||||||
List<String>? groups,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddRosterItemFunction mkAddRosterItem(void Function(String) callback) {
|
|
||||||
return (
|
|
||||||
String avatarUrl,
|
|
||||||
String avatarHash,
|
|
||||||
String jid,
|
|
||||||
String title,
|
|
||||||
String subscription,
|
|
||||||
String ask,
|
|
||||||
{
|
|
||||||
List<String> groups = const [],
|
|
||||||
}
|
|
||||||
) async {
|
|
||||||
callback(jid);
|
|
||||||
return await addRosterItemFromData(
|
|
||||||
avatarUrl,
|
|
||||||
avatarHash,
|
|
||||||
jid,
|
|
||||||
title,
|
|
||||||
subscription,
|
|
||||||
ask,
|
|
||||||
groups: groups,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<RosterItem> addRosterItemFromData(
|
|
||||||
String avatarUrl,
|
|
||||||
String avatarHash,
|
|
||||||
String jid,
|
|
||||||
String title,
|
|
||||||
String subscription,
|
|
||||||
String ask,
|
|
||||||
{
|
|
||||||
List<String> groups = const [],
|
|
||||||
}
|
|
||||||
) async => RosterItem(
|
|
||||||
0,
|
|
||||||
avatarUrl,
|
|
||||||
avatarHash,
|
|
||||||
jid,
|
|
||||||
title,
|
|
||||||
subscription,
|
|
||||||
ask,
|
|
||||||
groups,
|
|
||||||
);
|
|
||||||
|
|
||||||
UpdateRosterItemFunction mkRosterUpdate(List<RosterItem> roster) {
|
|
||||||
return (
|
|
||||||
int id, {
|
|
||||||
String? avatarUrl,
|
|
||||||
String? avatarHash,
|
|
||||||
String? title,
|
|
||||||
String? subscription,
|
|
||||||
String? ask,
|
|
||||||
List<String>? groups,
|
|
||||||
}
|
|
||||||
) async {
|
|
||||||
final item = firstWhereOrNull(roster, (RosterItem item) => item.id == id)!;
|
|
||||||
return item.copyWith(
|
|
||||||
avatarUrl: avatarUrl ?? item.avatarUrl,
|
|
||||||
avatarHash: avatarHash ?? item.avatarHash,
|
|
||||||
title: title ?? item.title,
|
|
||||||
subscription: subscription ?? item.subscription,
|
|
||||||
ask: ask ?? item.ask,
|
|
||||||
groups: groups ?? item.groups,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
final localRosterSingle = [
|
|
||||||
RosterItem(
|
|
||||||
0,
|
|
||||||
'',
|
|
||||||
'',
|
|
||||||
'hallo@server.example',
|
|
||||||
'hallo',
|
|
||||||
'none',
|
|
||||||
'',
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
];
|
|
||||||
final localRosterDouble = [
|
|
||||||
RosterItem(
|
|
||||||
0,
|
|
||||||
'',
|
|
||||||
'',
|
|
||||||
'hallo@server.example',
|
|
||||||
'hallo',
|
|
||||||
'none',
|
|
||||||
'',
|
|
||||||
[],
|
|
||||||
),
|
|
||||||
RosterItem(
|
|
||||||
1,
|
|
||||||
'',
|
|
||||||
'',
|
|
||||||
'welt@different.server.example',
|
|
||||||
'welt',
|
|
||||||
'from',
|
|
||||||
'',
|
|
||||||
[ 'Friends' ],
|
|
||||||
)
|
|
||||||
];
|
|
||||||
|
|
||||||
group('Test roster pushes', () {
|
|
||||||
test('Test removing an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterDouble,
|
|
||||||
[
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'hallo@server.example', subscription: 'remove',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
true,
|
|
||||||
mkAddRosterItem((_) { addCalled = true; }),
|
|
||||||
mkRosterUpdate(localRosterDouble),
|
|
||||||
(jid) async {
|
|
||||||
if (jid == 'hallo@server.example') {
|
|
||||||
removeCalled = true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ 'hallo@server.example' ]);
|
|
||||||
expect(result.modified.length, 0);
|
|
||||||
expect(result.added.length, 0);
|
|
||||||
expect(removeCalled, true);
|
|
||||||
expect(addCalled, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Test adding an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterSingle,
|
|
||||||
[
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'welt@different.server.example',
|
|
||||||
subscription: 'from',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
true,
|
|
||||||
mkAddRosterItem(
|
|
||||||
(jid) {
|
|
||||||
if (jid == 'welt@different.server.example') {
|
|
||||||
addCalled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
),
|
|
||||||
mkRosterUpdate(localRosterSingle),
|
|
||||||
(_) async { removeCalled = true; },
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ ]);
|
|
||||||
expect(result.modified.length, 0);
|
|
||||||
expect(result.added.length, 1);
|
|
||||||
expect(result.added.first.subscription, 'from');
|
|
||||||
expect(removeCalled, false);
|
|
||||||
expect(addCalled, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Test modifying an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterDouble,
|
|
||||||
[
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'welt@different.server.example',
|
|
||||||
subscription: 'both',
|
|
||||||
name: 'The World',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
true,
|
|
||||||
mkAddRosterItem((_) { addCalled = false; }),
|
|
||||||
mkRosterUpdate(localRosterDouble),
|
|
||||||
(_) async { removeCalled = true; },
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ ]);
|
|
||||||
expect(result.modified.length, 1);
|
|
||||||
expect(result.added.length, 0);
|
|
||||||
expect(result.modified.first.subscription, 'both');
|
|
||||||
expect(result.modified.first.jid, 'welt@different.server.example');
|
|
||||||
expect(result.modified.first.title, 'The World');
|
|
||||||
expect(removeCalled, false);
|
|
||||||
expect(addCalled, false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('Test roster requests', () {
|
|
||||||
test('Test removing an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterSingle,
|
|
||||||
[],
|
|
||||||
false,
|
|
||||||
mkAddRosterItem((_) { addCalled = true; }),
|
|
||||||
mkRosterUpdate(localRosterDouble),
|
|
||||||
(jid) async {
|
|
||||||
if (jid == 'hallo@server.example') {
|
|
||||||
removeCalled = true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ 'hallo@server.example' ]);
|
|
||||||
expect(result.modified.length, 0);
|
|
||||||
expect(result.added.length, 0);
|
|
||||||
expect(removeCalled, true);
|
|
||||||
expect(addCalled, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Test adding an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterSingle,
|
|
||||||
[
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'hallo@server.example',
|
|
||||||
name: 'hallo',
|
|
||||||
subscription: 'none',
|
|
||||||
),
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'welt@different.server.example',
|
|
||||||
subscription: 'both',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
mkAddRosterItem(
|
|
||||||
(jid) {
|
|
||||||
if (jid == 'welt@different.server.example') {
|
|
||||||
addCalled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
),
|
|
||||||
mkRosterUpdate(localRosterSingle),
|
|
||||||
(_) async { removeCalled = true; },
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ ]);
|
|
||||||
expect(result.modified.length, 0);
|
|
||||||
expect(result.added.length, 1);
|
|
||||||
expect(result.added.first.subscription, 'both');
|
|
||||||
expect(removeCalled, false);
|
|
||||||
expect(addCalled, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Test modifying an item', () async {
|
|
||||||
var removeCalled = false;
|
|
||||||
var addCalled = false;
|
|
||||||
final result = await processRosterDiff(
|
|
||||||
localRosterSingle,
|
|
||||||
[
|
|
||||||
XmppRosterItem(
|
|
||||||
jid: 'hallo@server.example',
|
|
||||||
subscription: 'both',
|
|
||||||
name: 'Hallo Welt',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
mkAddRosterItem((_) { addCalled = false; }),
|
|
||||||
mkRosterUpdate(localRosterDouble),
|
|
||||||
(_) async { removeCalled = true; },
|
|
||||||
(_) async => null,
|
|
||||||
(_, { String? id }) async {},
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.removed, [ ]);
|
|
||||||
expect(result.modified.length, 1);
|
|
||||||
expect(result.added.length, 0);
|
|
||||||
expect(result.modified.first.subscription, 'both');
|
|
||||||
expect(result.modified.first.jid, 'hallo@server.example');
|
|
||||||
expect(result.modified.first.title, 'Hallo Welt');
|
|
||||||
expect(removeCalled, false);
|
|
||||||
expect(addCalled, false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
27
packages/moxxmpp/test/sasl/kv_test.dart
Normal file
27
packages/moxxmpp/test/sasl/kv_test.dart
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import 'package:moxxmpp/src/negotiators/sasl/kv.dart';
|
||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test the Key-Value parser', () {
|
||||||
|
final result1 = parseKeyValue('n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL');
|
||||||
|
expect(result1.length, 2);
|
||||||
|
expect(result1['n']!, 'user');
|
||||||
|
expect(result1['r']!, 'fyko+d2lbbFgONRv9qkxdawL');
|
||||||
|
|
||||||
|
final result2 = parseKeyValue('r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096');
|
||||||
|
expect(result2.length, 3);
|
||||||
|
expect(result2['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j');
|
||||||
|
expect(result2['s']!, 'QSXCR+Q6sek8bf92');
|
||||||
|
expect(result2['i']!, '4096');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Test the Key-Value parser with '=' as a value", () {
|
||||||
|
final result = parseKeyValue('c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=,o=123');
|
||||||
|
expect(result.length, 4);
|
||||||
|
expect(result['c']!, 'biws');
|
||||||
|
expect(result['r']!, 'fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j');
|
||||||
|
expect(result['p']!, 'v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=');
|
||||||
|
expect(result['o']!, '123');
|
||||||
|
});
|
||||||
|
}
|
||||||
211
packages/moxxmpp/test/sasl/scram_test.dart
Normal file
211
packages/moxxmpp/test/sasl/scram_test.dart
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:hex/hex.dart';
|
||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import '../helpers/xmpp.dart';
|
||||||
|
|
||||||
|
final scramSha1StreamFeatures = XMLNode(
|
||||||
|
tag: 'stream:features',
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'mechanisms',
|
||||||
|
xmlns: saslXmlns,
|
||||||
|
children: [
|
||||||
|
XMLNode(
|
||||||
|
tag: 'mechanism',
|
||||||
|
text: 'SCRAM-SHA-1',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
final scramSha256StreamFeatures = XMLNode(
|
||||||
|
tag: 'stream:features',
|
||||||
|
children: [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'mechanisms',
|
||||||
|
xmlns: saslXmlns,
|
||||||
|
children: [
|
||||||
|
XMLNode(
|
||||||
|
tag: 'mechanism',
|
||||||
|
text: 'SCRAM-SHA-256',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final fakeSocket = StubTCPSocket(play: []);
|
||||||
|
test('Test SASL SCRAM-SHA-1', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
HEX.encode(await negotiator.calculateSaltedPassword('QSXCR+Q6sek8bf92', 4096)),
|
||||||
|
'1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateClientKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')),
|
||||||
|
),
|
||||||
|
'e234c47bf6c36696dd6d852b99aaa2ba26555728',
|
||||||
|
);
|
||||||
|
const authMessage = 'n=user,r=fyko+d2lbbFgONRv9qkxdawL,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096,c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j';
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateClientSignature(authMessage, HEX.decode('e9d94660c39d65c38fbad91c358f14da0eef2bd6')),
|
||||||
|
),
|
||||||
|
'5d7138c486b0bfabdf49e3e2da8bd6e5c79db613',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
negotiator.calculateClientProof(HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'), HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613')),
|
||||||
|
),
|
||||||
|
'bf45fcbf7073d93d022466c94321745fe1c8e13b',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateServerSignature(authMessage, HEX.decode('0fe09258b3ac852ba502cc62ba903eaacdbf7d31')),
|
||||||
|
),
|
||||||
|
'ae617da6a57c4bbb2e0286568dae1d251905b0a4',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
await negotiator.calculateServerKey(HEX.decode('1d96ee3a529b5a5f9e47c01f229a2cb8a6e15f7d')),
|
||||||
|
),
|
||||||
|
'0fe09258b3ac852ba502cc62ba903eaacdbf7d31',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
HEX.encode(
|
||||||
|
negotiator.calculateClientProof(
|
||||||
|
HEX.decode('e234c47bf6c36696dd6d852b99aaa2ba26555728'),
|
||||||
|
HEX.decode('5d7138c486b0bfabdf49e3e2da8bd6e5c79db613'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'bf45fcbf7073d93d022466c94321745fe1c8e13b',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(await negotiator.calculateChallengeResponse('cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng=='), 'c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test SASL SCRAM-SHA-256', () async {
|
||||||
|
String? lastMessage;
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=rOprNGfwEbeRWgbNEkqO', 'rOprNGfwEbeRWgbNEkqO', ScramHashType.sha256);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode n, {String? redact}) => lastMessage = n.innerText(),
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha256StreamFeatures);
|
||||||
|
expect(
|
||||||
|
utf8.decode(base64Decode(lastMessage!)),
|
||||||
|
'n,,n=user,r=rOprNGfwEbeRWgbNEkqO',
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1yT3ByTkdmd0ViZVJXZ2JORWtxTyVodllEcFdVYTJSYVRDQWZ1eEZJbGopaE5sRiRrMCxzPVcyMlphSjBTTlk3c29Fc1VFamI2Z1E9PSxpPTQwOTY=</challenge>"));
|
||||||
|
expect(
|
||||||
|
utf8.decode(base64Decode(lastMessage!)),
|
||||||
|
'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF\$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=',
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj02cnJpVFJCaTIzV3BSUi93dHVwK21NaFVaVW4vZEI1bkxUSlJzamw5NUc0PQ==</success>"));
|
||||||
|
|
||||||
|
expect(result.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a positive server signature check', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
|
||||||
|
expect(result.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a negative server signature check', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
var result;
|
||||||
|
result = await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
expect(result.isType<NegotiatorState>(), true);
|
||||||
|
|
||||||
|
result = await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
expect(result.isType<NegotiatorState>(), true);
|
||||||
|
|
||||||
|
result = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1zbUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result.isType<NegotiatorError>(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test a resetting the SCRAM negotiator', () async {
|
||||||
|
final negotiator = SaslScramNegotiator(0, 'n=user,r=fyko+d2lbbFgONRv9qkxdawL', 'fyko+d2lbbFgONRv9qkxdawL', ScramHashType.sha1);
|
||||||
|
negotiator.register(
|
||||||
|
NegotiatorAttributes(
|
||||||
|
(XMLNode _, {String? redact}) {},
|
||||||
|
() => ConnectionSettings(jid: JID.fromString('user@server'), password: 'pencil', useDirectTLS: true, allowPlainAuth: true),
|
||||||
|
(_) async {},
|
||||||
|
getNegotiatorNullStub,
|
||||||
|
getManagerNullStub,
|
||||||
|
() => JID.fromString('user@server'),
|
||||||
|
() => fakeSocket,
|
||||||
|
() => false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result1 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result1.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
|
||||||
|
// Reset and try again
|
||||||
|
negotiator.reset();
|
||||||
|
await negotiator.negotiate(scramSha1StreamFeatures);
|
||||||
|
await negotiator.negotiate(XMLNode.fromString("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0wzcmZjTkhZSlkxWlZ2V1ZzN2oscz1RU1hDUitRNnNlazhiZjkyLGk9NDA5Ng==</challenge>"));
|
||||||
|
final result2 = await negotiator.negotiate(XMLNode.fromString("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dj1ybUY5cHFWOFM3c3VBb1pXamE0ZEpSa0ZzS1E9</success>"));
|
||||||
|
expect(result2.get<NegotiatorState>(), NegotiatorState.done);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import '../helpers/logging.dart';
|
|||||||
import '../helpers/xmpp.dart';
|
import '../helpers/xmpp.dart';
|
||||||
|
|
||||||
Future<void> runIncomingStanzaHandlers(StreamManagementManager man, Stanza stanza) async {
|
Future<void> runIncomingStanzaHandlers(StreamManagementManager man, Stanza stanza) async {
|
||||||
for (final handler in man.getIncomingStanzaHandlers()) {
|
for (final handler in man.getIncomingPreStanzaHandlers()) {
|
||||||
if (handler.matches(stanza)) await handler.callback(stanza, StanzaHandlerData(false, false, null, stanza));
|
if (handler.matches(stanza)) await handler.callback(stanza, StanzaHandlerData(false, false, null, stanza));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,7 @@ void main() {
|
|||||||
final sm = StreamManagementManager();
|
final sm = StreamManagementManager();
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
@@ -348,7 +348,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
StanzaExpectation(
|
StanzaExpectation(
|
||||||
"<iq to='user@example.com' type='get' id='a' xmlns='jabber:client' />",
|
"<iq to='user@example.com' type='get' id='a' xmlns='jabber:client' />",
|
||||||
"<iq to='user@example.com' type='result' id='a' />",
|
"<iq from='user@example.com' type='result' id='a' />",
|
||||||
ignoreId: true,
|
ignoreId: true,
|
||||||
adjustId: true,
|
adjustId: true,
|
||||||
),
|
),
|
||||||
@@ -365,7 +365,7 @@ void main() {
|
|||||||
final sm = StreamManagementManager();
|
final sm = StreamManagementManager();
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
@@ -519,7 +519,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
@@ -611,7 +611,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
@@ -703,7 +703,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
|
|||||||
@@ -4,33 +4,33 @@ import '../helpers/xmpp.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test("Test if we're vulnerable against CVE-2020-26547 style vulnerabilities", () async {
|
test("Test if we're vulnerable against CVE-2020-26547 style vulnerabilities", () async {
|
||||||
final attributes = XmppManagerAttributes(
|
final attributes = XmppManagerAttributes(
|
||||||
sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async {
|
sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async {
|
||||||
// ignore: avoid_print
|
// ignore: avoid_print
|
||||||
print('==> ${stanza.toXml()}');
|
print('==> ${stanza.toXml()}');
|
||||||
return XMLNode(tag: 'iq', attributes: { 'type': 'result' });
|
return XMLNode(tag: 'iq', attributes: { 'type': 'result' });
|
||||||
},
|
},
|
||||||
sendNonza: (nonza) {},
|
sendNonza: (nonza) {},
|
||||||
sendEvent: (event) {},
|
sendEvent: (event) {},
|
||||||
getManagerById: getManagerNullStub,
|
getManagerById: getManagerNullStub,
|
||||||
getConnectionSettings: () => ConnectionSettings(
|
getConnectionSettings: () => ConnectionSettings(
|
||||||
jid: JID.fromString('bob@xmpp.example'),
|
jid: JID.fromString('bob@xmpp.example'),
|
||||||
password: 'password',
|
password: 'password',
|
||||||
useDirectTLS: true,
|
useDirectTLS: true,
|
||||||
allowPlainAuth: false,
|
allowPlainAuth: false,
|
||||||
),
|
),
|
||||||
isFeatureSupported: (_) => false,
|
isFeatureSupported: (_) => false,
|
||||||
getFullJID: () => JID.fromString('bob@xmpp.example/uwu'),
|
getFullJID: () => JID.fromString('bob@xmpp.example/uwu'),
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
||||||
getNegotiatorById: getNegotiatorNullStub,
|
getNegotiatorById: getNegotiatorNullStub,
|
||||||
);
|
);
|
||||||
final manager = CarbonsManager();
|
final manager = CarbonsManager();
|
||||||
manager.register(attributes);
|
manager.register(attributes);
|
||||||
await manager.enableCarbons();
|
await manager.enableCarbons();
|
||||||
|
|
||||||
expect(manager.isCarbonValid(JID.fromString('mallory@evil.example')), false);
|
expect(manager.isCarbonValid(JID.fromString('mallory@evil.example')), false);
|
||||||
expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example')), true);
|
expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example')), true);
|
||||||
expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example/abc')), false);
|
expect(manager.isCarbonValid(JID.fromString('bob@xmpp.example/abc')), false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
44
packages/moxxmpp/test/xeps/xep_0461_test.dart
Normal file
44
packages/moxxmpp/test/xeps/xep_0461_test.dart
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test building a singleline quote', () {
|
||||||
|
final quote = QuoteData.fromBodies('Hallo Welt', 'Hello Earth!');
|
||||||
|
|
||||||
|
expect(quote.body, '> Hallo Welt\nHello Earth!');
|
||||||
|
expect(quote.fallbackLength, 13);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test building a multiline quote', () {
|
||||||
|
final quote = QuoteData.fromBodies('Hallo Welt\nHallo Erde', 'How are you?');
|
||||||
|
|
||||||
|
expect(quote.body, '> Hallo Welt\n> Hallo Erde\nHow are you?');
|
||||||
|
expect(quote.fallbackLength, 26);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Applying a singleline quote', () {
|
||||||
|
final body = '> Hallo Welt\nHello right back!';
|
||||||
|
final reply = ReplyData(
|
||||||
|
to: '',
|
||||||
|
id: '',
|
||||||
|
start: 0,
|
||||||
|
end: 13,
|
||||||
|
);
|
||||||
|
|
||||||
|
final bodyWithoutFallback = reply.removeFallback(body);
|
||||||
|
expect(bodyWithoutFallback, 'Hello right back!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Applying a multiline quote', () {
|
||||||
|
final body = "> Hallo Welt\n> How are you?\nI'm fine.\nThank you!";
|
||||||
|
final reply = ReplyData(
|
||||||
|
to: '',
|
||||||
|
id: '',
|
||||||
|
start: 0,
|
||||||
|
end: 28,
|
||||||
|
);
|
||||||
|
|
||||||
|
final bodyWithoutFallback = reply.removeFallback(body);
|
||||||
|
expect(bodyWithoutFallback, "I'm fine.\nThank you!");
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import 'helpers/xmpp.dart';
|
|||||||
/// Returns true if the roster manager triggeres an event for a given stanza
|
/// Returns true if the roster manager triggeres an event for a given stanza
|
||||||
Future<bool> testRosterManager(String bareJid, String resource, String stanzaString) async {
|
Future<bool> testRosterManager(String bareJid, String resource, String stanzaString) async {
|
||||||
var eventTriggered = false;
|
var eventTriggered = false;
|
||||||
final roster = RosterManager();
|
final roster = RosterManager(TestingRosterStateManager('', []));
|
||||||
roster.register(XmppManagerAttributes(
|
roster.register(XmppManagerAttributes(
|
||||||
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
||||||
sendEvent: (event) {
|
sendEvent: (event) {
|
||||||
@@ -127,7 +127,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
@@ -181,7 +181,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
@@ -235,7 +235,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
@@ -290,7 +290,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
@@ -308,7 +308,7 @@ void main() {
|
|||||||
group('Test roster pushes', () {
|
group('Test roster pushes', () {
|
||||||
test('Test for a CVE-2015-8688 style vulnerability', () async {
|
test('Test for a CVE-2015-8688 style vulnerability', () async {
|
||||||
var eventTriggered = false;
|
var eventTriggered = false;
|
||||||
final roster = RosterManager();
|
final roster = RosterManager(TestingRosterStateManager('', []));
|
||||||
roster.register(XmppManagerAttributes(
|
roster.register(XmppManagerAttributes(
|
||||||
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
||||||
sendEvent: (event) {
|
sendEvent: (event) {
|
||||||
|
|||||||
@@ -1,3 +1,31 @@
|
|||||||
|
## 0.1.2+9
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+8
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+7
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+6
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+5
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+4
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
|
## 0.1.2+3
|
||||||
|
|
||||||
|
- Update a dependency to the latest release.
|
||||||
|
|
||||||
## 0.1.2+2
|
## 0.1.2+2
|
||||||
|
|
||||||
- **FIX**: Fix reconnections when the connection is awaited.
|
- **FIX**: Fix reconnections when the connection is awaited.
|
||||||
|
|||||||
@@ -11,6 +11,22 @@ to return the list of SRV records, encoded by `MoxSrvRecord` objects. To perform
|
|||||||
resolution, one can use any DNS library. A Flutter plugin implementing SRV resolution
|
resolution, one can use any DNS library. A Flutter plugin implementing SRV resolution
|
||||||
is, for example, [moxdns](https://codeberg.org/moxxy/moxdns).
|
is, for example, [moxdns](https://codeberg.org/moxxy/moxdns).
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Include the following as a dependency in your pubspec file:
|
||||||
|
|
||||||
|
```
|
||||||
|
moxxmpp_socket_tcp:
|
||||||
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
version: 0.1.2+9
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
See `./LICENSE`.
|
See `./LICENSE`.
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
If you like what I do and you want to support me, feel free to donate to me on Ko-Fi.
|
||||||
|
|
||||||
|
[<img src="https://codeberg.org/moxxy/moxxyv2/raw/branch/master/assets/repo/kofi.png" height="36" style="height: 36px; border: 0px;"></img>](https://ko-fi.com/papatutuwawa)
|
||||||
|
|||||||
@@ -10,22 +10,31 @@ import 'package:moxxmpp_socket_tcp/src/rfc_2782.dart';
|
|||||||
|
|
||||||
/// TCP socket implementation for XmppConnection
|
/// TCP socket implementation for XmppConnection
|
||||||
class TCPSocketWrapper extends BaseSocketWrapper {
|
class TCPSocketWrapper extends BaseSocketWrapper {
|
||||||
TCPSocketWrapper(this._logData)
|
TCPSocketWrapper(this._logData);
|
||||||
: _log = Logger('TCPSocketWrapper'),
|
|
||||||
_dataStream = StreamController.broadcast(),
|
/// The underlying Socket/SecureSocket instance.
|
||||||
_eventStream = StreamController.broadcast(),
|
|
||||||
_secure = false,
|
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
Socket? _socket;
|
Socket? _socket;
|
||||||
bool _ignoreSocketClosure;
|
|
||||||
final StreamController<String> _dataStream;
|
/// Indicates that we expect a socket closure.
|
||||||
final StreamController<XmppSocketEvent> _eventStream;
|
bool _expectSocketClosure = false;
|
||||||
|
|
||||||
|
/// The stream of incoming data from the socket.
|
||||||
|
final StreamController<String> _dataStream = StreamController.broadcast();
|
||||||
|
|
||||||
|
/// The stream of outgoing (TCPSocketWrapper -> XmppConnection) events.
|
||||||
|
final StreamController<XmppSocketEvent> _eventStream = StreamController.broadcast();
|
||||||
|
|
||||||
|
/// A subscription on the socket's data stream.
|
||||||
StreamSubscription<dynamic>? _socketSubscription;
|
StreamSubscription<dynamic>? _socketSubscription;
|
||||||
|
|
||||||
final Logger _log;
|
/// Logger
|
||||||
|
final Logger _log = Logger('TCPSocketWrapper');
|
||||||
|
|
||||||
|
/// Flag to indicate if incoming and outgoing data should get logged.
|
||||||
final bool _logData;
|
final bool _logData;
|
||||||
|
|
||||||
bool _secure;
|
/// Indiacted whether the connection is secure.
|
||||||
|
bool _secure = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool isSecure() => _secure;
|
bool isSecure() => _secure;
|
||||||
@@ -72,7 +81,6 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
for (final srv in results) {
|
for (final srv in results) {
|
||||||
try {
|
try {
|
||||||
_log.finest('Attempting secure connection to ${srv.target}:${srv.port}...');
|
_log.finest('Attempting secure connection to ${srv.target}:${srv.port}...');
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
|
|
||||||
// Workaround: We cannot set the SNI directly when using SecureSocket.connect.
|
// Workaround: We cannot set the SNI directly when using SecureSocket.connect.
|
||||||
// instead, we connect using a regular socket and then secure it. This allows
|
// instead, we connect using a regular socket and then secure it. This allows
|
||||||
@@ -89,13 +97,11 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
onBadCertificate: (cert) => onBadCertificate(cert, domain),
|
onBadCertificate: (cert) => onBadCertificate(cert, domain),
|
||||||
);
|
);
|
||||||
|
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
_secure = true;
|
_secure = true;
|
||||||
_log.finest('Success!');
|
_log.finest('Success!');
|
||||||
return true;
|
return true;
|
||||||
} on Exception catch(e) {
|
} on Exception catch(e) {
|
||||||
_log.finest('Failure! $e');
|
_log.finest('Failure! $e');
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
|
|
||||||
if (e is HandshakeException) {
|
if (e is HandshakeException) {
|
||||||
failedDueToTLS = true;
|
failedDueToTLS = true;
|
||||||
@@ -118,19 +124,16 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
for (final srv in results) {
|
for (final srv in results) {
|
||||||
try {
|
try {
|
||||||
_log.finest('Attempting connection to ${srv.target}:${srv.port}...');
|
_log.finest('Attempting connection to ${srv.target}:${srv.port}...');
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
_socket = await Socket.connect(
|
_socket = await Socket.connect(
|
||||||
srv.target,
|
srv.target,
|
||||||
srv.port,
|
srv.port,
|
||||||
timeout: const Duration(seconds: 5),
|
timeout: const Duration(seconds: 5),
|
||||||
);
|
);
|
||||||
|
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
_log.finest('Success!');
|
_log.finest('Success!');
|
||||||
return true;
|
return true;
|
||||||
} on Exception catch(e) {
|
} on Exception catch(e) {
|
||||||
_log.finest('Failure! $e');
|
_log.finest('Failure! $e');
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,7 +147,6 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
Future<bool> _hostPortConnect(String host, int port) async {
|
Future<bool> _hostPortConnect(String host, int port) async {
|
||||||
try {
|
try {
|
||||||
_log.finest('Attempting fallback connection to $host:$port...');
|
_log.finest('Attempting fallback connection to $host:$port...');
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
_socket = await Socket.connect(
|
_socket = await Socket.connect(
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
@@ -154,7 +156,6 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
return true;
|
return true;
|
||||||
} on Exception catch(e) {
|
} on Exception catch(e) {
|
||||||
_log.finest('Failure! $e');
|
_log.finest('Failure! $e');
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,10 +180,11 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
_log.severe('Failed to secure socket since _socket is null');
|
_log.severe('Failed to secure socket since _socket is null');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// The socket is closed during the entire process
|
||||||
|
_expectSocketClosure = true;
|
||||||
|
|
||||||
_socket = await SecureSocket.secure(
|
_socket = await SecureSocket.secure(
|
||||||
_socket!,
|
_socket!,
|
||||||
supportedProtocols: const [ xmppClientALPNId ],
|
supportedProtocols: const [ xmppClientALPNId ],
|
||||||
@@ -190,12 +192,10 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
);
|
);
|
||||||
|
|
||||||
_secure = true;
|
_secure = true;
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
_setupStreams();
|
_setupStreams();
|
||||||
return true;
|
return true;
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
_log.severe('Failed to secure socket: $e');
|
_log.severe('Failed to secure socket: $e');
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
|
|
||||||
if (e is HandshakeException) {
|
if (e is HandshakeException) {
|
||||||
_eventStream.add(XmppSocketTLSFailedEvent());
|
_eventStream.add(XmppSocketTLSFailedEvent());
|
||||||
@@ -226,15 +226,14 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
);
|
);
|
||||||
// ignore: implicit_dynamic_parameter
|
// ignore: implicit_dynamic_parameter
|
||||||
_socket!.done.then((_) {
|
_socket!.done.then((_) {
|
||||||
if (!_ignoreSocketClosure) {
|
_eventStream.add(XmppSocketClosureEvent(_expectSocketClosure));
|
||||||
_eventStream.add(XmppSocketClosureEvent());
|
_expectSocketClosure = false;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> connect(String domain, { String? host, int? port }) async {
|
Future<bool> connect(String domain, { String? host, int? port }) async {
|
||||||
_ignoreSocketClosure = false;
|
_expectSocketClosure = false;
|
||||||
_secure = false;
|
_secure = false;
|
||||||
|
|
||||||
// Connection order:
|
// Connection order:
|
||||||
@@ -267,6 +266,8 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void close() {
|
void close() {
|
||||||
|
_expectSocketClosure = true;
|
||||||
|
|
||||||
if (_socketSubscription != null) {
|
if (_socketSubscription != null) {
|
||||||
_log.finest('Closing socket subscription');
|
_log.finest('Closing socket subscription');
|
||||||
_socketSubscription!.cancel();
|
_socketSubscription!.cancel();
|
||||||
@@ -277,13 +278,11 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
try {
|
try {
|
||||||
_socket!.close();
|
_socket!.close();
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
_log.warning('Closing socket threw exception: $e');
|
_log.warning('Closing socket threw exception: $e');
|
||||||
}
|
}
|
||||||
_ignoreSocketClosure = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -316,7 +315,5 @@ class TCPSocketWrapper extends BaseSocketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void prepareDisconnect() {
|
void prepareDisconnect() {}
|
||||||
_ignoreSocketClosure = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: moxxmpp_socket_tcp
|
name: moxxmpp_socket_tcp
|
||||||
description: A socket for moxxmpp using TCP that implements the RFC6120 connection algorithm and XEP-0368
|
description: A socket for moxxmpp using TCP that implements the RFC6120 connection algorithm and XEP-0368
|
||||||
version: 0.1.2+2
|
version: 0.1.2+9
|
||||||
homepage: https://codeberg.org/moxxy/moxxmpp
|
homepage: https://codeberg.org/moxxy/moxxmpp
|
||||||
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ dependencies:
|
|||||||
meta: ^1.6.0
|
meta: ^1.6.0
|
||||||
moxxmpp:
|
moxxmpp:
|
||||||
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
hosted: https://git.polynom.me/api/packages/Moxxy/pub
|
||||||
version: ^0.1.2+2
|
version: ^0.1.6+1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
lints: ^2.0.0
|
lints: ^2.0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user