Compare commits
42 Commits
763c93857d
...
moxxmpp-v0
| Author | SHA1 | Date | |
|---|---|---|---|
| 098687de45 | |||
| 6da3342f22 | |||
| 47337540f5 | |||
| 7e588f01b0 | |||
| c7c6c9dae4 | |||
| c77cfc4dcd | |||
| 1bd61076ea | |||
| bff4a6f707 | |||
| 1cc266c675 | |||
| 72099dfde5 | |||
| c9c45baabc | |||
| a01022c217 | |||
| c3459e6820 | |||
| e031e6d760 | |||
| 6c63b53cf4 | |||
| 1aa50699ad | |||
| b2c54ae8c0 | |||
| b16c9f4b30 | |||
| a8d80eaddf | |||
| 9baf1ed73c | |||
| ce3ea656ad | |||
| ed49212f5a | |||
| ad1242c47d | |||
| 890fcfb506 | |||
| d7723615fe | |||
| 6517065a1a | |||
| 9223a7d403 | |||
| 7ce6703c5b | |||
| 37261cddbb | |||
| d8c2ef6f3b | |||
| 98e5324409 | |||
| a69c2a23f2 | |||
| d8de093e4d | |||
| 678564dbb3 | |||
| 09d2601e85 | |||
| 41560682a1 | |||
| 473f8e4bb6 | |||
| 67446285c1 | |||
| e12f4688d3 | |||
| 2581bbe203 | |||
| 995f2e0248 | |||
| e2c8f79429 |
28
.woodpecker.yml
Normal file
28
.woodpecker.yml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
pipeline:
|
||||||
|
# Check moxxmpp
|
||||||
|
moxxmpp-lint:
|
||||||
|
image: dart:2.18.1
|
||||||
|
commands:
|
||||||
|
- cd packages/moxxmpp
|
||||||
|
- dart pub get
|
||||||
|
- dart analyze --fatal-infos --fatal-warnings
|
||||||
|
moxxmpp-test:
|
||||||
|
image: dart:2.18.1
|
||||||
|
commands:
|
||||||
|
- cd packages/moxxmpp
|
||||||
|
- dart pub get
|
||||||
|
- dart test
|
||||||
|
|
||||||
|
# Check moxxmpp_socket_tcp
|
||||||
|
moxxmpp_socket_tcp-lint:
|
||||||
|
image: dart:2.18.1
|
||||||
|
commands:
|
||||||
|
- cd packages/moxxmpp_socket_tcp
|
||||||
|
- dart pub get
|
||||||
|
- dart analyze --fatal-infos --fatal-warnings
|
||||||
|
# moxxmpp-test:
|
||||||
|
# image: dart:2.18.1
|
||||||
|
# commands:
|
||||||
|
# - cd packages/moxxmpp
|
||||||
|
# - dart pub get
|
||||||
|
# - dart test
|
||||||
@@ -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.
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,16 @@
|
|||||||
|
|
||||||
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`.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
library moxxmpp;
|
library moxxmpp;
|
||||||
|
|
||||||
export 'package:moxxmpp/src/connection.dart';
|
export 'package:moxxmpp/src/connection.dart';
|
||||||
|
export 'package:moxxmpp/src/connectivity.dart';
|
||||||
export 'package:moxxmpp/src/errors.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';
|
||||||
@@ -29,6 +30,7 @@ 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/errors.dart';
|
export 'package:moxxmpp/src/roster/errors.dart';
|
||||||
export 'package:moxxmpp/src/roster/roster.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';
|
||||||
|
|||||||
94
packages/moxxmpp/lib/src/awaiter.dart
Normal file
94
packages/moxxmpp/lib/src/awaiter.dart
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
|
/// A surrogate key for awaiting stanzas.
|
||||||
|
@immutable
|
||||||
|
class _StanzaSurrogateKey {
|
||||||
|
const _StanzaSurrogateKey(this.sentTo, this.id, this.tag);
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
|
||||||
|
/// The tag name of the stanza.
|
||||||
|
final String tag;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => sentTo.hashCode ^ id.hashCode ^ tag.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator==(Object other) {
|
||||||
|
return other is _StanzaSurrogateKey &&
|
||||||
|
other.sentTo == sentTo &&
|
||||||
|
other.id == id &&
|
||||||
|
other.tag == tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This class handles the await semantics for stanzas. Stanzas are given a "unique"
|
||||||
|
/// key equal to the tuple (to, id, tag) with which their response is identified.
|
||||||
|
///
|
||||||
|
/// That means that when sending ```<iq to="example@some.server.example" id="abc123" />```,
|
||||||
|
/// the response stanza must be from "example@some.server.example", have id "abc123" and
|
||||||
|
/// be an iq stanza.
|
||||||
|
///
|
||||||
|
/// This class also handles some "edge cases" of RFC 6120, like an empty "from" attribute.
|
||||||
|
class StanzaAwaiter {
|
||||||
|
/// The pending stanzas, identified by their surrogate key.
|
||||||
|
final Map<_StanzaSurrogateKey, Completer<XMLNode>> _pending = {};
|
||||||
|
|
||||||
|
/// The critical section for accessing [StanzaAwaiter._pending].
|
||||||
|
final Lock _lock = Lock();
|
||||||
|
|
||||||
|
/// Register a stanza as pending.
|
||||||
|
/// [to] is the value of the stanza's "to" attribute.
|
||||||
|
/// [id] is the value of the stanza's "id" attribute.
|
||||||
|
/// [tag] is the stanza's tag name.
|
||||||
|
///
|
||||||
|
/// Returns a future that might resolve to the response to the stanza.
|
||||||
|
Future<Future<XMLNode>> addPending(String to, String id, String tag) async {
|
||||||
|
final completer = await _lock.synchronized(() {
|
||||||
|
final completer = Completer<XMLNode>();
|
||||||
|
_pending[_StanzaSurrogateKey(to, id, tag)] = completer;
|
||||||
|
return completer;
|
||||||
|
});
|
||||||
|
|
||||||
|
return completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the stanza [stanza] is being awaited. [bareJid] is the bare JID of
|
||||||
|
/// the connection.
|
||||||
|
/// If [stanza] is awaited, resolves the future and returns true. If not, returns
|
||||||
|
/// false.
|
||||||
|
Future<bool> onData(XMLNode stanza, JID bareJid) async {
|
||||||
|
assert(bareJid.isBare(), 'bareJid must be bare');
|
||||||
|
|
||||||
|
final id = stanza.attributes['id'] as String?;
|
||||||
|
if (id == null) return false;
|
||||||
|
|
||||||
|
final key = _StanzaSurrogateKey(
|
||||||
|
// Section 8.1.2.1 § 3 of RFC 6120 says that an empty "from" indicates that the
|
||||||
|
// attribute is implicitly from our own bare JID.
|
||||||
|
stanza.attributes['from'] as String? ?? bareJid.toString(),
|
||||||
|
id,
|
||||||
|
stanza.tag,
|
||||||
|
);
|
||||||
|
|
||||||
|
return _lock.synchronized(() {
|
||||||
|
final completer = _pending[key];
|
||||||
|
if (completer != null) {
|
||||||
|
_pending.remove(key);
|
||||||
|
completer.complete(stanza);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
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/awaiter.dart';
|
||||||
import 'package:moxxmpp/src/buffer.dart';
|
import 'package:moxxmpp/src/buffer.dart';
|
||||||
|
import 'package:moxxmpp/src/connectivity.dart';
|
||||||
import 'package:moxxmpp/src/errors.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';
|
||||||
@@ -28,22 +31,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',
|
||||||
@@ -58,6 +74,7 @@ class StreamHeaderNonza extends XMLNode {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result of an awaited connection.
|
||||||
class XmppConnectionResult {
|
class XmppConnectionResult {
|
||||||
const XmppConnectionResult(
|
const XmppConnectionResult(
|
||||||
this.success,
|
this.success,
|
||||||
@@ -66,118 +83,136 @@ class XmppConnectionResult {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// True if the connection was successful. False if it failed for any reason.
|
||||||
final bool success;
|
final bool success;
|
||||||
|
|
||||||
// If a connection attempt fails, i.e. success is false, then this indicates the
|
// If a connection attempt fails, i.e. success is false, then this indicates the
|
||||||
// reason the connection failed.
|
// reason the connection failed.
|
||||||
final XmppError? error;
|
final XmppError? error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
ConnectivityManager connectivityManager,
|
||||||
this._socket,
|
this._socket,
|
||||||
{
|
{
|
||||||
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,
|
_connectivityManager = connectivityManager {
|
||||||
_routingState = RoutingState.preConnection,
|
// Allow the reconnection policy to perform reconnections by itself
|
||||||
_eventStreamController = StreamController.broadcast(),
|
_reconnectionPolicy.register(
|
||||||
_resource = '',
|
_attemptReconnection,
|
||||||
_streamBuffer = XmlStreamBuffer(),
|
_onNetworkConnectionLost,
|
||||||
_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
|
|
||||||
_reconnectionPolicy.register(
|
|
||||||
_attemptReconnection,
|
|
||||||
_onNetworkConnectionLost,
|
|
||||||
);
|
|
||||||
|
|
||||||
_socketStream = _socket.getDataStream();
|
_socketStream = _socket.getDataStream();
|
||||||
// TODO(Unknown): Handle on done
|
// TODO(Unknown): Handle on done
|
||||||
_socketStream.transform(_streamBuffer).forEach(handleXmlStream);
|
_socketStream.transform(_streamBuffer).forEach(handleXmlStream);
|
||||||
_socket.getEventStream().listen(_handleSocketEvent);
|
_socket.getEventStream().listen(_handleSocketEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// 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;
|
/// The class responsible for preventing errors on initial connection due
|
||||||
final Lock _awaitingResponseLock;
|
/// to no network.
|
||||||
|
final ConnectivityManager _connectivityManager;
|
||||||
|
|
||||||
|
/// A helper for handling await semantics with stanzas
|
||||||
|
final StanzaAwaiter _stanzaAwaiter = StanzaAwaiter();
|
||||||
|
|
||||||
/// 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;
|
||||||
@@ -188,61 +223,46 @@ class XmppConnection {
|
|||||||
/// none can be found.
|
/// none can be found.
|
||||||
T? getNegotiatorById<T extends XmppFeatureNegotiatorBase>(String id) => _featureNegotiators[id] as T?;
|
T? getNegotiatorById<T extends XmppFeatureNegotiatorBase>(String id) => _featureNegotiators[id] as T?;
|
||||||
|
|
||||||
/// Registers an [XmppManagerBase] sub-class as a manager on this connection.
|
/// Registers a list of [XmppManagerBase] sub-classes as managers on this connection.
|
||||||
/// [sortHandlers] should NOT be touched. It specified if the handler priorities
|
Future<void> registerManagers(List<XmppManagerBase> managers) async {
|
||||||
/// should be set up. The only time this should be false is when called via
|
|
||||||
/// [registerManagers].
|
|
||||||
void registerManager(XmppManagerBase manager, { bool sortHandlers = true }) {
|
|
||||||
_log.finest('Registering ${manager.getId()}');
|
|
||||||
manager.register(
|
|
||||||
XmppManagerAttributes(
|
|
||||||
sendStanza: sendStanza,
|
|
||||||
sendNonza: sendRawXML,
|
|
||||||
sendEvent: _sendEvent,
|
|
||||||
getConnectionSettings: () => _connectionSettings,
|
|
||||||
getManagerById: getManagerById,
|
|
||||||
isFeatureSupported: _serverFeatures.contains,
|
|
||||||
getFullJID: () => _connectionSettings.jid.withResource(_resource),
|
|
||||||
getSocket: () => _socket,
|
|
||||||
getConnection: () => this,
|
|
||||||
getNegotiatorById: getNegotiatorById,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
final id = manager.getId();
|
|
||||||
_xmppManagers[id] = manager;
|
|
||||||
|
|
||||||
if (id == discoManager) {
|
|
||||||
// NOTE: It is intentional that we do not exclude the [DiscoManager] from this
|
|
||||||
// loop. It may also register features.
|
|
||||||
for (final registeredManager in _xmppManagers.values) {
|
|
||||||
(manager as DiscoManager).addDiscoFeatures(registeredManager.getDiscoFeatures());
|
|
||||||
}
|
|
||||||
} else if (_xmppManagers.containsKey(discoManager)) {
|
|
||||||
(_xmppManagers[discoManager]! as DiscoManager).addDiscoFeatures(manager.getDiscoFeatures());
|
|
||||||
}
|
|
||||||
|
|
||||||
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers());
|
|
||||||
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers());
|
|
||||||
_outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers());
|
|
||||||
|
|
||||||
if (sortHandlers) {
|
|
||||||
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator);
|
|
||||||
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
|
||||||
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Like [registerManager], but for a list of managers.
|
|
||||||
void registerManagers(List<XmppManagerBase> managers) {
|
|
||||||
for (final manager in managers) {
|
for (final manager in managers) {
|
||||||
registerManager(manager, sortHandlers: false);
|
_log.finest('Registering ${manager.id}');
|
||||||
|
manager.register(
|
||||||
|
XmppManagerAttributes(
|
||||||
|
sendStanza: sendStanza,
|
||||||
|
sendNonza: sendRawXML,
|
||||||
|
sendEvent: _sendEvent,
|
||||||
|
getConnectionSettings: () => _connectionSettings,
|
||||||
|
getManagerById: getManagerById,
|
||||||
|
isFeatureSupported: _serverFeatures.contains,
|
||||||
|
getFullJID: () => _connectionSettings.jid.withResource(_resource),
|
||||||
|
getSocket: () => _socket,
|
||||||
|
getConnection: () => this,
|
||||||
|
getNegotiatorById: getNegotiatorById,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
_xmppManagers[manager.id] = manager;
|
||||||
|
|
||||||
|
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers());
|
||||||
|
_incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers());
|
||||||
|
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers());
|
||||||
|
_outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort them
|
// Sort them
|
||||||
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_incomingStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
|
_incomingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator);
|
_outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator);
|
||||||
|
|
||||||
|
// Run the post register callbacks
|
||||||
|
for (final manager in _xmppManagers.values) {
|
||||||
|
if (!manager.initialized) {
|
||||||
|
_log.finest('Running post-registration callback for ${manager.name}');
|
||||||
|
await manager.postRegisterCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register a list of negotiator with the connection.
|
/// Register a list of negotiator with the connection.
|
||||||
@@ -331,6 +351,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');
|
||||||
@@ -342,7 +367,7 @@ class XmppConnection {
|
|||||||
// Connect again
|
// Connect again
|
||||||
// ignore: cascade_invocations
|
// ignore: cascade_invocations
|
||||||
_log.finest('Calling connect() from _attemptReconnection');
|
_log.finest('Calling connect() from _attemptReconnection');
|
||||||
await connect();
|
await connect(waitForConnection: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when a stream ending error has occurred
|
/// Called when a stream ending error has occurred
|
||||||
@@ -365,7 +390,11 @@ class XmppConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _setConnectionState(XmppConnectionState.error);
|
if (await _connectivityManager.hasConnection()) {
|
||||||
|
await _setConnectionState(XmppConnectionState.error);
|
||||||
|
} else {
|
||||||
|
await _setConnectionState(XmppConnectionState.notConnected);
|
||||||
|
}
|
||||||
await _reconnectionPolicy.onFailure();
|
await _reconnectionPolicy.onFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,9 +403,9 @@ class XmppConnection {
|
|||||||
if (event is XmppSocketErrorEvent) {
|
if (event is XmppSocketErrorEvent) {
|
||||||
await handleError(SocketError(event));
|
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...');
|
||||||
}
|
}
|
||||||
@@ -423,10 +452,11 @@ class XmppConnection {
|
|||||||
/// If addId is true, then an 'id' attribute will be added to the stanza if [stanza] has
|
/// If addId is true, then an 'id' attribute will be added to the stanza if [stanza] has
|
||||||
/// 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, bool forceEncryption = 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());
|
||||||
}
|
}
|
||||||
@@ -444,8 +474,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_,
|
||||||
@@ -455,6 +483,7 @@ class XmppConnection {
|
|||||||
null,
|
null,
|
||||||
stanza_,
|
stanza_,
|
||||||
encrypted: encrypted,
|
encrypted: encrypted,
|
||||||
|
forceEncryption: forceEncryption,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
_log.fine('Done');
|
_log.fine('Done');
|
||||||
@@ -483,43 +512,41 @@ 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 {
|
if (awaitable) {
|
||||||
_log.fine('Lock acquired for $id');
|
future = await _stanzaAwaiter.addPending(
|
||||||
if (awaitable) {
|
// A stanza with no to attribute is for direct processing by the server. As such,
|
||||||
_awaitingResponse[id] = Completer();
|
// we can correlate it by just *assuming* we have that attribute
|
||||||
}
|
// (RFC 6120 Section 8.1.1.1)
|
||||||
|
data.stanza.to ?? _connectionSettings.jid.toBare().toString(),
|
||||||
|
data.stanza.id!,
|
||||||
|
data.stanza.tag,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// This uses the StreamManager to behave like a send queue
|
// This uses the StreamManager to behave like a send queue
|
||||||
if (await _canSendData()) {
|
if (await _canSendData()) {
|
||||||
_socket.write(stanzaString);
|
_socket.write(stanzaString);
|
||||||
|
|
||||||
// Try to ack every stanza
|
// Try to ack every stanza
|
||||||
// NOTE: Here we have send an Ack request nonza. This is now done by StreamManagementManager when receiving the StanzaSentEvent
|
// NOTE: Here we have send an Ack request nonza. This is now done by StreamManagementManager when receiving the StanzaSentEvent
|
||||||
} else {
|
} else {
|
||||||
_log.fine('_canSendData() returned false.');
|
_log.fine('_canSendData() returned false.');
|
||||||
}
|
}
|
||||||
|
|
||||||
_log.fine('Running post stanza handlers..');
|
_log.fine('Running post stanza handlers..');
|
||||||
await _runOutgoingPostStanzaHandlers(
|
await _runOutgoingPostStanzaHandlers(
|
||||||
stanza_,
|
stanza_,
|
||||||
initial: StanzaHandlerData(
|
initial: StanzaHandlerData(
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
null,
|
null,
|
||||||
stanza_,
|
stanza_,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
_log.fine('Done');
|
_log.fine('Done');
|
||||||
|
|
||||||
if (awaitable) {
|
|
||||||
future = _awaitingResponse[id]!.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
_log.fine('Releasing lock for $id');
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
@@ -630,8 +657,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 {
|
||||||
@@ -673,30 +704,34 @@ 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
|
|
||||||
final id = stanza.attributes['id'] as String?;
|
|
||||||
var awaited = false;
|
|
||||||
await _awaitingResponseLock.synchronized(() async {
|
|
||||||
if (id != null && _awaitingResponse.containsKey(id)) {
|
|
||||||
_awaitingResponse[id]!.complete(incomingHandlers.stanza);
|
|
||||||
_awaitingResponse.remove(id);
|
|
||||||
awaited = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
final awaited = await _stanzaAwaiter.onData(
|
||||||
|
incomingPreHandlers.stanza,
|
||||||
|
_connectionSettings.jid.toBare(),
|
||||||
|
);
|
||||||
if (awaited) {
|
if (awaited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
await handleUnhandledStanza(this, incomingPreHandlers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -742,6 +777,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
|
||||||
@@ -765,6 +801,7 @@ class XmppConnection {
|
|||||||
final result = await _currentNegotiator!.negotiate(nonza);
|
final result = await _currentNegotiator!.negotiate(nonza);
|
||||||
if (result.isType<NegotiatorError>()) {
|
if (result.isType<NegotiatorError>()) {
|
||||||
_log.severe('Negotiator returned an error');
|
_log.severe('Negotiator returned an error');
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await handleError(result.get<NegotiatorError>());
|
await handleError(result.get<NegotiatorError>());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -788,6 +825,7 @@ 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 _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else {
|
} else {
|
||||||
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
_currentNegotiator = getNextNegotiator(_streamFeatures);
|
||||||
@@ -811,6 +849,7 @@ class XmppConnection {
|
|||||||
_log.finest('Negotiations done!');
|
_log.finest('Negotiations done!');
|
||||||
|
|
||||||
_updateRoutingState(RoutingState.handleStanzas);
|
_updateRoutingState(RoutingState.handleStanzas);
|
||||||
|
await _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
} else {
|
} else {
|
||||||
_log.finest('Picking new negotiator...');
|
_log.finest('Picking new negotiator...');
|
||||||
@@ -827,6 +866,7 @@ class XmppConnection {
|
|||||||
_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 _resetIsConnectionRunning();
|
||||||
await _onNegotiationsDone();
|
await _onNegotiationsDone();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -925,9 +965,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
|
||||||
@@ -936,8 +977,7 @@ 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);
|
await _reconnectionPolicy.setShouldReconnect(false);
|
||||||
_socketClosureTriggersReconnect = false;
|
|
||||||
|
|
||||||
if (triggeredByUser) {
|
if (triggeredByUser) {
|
||||||
getPresenceManager().sendUnavailablePresence();
|
getPresenceManager().sendUnavailablePresence();
|
||||||
@@ -968,32 +1008,47 @@ 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, bool waitForConnection = false }) 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,
|
||||||
|
waitForConnection: waitForConnection,
|
||||||
|
shouldReconnect: false,
|
||||||
|
);
|
||||||
return _connectionCompleter!.future;
|
return _connectionCompleter!.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start the connection process using the provided connection settings.
|
/// Start the connection process using the provided connection settings.
|
||||||
Future<void> connect({ String? lastResource }) async {
|
Future<void> connect({ String? lastResource, bool waitForConnection = false, bool shouldReconnect = true }) async {
|
||||||
if (_connectionState != XmppConnectionState.notConnected && _connectionState != XmppConnectionState.error) {
|
if (_connectionState != XmppConnectionState.notConnected && _connectionState != XmppConnectionState.error) {
|
||||||
_log.fine('Cancelling this connection attempt as one appears to be already running.');
|
_log.fine('Cancelling this connection attempt as one appears to be already running.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_runPreConnectionAssertions();
|
_runPreConnectionAssertions();
|
||||||
_reconnectionPolicy.setShouldReconnect(true);
|
await _resetIsConnectionRunning();
|
||||||
|
|
||||||
if (lastResource != null) {
|
if (lastResource != null) {
|
||||||
setResource(lastResource);
|
setResource(lastResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldReconnect) {
|
||||||
|
await _reconnectionPolicy.setShouldReconnect(true);
|
||||||
|
}
|
||||||
|
|
||||||
await _reconnectionPolicy.reset();
|
await _reconnectionPolicy.reset();
|
||||||
_socketClosureTriggersReconnect = true;
|
|
||||||
await _sendEvent(ConnectingEvent());
|
await _sendEvent(ConnectingEvent());
|
||||||
|
|
||||||
|
// If requested, wait until we have a network connection
|
||||||
|
if (waitForConnection) {
|
||||||
|
_log.info('Waiting for okay from connectivityManager');
|
||||||
|
await _connectivityManager.waitForConnection();
|
||||||
|
_log.info('Got okay from connectivityManager');
|
||||||
|
}
|
||||||
|
|
||||||
final smManager = getStreamManagementManager();
|
final smManager = getStreamManagementManager();
|
||||||
String? host;
|
String? host;
|
||||||
int? port;
|
int? port;
|
||||||
|
|||||||
18
packages/moxxmpp/lib/src/connectivity.dart
Normal file
18
packages/moxxmpp/lib/src/connectivity.dart
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/// This manager class is responsible to tell the moxxmpp XmppConnection
|
||||||
|
/// when a connection can be established or not, regarding the network availability.
|
||||||
|
abstract class ConnectivityManager {
|
||||||
|
/// Returns true if a network connection is available. If not, returns false.
|
||||||
|
Future<bool> hasConnection();
|
||||||
|
|
||||||
|
/// Returns a future that resolves once we have a network connection.
|
||||||
|
Future<void> waitForConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An implementation of [ConnectivityManager] that is always connected.
|
||||||
|
class AlwaysConnectedConnectivityManager extends ConnectivityManager {
|
||||||
|
@override
|
||||||
|
Future<bool> hasConnection() async => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> waitForConnection() async {}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
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';
|
||||||
@@ -53,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,
|
||||||
|
|||||||
@@ -1,10 +1,28 @@
|
|||||||
import 'package:moxxmpp/src/connection.dart';
|
import 'package:moxxmpp/src/connection.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
import 'package:moxxmpp/src/stanza.dart';
|
import 'package:moxxmpp/src/stanza.dart';
|
||||||
|
|
||||||
bool handleUnhandledStanza(XmppConnection conn, Stanza stanza) {
|
/// Bounce a stanza if it was not handled by any manager. [conn] is the connection object
|
||||||
if (stanza.type != 'error' && stanza.type != 'result') {
|
/// to use for sending the stanza. [data] is the StanzaHandlerData of the unhandled
|
||||||
conn.sendStanza(stanza.errorReply('cancel', 'feature-not-implemented'));
|
/// stanza.
|
||||||
}
|
Future<void> handleUnhandledStanza(XmppConnection conn, StanzaHandlerData data) async {
|
||||||
|
if (data.stanza.type != 'error' && data.stanza.type != 'result') {
|
||||||
|
final stanza = data.stanza.copyWith(
|
||||||
|
to: data.stanza.from,
|
||||||
|
from: data.stanza.to,
|
||||||
|
type: 'error',
|
||||||
|
children: [
|
||||||
|
buildErrorElement(
|
||||||
|
'cancel',
|
||||||
|
'feature-not-implemented',
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
return true;
|
await conn.sendStanza(
|
||||||
|
stanza,
|
||||||
|
awaitable: false,
|
||||||
|
forceEncryption: data.encrypted,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
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) {
|
||||||
// Algorithm taken from here: https://blog.samwhited.com/2021/02/xmpp-addresses/
|
// Algorithm taken from here: https://blog.samwhited.com/2021/02/xmpp-addresses/
|
||||||
var localPart = '';
|
var localPart = '';
|
||||||
@@ -43,12 +44,33 @@ class JID {
|
|||||||
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;
|
||||||
|
|
||||||
JID toBare() => JID(local, domain, '');
|
/// Converts the JID into a bare JID.
|
||||||
|
JID toBare() {
|
||||||
|
if (isBare()) return this;
|
||||||
|
|
||||||
|
return 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 = '';
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class XmppManagerAttributes {
|
|||||||
required this.getNegotiatorById,
|
required this.getNegotiatorById,
|
||||||
});
|
});
|
||||||
/// Send a stanza whose response can be awaited.
|
/// Send a stanza whose response can be awaited.
|
||||||
final Future<XMLNode> Function(Stanza stanza, { StanzaFromType addFrom, bool addId, bool awaitable, bool encrypted}) sendStanza;
|
final Future<XMLNode> Function(Stanza stanza, { StanzaFromType addFrom, bool addId, bool awaitable, bool encrypted, bool forceEncryption}) sendStanza;
|
||||||
|
|
||||||
/// Send a nonza.
|
/// Send a nonza.
|
||||||
final void Function(XMLNode) sendNonza;
|
final void Function(XMLNode) sendNonza;
|
||||||
|
|||||||
@@ -1,17 +1,27 @@
|
|||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/managers/attributes.dart';
|
import 'package:moxxmpp/src/managers/attributes.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/data.dart';
|
||||||
import 'package:moxxmpp/src/managers/handlers.dart';
|
import 'package:moxxmpp/src/managers/handlers.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
||||||
|
|
||||||
abstract class XmppManagerBase {
|
abstract class XmppManagerBase {
|
||||||
|
XmppManagerBase(this.id);
|
||||||
|
|
||||||
late final XmppManagerAttributes _managerAttributes;
|
late final XmppManagerAttributes _managerAttributes;
|
||||||
late final Logger _log;
|
late final Logger _log;
|
||||||
|
|
||||||
|
/// Flag indicating that the post registration callback has been called once.
|
||||||
|
bool initialized = false;
|
||||||
|
|
||||||
/// Registers the callbacks from XmppConnection with the manager
|
/// Registers the callbacks from XmppConnection with the manager
|
||||||
void register(XmppManagerAttributes attributes) {
|
void register(XmppManagerAttributes attributes) {
|
||||||
_managerAttributes = attributes;
|
_managerAttributes = attributes;
|
||||||
_log = Logger(getName());
|
_log = Logger(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the attributes that are registered with the manager.
|
/// Returns the attributes that are registered with the manager.
|
||||||
@@ -21,29 +31,42 @@ 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.
|
||||||
List<String> getDiscoFeatures() => [];
|
List<String> getDiscoFeatures() => [];
|
||||||
|
|
||||||
|
/// Return a list of identities that should be included in a disco response.
|
||||||
|
List<Identity> getDiscoIdentities() => [];
|
||||||
|
|
||||||
/// Return the Id (akin to xmlns) of this manager.
|
/// Return the Id (akin to xmlns) of this manager.
|
||||||
String getId();
|
final String id;
|
||||||
|
|
||||||
/// Return a name that will be used for logging.
|
|
||||||
String getName();
|
|
||||||
|
|
||||||
|
/// The name of the manager.
|
||||||
|
String get name => toString();
|
||||||
|
|
||||||
/// Return the logger for this manager.
|
/// Return the logger for this manager.
|
||||||
Logger get logger => _log;
|
Logger get logger => _log;
|
||||||
|
|
||||||
@@ -52,6 +75,24 @@ abstract class XmppManagerBase {
|
|||||||
|
|
||||||
/// Returns true if the XEP is supported on the server. If not, returns false
|
/// Returns true if the XEP is supported on the server. If not, returns false
|
||||||
Future<bool> isSupported();
|
Future<bool> isSupported();
|
||||||
|
|
||||||
|
/// Called after the registration of all managers against the XmppConnection is done.
|
||||||
|
/// This method is only called once during the entire lifetime of it.
|
||||||
|
@mustCallSuper
|
||||||
|
Future<void> postRegisterCallback() async {
|
||||||
|
initialized = true;
|
||||||
|
|
||||||
|
final disco = getAttributes().getManagerById<DiscoManager>(discoManager);
|
||||||
|
if (disco != null) {
|
||||||
|
if (getDiscoFeatures().isNotEmpty) {
|
||||||
|
disco.addFeatures(getDiscoFeatures());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getDiscoIdentities().isNotEmpty) {
|
||||||
|
disco.addIdentities(getDiscoIdentities());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Runs all NonzaHandlers of this Manager which match the nonza. Resolves to true if
|
/// Runs all NonzaHandlers of this Manager which match the nonza. Resolves to true if
|
||||||
/// the nonza has been handled by one of the handlers. Resolves to false otherwise.
|
/// the nonza has been handled by one of the handlers. Resolves to false otherwise.
|
||||||
@@ -69,4 +110,25 @@ abstract class XmppManagerBase {
|
|||||||
|
|
||||||
return handled;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's
|
||||||
|
/// children with [children].
|
||||||
|
///
|
||||||
|
/// Note that this function currently only accepts IQ stanzas.
|
||||||
|
Future<void> reply(StanzaHandlerData data, String type, List<XMLNode> children) async {
|
||||||
|
assert(data.stanza.tag == 'iq', 'Reply makes little sense for non-IQ stanzas');
|
||||||
|
|
||||||
|
final stanza = data.stanza.copyWith(
|
||||||
|
to: data.stanza.from,
|
||||||
|
from: data.stanza.to,
|
||||||
|
type: type,
|
||||||
|
children: children,
|
||||||
|
);
|
||||||
|
|
||||||
|
await getAttributes().sendStanza(
|
||||||
|
stanza,
|
||||||
|
awaitable: false,
|
||||||
|
forceEncryption: data.encrypted,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ class StanzaHandlerData with _$StanzaHandlerData {
|
|||||||
String? funCancellation,
|
String? funCancellation,
|
||||||
// Whether the stanza was received encrypted
|
// Whether the stanza was received encrypted
|
||||||
@Default(false) bool encrypted,
|
@Default(false) bool encrypted,
|
||||||
|
// If true, forces the encryption manager to encrypt to the JID, even if it
|
||||||
|
// would not normally. In the case of OMEMO: If shouldEncrypt returns false
|
||||||
|
// but forceEncryption is true, then the OMEMO manager will try to encrypt
|
||||||
|
// to the JID anyway.
|
||||||
|
@Default(false) bool forceEncryption,
|
||||||
// The stated type of encryption used, if any was used
|
// The stated type of encryption used, if any was used
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
// Delayed Delivery
|
// Delayed Delivery
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ mixin _$StanzaHandlerData {
|
|||||||
String? get funCancellation =>
|
String? get funCancellation =>
|
||||||
throw _privateConstructorUsedError; // Whether the stanza was received encrypted
|
throw _privateConstructorUsedError; // Whether the stanza was received encrypted
|
||||||
bool get encrypted =>
|
bool get encrypted =>
|
||||||
|
throw _privateConstructorUsedError; // If true, forces the encryption manager to encrypt to the JID, even if it
|
||||||
|
// would not normally. In the case of OMEMO: If shouldEncrypt returns false
|
||||||
|
// but forceEncryption is true, then the OMEMO manager will try to encrypt
|
||||||
|
// to the JID anyway.
|
||||||
|
bool get forceEncryption =>
|
||||||
throw _privateConstructorUsedError; // The stated type of encryption used, if any was used
|
throw _privateConstructorUsedError; // The stated type of encryption used, if any was used
|
||||||
ExplicitEncryptionType? get encryptionType =>
|
ExplicitEncryptionType? get encryptionType =>
|
||||||
throw _privateConstructorUsedError; // Delayed Delivery
|
throw _privateConstructorUsedError; // Delayed Delivery
|
||||||
@@ -94,6 +99,7 @@ abstract class $StanzaHandlerDataCopyWith<$Res> {
|
|||||||
String? funReplacement,
|
String? funReplacement,
|
||||||
String? funCancellation,
|
String? funCancellation,
|
||||||
bool encrypted,
|
bool encrypted,
|
||||||
|
bool forceEncryption,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other,
|
Map<String, dynamic> other,
|
||||||
@@ -132,6 +138,7 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? funReplacement = freezed,
|
Object? funReplacement = freezed,
|
||||||
Object? funCancellation = freezed,
|
Object? funCancellation = freezed,
|
||||||
Object? encrypted = freezed,
|
Object? encrypted = freezed,
|
||||||
|
Object? forceEncryption = freezed,
|
||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
@@ -213,6 +220,10 @@ class _$StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value.encrypted
|
? _value.encrypted
|
||||||
: encrypted // ignore: cast_nullable_to_non_nullable
|
: encrypted // ignore: cast_nullable_to_non_nullable
|
||||||
as bool,
|
as bool,
|
||||||
|
forceEncryption: forceEncryption == freezed
|
||||||
|
? _value.forceEncryption
|
||||||
|
: forceEncryption // ignore: cast_nullable_to_non_nullable
|
||||||
|
as bool,
|
||||||
encryptionType: encryptionType == freezed
|
encryptionType: encryptionType == freezed
|
||||||
? _value.encryptionType
|
? _value.encryptionType
|
||||||
: encryptionType // ignore: cast_nullable_to_non_nullable
|
: encryptionType // ignore: cast_nullable_to_non_nullable
|
||||||
@@ -271,6 +282,7 @@ abstract class _$$_StanzaHandlerDataCopyWith<$Res>
|
|||||||
String? funReplacement,
|
String? funReplacement,
|
||||||
String? funCancellation,
|
String? funCancellation,
|
||||||
bool encrypted,
|
bool encrypted,
|
||||||
|
bool forceEncryption,
|
||||||
ExplicitEncryptionType? encryptionType,
|
ExplicitEncryptionType? encryptionType,
|
||||||
DelayedDelivery? delayedDelivery,
|
DelayedDelivery? delayedDelivery,
|
||||||
Map<String, dynamic> other,
|
Map<String, dynamic> other,
|
||||||
@@ -311,6 +323,7 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
Object? funReplacement = freezed,
|
Object? funReplacement = freezed,
|
||||||
Object? funCancellation = freezed,
|
Object? funCancellation = freezed,
|
||||||
Object? encrypted = freezed,
|
Object? encrypted = freezed,
|
||||||
|
Object? forceEncryption = freezed,
|
||||||
Object? encryptionType = freezed,
|
Object? encryptionType = freezed,
|
||||||
Object? delayedDelivery = freezed,
|
Object? delayedDelivery = freezed,
|
||||||
Object? other = freezed,
|
Object? other = freezed,
|
||||||
@@ -392,6 +405,10 @@ class __$$_StanzaHandlerDataCopyWithImpl<$Res>
|
|||||||
? _value.encrypted
|
? _value.encrypted
|
||||||
: encrypted // ignore: cast_nullable_to_non_nullable
|
: encrypted // ignore: cast_nullable_to_non_nullable
|
||||||
as bool,
|
as bool,
|
||||||
|
forceEncryption: forceEncryption == freezed
|
||||||
|
? _value.forceEncryption
|
||||||
|
: forceEncryption // ignore: cast_nullable_to_non_nullable
|
||||||
|
as bool,
|
||||||
encryptionType: encryptionType == freezed
|
encryptionType: encryptionType == freezed
|
||||||
? _value.encryptionType
|
? _value.encryptionType
|
||||||
: encryptionType // ignore: cast_nullable_to_non_nullable
|
: encryptionType // ignore: cast_nullable_to_non_nullable
|
||||||
@@ -442,6 +459,7 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
this.funReplacement,
|
this.funReplacement,
|
||||||
this.funCancellation,
|
this.funCancellation,
|
||||||
this.encrypted = false,
|
this.encrypted = false,
|
||||||
|
this.forceEncryption = 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>{},
|
||||||
@@ -506,6 +524,13 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
@override
|
@override
|
||||||
@JsonKey()
|
@JsonKey()
|
||||||
final bool encrypted;
|
final bool encrypted;
|
||||||
|
// If true, forces the encryption manager to encrypt to the JID, even if it
|
||||||
|
// would not normally. In the case of OMEMO: If shouldEncrypt returns false
|
||||||
|
// but forceEncryption is true, then the OMEMO manager will try to encrypt
|
||||||
|
// to the JID anyway.
|
||||||
|
@override
|
||||||
|
@JsonKey()
|
||||||
|
final bool forceEncryption;
|
||||||
// The stated type of encryption used, if any was used
|
// The stated type of encryption used, if any was used
|
||||||
@override
|
@override
|
||||||
final ExplicitEncryptionType? encryptionType;
|
final ExplicitEncryptionType? encryptionType;
|
||||||
@@ -540,7 +565,7 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
|
|
||||||
@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, messageRetraction: $messageRetraction, lastMessageCorrectionSid: $lastMessageCorrectionSid, messageReactions: $messageReactions, stickerPackId: $stickerPackId)';
|
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, forceEncryption: $forceEncryption, encryptionType: $encryptionType, delayedDelivery: $delayedDelivery, other: $other, messageRetraction: $messageRetraction, lastMessageCorrectionSid: $lastMessageCorrectionSid, messageReactions: $messageReactions, stickerPackId: $stickerPackId)';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -572,6 +597,8 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
const DeepCollectionEquality()
|
const DeepCollectionEquality()
|
||||||
.equals(other.funCancellation, funCancellation) &&
|
.equals(other.funCancellation, funCancellation) &&
|
||||||
const DeepCollectionEquality().equals(other.encrypted, encrypted) &&
|
const DeepCollectionEquality().equals(other.encrypted, encrypted) &&
|
||||||
|
const DeepCollectionEquality()
|
||||||
|
.equals(other.forceEncryption, forceEncryption) &&
|
||||||
const DeepCollectionEquality()
|
const DeepCollectionEquality()
|
||||||
.equals(other.encryptionType, encryptionType) &&
|
.equals(other.encryptionType, encryptionType) &&
|
||||||
const DeepCollectionEquality()
|
const DeepCollectionEquality()
|
||||||
@@ -608,6 +635,7 @@ class _$_StanzaHandlerData implements _StanzaHandlerData {
|
|||||||
const DeepCollectionEquality().hash(funReplacement),
|
const DeepCollectionEquality().hash(funReplacement),
|
||||||
const DeepCollectionEquality().hash(funCancellation),
|
const DeepCollectionEquality().hash(funCancellation),
|
||||||
const DeepCollectionEquality().hash(encrypted),
|
const DeepCollectionEquality().hash(encrypted),
|
||||||
|
const DeepCollectionEquality().hash(forceEncryption),
|
||||||
const DeepCollectionEquality().hash(encryptionType),
|
const DeepCollectionEquality().hash(encryptionType),
|
||||||
const DeepCollectionEquality().hash(delayedDelivery),
|
const DeepCollectionEquality().hash(delayedDelivery),
|
||||||
const DeepCollectionEquality().hash(_other),
|
const DeepCollectionEquality().hash(_other),
|
||||||
@@ -641,6 +669,7 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
final String? funReplacement,
|
final String? funReplacement,
|
||||||
final String? funCancellation,
|
final String? funCancellation,
|
||||||
final bool encrypted,
|
final bool encrypted,
|
||||||
|
final bool forceEncryption,
|
||||||
final ExplicitEncryptionType? encryptionType,
|
final ExplicitEncryptionType? encryptionType,
|
||||||
final DelayedDelivery? delayedDelivery,
|
final DelayedDelivery? delayedDelivery,
|
||||||
final Map<String, dynamic> other,
|
final Map<String, dynamic> other,
|
||||||
@@ -690,6 +719,11 @@ abstract class _StanzaHandlerData implements StanzaHandlerData {
|
|||||||
String? get funCancellation;
|
String? get funCancellation;
|
||||||
@override // Whether the stanza was received encrypted
|
@override // Whether the stanza was received encrypted
|
||||||
bool get encrypted;
|
bool get encrypted;
|
||||||
|
@override // If true, forces the encryption manager to encrypt to the JID, even if it
|
||||||
|
// would not normally. In the case of OMEMO: If shouldEncrypt returns false
|
||||||
|
// but forceEncryption is true, then the OMEMO manager will try to encrypt
|
||||||
|
// to the JID anyway.
|
||||||
|
bool get forceEncryption;
|
||||||
@override // The stated type of encryption used, if any was used
|
@override // The stated type of encryption used, if any was used
|
||||||
ExplicitEncryptionType? get encryptionType;
|
ExplicitEncryptionType? get encryptionType;
|
||||||
@override // Delayed Delivery
|
@override // Delayed Delivery
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -1,25 +1,25 @@
|
|||||||
const smManager = 'im.moxxmpp.streammangementmanager';
|
const smManager = 'org.moxxmpp.streammangementmanager';
|
||||||
const discoManager = 'im.moxxmpp.discomanager';
|
const discoManager = 'org.moxxmpp.discomanager';
|
||||||
const messageManager = 'im.moxxmpp.messagemanager';
|
const messageManager = 'org.moxxmpp.messagemanager';
|
||||||
const rosterManager = 'im.moxxmpp.rostermanager';
|
const rosterManager = 'org.moxxmpp.rostermanager';
|
||||||
const presenceManager = 'im.moxxmpp.presencemanager';
|
const presenceManager = 'org.moxxmpp.presencemanager';
|
||||||
const csiManager = 'im.moxxmpp.csimanager';
|
const csiManager = 'org.moxxmpp.csimanager';
|
||||||
const carbonsManager = 'im.moxxmpp.carbonsmanager';
|
const carbonsManager = 'org.moxxmpp.carbonsmanager';
|
||||||
const vcardManager = 'im.moxxmpp.vcardmanager';
|
const vcardManager = 'org.moxxmpp.vcardmanager';
|
||||||
const pubsubManager = 'im.moxxmpp.pubsubmanager';
|
const pubsubManager = 'org.moxxmpp.pubsubmanager';
|
||||||
const userAvatarManager = 'im.moxxmpp.useravatarmanager';
|
const userAvatarManager = 'org.moxxmpp.useravatarmanager';
|
||||||
const stableIdManager = 'im.moxxmpp.stableidmanager';
|
const stableIdManager = 'org.moxxmpp.stableidmanager';
|
||||||
const simsManager = 'im.moxxmpp.simsmanager';
|
const simsManager = 'org.moxxmpp.simsmanager';
|
||||||
const messageDeliveryReceiptManager = 'im.moxxmpp.messagedeliveryreceiptmanager';
|
const messageDeliveryReceiptManager = 'org.moxxmpp.messagedeliveryreceiptmanager';
|
||||||
const chatMarkerManager = 'im.moxxmpp.chatmarkermanager';
|
const chatMarkerManager = 'org.moxxmpp.chatmarkermanager';
|
||||||
const oobManager = 'im.moxxmpp.oobmanager';
|
const oobManager = 'org.moxxmpp.oobmanager';
|
||||||
const sfsManager = 'im.moxxmpp.sfsmanager';
|
const sfsManager = 'org.moxxmpp.sfsmanager';
|
||||||
const messageRepliesManager = 'im.moxxmpp.messagerepliesmanager';
|
const messageRepliesManager = 'org.moxxmpp.messagerepliesmanager';
|
||||||
const blockingManager = 'im.moxxmpp.blockingmanager';
|
const blockingManager = 'org.moxxmpp.blockingmanager';
|
||||||
const httpFileUploadManager = 'im.moxxmpp.httpfileuploadmanager';
|
const httpFileUploadManager = 'org.moxxmpp.httpfileuploadmanager';
|
||||||
const chatStateManager = 'im.moxxmpp.chatstatemanager';
|
const chatStateManager = 'org.moxxmpp.chatstatemanager';
|
||||||
const pingManager = 'im.moxxmpp.ping';
|
const pingManager = 'org.moxxmpp.ping';
|
||||||
const fileUploadNotificationManager = 'im.moxxmpp.fileuploadnotificationmanager';
|
const fileUploadNotificationManager = 'org.moxxmpp.fileuploadnotificationmanager';
|
||||||
const omemoManager = 'org.moxxmpp.omemomanager';
|
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';
|
||||||
@@ -28,3 +28,4 @@ const messageRetractionManager = 'org.moxxmpp.messageretractionmanager';
|
|||||||
const lastMessageCorrectionManager = 'org.moxxmpp.lastmessagecorrectionmanager';
|
const lastMessageCorrectionManager = 'org.moxxmpp.lastmessagecorrectionmanager';
|
||||||
const messageReactionsManager = 'org.moxxmpp.messagereactionsmanager';
|
const messageReactionsManager = 'org.moxxmpp.messagereactionsmanager';
|
||||||
const stickersManager = 'org.moxxmpp.stickersmanager';
|
const stickersManager = 'org.moxxmpp.stickersmanager';
|
||||||
|
const entityCapabilitiesManager = 'org.moxxmpp.entitycapabilities';
|
||||||
|
|||||||
@@ -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';
|
||||||
@@ -20,6 +21,7 @@ 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.
|
/// Data used to build a message stanza.
|
||||||
///
|
///
|
||||||
@@ -74,11 +76,7 @@ class MessageDetails {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MessageManager extends XmppManagerBase {
|
class MessageManager extends XmppManagerBase {
|
||||||
@override
|
MessageManager() : super(messageManager);
|
||||||
String getId() => messageManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'MessageManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
@@ -140,6 +138,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',
|
||||||
@@ -148,11 +151,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(
|
||||||
@@ -176,7 +179,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}',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -4,11 +4,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
|
||||||
|
|
||||||
class PingManager extends XmppManagerBase {
|
class PingManager extends XmppManagerBase {
|
||||||
@override
|
PingManager() : super(pingManager);
|
||||||
String getId() => pingManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'PingManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|||||||
@@ -8,23 +8,19 @@ 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/xeps/xep_0030/types.dart';
|
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
|
||||||
import 'package:moxxmpp/src/xeps/xep_0115.dart';
|
|
||||||
import 'package:moxxmpp/src/xeps/xep_0414.dart';
|
|
||||||
|
|
||||||
|
/// A function that will be called when presence, outside of subscription request
|
||||||
|
/// management, will be sent. Useful for managers that want to add [XMLNode]s to said
|
||||||
|
/// presence.
|
||||||
|
typedef PresencePreSendCallback = Future<List<XMLNode>> Function();
|
||||||
|
|
||||||
|
/// A mandatory manager that handles initial presence sending, sending of subscription
|
||||||
|
/// request management requests and triggers events for incoming presence stanzas.
|
||||||
class PresenceManager extends XmppManagerBase {
|
class PresenceManager extends XmppManagerBase {
|
||||||
PresenceManager(this._capHashNode) : _capabilityHash = null, super();
|
PresenceManager() : super(presenceManager);
|
||||||
String? _capabilityHash;
|
|
||||||
final String _capHashNode;
|
|
||||||
|
|
||||||
String get capabilityHashNode => _capHashNode;
|
/// The list of pre-send callbacks.
|
||||||
|
final List<PresencePreSendCallback> _presenceCallbacks = List.empty(growable: true);
|
||||||
@override
|
|
||||||
String getId() => presenceManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'PresenceManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
@@ -39,6 +35,11 @@ class PresenceManager extends XmppManagerBase {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
/// Register the pre-send callback [callback].
|
||||||
|
void registerPreSendCallback(PresencePreSendCallback callback) {
|
||||||
|
_presenceCallbacks.add(callback);
|
||||||
|
}
|
||||||
|
|
||||||
Future<StanzaHandlerData> _onPresence(Stanza presence, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onPresence(Stanza presence, StanzaHandlerData state) async {
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
@@ -63,43 +64,26 @@ class PresenceManager extends XmppManagerBase {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the capability hash.
|
|
||||||
Future<String> getCapabilityHash() async {
|
|
||||||
final manager = getAttributes().getManagerById(discoManager)! as DiscoManager;
|
|
||||||
_capabilityHash ??= await calculateCapabilityHash(
|
|
||||||
DiscoInfo(
|
|
||||||
manager.getRegisteredDiscoFeatures(),
|
|
||||||
manager.getIdentities(),
|
|
||||||
[],
|
|
||||||
getAttributes().getFullJID(),
|
|
||||||
),
|
|
||||||
getHashByName('sha-1')!,
|
|
||||||
);
|
|
||||||
|
|
||||||
return _capabilityHash!;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sends the initial presence to enable receiving messages.
|
/// Sends the initial presence to enable receiving messages.
|
||||||
Future<void> sendInitialPresence() async {
|
Future<void> sendInitialPresence() async {
|
||||||
|
final children = List<XMLNode>.from([
|
||||||
|
XMLNode(
|
||||||
|
tag: 'show',
|
||||||
|
text: 'chat',
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
for (final callback in _presenceCallbacks) {
|
||||||
|
children.addAll(
|
||||||
|
await callback(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final attrs = getAttributes();
|
final attrs = getAttributes();
|
||||||
attrs.sendNonza(
|
attrs.sendNonza(
|
||||||
Stanza.presence(
|
Stanza.presence(
|
||||||
from: attrs.getFullJID().toString(),
|
from: attrs.getFullJID().toString(),
|
||||||
children: [
|
children: children,
|
||||||
XMLNode(
|
|
||||||
tag: 'show',
|
|
||||||
text: 'chat',
|
|
||||||
),
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'c',
|
|
||||||
xmlns: capsXmlns,
|
|
||||||
attributes: {
|
|
||||||
'hash': 'sha-1',
|
|
||||||
'node': _capHashNode,
|
|
||||||
'ver': await getCapabilityHash()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,29 +2,42 @@ import 'dart:async';
|
|||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moxxmpp/src/util/queue.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;
|
@protected
|
||||||
|
bool isReconnecting = false;
|
||||||
|
|
||||||
/// And the corresponding lock
|
/// And the corresponding lock
|
||||||
final Lock _isReconnectingLock;
|
@protected
|
||||||
|
final Lock lock = Lock();
|
||||||
|
|
||||||
|
/// The lock for accessing [_shouldAttemptReconnection]
|
||||||
|
@protected
|
||||||
|
final Lock shouldReconnectLock = 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;
|
||||||
|
|
||||||
@@ -42,92 +55,122 @@ abstract class ReconnectionPolicy {
|
|||||||
/// Caled by the XmppConnection when the reconnection was successful.
|
/// Caled by the XmppConnection when the reconnection was successful.
|
||||||
Future<void> onSuccess();
|
Future<void> onSuccess();
|
||||||
|
|
||||||
bool get shouldReconnect => _shouldAttemptReconnection;
|
Future<bool> getShouldReconnect() async {
|
||||||
|
return shouldReconnectLock.synchronized(() => _shouldAttemptReconnection);
|
||||||
|
}
|
||||||
|
|
||||||
/// Set whether a reconnection attempt should be made.
|
/// Set whether a reconnection attempt should be made.
|
||||||
void setShouldReconnect(bool value) {
|
Future<void> setShouldReconnect(bool value) async {
|
||||||
_shouldAttemptReconnection = value;
|
return shouldReconnectLock.synchronized(() => _shouldAttemptReconnection = value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the manager is currently triggering a reconnection. If not, returns
|
/// Returns true if the manager is currently triggering a reconnection. If not, returns
|
||||||
/// false.
|
/// false.
|
||||||
Future<bool> isReconnectionRunning() async {
|
Future<bool> isReconnectionRunning() async {
|
||||||
return _isReconnectingLock.synchronized(() => _isReconnecting);
|
return lock.synchronized(() => isReconnecting);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the _isReconnecting state to [value].
|
/// Set the isReconnecting state to [value].
|
||||||
@protected
|
@protected
|
||||||
Future<void> setIsReconnecting(bool value) async {
|
Future<void> setIsReconnecting(bool value) async {
|
||||||
await _isReconnectingLock.synchronized(() async {
|
await lock.synchronized(() async {
|
||||||
_isReconnecting = value;
|
isReconnecting = value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@protected
|
|
||||||
Future<bool> testAndSetIsReconnecting() async {
|
|
||||||
return _isReconnectingLock.synchronized(() {
|
|
||||||
if (_isReconnecting) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
_isReconnecting = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A simple reconnection strategy: Make the reconnection delays exponentially longer
|
/// A simple reconnection strategy: Make the reconnection delays exponentially longer
|
||||||
/// for every failed attempt.
|
/// for every failed attempt.
|
||||||
class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
/// NOTE: This ReconnectionPolicy may be broken
|
||||||
|
class RandomBackoffReconnectionPolicy extends ReconnectionPolicy {
|
||||||
|
RandomBackoffReconnectionPolicy(
|
||||||
|
this._minBackoffTime,
|
||||||
|
this._maxBackoffTime,
|
||||||
|
) : assert(_minBackoffTime < _maxBackoffTime, '_minBackoffTime must be smaller than _maxBackoffTime'),
|
||||||
|
super();
|
||||||
|
|
||||||
ExponentialBackoffReconnectionPolicy(this._maxBackoffTime)
|
/// The maximum time in seconds that a backoff should be.
|
||||||
: _counter = 0,
|
|
||||||
_log = Logger('ExponentialBackoffReconnectionPolicy'),
|
|
||||||
super();
|
|
||||||
final int _maxBackoffTime;
|
final int _maxBackoffTime;
|
||||||
int _counter;
|
|
||||||
|
/// The minimum time in seconds that a backoff should be.
|
||||||
|
final int _minBackoffTime;
|
||||||
|
|
||||||
|
/// Backoff timer.
|
||||||
Timer? _timer;
|
Timer? _timer;
|
||||||
final Logger _log;
|
|
||||||
|
final Lock _timerLock = Lock();
|
||||||
|
|
||||||
|
/// Logger.
|
||||||
|
final Logger _log = Logger('RandomBackoffReconnectionPolicy');
|
||||||
|
|
||||||
|
/// Event queue
|
||||||
|
final AsyncQueue _eventQueue = AsyncQueue();
|
||||||
|
|
||||||
/// Called when the backoff expired
|
/// Called when the backoff expired
|
||||||
Future<void> _onTimerElapsed() async {
|
Future<void> _onTimerElapsed() async {
|
||||||
final isReconnecting = await isReconnectionRunning();
|
_log.fine('Timer elapsed. Waiting for lock');
|
||||||
if (shouldReconnect) {
|
await lock.synchronized(() async {
|
||||||
if (!isReconnecting) {
|
_log.fine('Lock aquired');
|
||||||
await setIsReconnecting(true);
|
if (!(await getShouldReconnect())) {
|
||||||
await performReconnect!();
|
_log.fine('Backoff timer expired but getShouldReconnect() returned false');
|
||||||
} else {
|
return;
|
||||||
// Should never happen.
|
|
||||||
_log.fine('Backoff timer expired but reconnection is running, so doing nothing.');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (isReconnecting) {
|
||||||
|
_log.fine('Backoff timer expired but a reconnection is running, so doing nothing.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_log.fine('Triggering reconnect');
|
||||||
|
isReconnecting = true;
|
||||||
|
await performReconnect!();
|
||||||
|
});
|
||||||
|
|
||||||
|
await _timerLock.synchronized(() {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _reset() async {
|
||||||
|
_log.finest('Resetting internal state');
|
||||||
|
|
||||||
|
await _timerLock.synchronized(() {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
await setIsReconnecting(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> reset() async {
|
Future<void> reset() async {
|
||||||
_log.finest('Resetting internal state');
|
// ignore: unnecessary_lambdas
|
||||||
_counter = 0;
|
await _eventQueue.addJob(() => _reset());
|
||||||
await setIsReconnecting(false);
|
|
||||||
|
|
||||||
if (_timer != null) {
|
|
||||||
_timer!.cancel();
|
|
||||||
_timer = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
Future<void> _onFailure() async {
|
||||||
Future<void> onFailure() async {
|
final shouldContinue = await _timerLock.synchronized(() {
|
||||||
_log.finest('Failure occured. Starting exponential backoff');
|
return _timer == null;
|
||||||
_counter++;
|
});
|
||||||
|
if (!shouldContinue) {
|
||||||
if (_timer != null) {
|
_log.finest('_onFailure: Not backing off since _timer is already running');
|
||||||
_timer!.cancel();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait at max 80 seconds.
|
final seconds = Random().nextInt(_maxBackoffTime - _minBackoffTime) + _minBackoffTime;
|
||||||
final seconds = min(min(pow(2, _counter).toInt(), 80), _maxBackoffTime);
|
_log.finest('Failure occured. Starting random backoff with ${seconds}s');
|
||||||
|
_timer?.cancel();
|
||||||
|
|
||||||
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onFailure() async {
|
||||||
|
// ignore: unnecessary_lambdas
|
||||||
|
await _eventQueue.addJob(() => _onFailure());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> onSuccess() async {
|
Future<void> onSuccess() async {
|
||||||
@@ -135,7 +178,7 @@ class ExponentialBackoffReconnectionPolicy extends ReconnectionPolicy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A stub reconnection policy for tests
|
/// A stub reconnection policy for tests.
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
class TestingReconnectionPolicy extends ReconnectionPolicy {
|
class TestingReconnectionPolicy extends ReconnectionPolicy {
|
||||||
TestingReconnectionPolicy() : super();
|
TestingReconnectionPolicy() : super();
|
||||||
|
|||||||
@@ -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';
|
||||||
@@ -8,17 +11,42 @@ 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/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/result.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
|
|
||||||
|
@immutable
|
||||||
class XmppRosterItem {
|
class XmppRosterItem {
|
||||||
XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
|
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 {
|
||||||
@@ -28,13 +56,13 @@ enum RosterRemovalResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RosterRequestResult {
|
class RosterRequestResult {
|
||||||
RosterRequestResult({ required this.items, this.ver });
|
RosterRequestResult(this.items, this.ver);
|
||||||
List<XmppRosterItem> items;
|
List<XmppRosterItem> items;
|
||||||
String? ver;
|
String? ver;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RosterPushEvent extends XmppEvent {
|
class RosterPushResult {
|
||||||
RosterPushEvent({ required this.item, this.ver });
|
RosterPushResult(this.item, this.ver);
|
||||||
final XmppRosterItem item;
|
final XmppRosterItem item;
|
||||||
final String? ver;
|
final String? ver;
|
||||||
}
|
}
|
||||||
@@ -65,16 +93,17 @@ 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);
|
||||||
|
|
||||||
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
|
|
||||||
String getId() => rosterManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'RosterManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
@@ -87,17 +116,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?;
|
||||||
@@ -114,6 +133,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) {
|
||||||
@@ -121,22 +141,25 @@ 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 reply(
|
||||||
|
state,
|
||||||
|
'result',
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
}
|
}
|
||||||
@@ -145,71 +168,69 @@ class RosterManager extends XmppManagerBase {
|
|||||||
/// 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<Result<RosterRequestResult, RosterError>> _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 Result(NoQueryError());
|
return Result(NoQueryError());
|
||||||
}
|
}
|
||||||
|
|
||||||
final ver = query.attributes['ver'] as String?;
|
final result = RosterRequestResult(
|
||||||
if (ver != null) {
|
items,
|
||||||
_rosterVersion = ver;
|
rosterVersion,
|
||||||
await commitLastRosterVersion(ver);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result(
|
|
||||||
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<Result<RosterRequestResult, RosterError>> 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 Result(UnknownError());
|
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<Result<RosterRequestResult?, RosterError>> 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(
|
||||||
@@ -219,7 +240,7 @@ class RosterManager extends XmppManagerBase {
|
|||||||
tag: 'query',
|
tag: 'query',
|
||||||
xmlns: rosterXmlns,
|
xmlns: rosterXmlns,
|
||||||
attributes: {
|
attributes: {
|
||||||
'ver': _rosterVersion ?? ''
|
'ver': await _stateManager.getRosterVersion() ?? '',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
|
|||||||
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 {
|
||||||
|
|||||||
@@ -114,40 +114,27 @@ class Stanza extends XMLNode {
|
|||||||
children: children ?? this.children,
|
children: children ?? this.children,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Stanza reply({ List<XMLNode> children = const [] }) {
|
|
||||||
return copyWith(
|
/// Build an <error /> element with a child <[condition] type="[type]" />. If [text]
|
||||||
from: attributes['to'] as String?,
|
/// is not null, then the condition element will contain a <text /> element with [text]
|
||||||
to: attributes['from'] as String?,
|
/// as the body.
|
||||||
type: tag == 'iq' ? 'result' : attributes['type'] as String?,
|
XMLNode buildErrorElement(String type, String condition, { String? text }) {
|
||||||
children: children,
|
return XMLNode(
|
||||||
);
|
tag: 'error',
|
||||||
}
|
attributes: <String, dynamic>{ 'type': type },
|
||||||
|
children: [
|
||||||
Stanza errorReply(String type, String condition, { String? text }) {
|
XMLNode.xmlns(
|
||||||
return copyWith(
|
tag: condition,
|
||||||
from: attributes['to'] as String?,
|
xmlns: fullStanzaXmlns,
|
||||||
to: attributes['from'] as String?,
|
children: text != null ? [
|
||||||
type: 'error',
|
XMLNode.xmlns(
|
||||||
children: [
|
tag: 'text',
|
||||||
XMLNode(
|
xmlns: fullStanzaXmlns,
|
||||||
tag: 'error',
|
text: text,
|
||||||
attributes: <String, dynamic>{ 'type': type },
|
)
|
||||||
children: [
|
] : [],
|
||||||
XMLNode.xmlns(
|
),
|
||||||
tag: condition,
|
],
|
||||||
xmlns: fullStanzaXmlns,
|
);
|
||||||
children: text != null ?[
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'text',
|
|
||||||
xmlns: fullStanzaXmlns,
|
|
||||||
text: text,
|
|
||||||
)
|
|
||||||
] : [],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import 'package:xml/xml.dart';
|
import 'package:xml/xml.dart';
|
||||||
|
|
||||||
class XMLNode {
|
class XMLNode {
|
||||||
|
|
||||||
XMLNode({
|
XMLNode({
|
||||||
required this.tag,
|
required this.tag,
|
||||||
this.attributes = const <String, dynamic>{},
|
this.attributes = const <String, dynamic>{},
|
||||||
|
|||||||
56
packages/moxxmpp/lib/src/util/queue.dart
Normal file
56
packages/moxxmpp/lib/src/util/queue.dart
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:collection';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
|
/// A job to be submitted to an [AsyncQueue].
|
||||||
|
typedef AsyncQueueJob = Future<void> Function();
|
||||||
|
|
||||||
|
/// A (hopefully) async-safe queue that attempts to force
|
||||||
|
/// in-order execution of its jobs.
|
||||||
|
class AsyncQueue {
|
||||||
|
/// The lock for accessing [AsyncQueue._lock] and [AsyncQueue._running].
|
||||||
|
final Lock _lock = Lock();
|
||||||
|
|
||||||
|
/// The actual job queue.
|
||||||
|
final Queue<AsyncQueueJob> _queue = Queue<AsyncQueueJob>();
|
||||||
|
|
||||||
|
/// Indicates whether we are currently executing a job.
|
||||||
|
bool _running = false;
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
Queue<AsyncQueueJob> get queue => _queue;
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
bool get isRunning => _running;
|
||||||
|
|
||||||
|
/// Adds a job [job] to the queue.
|
||||||
|
Future<void> addJob(AsyncQueueJob job) async {
|
||||||
|
await _lock.synchronized(() {
|
||||||
|
_queue.add(job);
|
||||||
|
|
||||||
|
if (!_running && _queue.isNotEmpty) {
|
||||||
|
_running = true;
|
||||||
|
unawaited(_popJob());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> clear() async {
|
||||||
|
await _lock.synchronized(_queue.clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _popJob() async {
|
||||||
|
final job = _queue.removeFirst();
|
||||||
|
final future = job();
|
||||||
|
await future;
|
||||||
|
|
||||||
|
await _lock.synchronized(() {
|
||||||
|
if (_queue.isNotEmpty) {
|
||||||
|
unawaited(_popJob());
|
||||||
|
} else {
|
||||||
|
_running = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
67
packages/moxxmpp/lib/src/util/wait.dart
Normal file
67
packages/moxxmpp/lib/src/util/wait.dart
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
|
/// This class allows for multiple asynchronous code places to wait on the
|
||||||
|
/// same computation of type [V], indentified by a key of type [K].
|
||||||
|
class WaitForTracker<K, V> {
|
||||||
|
/// The mapping of key -> Completer for the pending tasks.
|
||||||
|
final Map<K, List<Completer<V>>> _tracker = {};
|
||||||
|
|
||||||
|
/// The lock for accessing _tracker.
|
||||||
|
final Lock _lock = Lock();
|
||||||
|
|
||||||
|
/// Wait for a task with key [key]. If there was no such task already
|
||||||
|
/// present, returns null. If one or more tasks were already present, returns
|
||||||
|
/// a future that will resolve to the result of the first task.
|
||||||
|
Future<Future<V>?> waitFor(K key) async {
|
||||||
|
final result = await _lock.synchronized(() {
|
||||||
|
if (_tracker.containsKey(key)) {
|
||||||
|
// The task already exists. Just append outselves
|
||||||
|
final completer = Completer<V>();
|
||||||
|
_tracker[key]!.add(completer);
|
||||||
|
return completer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The task does not exist yet
|
||||||
|
_tracker[key] = List<Completer<V>>.empty(growable: true);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return result?.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve a task with key [key] to [value].
|
||||||
|
Future<void> resolve(K key, V value) async {
|
||||||
|
await _lock.synchronized(() {
|
||||||
|
if (!_tracker.containsKey(key)) return;
|
||||||
|
|
||||||
|
for (final completer in _tracker[key]!) {
|
||||||
|
completer.complete(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
_tracker.remove(key);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> resolveAll(V value) async {
|
||||||
|
await _lock.synchronized(() {
|
||||||
|
for (final key in _tracker.keys) {
|
||||||
|
for (final completer in _tracker[key]!) {
|
||||||
|
completer.complete(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove all tasks from the tracker.
|
||||||
|
Future<void> clear() async {
|
||||||
|
await _lock.synchronized(_tracker.clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
bool hasTasksRunning() => _tracker.isNotEmpty;
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
List<Completer<V>> getRunningTasks(K key) => _tracker[key]!;
|
||||||
|
}
|
||||||
@@ -11,13 +11,7 @@ import 'package:moxxmpp/src/xeps/xep_0446.dart';
|
|||||||
const fileUploadNotificationXmlns = 'proto:urn:xmpp:fun:0';
|
const fileUploadNotificationXmlns = 'proto:urn:xmpp:fun:0';
|
||||||
|
|
||||||
class FileUploadNotificationManager extends XmppManagerBase {
|
class FileUploadNotificationManager extends XmppManagerBase {
|
||||||
FileUploadNotificationManager() : super();
|
FileUploadNotificationManager() : super(fileUploadNotificationManager);
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => fileUploadNotificationManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'FileUploadNotificationManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import 'package:moxxmpp/src/namespaces.dart';
|
|||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
|
|
||||||
class DataFormOption {
|
class DataFormOption {
|
||||||
|
|
||||||
const DataFormOption({ required this.value, this.label });
|
const DataFormOption({ required this.value, this.label });
|
||||||
final String? label;
|
final String? label;
|
||||||
final String value;
|
final String value;
|
||||||
@@ -23,7 +22,6 @@ class DataFormOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DataFormField {
|
class DataFormField {
|
||||||
|
|
||||||
const DataFormField({
|
const DataFormField({
|
||||||
required this.options,
|
required this.options,
|
||||||
required this.values,
|
required this.values,
|
||||||
@@ -60,7 +58,6 @@ class DataFormField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DataForm {
|
class DataForm {
|
||||||
|
|
||||||
const DataForm({
|
const DataForm({
|
||||||
required this.type,
|
required this.type,
|
||||||
required this.instructions,
|
required this.instructions,
|
||||||
|
|||||||
23
packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart
Normal file
23
packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
@internal
|
||||||
|
@immutable
|
||||||
|
class DiscoCacheKey {
|
||||||
|
const DiscoCacheKey(this.jid, this.node);
|
||||||
|
|
||||||
|
/// The JID we're requesting disco data from.
|
||||||
|
final String jid;
|
||||||
|
|
||||||
|
/// Optionally the node we are requesting from.
|
||||||
|
final String? node;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) {
|
||||||
|
return other is DiscoCacheKey &&
|
||||||
|
jid == other.jid &&
|
||||||
|
node == other.node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => jid.hashCode ^ node.hashCode;
|
||||||
|
}
|
||||||
@@ -6,20 +6,20 @@ import 'package:moxxmpp/src/stringxml.dart';
|
|||||||
|
|
||||||
Stanza buildDiscoInfoQueryStanza(String entity, String? node) {
|
Stanza buildDiscoInfoQueryStanza(String entity, String? node) {
|
||||||
return Stanza.iq(to: entity, type: 'get', children: [
|
return Stanza.iq(to: entity, type: 'get', children: [
|
||||||
XMLNode.xmlns(
|
XMLNode.xmlns(
|
||||||
tag: 'query',
|
tag: 'query',
|
||||||
xmlns: discoInfoXmlns,
|
xmlns: discoInfoXmlns,
|
||||||
attributes: node != null ? { 'node': node } : {},
|
attributes: node != null ? { 'node': node } : {},
|
||||||
)
|
)
|
||||||
],);
|
],);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stanza buildDiscoItemsQueryStanza(String entity, { String? node }) {
|
Stanza buildDiscoItemsQueryStanza(String entity, { String? node }) {
|
||||||
return Stanza.iq(to: entity, type: 'get', children: [
|
return Stanza.iq(to: entity, type: 'get', children: [
|
||||||
XMLNode.xmlns(
|
XMLNode.xmlns(
|
||||||
tag: 'query',
|
tag: 'query',
|
||||||
xmlns: discoItemsXmlns,
|
xmlns: discoItemsXmlns,
|
||||||
attributes: node != null ? { 'node': node } : {},
|
attributes: node != null ? { 'node': node } : {},
|
||||||
)
|
)
|
||||||
],);
|
],);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moxxmpp/src/jid.dart';
|
import 'package:moxxmpp/src/jid.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
||||||
|
|
||||||
class Identity {
|
class Identity {
|
||||||
|
|
||||||
const Identity({ required this.category, required this.type, this.name, this.lang });
|
const Identity({ required this.category, required this.type, this.name, this.lang });
|
||||||
final String category;
|
final String category;
|
||||||
final String type;
|
final String type;
|
||||||
@@ -23,24 +24,96 @@ class Identity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@immutable
|
||||||
class DiscoInfo {
|
class DiscoInfo {
|
||||||
|
|
||||||
const DiscoInfo(
|
const DiscoInfo(
|
||||||
this.features,
|
this.features,
|
||||||
this.identities,
|
this.identities,
|
||||||
this.extendedInfo,
|
this.extendedInfo,
|
||||||
|
this.node,
|
||||||
this.jid,
|
this.jid,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
factory DiscoInfo.fromQuery(XMLNode query, JID jid) {
|
||||||
|
final features = List<String>.empty(growable: true);
|
||||||
|
final identities = List<Identity>.empty(growable: true);
|
||||||
|
final extendedInfo = List<DataForm>.empty(growable: true);
|
||||||
|
|
||||||
|
for (final element in query.children) {
|
||||||
|
if (element.tag == 'feature') {
|
||||||
|
features.add(element.attributes['var']! as String);
|
||||||
|
} else if (element.tag == 'identity') {
|
||||||
|
identities.add(
|
||||||
|
Identity(
|
||||||
|
category: element.attributes['category']! as String,
|
||||||
|
type: element.attributes['type']! as String,
|
||||||
|
name: element.attributes['name'] as String?,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else if (element.tag == 'x' && element.attributes['xmlns'] == dataFormsXmlns) {
|
||||||
|
extendedInfo.add(
|
||||||
|
parseDataForm(element),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return DiscoInfo(
|
||||||
|
features,
|
||||||
|
identities,
|
||||||
|
extendedInfo,
|
||||||
|
query.attributes['node'] as String?,
|
||||||
|
jid,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final List<String> features;
|
final List<String> features;
|
||||||
final List<Identity> identities;
|
final List<Identity> identities;
|
||||||
final List<DataForm> extendedInfo;
|
final List<DataForm> extendedInfo;
|
||||||
final JID jid;
|
final String? node;
|
||||||
|
final JID? jid;
|
||||||
|
|
||||||
|
XMLNode toXml() {
|
||||||
|
return XMLNode.xmlns(
|
||||||
|
tag: 'query',
|
||||||
|
xmlns: discoInfoXmlns,
|
||||||
|
attributes: node != null ?
|
||||||
|
<String, String>{ 'node': node!, } :
|
||||||
|
<String, String>{},
|
||||||
|
children: [
|
||||||
|
...identities.map((identity) => identity.toXMLNode()),
|
||||||
|
...features.map((feature) => XMLNode(
|
||||||
|
tag: 'feature',
|
||||||
|
attributes: { 'var': feature, },
|
||||||
|
),),
|
||||||
|
|
||||||
|
if (extendedInfo.isNotEmpty)
|
||||||
|
...extendedInfo.map((ei) => ei.toXml()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@immutable
|
||||||
class DiscoItem {
|
class DiscoItem {
|
||||||
|
|
||||||
const DiscoItem({ required this.jid, this.node, this.name });
|
const DiscoItem({ required this.jid, this.node, this.name });
|
||||||
final String jid;
|
final String jid;
|
||||||
final String? node;
|
final String? node;
|
||||||
final String? name;
|
final String? name;
|
||||||
|
|
||||||
|
XMLNode toXml() {
|
||||||
|
final attributes = {
|
||||||
|
'jid': jid,
|
||||||
|
};
|
||||||
|
if (node != null) {
|
||||||
|
attributes['node'] = node!;
|
||||||
|
}
|
||||||
|
if (name != null) {
|
||||||
|
attributes['name'] = name!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return XMLNode(
|
||||||
|
tag: 'node',
|
||||||
|
attributes: attributes,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.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';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
@@ -7,62 +8,71 @@ import 'package:moxxmpp/src/managers/data.dart';
|
|||||||
import 'package:moxxmpp/src/managers/handlers.dart';
|
import 'package:moxxmpp/src/managers/handlers.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/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/result.dart';
|
import 'package:moxxmpp/src/types/result.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0004.dart';
|
import 'package:moxxmpp/src/util/wait.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0030/cache.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';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0115.dart';
|
import 'package:moxxmpp/src/xeps/xep_0115.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
@immutable
|
/// Callback that is called when a disco#info requests is received on a given node.
|
||||||
class DiscoCacheKey {
|
typedef DiscoInfoRequestCallback = Future<DiscoInfo> Function();
|
||||||
|
|
||||||
const DiscoCacheKey(this.jid, this.node);
|
/// Callback that is called when a disco#items requests is received on a given node.
|
||||||
final String jid;
|
typedef DiscoItemsRequestCallback = Future<List<DiscoItem>> Function();
|
||||||
final String? node;
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool operator ==(Object other) {
|
|
||||||
return other is DiscoCacheKey && jid == other.jid && node == other.node;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
int get hashCode => jid.hashCode ^ node.hashCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/// This manager implements XEP-0030 by providing a way of performing disco#info and
|
||||||
|
/// disco#items requests and answering those requests.
|
||||||
|
/// A caching mechanism is also provided.
|
||||||
class DiscoManager extends XmppManagerBase {
|
class DiscoManager extends XmppManagerBase {
|
||||||
|
/// [identities] is a list of disco identities that should be added by default
|
||||||
DiscoManager()
|
/// to a disco#info response.
|
||||||
: _features = List.empty(growable: true),
|
DiscoManager(List<Identity> identities)
|
||||||
_capHashCache = {},
|
: _identities = List<Identity>.from(identities),
|
||||||
_capHashInfoCache = {},
|
super(discoManager);
|
||||||
_discoInfoCache = {},
|
|
||||||
_runningInfoQueries = {},
|
|
||||||
_cacheLock = Lock(),
|
|
||||||
super();
|
|
||||||
/// Our features
|
/// Our features
|
||||||
final List<String> _features;
|
final List<String> _features = List.empty(growable: true);
|
||||||
|
|
||||||
// Map full JID to Capability hashes
|
/// Disco identities that we advertise
|
||||||
final Map<String, CapabilityHashInfo> _capHashCache;
|
final List<Identity> _identities;
|
||||||
// Map capability hash to the disco info
|
|
||||||
final Map<String, DiscoInfo> _capHashInfoCache;
|
/// Map full JID to Capability hashes
|
||||||
// Map full JID to Disco Info
|
final Map<String, CapabilityHashInfo> _capHashCache = {};
|
||||||
final Map<DiscoCacheKey, DiscoInfo> _discoInfoCache;
|
|
||||||
// Mapping the full JID to a list of running requests
|
/// Map capability hash to the disco info
|
||||||
final Map<DiscoCacheKey, List<Completer<Result<DiscoError, DiscoInfo>>>> _runningInfoQueries;
|
final Map<String, DiscoInfo> _capHashInfoCache = {};
|
||||||
// Cache lock
|
|
||||||
final Lock _cacheLock;
|
/// Map full JID to Disco Info
|
||||||
|
final Map<DiscoCacheKey, DiscoInfo> _discoInfoCache = {};
|
||||||
|
|
||||||
|
/// The tracker for tracking disco#info queries that are in flight.
|
||||||
|
final WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> _discoInfoTracker = WaitForTracker();
|
||||||
|
|
||||||
|
/// The tracker for tracking disco#info queries that are in flight.
|
||||||
|
final WaitForTracker<DiscoCacheKey, Result<DiscoError, List<DiscoItem>>> _discoItemsTracker = WaitForTracker();
|
||||||
|
|
||||||
|
/// Cache lock
|
||||||
|
final Lock _cacheLock = Lock();
|
||||||
|
|
||||||
|
/// disco#info callbacks: node -> Callback
|
||||||
|
final Map<String, DiscoInfoRequestCallback> _discoInfoCallbacks = {};
|
||||||
|
|
||||||
|
/// disco#items callbacks: node -> Callback
|
||||||
|
final Map<String, DiscoItemsRequestCallback> _discoItemsCallbacks = {};
|
||||||
|
|
||||||
|
/// The list of identities that are registered.
|
||||||
|
List<Identity> get identities => _identities;
|
||||||
|
|
||||||
|
/// The list of disco features that are registered.
|
||||||
|
List<String> get features => _features;
|
||||||
|
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
bool hasInfoQueriesRunning() => _runningInfoQueries.isNotEmpty;
|
WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> get infoTracker => _discoInfoTracker;
|
||||||
|
|
||||||
@visibleForTesting
|
|
||||||
List<Completer<Result<DiscoError, DiscoInfo>>> getRunningInfoQueries(DiscoCacheKey key) => _runningInfoQueries[key]!;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
@@ -80,12 +90,6 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => discoManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'DiscoManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ discoInfoXmlns, discoItemsXmlns ];
|
List<String> getDiscoFeatures() => [ discoInfoXmlns, discoItemsXmlns ];
|
||||||
|
|
||||||
@@ -96,17 +100,41 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
Future<void> onXmppEvent(XmppEvent event) async {
|
Future<void> onXmppEvent(XmppEvent event) async {
|
||||||
if (event is PresenceReceivedEvent) {
|
if (event is PresenceReceivedEvent) {
|
||||||
await _onPresence(event.jid, event.presence);
|
await _onPresence(event.jid, event.presence);
|
||||||
} else if (event is StreamResumeFailedEvent) {
|
} else if (event is ConnectionStateChangedEvent) {
|
||||||
|
// TODO(Unknown): This handling is stupid. We should have an event that is
|
||||||
|
// triggered when we cannot guarantee that everything is as
|
||||||
|
// it was before.
|
||||||
|
if (event.state != XmppConnectionState.connected) return;
|
||||||
|
if (event.resumed) return;
|
||||||
|
|
||||||
|
// Cancel all waiting requests
|
||||||
|
await _discoInfoTracker.resolveAll(
|
||||||
|
Result<DiscoError, DiscoInfo>(UnknownDiscoError()),
|
||||||
|
);
|
||||||
|
await _discoItemsTracker.resolveAll(
|
||||||
|
Result<DiscoError, List<DiscoItem>>(UnknownDiscoError()),
|
||||||
|
);
|
||||||
|
|
||||||
await _cacheLock.synchronized(() async {
|
await _cacheLock.synchronized(() async {
|
||||||
// Clear the cache
|
// Clear the cache
|
||||||
_discoInfoCache.clear();
|
_discoInfoCache.clear();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register a callback [callback] for a disco#info query on [node].
|
||||||
|
void registerInfoCallback(String node, DiscoInfoRequestCallback callback) {
|
||||||
|
_discoInfoCallbacks[node] = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register a callback [callback] for a disco#items query on [node].
|
||||||
|
void registerItemsCallback(String node, DiscoItemsRequestCallback callback) {
|
||||||
|
_discoItemsCallbacks[node] = callback;
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds a list of features to the possible disco info response.
|
/// Adds a list of features to the possible disco info response.
|
||||||
/// This function only adds features that are not already present in the disco features.
|
/// This function only adds features that are not already present in the disco features.
|
||||||
void addDiscoFeatures(List<String> features) {
|
void addFeatures(List<String> features) {
|
||||||
for (final feat in features) {
|
for (final feat in features) {
|
||||||
if (!_features.contains(feat)) {
|
if (!_features.contains(feat)) {
|
||||||
_features.add(feat);
|
_features.add(feat);
|
||||||
@@ -114,6 +142,16 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a list of identities to the possible disco info response.
|
||||||
|
/// This function only adds features that are not already present in the disco features.
|
||||||
|
void addIdentities(List<Identity> identities) {
|
||||||
|
for (final identity in identities) {
|
||||||
|
if (!_identities.contains(identity)) {
|
||||||
|
_identities.add(identity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _onPresence(JID from, Stanza presence) async {
|
Future<void> _onPresence(JID from, Stanza presence) async {
|
||||||
final c = presence.firstTag('c', xmlns: capsXmlns);
|
final c = presence.firstTag('c', xmlns: capsXmlns);
|
||||||
if (c == null) return;
|
if (c == null) return;
|
||||||
@@ -144,77 +182,46 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the list of disco features registered.
|
/// Returns the [DiscoInfo] object that would be used as the response to a disco#info
|
||||||
List<String> getRegisteredDiscoFeatures() => _features;
|
/// query against our bare JID with no node. The results node attribute is set
|
||||||
|
/// to [node].
|
||||||
/// May be overriden. Specifies the identities which will be returned in a disco info response.
|
DiscoInfo getDiscoInfo(String? node) {
|
||||||
List<Identity> getIdentities() => const [ Identity(category: 'client', type: 'pc', name: 'moxxmpp', lang: 'en') ];
|
return DiscoInfo(
|
||||||
|
_features,
|
||||||
|
_identities,
|
||||||
|
const [],
|
||||||
|
node,
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<StanzaHandlerData> _onDiscoInfoRequest(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onDiscoInfoRequest(Stanza stanza, StanzaHandlerData state) async {
|
||||||
if (stanza.type != 'get') return state;
|
if (stanza.type != 'get') return state;
|
||||||
|
|
||||||
final presence = getAttributes().getManagerById(presenceManager)! as PresenceManager;
|
final query = stanza.firstTag('query', xmlns: discoInfoXmlns)!;
|
||||||
final query = stanza.firstTag('query')!;
|
|
||||||
final node = query.attributes['node'] as String?;
|
final node = query.attributes['node'] as String?;
|
||||||
final capHash = await presence.getCapabilityHash();
|
|
||||||
final isCapabilityNode = node == '${presence.capabilityHashNode}#$capHash';
|
|
||||||
|
|
||||||
if (!isCapabilityNode && node != null) {
|
if (_discoInfoCallbacks.containsKey(node)) {
|
||||||
await getAttributes().sendStanza(Stanza.iq(
|
// We can now assume that node != null
|
||||||
to: stanza.from,
|
final result = await _discoInfoCallbacks[node]!();
|
||||||
from: stanza.to,
|
await reply(
|
||||||
id: stanza.id,
|
state,
|
||||||
type: 'error',
|
'result',
|
||||||
children: [
|
[
|
||||||
XMLNode.xmlns(
|
result.toXml(),
|
||||||
tag: 'query',
|
],
|
||||||
// TODO(PapaTutuWawa): Why are we copying the xmlns?
|
);
|
||||||
xmlns: query.attributes['xmlns']! as String,
|
|
||||||
attributes: <String, String>{
|
|
||||||
'node': node
|
|
||||||
},
|
|
||||||
),
|
|
||||||
XMLNode(
|
|
||||||
tag: 'error',
|
|
||||||
attributes: <String, String>{
|
|
||||||
'type': 'cancel'
|
|
||||||
},
|
|
||||||
children: [
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'not-allowed',
|
|
||||||
xmlns: fullStanzaXmlns,
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
,);
|
|
||||||
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
await getAttributes().sendStanza(stanza.reply(
|
await reply(
|
||||||
children: [
|
state,
|
||||||
XMLNode.xmlns(
|
'result',
|
||||||
tag: 'query',
|
[
|
||||||
xmlns: discoInfoXmlns,
|
getDiscoInfo(node).toXml(),
|
||||||
attributes: {
|
],
|
||||||
...!isCapabilityNode ? {} : {
|
);
|
||||||
'node': '${presence.capabilityHashNode}#$capHash'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
children: [
|
|
||||||
...getIdentities().map((identity) => identity.toXMLNode()),
|
|
||||||
..._features.map((feat) {
|
|
||||||
return XMLNode(
|
|
||||||
tag: 'feature',
|
|
||||||
attributes: <String, dynamic>{ 'var': feat },
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),);
|
|
||||||
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
}
|
}
|
||||||
@@ -222,96 +229,63 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
Future<StanzaHandlerData> _onDiscoItemsRequest(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onDiscoItemsRequest(Stanza stanza, StanzaHandlerData state) async {
|
||||||
if (stanza.type != 'get') return state;
|
if (stanza.type != 'get') return state;
|
||||||
|
|
||||||
final query = stanza.firstTag('query')!;
|
final query = stanza.firstTag('query', xmlns: discoItemsXmlns)!;
|
||||||
if (query.attributes['node'] != null) {
|
final node = query.attributes['node'] as String?;
|
||||||
// TODO(Unknown): Handle the node we specified for XEP-0115
|
if (_discoItemsCallbacks.containsKey(node)) {
|
||||||
await getAttributes().sendStanza(
|
final result = await _discoItemsCallbacks[node]!();
|
||||||
Stanza.iq(
|
await reply(
|
||||||
to: stanza.from,
|
state,
|
||||||
from: stanza.to,
|
'result',
|
||||||
id: stanza.id,
|
[
|
||||||
type: 'error',
|
|
||||||
children: [
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'query',
|
|
||||||
// TODO(PapaTutuWawa): Why copy the xmlns?
|
|
||||||
xmlns: query.attributes['xmlns']! as String,
|
|
||||||
attributes: <String, String>{
|
|
||||||
'node': query.attributes['node']! as String,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
XMLNode(
|
|
||||||
tag: 'error',
|
|
||||||
attributes: <String, dynamic>{
|
|
||||||
'type': 'cancel'
|
|
||||||
},
|
|
||||||
children: [
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'not-allowed',
|
|
||||||
xmlns: fullStanzaXmlns,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return state.copyWith(done: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
await getAttributes().sendStanza(
|
|
||||||
stanza.reply(
|
|
||||||
children: [
|
|
||||||
XMLNode.xmlns(
|
XMLNode.xmlns(
|
||||||
tag: 'query',
|
tag: 'query',
|
||||||
xmlns: discoItemsXmlns,
|
xmlns: discoItemsXmlns,
|
||||||
|
attributes: <String, String>{
|
||||||
|
'node': node!,
|
||||||
|
},
|
||||||
|
children: result.map((item) => item.toXml()).toList(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
);
|
||||||
);
|
|
||||||
return state.copyWith(done: true);
|
return state.copyWith(done: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _exitDiscoInfoCriticalSection(DiscoCacheKey key, Result<DiscoError, DiscoInfo> result) async {
|
Future<void> _exitDiscoInfoCriticalSection(DiscoCacheKey key, Result<DiscoError, DiscoInfo> result) async {
|
||||||
return _cacheLock.synchronized(() async {
|
await _cacheLock.synchronized(() async {
|
||||||
// Complete all futures
|
|
||||||
for (final completer in _runningInfoQueries[key]!) {
|
|
||||||
completer.complete(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to cache if it is a result
|
// Add to cache if it is a result
|
||||||
if (result.isType<DiscoInfo>()) {
|
if (result.isType<DiscoInfo>()) {
|
||||||
_discoInfoCache[key] = result.get<DiscoInfo>();
|
_discoInfoCache[key] = result.get<DiscoInfo>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove from the request cache
|
|
||||||
_runningInfoQueries.remove(key);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await _discoInfoTracker.resolve(key, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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, bool shouldEncrypt = true }) 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;
|
final ffuture = await _cacheLock.synchronized<Future<Future<Result<DiscoError, DiscoInfo>>?>?>(() async {
|
||||||
await _cacheLock.synchronized(() async {
|
|
||||||
// Check if we already know what the JID supports
|
// Check if we already know what the JID supports
|
||||||
if (_discoInfoCache.containsKey(cacheKey)) {
|
if (_discoInfoCache.containsKey(cacheKey)) {
|
||||||
info = _discoInfoCache[cacheKey];
|
info = _discoInfoCache[cacheKey];
|
||||||
|
return null;
|
||||||
} else {
|
} else {
|
||||||
// Is a request running?
|
return _discoInfoTracker.waitFor(cacheKey);
|
||||||
if (_runningInfoQueries.containsKey(cacheKey)) {
|
|
||||||
completer = Completer();
|
|
||||||
_runningInfoQueries[cacheKey]!.add(completer!);
|
|
||||||
} else {
|
|
||||||
_runningInfoQueries[cacheKey] = List.from(<Completer<DiscoInfo?>>[]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (info != null) {
|
if (info != null) {
|
||||||
return Result<DiscoError, DiscoInfo>(info);
|
return Result<DiscoError, DiscoInfo>(info);
|
||||||
} else if (completer != null) {
|
} else {
|
||||||
return completer!.future;
|
final future = await ffuture;
|
||||||
|
if (future != null) {
|
||||||
|
return future;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final stanza = await getAttributes().sendStanza(
|
final stanza = await getAttributes().sendStanza(
|
||||||
@@ -325,34 +299,17 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
final error = stanza.firstTag('error');
|
if (stanza.attributes['type'] == 'error') {
|
||||||
if (error != null && stanza.attributes['type'] == 'error') {
|
//final error = stanza.firstTag('error');
|
||||||
final result = Result<DiscoError, DiscoInfo>(ErrorResponseDiscoError());
|
final result = Result<DiscoError, DiscoInfo>(ErrorResponseDiscoError());
|
||||||
await _exitDiscoInfoCriticalSection(cacheKey, result);
|
await _exitDiscoInfoCriticalSection(cacheKey, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
final features = List<String>.empty(growable: true);
|
|
||||||
final identities = List<Identity>.empty(growable: true);
|
|
||||||
|
|
||||||
for (final element in query.children) {
|
|
||||||
if (element.tag == 'feature') {
|
|
||||||
features.add(element.attributes['var']! as String);
|
|
||||||
} else if (element.tag == 'identity') {
|
|
||||||
identities.add(Identity(
|
|
||||||
category: element.attributes['category']! as String,
|
|
||||||
type: element.attributes['type']! as String,
|
|
||||||
name: element.attributes['name'] as String?,
|
|
||||||
),);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final result = Result<DiscoError, DiscoInfo>(
|
final result = Result<DiscoError, DiscoInfo>(
|
||||||
DiscoInfo(
|
DiscoInfo.fromQuery(
|
||||||
features,
|
query,
|
||||||
identities,
|
JID.fromString(entity),
|
||||||
query.findTags('x', xmlns: dataFormsXmlns).map(parseDataForm).toList(),
|
|
||||||
JID.fromString(stanza.attributes['from']! as String),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
await _exitDiscoInfoCriticalSection(cacheKey, result);
|
await _exitDiscoInfoCriticalSection(cacheKey, result);
|
||||||
@@ -361,6 +318,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, bool shouldEncrypt = true }) async {
|
Future<Result<DiscoError, List<DiscoItem>>> discoItemsQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
|
||||||
|
final key = DiscoCacheKey(entity, node);
|
||||||
|
final future = await _discoItemsTracker.waitFor(key);
|
||||||
|
if (future != null) {
|
||||||
|
return future;
|
||||||
|
}
|
||||||
|
|
||||||
final stanza = await getAttributes()
|
final stanza = await getAttributes()
|
||||||
.sendStanza(
|
.sendStanza(
|
||||||
buildDiscoItemsQueryStanza(entity, node: node),
|
buildDiscoItemsQueryStanza(entity, node: node),
|
||||||
@@ -368,12 +331,18 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
) as Stanza;
|
) as Stanza;
|
||||||
|
|
||||||
final query = stanza.firstTag('query');
|
final query = stanza.firstTag('query');
|
||||||
if (query == null) return Result(InvalidResponseDiscoError());
|
if (query == null) {
|
||||||
|
final result = Result<DiscoError, List<DiscoItem>>(InvalidResponseDiscoError());
|
||||||
|
await _discoItemsTracker.resolve(key, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
final error = stanza.firstTag('error');
|
if (stanza.type == 'error') {
|
||||||
if (error != null && stanza.type == 'error') {
|
//final error = stanza.firstTag('error');
|
||||||
//print("Disco Items error: " + error.toXml());
|
//print("Disco Items error: " + error.toXml());
|
||||||
return Result(ErrorResponseDiscoError());
|
final result = Result<DiscoError, List<DiscoItem>>(ErrorResponseDiscoError());
|
||||||
|
await _discoItemsTracker.resolve(key, result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
final items = query.findTags('item').map((node) => DiscoItem(
|
final items = query.findTags('item').map((node) => DiscoItem(
|
||||||
@@ -382,7 +351,9 @@ class DiscoManager extends XmppManagerBase {
|
|||||||
name: node.attributes['name'] as String?,
|
name: node.attributes['name'] as String?,
|
||||||
),).toList();
|
),).toList();
|
||||||
|
|
||||||
return Result(items);
|
final result = Result<DiscoError, List<DiscoItem>>(items);
|
||||||
|
await _discoItemsTracker.resolve(key, result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Queries information about a jid based on its node and capability hash.
|
/// Queries information about a jid based on its node and capability hash.
|
||||||
|
|||||||
@@ -28,15 +28,9 @@ class VCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class VCardManager extends XmppManagerBase {
|
class VCardManager extends XmppManagerBase {
|
||||||
VCardManager() : _lastHash = {}, super();
|
VCardManager() : super(vcardManager);
|
||||||
final Map<String, String> _lastHash;
|
final Map<String, String> _lastHash = {};
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => vcardManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'vCardManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
|
|||||||
@@ -69,11 +69,7 @@ class PubSubItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PubSubManager extends XmppManagerBase {
|
class PubSubManager extends XmppManagerBase {
|
||||||
@override
|
PubSubManager() : super(pubsubManager);
|
||||||
String getId() => pubsubManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'PubsubManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import 'package:moxxmpp/src/stringxml.dart';
|
|||||||
|
|
||||||
/// A data class representing the jabber:x:oob tag.
|
/// A data class representing the jabber:x:oob tag.
|
||||||
class OOBData {
|
class OOBData {
|
||||||
|
|
||||||
const OOBData({ this.url, this.desc });
|
const OOBData({ this.url, this.desc });
|
||||||
final String? url;
|
final String? url;
|
||||||
final String? desc;
|
final String? desc;
|
||||||
@@ -32,11 +31,7 @@ XMLNode constructOOBNode(OOBData data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class OOBManager extends XmppManagerBase {
|
class OOBManager extends XmppManagerBase {
|
||||||
@override
|
OOBManager() : super(oobManager);
|
||||||
String getName() => 'OOBName';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => oobManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ oobDataXmlns ];
|
List<String> getDiscoFeatures() => [ oobDataXmlns ];
|
||||||
|
|||||||
@@ -28,24 +28,24 @@ class UserAvatarMetadata {
|
|||||||
this.height,
|
this.height,
|
||||||
this.mime,
|
this.mime,
|
||||||
);
|
);
|
||||||
|
|
||||||
/// The amount of bytes in the file
|
/// The amount of bytes in the file
|
||||||
final int length;
|
final int length;
|
||||||
|
|
||||||
/// The identifier of the avatar
|
/// The identifier of the avatar
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
/// Image proportions
|
/// Image proportions
|
||||||
final int width;
|
final int width;
|
||||||
final int height;
|
final int height;
|
||||||
|
|
||||||
/// The MIME type of the avatar
|
/// The MIME type of the avatar
|
||||||
final String mime;
|
final String mime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// NOTE: This class requires a PubSubManager
|
/// NOTE: This class requires a PubSubManager
|
||||||
class UserAvatarManager extends XmppManagerBase {
|
class UserAvatarManager extends XmppManagerBase {
|
||||||
@override
|
UserAvatarManager() : super(userAvatarManager);
|
||||||
String getId() => userAvatarManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'UserAvatarManager';
|
|
||||||
|
|
||||||
PubSubManager _getPubSubManager() => getAttributes().getManagerById(pubsubManager)! as PubSubManager;
|
PubSubManager _getPubSubManager() => getAttributes().getManagerById(pubsubManager)! as PubSubManager;
|
||||||
|
|
||||||
|
|||||||
@@ -39,15 +39,11 @@ ChatState chatStateFromString(String raw) {
|
|||||||
String chatStateToString(ChatState state) => state.toString().split('.').last;
|
String chatStateToString(ChatState state) => state.toString().split('.').last;
|
||||||
|
|
||||||
class ChatStateManager extends XmppManagerBase {
|
class ChatStateManager extends XmppManagerBase {
|
||||||
|
ChatStateManager() : super(chatStateManager);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ chatStateXmlns ];
|
List<String> getDiscoFeatures() => [ chatStateXmlns ];
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'ChatStateManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => chatStateManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:cryptography/cryptography.dart';
|
import 'package:cryptography/cryptography.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/namespaces.dart';
|
||||||
|
import 'package:moxxmpp/src/presence.dart';
|
||||||
import 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
import 'package:moxxmpp/src/rfcs/rfc_4790.dart';
|
||||||
|
import 'package:moxxmpp/src/stringxml.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_0414.dart';
|
||||||
|
|
||||||
|
@immutable
|
||||||
class CapabilityHashInfo {
|
class CapabilityHashInfo {
|
||||||
|
|
||||||
const CapabilityHashInfo(this.ver, this.node, this.hash);
|
const CapabilityHashInfo(this.ver, this.node, this.hash);
|
||||||
final String ver;
|
final String ver;
|
||||||
final String node;
|
final String node;
|
||||||
@@ -57,3 +65,78 @@ Future<String> calculateCapabilityHash(DiscoInfo info, HashAlgorithm algorithm)
|
|||||||
|
|
||||||
return base64.encode((await algorithm.hash(utf8.encode(buffer.toString()))).bytes);
|
return base64.encode((await algorithm.hash(utf8.encode(buffer.toString()))).bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A manager implementing the advertising of XEP-0115. It responds to the
|
||||||
|
/// disco#info requests on the specified node with the information provided by
|
||||||
|
/// the DiscoManager.
|
||||||
|
/// NOTE: This manager requires that the DiscoManager is also registered.
|
||||||
|
class EntityCapabilitiesManager extends XmppManagerBase {
|
||||||
|
EntityCapabilitiesManager(this._capabilityHashBase) : super(entityCapabilitiesManager);
|
||||||
|
|
||||||
|
/// The string that is both the node under which we advertise the disco info
|
||||||
|
/// and the base for the actual node on which we respond to disco#info requests.
|
||||||
|
final String _capabilityHashBase;
|
||||||
|
|
||||||
|
/// The cached capability hash.
|
||||||
|
String? _capabilityHash;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<String> getDiscoFeatures() => [ capsXmlns ];
|
||||||
|
|
||||||
|
/// Computes, if required, the capability hash of the data provided by
|
||||||
|
/// the DiscoManager.
|
||||||
|
Future<String> getCapabilityHash() async {
|
||||||
|
_capabilityHash ??= await calculateCapabilityHash(
|
||||||
|
getAttributes()
|
||||||
|
.getManagerById<DiscoManager>(discoManager)!
|
||||||
|
.getDiscoInfo(null),
|
||||||
|
getHashByName('sha-1')!,
|
||||||
|
);
|
||||||
|
|
||||||
|
return _capabilityHash!;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> _getNode() async {
|
||||||
|
final hash = await getCapabilityHash();
|
||||||
|
return '$_capabilityHashBase#$hash';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<DiscoInfo> _onInfoQuery() async {
|
||||||
|
return getAttributes()
|
||||||
|
.getManagerById<DiscoManager>(discoManager)!
|
||||||
|
.getDiscoInfo(await _getNode());
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<XMLNode>> _prePresenceSent() async {
|
||||||
|
return [
|
||||||
|
XMLNode.xmlns(
|
||||||
|
tag: 'c',
|
||||||
|
xmlns: capsXmlns,
|
||||||
|
attributes: {
|
||||||
|
'hash': 'sha-1',
|
||||||
|
'node': _capabilityHashBase,
|
||||||
|
'ver': await getCapabilityHash(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> postRegisterCallback() async {
|
||||||
|
await super.postRegisterCallback();
|
||||||
|
|
||||||
|
getAttributes().getManagerById<DiscoManager>(discoManager)!.registerInfoCallback(
|
||||||
|
await _getNode(),
|
||||||
|
_onInfoQuery,
|
||||||
|
);
|
||||||
|
|
||||||
|
getAttributes()
|
||||||
|
.getManagerById<PresenceManager>(presenceManager)!
|
||||||
|
.registerPreSendCallback(
|
||||||
|
_prePresenceSent,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,15 +24,11 @@ XMLNode makeMessageDeliveryResponse(String id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MessageDeliveryReceiptManager extends XmppManagerBase {
|
class MessageDeliveryReceiptManager extends XmppManagerBase {
|
||||||
|
MessageDeliveryReceiptManager() : super(messageDeliveryReceiptManager);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ deliveryXmlns ];
|
List<String> getDiscoFeatures() => [ deliveryXmlns ];
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'MessageDeliveryReceiptManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => messageDeliveryReceiptManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
|
|||||||
@@ -9,16 +9,10 @@ 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';
|
||||||
|
|
||||||
class BlockingManager extends XmppManagerBase {
|
class BlockingManager extends XmppManagerBase {
|
||||||
BlockingManager() : _supported = false, _gotSupported = false, super();
|
BlockingManager() : super(blockingManager);
|
||||||
|
|
||||||
bool _supported;
|
bool _supported = false;
|
||||||
bool _gotSupported;
|
bool _gotSupported = false;
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => blockingManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'BlockingManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
|||||||
@@ -21,40 +21,41 @@ const xmlUintMax = 4294967296; // 2**32
|
|||||||
typedef StanzaAckedCallback = bool Function(Stanza stanza);
|
typedef StanzaAckedCallback = bool Function(Stanza stanza);
|
||||||
|
|
||||||
class StreamManagementManager extends XmppManagerBase {
|
class StreamManagementManager extends XmppManagerBase {
|
||||||
|
|
||||||
StreamManagementManager({
|
StreamManagementManager({
|
||||||
this.ackTimeout = const Duration(seconds: 30),
|
this.ackTimeout = const Duration(seconds: 30),
|
||||||
})
|
}) : super(smManager);
|
||||||
: _state = StreamManagementState(0, 0),
|
|
||||||
_unackedStanzas = {},
|
|
||||||
_stateLock = Lock(),
|
|
||||||
_streamManagementEnabled = false,
|
|
||||||
_lastAckTimestamp = -1,
|
|
||||||
_pendingAcks = 0,
|
|
||||||
_streamResumed = false,
|
|
||||||
_ackLock = Lock();
|
|
||||||
/// The queue of stanzas that are not (yet) acked
|
/// The queue of stanzas that are not (yet) acked
|
||||||
final Map<int, Stanza> _unackedStanzas;
|
final Map<int, Stanza> _unackedStanzas = {};
|
||||||
|
|
||||||
/// Commitable state of the StreamManagementManager
|
/// Commitable state of the StreamManagementManager
|
||||||
StreamManagementState _state;
|
StreamManagementState _state = StreamManagementState(0, 0);
|
||||||
|
|
||||||
/// Mutex lock for _state
|
/// Mutex lock for _state
|
||||||
final Lock _stateLock;
|
final Lock _stateLock = Lock();
|
||||||
|
|
||||||
/// If the have enabled SM on the stream yet
|
/// If the have enabled SM on the stream yet
|
||||||
bool _streamManagementEnabled;
|
bool _streamManagementEnabled = false;
|
||||||
|
|
||||||
/// If the current stream has been resumed;
|
/// If the current stream has been resumed;
|
||||||
bool _streamResumed;
|
bool _streamResumed = false;
|
||||||
|
|
||||||
/// The time in which the response to an ack is still valid. Counts as a timeout
|
/// The time in which the response to an ack is still valid. Counts as a timeout
|
||||||
/// otherwise
|
/// otherwise
|
||||||
@internal
|
@internal
|
||||||
final Duration ackTimeout;
|
final Duration ackTimeout;
|
||||||
|
|
||||||
/// The time at which the last ack has been sent
|
/// The time at which the last ack has been sent
|
||||||
int _lastAckTimestamp;
|
int _lastAckTimestamp = -1;
|
||||||
|
|
||||||
/// The timer to see if we timed the connection out
|
/// The timer to see if we timed the connection out
|
||||||
Timer? _ackTimer;
|
Timer? _ackTimer;
|
||||||
|
|
||||||
/// Counts how many acks we're waiting for
|
/// Counts how many acks we're waiting for
|
||||||
int _pendingAcks;
|
int _pendingAcks = 0;
|
||||||
|
|
||||||
/// Lock for both [_lastAckTimestamp] and [_pendingAcks].
|
/// Lock for both [_lastAckTimestamp] and [_pendingAcks].
|
||||||
final Lock _ackLock;
|
final Lock _ackLock = Lock();
|
||||||
|
|
||||||
/// Functions for testing
|
/// Functions for testing
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
@@ -120,12 +121,6 @@ class StreamManagementManager extends XmppManagerBase {
|
|||||||
StreamManagementState get state => _state;
|
StreamManagementState get state => _state;
|
||||||
|
|
||||||
bool get streamResumed => _streamResumed;
|
bool get streamResumed => _streamResumed;
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => smManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'StreamManagementManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<NonzaHandler> getNonzaHandlers() => [
|
List<NonzaHandler> getNonzaHandlers() => [
|
||||||
@@ -142,7 +137,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 +180,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,7 @@ class DelayedDelivery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DelayedDeliveryManager extends XmppManagerBase {
|
class DelayedDeliveryManager extends XmppManagerBase {
|
||||||
@override
|
DelayedDeliveryManager() : super(delayedDeliveryManager);
|
||||||
String getId() => delayedDeliveryManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'DelayedDeliveryManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|||||||
@@ -12,26 +12,26 @@ 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() : super(carbonsManager);
|
||||||
|
|
||||||
|
/// Indicates that message carbons are enabled.
|
||||||
bool _isEnabled = false;
|
bool _isEnabled = false;
|
||||||
|
|
||||||
|
/// Indicates that the server supports message carbons.
|
||||||
bool _supported = false;
|
bool _supported = false;
|
||||||
|
|
||||||
|
/// Indicates that we know that [CarbonsManager._supported] is accurate.
|
||||||
bool _gotSupported = false;
|
bool _gotSupported = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => carbonsManager;
|
List<StanzaHandler> getIncomingPreStanzaHandlers() => [
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'CarbonsManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
|
||||||
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(
|
||||||
@@ -39,7 +39,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,
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
@@ -103,10 +102,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(
|
||||||
@@ -131,6 +135,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(
|
||||||
@@ -165,8 +172,15 @@ class CarbonsManager extends XmppManagerBase {
|
|||||||
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,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,11 +61,7 @@ HashFunction hashFunctionFromName(String name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CryptographicHashManager extends XmppManagerBase {
|
class CryptographicHashManager extends XmppManagerBase {
|
||||||
@override
|
CryptographicHashManager() : super(cryptographicHashManager);
|
||||||
String getId() => cryptographicHashManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'CryptographicHashManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
@@ -81,7 +77,7 @@ class CryptographicHashManager extends XmppManagerBase {
|
|||||||
];
|
];
|
||||||
|
|
||||||
static Future<List<int>> hashFromData(List<int> data, HashFunction function) async {
|
static Future<List<int>> hashFromData(List<int> data, HashFunction function) async {
|
||||||
// TODO(PapaTutuWawa): Implemen the others as well
|
// TODO(PapaTutuWawa): Implement the others as well
|
||||||
HashAlgorithm algo;
|
HashAlgorithm algo;
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case HashFunction.sha256:
|
case HashFunction.sha256:
|
||||||
|
|||||||
@@ -17,11 +17,7 @@ XMLNode makeLastMessageCorrectionEdit(String id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class LastMessageCorrectionManager extends XmppManagerBase {
|
class LastMessageCorrectionManager extends XmppManagerBase {
|
||||||
@override
|
LastMessageCorrectionManager() : super(lastMessageCorrectionManager);
|
||||||
String getName() => 'LastMessageCorrectionManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => lastMessageCorrectionManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ lmcXmlns ];
|
List<String> getDiscoFeatures() => [ lmcXmlns ];
|
||||||
|
|||||||
@@ -25,11 +25,7 @@ XMLNode makeChatMarker(String tag, String id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ChatMarkerManager extends XmppManagerBase {
|
class ChatMarkerManager extends XmppManagerBase {
|
||||||
@override
|
ChatMarkerManager() : super(chatMarkerManager);
|
||||||
String getName() => 'ChatMarkerManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => chatMarkerManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ chatMarkersXmlns ];
|
List<String> getDiscoFeatures() => [ chatMarkersXmlns ];
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ class CSIInactiveNonza extends XMLNode {
|
|||||||
|
|
||||||
/// A Stub negotiator that is just for "intercepting" the stream feature.
|
/// A Stub negotiator that is just for "intercepting" the stream feature.
|
||||||
class CSINegotiator extends XmppFeatureNegotiatorBase {
|
class CSINegotiator extends XmppFeatureNegotiatorBase {
|
||||||
CSINegotiator() : _supported = false, super(11, false, csiXmlns, csiNegotiator);
|
CSINegotiator() : super(11, false, csiXmlns, csiNegotiator);
|
||||||
|
|
||||||
/// True if CSI is supported. False otherwise.
|
/// True if CSI is supported. False otherwise.
|
||||||
bool _supported;
|
bool _supported = false;
|
||||||
bool get isSupported => _supported;
|
bool get isSupported => _supported;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -50,15 +50,9 @@ class CSINegotiator extends XmppFeatureNegotiatorBase {
|
|||||||
|
|
||||||
/// The manager requires a CSINegotiator to be registered as a feature negotiator.
|
/// The manager requires a CSINegotiator to be registered as a feature negotiator.
|
||||||
class CSIManager extends XmppManagerBase {
|
class CSIManager extends XmppManagerBase {
|
||||||
|
CSIManager() : super(csiManager);
|
||||||
|
|
||||||
CSIManager() : _isActive = true, super();
|
bool _isActive = true;
|
||||||
bool _isActive;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => csiManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'CSIManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async {
|
Future<bool> isSupported() async {
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
|
|||||||
/// NOTE: [StableStanzaId.stanzaId] must not be confused with the actual id attribute of
|
/// NOTE: [StableStanzaId.stanzaId] must not be confused with the actual id attribute of
|
||||||
/// the message stanza.
|
/// the message stanza.
|
||||||
class StableStanzaId {
|
class StableStanzaId {
|
||||||
|
|
||||||
const StableStanzaId({ this.originId, this.stanzaId, this.stanzaIdBy });
|
const StableStanzaId({ this.originId, this.stanzaId, this.stanzaIdBy });
|
||||||
final String? originId;
|
final String? originId;
|
||||||
final String? stanzaId;
|
final String? stanzaId;
|
||||||
@@ -29,11 +28,7 @@ XMLNode makeOriginIdElement(String id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class StableIdManager extends XmppManagerBase {
|
class StableIdManager extends XmppManagerBase {
|
||||||
@override
|
StableIdManager() : super(stableIdManager);
|
||||||
String getName() => 'StableIdManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => stableIdManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ stableIdXmlns ];
|
List<String> getDiscoFeatures() => [ stableIdXmlns ];
|
||||||
|
|||||||
@@ -41,17 +41,19 @@ Map<String, String> prepareHeaders(Map<String, String> headers) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HttpFileUploadManager extends XmppManagerBase {
|
class HttpFileUploadManager extends XmppManagerBase {
|
||||||
HttpFileUploadManager() : _gotSupported = false, _supported = false, super();
|
HttpFileUploadManager() : super(httpFileUploadManager);
|
||||||
|
|
||||||
|
/// The entity that we will request file uploads from, if discovered.
|
||||||
JID? _entityJid;
|
JID? _entityJid;
|
||||||
|
|
||||||
|
/// The maximum file upload file size, if advertised and discovered.
|
||||||
int? _maxUploadSize;
|
int? _maxUploadSize;
|
||||||
bool _gotSupported;
|
|
||||||
bool _supported;
|
|
||||||
|
|
||||||
@override
|
/// Flag, if we every tried to discover the upload entity.
|
||||||
String getId() => httpFileUploadManager;
|
bool _gotSupported = false;
|
||||||
|
|
||||||
@override
|
/// Flag, if we can use HTTP File Upload
|
||||||
String getName() => 'HttpFileUploadManager';
|
bool _supported = false;
|
||||||
|
|
||||||
/// Returns whether the entity provided an identity that tells us that we can ask it
|
/// Returns whether the entity provided an identity that tells us that we can ask it
|
||||||
/// for an HTTP upload slot.
|
/// for an HTTP upload slot.
|
||||||
|
|||||||
@@ -53,20 +53,12 @@ XMLNode buildEmeElement(ExplicitEncryptionType type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class EmeManager extends XmppManagerBase {
|
class EmeManager extends XmppManagerBase {
|
||||||
|
EmeManager() : super(emeManager);
|
||||||
EmeManager() : super();
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => emeManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'EmeManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [emeXmlns];
|
List<String> getDiscoFeatures() => [ emeXmlns ];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
|||||||
@@ -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 {}
|
||||||
|
|||||||
@@ -24,6 +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:xml/xml.dart';
|
||||||
|
|
||||||
const _doNotEncryptList = [
|
const _doNotEncryptList = [
|
||||||
// XEP-0033
|
// XEP-0033
|
||||||
@@ -41,39 +42,33 @@ const _doNotEncryptList = [
|
|||||||
DoNotEncrypt('stanza-id', stableIdXmlns),
|
DoNotEncrypt('stanza-id', stableIdXmlns),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@mustCallSuper
|
||||||
abstract class BaseOmemoManager extends XmppManagerBase {
|
abstract class BaseOmemoManager extends XmppManagerBase {
|
||||||
@override
|
BaseOmemoManager() : super(omemoManager);
|
||||||
String getId() => omemoManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'OmemoManager';
|
|
||||||
|
|
||||||
// TODO(Unknown): Technically, this is not always true
|
// TODO(Unknown): Technically, this is not always true
|
||||||
@override
|
@override
|
||||||
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,
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -320,11 +315,12 @@ abstract class BaseOmemoManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final toJid = JID.fromString(stanza.to!).toBare();
|
final toJid = JID.fromString(stanza.to!).toBare();
|
||||||
if (!(await shouldEncryptStanza(toJid, stanza))) {
|
final shouldEncryptResult = await shouldEncryptStanza(toJid, stanza);
|
||||||
logger.finest('shouldEncryptStanza returned false for message to $toJid. Not encrypting.');
|
if (!shouldEncryptResult && !state.forceEncryption) {
|
||||||
|
logger.finest('Not encrypting stanza for $toJid: Both shouldEncryptStanza and forceEncryption are false.');
|
||||||
return state;
|
return state;
|
||||||
} else {
|
} else {
|
||||||
logger.finest('shouldEncryptStanza returned true for message to $toJid.');
|
logger.finest('Encrypting stanza for $toJid: shouldEncryptResult=$shouldEncryptResult, forceEncryption=${state.forceEncryption}');
|
||||||
}
|
}
|
||||||
|
|
||||||
final toEncrypt = List<XMLNode>.empty(growable: true);
|
final toEncrypt = List<XMLNode>.empty(growable: true);
|
||||||
@@ -359,6 +355,11 @@ abstract class BaseOmemoManager extends XmppManagerBase {
|
|||||||
other['encryption_error_devices'] = result.deviceEncryptionErrors;
|
other['encryption_error_devices'] = result.deviceEncryptionErrors;
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
other: other,
|
other: other,
|
||||||
|
// If we have no device list for toJid, then the contact most likely does not
|
||||||
|
// support OMEMO:2
|
||||||
|
cancelReason: result.jidEncryptionErrors[toJid.toString()] is NoKeyMaterialAvailableException ?
|
||||||
|
OmemoNotSupportedForContactException() :
|
||||||
|
UnknownOmemoError(),
|
||||||
cancel: true,
|
cancel: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -432,19 +433,38 @@ abstract class BaseOmemoManager extends XmppManagerBase {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final children = stanza.children.where(
|
|
||||||
(child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
|
|
||||||
).toList();
|
|
||||||
final other = Map<String, dynamic>.from(state.other);
|
final other = Map<String, dynamic>.from(state.other);
|
||||||
|
var children = stanza.children;
|
||||||
if (result.error != null) {
|
if (result.error != null) {
|
||||||
other['encryption_error'] = result.error;
|
other['encryption_error'] = result.error;
|
||||||
|
} else {
|
||||||
|
children = stanza.children.where(
|
||||||
|
(child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
|
||||||
|
).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.payload != null) {
|
if (result.payload != null) {
|
||||||
final envelope = XMLNode.fromString(result.payload!);
|
XMLNode envelope;
|
||||||
children.addAll(
|
try {
|
||||||
envelope.firstTag('content')!.children,
|
envelope = XMLNode.fromString(result.payload!);
|
||||||
);
|
} on XmlParserException catch (_) {
|
||||||
|
logger.warning('Failed to parse envelope payload: ${result.payload!}');
|
||||||
|
other['encryption_error'] = InvalidEnvelopePayloadException();
|
||||||
|
return state.copyWith(
|
||||||
|
encrypted: true,
|
||||||
|
other: other,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final envelopeChildren = envelope.firstTag('content')?.children;
|
||||||
|
if (envelopeChildren != null) {
|
||||||
|
children.addAll(
|
||||||
|
// Do not add forbidden elements from the envelope
|
||||||
|
envelopeChildren.where(shouldEncryptElement),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logger.warning('Invalid envelope element: No <content /> element');
|
||||||
|
}
|
||||||
|
|
||||||
if (!checkAffixElements(envelope, stanza.from!, ourJid)) {
|
if (!checkAffixElements(envelope, stanza.from!, ourJid)) {
|
||||||
other['encryption_error'] = InvalidAffixElementsException();
|
other['encryption_error'] = InvalidAffixElementsException();
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import 'package:moxxmpp/src/stringxml.dart';
|
|||||||
import 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
import 'package:moxxmpp/src/xeps/staging/extensible_file_thumbnails.dart';
|
||||||
|
|
||||||
class StatelessMediaSharingData {
|
class StatelessMediaSharingData {
|
||||||
|
|
||||||
const StatelessMediaSharingData({ required this.mediaType, required this.size, required this.description, required this.hashes, required this.url, required this.thumbnails });
|
const StatelessMediaSharingData({ required this.mediaType, required this.size, required this.description, required this.hashes, required this.url, required this.thumbnails });
|
||||||
final String mediaType;
|
final String mediaType;
|
||||||
final int size;
|
final int size;
|
||||||
@@ -63,11 +62,7 @@ StatelessMediaSharingData parseSIMSElement(XMLNode node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SIMSManager extends XmppManagerBase {
|
class SIMSManager extends XmppManagerBase {
|
||||||
@override
|
SIMSManager() : super(simsManager);
|
||||||
String getName() => 'SIMSManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => simsManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ simsXmlns ];
|
List<String> getDiscoFeatures() => [ simsXmlns ];
|
||||||
|
|||||||
@@ -12,11 +12,7 @@ class MessageRetractionData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MessageRetractionManager extends XmppManagerBase {
|
class MessageRetractionManager extends XmppManagerBase {
|
||||||
@override
|
MessageRetractionManager() : super(messageRetractionManager);
|
||||||
String getName() => 'MessageRetractionManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => messageRetractionManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ messageRetractionXmlns ];
|
List<String> getDiscoFeatures() => [ messageRetractionXmlns ];
|
||||||
|
|||||||
@@ -29,15 +29,11 @@ class MessageReactions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MessageReactionsManager extends XmppManagerBase {
|
class MessageReactionsManager extends XmppManagerBase {
|
||||||
|
MessageReactionsManager() : super(messageReactionsManager);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<String> getDiscoFeatures() => [ messageReactionsXmlns ];
|
List<String> getDiscoFeatures() => [ messageReactionsXmlns ];
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'MessageReactionsManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => messageReactionsManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
|
|||||||
@@ -105,11 +105,7 @@ class StatelessFileSharingData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SFSManager extends XmppManagerBase {
|
class SFSManager extends XmppManagerBase {
|
||||||
@override
|
SFSManager() : super(sfsManager);
|
||||||
String getName() => 'SFSManager';
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getId() => sfsManager;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
|
|||||||
@@ -225,11 +225,7 @@ class StickerPack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class StickersManager extends XmppManagerBase {
|
class StickersManager extends XmppManagerBase {
|
||||||
@override
|
StickersManager() : super(stickersManager);
|
||||||
String getId() => stickersManager;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String getName() => 'StickersManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
|
|||||||
@@ -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,26 +6,73 @@ 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.id,
|
required this.id,
|
||||||
|
this.to,
|
||||||
this.start,
|
this.start,
|
||||||
this.end,
|
this.end,
|
||||||
});
|
});
|
||||||
final String to;
|
|
||||||
|
/// The bare JID to whom the reply applies 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
|
MessageRepliesManager() : super(messageRepliesManager);
|
||||||
String getName() => 'MessageRepliesManager';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getId() => messageRepliesManager;
|
List<String> getDiscoFeatures() => [
|
||||||
|
replyXmlns,
|
||||||
|
];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
List<StanzaHandler> getIncomingStanzaHandlers() => [
|
||||||
StanzaHandler(
|
StanzaHandler(
|
||||||
@@ -43,7 +91,7 @@ class MessageRepliesManager extends XmppManagerBase {
|
|||||||
Future<StanzaHandlerData> _onMessage(Stanza stanza, StanzaHandlerData state) async {
|
Future<StanzaHandlerData> _onMessage(Stanza stanza, StanzaHandlerData state) async {
|
||||||
final reply = stanza.firstTag('reply', xmlns: replyXmlns)!;
|
final reply = stanza.firstTag('reply', xmlns: replyXmlns)!;
|
||||||
final id = reply.attributes['id']! as String;
|
final id = reply.attributes['id']! as String;
|
||||||
final to = reply.attributes['to']! as String;
|
final to = reply.attributes['to'] as String?;
|
||||||
int? start;
|
int? start;
|
||||||
int? end;
|
int? end;
|
||||||
|
|
||||||
@@ -55,11 +103,13 @@ class MessageRepliesManager extends XmppManagerBase {
|
|||||||
end = int.parse(body.attributes['end']! as String);
|
end = int.parse(body.attributes['end']! as String);
|
||||||
}
|
}
|
||||||
|
|
||||||
return state.copyWith(reply: ReplyData(
|
return state.copyWith(
|
||||||
|
reply: ReplyData(
|
||||||
id: id,
|
id: id,
|
||||||
to: to,
|
to: to,
|
||||||
start: start,
|
start: start,
|
||||||
end: end,
|
end: end,
|
||||||
),);
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
276
packages/moxxmpp/moxxmpp.doap
Normal file
276
packages/moxxmpp/moxxmpp.doap
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
|
||||||
|
xmlns='http://usefulinc.com/ns/doap#'
|
||||||
|
xmlns:foaf='http://xmlns.com/foaf/0.1/'
|
||||||
|
xmlns:xmpp='https://linkmauve.fr/ns/xmpp-doap#'>
|
||||||
|
<Project xml:lang='en'>
|
||||||
|
<name>moxxmpp</name>
|
||||||
|
<created>2021-12-26</created>
|
||||||
|
<homepage rdf:resource='https://codeberg.org/moxxy/moxxmpp'/>
|
||||||
|
<os>Linux</os>
|
||||||
|
<os>Windows</os>
|
||||||
|
<os>macOS</os>
|
||||||
|
<os>Android</os>
|
||||||
|
<os>iOS</os>
|
||||||
|
|
||||||
|
<programming-language>Dart</programming-language>
|
||||||
|
|
||||||
|
<maintainer>
|
||||||
|
<foaf:Person>
|
||||||
|
<foaf:name>Alexander "Polynomdivision"</foaf:name>
|
||||||
|
<foaf:homepage rdf:resource="https://blog.polynom.me" />
|
||||||
|
</foaf:Person>
|
||||||
|
</maintainer>
|
||||||
|
|
||||||
|
<implements rdf:resource="https://xmpp.org/rfcs/rfc6120.html" />
|
||||||
|
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0004.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>2.13.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0030.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>2.5rc3</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0054.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.2</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0060.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.24.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0066.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Only jabber:x:oob</xmpp:note>
|
||||||
|
<xmpp:version>1.5</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0084.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Receiving data</xmpp:note>
|
||||||
|
<xmpp:version>1.1.4</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0085.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>2.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0115.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.5.2</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0153.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0184.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.4.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0191.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.3.0</xmpp:version>
|
||||||
|
<xmpp:note xml:lang="en">Not plugged into the UI</xmpp:note>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0198.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.6</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0280.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.0.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0297.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Exists only as part of support for XEP-0280</xmpp:note>
|
||||||
|
<xmpp:version>1.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0300.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Supports only Sha256, Sha512 and blake2b512</xmpp:note>
|
||||||
|
<xmpp:version>1.0.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0308.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.2.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0333.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Read-only support</xmpp:note>
|
||||||
|
<xmpp:version>0.4</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0334.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:note xml:lang="en">Write-only support</xmpp:note>
|
||||||
|
<xmpp:version>0.3.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0352.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>1.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0359.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.6.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0363.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.1.0</xmpp:version>
|
||||||
|
<xmpp:note xml:lang="en">Only handles the success case; not accessible via the App</xmpp:note>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0368.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>1.1.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0380.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.4.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0384.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.8.3</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0420.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>0.4.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0424.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.3.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0444.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.1.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0446.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.2.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0447.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.1.2</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0448.html" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>0.2.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0449.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.1.1</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0461.html" />
|
||||||
|
<xmpp:status>complete</xmpp:status>
|
||||||
|
<xmpp:version>0.2.0</xmpp:version>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://codeberg.org/moxxy/custom-xeps/src/branch/master/xep-xxxx-extensible-file-thumbnails.md" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>0.2.1</xmpp:version>
|
||||||
|
<xmpp:note xml:lang="en">Only Blurhash is implemented</xmpp:note>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
<implements>
|
||||||
|
<xmpp:SupportedXep>
|
||||||
|
<xmpp:xep rdf:resource="https://codeberg.org/moxxy/custom-xeps/src/branch/master/xep-xxxx-file-upload-notification.md" />
|
||||||
|
<xmpp:status>partial</xmpp:status>
|
||||||
|
<xmpp:version>0.0.5</xmpp:version>
|
||||||
|
<xmpp:note xml:lang="en">Sending and receiving implemented; cancellation not implemented</xmpp:note>
|
||||||
|
</xmpp:SupportedXep>
|
||||||
|
</implements>
|
||||||
|
</Project>
|
||||||
|
</rdf:RDF>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: moxxmpp
|
name: moxxmpp
|
||||||
description: A pure-Dart XMPP library
|
description: A pure-Dart XMPP library
|
||||||
version: 0.1.6+1
|
version: 0.2.0
|
||||||
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
|
||||||
@@ -20,7 +21,7 @@ dependencies:
|
|||||||
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.4.1
|
version: ^0.4.3
|
||||||
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
|
||||||
|
|||||||
43
packages/moxxmpp/test/async_queue_test.dart
Normal file
43
packages/moxxmpp/test/async_queue_test.dart
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import 'package:moxxmpp/src/util/queue.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test the async queue', () async {
|
||||||
|
final queue = AsyncQueue();
|
||||||
|
int future1Finish = 0;
|
||||||
|
int future2Finish = 0;
|
||||||
|
int future3Finish = 0;
|
||||||
|
|
||||||
|
await queue.addJob(() => Future<void>.delayed(const Duration(seconds: 3), () => future1Finish = DateTime.now().millisecondsSinceEpoch));
|
||||||
|
await queue.addJob(() => Future<void>.delayed(const Duration(seconds: 3), () => future2Finish = DateTime.now().millisecondsSinceEpoch));
|
||||||
|
await queue.addJob(() => Future<void>.delayed(const Duration(seconds: 3), () => future3Finish = DateTime.now().millisecondsSinceEpoch));
|
||||||
|
|
||||||
|
await Future<void>.delayed(const Duration(seconds: 12));
|
||||||
|
|
||||||
|
// The three futures must be done
|
||||||
|
expect(future1Finish != 0, true);
|
||||||
|
expect(future2Finish != 0, true);
|
||||||
|
expect(future3Finish != 0, true);
|
||||||
|
|
||||||
|
// The end times of the futures must be ordered (on a timeline)
|
||||||
|
// |-- future1Finish -- future2Finish -- future3Finish --|
|
||||||
|
expect(
|
||||||
|
future1Finish < future2Finish && future1Finish < future3Finish,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
future2Finish < future3Finish && future2Finish > future1Finish,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
future3Finish > future1Finish && future3Finish > future2Finish,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
// The queue must be empty at the end
|
||||||
|
expect(queue.queue.isEmpty, true);
|
||||||
|
|
||||||
|
// The queue must not be executing anything at the end
|
||||||
|
expect(queue.isRunning, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
104
packages/moxxmpp/test/awaiter_test.dart
Normal file
104
packages/moxxmpp/test/awaiter_test.dart
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:moxxmpp/src/awaiter.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final bareJid = JID('moxxmpp', 'server3.example', '');
|
||||||
|
|
||||||
|
test('Test awaiting an awaited stanza with a from attribute', () async {
|
||||||
|
final awaiter = StanzaAwaiter();
|
||||||
|
|
||||||
|
// "Send" a stanza
|
||||||
|
final future = await awaiter.addPending('user1@server.example', 'abc123', 'iq');
|
||||||
|
|
||||||
|
// Receive the wrong answer
|
||||||
|
final result1 = await awaiter.onData(
|
||||||
|
XMLNode.fromString('<iq from="user3@server.example" id="abc123" type="result" />'),
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result1, false);
|
||||||
|
final result2 = await awaiter.onData(
|
||||||
|
XMLNode.fromString('<iq from="user1@server.example" id="lol" type="result" />'),
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result2, false);
|
||||||
|
|
||||||
|
// Receive the correct answer
|
||||||
|
final stanza = XMLNode.fromString('<iq from="user1@server.example" id="abc123" type="result" />');
|
||||||
|
final result3 = await awaiter.onData(
|
||||||
|
stanza,
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result3, true);
|
||||||
|
expect(await future, stanza);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test awaiting an awaited stanza without a from attribute', () async {
|
||||||
|
final awaiter = StanzaAwaiter();
|
||||||
|
|
||||||
|
// "Send" a stanza
|
||||||
|
final future = await awaiter.addPending(bareJid.toString(), 'abc123', 'iq');
|
||||||
|
|
||||||
|
// Receive the wrong answer
|
||||||
|
final result1 = await awaiter.onData(
|
||||||
|
XMLNode.fromString('<iq id="lol" type="result" />'),
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result1, false);
|
||||||
|
|
||||||
|
// Receive the correct answer
|
||||||
|
final stanza = XMLNode.fromString('<iq id="abc123" type="result" />');
|
||||||
|
final result2 = await awaiter.onData(
|
||||||
|
stanza,
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result2, true);
|
||||||
|
expect(await future, stanza);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test awaiting a stanza that was already awaited', () async {
|
||||||
|
final awaiter = StanzaAwaiter();
|
||||||
|
|
||||||
|
// "Send" a stanza
|
||||||
|
final future = await awaiter.addPending(bareJid.toString(), 'abc123', 'iq');
|
||||||
|
|
||||||
|
// Receive the correct answer
|
||||||
|
final stanza = XMLNode.fromString('<iq id="abc123" type="result" />');
|
||||||
|
final result1 = await awaiter.onData(
|
||||||
|
stanza,
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result1, true);
|
||||||
|
expect(await future, stanza);
|
||||||
|
|
||||||
|
// Receive it again
|
||||||
|
final result2 = await awaiter.onData(
|
||||||
|
stanza,
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result2, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Test ignoring a stanza that has the wrong tag', () async {
|
||||||
|
final awaiter = StanzaAwaiter();
|
||||||
|
|
||||||
|
// "Send" a stanza
|
||||||
|
final future = await awaiter.addPending(bareJid.toString(), 'abc123', 'iq');
|
||||||
|
|
||||||
|
// Receive the wrong answer
|
||||||
|
final stanza = XMLNode.fromString('<iq id="abc123" type="result" />');
|
||||||
|
final result1 = await awaiter.onData(
|
||||||
|
XMLNode.fromString('<message id="abc123" type="result" />'),
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result1, false);
|
||||||
|
|
||||||
|
// Receive the correct answer
|
||||||
|
final result2 = await awaiter.onData(
|
||||||
|
stanza,
|
||||||
|
bareJid,
|
||||||
|
);
|
||||||
|
expect(result2, true);
|
||||||
|
expect(await future, stanza);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
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');
|
||||||
@@ -42,4 +41,15 @@ void main() {
|
|||||||
test('Parse resource with a slash', () {
|
test('Parse resource with a slash', () {
|
||||||
expect(JID.fromString('hallo@welt.example./test/welt') == JID('hallo', 'welt.example', 'test/welt'), true);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,16 +54,20 @@ void main() {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
final connection = XmppConnection(TestingReconnectionPolicy(), stubSocket)
|
final connection = XmppConnection(
|
||||||
..registerFeatureNegotiators([
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
stubSocket,
|
||||||
|
)..registerFeatureNegotiators([
|
||||||
StubNegotiator1(),
|
StubNegotiator1(),
|
||||||
StubNegotiator2(),
|
StubNegotiator2(),
|
||||||
])
|
])
|
||||||
..registerManagers([
|
..registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
])
|
])
|
||||||
..setConnectionSettings(
|
..setConnectionSettings(
|
||||||
ConnectionSettings(
|
ConnectionSettings(
|
||||||
|
|||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import 'package:moxxmpp/moxxmpp.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('Make sure reply does not copy the children', () {
|
|
||||||
final stanza = Stanza.iq(
|
|
||||||
to: 'hallo',
|
|
||||||
from: 'world',
|
|
||||||
id: 'abc123',
|
|
||||||
type: 'get',
|
|
||||||
children: [
|
|
||||||
XMLNode(tag: 'test-tag'),
|
|
||||||
XMLNode(tag: 'test-tag2')
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
final reply = stanza.reply();
|
|
||||||
|
|
||||||
expect(reply.children, []);
|
|
||||||
expect(reply.type, 'result');
|
|
||||||
expect(reply.from, stanza.to);
|
|
||||||
expect(reply.to, stanza.from);
|
|
||||||
expect(reply.id, stanza.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Make sure reply includes the new children', () {
|
|
||||||
final stanza = Stanza.iq(
|
|
||||||
to: 'hallo',
|
|
||||||
from: 'world',
|
|
||||||
id: 'abc123',
|
|
||||||
type: 'get',
|
|
||||||
children: [
|
|
||||||
XMLNode(tag: 'test-tag'),
|
|
||||||
XMLNode(tag: 'test-tag2')
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
final reply = stanza.reply(
|
|
||||||
children: [
|
|
||||||
XMLNode.xmlns(
|
|
||||||
tag: 'test',
|
|
||||||
xmlns: 'test',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(reply.children.length, 1);
|
|
||||||
expect(reply.firstTag('test') != null, true);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
40
packages/moxxmpp/test/wait_test.dart
Normal file
40
packages/moxxmpp/test/wait_test.dart
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'package:moxxmpp/src/util/wait.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Test adding and resolving', () async {
|
||||||
|
// ID -> Milliseconds since epoch
|
||||||
|
final tracker = WaitForTracker<int, int>();
|
||||||
|
|
||||||
|
int r2 = 0;
|
||||||
|
int r3 = 0;
|
||||||
|
|
||||||
|
// Queue some jobs
|
||||||
|
final r1 = await tracker.waitFor(0);
|
||||||
|
expect(r1, null);
|
||||||
|
|
||||||
|
tracker
|
||||||
|
.waitFor(0)
|
||||||
|
.then((result) async {
|
||||||
|
expect(result != null, true);
|
||||||
|
r2 = await result!;
|
||||||
|
});
|
||||||
|
tracker
|
||||||
|
.waitFor(0)
|
||||||
|
.then((result) async {
|
||||||
|
expect(result != null, true);
|
||||||
|
r3 = await result!;
|
||||||
|
});
|
||||||
|
|
||||||
|
final c = await tracker.waitFor(1);
|
||||||
|
expect(c, null);
|
||||||
|
|
||||||
|
// Resolve jobs
|
||||||
|
await tracker.resolve(0, 42);
|
||||||
|
await tracker.resolve(1, 25);
|
||||||
|
await tracker.resolve(2, -1);
|
||||||
|
|
||||||
|
expect(r2, 42);
|
||||||
|
expect(r3, 42);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -3,14 +3,14 @@ import 'package:test/test.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('Parsing', () {
|
test('Parsing', () {
|
||||||
const testData = "<x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>urn:xmpp:dataforms:softwareinfo</value></field><field var='ip_version' type='text-multi' ><value>ipv4</value><value>ipv6</value></field><field var='os'><value>Mac</value></field><field var='os_version'><value>10.5.1</value></field><field var='software'><value>Psi</value></field><field var='software_version'><value>0.11</value></field></x>";
|
const testData = "<x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>urn:xmpp:dataforms:softwareinfo</value></field><field var='ip_version' type='text-multi' ><value>ipv4</value><value>ipv6</value></field><field var='os'><value>Mac</value></field><field var='os_version'><value>10.5.1</value></field><field var='software'><value>Psi</value></field><field var='software_version'><value>0.11</value></field></x>";
|
||||||
|
|
||||||
final form = parseDataForm(XMLNode.fromString(testData));
|
final form = parseDataForm(XMLNode.fromString(testData));
|
||||||
expect(form.getFieldByVar('FORM_TYPE')?.values.first, 'urn:xmpp:dataforms:softwareinfo');
|
expect(form.getFieldByVar('FORM_TYPE')?.values.first, 'urn:xmpp:dataforms:softwareinfo');
|
||||||
expect(form.getFieldByVar('ip_version')?.values, [ 'ipv4', 'ipv6' ]);
|
expect(form.getFieldByVar('ip_version')?.values, [ 'ipv4', 'ipv6' ]);
|
||||||
expect(form.getFieldByVar('os')?.values.first, 'Mac');
|
expect(form.getFieldByVar('os')?.values.first, 'Mac');
|
||||||
expect(form.getFieldByVar('os_version')?.values.first, '10.5.1');
|
expect(form.getFieldByVar('os_version')?.values.first, '10.5.1');
|
||||||
expect(form.getFieldByVar('software')?.values.first, 'Psi');
|
expect(form.getFieldByVar('software')?.values.first, 'Psi');
|
||||||
expect(form.getFieldByVar('software_version')?.values.first, '0.11');
|
expect(form.getFieldByVar('software_version')?.values.first, '0.11');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
import 'package:moxxmpp/moxxmpp.dart';
|
import 'package:moxxmpp/moxxmpp.dart';
|
||||||
|
import 'package:moxxmpp/src/xeps/xep_0030/cache.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import '../helpers/logging.dart';
|
||||||
import '../helpers/xmpp.dart';
|
import '../helpers/xmpp.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
initLogger();
|
||||||
|
|
||||||
test('Test having multiple disco requests for the same JID', () async {
|
test('Test having multiple disco requests for the same JID', () async {
|
||||||
final fakeSocket = StubTCPSocket(
|
final fakeSocket = StubTCPSocket(
|
||||||
play: [
|
play: [
|
||||||
@@ -53,7 +57,7 @@ void main() {
|
|||||||
ignoreId: true,
|
ignoreId: true,
|
||||||
),
|
),
|
||||||
StringExpectation(
|
StringExpectation(
|
||||||
"<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show>chat</show><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://moxxmpp.example' ver='QRTBC5cg/oYd+UOTYazSQR4zb/I=' /></presence>",
|
"<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show>chat</show><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://moxxmpp.example' ver='3QvQ2RAy45XBDhArjxy/vEWMl+E=' /></presence>",
|
||||||
'',
|
'',
|
||||||
),
|
),
|
||||||
StanzaExpectation(
|
StanzaExpectation(
|
||||||
@@ -65,7 +69,11 @@ void main() {
|
|||||||
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), socket: fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -73,10 +81,11 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager(null, [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators(
|
conn.registerFeatureNegotiators(
|
||||||
[
|
[
|
||||||
@@ -97,7 +106,7 @@ void main() {
|
|||||||
|
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
expect(
|
expect(
|
||||||
disco.getRunningInfoQueries(DiscoCacheKey(jid.toString(), null)).length,
|
disco.infoTracker.getRunningTasks(DiscoCacheKey(jid.toString(), null)).length,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
fakeSocket.injectRawXml("<iq type='result' id='${fakeSocket.lastId!}' from='romeo@montague.lit/orchard' to='polynomdivision@test.server/MU29eEZn' xmlns='jabber:client'><query xmlns='http://jabber.org/protocol/disco#info' /></iq>");
|
fakeSocket.injectRawXml("<iq type='result' id='${fakeSocket.lastId!}' from='romeo@montague.lit/orchard' to='polynomdivision@test.server/MU29eEZn' xmlns='jabber:client'><query xmlns='http://jabber.org/protocol/disco#info' /></iq>");
|
||||||
@@ -106,6 +115,6 @@ void main() {
|
|||||||
|
|
||||||
expect(fakeSocket.getState(), 6);
|
expect(fakeSocket.getState(), 6);
|
||||||
expect(await result1, await result2);
|
expect(await result1, await result2);
|
||||||
expect(disco.hasInfoQueriesRunning(), false);
|
expect(disco.infoTracker.hasTasksRunning(), false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -4,164 +4,167 @@ import 'package:test/test.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('Test XEP example', () async {
|
test('Test XEP example', () async {
|
||||||
final data = DiscoInfo(
|
final data = DiscoInfo(
|
||||||
[
|
[
|
||||||
'http://jabber.org/protocol/caps',
|
'http://jabber.org/protocol/caps',
|
||||||
'http://jabber.org/protocol/disco#info',
|
'http://jabber.org/protocol/disco#info',
|
||||||
'http://jabber.org/protocol/disco#items',
|
'http://jabber.org/protocol/disco#items',
|
||||||
'http://jabber.org/protocol/muc'
|
'http://jabber.org/protocol/muc'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
Identity(
|
Identity(
|
||||||
category: 'client',
|
category: 'client',
|
||||||
type: 'pc',
|
type: 'pc',
|
||||||
name: 'Exodus 0.9.1',
|
name: 'Exodus 0.9.1',
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
JID.fromString('some@user.local/test'),
|
null,
|
||||||
);
|
JID.fromString('some@user.local/test'),
|
||||||
|
);
|
||||||
|
|
||||||
final hash = await calculateCapabilityHash(data, Sha1());
|
final hash = await calculateCapabilityHash(data, Sha1());
|
||||||
expect(hash, 'QgayPKawpkPSDYmwT/WM94uAlu0=');
|
expect(hash, 'QgayPKawpkPSDYmwT/WM94uAlu0=');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test complex generation example', () async {
|
test('Test complex generation example', () async {
|
||||||
const extDiscoDataString = "<x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>urn:xmpp:dataforms:softwareinfo</value></field><field var='ip_version' type='text-multi' ><value>ipv4</value><value>ipv6</value></field><field var='os'><value>Mac</value></field><field var='os_version'><value>10.5.1</value></field><field var='software'><value>Psi</value></field><field var='software_version'><value>0.11</value></field></x>";
|
const extDiscoDataString = "<x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>urn:xmpp:dataforms:softwareinfo</value></field><field var='ip_version' type='text-multi' ><value>ipv4</value><value>ipv6</value></field><field var='os'><value>Mac</value></field><field var='os_version'><value>10.5.1</value></field><field var='software'><value>Psi</value></field><field var='software_version'><value>0.11</value></field></x>";
|
||||||
final data = DiscoInfo(
|
final data = DiscoInfo(
|
||||||
[
|
[
|
||||||
'http://jabber.org/protocol/caps',
|
'http://jabber.org/protocol/caps',
|
||||||
'http://jabber.org/protocol/disco#info',
|
'http://jabber.org/protocol/disco#info',
|
||||||
'http://jabber.org/protocol/disco#items',
|
'http://jabber.org/protocol/disco#items',
|
||||||
'http://jabber.org/protocol/muc'
|
'http://jabber.org/protocol/muc'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
const Identity(
|
const Identity(
|
||||||
category: 'client',
|
category: 'client',
|
||||||
type: 'pc',
|
type: 'pc',
|
||||||
name: 'Psi 0.11',
|
name: 'Psi 0.11',
|
||||||
lang: 'en',
|
lang: 'en',
|
||||||
),
|
),
|
||||||
const Identity(
|
const Identity(
|
||||||
category: 'client',
|
category: 'client',
|
||||||
type: 'pc',
|
type: 'pc',
|
||||||
name: 'Ψ 0.11',
|
name: 'Ψ 0.11',
|
||||||
lang: 'el',
|
lang: 'el',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
[ parseDataForm(XMLNode.fromString(extDiscoDataString)) ],
|
[ parseDataForm(XMLNode.fromString(extDiscoDataString)) ],
|
||||||
JID.fromString('some@user.local/test'),
|
null,
|
||||||
);
|
JID.fromString('some@user.local/test'),
|
||||||
|
);
|
||||||
|
|
||||||
final hash = await calculateCapabilityHash(data, Sha1());
|
final hash = await calculateCapabilityHash(data, Sha1());
|
||||||
expect(hash, 'q07IKJEyjvHSyhy//CH0CxmKi8w=');
|
expect(hash, 'q07IKJEyjvHSyhy//CH0CxmKi8w=');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test Gajim capability hash computation', () async {
|
test('Test Gajim capability hash computation', () async {
|
||||||
// TODO: This one fails
|
// TODO: This one fails
|
||||||
/*
|
/*
|
||||||
final data = DiscoInfo(
|
final data = DiscoInfo(
|
||||||
features: [
|
features: [
|
||||||
"http://jabber.org/protocol/bytestreams",
|
"http://jabber.org/protocol/bytestreams",
|
||||||
"http://jabber.org/protocol/muc",
|
"http://jabber.org/protocol/muc",
|
||||||
"http://jabber.org/protocol/commands",
|
"http://jabber.org/protocol/commands",
|
||||||
"http://jabber.org/protocol/disco#info",
|
"http://jabber.org/protocol/disco#info",
|
||||||
"jabber:iq:last",
|
"jabber:iq:last",
|
||||||
"jabber:x:data",
|
"jabber:x:data",
|
||||||
"jabber:x:encrypted",
|
"jabber:x:encrypted",
|
||||||
"urn:xmpp:ping",
|
"urn:xmpp:ping",
|
||||||
"http://jabber.org/protocol/chatstates",
|
"http://jabber.org/protocol/chatstates",
|
||||||
"urn:xmpp:receipts",
|
"urn:xmpp:receipts",
|
||||||
"urn:xmpp:time",
|
"urn:xmpp:time",
|
||||||
"jabber:iq:version",
|
"jabber:iq:version",
|
||||||
"http://jabber.org/protocol/rosterx",
|
"http://jabber.org/protocol/rosterx",
|
||||||
"urn:xmpp:sec-label:0",
|
"urn:xmpp:sec-label:0",
|
||||||
"jabber:x:conference",
|
"jabber:x:conference",
|
||||||
"urn:xmpp:message-correct:0",
|
"urn:xmpp:message-correct:0",
|
||||||
"urn:xmpp:chat-markers:0",
|
"urn:xmpp:chat-markers:0",
|
||||||
"urn:xmpp:eme:0",
|
"urn:xmpp:eme:0",
|
||||||
"http://jabber.org/protocol/xhtml-im",
|
"http://jabber.org/protocol/xhtml-im",
|
||||||
"urn:xmpp:hashes:2",
|
"urn:xmpp:hashes:2",
|
||||||
"urn:xmpp:hash-function-text-names:md5",
|
"urn:xmpp:hash-function-text-names:md5",
|
||||||
"urn:xmpp:hash-function-text-names:sha-1",
|
"urn:xmpp:hash-function-text-names:sha-1",
|
||||||
"urn:xmpp:hash-function-text-names:sha-256",
|
"urn:xmpp:hash-function-text-names:sha-256",
|
||||||
"urn:xmpp:hash-function-text-names:sha-512",
|
"urn:xmpp:hash-function-text-names:sha-512",
|
||||||
"urn:xmpp:hash-function-text-names:sha3-256",
|
"urn:xmpp:hash-function-text-names:sha3-256",
|
||||||
"urn:xmpp:hash-function-text-names:sha3-512",
|
"urn:xmpp:hash-function-text-names:sha3-512",
|
||||||
"urn:xmpp:hash-function-text-names:id-blake2b256",
|
"urn:xmpp:hash-function-text-names:id-blake2b256",
|
||||||
"urn:xmpp:hash-function-text-names:id-blake2b512",
|
"urn:xmpp:hash-function-text-names:id-blake2b512",
|
||||||
"urn:xmpp:jingle:1",
|
"urn:xmpp:jingle:1",
|
||||||
"urn:xmpp:jingle:apps:file-transfer:5",
|
"urn:xmpp:jingle:apps:file-transfer:5",
|
||||||
"urn:xmpp:jingle:security:xtls:0",
|
"urn:xmpp:jingle:security:xtls:0",
|
||||||
"urn:xmpp:jingle:transports:s5b:1",
|
"urn:xmpp:jingle:transports:s5b:1",
|
||||||
"urn:xmpp:jingle:transports:ibb:1",
|
"urn:xmpp:jingle:transports:ibb:1",
|
||||||
"urn:xmpp:avatar:metadata+notify",
|
"urn:xmpp:avatar:metadata+notify",
|
||||||
"urn:xmpp:message-moderate:0",
|
"urn:xmpp:message-moderate:0",
|
||||||
"http://jabber.org/protocol/tune+notify",
|
"http://jabber.org/protocol/tune+notify",
|
||||||
"http://jabber.org/protocol/geoloc+notify",
|
"http://jabber.org/protocol/geoloc+notify",
|
||||||
"http://jabber.org/protocol/nick+notify",
|
"http://jabber.org/protocol/nick+notify",
|
||||||
"eu.siacs.conversations.axolotl.devicelist+notify",
|
"eu.siacs.conversations.axolotl.devicelist+notify",
|
||||||
],
|
],
|
||||||
identities: [
|
identities: [
|
||||||
Identity(
|
Identity(
|
||||||
category: "client",
|
category: "client",
|
||||||
type: "pc",
|
type: "pc",
|
||||||
name: "Gajim"
|
name: "Gajim"
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final hash = await calculateCapabilityHash(data, Sha1());
|
final hash = await calculateCapabilityHash(data, Sha1());
|
||||||
expect(hash, "T7fOZrtBnV8sDA2fFTS59vyOyUs=");
|
expect(hash, "T7fOZrtBnV8sDA2fFTS59vyOyUs=");
|
||||||
*/
|
*/
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Test Conversations hash computation', () async {
|
test('Test Conversations hash computation', () async {
|
||||||
final data = DiscoInfo(
|
final data = DiscoInfo(
|
||||||
[
|
[
|
||||||
'eu.siacs.conversations.axolotl.devicelist+notify',
|
'eu.siacs.conversations.axolotl.devicelist+notify',
|
||||||
'http://jabber.org/protocol/caps',
|
'http://jabber.org/protocol/caps',
|
||||||
'http://jabber.org/protocol/chatstates',
|
'http://jabber.org/protocol/chatstates',
|
||||||
'http://jabber.org/protocol/disco#info',
|
'http://jabber.org/protocol/disco#info',
|
||||||
'http://jabber.org/protocol/muc',
|
'http://jabber.org/protocol/muc',
|
||||||
'http://jabber.org/protocol/nick+notify',
|
'http://jabber.org/protocol/nick+notify',
|
||||||
'jabber:iq:version',
|
'jabber:iq:version',
|
||||||
'jabber:x:conference',
|
'jabber:x:conference',
|
||||||
'jabber:x:oob',
|
'jabber:x:oob',
|
||||||
'storage:bookmarks+notify',
|
'storage:bookmarks+notify',
|
||||||
'urn:xmpp:avatar:metadata+notify',
|
'urn:xmpp:avatar:metadata+notify',
|
||||||
'urn:xmpp:chat-markers:0',
|
'urn:xmpp:chat-markers:0',
|
||||||
'urn:xmpp:jingle-message:0',
|
'urn:xmpp:jingle-message:0',
|
||||||
'urn:xmpp:jingle:1',
|
'urn:xmpp:jingle:1',
|
||||||
'urn:xmpp:jingle:apps:dtls:0',
|
'urn:xmpp:jingle:apps:dtls:0',
|
||||||
'urn:xmpp:jingle:apps:file-transfer:3',
|
'urn:xmpp:jingle:apps:file-transfer:3',
|
||||||
'urn:xmpp:jingle:apps:file-transfer:4',
|
'urn:xmpp:jingle:apps:file-transfer:4',
|
||||||
'urn:xmpp:jingle:apps:file-transfer:5',
|
'urn:xmpp:jingle:apps:file-transfer:5',
|
||||||
'urn:xmpp:jingle:apps:rtp:1',
|
'urn:xmpp:jingle:apps:rtp:1',
|
||||||
'urn:xmpp:jingle:apps:rtp:audio',
|
'urn:xmpp:jingle:apps:rtp:audio',
|
||||||
'urn:xmpp:jingle:apps:rtp:video',
|
'urn:xmpp:jingle:apps:rtp:video',
|
||||||
'urn:xmpp:jingle:jet-omemo:0',
|
'urn:xmpp:jingle:jet-omemo:0',
|
||||||
'urn:xmpp:jingle:jet:0',
|
'urn:xmpp:jingle:jet:0',
|
||||||
'urn:xmpp:jingle:transports:ibb:1',
|
'urn:xmpp:jingle:transports:ibb:1',
|
||||||
'urn:xmpp:jingle:transports:ice-udp:1',
|
'urn:xmpp:jingle:transports:ice-udp:1',
|
||||||
'urn:xmpp:jingle:transports:s5b:1',
|
'urn:xmpp:jingle:transports:s5b:1',
|
||||||
'urn:xmpp:message-correct:0',
|
'urn:xmpp:message-correct:0',
|
||||||
'urn:xmpp:ping',
|
'urn:xmpp:ping',
|
||||||
'urn:xmpp:receipts',
|
'urn:xmpp:receipts',
|
||||||
'urn:xmpp:time'
|
'urn:xmpp:time'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
Identity(
|
Identity(
|
||||||
category: 'client',
|
category: 'client',
|
||||||
type: 'phone',
|
type: 'phone',
|
||||||
name: 'Conversations',
|
name: 'Conversations',
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
JID.fromString('user@server.local/test'),
|
null,
|
||||||
);
|
JID.fromString('user@server.local/test'),
|
||||||
|
);
|
||||||
|
|
||||||
final hash = await calculateCapabilityHash(data, Sha1());
|
final hash = await calculateCapabilityHash(data, Sha1());
|
||||||
expect(hash, 'zcIke+Rk13ah4d1pwDG7bEZsVwA=');
|
expect(hash, 'zcIke+Rk13ah4d1pwDG7bEZsVwA=');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ Future<void> runOutgoingStanzaHandlers(StreamManagementManager man, Stanza stanz
|
|||||||
|
|
||||||
XmppManagerAttributes mkAttributes(void Function(Stanza) callback) {
|
XmppManagerAttributes mkAttributes(void Function(Stanza) callback) {
|
||||||
return XmppManagerAttributes(
|
return XmppManagerAttributes(
|
||||||
sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false }) async {
|
sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async {
|
||||||
callback(stanza);
|
callback(stanza);
|
||||||
|
|
||||||
return Stanza.message();
|
return Stanza.message();
|
||||||
@@ -34,7 +34,7 @@ XmppManagerAttributes mkAttributes(void Function(Stanza) callback) {
|
|||||||
isFeatureSupported: (_) => false,
|
isFeatureSupported: (_) => false,
|
||||||
getFullJID: () => JID.fromString('hallo@example.server/uwu'),
|
getFullJID: () => JID.fromString('hallo@example.server/uwu'),
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
|
||||||
getNegotiatorById: getNegotiatorNullStub,
|
getNegotiatorById: getNegotiatorNullStub,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -233,7 +233,11 @@ void main() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -242,12 +246,13 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
final sm = StreamManagementManager();
|
final sm = StreamManagementManager();
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
CarbonsManager()..forceEnable(),
|
CarbonsManager()..forceEnable(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators(
|
conn.registerFeatureNegotiators(
|
||||||
[
|
[
|
||||||
@@ -343,19 +348,23 @@ void main() {
|
|||||||
'<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true" />',
|
'<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true" />',
|
||||||
),
|
),
|
||||||
StringExpectation(
|
StringExpectation(
|
||||||
"<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show>chat</show><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://moxxmpp.example' ver='QRTBC5cg/oYd+UOTYazSQR4zb/I=' /></presence>",
|
"<presence xmlns='jabber:client' from='polynomdivision@test.server/MU29eEZn'><show>chat</show><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://moxxmpp.example' ver='3QvQ2RAy45XBDhArjxy/vEWMl+E=' /></presence>",
|
||||||
'<iq type="result" />',
|
'<iq type="result" />',
|
||||||
),
|
),
|
||||||
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,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -364,12 +373,13 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
final sm = StreamManagementManager();
|
final sm = StreamManagementManager();
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
CarbonsManager()..forceEnable(),
|
CarbonsManager()..forceEnable(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators(
|
conn.registerFeatureNegotiators(
|
||||||
[
|
[
|
||||||
@@ -510,7 +520,11 @@ void main() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -518,9 +532,9 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
]);
|
]);
|
||||||
@@ -602,7 +616,11 @@ void main() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -610,9 +628,9 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
]);
|
]);
|
||||||
@@ -694,7 +712,11 @@ void main() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -702,9 +724,9 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
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, bool forceEncryption = 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(), AlwaysConnectedConnectivityManager(), 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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,59 +29,62 @@ T? getUnsupportedCSINegotiator<T extends XmppFeatureNegotiatorBase>(String id) {
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Test the XEP-0352 implementation', () {
|
group('Test the XEP-0352 implementation', () {
|
||||||
test('Test setting the CSI state when CSI is unsupported', () {
|
test('Test setting the CSI state when CSI is unsupported', () {
|
||||||
var nonzaSent = false;
|
var nonzaSent = false;
|
||||||
final csi = CSIManager();
|
final csi = CSIManager();
|
||||||
csi.register(XmppManagerAttributes(
|
csi.register(
|
||||||
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
XmppManagerAttributes(
|
||||||
sendEvent: (event) {},
|
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'),
|
||||||
sendNonza: (nonza) {
|
sendEvent: (event) {},
|
||||||
nonzaSent = true;
|
sendNonza: (nonza) {
|
||||||
},
|
nonzaSent = true;
|
||||||
getConnectionSettings: () => ConnectionSettings(
|
},
|
||||||
jid: JID.fromString('some.user@example.server'),
|
getConnectionSettings: () => ConnectionSettings(
|
||||||
password: 'password',
|
jid: JID.fromString('some.user@example.server'),
|
||||||
useDirectTLS: true,
|
password: 'password',
|
||||||
allowPlainAuth: false,
|
useDirectTLS: true,
|
||||||
),
|
allowPlainAuth: false,
|
||||||
getManagerById: getManagerNullStub,
|
),
|
||||||
getNegotiatorById: getUnsupportedCSINegotiator,
|
getManagerById: getManagerNullStub,
|
||||||
isFeatureSupported: (_) => false,
|
getNegotiatorById: getUnsupportedCSINegotiator,
|
||||||
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
isFeatureSupported: (_) => false,
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
),
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
|
||||||
);
|
),
|
||||||
|
);
|
||||||
|
|
||||||
csi.setActive();
|
csi.setActive();
|
||||||
csi.setInactive();
|
csi.setInactive();
|
||||||
|
|
||||||
expect(nonzaSent, false, reason: 'Expected that no nonza is sent');
|
expect(nonzaSent, false, reason: 'Expected that no nonza is sent');
|
||||||
});
|
});
|
||||||
test('Test setting the CSI state when CSI is supported', () {
|
test('Test setting the CSI state when CSI is supported', () {
|
||||||
final csi = CSIManager();
|
final csi = CSIManager();
|
||||||
csi.register(XmppManagerAttributes(
|
csi.register(
|
||||||
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false }) async => XMLNode(tag: 'hallo'),
|
XmppManagerAttributes(
|
||||||
sendEvent: (event) {},
|
sendStanza: (_, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool retransmitted = false, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'),
|
||||||
sendNonza: (nonza) {
|
sendEvent: (event) {},
|
||||||
expect(nonza.attributes['xmlns'] == csiXmlns, true, reason: "Expected only nonzas with XMLNS '$csiXmlns'");
|
sendNonza: (nonza) {
|
||||||
},
|
expect(nonza.attributes['xmlns'] == csiXmlns, true, reason: "Expected only nonzas with XMLNS '$csiXmlns'");
|
||||||
getConnectionSettings: () => ConnectionSettings(
|
},
|
||||||
jid: JID.fromString('some.user@example.server'),
|
getConnectionSettings: () => ConnectionSettings(
|
||||||
password: 'password',
|
jid: JID.fromString('some.user@example.server'),
|
||||||
useDirectTLS: true,
|
password: 'password',
|
||||||
allowPlainAuth: false,
|
useDirectTLS: true,
|
||||||
),
|
allowPlainAuth: false,
|
||||||
getManagerById: getManagerNullStub,
|
),
|
||||||
getNegotiatorById: getSupportedCSINegotiator,
|
getManagerById: getManagerNullStub,
|
||||||
isFeatureSupported: (_) => false,
|
getNegotiatorById: getSupportedCSINegotiator,
|
||||||
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
isFeatureSupported: (_) => false,
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
),);
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
csi.setActive();
|
csi.setActive();
|
||||||
csi.setInactive();
|
csi.setInactive();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,52 +3,52 @@ import 'package:test/test.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Test the XEP-0363 header preparation', () {
|
group('Test the XEP-0363 header preparation', () {
|
||||||
test('invariance', () {
|
test('invariance', () {
|
||||||
final headers = {
|
final headers = {
|
||||||
'authorization': 'Basic Base64String==',
|
'authorization': 'Basic Base64String==',
|
||||||
'cookie': 'foo=bar; user=romeo'
|
'cookie': 'foo=bar; user=romeo'
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
prepareHeaders(headers),
|
prepareHeaders(headers),
|
||||||
headers,
|
headers,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
test('invariance through uppercase', () {
|
test('invariance through uppercase', () {
|
||||||
final headers = {
|
final headers = {
|
||||||
'Authorization': 'Basic Base64String==',
|
'Authorization': 'Basic Base64String==',
|
||||||
'Cookie': 'foo=bar; user=romeo'
|
'Cookie': 'foo=bar; user=romeo'
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
prepareHeaders(headers),
|
prepareHeaders(headers),
|
||||||
headers,
|
headers,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
test('remove unspecified headers', () {
|
test('remove unspecified headers', () {
|
||||||
final headers = {
|
final headers = {
|
||||||
'Authorization': 'Basic Base64String==',
|
'Authorization': 'Basic Base64String==',
|
||||||
'Cookie': 'foo=bar; user=romeo',
|
'Cookie': 'foo=bar; user=romeo',
|
||||||
'X-Tracking': 'Base64String=='
|
'X-Tracking': 'Base64String=='
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
prepareHeaders(headers),
|
prepareHeaders(headers),
|
||||||
{
|
{
|
||||||
'Authorization': 'Basic Base64String==',
|
'Authorization': 'Basic Base64String==',
|
||||||
'Cookie': 'foo=bar; user=romeo',
|
'Cookie': 'foo=bar; user=romeo',
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
test('remove newlines', () {
|
test('remove newlines', () {
|
||||||
final headers = {
|
final headers = {
|
||||||
'Authorization': '\n\nBasic Base64String==\n\n',
|
'Authorization': '\n\nBasic Base64String==\n\n',
|
||||||
'\nCookie\r\n': 'foo=bar; user=romeo',
|
'\nCookie\r\n': 'foo=bar; user=romeo',
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
prepareHeaders(headers),
|
prepareHeaders(headers),
|
||||||
{
|
{
|
||||||
'Authorization': 'Basic Base64String==',
|
'Authorization': 'Basic Base64String==',
|
||||||
'Cookie': 'foo=bar; user=romeo',
|
'Cookie': 'foo=bar; user=romeo',
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
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,9 +7,9 @@ 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, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'),
|
||||||
sendEvent: (event) {
|
sendEvent: (event) {
|
||||||
eventTriggered = true;
|
eventTriggered = true;
|
||||||
},
|
},
|
||||||
@@ -25,7 +25,7 @@ Future<bool> testRosterManager(String bareJid, String resource, String stanzaStr
|
|||||||
isFeatureSupported: (_) => false,
|
isFeatureSupported: (_) => false,
|
||||||
getFullJID: () => JID.fromString('$bareJid/$resource'),
|
getFullJID: () => JID.fromString('$bareJid/$resource'),
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
|
||||||
),);
|
),);
|
||||||
|
|
||||||
final stanza = Stanza.fromXMLNode(XMLNode.fromString(stanzaString));
|
final stanza = Stanza.fromXMLNode(XMLNode.fromString(stanzaString));
|
||||||
@@ -118,7 +118,10 @@ void main() {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
// TODO: This test is broken since we query the server and enable carbons
|
// TODO: This test is broken since we query the server and enable carbons
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -126,11 +129,12 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators(
|
conn.registerFeatureNegotiators(
|
||||||
[
|
[
|
||||||
@@ -172,7 +176,11 @@ void main() {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
var receivedEvent = false;
|
var receivedEvent = false;
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -180,10 +188,11 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
SaslPlainNegotiator()
|
SaslPlainNegotiator()
|
||||||
@@ -226,7 +235,11 @@ void main() {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
var receivedEvent = false;
|
var receivedEvent = false;
|
||||||
final XmppConnection conn = XmppConnection(TestingReconnectionPolicy(), fakeSocket);
|
final XmppConnection conn = XmppConnection(
|
||||||
|
TestingReconnectionPolicy(),
|
||||||
|
AlwaysConnectedConnectivityManager(),
|
||||||
|
fakeSocket,
|
||||||
|
);
|
||||||
conn.setConnectionSettings(ConnectionSettings(
|
conn.setConnectionSettings(ConnectionSettings(
|
||||||
jid: JID.fromString('polynomdivision@test.server'),
|
jid: JID.fromString('polynomdivision@test.server'),
|
||||||
password: 'aaaa',
|
password: 'aaaa',
|
||||||
@@ -234,10 +247,11 @@ void main() {
|
|||||||
allowPlainAuth: true,
|
allowPlainAuth: true,
|
||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager(),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager([]),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
SaslPlainNegotiator()
|
SaslPlainNegotiator()
|
||||||
@@ -290,7 +304,7 @@ void main() {
|
|||||||
),);
|
),);
|
||||||
conn.registerManagers([
|
conn.registerManagers([
|
||||||
PresenceManager('http://moxxmpp.example'),
|
PresenceManager('http://moxxmpp.example'),
|
||||||
RosterManager(),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager(),
|
DiscoManager(),
|
||||||
PingManager(),
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
@@ -308,9 +322,9 @@ 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, bool forceEncryption = false, }) async => XMLNode(tag: 'hallo'),
|
||||||
sendEvent: (event) {
|
sendEvent: (event) {
|
||||||
eventTriggered = true;
|
eventTriggered = true;
|
||||||
},
|
},
|
||||||
@@ -326,7 +340,7 @@ void main() {
|
|||||||
isFeatureSupported: (_) => false,
|
isFeatureSupported: (_) => false,
|
||||||
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
getFullJID: () => JID.fromString('some.user@example.server/aaaaa'),
|
||||||
getSocket: () => StubTCPSocket(play: []),
|
getSocket: () => StubTCPSocket(play: []),
|
||||||
getConnection: () => XmppConnection(TestingReconnectionPolicy(), StubTCPSocket(play: [])),
|
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
|
||||||
),);
|
),);
|
||||||
|
|
||||||
// NOTE: Based on https://gultsch.de/gajim_roster_push_and_message_interception.html
|
// NOTE: Based on https://gultsch.de/gajim_roster_push_and_message_interception.html
|
||||||
|
|||||||
@@ -11,6 +11,16 @@ 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`.
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user