Compare commits

..

No commits in common. "93d08188ea72892eb8d09660ed445a17ef0dc3c6" and "1000e0756b8521ac247c51426683596c46e2fef3" have entirely different histories.

88 changed files with 1741 additions and 2745 deletions

View File

@ -1,14 +0,0 @@
[general]
ignore=B5,B6,B7,B8
[title-max-length]
line-length=72
[title-trailing-punctuation]
[title-hard-tab]
[title-match-regex]
regex=^((feat|fix|chore|refactor|docs|release|test)\((meta|tests|style|docs|xep|core)+(,(meta|tests|style|docs|xep|core))*\)|release): [A-Z0-9].*$
[body-trailing-whitespace]
[body-first-line-empty]

View File

@ -1,19 +0,0 @@
# Contribution Guide
Thanks for your interest in the moxxmpp XMPP library! This document contains guidelines and guides for working on the moxxmpp codebase.
## Contributing
If you want to fix a small issue, you can just fork, create a new branch, and start working right away. However, if you want to work
on a bigger feature, please first create an issue (if an issue does not already exist) or join the [development chat](xmpp:moxxy@muc.moxxy.org?join) (xmpp:moxxy@muc.moxxy.org?join)
to discuss the feature first.
Before creating a pull request, please make sure you checked every item on the following checklist:
- [ ] I formatted the code with the dart formatter (`dart format`) before running the linter
- [ ] I ran the linter (`dart analyze`) and introduced no new linter warnings
- [ ] I ran the tests (`dart test`) and introduced no new failing tests
- [ ] I used [gitlint](https://github.com/jorisroovers/gitlint) to ensure propper formatting of my commig messages
If you think that your code is ready for a pull request, but you are not sure if it is ready, prefix the PR's title with "WIP: ", so that discussion
can happen there. If you think your PR is ready for review, remove the "WIP: " prefix.

View File

@ -17,16 +17,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1676076353, "lastModified": 1667610399,
"narHash": "sha256-mdUtE8Tp40cZETwcq5tCwwLqkJVV1ULJQ5GKRtbshag=", "narHash": "sha256-XZd0f4ZWAY0QOoUSdiNWj/eFiKb4B9CJPtl9uO9SYY4=",
"owner": "AtaraxiaSjel", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "5deb99bdccbbb97e7562dee4ba8a3ee3021688e6", "rev": "1dd8696f96db47156e1424a49578fe7dd4ce99a4",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "AtaraxiaSjel", "owner": "NixOS",
"ref": "update/flutter", "ref": "nixpkgs-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View File

@ -1,7 +1,7 @@
{ {
description = "moxxmpp"; description = "moxxmpp";
inputs = { inputs = {
nixpkgs.url = "github:AtaraxiaSjel/nixpkgs/update/flutter"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
}; };

View File

@ -18,16 +18,16 @@ class _StanzaSurrogateKey {
/// The tag name of the stanza. /// The tag name of the stanza.
final String tag; final String tag;
@override @override
int get hashCode => sentTo.hashCode ^ id.hashCode ^ tag.hashCode; int get hashCode => sentTo.hashCode ^ id.hashCode ^ tag.hashCode;
@override @override
bool operator ==(Object other) { bool operator==(Object other) {
return other is _StanzaSurrogateKey && return other is _StanzaSurrogateKey &&
other.sentTo == sentTo && other.sentTo == sentTo &&
other.id == id && other.id == id &&
other.tag == tag; other.tag == tag;
} }
} }
@ -71,7 +71,7 @@ class StanzaAwaiter {
final id = stanza.attributes['id'] as String?; final id = stanza.attributes['id'] as String?;
if (id == null) return false; if (id == null) return false;
final key = _StanzaSurrogateKey( final key = _StanzaSurrogateKey(
// Section 8.1.2.1 § 3 of RFC 6120 says that an empty "from" indicates that the // 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. // attribute is implicitly from our own bare JID.

View File

@ -6,27 +6,22 @@ import 'package:xml/xml.dart';
import 'package:xml/xml_events.dart'; import 'package:xml/xml_events.dart';
class XmlStreamBuffer extends StreamTransformerBase<String, XMLNode> { class XmlStreamBuffer extends StreamTransformerBase<String, XMLNode> {
XmlStreamBuffer()
: _streamController = StreamController(), XmlStreamBuffer() : _streamController = StreamController(), _decoder = const XmlNodeDecoder();
_decoder = const XmlNodeDecoder();
final StreamController<XMLNode> _streamController; final StreamController<XMLNode> _streamController;
final XmlNodeDecoder _decoder; final XmlNodeDecoder _decoder;
@override @override
Stream<XMLNode> bind(Stream<String> stream) { Stream<XMLNode> bind(Stream<String> stream) {
stream stream.toXmlEvents().selectSubtreeEvents((event) {
.toXmlEvents() return event.qualifiedName != 'stream:stream';
.selectSubtreeEvents((event) { }).transform(_decoder).listen((nodes) {
return event.qualifiedName != 'stream:stream'; for (final node in nodes) {
}) if (node.nodeType == XmlNodeType.ELEMENT) {
.transform(_decoder) _streamController.add(XMLNode.fromXmlElement(node as XmlElement));
.listen((nodes) { }
for (final node in nodes) { }
if (node.nodeType == XmlNodeType.ELEMENT) { });
_streamController.add(XMLNode.fromXmlElement(node as XmlElement));
}
}
});
return _streamController.stream; return _streamController.stream;
} }
} }

View File

@ -61,26 +61,27 @@ enum StanzaFromType {
/// Nonza describing the XMPP stream header. /// Nonza describing the XMPP stream header.
class StreamHeaderNonza extends XMLNode { class StreamHeaderNonza extends XMLNode {
StreamHeaderNonza(String serverDomain) StreamHeaderNonza(String serverDomain) : super(
: super( tag: 'stream:stream',
tag: 'stream:stream', attributes: <String, String>{
attributes: <String, String>{ 'xmlns': stanzaXmlns,
'xmlns': stanzaXmlns, 'version': '1.0',
'version': '1.0', 'xmlns:stream': streamXmlns,
'xmlns:stream': streamXmlns, 'to': serverDomain,
'to': serverDomain, 'xml:lang': 'en',
'xml:lang': 'en', },
}, closeTag: false,
closeTag: false, );
);
} }
/// The result of an awaited connection. /// The result of an awaited connection.
class XmppConnectionResult { class XmppConnectionResult {
const XmppConnectionResult( const XmppConnectionResult(
this.success, { this.success,
this.error, {
}); this.error,
}
);
/// True if the connection was successful. False if it failed for any reason. /// True if the connection was successful. False if it failed for any reason.
final bool success; final bool success;
@ -95,11 +96,13 @@ class XmppConnection {
XmppConnection( XmppConnection(
ReconnectionPolicy reconnectionPolicy, ReconnectionPolicy reconnectionPolicy,
ConnectivityManager connectivityManager, ConnectivityManager connectivityManager,
this._socket, { this._socket,
this.connectionPingDuration = const Duration(minutes: 3), {
this.connectingTimeout = const Duration(minutes: 2), this.connectionPingDuration = const Duration(minutes: 3),
}) : _reconnectionPolicy = reconnectionPolicy, this.connectingTimeout = const Duration(minutes: 2),
_connectivityManager = connectivityManager { }
) : _reconnectionPolicy = reconnectionPolicy,
_connectivityManager = connectivityManager {
// Allow the reconnection policy to perform reconnections by itself // Allow the reconnection policy to perform reconnections by itself
_reconnectionPolicy.register( _reconnectionPolicy.register(
_attemptReconnection, _attemptReconnection,
@ -112,6 +115,7 @@ class XmppConnection {
_socket.getEventStream().listen(_handleSocketEvent); _socket.getEventStream().listen(_handleSocketEvent);
} }
/// The state that the connection is currently in /// The state that the connection is currently in
XmppConnectionState _connectionState = XmppConnectionState.notConnected; XmppConnectionState _connectionState = XmppConnectionState.notConnected;
@ -124,7 +128,7 @@ class XmppConnection {
/// Connection 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;
/// The class responsible for preventing errors on initial connection due /// The class responsible for preventing errors on initial connection due
@ -133,20 +137,15 @@ class XmppConnection {
/// A helper for handling await semantics with stanzas /// A helper for handling await semantics with stanzas
final StanzaAwaiter _stanzaAwaiter = StanzaAwaiter(); final StanzaAwaiter _stanzaAwaiter = StanzaAwaiter();
/// Sorted list of handlers that we call or incoming and outgoing stanzas /// Sorted list of handlers that we call or incoming and outgoing stanzas
final List<StanzaHandler> _incomingStanzaHandlers = final List<StanzaHandler> _incomingStanzaHandlers = List.empty(growable: true);
List.empty(growable: true); final List<StanzaHandler> _incomingPreStanzaHandlers = List.empty(growable: true);
final List<StanzaHandler> _incomingPreStanzaHandlers = final List<StanzaHandler> _outgoingPreStanzaHandlers = List.empty(growable: true);
List.empty(growable: true); final List<StanzaHandler> _outgoingPostStanzaHandlers = List.empty(growable: true);
final List<StanzaHandler> _outgoingPreStanzaHandlers = final StreamController<XmppEvent> _eventStreamController = StreamController.broadcast();
List.empty(growable: true);
final List<StanzaHandler> _outgoingPostStanzaHandlers =
List.empty(growable: true);
final StreamController<XmppEvent> _eventStreamController =
StreamController.broadcast();
final Map<String, XmppManagerBase> _xmppManagers = {}; final Map<String, XmppManagerBase> _xmppManagers = {};
/// 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);
@ -186,11 +185,10 @@ class XmppConnection {
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators = {}; final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators = {};
XmppFeatureNegotiatorBase? _currentNegotiator; XmppFeatureNegotiatorBase? _currentNegotiator;
final List<XMLNode> _streamFeatures = List.empty(growable: true); 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 = Lock(); final Lock _negotiationLock = Lock();
/// The logger for the class /// The logger for the class
final Logger _log = Logger('XmppConnection'); final Logger _log = Logger('XmppConnection');
@ -202,32 +200,29 @@ class XmppConnection {
/// and does the following: /// and does the following:
/// - if _isConnectionRunning is false, set it to true and return false. /// - if _isConnectionRunning is false, set it to true and return false.
/// - if _isConnectionRunning is true, return true. /// - if _isConnectionRunning is true, return true.
Future<bool> _testAndSetIsConnectionRunning() async => Future<bool> _testAndSetIsConnectionRunning() async => _connectionRunningLock.synchronized(() {
_connectionRunningLock.synchronized(() { if (!_isConnectionRunning) {
if (!_isConnectionRunning) { _isConnectionRunning = true;
_isConnectionRunning = true; return false;
return false; }
}
return true; return true;
}); });
/// Enters the critical section for accessing [XmppConnection._isConnectionRunning] /// Enters the critical section for accessing [XmppConnection._isConnectionRunning]
/// and sets it to false. /// and sets it to false.
Future<void> _resetIsConnectionRunning() async => Future<void> _resetIsConnectionRunning() async => _connectionRunningLock.synchronized(() => _isConnectionRunning = false);
_connectionRunningLock.synchronized(() => _isConnectionRunning = false);
ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy; ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy;
List<String> get serverFeatures => _serverFeatures; List<String> get serverFeatures => _serverFeatures;
bool get isAuthenticated => _isAuthenticated; bool get isAuthenticated => _isAuthenticated;
/// Return the registered feature negotiator that has id [id]. Returns null if /// Return the registered feature negotiator that has id [id]. Returns null if
/// none can be found. /// none can be found.
T? getNegotiatorById<T extends XmppFeatureNegotiatorBase>(String id) => T? getNegotiatorById<T extends XmppFeatureNegotiatorBase>(String id) => _featureNegotiators[id] as T?;
_featureNegotiators[id] as T?;
/// Registers a list of [XmppManagerBase] sub-classes as managers on this connection. /// Registers a list of [XmppManagerBase] sub-classes as managers on this connection.
Future<void> registerManagers(List<XmppManagerBase> managers) async { Future<void> registerManagers(List<XmppManagerBase> managers) async {
for (final manager in managers) { for (final manager in managers) {
@ -252,8 +247,7 @@ class XmppConnection {
_incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers()); _incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers());
_incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers()); _incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers());
_outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers()); _outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers());
_outgoingPostStanzaHandlers _outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers());
.addAll(manager.getOutgoingPostStanzaHandlers());
} }
// Sort them // Sort them
@ -270,7 +264,7 @@ class XmppConnection {
} }
} }
} }
/// Register a list of negotiator with the connection. /// Register a list of negotiator with the connection.
void registerFeatureNegotiators(List<XmppFeatureNegotiatorBase> negotiators) { void registerFeatureNegotiators(List<XmppFeatureNegotiatorBase> negotiators) {
for (final negotiator in negotiators) { for (final negotiator in negotiators) {
@ -302,23 +296,19 @@ class XmppConnection {
// Prevent leaking the last active negotiator // Prevent leaking the last active negotiator
_currentNegotiator = null; _currentNegotiator = null;
} }
/// Generate an Id suitable for an origin-id or stanza id /// Generate an Id suitable for an origin-id or stanza id
String generateId() { String generateId() {
return _uuid.v4(); return _uuid.v4();
} }
/// Returns the Manager with id [id] or null if such a manager is not registered. /// Returns the Manager with id [id] or null if such a manager is not registered.
T? getManagerById<T extends XmppManagerBase>(String id) => T? getManagerById<T extends XmppManagerBase>(String id) => _xmppManagers[id] as T?;
_xmppManagers[id] as T?;
/// A [PresenceManager] is required, so have a wrapper for getting it. /// A [PresenceManager] is required, so have a wrapper for getting it.
/// Returns the registered [PresenceManager]. /// Returns the registered [PresenceManager].
PresenceManager getPresenceManager() { PresenceManager getPresenceManager() {
assert( assert(_xmppManagers.containsKey(presenceManager), 'A PresenceManager is mandatory');
_xmppManagers.containsKey(presenceManager),
'A PresenceManager is mandatory',
);
return getManagerById(presenceManager)!; return getManagerById(presenceManager)!;
} }
@ -326,10 +316,7 @@ class XmppConnection {
/// A [DiscoManager] is required so, have a wrapper for getting it. /// A [DiscoManager] is required so, have a wrapper for getting it.
/// Returns the registered [DiscoManager]. /// Returns the registered [DiscoManager].
DiscoManager getDiscoManager() { DiscoManager getDiscoManager() {
assert( assert(_xmppManagers.containsKey(discoManager), 'A DiscoManager is mandatory');
_xmppManagers.containsKey(discoManager),
'A DiscoManager is mandatory',
);
return getManagerById(discoManager)!; return getManagerById(discoManager)!;
} }
@ -337,14 +324,11 @@ class XmppConnection {
/// A [RosterManager] is required, so have a wrapper for getting it. /// A [RosterManager] is required, so have a wrapper for getting it.
/// Returns the registered [RosterManager]. /// Returns the registered [RosterManager].
RosterManager getRosterManager() { RosterManager getRosterManager() {
assert( assert(_xmppManagers.containsKey(rosterManager), 'A RosterManager is mandatory');
_xmppManagers.containsKey(rosterManager),
'A RosterManager is mandatory',
);
return getManagerById(rosterManager)!; return getManagerById(rosterManager)!;
} }
/// Returns the registered [StreamManagementManager], if one is registered. /// Returns the registered [StreamManagementManager], if one is registered.
StreamManagementManager? getStreamManagementManager() { StreamManagementManager? getStreamManagementManager() {
return getManagerById(smManager); return getManagerById(smManager);
@ -354,7 +338,7 @@ class XmppConnection {
CSIManager? getCSIManager() { CSIManager? getCSIManager() {
return getManagerById(csiManager); return getManagerById(csiManager);
} }
/// Set the connection settings of this connection. /// Set the connection settings of this connection.
void setConnectionSettings(ConnectionSettings settings) { void setConnectionSettings(ConnectionSettings settings) {
_connectionSettings = settings; _connectionSettings = settings;
@ -368,9 +352,7 @@ 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()) { if (await _testAndSetIsConnectionRunning()) {
_log.warning( _log.warning('_attemptReconnection is called but connection attempt is already running. Ignoring...');
'_attemptReconnection is called but connection attempt is already running. Ignoring...',
);
return; return;
} }
@ -387,22 +369,17 @@ class XmppConnection {
_log.finest('Calling connect() from _attemptReconnection'); _log.finest('Calling connect() from _attemptReconnection');
await connect(waitForConnection: true); await connect(waitForConnection: true);
} }
/// Called when a stream ending error has occurred /// Called when a stream ending error has occurred
Future<void> handleError(XmppError error) async { Future<void> handleError(XmppError error) async {
_log.severe('handleError called with ${error.toString()}'); _log.severe('handleError called with ${error.toString()}');
// Whenever we encounter an error that would trigger a reconnection attempt while // Whenever we encounter an error that would trigger a reconnection attempt while
// the connection result is being awaited, don't attempt a reconnection but instead // the connection result is being awaited, don't attempt a reconnection but instead
// try to gracefully disconnect. // try to gracefully disconnect.
if (_connectionCompleter != null) { if (_connectionCompleter != null) {
_log.info( _log.info('Not triggering reconnection since connection result is being awaited');
'Not triggering reconnection since connection result is being awaited', await _disconnect(triggeredByUser: false, state: XmppConnectionState.error);
);
await _disconnect(
triggeredByUser: false,
state: XmppConnectionState.error,
);
_connectionCompleter?.complete( _connectionCompleter?.complete(
XmppConnectionResult( XmppConnectionResult(
false, false,
@ -415,9 +392,7 @@ class XmppConnection {
if (!error.isRecoverable()) { if (!error.isRecoverable()) {
// We cannot recover this error // We cannot recover this error
_log.severe( _log.severe('Since a $error is not recoverable, not attempting a reconnection');
'Since a $error is not recoverable, not attempting a reconnection',
);
await _setConnectionState(XmppConnectionState.error); await _setConnectionState(XmppConnectionState.error);
await _sendEvent( await _sendEvent(
NonRecoverableErrorEvent(error), NonRecoverableErrorEvent(error),
@ -436,14 +411,10 @@ class XmppConnection {
await handleError(SocketError(event)); await handleError(SocketError(event));
} else if (event is XmppSocketClosureEvent) { } else if (event is XmppSocketClosureEvent) {
if (!event.expected) { if (!event.expected) {
_log.fine( _log.fine('Received unexpected XmppSocketClosureEvent. Reconnecting...');
'Received unexpected XmppSocketClosureEvent. Reconnecting...',
);
await handleError(SocketError(XmppSocketErrorEvent(event))); await handleError(SocketError(XmppSocketErrorEvent(event)));
} else { } else {
_log.fine( _log.fine('Received XmppSocketClosureEvent. No reconnection attempt since _socketClosureTriggersReconnect is false...');
'Received XmppSocketClosureEvent. No reconnection attempt since _socketClosureTriggersReconnect is false...',
);
} }
} }
} }
@ -458,9 +429,9 @@ class XmppConnection {
Future<XmppConnectionState> getConnectionState() async { Future<XmppConnectionState> getConnectionState() async {
return _connectionState; return _connectionState;
} }
/// Sends an [XMLNode] without any further processing to the server. /// Sends an [XMLNode] without any further processing to the server.
void sendRawXML(XMLNode node, {String? redact}) { void sendRawXML(XMLNode node, { String? redact }) {
final string = node.toXml(); final string = node.toXml();
_log.finest('==> $string'); _log.finest('==> $string');
_socket.write(string, redact: redact); _socket.write(string, redact: redact);
@ -470,13 +441,15 @@ class XmppConnection {
void sendRawString(String raw) { void sendRawString(String raw) {
_socket.write(raw); _socket.write(raw);
} }
/// Returns true if we can send data through the socket. /// Returns true if we can send data through the socket.
Future<bool> _canSendData() async { Future<bool> _canSendData() async {
return [XmppConnectionState.connected, XmppConnectionState.connecting] return [
.contains(await getConnectionState()); XmppConnectionState.connected,
XmppConnectionState.connecting
].contains(await getConnectionState());
} }
/// Sends a [stanza] to the server. If stream management is enabled, then keeping track /// Sends a [stanza] to the server. If stream management is enabled, then keeping track
/// of the stanza is taken care of. Returns a Future that resolves when we receive a /// of the stanza is taken care of. Returns a Future that resolves when we receive a
/// response to the stanza. /// response to the stanza.
@ -486,43 +459,25 @@ 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( Future<XMLNode> sendStanza(Stanza stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async {
Stanza stanza, { assert(implies(addId == false && stanza.id == null, !awaitable), 'Cannot await a stanza with no id');
StanzaFromType addFrom = StanzaFromType.full,
bool addId = true,
bool awaitable = true,
bool encrypted = false,
bool forceEncryption = false,
}) async {
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; 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());
} }
if (addFrom != StanzaFromType.none && if (addFrom != StanzaFromType.none && (stanza_.from == null || stanza_.from == '')) {
(stanza_.from == null || stanza_.from == '')) {
switch (addFrom) { switch (addFrom) {
case StanzaFromType.full: case StanzaFromType.full: {
{ stanza_ = stanza_.copyWith(from: _connectionSettings.jid.withResource(_resource).toString());
stanza_ = stanza_.copyWith( }
from: _connectionSettings.jid.withResource(_resource).toString(), break;
); case StanzaFromType.bare: {
} stanza_ = stanza_.copyWith(from: _connectionSettings.jid.toBare().toString());
break; }
case StanzaFromType.bare: break;
{ case StanzaFromType.none: break;
stanza_ = stanza_.copyWith(
from: _connectionSettings.jid.toBare().toString(),
);
}
break;
case StanzaFromType.none:
break;
} }
} }
@ -549,16 +504,16 @@ class XmppConnection {
from: data.stanza.to, from: data.stanza.to,
attributes: <String, String>{ attributes: <String, String>{
'type': 'error', 'type': 'error',
...data.stanza.id != null ...data.stanza.id != null ? {
? { 'id': data.stanza.id!,
'id': data.stanza.id!, } : {},
}
: {},
}, },
); );
} }
final prefix = data.encrypted ? '(Encrypted) ' : ''; final prefix = data.encrypted ?
'(Encrypted) ' :
'';
_log.finest('==> $prefix${stanza_.toXml()}'); _log.finest('==> $prefix${stanza_.toXml()}');
final stanzaString = data.stanza.toXml(); final stanzaString = data.stanza.toXml();
@ -617,20 +572,18 @@ class XmppConnection {
_log.finest('Destroying connecting timeout timer...'); _log.finest('Destroying connecting timeout timer...');
} }
} }
/// Sets the connection state to [state] and triggers an event of type /// Sets the connection state to [state] and triggers an event of type
/// [ConnectionStateChangedEvent]. /// [ConnectionStateChangedEvent].
Future<void> _setConnectionState(XmppConnectionState state) async { Future<void> _setConnectionState(XmppConnectionState state) async {
// Ignore changes that are not really changes. // Ignore changes that are not really changes.
if (state == _connectionState) return; if (state == _connectionState) return;
_log.finest('Updating _connectionState from $_connectionState to $state'); _log.finest('Updating _connectionState from $_connectionState to $state');
final oldState = _connectionState; final oldState = _connectionState;
_connectionState = state; _connectionState = state;
final sm = getNegotiatorById<StreamManagementNegotiator>( final sm = getNegotiatorById<StreamManagementNegotiator>(streamManagementNegotiator);
streamManagementNegotiator,
);
await _sendEvent( await _sendEvent(
ConnectionStateChangedEvent( ConnectionStateChangedEvent(
state, state,
@ -638,11 +591,10 @@ class XmppConnection {
sm?.isResumed ?? false, sm?.isResumed ?? false,
), ),
); );
if (state == XmppConnectionState.connected) { if (state == XmppConnectionState.connected) {
_log.finest('Starting _pingConnectionTimer'); _log.finest('Starting _pingConnectionTimer');
_connectionPingTimer = _connectionPingTimer = Timer.periodic(connectionPingDuration, _pingConnectionOpen);
Timer.periodic(connectionPingDuration, _pingConnectionOpen);
// We are connected, so the timer can stop. // We are connected, so the timer can stop.
_destroyConnectingTimer(); _destroyConnectingTimer();
@ -665,7 +617,7 @@ class XmppConnection {
} }
} }
} }
/// Sets the routing state and logs the change /// Sets the routing state and logs the change
void _updateRoutingState(RoutingState state) { void _updateRoutingState(RoutingState state) {
_log.finest('Updating _routingState from $_routingState to $state'); _log.finest('Updating _routingState from $_routingState to $state');
@ -677,12 +629,12 @@ class XmppConnection {
_log.finest('Updating _resource to $resource'); _log.finest('Updating _resource to $resource');
_resource = resource; _resource = resource;
} }
/// Returns the connection's events as a stream. /// Returns the connection's events as a stream.
Stream<XmppEvent> asBroadcastStream() { Stream<XmppEvent> asBroadcastStream() {
return _eventStreamController.stream.asBroadcastStream(); return _eventStreamController.stream.asBroadcastStream();
} }
/// Timer callback to prevent the connection from timing out. /// Timer callback to prevent the connection from timing out.
Future<void> _pingConnectionOpen(Timer timer) async { Future<void> _pingConnectionOpen(Timer timer) async {
// Follow the recommendation of XEP-0198 and just request an ack. If SM is not enabled, // Follow the recommendation of XEP-0198 and just request an ack. If SM is not enabled,
@ -693,20 +645,14 @@ class XmppConnection {
_log.finest('_pingConnectionTimer: Connected. Triggering a ping event.'); _log.finest('_pingConnectionTimer: Connected. Triggering a ping event.');
unawaited(_sendEvent(SendPingEvent())); unawaited(_sendEvent(SendPingEvent()));
} else { } else {
_log.finest( _log.finest('_pingConnectionTimer: Not connected. Not triggering an event.');
'_pingConnectionTimer: Not connected. Not triggering an event.',
);
} }
} }
/// Iterate over [handlers] and check if the handler matches [stanza]. If it does, /// Iterate over [handlers] and check if the handler matches [stanza]. If it does,
/// call its callback and end the processing if the callback returned true; continue /// call its callback and end the processing if the callback returned true; continue
/// if it returned false. /// if it returned false.
Future<StanzaHandlerData> _runStanzaHandlers( Future<StanzaHandlerData> _runStanzaHandlers(List<StanzaHandler> handlers, Stanza stanza, { StanzaHandlerData? initial }) async {
List<StanzaHandler> handlers,
Stanza stanza, {
StanzaHandlerData? initial,
}) async {
var state = initial ?? StanzaHandlerData(false, false, null, stanza); var state = initial ?? StanzaHandlerData(false, false, null, stanza);
for (final handler in handlers) { for (final handler in handlers) {
if (handler.matches(state.stanza)) { if (handler.matches(state.stanza)) {
@ -718,36 +664,19 @@ class XmppConnection {
return state; return state;
} }
Future<StanzaHandlerData> _runIncomingStanzaHandlers( Future<StanzaHandlerData> _runIncomingStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
Stanza stanza, { return _runStanzaHandlers(_incomingStanzaHandlers, stanza, initial: initial);
StanzaHandlerData? initial,
}) async {
return _runStanzaHandlers(
_incomingStanzaHandlers,
stanza,
initial: initial,
);
} }
Future<StanzaHandlerData> _runIncomingPreStanzaHandlers(Stanza stanza) async { Future<StanzaHandlerData> _runIncomingPreStanzaHandlers(Stanza stanza) async {
return _runStanzaHandlers(_incomingPreStanzaHandlers, stanza); return _runStanzaHandlers(_incomingPreStanzaHandlers, stanza);
} }
Future<StanzaHandlerData> _runOutgoingPreStanzaHandlers( Future<StanzaHandlerData> _runOutgoingPreStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
Stanza stanza, { return _runStanzaHandlers(_outgoingPreStanzaHandlers, stanza, initial: initial);
StanzaHandlerData? initial,
}) async {
return _runStanzaHandlers(
_outgoingPreStanzaHandlers,
stanza,
initial: initial,
);
} }
Future<bool> _runOutgoingPostStanzaHandlers( Future<bool> _runOutgoingPostStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async {
Stanza stanza, {
StanzaHandlerData? initial,
}) async {
final data = await _runStanzaHandlers( final data = await _runStanzaHandlers(
_outgoingPostStanzaHandlers, _outgoingPostStanzaHandlers,
stanza, stanza,
@ -755,7 +684,7 @@ class XmppConnection {
); );
return data.done; return data.done;
} }
/// Called whenever we receive a stanza after resource binding or stream resumption. /// Called whenever we receive a stanza after resource binding or stream resumption.
Future<void> _handleStanza(XMLNode nonza) async { Future<void> _handleStanza(XMLNode nonza) async {
// Process nonzas separately // Process nonzas separately
@ -763,12 +692,14 @@ class XmppConnection {
_log.finest('<== ${nonza.toXml()}'); _log.finest('<== ${nonza.toXml()}');
var nonzaHandled = false; var nonzaHandled = false;
await Future.forEach(_xmppManagers.values, await Future.forEach(
(XmppManagerBase manager) async { _xmppManagers.values,
final handled = await manager.runNonzaHandlers(nonza); (XmppManagerBase manager) async {
final handled = await manager.runNonzaHandlers(nonza);
if (!nonzaHandled && handled) nonzaHandled = true; if (!nonzaHandled && handled) nonzaHandled = true;
}); }
);
if (!nonzaHandled) { if (!nonzaHandled) {
_log.warning('Unhandled nonza received: ${nonza.toXml()}'); _log.warning('Unhandled nonza received: ${nonza.toXml()}');
@ -781,10 +712,9 @@ 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 incomingPreHandlers = await _runIncomingPreStanzaHandlers(stanza); final incomingPreHandlers = await _runIncomingPreStanzaHandlers(stanza);
final prefix = incomingPreHandlers.encrypted && final prefix = incomingPreHandlers.encrypted && incomingPreHandlers.other['encryption_error'] == null ?
incomingPreHandlers.other['encryption_error'] == null '(Encrypted) ' :
? '(Encrypted) ' '';
: '';
_log.finest('<== $prefix${incomingPreHandlers.stanza.toXml()}'); _log.finest('<== $prefix${incomingPreHandlers.stanza.toXml()}');
final awaited = await _stanzaAwaiter.onData( final awaited = await _stanzaAwaiter.onData(
@ -815,10 +745,11 @@ class XmppConnection {
/// Returns true if all mandatory features in [features] have been negotiated. /// Returns true if all mandatory features in [features] have been negotiated.
/// Otherwise returns false. /// Otherwise returns false.
bool _isMandatoryNegotiationDone(List<XMLNode> features) { bool _isMandatoryNegotiationDone(List<XMLNode> features) {
return features.every((XMLNode feature) { return features.every(
return feature.firstTag('required') == null && (XMLNode feature) {
feature.tag != 'mechanisms'; return feature.firstTag('required') == null && feature.tag != 'mechanisms';
}); }
);
} }
/// Returns true if we can still negotiate. Returns false if no negotiator is /// Returns true if we can still negotiate. Returns false if no negotiator is
@ -830,23 +761,20 @@ class XmppConnection {
/// Returns the next negotiator that matches [features]. Returns null if none can be /// Returns the next negotiator that matches [features]. Returns null if none can be
/// picked. If [log] is true, then the list of matching negotiators will be logged. /// picked. If [log] is true, then the list of matching negotiators will be logged.
@visibleForTesting @visibleForTesting
XmppFeatureNegotiatorBase? getNextNegotiator( XmppFeatureNegotiatorBase? getNextNegotiator(List<XMLNode> features, {bool log = true}) {
List<XMLNode> features, {
bool log = true,
}) {
final matchingNegotiators = _featureNegotiators.values final matchingNegotiators = _featureNegotiators.values
.where((XmppFeatureNegotiatorBase negotiator) { .where(
return negotiator.state == NegotiatorState.ready && (XmppFeatureNegotiatorBase negotiator) {
negotiator.matchesFeature(features); return negotiator.state == NegotiatorState.ready && negotiator.matchesFeature(features);
}).toList() }
)
.toList()
..sort((a, b) => b.priority.compareTo(a.priority)); ..sort((a, b) => b.priority.compareTo(a.priority));
if (log) { if (log) {
_log.finest( _log.finest('List of matching negotiators: ${matchingNegotiators.map((a) => a.id)}');
'List of matching negotiators: ${matchingNegotiators.map((a) => a.id)}',
);
} }
if (matchingNegotiators.isEmpty) return null; if (matchingNegotiators.isEmpty) return null;
return matchingNegotiators.first; return matchingNegotiators.first;
@ -866,7 +794,7 @@ class XmppConnection {
// Tell consumers of the event stream that we're done with stream feature // Tell consumers of the event stream that we're done with stream feature
// negotiations // negotiations
await _sendEvent(StreamNegotiationsDoneEvent()); await _sendEvent(StreamNegotiationsDoneEvent());
// Send out initial presence // Send out initial presence
await getPresenceManager().sendInitialPresence(); await getPresenceManager().sendInitialPresence();
} }
@ -874,9 +802,7 @@ class XmppConnection {
Future<void> _executeCurrentNegotiator(XMLNode nonza) async { Future<void> _executeCurrentNegotiator(XMLNode nonza) async {
// If we don't have a negotiator get one // If we don't have a negotiator get one
_currentNegotiator ??= getNextNegotiator(_streamFeatures); _currentNegotiator ??= getNextNegotiator(_streamFeatures);
if (_currentNegotiator == null && if (_currentNegotiator == null && _isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
_isMandatoryNegotiationDone(_streamFeatures) &&
!_isNegotiationPossible(_streamFeatures)) {
_log.finest('Negotiations done!'); _log.finest('Negotiations done!');
_updateRoutingState(RoutingState.handleStanzas); _updateRoutingState(RoutingState.handleStanzas);
await _onNegotiationsDone(); await _onNegotiationsDone();
@ -894,73 +820,69 @@ class XmppConnection {
final state = result.get<NegotiatorState>(); final state = result.get<NegotiatorState>();
_currentNegotiator!.state = state; _currentNegotiator!.state = state;
switch (state) { switch (state) {
case NegotiatorState.ready: case NegotiatorState.ready: return;
return;
case NegotiatorState.done: case NegotiatorState.done:
if (_currentNegotiator!.sendStreamHeaderWhenDone) { if (_currentNegotiator!.sendStreamHeaderWhenDone) {
_currentNegotiator = null; _currentNegotiator = null;
_streamFeatures.clear(); _streamFeatures.clear();
_sendStreamHeader(); _sendStreamHeader();
} else { } else {
_streamFeatures.removeWhere((node) { _streamFeatures
return node.attributes['xmlns'] == .removeWhere((node) {
_currentNegotiator!.negotiatingXmlns; return node.attributes['xmlns'] == _currentNegotiator!.negotiatingXmlns;
}); });
_currentNegotiator = null; _currentNegotiator = null;
if (_isMandatoryNegotiationDone(_streamFeatures) && if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
!_isNegotiationPossible(_streamFeatures)) {
_log.finest('Negotiations done!');
_updateRoutingState(RoutingState.handleStanzas);
await _resetIsConnectionRunning();
await _onNegotiationsDone();
} else {
_currentNegotiator = getNextNegotiator(_streamFeatures);
_log.finest('Chose ${_currentNegotiator!.id} as next negotiator');
final fakeStanza = XMLNode(
tag: 'stream:features',
children: _streamFeatures,
);
await _executeCurrentNegotiator(fakeStanza);
}
}
break;
case NegotiatorState.retryLater:
_log.finest('Negotiator wants to continue later. Picking new one...');
_currentNegotiator!.state = NegotiatorState.ready;
if (_isMandatoryNegotiationDone(_streamFeatures) &&
!_isNegotiationPossible(_streamFeatures)) {
_log.finest('Negotiations done!'); _log.finest('Negotiations done!');
_updateRoutingState(RoutingState.handleStanzas); _updateRoutingState(RoutingState.handleStanzas);
await _resetIsConnectionRunning(); await _resetIsConnectionRunning();
await _onNegotiationsDone(); await _onNegotiationsDone();
} else { } else {
_log.finest('Picking new negotiator...');
_currentNegotiator = getNextNegotiator(_streamFeatures); _currentNegotiator = getNextNegotiator(_streamFeatures);
_log.finest('Chose $_currentNegotiator as next negotiator'); _log.finest('Chose ${_currentNegotiator!.id} as next negotiator');
final fakeStanza = XMLNode( final fakeStanza = XMLNode(
tag: 'stream:features', tag: 'stream:features',
children: _streamFeatures, children: _streamFeatures,
); );
await _executeCurrentNegotiator(fakeStanza); await _executeCurrentNegotiator(fakeStanza);
} }
break; }
case NegotiatorState.skipRest: break;
_log.finest( case NegotiatorState.retryLater:
'Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!', _log.finest('Negotiator wants to continue later. Picking new one...');
); _currentNegotiator!.state = NegotiatorState.ready;
if (_isMandatoryNegotiationDone(_streamFeatures) && !_isNegotiationPossible(_streamFeatures)) {
_log.finest('Negotiations done!');
_updateRoutingState(RoutingState.handleStanzas); _updateRoutingState(RoutingState.handleStanzas);
await _resetIsConnectionRunning(); await _resetIsConnectionRunning();
await _onNegotiationsDone(); await _onNegotiationsDone();
break; } else {
_log.finest('Picking new negotiator...');
_currentNegotiator = getNextNegotiator(_streamFeatures);
_log.finest('Chose $_currentNegotiator as next negotiator');
final fakeStanza = XMLNode(
tag: 'stream:features',
children: _streamFeatures,
);
await _executeCurrentNegotiator(fakeStanza);
}
break;
case NegotiatorState.skipRest:
_log.finest('Negotiator wants to skip the remaining negotiation... Negotiations (assumed) done!');
_updateRoutingState(RoutingState.handleStanzas);
await _resetIsConnectionRunning();
await _onNegotiationsDone();
break;
} }
} }
/// Called whenever we receive data that has been parsed as XML. /// Called whenever we receive data that has been parsed as XML.
Future<void> handleXmlStream(XMLNode node) async { Future<void> handleXmlStream(XMLNode node) async {
// Check if we received a stream error // Check if we received a stream error
@ -969,7 +891,7 @@ class XmppConnection {
..finest('<== ${node.toXml()}') ..finest('<== ${node.toXml()}')
..severe('Received a stream error! Attempting reconnection'); ..severe('Received a stream error! Attempting reconnection');
await handleError(StreamError()); await handleError(StreamError());
return; return;
} }
@ -998,7 +920,7 @@ class XmppConnection {
await _executeCurrentNegotiator(node); await _executeCurrentNegotiator(node);
}); });
break; break;
case RoutingState.handleStanzas: case RoutingState.handleStanzas:
await _handleStanza(node); await _handleStanza(node);
break; break;
case RoutingState.preConnection: case RoutingState.preConnection:
@ -1012,27 +934,23 @@ class XmppConnection {
void sendWhitespacePing() { void sendWhitespacePing() {
_socket.write(''); _socket.write('');
} }
/// Sends an event to the connection's event stream. /// Sends an event to the connection's event stream.
Future<void> _sendEvent(XmppEvent event) async { Future<void> _sendEvent(XmppEvent event) async {
_log.finest('Event: ${event.toString()}'); _log.finest('Event: ${event.toString()}');
// Specific event handling // Specific event handling
if (event is ResourceBindingSuccessEvent) { if (event is ResourceBindingSuccessEvent) {
_log.finest( _log.finest('Received ResourceBindingSuccessEvent. Setting _resource to ${event.resource}');
'Received ResourceBindingSuccessEvent. Setting _resource to ${event.resource}',
);
setResource(event.resource); setResource(event.resource);
_log.finest('Resetting _serverFeatures'); _log.finest('Resetting _serverFeatures');
_serverFeatures.clear(); _serverFeatures.clear();
} else if (event is AuthenticationSuccessEvent) { } else if (event is AuthenticationSuccessEvent) {
_log.finest( _log.finest('Received AuthenticationSuccessEvent. Setting _isAuthenticated to true');
'Received AuthenticationSuccessEvent. Setting _isAuthenticated to true',
);
_isAuthenticated = true; _isAuthenticated = true;
} }
for (final manager in _xmppManagers.values) { for (final manager in _xmppManagers.values) {
await manager.onXmppEvent(event); await manager.onXmppEvent(event);
} }
@ -1045,7 +963,9 @@ class XmppConnection {
_socket.write( _socket.write(
XMLNode( XMLNode(
tag: 'xml', tag: 'xml',
attributes: <String, String>{'version': '1.0'}, attributes: <String, String>{
'version': '1.0'
},
closeTag: false, closeTag: false,
isDeclaration: true, isDeclaration: true,
children: [ children: [
@ -1067,10 +987,7 @@ class XmppConnection {
await _disconnect(state: XmppConnectionState.notConnected); await _disconnect(state: XmppConnectionState.notConnected);
} }
Future<void> _disconnect({ Future<void> _disconnect({required XmppConnectionState state, bool triggeredByUser = true}) async {
required XmppConnectionState state,
bool triggeredByUser = true,
}) async {
await _reconnectionPolicy.setShouldReconnect(false); await _reconnectionPolicy.setShouldReconnect(false);
if (triggeredByUser) { if (triggeredByUser) {
@ -1091,33 +1008,18 @@ class XmppConnection {
await getStreamManagementManager()?.resetState(); await getStreamManagementManager()?.resetState();
} }
} }
/// Make sure that all required managers are registered /// Make sure that all required managers are registered
void _runPreConnectionAssertions() { void _runPreConnectionAssertions() {
assert( assert(_xmppManagers.containsKey(presenceManager), 'A PresenceManager is mandatory');
_xmppManagers.containsKey(presenceManager), assert(_xmppManagers.containsKey(rosterManager), 'A RosterManager is mandatory');
'A PresenceManager is mandatory', assert(_xmppManagers.containsKey(discoManager), 'A DiscoManager is mandatory');
); assert(_xmppManagers.containsKey(pingManager), 'A PingManager is mandatory');
assert(
_xmppManagers.containsKey(rosterManager),
'A RosterManager is mandatory',
);
assert(
_xmppManagers.containsKey(discoManager),
'A DiscoManager is mandatory',
);
assert(
_xmppManagers.containsKey(pingManager),
'A PingManager is mandatory',
);
} }
/// 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({ Future<XmppConnectionResult> connectAwaitable({ String? lastResource, bool waitForConnection = false }) async {
String? lastResource,
bool waitForConnection = false,
}) async {
_runPreConnectionAssertions(); _runPreConnectionAssertions();
await _resetIsConnectionRunning(); await _resetIsConnectionRunning();
_connectionCompleter = Completer(); _connectionCompleter = Completer();
@ -1129,24 +1031,17 @@ class XmppConnection {
); );
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({ Future<void> connect({ String? lastResource, bool waitForConnection = false, bool shouldReconnect = true }) async {
String? lastResource, if (_connectionState != XmppConnectionState.notConnected && _connectionState != XmppConnectionState.error) {
bool waitForConnection = false, _log.fine('Cancelling this connection attempt as one appears to be already running.');
bool shouldReconnect = true,
}) async {
if (_connectionState != XmppConnectionState.notConnected &&
_connectionState != XmppConnectionState.error) {
_log.fine(
'Cancelling this connection attempt as one appears to be already running.',
);
return; return;
} }
_runPreConnectionAssertions(); _runPreConnectionAssertions();
await _resetIsConnectionRunning(); await _resetIsConnectionRunning();
if (lastResource != null) { if (lastResource != null) {
setResource(lastResource); setResource(lastResource);
} }
@ -1164,7 +1059,7 @@ class XmppConnection {
await _connectivityManager.waitForConnection(); await _connectivityManager.waitForConnection();
_log.info('Got okay from connectivityManager'); _log.info('Got okay from connectivityManager');
} }
final smManager = getStreamManagementManager(); final smManager = getStreamManagementManager();
String? host; String? host;
int? port; int? port;

View File

@ -30,7 +30,7 @@ class ConnectionStateChangedEvent extends XmppEvent {
/// Triggered when we encounter a stream error. /// Triggered when we encounter a stream error.
class StreamErrorEvent extends XmppEvent { class StreamErrorEvent extends XmppEvent {
StreamErrorEvent({required this.error}); StreamErrorEvent({ required this.error });
final String error; final String error;
} }
@ -48,7 +48,7 @@ class SendPingEvent extends XmppEvent {}
/// Triggered when the stream resumption was successful /// Triggered when the stream resumption was successful
class StreamResumedEvent extends XmppEvent { class StreamResumedEvent extends XmppEvent {
StreamResumedEvent({required this.h}); StreamResumedEvent({ required this.h });
final int h; final int h;
} }
@ -128,7 +128,7 @@ class MessageEvent extends XmppEvent {
/// Triggered when a client responds to our delivery receipt request /// Triggered when a client responds to our delivery receipt request
class DeliveryReceiptReceivedEvent extends XmppEvent { class DeliveryReceiptReceivedEvent extends XmppEvent {
DeliveryReceiptReceivedEvent({required this.from, required this.id}); DeliveryReceiptReceivedEvent({ required this.from, required this.id });
final JID from; final JID from;
final String id; final String id;
} }
@ -147,9 +147,9 @@ class ChatMarkerEvent extends XmppEvent {
// Triggered when we received a Stream resumption ID // Triggered when we received a Stream resumption ID
class StreamManagementEnabledEvent extends XmppEvent { class StreamManagementEnabledEvent extends XmppEvent {
StreamManagementEnabledEvent({ StreamManagementEnabledEvent({
required this.resource, required this.resource,
this.id, this.id,
this.location, this.location,
}); });
final String resource; final String resource;
final String? id; final String? id;
@ -158,7 +158,7 @@ class StreamManagementEnabledEvent extends XmppEvent {
/// Triggered when we bound a resource /// Triggered when we bound a resource
class ResourceBindingSuccessEvent extends XmppEvent { class ResourceBindingSuccessEvent extends XmppEvent {
ResourceBindingSuccessEvent({required this.resource}); ResourceBindingSuccessEvent({ required this.resource });
final String resource; final String resource;
} }
@ -182,17 +182,13 @@ class ServerItemDiscoEvent extends XmppEvent {
/// Triggered when we receive a subscription request /// Triggered when we receive a subscription request
class SubscriptionRequestReceivedEvent extends XmppEvent { class SubscriptionRequestReceivedEvent extends XmppEvent {
SubscriptionRequestReceivedEvent({required this.from}); SubscriptionRequestReceivedEvent({ required this.from });
final JID from; final JID from;
} }
/// Triggered when we receive a new or updated avatar /// Triggered when we receive a new or updated avatar
class AvatarUpdatedEvent extends XmppEvent { class AvatarUpdatedEvent extends XmppEvent {
AvatarUpdatedEvent({ AvatarUpdatedEvent({ required this.jid, required this.base64, required this.hash });
required this.jid,
required this.base64,
required this.hash,
});
final String jid; final String jid;
final String base64; final String base64;
final String hash; final String hash;
@ -200,7 +196,7 @@ class AvatarUpdatedEvent extends XmppEvent {
/// Triggered when a PubSub notification has been received /// Triggered when a PubSub notification has been received
class PubSubNotificationEvent extends XmppEvent { class PubSubNotificationEvent extends XmppEvent {
PubSubNotificationEvent({required this.item, required this.from}); PubSubNotificationEvent({ required this.item, required this.from });
final PubSubItem item; final PubSubItem item;
final String from; final String from;
} }
@ -213,13 +209,13 @@ class StanzaAckedEvent extends XmppEvent {
/// Triggered when receiving a push of the blocklist /// Triggered when receiving a push of the blocklist
class BlocklistBlockPushEvent extends XmppEvent { class BlocklistBlockPushEvent extends XmppEvent {
BlocklistBlockPushEvent({required this.items}); BlocklistBlockPushEvent({ required this.items });
final List<String> items; final List<String> items;
} }
/// Triggered when receiving a push of the blocklist /// Triggered when receiving a push of the blocklist
class BlocklistUnblockPushEvent extends XmppEvent { class BlocklistUnblockPushEvent extends XmppEvent {
BlocklistUnblockPushEvent({required this.items}); BlocklistUnblockPushEvent({ required this.items });
final List<String> items; final List<String> items;
} }
@ -246,7 +242,7 @@ class OmemoDeviceListUpdatedEvent extends XmppEvent {
/// error. /// error.
class NonRecoverableErrorEvent extends XmppEvent { class NonRecoverableErrorEvent extends XmppEvent {
NonRecoverableErrorEvent(this.error); NonRecoverableErrorEvent(this.error);
/// The error in question. /// The error in question.
final XmppError error; final XmppError error;
} }

View File

@ -5,10 +5,7 @@ import 'package:moxxmpp/src/stanza.dart';
/// Bounce a stanza if it was not handled by any manager. [conn] is the connection object /// Bounce a stanza if it was not handled by any manager. [conn] is the connection object
/// to use for sending the stanza. [data] is the StanzaHandlerData of the unhandled /// to use for sending the stanza. [data] is the StanzaHandlerData of the unhandled
/// stanza. /// stanza.
Future<void> handleUnhandledStanza( Future<void> handleUnhandledStanza(XmppConnection conn, StanzaHandlerData data) async {
XmppConnection conn,
StanzaHandlerData data,
) async {
if (data.stanza.type != 'error' && data.stanza.type != 'result') { if (data.stanza.type != 'error' && data.stanza.type != 'result') {
final stanza = data.stanza.copyWith( final stanza = data.stanza.copyWith(
to: data.stanza.from, to: data.stanza.from,

View File

@ -18,10 +18,7 @@ class JID {
} else { } else {
resourcePart = slashParts.sublist(1).join('/'); resourcePart = slashParts.sublist(1).join('/');
assert( assert(resourcePart.isNotEmpty, 'Resource part cannot be there and empty');
resourcePart.isNotEmpty,
'Resource part cannot be there and empty',
);
} }
final atParts = slashParts.first.split('@'); final atParts = slashParts.first.split('@');
@ -37,9 +34,9 @@ class JID {
return JID( return JID(
localPart, localPart,
domainPart.endsWith('.') domainPart.endsWith('.') ?
? domainPart.substring(0, domainPart.length - 1) domainPart.substring(0, domainPart.length - 1) :
: domainPart, domainPart,
resourcePart, resourcePart,
); );
} }
@ -56,7 +53,7 @@ class JID {
/// Converts the JID into a bare JID. /// Converts the JID into a bare JID.
JID toBare() { JID toBare() {
if (isBare()) return this; if (isBare()) return this;
return JID(local, domain, ''); return JID(local, domain, '');
} }
@ -66,12 +63,12 @@ class JID {
/// Compares the JID with [other]. This function assumes that JID and [other] /// 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] /// 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. /// is optionally set to true, then [other] MUST be bare. Otherwise, false is returned.
bool bareCompare(JID other, {bool ensureBare = false}) { bool bareCompare(JID other, { bool ensureBare = false }) {
if (ensureBare && !other.isBare()) return false; if (ensureBare && !other.isBare()) return false;
return local == other.local && domain == other.domain; return local == other.local && domain == other.domain;
} }
/// Converts to JID instance into its string representation of /// Converts to JID instance into its string representation of
/// localpart@domainpart/resource. /// localpart@domainpart/resource.
@override @override
@ -93,9 +90,7 @@ class JID {
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
if (other is JID) { if (other is JID) {
return other.local == local && return other.local == local && other.domain == domain && other.resource == resource;
other.domain == domain &&
other.resource == resource;
} }
return false; return false;

View File

@ -22,16 +22,8 @@ class XmppManagerAttributes {
required this.getConnection, required this.getConnection,
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( final Future<XMLNode> Function(Stanza stanza, { StanzaFromType addFrom, bool addId, bool awaitable, bool encrypted, bool forceEncryption}) sendStanza;
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;
@ -47,7 +39,7 @@ class XmppManagerAttributes {
/// Returns true if a server feature is supported /// Returns true if a server feature is supported
final bool Function(String) isFeatureSupported; final bool Function(String) isFeatureSupported;
/// Returns the full JID of the current account /// Returns the full JID of the current account
final JID Function() getFullJID; final JID Function() getFullJID;
@ -57,6 +49,5 @@ class XmppManagerAttributes {
/// Return the [XmppConnection] the manager is registered against. /// Return the [XmppConnection] the manager is registered against.
final XmppConnection Function() getConnection; final XmppConnection Function() getConnection;
final T? Function<T extends XmppFeatureNegotiatorBase>(String) final T? Function<T extends XmppFeatureNegotiatorBase>(String) getNegotiatorById;
getNegotiatorById;
} }

View File

@ -18,13 +18,13 @@ abstract class XmppManagerBase {
/// Flag indicating that the post registration callback has been called once. /// Flag indicating that the post registration callback has been called once.
bool initialized = false; 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(name); _log = Logger(name);
} }
/// Returns the attributes that are registered with the manager. /// Returns the attributes that are registered with the manager.
/// Must only be called after register has been called on it. /// Must only be called after register has been called on it.
XmppManagerAttributes getAttributes() { XmppManagerAttributes getAttributes() {
@ -40,7 +40,7 @@ abstract class XmppManagerBase {
/// send. These are run after the stanza is sent. The higher the value of the /// send. These are run after the stanza is sent. The higher the value of the
/// handler's priority, the earlier it is run. /// 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. The higher the value of the /// receive. The higher the value of the
/// handler's priority, the earlier it is run. /// handler's priority, the earlier it is run.
@ -51,7 +51,7 @@ abstract class XmppManagerBase {
/// as we have to decrypt the stanza before we do anything else. The higher the value /// 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. /// of the handler's priority, the earlier it is run.
List<StanzaHandler> getIncomingPreStanzaHandlers() => []; List<StanzaHandler> getIncomingPreStanzaHandlers() => [];
/// Return the NonzaHandlers associated with this manager. The higher the value of the /// Return the NonzaHandlers associated with this manager. The higher the value of the
/// handler's priority, the earlier it is run. /// handler's priority, the earlier it is run.
List<NonzaHandler> getNonzaHandlers() => []; List<NonzaHandler> getNonzaHandlers() => [];
@ -61,16 +61,16 @@ abstract class XmppManagerBase {
/// Return a list of identities that should be included in a disco response. /// Return a list of identities that should be included in a disco response.
List<Identity> getDiscoIdentities() => []; List<Identity> getDiscoIdentities() => [];
/// Return the Id (akin to xmlns) of this manager. /// Return the Id (akin to xmlns) of this manager.
final String id; final String id;
/// The name of the manager. /// The name of the manager.
String get name => toString(); String get name => toString();
/// Return the logger for this manager. /// Return the logger for this manager.
Logger get logger => _log; Logger get logger => _log;
/// Called when XmppConnection triggers an event /// Called when XmppConnection triggers an event
Future<void> onXmppEvent(XmppEvent event) async {} Future<void> onXmppEvent(XmppEvent event) async {}
@ -94,17 +94,20 @@ abstract class XmppManagerBase {
} }
} }
} }
/// 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.
Future<bool> runNonzaHandlers(XMLNode nonza) async { Future<bool> runNonzaHandlers(XMLNode nonza) async {
var handled = false; var handled = false;
await Future.forEach(getNonzaHandlers(), (NonzaHandler handler) async { await Future.forEach(
if (handler.matches(nonza)) { getNonzaHandlers(),
handled = true; (NonzaHandler handler) async {
await handler.callback(nonza); if (handler.matches(nonza)) {
handled = true;
await handler.callback(nonza);
}
} }
}); );
return handled; return handled;
} }
@ -113,25 +116,17 @@ abstract class XmppManagerBase {
/// for plugins to reset their cache in case of a new stream. /// for plugins to reset their cache in case of a new stream.
/// The value only makes sense after receiving a StreamNegotiationsDoneEvent. /// The value only makes sense after receiving a StreamNegotiationsDoneEvent.
Future<bool> isNewStream() async { Future<bool> isNewStream() async {
final sm = final sm = getAttributes().getManagerById<StreamManagementManager>(smManager);
getAttributes().getManagerById<StreamManagementManager>(smManager);
return sm?.streamResumed == false; return sm?.streamResumed == false;
} }
/// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's /// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's
/// children with [children]. /// children with [children].
/// ///
/// Note that this function currently only accepts IQ stanzas. /// Note that this function currently only accepts IQ stanzas.
Future<void> reply( Future<void> reply(StanzaHandlerData data, String type, List<XMLNode> children) async {
StanzaHandlerData data, assert(data.stanza.tag == 'iq', 'Reply makes little sense for non-IQ stanzas');
String type,
List<XMLNode> children,
) async {
assert(
data.stanza.tag == 'iq',
'Reply makes little sense for non-IQ stanzas',
);
final stanza = data.stanza.copyWith( final stanza = data.stanza.copyWith(
to: data.stanza.from, to: data.stanza.from,

View File

@ -27,48 +27,50 @@ class StanzaHandlerData with _$StanzaHandlerData {
dynamic cancelReason, dynamic cancelReason,
// The stanza that is being dealt with. SHOULD NOT be overwritten, unless it is absolutely // The stanza that is being dealt with. SHOULD NOT be overwritten, unless it is absolutely
// necessary, e.g. with Message Carbons or OMEMO // necessary, e.g. with Message Carbons or OMEMO
Stanza stanza, { Stanza stanza,
// Whether the stanza is retransmitted. Only useful in the context of outgoing {
// stanza handlers. MUST NOT be overwritten. // Whether the stanza is retransmitted. Only useful in the context of outgoing
@Default(false) bool retransmitted, // stanza handlers. MUST NOT be overwritten.
StatelessMediaSharingData? sims, @Default(false) bool retransmitted,
StatelessFileSharingData? sfs, StatelessMediaSharingData? sims,
OOBData? oob, StatelessFileSharingData? sfs,
StableStanzaId? stableId, OOBData? oob,
ReplyData? reply, StableStanzaId? stableId,
ChatState? chatState, ReplyData? reply,
@Default(false) bool isCarbon, ChatState? chatState,
@Default(false) bool deliveryReceiptRequested, @Default(false) bool isCarbon,
@Default(false) bool isMarkable, @Default(false) bool deliveryReceiptRequested,
// File Upload Notifications @Default(false) bool isMarkable,
// A notification // File Upload Notifications
FileMetadataData? fun, // A notification
// The stanza id this replaces FileMetadataData? fun,
String? funReplacement, // The stanza id this replaces
// The stanza id this cancels String? funReplacement,
String? funCancellation, // The stanza id this cancels
// Whether the stanza was received encrypted String? funCancellation,
@Default(false) bool encrypted, // Whether the stanza was received encrypted
// If true, forces the encryption manager to encrypt to the JID, even if it @Default(false) bool encrypted,
// would not normally. In the case of OMEMO: If shouldEncrypt returns false // If true, forces the encryption manager to encrypt to the JID, even if it
// but forceEncryption is true, then the OMEMO manager will try to encrypt // would not normally. In the case of OMEMO: If shouldEncrypt returns false
// to the JID anyway. // but forceEncryption is true, then the OMEMO manager will try to encrypt
@Default(false) bool forceEncryption, // to the JID anyway.
// The stated type of encryption used, if any was used @Default(false) bool forceEncryption,
ExplicitEncryptionType? encryptionType, // The stated type of encryption used, if any was used
// Delayed Delivery ExplicitEncryptionType? encryptionType,
DelayedDelivery? delayedDelivery, // Delayed Delivery
// This is for stanza handlers that are not part of the XMPP library but still need DelayedDelivery? delayedDelivery,
// pass data around. // This is for stanza handlers that are not part of the XMPP library but still need
@Default(<String, dynamic>{}) Map<String, dynamic> other, // pass data around.
// If non-null, then it indicates the origin Id of the message that should be @Default(<String, dynamic>{}) Map<String, dynamic> other,
// retracted // If non-null, then it indicates the origin Id of the message that should be
MessageRetractionData? messageRetraction, // retracted
// If non-null, then the message is a correction for the specified stanza Id MessageRetractionData? messageRetraction,
String? lastMessageCorrectionSid, // If non-null, then the message is a correction for the specified stanza Id
// Reactions data String? lastMessageCorrectionSid,
MessageReactions? messageReactions, // Reactions data
// The Id of the sticker pack this sticker belongs to MessageReactions? messageReactions,
String? stickerPackId, // The Id of the sticker pack this sticker belongs to
}) = _StanzaHandlerData; String? stickerPackId,
}
) = _StanzaHandlerData;
} }

View File

@ -5,7 +5,7 @@ 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;
final bool matchStanzas; final bool matchStanzas;
@ -19,12 +19,11 @@ abstract class Handler {
} }
if (nonzaXmlns != null && nonzaTag != null) { if (nonzaXmlns != null && nonzaTag != null) {
matches = (node.attributes['xmlns'] ?? '') == nonzaXmlns! && matches = (node.attributes['xmlns'] ?? '') == nonzaXmlns! && node.tag == nonzaTag!;
node.tag == nonzaTag!;
} }
if (matchStanzas && nonzaTag == null) { if (matchStanzas && nonzaTag == null) {
matches = ['iq', 'presence', 'message'].contains(node.tag); matches = [ 'iq', 'presence', 'message' ].contains(node.tag);
} }
return matches; return matches;
@ -33,42 +32,42 @@ abstract class Handler {
class NonzaHandler extends Handler { class NonzaHandler extends Handler {
NonzaHandler({ NonzaHandler({
required this.callback, required this.callback,
String? nonzaTag, String? nonzaTag,
String? nonzaXmlns, String? nonzaXmlns,
}) : super( }) : super(
false, false,
nonzaTag: nonzaTag, nonzaTag: nonzaTag,
nonzaXmlns: nonzaXmlns, nonzaXmlns: nonzaXmlns,
); );
final Future<bool> Function(XMLNode) callback; final Future<bool> Function(XMLNode) callback;
} }
class StanzaHandler extends Handler { class StanzaHandler extends Handler {
StanzaHandler({ StanzaHandler({
required this.callback, required this.callback,
this.tagXmlns, this.tagXmlns,
this.tagName, this.tagName,
this.priority = 0, this.priority = 0,
String? stanzaTag, String? stanzaTag,
}) : super( }) : super(
true, true,
nonzaTag: stanzaTag, nonzaTag: stanzaTag,
nonzaXmlns: stanzaXmlns, nonzaXmlns: stanzaXmlns,
); );
final String? tagName; final String? tagName;
final String? tagXmlns; final String? tagXmlns;
final int priority; final int priority;
final Future<StanzaHandlerData> Function(Stanza, StanzaHandlerData) callback; final Future<StanzaHandlerData> Function(Stanza, StanzaHandlerData) callback;
@override @override
bool matches(XMLNode node) { bool matches(XMLNode node) {
var matches = super.matches(node); var matches = super.matches(node);
if (matches == false) { if (matches == false) {
return false; return false;
} }
if (tagName != null) { if (tagName != null) {
final firstTag = node.firstTag(tagName!, xmlns: tagXmlns); final firstTag = node.firstTag(tagName!, xmlns: tagXmlns);
@ -76,19 +75,16 @@ class StanzaHandler extends Handler {
} else if (tagXmlns != null) { } else if (tagXmlns != null) {
return listContains( return listContains(
node.children, node.children,
(XMLNode node_) => (XMLNode node_) => node_.attributes.containsKey('xmlns') && node_.attributes['xmlns'] == tagXmlns,
node_.attributes.containsKey('xmlns') &&
node_.attributes['xmlns'] == tagXmlns,
); );
} }
if (tagName == null && tagXmlns == null) { if (tagName == null && tagXmlns == null) {
matches = true; matches = true;
} }
return matches; return matches;
} }
} }
int stanzaHandlerSortComparator(StanzaHandler a, StanzaHandler b) => int stanzaHandlerSortComparator(StanzaHandler a, StanzaHandler b) => b.priority.compareTo(a.priority);
b.priority.compareTo(a.priority);

View File

@ -10,8 +10,7 @@ const pubsubManager = 'org.moxxmpp.pubsubmanager';
const userAvatarManager = 'org.moxxmpp.useravatarmanager'; const userAvatarManager = 'org.moxxmpp.useravatarmanager';
const stableIdManager = 'org.moxxmpp.stableidmanager'; const stableIdManager = 'org.moxxmpp.stableidmanager';
const simsManager = 'org.moxxmpp.simsmanager'; const simsManager = 'org.moxxmpp.simsmanager';
const messageDeliveryReceiptManager = const messageDeliveryReceiptManager = 'org.moxxmpp.messagedeliveryreceiptmanager';
'org.moxxmpp.messagedeliveryreceiptmanager';
const chatMarkerManager = 'org.moxxmpp.chatmarkermanager'; const chatMarkerManager = 'org.moxxmpp.chatmarkermanager';
const oobManager = 'org.moxxmpp.oobmanager'; const oobManager = 'org.moxxmpp.oobmanager';
const sfsManager = 'org.moxxmpp.sfsmanager'; const sfsManager = 'org.moxxmpp.sfsmanager';
@ -20,8 +19,7 @@ const blockingManager = 'org.moxxmpp.blockingmanager';
const httpFileUploadManager = 'org.moxxmpp.httpfileuploadmanager'; const httpFileUploadManager = 'org.moxxmpp.httpfileuploadmanager';
const chatStateManager = 'org.moxxmpp.chatstatemanager'; const chatStateManager = 'org.moxxmpp.chatstatemanager';
const pingManager = 'org.moxxmpp.ping'; const pingManager = 'org.moxxmpp.ping';
const fileUploadNotificationManager = const fileUploadNotificationManager = 'org.moxxmpp.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';

View File

@ -80,58 +80,54 @@ class MessageManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onMessage, callback: _onMessage,
priority: -100, priority: -100,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza _, StanzaHandlerData state) async {
Stanza _,
StanzaHandlerData state,
) async {
final message = state.stanza; final message = state.stanza;
final body = message.firstTag('body'); final body = message.firstTag('body');
final hints = List<MessageProcessingHint>.empty(growable: true); final hints = List<MessageProcessingHint>.empty(growable: true);
for (final element for (final element in message.findTagsByXmlns(messageProcessingHintsXmlns)) {
in message.findTagsByXmlns(messageProcessingHintsXmlns)) { hints.add(messageProcessingHintFromXml(element));
hints.add(messageProcessingHintFromXml(element));
} }
getAttributes().sendEvent( getAttributes().sendEvent(MessageEvent(
MessageEvent( body: body != null ? body.innerText() : '',
body: body != null ? body.innerText() : '', fromJid: JID.fromString(message.attributes['from']! as String),
fromJid: JID.fromString(message.attributes['from']! as String), toJid: JID.fromString(message.attributes['to']! as String),
toJid: JID.fromString(message.attributes['to']! as String), sid: message.attributes['id']! as String,
sid: message.attributes['id']! as String, stanzaId: state.stableId ?? const StableStanzaId(),
stanzaId: state.stableId ?? const StableStanzaId(), isCarbon: state.isCarbon,
isCarbon: state.isCarbon, deliveryReceiptRequested: state.deliveryReceiptRequested,
deliveryReceiptRequested: state.deliveryReceiptRequested, isMarkable: state.isMarkable,
isMarkable: state.isMarkable, type: message.attributes['type'] as String?,
type: message.attributes['type'] as String?, oob: state.oob,
oob: state.oob, sfs: state.sfs,
sfs: state.sfs, sims: state.sims,
sims: state.sims, reply: state.reply,
reply: state.reply, chatState: state.chatState,
chatState: state.chatState, fun: state.fun,
fun: state.fun, funReplacement: state.funReplacement,
funReplacement: state.funReplacement, funCancellation: state.funCancellation,
funCancellation: state.funCancellation, encrypted: state.encrypted,
encrypted: state.encrypted, messageRetraction: state.messageRetraction,
messageRetraction: state.messageRetraction, messageCorrectionId: state.lastMessageCorrectionSid,
messageCorrectionId: state.lastMessageCorrectionSid, messageReactions: state.messageReactions,
messageReactions: state.messageReactions, messageProcessingHints: hints.isEmpty ?
messageProcessingHints: hints.isEmpty ? null : hints, null :
stickerPackId: state.stickerPackId, hints,
other: state.other, stickerPackId: state.stickerPackId,
error: StanzaError.fromStanza(message), other: state.other,
), error: StanzaError.fromStanza(message),
); ),);
return state.copyWith(done: true); return state.copyWith(done: true);
} }
@ -143,10 +139,7 @@ class MessageManager extends XmppManagerBase {
/// 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( assert(
implies( implies(details.quoteBody != null, details.quoteFrom != null && details.quoteId != null),
details.quoteBody != null,
details.quoteFrom != null && details.quoteId != null,
),
'When quoting a message, then quoteFrom and quoteId must also be non-null', 'When quoting a message, then quoteFrom and quoteId must also be non-null',
); );
@ -159,7 +152,7 @@ class MessageManager extends XmppManagerBase {
if (details.quoteBody != null) { if (details.quoteBody != null) {
final quote = QuoteData.fromBodies(details.quoteBody!, details.body!); final quote = QuoteData.fromBodies(details.quoteBody!, details.body!);
stanza stanza
..addChild( ..addChild(
XMLNode(tag: 'body', text: quote.body), XMLNode(tag: 'body', text: quote.body),
@ -168,14 +161,19 @@ class MessageManager extends XmppManagerBase {
XMLNode.xmlns( XMLNode.xmlns(
tag: 'reply', tag: 'reply',
xmlns: replyXmlns, xmlns: replyXmlns,
attributes: {'to': details.quoteFrom!, 'id': details.quoteId!}, attributes: {
'to': details.quoteFrom!,
'id': details.quoteId!
},
), ),
) )
..addChild( ..addChild(
XMLNode.xmlns( XMLNode.xmlns(
tag: 'fallback', tag: 'fallback',
xmlns: fallbackXmlns, xmlns: fallbackXmlns,
attributes: {'for': replyXmlns}, attributes: {
'for': replyXmlns
},
children: [ children: [
XMLNode( XMLNode(
tag: 'body', tag: 'body',
@ -222,20 +220,16 @@ class MessageManager extends XmppManagerBase {
stanza.addChild(details.sfs!.toXML()); stanza.addChild(details.sfs!.toXML());
final source = details.sfs!.sources.first; final source = details.sfs!.sources.first;
if (source is StatelessFileSharingUrlSource && if (source is StatelessFileSharingUrlSource && details.setOOBFallbackBody) {
details.setOOBFallbackBody) {
// SFS recommends OOB as a fallback // SFS recommends OOB as a fallback
stanza.addChild(constructOOBNode(OOBData(url: source.url))); stanza.addChild(constructOOBNode(OOBData(url: source.url)));
} }
} }
if (details.chatState != null) { if (details.chatState != null) {
stanza.addChild( stanza.addChild(
// TODO(Unknown): Move this into xep_0085.dart // TODO(Unknown): Move this into xep_0085.dart
XMLNode.xmlns( XMLNode.xmlns(tag: chatStateToString(details.chatState!), xmlns: chatStateXmlns),
tag: chatStateToString(details.chatState!),
xmlns: chatStateXmlns,
),
); );
} }
@ -301,7 +295,7 @@ class MessageManager extends XmppManagerBase {
if (details.messageReactions != null) { if (details.messageReactions != null) {
stanza.addChild(details.messageReactions!.toXml()); stanza.addChild(details.messageReactions!.toXml());
} }
if (details.messageProcessingHints != null) { if (details.messageProcessingHints != null) {
for (final hint in details.messageProcessingHints!) { for (final hint in details.messageProcessingHints!) {
stanza.addChild(hint.toXml()); stanza.addChild(hint.toXml());
@ -319,7 +313,7 @@ class MessageManager extends XmppManagerBase {
), ),
); );
} }
getAttributes().sendStanza(stanza, awaitable: false); getAttributes().sendStanza(stanza, awaitable: false);
} }
} }

View File

@ -28,11 +28,9 @@ const vCardTempUpdate = 'vcard-temp:x:update';
const pubsubXmlns = 'http://jabber.org/protocol/pubsub'; const pubsubXmlns = 'http://jabber.org/protocol/pubsub';
const pubsubEventXmlns = 'http://jabber.org/protocol/pubsub#event'; const pubsubEventXmlns = 'http://jabber.org/protocol/pubsub#event';
const pubsubOwnerXmlns = 'http://jabber.org/protocol/pubsub#owner'; const pubsubOwnerXmlns = 'http://jabber.org/protocol/pubsub#owner';
const pubsubPublishOptionsXmlns = const pubsubPublishOptionsXmlns = 'http://jabber.org/protocol/pubsub#publish-options';
'http://jabber.org/protocol/pubsub#publish-options';
const pubsubNodeConfigMax = 'http://jabber.org/protocol/pubsub#config-node-max'; const pubsubNodeConfigMax = 'http://jabber.org/protocol/pubsub#config-node-max';
const pubsubNodeConfigMultiItems = const pubsubNodeConfigMultiItems = 'http://jabber.org/protocol/pubsub#multi-items';
'http://jabber.org/protocol/pubsub#multi-items';
// XEP-0066 // XEP-0066
const oobDataXmlns = 'jabber:x:oob'; const oobDataXmlns = 'jabber:x:oob';
@ -139,10 +137,8 @@ const sfsXmlns = 'urn:xmpp:sfs:0';
// XEP-0448 // XEP-0448
const sfsEncryptionXmlns = 'urn:xmpp:esfs:0'; const sfsEncryptionXmlns = 'urn:xmpp:esfs:0';
const sfsEncryptionAes128GcmNoPaddingXmlns = const sfsEncryptionAes128GcmNoPaddingXmlns = 'urn:xmpp:ciphers:aes-128-gcm-nopadding:0';
'urn:xmpp:ciphers:aes-128-gcm-nopadding:0'; const sfsEncryptionAes256GcmNoPaddingXmlns = 'urn:xmpp:ciphers:aes-256-gcm-nopadding:0';
const sfsEncryptionAes256GcmNoPaddingXmlns =
'urn:xmpp:ciphers:aes-256-gcm-nopadding:0';
const sfsEncryptionAes256CbcPkcs7Xmlns = 'urn:xmpp:ciphers:aes-256-cbc-pkcs7:0'; const sfsEncryptionAes256CbcPkcs7Xmlns = 'urn:xmpp:ciphers:aes-256-cbc-pkcs7:0';
// XEP-0449 // XEP-0449

View File

@ -35,41 +35,28 @@ class NegotiatorAttributes {
this.getSocket, this.getSocket,
this.isAuthenticated, this.isAuthenticated,
); );
/// Sends the nonza nonza and optionally redacts it in logs if redact is not null. /// Sends the nonza nonza and optionally redacts it in logs if redact is not null.
final void Function(XMLNode nonza, {String? redact}) sendNonza; final void Function(XMLNode nonza, {String? redact}) sendNonza;
/// Returns the connection settings. /// Returns the connection settings.
final ConnectionSettings Function() getConnectionSettings; final ConnectionSettings Function() getConnectionSettings;
/// Send an event event to the connection's event bus /// Send an event event to the connection's event bus
final Future<void> Function(XmppEvent event) sendEvent; final Future<void> Function(XmppEvent event) sendEvent;
/// Returns the negotiator with id id of the connection or null. /// Returns the negotiator with id id of the connection or null.
final T? Function<T extends XmppFeatureNegotiatorBase>(String) final T? Function<T extends XmppFeatureNegotiatorBase>(String) getNegotiatorById;
getNegotiatorById;
/// Returns the manager with id id of the connection or null. /// Returns the manager with id id of the connection or null.
final T? Function<T extends XmppManagerBase>(String) getManagerById; final T? Function<T extends XmppManagerBase>(String) getManagerById;
/// Returns the full JID of the current account /// Returns the full JID of the current account
final JID Function() getFullJID; final JID Function() getFullJID;
/// Returns the socket the negotiator is attached to /// Returns the socket the negotiator is attached to
final BaseSocketWrapper Function() getSocket; final BaseSocketWrapper Function() getSocket;
/// Returns true if the stream is authenticated. Returns false if not. /// Returns true if the stream is authenticated. Returns false if not.
final bool Function() isAuthenticated; final bool Function() isAuthenticated;
} }
abstract class XmppFeatureNegotiatorBase { abstract class XmppFeatureNegotiatorBase {
XmppFeatureNegotiatorBase(
this.priority,
this.sendStreamHeaderWhenDone,
this.negotiatingXmlns,
this.id,
) : state = NegotiatorState.ready;
XmppFeatureNegotiatorBase(this.priority, this.sendStreamHeaderWhenDone, this.negotiatingXmlns, this.id)
: state = NegotiatorState.ready;
/// The priority regarding other negotiators. The higher, the earlier will the /// The priority regarding other negotiators. The higher, the earlier will the
/// negotiator be used /// negotiator be used
final int priority; final int priority;
@ -83,25 +70,24 @@ abstract class XmppFeatureNegotiatorBase {
/// The Id of the negotiator /// The Id of the negotiator
final String id; final String id;
/// The state the negotiator is currently in /// The state the negotiator is currently in
NegotiatorState state; NegotiatorState state;
late NegotiatorAttributes _attributes; late NegotiatorAttributes _attributes;
/// Register the negotiator against a connection class by means of [attributes]. /// Register the negotiator against a connection class by means of [attributes].
void register(NegotiatorAttributes attributes) { void register(NegotiatorAttributes attributes) {
_attributes = attributes; _attributes = attributes;
} }
/// Returns true if a feature in [features], which are the children of the /// Returns true if a feature in [features], which are the children of the
/// <stream:features /> nonza, can be negotiated. Otherwise, returns false. /// <stream:features /> nonza, can be negotiated. Otherwise, returns false.
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
return firstWhereOrNull( return firstWhereOrNull(
features, features,
(XMLNode feature) => feature.attributes['xmlns'] == negotiatingXmlns, (XMLNode feature) => feature.attributes['xmlns'] == negotiatingXmlns,
) != ) != null;
null;
} }
/// Called with the currently received nonza [nonza] when the negotiator is active. /// Called with the currently received nonza [nonza] when the negotiator is active.
@ -119,6 +105,6 @@ abstract class XmppFeatureNegotiatorBase {
void reset() { void reset() {
state = NegotiatorState.ready; state = NegotiatorState.ready;
} }
NegotiatorAttributes get attributes => _attributes; NegotiatorAttributes get attributes => _attributes;
} }

View File

@ -16,8 +16,7 @@ class ResourceBindingFailedError extends NegotiatorError {
/// A negotiator that implements resource binding against a random server-provided /// A negotiator that implements resource binding against a random server-provided
/// resource. /// resource.
class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase { class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
ResourceBindingNegotiator() ResourceBindingNegotiator() : super(0, false, bindXmlns, resourceBindingNegotiator);
: super(0, false, bindXmlns, resourceBindingNegotiator);
/// Flag indicating the state of the negotiator: /// Flag indicating the state of the negotiator:
/// - True: We sent a binding request /// - True: We sent a binding request
@ -28,18 +27,14 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
final sm = attributes.getManagerById<StreamManagementManager>(smManager); final sm = attributes.getManagerById<StreamManagementManager>(smManager);
if (sm != null) { if (sm != null) {
return super.matchesFeature(features) && return super.matchesFeature(features) && !sm.streamResumed && attributes.isAuthenticated();
!sm.streamResumed &&
attributes.isAuthenticated();
} }
return super.matchesFeature(features) && attributes.isAuthenticated(); return super.matchesFeature(features) && attributes.isAuthenticated();
} }
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
if (!_requestSent) { if (!_requestSent) {
final stanza = XMLNode.xmlns( final stanza = XMLNode.xmlns(
tag: 'iq', tag: 'iq',
@ -68,12 +63,11 @@ class ResourceBindingNegotiator extends XmppFeatureNegotiatorBase {
final jid = bind.firstTag('jid')!; final jid = bind.firstTag('jid')!;
final resource = jid.innerText().split('/')[1]; final resource = jid.innerText().split('/')[1];
await attributes await attributes.sendEvent(ResourceBindingSuccessEvent(resource: resource));
.sendEvent(ResourceBindingSuccessEvent(resource: resource));
return const Result(NegotiatorState.done); return const Result(NegotiatorState.done);
} }
} }
@override @override
void reset() { void reset() {
_requestSent = false; _requestSent = false;

View File

@ -12,12 +12,9 @@ abstract class SaslError extends NegotiatorError {
} }
switch (error?.tag) { switch (error?.tag) {
case 'credentials-expired': case 'credentials-expired': return SaslCredentialsExpiredError();
return SaslCredentialsExpiredError(); case 'not-authorized': return SaslNotAuthorizedError();
case 'not-authorized': case 'account-disabled': return SaslAccountDisabledError();
return SaslNotAuthorizedError();
case 'account-disabled':
return SaslAccountDisabledError();
} }
return SaslUnspecifiedError(); return SaslUnspecifiedError();

View File

@ -1,4 +1,7 @@
enum ParserState { variableName, variableValue } enum ParserState {
variableName,
variableValue
}
/// Parse a string like "n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL" into /// Parse a string like "n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL" into
/// { "n": "user", "r": "fyko+d2lbbFgONRv9qkxdawL"}. /// { "n": "user", "r": "fyko+d2lbbFgONRv9qkxdawL"}.
@ -11,33 +14,31 @@ Map<String, String> parseKeyValue(String keyValueString) {
for (var i = 0; i < keyValueString.length; i++) { for (var i = 0; i < keyValueString.length; i++) {
final char = keyValueString[i]; final char = keyValueString[i];
switch (state) { switch (state) {
case ParserState.variableName: case ParserState.variableName: {
{ if (char == '=') {
if (char == '=') { state = ParserState.variableValue;
state = ParserState.variableValue; } else if (char == ',') {
} else if (char == ',') { name = '';
name = ''; } else {
} else { name += char;
name += char;
}
} }
break; }
case ParserState.variableValue: break;
{ case ParserState.variableValue: {
if (char == ',' || i == keyValueString.length - 1) { if (char == ',' || i == keyValueString.length - 1) {
if (char != ',') { if (char != ',') {
value += char;
}
values[name] = value;
value = '';
name = '';
state = ParserState.variableName;
} else {
value += char; value += char;
} }
values[name] = value;
value = '';
name = '';
state = ParserState.variableName;
} else {
value += char;
} }
break; }
break;
} }
} }

View File

@ -4,12 +4,11 @@ import 'package:moxxmpp/src/negotiators/negotiator.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
abstract class SaslNegotiator extends XmppFeatureNegotiatorBase { abstract class SaslNegotiator extends XmppFeatureNegotiatorBase {
SaslNegotiator(int priority, String id, this.mechanismName)
: super(priority, true, saslXmlns, id);
SaslNegotiator(int priority, String id, this.mechanismName) : super(priority, true, saslXmlns, id);
/// The name inside the <mechanism /> element /// The name inside the <mechanism /> element
final String mechanismName; final String mechanismName;
@override @override
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
// Is SASL advertised? // Is SASL advertised?
@ -21,9 +20,8 @@ abstract class SaslNegotiator extends XmppFeatureNegotiatorBase {
// Is SASL PLAIN advertised? // Is SASL PLAIN advertised?
return firstWhereOrNull( return firstWhereOrNull(
mechanisms.children, mechanisms.children,
(XMLNode mechanism) => mechanism.text == mechanismName, (XMLNode mechanism) => mechanism.text == mechanismName,
) != ) != null;
null;
} }
} }

View File

@ -2,13 +2,12 @@ import 'package:moxxmpp/src/namespaces.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
class SaslAuthNonza extends XMLNode { class SaslAuthNonza extends XMLNode {
SaslAuthNonza(String mechanism, String body) SaslAuthNonza(String mechanism, String body) : super(
: super( tag: 'auth',
tag: 'auth', attributes: <String, String>{
attributes: <String, String>{ 'xmlns': saslXmlns,
'xmlns': saslXmlns, 'mechanism': mechanism ,
'mechanism': mechanism, },
}, text: body,
text: body, );
);
} }

View File

@ -10,18 +10,16 @@ import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/types/result.dart'; import 'package:moxxmpp/src/types/result.dart';
class SaslPlainAuthNonza extends SaslAuthNonza { class SaslPlainAuthNonza extends SaslAuthNonza {
SaslPlainAuthNonza(String username, String password) SaslPlainAuthNonza(String username, String password) : super(
: super( 'PLAIN', base64.encode(utf8.encode('\u0000$username\u0000$password')),
'PLAIN', );
base64.encode(utf8.encode('\u0000$username\u0000$password')),
);
} }
class SaslPlainNegotiator extends SaslNegotiator { class SaslPlainNegotiator extends SaslNegotiator {
SaslPlainNegotiator() SaslPlainNegotiator()
: _authSent = false, : _authSent = false,
_log = Logger('SaslPlainNegotiator'), _log = Logger('SaslPlainNegotiator'),
super(0, saslPlainNegotiator, 'PLAIN'); super(0, saslPlainNegotiator, 'PLAIN');
bool _authSent; bool _authSent;
final Logger _log; final Logger _log;
@ -29,12 +27,10 @@ class SaslPlainNegotiator extends SaslNegotiator {
@override @override
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
if (!attributes.getConnectionSettings().allowPlainAuth) return false; if (!attributes.getConnectionSettings().allowPlainAuth) return false;
if (super.matchesFeature(features)) { if (super.matchesFeature(features)) {
if (!attributes.getSocket().isSecure()) { if (!attributes.getSocket().isSecure()) {
_log.warning( _log.warning('Refusing to match SASL feature due to unsecured connection');
'Refusing to match SASL feature due to unsecured connection',
);
return false; return false;
} }
@ -45,9 +41,7 @@ class SaslPlainNegotiator extends SaslNegotiator {
} }
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
if (!_authSent) { if (!_authSent) {
final settings = attributes.getConnectionSettings(); final settings = attributes.getConnectionSettings();
attributes.sendNonza( attributes.sendNonza(

View File

@ -17,30 +17,28 @@ import 'package:saslprep/saslprep.dart';
// NOTE: Inspired by https://github.com/vukoye/xmpp_dart/blob/3b1a0588562b9e591488c99d834088391840911d/lib/src/features/sasl/ScramSaslHandler.dart // NOTE: Inspired by https://github.com/vukoye/xmpp_dart/blob/3b1a0588562b9e591488c99d834088391840911d/lib/src/features/sasl/ScramSaslHandler.dart
enum ScramHashType { sha1, sha256, sha512 } enum ScramHashType {
sha1,
sha256,
sha512
}
HashAlgorithm hashFromType(ScramHashType type) { HashAlgorithm hashFromType(ScramHashType type) {
switch (type) { switch (type) {
case ScramHashType.sha1: case ScramHashType.sha1: return Sha1();
return Sha1(); case ScramHashType.sha256: return Sha256();
case ScramHashType.sha256: case ScramHashType.sha512: return Sha512();
return Sha256();
case ScramHashType.sha512:
return Sha512();
} }
} }
int pbkdfBitsFromHash(ScramHashType type) { int pbkdfBitsFromHash(ScramHashType type) {
switch (type) { switch (type) {
// NOTE: SHA1 is 20 octets long => 20 octets * 8 bits/octet // NOTE: SHA1 is 20 octets long => 20 octets * 8 bits/octet
case ScramHashType.sha1: case ScramHashType.sha1: return 160;
return 160;
// NOTE: SHA256 is 32 octets long => 32 octets * 8 bits/octet // NOTE: SHA256 is 32 octets long => 32 octets * 8 bits/octet
case ScramHashType.sha256: case ScramHashType.sha256: return 256;
return 256;
// NOTE: SHA512 is 64 octets long => 64 octets * 8 bits/octet // NOTE: SHA512 is 64 octets long => 64 octets * 8 bits/octet
case ScramHashType.sha512: case ScramHashType.sha512: return 512;
return 512;
} }
} }
@ -50,48 +48,44 @@ const scramSha512Mechanism = 'SCRAM-SHA-512';
String mechanismNameFromType(ScramHashType type) { String mechanismNameFromType(ScramHashType type) {
switch (type) { switch (type) {
case ScramHashType.sha1: case ScramHashType.sha1: return scramSha1Mechanism;
return scramSha1Mechanism; case ScramHashType.sha256: return scramSha256Mechanism;
case ScramHashType.sha256: case ScramHashType.sha512: return scramSha512Mechanism;
return scramSha256Mechanism;
case ScramHashType.sha512:
return scramSha512Mechanism;
} }
} }
String namespaceFromType(ScramHashType type) { String namespaceFromType(ScramHashType type) {
switch (type) { switch (type) {
case ScramHashType.sha1: case ScramHashType.sha1: return saslScramSha1Negotiator;
return saslScramSha1Negotiator; case ScramHashType.sha256: return saslScramSha256Negotiator;
case ScramHashType.sha256: case ScramHashType.sha512: return saslScramSha512Negotiator;
return saslScramSha256Negotiator;
case ScramHashType.sha512:
return saslScramSha512Negotiator;
} }
} }
class SaslScramAuthNonza extends SaslAuthNonza { class SaslScramAuthNonza extends SaslAuthNonza {
// This subclassing makes less sense here, but this is since the auth nonza here // This subclassing makes less sense here, but this is since the auth nonza here
// requires knowledge of the inner state of the Negotiator. // requires knowledge of the inner state of the Negotiator.
SaslScramAuthNonza({required ScramHashType type, required String body}) SaslScramAuthNonza({ required ScramHashType type, required String body }) : super(
: super( mechanismNameFromType(type), body,
mechanismNameFromType(type), );
body,
);
} }
class SaslScramResponseNonza extends XMLNode { class SaslScramResponseNonza extends XMLNode {
SaslScramResponseNonza({required String body}) SaslScramResponseNonza({ required String body }) : super(
: super( tag: 'response',
tag: 'response', attributes: <String, String>{
attributes: <String, String>{ 'xmlns': saslXmlns,
'xmlns': saslXmlns, },
}, text: body,
text: body, );
);
} }
enum ScramState { preSent, initialMessageSent, challengeResponseSent, error } enum ScramState {
preSent,
initialMessageSent,
challengeResponseSent,
error
}
const gs2Header = 'n,,'; const gs2Header = 'n,,';
@ -102,16 +96,12 @@ class SaslScramNegotiator extends SaslNegotiator {
this.initialMessageNoGS2, this.initialMessageNoGS2,
this.clientNonce, this.clientNonce,
this.hashType, this.hashType,
) : _hash = hashFromType(hashType), ) :
_serverSignature = '', _hash = hashFromType(hashType),
_scramState = ScramState.preSent, _serverSignature = '',
_log = _scramState = ScramState.preSent,
Logger('SaslScramNegotiator(${mechanismNameFromType(hashType)})'), _log = Logger('SaslScramNegotiator(${mechanismNameFromType(hashType)})'),
super( super(priority, namespaceFromType(hashType), mechanismNameFromType(hashType));
priority,
namespaceFromType(hashType),
mechanismNameFromType(hashType),
);
String? clientNonce; String? clientNonce;
String initialMessageNoGS2; String initialMessageNoGS2;
final ScramHashType hashType; final ScramHashType hashType;
@ -132,9 +122,7 @@ class SaslScramNegotiator extends SaslNegotiator {
final saltedPasswordRaw = await pbkdf2.deriveKey( final saltedPasswordRaw = await pbkdf2.deriveKey(
secretKey: SecretKey( secretKey: SecretKey(
utf8.encode( utf8.encode(Saslprep.saslprep(attributes.getConnectionSettings().password)),
Saslprep.saslprep(attributes.getConnectionSettings().password),
),
), ),
nonce: base64.decode(salt), nonce: base64.decode(salt),
); );
@ -143,46 +131,32 @@ class SaslScramNegotiator extends SaslNegotiator {
Future<List<int>> calculateClientKey(List<int> saltedPassword) async { Future<List<int>> calculateClientKey(List<int> saltedPassword) async {
return (await Hmac(_hash).calculateMac( return (await Hmac(_hash).calculateMac(
utf8.encode('Client Key'), utf8.encode('Client Key'), secretKey: SecretKey(saltedPassword),
secretKey: SecretKey(saltedPassword), )).bytes;
))
.bytes;
} }
Future<List<int>> calculateClientSignature( Future<List<int>> calculateClientSignature(String authMessage, List<int> storedKey) async {
String authMessage,
List<int> storedKey,
) async {
return (await Hmac(_hash).calculateMac( return (await Hmac(_hash).calculateMac(
utf8.encode(authMessage), utf8.encode(authMessage),
secretKey: SecretKey(storedKey), secretKey: SecretKey(storedKey),
)) )).bytes;
.bytes;
} }
Future<List<int>> calculateServerKey(List<int> saltedPassword) async { Future<List<int>> calculateServerKey(List<int> saltedPassword) async {
return (await Hmac(_hash).calculateMac( return (await Hmac(_hash).calculateMac(
utf8.encode('Server Key'), utf8.encode('Server Key'),
secretKey: SecretKey(saltedPassword), secretKey: SecretKey(saltedPassword),
)) )).bytes;
.bytes;
} }
Future<List<int>> calculateServerSignature( Future<List<int>> calculateServerSignature(String authMessage, List<int> serverKey) async {
String authMessage,
List<int> serverKey,
) async {
return (await Hmac(_hash).calculateMac( return (await Hmac(_hash).calculateMac(
utf8.encode(authMessage), utf8.encode(authMessage),
secretKey: SecretKey(serverKey), secretKey: SecretKey(serverKey),
)) )).bytes;
.bytes;
} }
List<int> calculateClientProof( List<int> calculateClientProof(List<int> clientKey, List<int> clientSignature) {
List<int> clientKey,
List<int> clientSignature,
) {
final clientProof = List<int>.filled(clientKey.length, 0); final clientProof = List<int>.filled(clientKey.length, 0);
for (var i = 0; i < clientKey.length; i++) { for (var i = 0; i < clientKey.length; i++) {
clientProof[i] = clientKey[i] ^ clientSignature[i]; clientProof[i] = clientKey[i] ^ clientSignature[i];
@ -190,26 +164,20 @@ class SaslScramNegotiator extends SaslNegotiator {
return clientProof; return clientProof;
} }
Future<String> calculateChallengeResponse(String base64Challenge) async { Future<String> calculateChallengeResponse(String base64Challenge) async {
final challengeString = utf8.decode(base64.decode(base64Challenge)); final challengeString = utf8.decode(base64.decode(base64Challenge));
final challenge = parseKeyValue(challengeString); final challenge = parseKeyValue(challengeString);
final clientFinalMessageBare = 'c=biws,r=${challenge['r']!}'; final clientFinalMessageBare = 'c=biws,r=${challenge['r']!}';
final saltedPassword = await calculateSaltedPassword( final saltedPassword = await calculateSaltedPassword(challenge['s']!, int.parse(challenge['i']!));
challenge['s']!,
int.parse(challenge['i']!),
);
final clientKey = await calculateClientKey(saltedPassword); final clientKey = await calculateClientKey(saltedPassword);
final storedKey = (await _hash.hash(clientKey)).bytes; final storedKey = (await _hash.hash(clientKey)).bytes;
final authMessage = final authMessage = '$initialMessageNoGS2,$challengeString,$clientFinalMessageBare';
'$initialMessageNoGS2,$challengeString,$clientFinalMessageBare'; final clientSignature = await calculateClientSignature(authMessage, storedKey);
final clientSignature =
await calculateClientSignature(authMessage, storedKey);
final clientProof = calculateClientProof(clientKey, clientSignature); final clientProof = calculateClientProof(clientKey, clientSignature);
final serverKey = await calculateServerKey(saltedPassword); final serverKey = await calculateServerKey(saltedPassword);
_serverSignature = _serverSignature = base64.encode(await calculateServerSignature(authMessage, serverKey));
base64.encode(await calculateServerSignature(authMessage, serverKey));
return '$clientFinalMessageBare,p=${base64.encode(clientProof)}'; return '$clientFinalMessageBare,p=${base64.encode(clientProof)}';
} }
@ -218,9 +186,7 @@ class SaslScramNegotiator extends SaslNegotiator {
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
if (super.matchesFeature(features)) { if (super.matchesFeature(features)) {
if (!attributes.getSocket().isSecure()) { if (!attributes.getSocket().isSecure()) {
_log.warning( _log.warning('Refusing to match SASL feature due to unsecured connection');
'Refusing to match SASL feature due to unsecured connection',
);
return false; return false;
} }
@ -229,29 +195,20 @@ class SaslScramNegotiator extends SaslNegotiator {
return false; return false;
} }
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
switch (_scramState) { switch (_scramState) {
case ScramState.preSent: case ScramState.preSent:
if (clientNonce == null || clientNonce == '') { if (clientNonce == null || clientNonce == '') {
clientNonce = randomAlphaNumeric( clientNonce = randomAlphaNumeric(40, provider: CoreRandomProvider.from(Random.secure()));
40,
provider: CoreRandomProvider.from(Random.secure()),
);
} }
initialMessageNoGS2 = initialMessageNoGS2 = 'n=${attributes.getConnectionSettings().jid.local},r=$clientNonce';
'n=${attributes.getConnectionSettings().jid.local},r=$clientNonce';
_scramState = ScramState.initialMessageSent; _scramState = ScramState.initialMessageSent;
attributes.sendNonza( attributes.sendNonza(
SaslScramAuthNonza( SaslScramAuthNonza(body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)), type: hashType),
body: base64.encode(utf8.encode(gs2Header + initialMessageNoGS2)),
type: hashType,
),
redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(), redact: SaslScramAuthNonza(body: '******', type: hashType).toXml(),
); );
return const Result(NegotiatorState.ready); return const Result(NegotiatorState.ready);
@ -287,8 +244,7 @@ class SaslScramNegotiator extends SaslNegotiator {
} }
// NOTE: This assumes that the string is always "v=..." and contains no other parameters // NOTE: This assumes that the string is always "v=..." and contains no other parameters
final signature = final signature = parseKeyValue(utf8.decode(base64.decode(nonza.innerText())));
parseKeyValue(utf8.decode(base64.decode(nonza.innerText())));
if (signature['v']! != _serverSignature) { if (signature['v']! != _serverSignature) {
// TODO(Unknown): Notify of a signature mismatch // TODO(Unknown): Notify of a signature mismatch
//final error = nonza.children.first.tag; //final error = nonza.children.first.tag;

View File

@ -5,7 +5,10 @@ import 'package:moxxmpp/src/negotiators/negotiator.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/types/result.dart'; import 'package:moxxmpp/src/types/result.dart';
enum _StartTlsState { ready, requested } enum _StartTlsState {
ready,
requested
}
class StartTLSFailedError extends NegotiatorError { class StartTLSFailedError extends NegotiatorError {
@override @override
@ -13,11 +16,10 @@ class StartTLSFailedError extends NegotiatorError {
} }
class StartTLSNonza extends XMLNode { class StartTLSNonza extends XMLNode {
StartTLSNonza() StartTLSNonza() : super.xmlns(
: super.xmlns( tag: 'starttls',
tag: 'starttls', xmlns: startTlsXmlns,
xmlns: startTlsXmlns, );
);
} }
/// A negotiator implementing StartTLS. /// A negotiator implementing StartTLS.
@ -31,9 +33,7 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
final Logger _log = Logger('StartTlsNegotiator'); final Logger _log = Logger('StartTlsNegotiator');
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
switch (_state) { switch (_state) {
case _StartTlsState.ready: case _StartTlsState.ready:
_log.fine('StartTLS is available. Performing StartTLS upgrade...'); _log.fine('StartTLS is available. Performing StartTLS upgrade...');
@ -41,16 +41,14 @@ class StartTlsNegotiator extends XmppFeatureNegotiatorBase {
attributes.sendNonza(StartTLSNonza()); attributes.sendNonza(StartTLSNonza());
return const Result(NegotiatorState.ready); return const Result(NegotiatorState.ready);
case _StartTlsState.requested: case _StartTlsState.requested:
if (nonza.tag != 'proceed' || if (nonza.tag != 'proceed' || nonza.attributes['xmlns'] != startTlsXmlns) {
nonza.attributes['xmlns'] != startTlsXmlns) {
_log.severe('Failed to perform StartTLS negotiation'); _log.severe('Failed to perform StartTLS negotiation');
return Result(StartTLSFailedError()); return Result(StartTLSFailedError());
} }
_log.fine('Securing socket'); _log.fine('Securing socket');
final result = await attributes final result = await attributes.getSocket()
.getSocket() .secure(attributes.getConnectionSettings().jid.domain);
.secure(attributes.getConnectionSettings().jid.domain);
if (!result) { if (!result) {
_log.severe('Failed to secure stream'); _log.severe('Failed to secure stream');
return Result(StartTLSFailedError()); return Result(StartTLSFailedError());

View File

@ -8,13 +8,11 @@ class PingManager extends XmppManagerBase {
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
void _logWarning() { void _logWarning() {
logger.warning( logger.warning('Cannot send keepalives as SM is not available, the socket disallows whitespace pings and does not manage its own keepalives. Cannot guarantee that the connection survives.');
'Cannot send keepalives as SM is not available, the socket disallows whitespace pings and does not manage its own keepalives. Cannot guarantee that the connection survives.',
);
} }
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is SendPingEvent) { if (event is SendPingEvent) {
@ -26,18 +24,14 @@ class PingManager extends XmppManagerBase {
logger.finest('Not sending ping as the socket manages it.'); logger.finest('Not sending ping as the socket manages it.');
return; return;
} }
final stream = final stream = attrs.getManagerById(smManager) as StreamManagementManager?;
attrs.getManagerById(smManager) as StreamManagementManager?;
if (stream != null) { if (stream != null) {
if (stream if (stream.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
logger.finest('Sending an ack ping as Stream Management is enabled'); logger.finest('Sending an ack ping as Stream Management is enabled');
stream.sendAckRequestPing(); stream.sendAckRequestPing();
} else if (attrs.getSocket().whitespacePingAllowed()) { } else if (attrs.getSocket().whitespacePingAllowed()) {
logger.finest( logger.finest('Sending a whitespace ping as Stream Management is not enabled');
'Sending a whitespace ping as Stream Management is not enabled',
);
attrs.getConnection().sendWhitespacePing(); attrs.getConnection().sendWhitespacePing();
} else { } else {
_logWarning(); _logWarning();

View File

@ -20,19 +20,18 @@ class PresenceManager extends XmppManagerBase {
PresenceManager() : super(presenceManager); PresenceManager() : super(presenceManager);
/// The list of pre-send callbacks. /// The list of pre-send callbacks.
final List<PresencePreSendCallback> _presenceCallbacks = final List<PresencePreSendCallback> _presenceCallbacks = List.empty(growable: true);
List.empty(growable: true);
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'presence', stanzaTag: 'presence',
callback: _onPresence, callback: _onPresence,
) )
]; ];
@override @override
List<String> getDiscoFeatures() => [capsXmlns]; List<String> getDiscoFeatures() => [ capsXmlns ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
@ -41,35 +40,26 @@ class PresenceManager extends XmppManagerBase {
void registerPreSendCallback(PresencePreSendCallback callback) { void registerPreSendCallback(PresencePreSendCallback callback) {
_presenceCallbacks.add(callback); _presenceCallbacks.add(callback);
} }
Future<StanzaHandlerData> _onPresence( Future<StanzaHandlerData> _onPresence(Stanza presence, StanzaHandlerData state) async {
Stanza presence,
StanzaHandlerData state,
) async {
final attrs = getAttributes(); final attrs = getAttributes();
switch (presence.type) { switch (presence.type) {
case 'subscribe': case 'subscribe':
case 'subscribed': case 'subscribed': {
{ attrs.sendEvent(
attrs.sendEvent( SubscriptionRequestReceivedEvent(from: JID.fromString(presence.from!)),
SubscriptionRequestReceivedEvent( );
from: JID.fromString(presence.from!), return state.copyWith(done: true);
), }
); default: break;
return state.copyWith(done: true);
}
default:
break;
} }
if (presence.from != null) { if (presence.from != null) {
logger.finest("Received presence from '${presence.from}'"); logger.finest("Received presence from '${presence.from}'");
getAttributes().sendEvent( getAttributes().sendEvent(PresenceReceivedEvent(JID.fromString(presence.from!), presence));
PresenceReceivedEvent(JID.fromString(presence.from!), presence),
);
return state.copyWith(done: true); return state.copyWith(done: true);
} }
return state; return state;
} }
@ -107,7 +97,7 @@ class PresenceManager extends XmppManagerBase {
addFrom: StanzaFromType.full, addFrom: StanzaFromType.full,
); );
} }
/// Sends a subscription request to [to]. /// Sends a subscription request to [to].
void sendSubscriptionRequest(String to) { void sendSubscriptionRequest(String to) {
getAttributes().sendStanza( getAttributes().sendStanza(

View File

@ -35,18 +35,15 @@ abstract class ReconnectionPolicy {
/// The lock for accessing [_shouldAttemptReconnection] /// The lock for accessing [_shouldAttemptReconnection]
@protected @protected
final Lock shouldReconnectLock = Lock(); final Lock shouldReconnectLock = Lock();
/// Called by XmppConnection to register the policy. /// Called by XmppConnection to register the policy.
void register( void register(PerformReconnectFunction performReconnect, ConnectionLostCallback triggerConnectionLost) {
PerformReconnectFunction performReconnect,
ConnectionLostCallback triggerConnectionLost,
) {
this.performReconnect = performReconnect; this.performReconnect = performReconnect;
this.triggerConnectionLost = triggerConnectionLost; this.triggerConnectionLost = triggerConnectionLost;
unawaited(reset()); unawaited(reset());
} }
/// In case the policy depends on some internal state, this state must be reset /// In case the policy depends on some internal state, this state must be reset
/// to an initial state when reset is called. In case timers run, they must be /// to an initial state when reset is called. In case timers run, they must be
/// terminated. /// terminated.
@ -64,8 +61,7 @@ abstract class ReconnectionPolicy {
/// Set whether a reconnection attempt should be made. /// Set whether a reconnection attempt should be made.
Future<void> setShouldReconnect(bool value) async { Future<void> setShouldReconnect(bool value) async {
return shouldReconnectLock return shouldReconnectLock.synchronized(() => _shouldAttemptReconnection = value);
.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
@ -81,6 +77,7 @@ abstract class ReconnectionPolicy {
isReconnecting = value; isReconnecting = value;
}); });
} }
} }
/// A simple reconnection strategy: Make the reconnection delays exponentially longer /// A simple reconnection strategy: Make the reconnection delays exponentially longer
@ -90,11 +87,8 @@ class RandomBackoffReconnectionPolicy extends ReconnectionPolicy {
RandomBackoffReconnectionPolicy( RandomBackoffReconnectionPolicy(
this._minBackoffTime, this._minBackoffTime,
this._maxBackoffTime, this._maxBackoffTime,
) : assert( ) : assert(_minBackoffTime < _maxBackoffTime, '_minBackoffTime must be smaller than _maxBackoffTime'),
_minBackoffTime < _maxBackoffTime, super();
'_minBackoffTime must be smaller than _maxBackoffTime',
),
super();
/// The maximum time in seconds that a backoff should be. /// The maximum time in seconds that a backoff should be.
final int _maxBackoffTime; final int _maxBackoffTime;
@ -119,16 +113,12 @@ class RandomBackoffReconnectionPolicy extends ReconnectionPolicy {
await lock.synchronized(() async { await lock.synchronized(() async {
_log.fine('Lock aquired'); _log.fine('Lock aquired');
if (!(await getShouldReconnect())) { if (!(await getShouldReconnect())) {
_log.fine( _log.fine('Backoff timer expired but getShouldReconnect() returned false');
'Backoff timer expired but getShouldReconnect() returned false',
);
return; return;
} }
if (isReconnecting) { if (isReconnecting) {
_log.fine( _log.fine('Backoff timer expired but a reconnection is running, so doing nothing.');
'Backoff timer expired but a reconnection is running, so doing nothing.',
);
return; return;
} }
@ -153,7 +143,7 @@ class RandomBackoffReconnectionPolicy extends ReconnectionPolicy {
await setIsReconnecting(false); await setIsReconnecting(false);
} }
@override @override
Future<void> reset() async { Future<void> reset() async {
// ignore: unnecessary_lambdas // ignore: unnecessary_lambdas
@ -165,20 +155,17 @@ class RandomBackoffReconnectionPolicy extends ReconnectionPolicy {
return _timer == null; return _timer == null;
}); });
if (!shouldContinue) { if (!shouldContinue) {
_log.finest( _log.finest('_onFailure: Not backing off since _timer is already running');
'_onFailure: Not backing off since _timer is already running',
);
return; return;
} }
final seconds = final seconds = Random().nextInt(_maxBackoffTime - _minBackoffTime) + _minBackoffTime;
Random().nextInt(_maxBackoffTime - _minBackoffTime) + _minBackoffTime;
_log.finest('Failure occured. Starting random backoff with ${seconds}s'); _log.finest('Failure occured. Starting random backoff with ${seconds}s');
_timer?.cancel(); _timer?.cancel();
_timer = Timer(Duration(seconds: seconds), _onTimerElapsed); _timer = Timer(Duration(seconds: seconds), _onTimerElapsed);
} }
@override @override
Future<void> onFailure() async { Future<void> onFailure() async {
// ignore: unnecessary_lambdas // ignore: unnecessary_lambdas
@ -212,7 +199,7 @@ class TestingReconnectionPolicy extends ReconnectionPolicy {
class TestingSleepReconnectionPolicy extends ReconnectionPolicy { class TestingSleepReconnectionPolicy extends ReconnectionPolicy {
TestingSleepReconnectionPolicy(this._sleepAmount) : super(); TestingSleepReconnectionPolicy(this._sleepAmount) : super();
final int _sleepAmount; final int _sleepAmount;
@override @override
Future<void> onSuccess() async {} Future<void> onSuccess() async {}

View File

@ -21,7 +21,7 @@ int ioctetSortComparator(String a, String b) {
if (a.codeUnitAt(0) < b.codeUnitAt(0)) { if (a.codeUnitAt(0) < b.codeUnitAt(0)) {
return -1; return -1;
} }
return 1; return 1;
} }
@ -46,6 +46,6 @@ int ioctetSortComparatorRaw(List<int> a, List<int> b) {
if (a[0] < b[0]) { if (a[0] < b[0]) {
return -1; return -1;
} }
return 1; return 1;
} }

View File

@ -18,13 +18,7 @@ import 'package:moxxmpp/src/types/result.dart';
@immutable @immutable
class XmppRosterItem { class XmppRosterItem {
const XmppRosterItem({ const XmppRosterItem({ required this.jid, required this.subscription, this.ask, this.name, this.groups = const [] });
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;
@ -32,35 +26,34 @@ class XmppRosterItem {
final List<String> groups; final List<String> groups;
@override @override
bool operator ==(Object other) { bool operator==(Object other) {
return other is XmppRosterItem && return other is XmppRosterItem &&
other.jid == jid && other.jid == jid &&
other.name == name && other.name == name &&
other.subscription == subscription && other.subscription == subscription &&
other.ask == ask && other.ask == ask &&
const ListEquality<String>().equals(other.groups, groups); const ListEquality<String>().equals(other.groups, groups);
} }
@override @override
int get hashCode => int get hashCode => jid.hashCode ^ name.hashCode ^ subscription.hashCode ^ ask.hashCode ^ groups.hashCode;
jid.hashCode ^
name.hashCode ^
subscription.hashCode ^
ask.hashCode ^
groups.hashCode;
@override @override
String toString() { String toString() {
return 'XmppRosterItem(' return 'XmppRosterItem('
'jid: $jid, ' 'jid: $jid, '
'name: $name, ' 'name: $name, '
'subscription: $subscription, ' 'subscription: $subscription, '
'ask: $ask, ' 'ask: $ask, '
'groups: $groups)'; 'groups: $groups)';
} }
} }
enum RosterRemovalResult { okay, error, itemNotFound } enum RosterRemovalResult {
okay,
error,
itemNotFound
}
class RosterRequestResult { class RosterRequestResult {
RosterRequestResult(this.items, this.ver); RosterRequestResult(this.items, this.ver);
@ -76,18 +69,14 @@ class RosterPushResult {
/// A Stub feature negotiator for finding out whether roster versioning is supported. /// A Stub feature negotiator for finding out whether roster versioning is supported.
class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase { class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase {
RosterFeatureNegotiator() RosterFeatureNegotiator() : _supported = false, super(11, false, rosterVersioningXmlns, rosterNegotiator);
: _supported = false,
super(11, false, rosterVersioningXmlns, rosterNegotiator);
/// True if rosterVersioning is supported. False otherwise. /// True if rosterVersioning is supported. False otherwise.
bool _supported; bool _supported;
bool get isSupported => _supported; bool get isSupported => _supported;
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
// negotiate is only called when the negotiator matched, meaning the server // negotiate is only called when the negotiator matched, meaning the server
// advertises roster versioning. // advertises roster versioning.
_supported = true; _supported = true;
@ -112,26 +101,23 @@ class RosterManager extends XmppManagerBase {
@override @override
void register(XmppManagerAttributes attributes) { void register(XmppManagerAttributes attributes) {
super.register(attributes); super.register(attributes);
_stateManager.register(attributes.sendEvent); _stateManager.register(attributes.sendEvent);
} }
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'iq', stanzaTag: 'iq',
tagName: 'query', tagName: 'query',
tagXmlns: rosterXmlns, tagXmlns: rosterXmlns,
callback: _onRosterPush, callback: _onRosterPush,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onRosterPush( Future<StanzaHandlerData> _onRosterPush(Stanza stanza, StanzaHandlerData state) async {
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?;
final selfJid = attrs.getConnectionSettings().jid; final selfJid = attrs.getConnectionSettings().jid;
@ -142,9 +128,7 @@ class RosterManager extends XmppManagerBase {
// - empty, i.e. not set // - empty, i.e. not set
// - a full JID of our own // - a full JID of our own
if (from != null && JID.fromString(from).toBare() != selfJid) { if (from != null && JID.fromString(from).toBare() != selfJid) {
logger.warning( logger.warning('Roster push invalid! Unexpected from attribute: ${stanza.toXml()}');
'Roster push invalid! Unexpected from attribute: ${stanza.toXml()}',
);
return state.copyWith(done: true); return state.copyWith(done: true);
} }
@ -164,13 +148,13 @@ class RosterManager extends XmppManagerBase {
jid: item.attributes['jid']! as String, jid: item.attributes['jid']! as String,
subscription: item.attributes['subscription']! as String, subscription: item.attributes['subscription']! as String,
ask: item.attributes['ask'] as String?, ask: item.attributes['ask'] as String?,
name: item.attributes['name'] as String?, name: item.attributes['name'] as String?,
), ),
query.attributes['ver'] as String?, query.attributes['ver'] as String?,
), ),
), ),
); );
await reply( await reply(
state, state,
'result', 'result',
@ -182,32 +166,23 @@ class RosterManager extends XmppManagerBase {
/// Shared code between requesting rosters without and with roster versioning, if /// Shared code between requesting rosters without and with roster versioning, if
/// the server deems a regular roster response more efficient than n roster pushes. /// the server deems a regular roster response more efficient than n roster pushes.
Future<Result<RosterRequestResult, RosterError>> _handleRosterResponse( Future<Result<RosterRequestResult, RosterError>> _handleRosterResponse(XMLNode? query) async {
XMLNode? query,
) async {
final List<XmppRosterItem> items; final List<XmppRosterItem> items;
String? rosterVersion; String? rosterVersion;
if (query != null) { if (query != null) {
items = query.children items = query.children.map(
.map( (item) => XmppRosterItem(
(item) => XmppRosterItem( name: item.attributes['name'] as String?,
name: item.attributes['name'] as String?, jid: item.attributes['jid']! as String,
jid: item.attributes['jid']! as String, subscription: item.attributes['subscription']! as String,
subscription: item.attributes['subscription']! as String, ask: item.attributes['ask'] as String?,
ask: item.attributes['ask'] as String?, groups: item.findTags('group').map((groupNode) => groupNode.innerText()).toList(),
groups: item ),
.findTags('group') ).toList();
.map((groupNode) => groupNode.innerText())
.toList(),
),
)
.toList();
rosterVersion = query.attributes['ver'] as String?; rosterVersion = query.attributes['ver'] as String?;
} else { } else {
logger.warning( 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');
'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());
} }
@ -222,7 +197,7 @@ class RosterManager extends XmppManagerBase {
return Result(result); return Result(result);
} }
/// Requests the roster following RFC 6121. /// Requests the roster following RFC 6121.
Future<Result<RosterRequestResult, RosterError>> requestRoster() async { Future<Result<RosterRequestResult, RosterError>> requestRoster() async {
final attrs = getAttributes(); final attrs = getAttributes();
@ -255,8 +230,7 @@ class RosterManager extends XmppManagerBase {
/// 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>> Future<Result<RosterRequestResult?, RosterError>> requestRosterPushes() async {
requestRosterPushes() async {
final attrs = getAttributes(); final attrs = getAttributes();
final result = await attrs.sendStanza( final result = await attrs.sendStanza(
Stanza.iq( Stanza.iq(
@ -283,18 +257,12 @@ class RosterManager extends XmppManagerBase {
} }
bool rosterVersioningAvailable() { bool rosterVersioningAvailable() {
return getAttributes() return getAttributes().getNegotiatorById<RosterFeatureNegotiator>(rosterNegotiator)!.isSupported;
.getNegotiatorById<RosterFeatureNegotiator>(rosterNegotiator)!
.isSupported;
} }
/// Attempts to add [jid] with a title of [title] and groups [groups] to the roster. /// Attempts to add [jid] with a title of [title] and groups [groups] to the roster.
/// Returns true if the process was successful, false otherwise. /// Returns true if the process was successful, false otherwise.
Future<bool> addToRoster( Future<bool> addToRoster(String jid, String title, { List<String>? groups }) async {
String jid,
String title, {
List<String>? groups,
}) async {
final attrs = getAttributes(); final attrs = getAttributes();
final response = await attrs.sendStanza( final response = await attrs.sendStanza(
Stanza.iq( Stanza.iq(
@ -308,13 +276,9 @@ class RosterManager extends XmppManagerBase {
tag: 'item', tag: 'item',
attributes: <String, String>{ attributes: <String, String>{
'jid': jid, 'jid': jid,
...title == jid.split('@')[0] ...title == jid.split('@')[0] ? <String, String>{} : <String, String>{ 'name': title }
? <String, String>{}
: <String, String>{'name': title}
}, },
children: (groups ?? []) children: (groups ?? []).map((group) => XMLNode(tag: 'group', text: group)).toList(),
.map((group) => XMLNode(tag: 'group', text: group))
.toList(),
) )
], ],
) )

View File

@ -30,7 +30,7 @@ abstract class BaseRosterStateManager {
/// A function to send an XmppEvent to moxxmpp's main event bus /// A function to send an XmppEvent to moxxmpp's main event bus
late void Function(XmppEvent) _sendEvent; late void Function(XmppEvent) _sendEvent;
/// Overrideable function /// Overrideable function
/// Loads the old cached version of the roster and optionally that roster version /// Loads the old cached version of the roster and optionally that roster version
/// from persistent storage into a RosterCacheLoadResult object. /// from persistent storage into a RosterCacheLoadResult object.
@ -50,19 +50,14 @@ abstract class BaseRosterStateManager {
/// ///
/// [added] is a (possibly empty) list of XmppRosterItems that are added by the /// [added] is a (possibly empty) list of XmppRosterItems that are added by the
/// roster push or roster fetch request. /// roster push or roster fetch request.
Future<void> commitRoster( Future<void> commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added);
String? version,
List<String> removed,
List<XmppRosterItem> modified,
List<XmppRosterItem> added,
);
/// Internal function. Registers functions from the RosterManger against this /// Internal function. Registers functions from the RosterManger against this
/// instance. /// instance.
void register(void Function(XmppEvent) sendEvent) { void register(void Function(XmppEvent) sendEvent) {
_sendEvent = sendEvent; _sendEvent = sendEvent;
} }
/// Load and cache or return the cached roster version. /// Load and cache or return the cached roster version.
Future<String?> getRosterVersion() async { Future<String?> getRosterVersion() async {
return _lock.synchronized(() async { return _lock.synchronized(() async {
@ -74,12 +69,7 @@ abstract class BaseRosterStateManager {
/// A wrapper around _commitRoster that also sends an event to moxxmpp's event /// A wrapper around _commitRoster that also sends an event to moxxmpp's event
/// bus. /// bus.
Future<void> _commitRoster( Future<void> _commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added) async {
String? version,
List<String> removed,
List<XmppRosterItem> modified,
List<XmppRosterItem> added,
) async {
_sendEvent( _sendEvent(
RosterUpdatedEvent( RosterUpdatedEvent(
removed, removed,
@ -87,10 +77,10 @@ abstract class BaseRosterStateManager {
added, added,
), ),
); );
await commitRoster(version, removed, modified, added); await commitRoster(version, removed, modified, added);
} }
/// Loads the cached roster data into memory, if that has not already happened. /// Loads the cached roster data into memory, if that has not already happened.
/// NOTE: Must be called from within the _lock critical section. /// NOTE: Must be called from within the _lock critical section.
Future<void> _loadRosterCache() async { Future<void> _loadRosterCache() async {
@ -114,7 +104,7 @@ abstract class BaseRosterStateManager {
null, null,
); );
} }
final index = _currentRoster!.indexWhere((i) => i.jid == item.jid); final index = _currentRoster!.indexWhere((i) => i.jid == item.jid);
if (index == -1) { if (index == -1) {
// The item does not exist // The item does not exist
@ -183,7 +173,7 @@ abstract class BaseRosterStateManager {
final added = List<XmppRosterItem>.empty(growable: true); final added = List<XmppRosterItem>.empty(growable: true);
await _loadRosterCache(); await _loadRosterCache();
_currentVersion = result.ver; _currentVersion = result.ver;
for (final item in result.items) { for (final item in result.items) {
final result = _handleRosterItem(item); final result = _handleRosterItem(item);
@ -226,10 +216,5 @@ class TestingRosterStateManager extends BaseRosterStateManager {
} }
@override @override
Future<void> commitRoster( Future<void> commitRoster(String? version, List<String> removed, List<XmppRosterItem> modified, List<XmppRosterItem> added) async {}
String? version,
List<String> removed,
List<XmppRosterItem> modified,
List<XmppRosterItem> added,
) async {}
} }

View File

@ -1 +1,6 @@
enum RoutingState { error, preConnection, negotiating, handleStanzas } enum RoutingState {
error,
preConnection,
negotiating,
handleStanzas
}

View File

@ -1,12 +1,8 @@
import 'package:moxxmpp/src/jid.dart'; import 'package:moxxmpp/src/jid.dart';
class ConnectionSettings { class ConnectionSettings {
ConnectionSettings({
required this.jid, ConnectionSettings({ required this.jid, required this.password, required this.useDirectTLS, required this.allowPlainAuth });
required this.password,
required this.useDirectTLS,
required this.allowPlainAuth,
});
final JID jid; final JID jid;
final String password; final String password;
final bool useDirectTLS; final bool useDirectTLS;

View File

@ -25,20 +25,20 @@ abstract class BaseSocketWrapper {
/// This must return events generated by the socket. /// This must return events generated by the socket.
/// See sub-classes of [XmppSocketEvent] for possible events. /// See sub-classes of [XmppSocketEvent] for possible events.
Stream<XmppSocketEvent> getEventStream(); Stream<XmppSocketEvent> getEventStream();
/// This must close the socket but not the streams so that the same class can be /// This must close the socket but not the streams so that the same class can be
/// reused by calling [this.connect] again. /// reused by calling [this.connect] again.
void close(); void close();
/// Write [data] into the socket. If [redact] is not null, then [redact] will be /// Write [data] into the socket. If [redact] is not null, then [redact] will be
/// logged instead of [data]. /// logged instead of [data].
void write(String data, {String? redact}); void write(String data, { String? redact });
/// This must connect to [host]:[port] and initialize the streams accordingly. /// This must connect to [host]:[port] and initialize the streams accordingly.
/// [domain] is the domain that TLS should be validated against, in case the Socket /// [domain] is the domain that TLS should be validated against, in case the Socket
/// provides TLS encryption. Returns true if the connection has been successfully /// provides TLS encryption. Returns true if the connection has been successfully
/// established. Returns false if the connection has failed. /// established. Returns false if the connection has failed.
Future<bool> connect(String domain, {String? host, int? port}); Future<bool> connect(String domain, { String? host, int? port });
/// Returns true if the socket is secured, e.g. using TLS. /// Returns true if the socket is secured, e.g. using TLS.
bool isSecure(); bool isSecure();

View File

@ -25,83 +25,59 @@ class StanzaError {
class Stanza extends XMLNode { class Stanza extends XMLNode {
// ignore: use_super_parameters // ignore: use_super_parameters
Stanza({ Stanza({ this.to, this.from, this.type, this.id, List<XMLNode> children = const [], required String tag, Map<String, String> attributes = const {} }) : super(
this.to, tag: tag,
this.from, attributes: <String, dynamic>{
this.type, ...attributes,
this.id, ...type != null ? <String, dynamic>{ 'type': type } : <String, dynamic>{},
List<XMLNode> children = const [], ...id != null ? <String, dynamic>{ 'id': id } : <String, dynamic>{},
required String tag, ...to != null ? <String, dynamic>{ 'to': to } : <String, dynamic>{},
Map<String, String> attributes = const {}, ...from != null ? <String, dynamic>{ 'from': from } : <String, dynamic>{},
}) : super( 'xmlns': stanzaXmlns
tag: tag, },
attributes: <String, dynamic>{ children: children,
...attributes, );
...type != null
? <String, dynamic>{'type': type}
: <String, dynamic>{},
...id != null ? <String, dynamic>{'id': id} : <String, dynamic>{},
...to != null ? <String, dynamic>{'to': to} : <String, dynamic>{},
...from != null
? <String, dynamic>{'from': from}
: <String, dynamic>{},
'xmlns': stanzaXmlns
},
children: children,
);
factory Stanza.iq({ factory Stanza.iq({ String? to, String? from, String? type, String? id, List<XMLNode> children = const [], Map<String, String>? attributes = const {} }) {
String? to,
String? from,
String? type,
String? id,
List<XMLNode> children = const [],
Map<String, String>? attributes = const {},
}) {
return Stanza( return Stanza(
tag: 'iq', tag: 'iq',
from: from, from: from,
to: to, to: to,
id: id, id: id,
type: type, type: type,
attributes: <String, String>{...attributes!, 'xmlns': stanzaXmlns}, attributes: <String, String>{
...attributes!,
'xmlns': stanzaXmlns
},
children: children, children: children,
); );
} }
factory Stanza.presence({ factory Stanza.presence({ String? to, String? from, String? type, String? id, List<XMLNode> children = const [], Map<String, String>? attributes = const {} }) {
String? to,
String? from,
String? type,
String? id,
List<XMLNode> children = const [],
Map<String, String>? attributes = const {},
}) {
return Stanza( return Stanza(
tag: 'presence', tag: 'presence',
from: from, from: from,
to: to, to: to,
id: id, id: id,
type: type, type: type,
attributes: <String, String>{...attributes!, 'xmlns': stanzaXmlns}, attributes: <String, String>{
...attributes!,
'xmlns': stanzaXmlns
},
children: children, children: children,
); );
} }
factory Stanza.message({ factory Stanza.message({ String? to, String? from, String? type, String? id, List<XMLNode> children = const [], Map<String, String>? attributes = const {} }) {
String? to,
String? from,
String? type,
String? id,
List<XMLNode> children = const [],
Map<String, String>? attributes = const {},
}) {
return Stanza( return Stanza(
tag: 'message', tag: 'message',
from: from, from: from,
to: to, to: to,
id: id, id: id,
type: type, type: type,
attributes: <String, String>{...attributes!, 'xmlns': stanzaXmlns}, attributes: <String, String>{
...attributes!,
'xmlns': stanzaXmlns
},
children: children, children: children,
); );
} }
@ -116,10 +92,10 @@ class Stanza extends XMLNode {
children: node.children, children: node.children,
// TODO(Unknown): Remove to, from, id, and type // TODO(Unknown): Remove to, from, id, and type
// TODO(Unknown): Not sure if this is the correct way to approach this // TODO(Unknown): Not sure if this is the correct way to approach this
attributes: attributes: node.attributes
node.attributes.map<String, String>((String key, dynamic value) { .map<String, String>((String key, dynamic value) {
return MapEntry(key, value.toString()); return MapEntry(key, value.toString());
}), }),
); );
} }
@ -128,13 +104,7 @@ class Stanza extends XMLNode {
String? type; String? type;
String? id; String? id;
Stanza copyWith({ Stanza copyWith({ String? id, String? from, String? to, String? type, List<XMLNode>? children }) {
String? id,
String? from,
String? to,
String? type,
List<XMLNode>? children,
}) {
return Stanza( return Stanza(
tag: tag, tag: tag,
to: to ?? this.to, to: to ?? this.to,
@ -149,23 +119,21 @@ class Stanza extends XMLNode {
/// Build an <error /> element with a child <[condition] type="[type]" />. If [text] /// Build an <error /> element with a child <[condition] type="[type]" />. If [text]
/// is not null, then the condition element will contain a <text /> element with [text] /// is not null, then the condition element will contain a <text /> element with [text]
/// as the body. /// as the body.
XMLNode buildErrorElement(String type, String condition, {String? text}) { XMLNode buildErrorElement(String type, String condition, { String? text }) {
return XMLNode( return XMLNode(
tag: 'error', tag: 'error',
attributes: <String, dynamic>{'type': type}, attributes: <String, dynamic>{ 'type': type },
children: [ children: [
XMLNode.xmlns( XMLNode.xmlns(
tag: condition, tag: condition,
xmlns: fullStanzaXmlns, xmlns: fullStanzaXmlns,
children: text != null children: text != null ? [
? [ XMLNode.xmlns(
XMLNode.xmlns( tag: 'text',
tag: 'text', xmlns: fullStanzaXmlns,
xmlns: fullStanzaXmlns, text: text,
text: text, )
) ] : [],
]
: [],
), ),
], ],
); );

View File

@ -10,15 +10,13 @@ class XMLNode {
this.isDeclaration = false, this.isDeclaration = false,
}); });
XMLNode.xmlns({ XMLNode.xmlns({
required this.tag, required this.tag,
required String xmlns, required String xmlns,
Map<String, String> attributes = const <String, String>{}, Map<String, String> attributes = const <String, String>{},
this.children = const [], this.children = const [],
this.closeTag = true, this.closeTag = true,
this.text, this.text,
}) : attributes = <String, String>{'xmlns': xmlns, ...attributes}, }) : attributes = <String, String>{ 'xmlns': xmlns, ...attributes }, isDeclaration = false;
isDeclaration = false;
/// Because this API is better ;) /// Because this API is better ;)
/// Don't use in production. Just for testing /// Don't use in production. Just for testing
factory XMLNode.fromXmlElement(XmlElement element) { factory XMLNode.fromXmlElement(XmlElement element) {
@ -38,12 +36,10 @@ class XMLNode {
return XMLNode( return XMLNode(
tag: element.name.qualified, tag: element.name.qualified,
attributes: attributes, attributes: attributes,
children: children: element.childElements.toList().map(XMLNode.fromXmlElement).toList(),
element.childElements.toList().map(XMLNode.fromXmlElement).toList(),
); );
} }
} }
/// Just for testing purposes /// Just for testing purposes
factory XMLNode.fromString(String str) { factory XMLNode.fromString(String str) {
return XMLNode.fromXmlElement( return XMLNode.fromXmlElement(
@ -65,16 +61,13 @@ class XMLNode {
/// Renders the attributes of the node into "attr1=\"value\" attr2=...". /// Renders the attributes of the node into "attr1=\"value\" attr2=...".
String renderAttributes() { String renderAttributes() {
return attributes.keys.map((String key) { return attributes.keys.map((String key) {
final dynamic value = attributes[key]; final dynamic value = attributes[key];
assert( assert(value is String || value is int, 'XML values must either be string or int');
value is String || value is int, if (value is String) {
'XML values must either be string or int', return "$key='$value'";
); } else {
if (value is String) { return '$key=$value';
return "$key='$value'"; }
} else {
return '$key=$value';
}
}).join(' '); }).join(' ');
} }
@ -87,8 +80,8 @@ class XMLNode {
return '<$tag$attrString>$text</$tag>'; return '<$tag$attrString>$text</$tag>';
} else { } else {
return '<$decl$tag ${renderAttributes()}${closeTag ? " />" : "$decl>"}'; return '<$decl$tag ${renderAttributes()}${closeTag ? " />" : "$decl>"}';
} }
} else { } else {
final childXml = children.map((child) => child.toXml()).join(); final childXml = children.map((child) => child.toXml()).join();
final xml = '<$decl$tag ${renderAttributes()}$decl>$childXml'; final xml = '<$decl$tag ${renderAttributes()}$decl>$childXml';
return xml + (closeTag ? '</$tag>' : ''); return xml + (closeTag ? '</$tag>' : '');
@ -100,16 +93,16 @@ class XMLNode {
XMLNode? _firstTag(bool Function(XMLNode) test) { XMLNode? _firstTag(bool Function(XMLNode) test) {
try { try {
return children.firstWhere(test); return children.firstWhere(test);
} catch (e) { } catch(e) {
return null; return null;
} }
} }
/// Returns the first xml node that matches the description: /// Returns the first xml node that matches the description:
/// - node's tag is equal to [tag] /// - node's tag is equal to [tag]
/// - (optional) node's xmlns attribute is equal to [xmlns] /// - (optional) node's xmlns attribute is equal to [xmlns]
/// Returns null if none is found. /// Returns null if none is found.
XMLNode? firstTag(String tag, {String? xmlns}) { XMLNode? firstTag(String tag, { String? xmlns}) {
return _firstTag((node) { return _firstTag((node) {
if (xmlns != null) { if (xmlns != null) {
return node.tag == tag && node.attributes['xmlns'] == xmlns; return node.tag == tag && node.attributes['xmlns'] == xmlns;
@ -126,22 +119,21 @@ class XMLNode {
return node.attributes['xmlns'] == xmlns; return node.attributes['xmlns'] == xmlns;
}); });
} }
/// Returns all children whose tag is equal to [tag]. /// Returns all children whose tag is equal to [tag].
List<XMLNode> findTags(String tag, {String? xmlns}) { List<XMLNode> findTags(String tag, { String? xmlns }) {
return children.where((element) { return children.where((element) {
final xmlnsMatches = final xmlnsMatches = xmlns != null ? element.attributes['xmlns'] == xmlns : true;
xmlns != null ? element.attributes['xmlns'] == xmlns : true;
return element.tag == tag && xmlnsMatches; return element.tag == tag && xmlnsMatches;
}).toList(); }).toList();
} }
List<XMLNode> findTagsByXmlns(String xmlns) { List<XMLNode> findTagsByXmlns(String xmlns) {
return children return children
.where((element) => element.attributes['xmlns'] == xmlns) .where((element) => element.attributes['xmlns'] == xmlns)
.toList(); .toList();
} }
/// Returns the inner text of the node. If none is set, returns the "". /// Returns the inner text of the node. If none is set, returns the "".
String innerText() { String innerText() {
return text ?? ''; return text ?? '';

View File

@ -1,9 +1,6 @@
class Result<T, V> { class Result<T, V> {
const Result(this._data)
: assert( const Result(this._data) : assert(_data is T || _data is V, 'Invalid data type: Must be either $T or $V');
_data is T || _data is V,
'Invalid data type: Must be either $T or $V',
);
final dynamic _data; final dynamic _data;
bool isType<S>() => _data is S; bool isType<S>() => _data is S;

View File

@ -14,7 +14,7 @@ class AsyncQueue {
/// The actual job queue. /// The actual job queue.
final Queue<AsyncQueueJob> _queue = Queue<AsyncQueueJob>(); final Queue<AsyncQueueJob> _queue = Queue<AsyncQueueJob>();
/// Indicates whether we are currently executing a job. /// Indicates whether we are currently executing a job.
bool _running = false; bool _running = false;
@ -23,7 +23,7 @@ class AsyncQueue {
@visibleForTesting @visibleForTesting
bool get isRunning => _running; bool get isRunning => _running;
/// Adds a job [job] to the queue. /// Adds a job [job] to the queue.
Future<void> addJob(AsyncQueueJob job) async { Future<void> addJob(AsyncQueueJob job) async {
await _lock.synchronized(() { await _lock.synchronized(() {
@ -39,7 +39,7 @@ class AsyncQueue {
Future<void> clear() async { Future<void> clear() async {
await _lock.synchronized(_queue.clear); await _lock.synchronized(_queue.clear);
} }
Future<void> _popJob() async { Future<void> _popJob() async {
final job = _queue.removeFirst(); final job = _queue.removeFirst();
final future = job(); final future = job();

View File

@ -53,7 +53,7 @@ class WaitForTracker<K, V> {
} }
}); });
} }
/// Remove all tasks from the tracker. /// Remove all tasks from the tracker.
Future<void> clear() async { Future<void> clear() async {
await _lock.synchronized(_tracker.clear); await _lock.synchronized(_tracker.clear);

View File

@ -8,23 +8,20 @@ const blurhashThumbnailType = '$fileThumbnailsXmlns:blurhash';
abstract class Thumbnail {} abstract class Thumbnail {}
class BlurhashThumbnail extends Thumbnail { class BlurhashThumbnail extends Thumbnail {
BlurhashThumbnail(this.hash); BlurhashThumbnail(this.hash);
final String hash; final String hash;
} }
Thumbnail? parseFileThumbnailElement(XMLNode node) { Thumbnail? parseFileThumbnailElement(XMLNode node) {
assert( assert(node.attributes['xmlns'] == fileThumbnailsXmlns, 'Invalid element xmlns');
node.attributes['xmlns'] == fileThumbnailsXmlns,
'Invalid element xmlns',
);
assert(node.tag == 'file-thumbnail', 'Invalid element name'); assert(node.tag == 'file-thumbnail', 'Invalid element name');
switch (node.attributes['type']!) { switch (node.attributes['type']!) {
case blurhashThumbnailType: case blurhashThumbnailType: {
{ final hash = node.firstTag('blurhash')!.innerText();
final hash = node.firstTag('blurhash')!.innerText(); return BlurhashThumbnail(hash);
return BlurhashThumbnail(hash); }
}
} }
return null; return null;
@ -51,7 +48,7 @@ XMLNode constructFileThumbnailElement(Thumbnail thumbnail) {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'file-thumbnail', tag: 'file-thumbnail',
xmlns: fileThumbnailsXmlns, xmlns: fileThumbnailsXmlns,
attributes: {'type': type}, attributes: { 'type': type },
children: [node], children: [ node ],
); );
} }

View File

@ -15,38 +15,34 @@ class FileUploadNotificationManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'file-upload', tagName: 'file-upload',
tagXmlns: fileUploadNotificationXmlns, tagXmlns: fileUploadNotificationXmlns,
callback: _onFileUploadNotificationReceived, callback: _onFileUploadNotificationReceived,
priority: -99, priority: -99,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'replaces', tagName: 'replaces',
tagXmlns: fileUploadNotificationXmlns, tagXmlns: fileUploadNotificationXmlns,
callback: _onFileUploadNotificationReplacementReceived, callback: _onFileUploadNotificationReplacementReceived,
priority: -99, priority: -99,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'cancelled', tagName: 'cancelled',
tagXmlns: fileUploadNotificationXmlns, tagXmlns: fileUploadNotificationXmlns,
callback: _onFileUploadNotificationCancellationReceived, callback: _onFileUploadNotificationCancellationReceived,
priority: -99, priority: -99,
), ),
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onFileUploadNotificationReceived( Future<StanzaHandlerData> _onFileUploadNotificationReceived(Stanza message, StanzaHandlerData state) async {
Stanza message, final funElement = message.firstTag('file-upload', xmlns: fileUploadNotificationXmlns)!;
StanzaHandlerData state,
) async {
final funElement =
message.firstTag('file-upload', xmlns: fileUploadNotificationXmlns)!;
return state.copyWith( return state.copyWith(
fun: FileMetadataData.fromXML( fun: FileMetadataData.fromXML(
funElement.firstTag('file', xmlns: fileMetadataXmlns)!, funElement.firstTag('file', xmlns: fileMetadataXmlns)!,
@ -54,23 +50,15 @@ class FileUploadNotificationManager extends XmppManagerBase {
); );
} }
Future<StanzaHandlerData> _onFileUploadNotificationReplacementReceived( Future<StanzaHandlerData> _onFileUploadNotificationReplacementReceived(Stanza message, StanzaHandlerData state) async {
Stanza message, final element = message.firstTag('replaces', xmlns: fileUploadNotificationXmlns)!;
StanzaHandlerData state,
) async {
final element =
message.firstTag('replaces', xmlns: fileUploadNotificationXmlns)!;
return state.copyWith( return state.copyWith(
funReplacement: element.attributes['id']! as String, funReplacement: element.attributes['id']! as String,
); );
} }
Future<StanzaHandlerData> _onFileUploadNotificationCancellationReceived( Future<StanzaHandlerData> _onFileUploadNotificationCancellationReceived(Stanza message, StanzaHandlerData state) async {
Stanza message, final element = message.firstTag('cancels', xmlns: fileUploadNotificationXmlns)!;
StanzaHandlerData state,
) async {
final element =
message.firstTag('cancels', xmlns: fileUploadNotificationXmlns)!;
return state.copyWith( return state.copyWith(
funCancellation: element.attributes['id']! as String, funCancellation: element.attributes['id']! as String,
); );

View File

@ -3,16 +3,14 @@ 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;
XMLNode toXml() { XMLNode toXml() {
return XMLNode( return XMLNode(
tag: 'option', tag: 'option',
attributes: label != null attributes: label != null ? <String, dynamic>{ 'label': label } : <String, dynamic>{},
? <String, dynamic>{'label': label}
: <String, dynamic>{},
children: [ children: [
XMLNode( XMLNode(
tag: 'value', tag: 'value',
@ -25,13 +23,13 @@ class DataFormOption {
class DataFormField { class DataFormField {
const DataFormField({ const DataFormField({
required this.options, required this.options,
required this.values, required this.values,
required this.isRequired, required this.isRequired,
this.varAttr, this.varAttr,
this.type, this.type,
this.description, this.description,
this.label, this.label,
}); });
final String? description; final String? description;
final bool isRequired; final bool isRequired;
@ -45,13 +43,9 @@ class DataFormField {
return XMLNode( return XMLNode(
tag: 'field', tag: 'field',
attributes: <String, dynamic>{ attributes: <String, dynamic>{
...varAttr != null ...varAttr != null ? <String, dynamic>{ 'var': varAttr } : <String, dynamic>{},
? <String, dynamic>{'var': varAttr} ...type != null ? <String, dynamic>{ 'type': type } : <String, dynamic>{},
: <String, dynamic>{}, ...label != null ? <String, dynamic>{ 'label': label } : <String, dynamic>{}
...type != null ? <String, dynamic>{'type': type} : <String, dynamic>{},
...label != null
? <String, dynamic>{'label': label}
: <String, dynamic>{}
}, },
children: [ children: [
...description != null ? [XMLNode(tag: 'desc', text: description)] : [], ...description != null ? [XMLNode(tag: 'desc', text: description)] : [],
@ -65,12 +59,12 @@ class DataFormField {
class DataForm { class DataForm {
const DataForm({ const DataForm({
required this.type, required this.type,
required this.instructions, required this.instructions,
required this.fields, required this.fields,
required this.reported, required this.reported,
required this.items, required this.items,
this.title, this.title,
}); });
final String type; final String type;
final String? title; final String? title;
@ -82,23 +76,23 @@ class DataForm {
DataFormField? getFieldByVar(String varAttr) { DataFormField? getFieldByVar(String varAttr) {
return firstWhereOrNull(fields, (field) => field.varAttr == varAttr); return firstWhereOrNull(fields, (field) => field.varAttr == varAttr);
} }
XMLNode toXml() { XMLNode toXml() {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'x', tag: 'x',
xmlns: dataFormsXmlns, xmlns: dataFormsXmlns,
attributes: {'type': type}, attributes: {
'type': type
},
children: [ children: [
...instructions.map((i) => XMLNode(tag: 'instruction', text: i)), ...instructions.map((i) => XMLNode(tag: 'instruction', text: i)),
...title != null ? [XMLNode(tag: 'title', text: title)] : [], ...title != null ? [XMLNode(tag: 'title', text: title)] : [],
...fields.map((field) => field.toXml()), ...fields.map((field) => field.toXml()),
...reported.map((report) => report.toXml()), ...reported.map((report) => report.toXml()),
...items.map( ...items.map((item) => XMLNode(
(item) => XMLNode( tag: 'item',
tag: 'item', children: item.map((i) => i.toXml()).toList(),
children: item.map((i) => i.toXml()).toList(), ),),
),
),
], ],
); );
} }
@ -134,19 +128,10 @@ DataForm parseDataForm(XMLNode x) {
final type = x.attributes['type']! as String; final type = x.attributes['type']! as String;
final title = x.firstTag('title')?.innerText(); final title = x.firstTag('title')?.innerText();
final instructions = final instructions = x.findTags('instructions').map((i) => i.innerText()).toList();
x.findTags('instructions').map((i) => i.innerText()).toList();
final fields = x.findTags('field').map(_parseDataFormField).toList(); final fields = x.findTags('field').map(_parseDataFormField).toList();
final reported = x final reported = x.firstTag('reported')?.findTags('field').map((i) => _parseDataFormField(i.firstTag('field')!)).toList() ?? [];
.firstTag('reported') final items = x.findTags('item').map((i) => i.findTags('field').map(_parseDataFormField).toList()).toList();
?.findTags('field')
.map((i) => _parseDataFormField(i.firstTag('field')!))
.toList() ??
[];
final items = x
.findTags('item')
.map((i) => i.findTags('field').map(_parseDataFormField).toList())
.toList();
return DataForm( return DataForm(
type: type, type: type,

View File

@ -4,7 +4,7 @@ import 'package:meta/meta.dart';
@immutable @immutable
class DiscoCacheKey { class DiscoCacheKey {
const DiscoCacheKey(this.jid, this.node); const DiscoCacheKey(this.jid, this.node);
/// The JID we're requesting disco data from. /// The JID we're requesting disco data from.
final String jid; final String jid;
@ -13,9 +13,11 @@ class DiscoCacheKey {
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
return other is DiscoCacheKey && jid == other.jid && node == other.node; return other is DiscoCacheKey &&
jid == other.jid &&
node == other.node;
} }
@override @override
int get hashCode => jid.hashCode ^ node.hashCode; int get hashCode => jid.hashCode ^ node.hashCode;
} }

View File

@ -5,29 +5,21 @@ import 'package:moxxmpp/src/stringxml.dart';
// TODO(PapaTutuWawa): Move types into types.dart // TODO(PapaTutuWawa): Move types into types.dart
Stanza buildDiscoInfoQueryStanza(String entity, String? node) { Stanza buildDiscoInfoQueryStanza(String entity, String? node) {
return Stanza.iq( return Stanza.iq(to: entity, type: 'get', children: [
to: entity, XMLNode.xmlns(
type: 'get', tag: 'query',
children: [ xmlns: discoInfoXmlns,
XMLNode.xmlns( attributes: node != null ? { 'node': node } : {},
tag: 'query', )
xmlns: discoInfoXmlns, ],);
attributes: node != null ? {'node': node} : {},
)
],
);
} }
Stanza buildDiscoItemsQueryStanza(String entity, {String? node}) { Stanza buildDiscoItemsQueryStanza(String entity, { String? node }) {
return Stanza.iq( return Stanza.iq(to: entity, type: 'get', children: [
to: entity, XMLNode.xmlns(
type: 'get', tag: 'query',
children: [ xmlns: discoItemsXmlns,
XMLNode.xmlns( attributes: node != null ? { 'node': node } : {},
tag: 'query', )
xmlns: discoItemsXmlns, ],);
attributes: node != null ? {'node': node} : {},
)
],
);
} }

View File

@ -5,12 +5,7 @@ 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({ const Identity({ required this.category, required this.type, this.name, this.lang });
required this.category,
required this.type,
this.name,
this.lang,
});
final String category; final String category;
final String type; final String type;
final String? name; final String? name;
@ -23,9 +18,7 @@ class Identity {
'category': category, 'category': category,
'type': type, 'type': type,
'name': name, 'name': name,
...lang == null ...lang == null ? <String, dynamic>{} : <String, dynamic>{ 'xml:lang': lang }
? <String, dynamic>{}
: <String, dynamic>{'xml:lang': lang}
}, },
); );
} }
@ -57,8 +50,7 @@ class DiscoInfo {
name: element.attributes['name'] as String?, name: element.attributes['name'] as String?,
), ),
); );
} else if (element.tag == 'x' && } else if (element.tag == 'x' && element.attributes['xmlns'] == dataFormsXmlns) {
element.attributes['xmlns'] == dataFormsXmlns) {
extendedInfo.add( extendedInfo.add(
parseDataForm(element), parseDataForm(element),
); );
@ -84,22 +76,18 @@ class DiscoInfo {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'query', tag: 'query',
xmlns: discoInfoXmlns, xmlns: discoInfoXmlns,
attributes: node != null attributes: node != null ?
? <String, String>{ <String, String>{ 'node': node!, } :
'node': node!, <String, String>{},
}
: <String, String>{},
children: [ children: [
...identities.map((identity) => identity.toXMLNode()), ...identities.map((identity) => identity.toXMLNode()),
...features.map( ...features.map((feature) => XMLNode(
(feature) => XMLNode( tag: 'feature',
tag: 'feature', attributes: { 'var': feature, },
attributes: { ),),
'var': feature,
}, if (extendedInfo.isNotEmpty)
), ...extendedInfo.map((ei) => ei.toXml()),
),
if (extendedInfo.isNotEmpty) ...extendedInfo.map((ei) => ei.toXml()),
], ],
); );
} }
@ -107,7 +95,7 @@ class DiscoInfo {
@immutable @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;

View File

@ -32,15 +32,15 @@ class DiscoManager extends XmppManagerBase {
/// [identities] is a list of disco identities that should be added by default /// [identities] is a list of disco identities that should be added by default
/// to a disco#info response. /// to a disco#info response.
DiscoManager(List<Identity> identities) DiscoManager(List<Identity> identities)
: _identities = List<Identity>.from(identities), : _identities = List<Identity>.from(identities),
super(discoManager); super(discoManager);
/// Our features /// Our features
final List<String> _features = List.empty(growable: true); final List<String> _features = List.empty(growable: true);
/// Disco identities that we advertise /// Disco identities that we advertise
final List<Identity> _identities; final List<Identity> _identities;
/// Map full JID to Capability hashes /// Map full JID to Capability hashes
final Map<String, CapabilityHashInfo> _capHashCache = {}; final Map<String, CapabilityHashInfo> _capHashCache = {};
@ -51,12 +51,10 @@ class DiscoManager extends XmppManagerBase {
final Map<DiscoCacheKey, DiscoInfo> _discoInfoCache = {}; final Map<DiscoCacheKey, DiscoInfo> _discoInfoCache = {};
/// The tracker for tracking disco#info queries that are in flight. /// The tracker for tracking disco#info queries that are in flight.
final WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> final WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> _discoInfoTracker = WaitForTracker();
_discoInfoTracker = WaitForTracker();
/// The tracker for tracking disco#info queries that are in flight. /// The tracker for tracking disco#info queries that are in flight.
final WaitForTracker<DiscoCacheKey, Result<DiscoError, List<DiscoItem>>> final WaitForTracker<DiscoCacheKey, Result<DiscoError, List<DiscoItem>>> _discoItemsTracker = WaitForTracker();
_discoItemsTracker = WaitForTracker();
/// Cache lock /// Cache lock
final Lock _cacheLock = Lock(); final Lock _cacheLock = Lock();
@ -74,27 +72,26 @@ class DiscoManager extends XmppManagerBase {
List<String> get features => _features; List<String> get features => _features;
@visibleForTesting @visibleForTesting
WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> WaitForTracker<DiscoCacheKey, Result<DiscoError, DiscoInfo>> get infoTracker => _discoInfoTracker;
get infoTracker => _discoInfoTracker;
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
tagName: 'query', tagName: 'query',
tagXmlns: discoInfoXmlns, tagXmlns: discoInfoXmlns,
stanzaTag: 'iq', stanzaTag: 'iq',
callback: _onDiscoInfoRequest, callback: _onDiscoInfoRequest,
), ),
StanzaHandler( StanzaHandler(
tagName: 'query', tagName: 'query',
tagXmlns: discoItemsXmlns, tagXmlns: discoItemsXmlns,
stanzaTag: 'iq', stanzaTag: 'iq',
callback: _onDiscoItemsRequest, callback: _onDiscoItemsRequest,
), ),
]; ];
@override @override
List<String> getDiscoFeatures() => [discoInfoXmlns, discoItemsXmlns]; List<String> getDiscoFeatures() => [ discoInfoXmlns, discoItemsXmlns ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
@ -117,7 +114,7 @@ class DiscoManager extends XmppManagerBase {
await _discoItemsTracker.resolveAll( await _discoItemsTracker.resolveAll(
Result<DiscoError, List<DiscoItem>>(UnknownDiscoError()), Result<DiscoError, List<DiscoItem>>(UnknownDiscoError()),
); );
await _cacheLock.synchronized(() async { await _cacheLock.synchronized(() async {
// Clear the cache // Clear the cache
_discoInfoCache.clear(); _discoInfoCache.clear();
@ -134,7 +131,7 @@ class DiscoManager extends XmppManagerBase {
void registerItemsCallback(String node, DiscoItemsRequestCallback callback) { void registerItemsCallback(String node, DiscoItemsRequestCallback callback) {
_discoItemsCallbacks[node] = 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 addFeatures(List<String> features) { void addFeatures(List<String> features) {
@ -154,7 +151,7 @@ class DiscoManager extends XmppManagerBase {
} }
} }
} }
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;
@ -164,7 +161,7 @@ class DiscoManager extends XmppManagerBase {
c.attributes['node']! as String, c.attributes['node']! as String,
c.attributes['hash']! as String, c.attributes['hash']! as String,
); );
// Check if we already know of that cache // Check if we already know of that cache
var cached = false; var cached = false;
await _cacheLock.synchronized(() async { await _cacheLock.synchronized(() async {
@ -175,11 +172,8 @@ class DiscoManager extends XmppManagerBase {
if (cached) return; if (cached) return;
// Request the cap hash // Request the cap hash
logger.finest( logger.finest("Received capability hash we don't know about. Requesting it...");
"Received capability hash we don't know about. Requesting it...", final result = await discoInfoQuery(from.toString(), node: '${info.node}#${info.ver}');
);
final result =
await discoInfoQuery(from.toString(), node: '${info.node}#${info.ver}');
if (result.isType<DiscoError>()) return; if (result.isType<DiscoError>()) return;
await _cacheLock.synchronized(() async { await _cacheLock.synchronized(() async {
@ -187,7 +181,7 @@ class DiscoManager extends XmppManagerBase {
_capHashInfoCache[info.ver] = result.get<DiscoInfo>(); _capHashInfoCache[info.ver] = result.get<DiscoInfo>();
}); });
} }
/// Returns the [DiscoInfo] object that would be used as the response to a disco#info /// Returns the [DiscoInfo] object that would be used as the response to a disco#info
/// query against our bare JID with no node. The results node attribute is set /// query against our bare JID with no node. The results node attribute is set
/// to [node]. /// to [node].
@ -200,11 +194,8 @@ class DiscoManager extends XmppManagerBase {
null, null,
); );
} }
Future<StanzaHandlerData> _onDiscoInfoRequest( Future<StanzaHandlerData> _onDiscoInfoRequest(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
if (stanza.type != 'get') return state; if (stanza.type != 'get') return state;
final query = stanza.firstTag('query', xmlns: discoInfoXmlns)!; final query = stanza.firstTag('query', xmlns: discoInfoXmlns)!;
@ -235,10 +226,7 @@ class DiscoManager extends XmppManagerBase {
return state.copyWith(done: true); return state.copyWith(done: true);
} }
Future<StanzaHandlerData> _onDiscoItemsRequest( Future<StanzaHandlerData> _onDiscoItemsRequest(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
if (stanza.type != 'get') return state; if (stanza.type != 'get') return state;
final query = stanza.firstTag('query', xmlns: discoItemsXmlns)!; final query = stanza.firstTag('query', xmlns: discoItemsXmlns)!;
@ -266,10 +254,7 @@ class DiscoManager extends XmppManagerBase {
return state; return state;
} }
Future<void> _exitDiscoInfoCriticalSection( Future<void> _exitDiscoInfoCriticalSection(DiscoCacheKey key, Result<DiscoError, DiscoInfo> result) async {
DiscoCacheKey key,
Result<DiscoError, DiscoInfo> result,
) async {
await _cacheLock.synchronized(() async { await _cacheLock.synchronized(() async {
// Add to cache if it is a result // Add to cache if it is a result
if (result.isType<DiscoInfo>()) { if (result.isType<DiscoInfo>()) {
@ -279,18 +264,12 @@ class DiscoManager extends XmppManagerBase {
await _discoInfoTracker.resolve(key, result); 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( Future<Result<DiscoError, DiscoInfo>> discoInfoQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
String entity, {
String? node,
bool shouldEncrypt = true,
}) async {
final cacheKey = DiscoCacheKey(entity, node); final cacheKey = DiscoCacheKey(entity, node);
DiscoInfo? info; DiscoInfo? info;
final ffuture = await _cacheLock final ffuture = await _cacheLock.synchronized<Future<Future<Result<DiscoError, DiscoInfo>>?>?>(() async {
.synchronized<Future<Future<Result<DiscoError, DiscoInfo>>?>?>(
() 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];
@ -326,7 +305,7 @@ class DiscoManager extends XmppManagerBase {
await _exitDiscoInfoCriticalSection(cacheKey, result); await _exitDiscoInfoCriticalSection(cacheKey, result);
return result; return result;
} }
final result = Result<DiscoError, DiscoInfo>( final result = Result<DiscoError, DiscoInfo>(
DiscoInfo.fromQuery( DiscoInfo.fromQuery(
query, query,
@ -338,26 +317,22 @@ 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( Future<Result<DiscoError, List<DiscoItem>>> discoItemsQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
String entity, {
String? node,
bool shouldEncrypt = true,
}) async {
final key = DiscoCacheKey(entity, node); final key = DiscoCacheKey(entity, node);
final future = await _discoItemsTracker.waitFor(key); final future = await _discoItemsTracker.waitFor(key);
if (future != null) { if (future != null) {
return future; return future;
} }
final stanza = await getAttributes().sendStanza( final stanza = await getAttributes()
buildDiscoItemsQueryStanza(entity, node: node), .sendStanza(
encrypted: !shouldEncrypt, buildDiscoItemsQueryStanza(entity, node: node),
) as Stanza; encrypted: !shouldEncrypt,
) as Stanza;
final query = stanza.firstTag('query'); final query = stanza.firstTag('query');
if (query == null) { if (query == null) {
final result = final result = Result<DiscoError, List<DiscoItem>>(InvalidResponseDiscoError());
Result<DiscoError, List<DiscoItem>>(InvalidResponseDiscoError());
await _discoItemsTracker.resolve(key, result); await _discoItemsTracker.resolve(key, result);
return result; return result;
} }
@ -365,22 +340,16 @@ class DiscoManager extends XmppManagerBase {
if (stanza.type == 'error') { if (stanza.type == 'error') {
//final error = stanza.firstTag('error'); //final error = stanza.firstTag('error');
//print("Disco Items error: " + error.toXml()); //print("Disco Items error: " + error.toXml());
final result = final result = Result<DiscoError, List<DiscoItem>>(ErrorResponseDiscoError());
Result<DiscoError, List<DiscoItem>>(ErrorResponseDiscoError());
await _discoItemsTracker.resolve(key, result); await _discoItemsTracker.resolve(key, result);
return result; return result;
} }
final items = query final items = query.findTags('item').map((node) => DiscoItem(
.findTags('item') jid: node.attributes['jid']! as String,
.map( node: node.attributes['node'] as String?,
(node) => DiscoItem( name: node.attributes['name'] as String?,
jid: node.attributes['jid']! as String, ),).toList();
node: node.attributes['node'] as String?,
name: node.attributes['name'] as String?,
),
)
.toList();
final result = Result<DiscoError, List<DiscoItem>>(items); final result = Result<DiscoError, List<DiscoItem>>(items);
await _discoItemsTracker.resolve(key, result); await _discoItemsTracker.resolve(key, result);
@ -388,11 +357,7 @@ class DiscoManager extends XmppManagerBase {
} }
/// Queries information about a jid based on its node and capability hash. /// Queries information about a jid based on its node and capability hash.
Future<Result<DiscoError, DiscoInfo>> discoInfoCapHashQuery( Future<Result<DiscoError, DiscoInfo>> discoInfoCapHashQuery(String jid, String node, String ver) async {
String jid,
String node,
String ver,
) async {
return discoInfoQuery(jid, node: '$node#$ver'); return discoInfoQuery(jid, node: '$node#$ver');
} }

View File

@ -16,12 +16,12 @@ class UnknownVCardError extends VCardError {}
class InvalidVCardError extends VCardError {} class InvalidVCardError extends VCardError {}
class VCardPhoto { class VCardPhoto {
const VCardPhoto({this.binval}); const VCardPhoto({ this.binval });
final String? binval; final String? binval;
} }
class VCard { class VCard {
const VCard({this.nickname, this.url, this.photo}); const VCard({ this.nickname, this.url, this.photo });
final String? nickname; final String? nickname;
final String? url; final String? url;
final VCardPhoto? photo; final VCardPhoto? photo;
@ -30,29 +30,26 @@ class VCard {
class VCardManager extends XmppManagerBase { class VCardManager extends XmppManagerBase {
VCardManager() : super(vcardManager); VCardManager() : super(vcardManager);
final Map<String, String> _lastHash = {}; final Map<String, String> _lastHash = {};
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'presence', stanzaTag: 'presence',
tagName: 'x', tagName: 'x',
tagXmlns: vCardTempUpdate, tagXmlns: vCardTempUpdate,
callback: _onPresence, callback: _onPresence,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
/// In case we get the avatar hash some other way. /// In case we get the avatar hash some other way.
void setLastHash(String jid, String hash) { void setLastHash(String jid, String hash) {
_lastHash[jid] = hash; _lastHash[jid] = hash;
} }
Future<StanzaHandlerData> _onPresence( Future<StanzaHandlerData> _onPresence(Stanza presence, StanzaHandlerData state) async {
Stanza presence,
StanzaHandlerData state,
) async {
final x = presence.firstTag('x', xmlns: vCardTempUpdate)!; final x = presence.firstTag('x', xmlns: vCardTempUpdate)!;
final hash = x.firstTag('photo')!.innerText(); final hash = x.firstTag('photo')!.innerText();
@ -79,10 +76,10 @@ class VCardManager extends XmppManagerBase {
logger.warning('Failed to retrieve vCard for $from'); logger.warning('Failed to retrieve vCard for $from');
} }
} }
return state.copyWith(done: true); return state.copyWith(done: true);
} }
VCardPhoto? _parseVCardPhoto(XMLNode? node) { VCardPhoto? _parseVCardPhoto(XMLNode? node) {
if (node == null) return null; if (node == null) return null;
@ -90,18 +87,18 @@ class VCardManager extends XmppManagerBase {
binval: node.firstTag('BINVAL')?.innerText(), binval: node.firstTag('BINVAL')?.innerText(),
); );
} }
VCard _parseVCard(XMLNode vcard) { VCard _parseVCard(XMLNode vcard) {
final nickname = vcard.firstTag('NICKNAME')?.innerText(); final nickname = vcard.firstTag('NICKNAME')?.innerText();
final url = vcard.firstTag('URL')?.innerText(); final url = vcard.firstTag('URL')?.innerText();
return VCard( return VCard(
url: url, url: url,
nickname: nickname, nickname: nickname,
photo: _parseVCardPhoto(vcard.firstTag('PHOTO')), photo: _parseVCardPhoto(vcard.firstTag('PHOTO')),
); );
} }
Future<Result<VCardError, VCard>> requestVCard(String jid) async { Future<Result<VCardError, VCard>> requestVCard(String jid) async {
final result = await getAttributes().sendStanza( final result = await getAttributes().sendStanza(
Stanza.iq( Stanza.iq(
@ -117,14 +114,10 @@ class VCardManager extends XmppManagerBase {
encrypted: true, encrypted: true,
); );
if (result.attributes['type'] != 'result') { if (result.attributes['type'] != 'result') return Result(UnknownVCardError());
return Result(UnknownVCardError());
}
final vcard = result.firstTag('vCard', xmlns: vCardTempXmlns); final vcard = result.firstTag('vCard', xmlns: vCardTempXmlns);
if (vcard == null) { if (vcard == null) return Result(UnknownVCardError());
return Result(UnknownVCardError());
}
return Result(_parseVCard(vcard)); return Result(_parseVCard(vcard));
} }
} }

View File

@ -20,6 +20,6 @@ PubSubError getPubSubError(XMLNode stanza) {
return EjabberdMaxItemsError(); return EjabberdMaxItemsError();
} }
} }
return UnknownPubSubError(); return UnknownPubSubError();
} }

View File

@ -22,7 +22,7 @@ class PubSubPublishOptions {
}); });
final String? accessModel; final String? accessModel;
final String? maxItems; final String? maxItems;
XMLNode toXml() { XMLNode toXml() {
return DataForm( return DataForm(
type: 'submit', type: 'submit',
@ -33,41 +33,33 @@ class PubSubPublishOptions {
const DataFormField( const DataFormField(
options: [], options: [],
isRequired: false, isRequired: false,
values: [pubsubPublishOptionsXmlns], values: [ pubsubPublishOptionsXmlns ],
varAttr: 'FORM_TYPE', varAttr: 'FORM_TYPE',
type: 'hidden', type: 'hidden',
), ),
...accessModel != null ...accessModel != null ? [
? [ DataFormField(
DataFormField( options: [],
options: [], isRequired: false,
isRequired: false, values: [ accessModel! ],
values: [accessModel!], varAttr: 'pubsub#access_model',
varAttr: 'pubsub#access_model', )
) ] : [],
] ...maxItems != null ? [
: [], DataFormField(
...maxItems != null options: [],
? [ isRequired: false,
DataFormField( values: [maxItems! ],
options: [], varAttr: 'pubsub#max_items',
isRequired: false, ),
values: [maxItems!], ] : [],
varAttr: 'pubsub#max_items',
),
]
: [],
], ],
).toXml(); ).toXml();
} }
} }
class PubSubItem { class PubSubItem {
const PubSubItem({ const PubSubItem({ required this.id, required this.node, required this.payload });
required this.id,
required this.node,
required this.payload,
});
final String id; final String id;
final String node; final String node;
final XMLNode payload; final XMLNode payload;
@ -81,37 +73,32 @@ class PubSubManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'event', tagName: 'event',
tagXmlns: pubsubEventXmlns, tagXmlns: pubsubEventXmlns,
callback: _onPubsubMessage, callback: _onPubsubMessage,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onPubsubMessage( Future<StanzaHandlerData> _onPubsubMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
logger.finest('Received PubSub event'); logger.finest('Received PubSub event');
final event = message.firstTag('event', xmlns: pubsubEventXmlns)!; final event = message.firstTag('event', xmlns: pubsubEventXmlns)!;
final items = event.firstTag('items')!; final items = event.firstTag('items')!;
final item = items.firstTag('item')!; final item = items.firstTag('item')!;
getAttributes().sendEvent( getAttributes().sendEvent(PubSubNotificationEvent(
PubSubNotificationEvent( item: PubSubItem(
item: PubSubItem( id: item.attributes['id']! as String,
id: item.attributes['id']! as String, node: items.attributes['node']! as String,
node: items.attributes['node']! as String, payload: item.children[0],
payload: item.children[0],
),
from: message.attributes['from']! as String,
), ),
); from: message.attributes['from']! as String,
),);
return state.copyWith(done: true); return state.copyWith(done: true);
} }
@ -120,37 +107,27 @@ class PubSubManager extends XmppManagerBase {
final response = await dm.discoItemsQuery(jid, node: node); final response = await dm.discoItemsQuery(jid, node: node);
var count = 0; var count = 0;
if (response.isType<DiscoError>()) { if (response.isType<DiscoError>()) {
logger.warning( logger.warning('_getNodeItemCount: disco#items query failed. Assuming no items.');
'_getNodeItemCount: disco#items query failed. Assuming no items.',
);
} else { } else {
count = response.get<List<DiscoItem>>().length; count = response.get<List<DiscoItem>>().length;
} }
return count; return count;
} }
Future<PubSubPublishOptions> _preprocessPublishOptions( Future<PubSubPublishOptions> _preprocessPublishOptions(String jid, String node, PubSubPublishOptions options) async {
String jid,
String node,
PubSubPublishOptions options,
) async {
if (options.maxItems != null) { if (options.maxItems != null) {
final dm = getAttributes().getManagerById<DiscoManager>(discoManager)!; final dm = getAttributes().getManagerById<DiscoManager>(discoManager)!;
final result = await dm.discoInfoQuery(jid); final result = await dm.discoInfoQuery(jid);
if (result.isType<DiscoError>()) { if (result.isType<DiscoError>()) {
if (options.maxItems == 'max') { if (options.maxItems == 'max') {
logger.severe( logger.severe('disco#info query failed and options.maxItems is set to "max".');
'disco#info query failed and options.maxItems is set to "max".',
);
return options; return options;
} }
} }
final nodeMultiItemsSupported = result.isType<DiscoInfo>() && final nodeMultiItemsSupported = result.isType<DiscoInfo>() && result.get<DiscoInfo>().features.contains(pubsubNodeConfigMultiItems);
result.get<DiscoInfo>().features.contains(pubsubNodeConfigMultiItems); final nodeMaxSupported = result.isType<DiscoInfo>() && result.get<DiscoInfo>().features.contains(pubsubNodeConfigMax);
final nodeMaxSupported = result.isType<DiscoInfo>() &&
result.get<DiscoInfo>().features.contains(pubsubNodeConfigMax);
if (options.maxItems != null && !nodeMultiItemsSupported) { if (options.maxItems != null && !nodeMultiItemsSupported) {
// TODO(PapaTutuWawa): Here, we need to admit defeat // TODO(PapaTutuWawa): Here, we need to admit defeat
logger.finest('PubSub host does not support multi-items!'); logger.finest('PubSub host does not support multi-items!');
@ -159,9 +136,7 @@ class PubSubManager extends XmppManagerBase {
accessModel: options.accessModel, accessModel: options.accessModel,
); );
} else if (options.maxItems == 'max' && !nodeMaxSupported) { } else if (options.maxItems == 'max' && !nodeMaxSupported) {
logger.finest( logger.finest('PubSub host does not support node-config-max. Working around it');
'PubSub host does not support node-config-max. Working around it',
);
final count = await _getNodeItemCount(jid, node) + 1; final count = await _getNodeItemCount(jid, node) + 1;
return PubSubPublishOptions( return PubSubPublishOptions(
@ -173,7 +148,7 @@ class PubSubManager extends XmppManagerBase {
return options; return options;
} }
Future<Result<PubSubError, bool>> subscribe(String jid, String node) async { Future<Result<PubSubError, bool>> subscribe(String jid, String node) async {
final attrs = getAttributes(); final attrs = getAttributes();
final result = await attrs.sendStanza( final result = await attrs.sendStanza(
@ -198,19 +173,13 @@ class PubSubManager extends XmppManagerBase {
), ),
); );
if (result.attributes['type'] != 'result') { if (result.attributes['type'] != 'result') return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns); final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns);
if (pubsub == null) { if (pubsub == null) return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
final subscription = pubsub.firstTag('subscription'); final subscription = pubsub.firstTag('subscription');
if (subscription == null) { if (subscription == null) return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
return Result(subscription.attributes['subscription'] == 'subscribed'); return Result(subscription.attributes['subscription'] == 'subscribed');
} }
@ -239,32 +208,27 @@ class PubSubManager extends XmppManagerBase {
), ),
); );
if (result.attributes['type'] != 'result') { if (result.attributes['type'] != 'result') return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns); final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns);
if (pubsub == null) { if (pubsub == null) return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
final subscription = pubsub.firstTag('subscription'); final subscription = pubsub.firstTag('subscription');
if (subscription == null) { if (subscription == null) return Result(UnknownPubSubError());
return Result(UnknownPubSubError());
}
return Result(subscription.attributes['subscription'] == 'none'); return Result(subscription.attributes['subscription'] == 'none');
} }
/// Publish [payload] to the PubSub node [node] on JID [jid]. Returns true if it /// Publish [payload] to the PubSub node [node] on JID [jid]. Returns true if it
/// was successful. False otherwise. /// was successful. False otherwise.
Future<Result<PubSubError, bool>> publish( Future<Result<PubSubError, bool>> publish(
String jid, String jid,
String node, String node,
XMLNode payload, { XMLNode payload, {
String? id, String? id,
PubSubPublishOptions? options, PubSubPublishOptions? options,
}) async { }
) async {
return _publish( return _publish(
jid, jid,
node, node,
@ -278,11 +242,12 @@ class PubSubManager extends XmppManagerBase {
String jid, String jid,
String node, String node,
XMLNode payload, { XMLNode payload, {
String? id, String? id,
PubSubPublishOptions? options, PubSubPublishOptions? options,
// Should, if publishing fails, try to reconfigure and publish again? // Should, if publishing fails, try to reconfigure and publish again?
bool tryConfigureAndPublish = true, bool tryConfigureAndPublish = true,
}) async { }
) async {
PubSubPublishOptions? pubOptions; PubSubPublishOptions? pubOptions;
if (options != null) { if (options != null) {
pubOptions = await _preprocessPublishOptions(jid, node, options); pubOptions = await _preprocessPublishOptions(jid, node, options);
@ -299,25 +264,21 @@ class PubSubManager extends XmppManagerBase {
children: [ children: [
XMLNode( XMLNode(
tag: 'publish', tag: 'publish',
attributes: <String, String>{'node': node}, attributes: <String, String>{ 'node': node },
children: [ children: [
XMLNode( XMLNode(
tag: 'item', tag: 'item',
attributes: id != null attributes: id != null ? <String, String>{ 'id': id } : <String, String>{},
? <String, String>{'id': id} children: [ payload ],
: <String, String>{},
children: [payload],
) )
], ],
), ),
...options != null ...options != null ? [
? [ XMLNode(
XMLNode( tag: 'publish-options',
tag: 'publish-options', children: [options.toXml()],
children: [options.toXml()], ),
), ] : [],
]
: [],
], ],
) )
], ],
@ -341,16 +302,10 @@ class PubSubManager extends XmppManagerBase {
options: options, options: options,
tryConfigureAndPublish: false, tryConfigureAndPublish: false,
); );
if (publishResult.isType<PubSubError>()) { if (publishResult.isType<PubSubError>()) return publishResult;
return publishResult; } else if (error is EjabberdMaxItemsError && tryConfigureAndPublish && options != null) {
}
} else if (error is EjabberdMaxItemsError &&
tryConfigureAndPublish &&
options != null) {
// TODO(Unknown): Remove once ejabberd fixes the bug. See errors.dart for more info. // TODO(Unknown): Remove once ejabberd fixes the bug. See errors.dart for more info.
logger.warning( logger.warning('Publish failed due to the server rejecting the usage of "max" for "max_items" in publish options. Configuring...');
'Publish failed due to the server rejecting the usage of "max" for "max_items" in publish options. Configuring...',
);
final count = await _getNodeItemCount(jid, node) + 1; final count = await _getNodeItemCount(jid, node) + 1;
return publish( return publish(
jid, jid,
@ -368,31 +323,20 @@ class PubSubManager extends XmppManagerBase {
} }
final pubsubElement = result.firstTag('pubsub', xmlns: pubsubXmlns); final pubsubElement = result.firstTag('pubsub', xmlns: pubsubXmlns);
if (pubsubElement == null) { if (pubsubElement == null) return Result(MalformedResponseError());
return Result(MalformedResponseError());
}
final publishElement = pubsubElement.firstTag('publish'); final publishElement = pubsubElement.firstTag('publish');
if (publishElement == null) { if (publishElement == null) return Result(MalformedResponseError());
return Result(MalformedResponseError());
}
final item = publishElement.firstTag('item'); final item = publishElement.firstTag('item');
if (item == null) { if (item == null) return Result(MalformedResponseError());
return Result(MalformedResponseError());
}
if (id != null) { if (id != null) return Result(item.attributes['id'] == id);
return Result(item.attributes['id'] == id);
}
return const Result(true); return const Result(true);
} }
Future<Result<PubSubError, List<PubSubItem>>> getItems( Future<Result<PubSubError, List<PubSubItem>>> getItems(String jid, String node) async {
String jid,
String node,
) async {
final result = await getAttributes().sendStanza( final result = await getAttributes().sendStanza(
Stanza.iq( Stanza.iq(
type: 'get', type: 'get',
@ -402,38 +346,33 @@ class PubSubManager extends XmppManagerBase {
tag: 'pubsub', tag: 'pubsub',
xmlns: pubsubXmlns, xmlns: pubsubXmlns,
children: [ children: [
XMLNode(tag: 'items', attributes: <String, String>{'node': node}), XMLNode(tag: 'items', attributes: <String, String>{ 'node': node }),
], ],
) )
], ],
), ),
); );
if (result.attributes['type'] != 'result') { if (result.attributes['type'] != 'result') return Result(getPubSubError(result));
return Result(getPubSubError(result));
}
final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns); final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns);
if (pubsub == null) { if (pubsub == null) return Result(getPubSubError(result));
return Result(getPubSubError(result));
}
final items = pubsub.firstTag('items')!.children.map((item) { final items = pubsub
return PubSubItem( .firstTag('items')!
id: item.attributes['id']! as String, .children.map((item) {
payload: item.children[0], return PubSubItem(
node: node, id: item.attributes['id']! as String,
); payload: item.children[0],
}).toList(); node: node,
);
})
.toList();
return Result(items); return Result(items);
} }
Future<Result<PubSubError, PubSubItem>> getItem( Future<Result<PubSubError, PubSubItem>> getItem(String jid, String node, String id) async {
String jid,
String node,
String id,
) async {
final result = await getAttributes().sendStanza( final result = await getAttributes().sendStanza(
Stanza.iq( Stanza.iq(
type: 'get', type: 'get',
@ -445,11 +384,11 @@ class PubSubManager extends XmppManagerBase {
children: [ children: [
XMLNode( XMLNode(
tag: 'items', tag: 'items',
attributes: <String, String>{'node': node}, attributes: <String, String>{ 'node': node },
children: [ children: [
XMLNode( XMLNode(
tag: 'item', tag: 'item',
attributes: <String, String>{'id': id}, attributes: <String, String>{ 'id': id },
), ),
], ],
), ),
@ -459,9 +398,7 @@ class PubSubManager extends XmppManagerBase {
), ),
); );
if (result.attributes['type'] != 'result') { if (result.attributes['type'] != 'result') return Result(getPubSubError(result));
return Result(getPubSubError(result));
}
final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns); final pubsub = result.firstTag('pubsub', xmlns: pubsubXmlns);
if (pubsub == null) return Result(getPubSubError(result)); if (pubsub == null) return Result(getPubSubError(result));
@ -478,11 +415,7 @@ class PubSubManager extends XmppManagerBase {
return Result(item); return Result(item);
} }
Future<Result<PubSubError, bool>> configure( Future<Result<PubSubError, bool>> configure(String jid, String node, PubSubPublishOptions options) async {
String jid,
String node,
PubSubPublishOptions options,
) async {
final attrs = getAttributes(); final attrs = getAttributes();
// Request the form // Request the form
@ -506,9 +439,7 @@ class PubSubManager extends XmppManagerBase {
], ],
), ),
); );
if (form.attributes['type'] != 'result') { if (form.attributes['type'] != 'result') return Result(getPubSubError(form));
return Result(getPubSubError(form));
}
final submit = await attrs.sendStanza( final submit = await attrs.sendStanza(
Stanza.iq( Stanza.iq(
@ -533,9 +464,7 @@ class PubSubManager extends XmppManagerBase {
], ],
), ),
); );
if (submit.attributes['type'] != 'result') { if (submit.attributes['type'] != 'result') return Result(getPubSubError(form));
return Result(getPubSubError(form));
}
return const Result(true); return const Result(true);
} }
@ -570,11 +499,7 @@ class PubSubManager extends XmppManagerBase {
return const Result(true); return const Result(true);
} }
Future<Result<PubSubError, bool>> retract( Future<Result<PubSubError, bool>> retract(JID host, String node, String itemId) async {
JID host,
String node,
String itemId,
) async {
final request = await getAttributes().sendStanza( final request = await getAttributes().sendStanza(
Stanza.iq( Stanza.iq(
type: 'set', type: 'set',

View File

@ -8,7 +8,7 @@ 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;
} }
@ -22,7 +22,7 @@ XMLNode constructOOBNode(OOBData data) {
if (data.desc != null) { if (data.desc != null) {
children.add(XMLNode(tag: 'desc', text: data.desc)); children.add(XMLNode(tag: 'desc', text: data.desc));
} }
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'x', tag: 'x',
xmlns: oobDataXmlns, xmlns: oobDataXmlns,
@ -34,27 +34,24 @@ class OOBManager extends XmppManagerBase {
OOBManager() : super(oobManager); OOBManager() : super(oobManager);
@override @override
List<String> getDiscoFeatures() => [oobDataXmlns]; List<String> getDiscoFeatures() => [ oobDataXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'x', tagName: 'x',
tagXmlns: oobDataXmlns, tagXmlns: oobDataXmlns,
callback: _onMessage, callback: _onMessage,
// Before the message manager // Before the message manager
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final x = message.firstTag('x', xmlns: oobDataXmlns)!; final x = message.firstTag('x', xmlns: oobDataXmlns)!;
final url = x.firstTag('url'); final url = x.firstTag('url');
final desc = x.firstTag('desc'); final desc = x.firstTag('desc');

View File

@ -15,7 +15,7 @@ abstract class AvatarError {}
class UnknownAvatarError extends AvatarError {} class UnknownAvatarError extends AvatarError {}
class UserAvatar { class UserAvatar {
const UserAvatar({required this.base64, required this.hash}); const UserAvatar({ required this.base64, required this.hash });
final String base64; final String base64;
final String hash; final String hash;
} }
@ -47,9 +47,8 @@ class UserAvatarMetadata {
class UserAvatarManager extends XmppManagerBase { class UserAvatarManager extends XmppManagerBase {
UserAvatarManager() : super(userAvatarManager); UserAvatarManager() : super(userAvatarManager);
PubSubManager _getPubSubManager() => PubSubManager _getPubSubManager() => getAttributes().getManagerById(pubsubManager)! as PubSubManager;
getAttributes().getManagerById(pubsubManager)! as PubSubManager;
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is PubSubNotificationEvent) { if (event is PubSubNotificationEvent) {
@ -57,9 +56,7 @@ class UserAvatarManager extends XmppManagerBase {
if (event.item.payload.tag != 'data' || if (event.item.payload.tag != 'data' ||
event.item.payload.attributes['xmlns'] != userAvatarDataXmlns) { event.item.payload.attributes['xmlns'] != userAvatarDataXmlns) {
logger.warning( logger.warning('Received avatar update from ${event.from} but the payload is invalid. Ignoring...');
'Received avatar update from ${event.from} but the payload is invalid. Ignoring...',
);
return; return;
} }
@ -99,11 +96,7 @@ class UserAvatarManager extends XmppManagerBase {
/// Publish the avatar data, [base64], on the pubsub node using [hash] as /// Publish the avatar data, [base64], on the pubsub node using [hash] as
/// the item id. [hash] must be the SHA-1 hash of the image data, while /// the item id. [hash] must be the SHA-1 hash of the image data, while
/// [base64] must be the base64-encoded version of the image data. /// [base64] must be the base64-encoded version of the image data.
Future<Result<AvatarError, bool>> publishUserAvatar( Future<Result<AvatarError, bool>> publishUserAvatar(String base64, String hash, bool public) async {
String base64,
String hash,
bool public,
) async {
final pubsub = _getPubSubManager(); final pubsub = _getPubSubManager();
final result = await pubsub.publish( final result = await pubsub.publish(
getAttributes().getFullJID().toBare().toString(), getAttributes().getFullJID().toBare().toString(),
@ -120,17 +113,14 @@ class UserAvatarManager extends XmppManagerBase {
); );
if (result.isType<PubSubError>()) return Result(UnknownAvatarError()); if (result.isType<PubSubError>()) return Result(UnknownAvatarError());
return const Result(true); return const Result(true);
} }
/// Publish avatar metadata [metadata] to the User Avatar's metadata node. If [public] /// Publish avatar metadata [metadata] to the User Avatar's metadata node. If [public]
/// is true, then the node will be set to an 'open' access model. If [public] is false, /// is true, then the node will be set to an 'open' access model. If [public] is false,
/// then the node will be set to an 'roster' access model. /// then the node will be set to an 'roster' access model.
Future<Result<AvatarError, bool>> publishUserAvatarMetadata( Future<Result<AvatarError, bool>> publishUserAvatarMetadata(UserAvatarMetadata metadata, bool public) async {
UserAvatarMetadata metadata,
bool public,
) async {
final pubsub = _getPubSubManager(); final pubsub = _getPubSubManager();
final result = await pubsub.publish( final result = await pubsub.publish(
getAttributes().getFullJID().toBare().toString(), getAttributes().getFullJID().toBare().toString(),
@ -160,7 +150,7 @@ class UserAvatarManager extends XmppManagerBase {
if (result.isType<PubSubError>()) return Result(UnknownAvatarError()); if (result.isType<PubSubError>()) return Result(UnknownAvatarError());
return const Result(true); return const Result(true);
} }
/// Subscribe the data and metadata node of [jid]. /// Subscribe the data and metadata node of [jid].
Future<Result<AvatarError, bool>> subscribe(String jid) async { Future<Result<AvatarError, bool>> subscribe(String jid) async {
await _getPubSubManager().subscribe(jid, userAvatarDataXmlns); await _getPubSubManager().subscribe(jid, userAvatarDataXmlns);
@ -182,11 +172,7 @@ class UserAvatarManager extends XmppManagerBase {
/// the node. /// the node.
Future<Result<AvatarError, String>> getAvatarId(String jid) async { Future<Result<AvatarError, String>> getAvatarId(String jid) async {
final disco = getAttributes().getManagerById(discoManager)! as DiscoManager; final disco = getAttributes().getManagerById(discoManager)! as DiscoManager;
final response = await disco.discoItemsQuery( final response = await disco.discoItemsQuery(jid, node: userAvatarDataXmlns, shouldEncrypt: false);
jid,
node: userAvatarDataXmlns,
shouldEncrypt: false,
);
if (response.isType<DiscoError>()) return Result(UnknownAvatarError()); if (response.isType<DiscoError>()) return Result(UnknownAvatarError());
final items = response.get<List<DiscoItem>>(); final items = response.get<List<DiscoItem>>();

View File

@ -6,96 +6,86 @@ 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';
enum ChatState { active, composing, paused, inactive, gone } enum ChatState {
active,
ChatState chatStateFromString(String raw) { composing,
switch (raw) { paused,
case 'active': inactive,
{ gone
return ChatState.active;
}
case 'composing':
{
return ChatState.composing;
}
case 'paused':
{
return ChatState.paused;
}
case 'inactive':
{
return ChatState.inactive;
}
case 'gone':
{
return ChatState.gone;
}
default:
{
return ChatState.gone;
}
}
} }
ChatState chatStateFromString(String raw) {
switch(raw) {
case 'active': {
return ChatState.active;
}
case 'composing': {
return ChatState.composing;
}
case 'paused': {
return ChatState.paused;
}
case 'inactive': {
return ChatState.inactive;
}
case 'gone': {
return ChatState.gone;
}
default: {
return ChatState.gone;
}
}
}
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); ChatStateManager() : super(chatStateManager);
@override @override
List<String> getDiscoFeatures() => [chatStateXmlns]; List<String> getDiscoFeatures() => [ chatStateXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagXmlns: chatStateXmlns, tagXmlns: chatStateXmlns,
callback: _onChatStateReceived, callback: _onChatStateReceived,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onChatStateReceived( Future<StanzaHandlerData> _onChatStateReceived(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final element = state.stanza.firstTagByXmlns(chatStateXmlns)!; final element = state.stanza.firstTagByXmlns(chatStateXmlns)!;
ChatState? chatState; ChatState? chatState;
switch (element.tag) { switch (element.tag) {
case 'active': case 'active': {
{ chatState = ChatState.active;
chatState = ChatState.active; }
} break;
break; case 'composing': {
case 'composing': chatState = ChatState.composing;
{ }
chatState = ChatState.composing; break;
} case 'paused': {
break; chatState = ChatState.paused;
case 'paused': }
{ break;
chatState = ChatState.paused; case 'inactive': {
} chatState = ChatState.inactive;
break; }
case 'inactive': break;
{ case 'gone': {
chatState = ChatState.inactive; chatState = ChatState.gone;
} }
break; break;
case 'gone': default: {
{ logger.warning("Received invalid chat state '${element.tag}'");
chatState = ChatState.gone; }
}
break;
default:
{
logger.warning("Received invalid chat state '${element.tag}'");
}
} }
return state.copyWith(chatState: chatState); return state.copyWith(chatState: chatState);
@ -103,18 +93,14 @@ class ChatStateManager extends XmppManagerBase {
/// Send a chat state notification to [to]. You can specify the type attribute /// Send a chat state notification to [to]. You can specify the type attribute
/// of the message with [messageType]. /// of the message with [messageType].
void sendChatState( void sendChatState(ChatState state, String to, { String messageType = 'chat' }) {
ChatState state,
String to, {
String messageType = 'chat',
}) {
final tagName = state.toString().split('.').last; final tagName = state.toString().split('.').last;
getAttributes().sendStanza( getAttributes().sendStanza(
Stanza.message( Stanza.message(
to: to, to: to,
type: messageType, type: messageType,
children: [XMLNode.xmlns(tag: tagName, xmlns: chatStateXmlns)], children: [ XMLNode.xmlns(tag: tagName, xmlns: chatStateXmlns) ],
), ),
); );
} }

View File

@ -21,17 +21,11 @@ class CapabilityHashInfo {
/// Calculates the Entitiy Capability hash according to XEP-0115 based on the /// Calculates the Entitiy Capability hash according to XEP-0115 based on the
/// disco information. /// disco information.
Future<String> calculateCapabilityHash( Future<String> calculateCapabilityHash(DiscoInfo info, HashAlgorithm algorithm) async {
DiscoInfo info,
HashAlgorithm algorithm,
) async {
final buffer = StringBuffer(); final buffer = StringBuffer();
final identitiesSorted = info.identities final identitiesSorted = info.identities
.map( .map((Identity i) => '${i.category}/${i.type}/${i.lang ?? ""}/${i.name ?? ""}')
(Identity i) => .toList();
'${i.category}/${i.type}/${i.lang ?? ""}/${i.name ?? ""}',
)
.toList();
// ignore: cascade_invocations // ignore: cascade_invocations
identitiesSorted.sort(ioctetSortComparator); identitiesSorted.sort(ioctetSortComparator);
buffer.write('${identitiesSorted.join("<")}<'); buffer.write('${identitiesSorted.join("<")}<');
@ -42,23 +36,20 @@ Future<String> calculateCapabilityHash(
if (info.extendedInfo.isNotEmpty) { if (info.extendedInfo.isNotEmpty) {
final sortedExt = info.extendedInfo final sortedExt = info.extendedInfo
..sort( ..sort((a, b) => ioctetSortComparator(
(a, b) => ioctetSortComparator( a.getFieldByVar('FORM_TYPE')!.values.first,
a.getFieldByVar('FORM_TYPE')!.values.first, b.getFieldByVar('FORM_TYPE')!.values.first,
b.getFieldByVar('FORM_TYPE')!.values.first, ),
), );
);
for (final ext in sortedExt) { for (final ext in sortedExt) {
buffer.write('${ext.getFieldByVar("FORM_TYPE")!.values.first}<'); buffer.write('${ext.getFieldByVar("FORM_TYPE")!.values.first}<');
final sortedFields = ext.fields final sortedFields = ext.fields..sort((a, b) => ioctetSortComparator(
..sort( a.varAttr!,
(a, b) => ioctetSortComparator( b.varAttr!,
a.varAttr!, ),
b.varAttr!, );
),
);
for (final field in sortedFields) { for (final field in sortedFields) {
if (field.varAttr == 'FORM_TYPE') continue; if (field.varAttr == 'FORM_TYPE') continue;
@ -71,9 +62,8 @@ Future<String> calculateCapabilityHash(
} }
} }
} }
return base64 return base64.encode((await algorithm.hash(utf8.encode(buffer.toString()))).bytes);
.encode((await algorithm.hash(utf8.encode(buffer.toString()))).bytes);
} }
/// A manager implementing the advertising of XEP-0115. It responds to the /// A manager implementing the advertising of XEP-0115. It responds to the
@ -81,8 +71,7 @@ Future<String> calculateCapabilityHash(
/// the DiscoManager. /// the DiscoManager.
/// NOTE: This manager requires that the DiscoManager is also registered. /// NOTE: This manager requires that the DiscoManager is also registered.
class EntityCapabilitiesManager extends XmppManagerBase { class EntityCapabilitiesManager extends XmppManagerBase {
EntityCapabilitiesManager(this._capabilityHashBase) EntityCapabilitiesManager(this._capabilityHashBase) : super(entityCapabilitiesManager);
: super(entityCapabilitiesManager);
/// The string that is both the node under which we advertise the disco info /// 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. /// and the base for the actual node on which we respond to disco#info requests.
@ -95,15 +84,15 @@ class EntityCapabilitiesManager extends XmppManagerBase {
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
@override @override
List<String> getDiscoFeatures() => [capsXmlns]; List<String> getDiscoFeatures() => [ capsXmlns ];
/// Computes, if required, the capability hash of the data provided by /// Computes, if required, the capability hash of the data provided by
/// the DiscoManager. /// the DiscoManager.
Future<String> getCapabilityHash() async { Future<String> getCapabilityHash() async {
_capabilityHash ??= await calculateCapabilityHash( _capabilityHash ??= await calculateCapabilityHash(
getAttributes() getAttributes()
.getManagerById<DiscoManager>(discoManager)! .getManagerById<DiscoManager>(discoManager)!
.getDiscoInfo(null), .getDiscoInfo(null),
getHashByName('sha-1')!, getHashByName('sha-1')!,
); );
@ -114,11 +103,11 @@ class EntityCapabilitiesManager extends XmppManagerBase {
final hash = await getCapabilityHash(); final hash = await getCapabilityHash();
return '$_capabilityHashBase#$hash'; return '$_capabilityHashBase#$hash';
} }
Future<DiscoInfo> _onInfoQuery() async { Future<DiscoInfo> _onInfoQuery() async {
return getAttributes() return getAttributes()
.getManagerById<DiscoManager>(discoManager)! .getManagerById<DiscoManager>(discoManager)!
.getDiscoInfo(await _getNode()); .getDiscoInfo(await _getNode());
} }
Future<List<XMLNode>> _prePresenceSent() async { Future<List<XMLNode>> _prePresenceSent() async {
@ -134,22 +123,20 @@ class EntityCapabilitiesManager extends XmppManagerBase {
), ),
]; ];
} }
@override @override
Future<void> postRegisterCallback() async { Future<void> postRegisterCallback() async {
await super.postRegisterCallback(); await super.postRegisterCallback();
getAttributes().getManagerById<DiscoManager>(discoManager)!.registerInfoCallback(
await _getNode(),
_onInfoQuery,
);
getAttributes() getAttributes()
.getManagerById<DiscoManager>(discoManager)! .getManagerById<PresenceManager>(presenceManager)!
.registerInfoCallback( .registerPreSendCallback(
await _getNode(), _prePresenceSent,
_onInfoQuery, );
);
getAttributes()
.getManagerById<PresenceManager>(presenceManager)!
.registerPreSendCallback(
_prePresenceSent,
);
} }
} }

View File

@ -19,7 +19,7 @@ XMLNode makeMessageDeliveryResponse(String id) {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'received', tag: 'received',
xmlns: deliveryXmlns, xmlns: deliveryXmlns,
attributes: {'id': id}, attributes: { 'id': id },
); );
} }
@ -27,49 +27,40 @@ class MessageDeliveryReceiptManager extends XmppManagerBase {
MessageDeliveryReceiptManager() : super(messageDeliveryReceiptManager); MessageDeliveryReceiptManager() : super(messageDeliveryReceiptManager);
@override @override
List<String> getDiscoFeatures() => [deliveryXmlns]; List<String> getDiscoFeatures() => [ deliveryXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'received', tagName: 'received',
tagXmlns: deliveryXmlns, tagXmlns: deliveryXmlns,
callback: _onDeliveryReceiptReceived, callback: _onDeliveryReceiptReceived,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'request', tagName: 'request',
tagXmlns: deliveryXmlns, tagXmlns: deliveryXmlns,
callback: _onDeliveryRequestReceived, callback: _onDeliveryRequestReceived,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onDeliveryRequestReceived( Future<StanzaHandlerData> _onDeliveryRequestReceived(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
return state.copyWith(deliveryReceiptRequested: true); return state.copyWith(deliveryReceiptRequested: true);
} }
Future<StanzaHandlerData> _onDeliveryReceiptReceived( Future<StanzaHandlerData> _onDeliveryReceiptReceived(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final received = message.firstTag('received', xmlns: deliveryXmlns)!; final received = message.firstTag('received', xmlns: deliveryXmlns)!;
for (final item in message.children) { for (final item in message.children) {
if (!['origin-id', 'stanza-id', 'delay', 'store', 'received'] if (!['origin-id', 'stanza-id', 'delay', 'store', 'received'].contains(item.tag)) {
.contains(item.tag)) { logger.info("Won't handle stanza as delivery receipt because we found an '${item.tag}' element");
logger.info(
"Won't handle stanza as delivery receipt because we found an '${item.tag}' element",
);
return state.copyWith(done: true); return state.copyWith(done: true);
} }

View File

@ -16,19 +16,19 @@ class BlockingManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'iq', stanzaTag: 'iq',
tagName: 'unblock', tagName: 'unblock',
tagXmlns: blockingXmlns, tagXmlns: blockingXmlns,
callback: _unblockPush, callback: _unblockPush,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'iq', stanzaTag: 'iq',
tagName: 'block', tagName: 'block',
tagXmlns: blockingXmlns, tagXmlns: blockingXmlns,
callback: _blockPush, callback: _blockPush,
) )
]; ];
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {
@ -54,29 +54,20 @@ class BlockingManager extends XmppManagerBase {
} }
} }
} }
Future<StanzaHandlerData> _blockPush( Future<StanzaHandlerData> _blockPush(Stanza iq, StanzaHandlerData state) async {
Stanza iq,
StanzaHandlerData state,
) async {
final block = iq.firstTag('block', xmlns: blockingXmlns)!; final block = iq.firstTag('block', xmlns: blockingXmlns)!;
getAttributes().sendEvent( getAttributes().sendEvent(
BlocklistBlockPushEvent( BlocklistBlockPushEvent(
items: block items: block.findTags('item').map((i) => i.attributes['jid']! as String).toList(),
.findTags('item')
.map((i) => i.attributes['jid']! as String)
.toList(),
), ),
); );
return state.copyWith(done: true); return state.copyWith(done: true);
} }
Future<StanzaHandlerData> _unblockPush( Future<StanzaHandlerData> _unblockPush(Stanza iq, StanzaHandlerData state) async {
Stanza iq,
StanzaHandlerData state,
) async {
final unblock = iq.firstTag('unblock', xmlns: blockingXmlns)!; final unblock = iq.firstTag('unblock', xmlns: blockingXmlns)!;
final items = unblock.findTags('item'); final items = unblock.findTags('item');
@ -94,7 +85,7 @@ class BlockingManager extends XmppManagerBase {
return state.copyWith(done: true); return state.copyWith(done: true);
} }
Future<bool> block(List<String> items) async { Future<bool> block(List<String> items) async {
final result = await getAttributes().sendStanza( final result = await getAttributes().sendStanza(
Stanza.iq( Stanza.iq(
@ -103,12 +94,14 @@ class BlockingManager extends XmppManagerBase {
XMLNode.xmlns( XMLNode.xmlns(
tag: 'block', tag: 'block',
xmlns: blockingXmlns, xmlns: blockingXmlns,
children: items.map((item) { children: items
return XMLNode( .map((item) {
tag: 'item', return XMLNode(
attributes: <String, String>{'jid': item}, tag: 'item',
); attributes: <String, String>{ 'jid': item },
}).toList(), );
})
.toList(),
) )
], ],
), ),
@ -132,7 +125,7 @@ class BlockingManager extends XmppManagerBase {
return result.attributes['type'] == 'result'; return result.attributes['type'] == 'result';
} }
Future<bool> unblock(List<String> items) async { Future<bool> unblock(List<String> items) async {
assert(items.isNotEmpty, 'The list of items to unblock must be non-empty'); assert(items.isNotEmpty, 'The list of items to unblock must be non-empty');
@ -143,14 +136,10 @@ class BlockingManager extends XmppManagerBase {
XMLNode.xmlns( XMLNode.xmlns(
tag: 'unblock', tag: 'unblock',
xmlns: blockingXmlns, xmlns: blockingXmlns,
children: items children: items.map((item) => XMLNode(
.map( tag: 'item',
(item) => XMLNode( attributes: <String, String>{ 'jid': item },
tag: 'item', ),).toList(),
attributes: <String, String>{'jid': item},
),
)
.toList(),
) )
], ],
), ),
@ -173,9 +162,6 @@ class BlockingManager extends XmppManagerBase {
); );
final blocklist = result.firstTag('blocklist', xmlns: blockingXmlns)!; final blocklist = result.firstTag('blocklist', xmlns: blockingXmlns)!;
return blocklist return blocklist.findTags('item').map((item) => item.attributes['jid']! as String).toList();
.findTags('item')
.map((item) => item.attributes['jid']! as String)
.toList();
} }
} }

View File

@ -25,16 +25,16 @@ enum _StreamManagementNegotiatorState {
/// is wanted. /// is wanted.
class StreamManagementNegotiator extends XmppFeatureNegotiatorBase { class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
StreamManagementNegotiator() StreamManagementNegotiator()
: _state = _StreamManagementNegotiatorState.ready, : _state = _StreamManagementNegotiatorState.ready,
_supported = false, _supported = false,
_resumeFailed = false, _resumeFailed = false,
_isResumed = false, _isResumed = false,
_log = Logger('StreamManagementNegotiator'), _log = Logger('StreamManagementNegotiator'),
super(10, false, smXmlns, streamManagementNegotiator); super(10, false, smXmlns, streamManagementNegotiator);
_StreamManagementNegotiatorState _state; _StreamManagementNegotiatorState _state;
bool _resumeFailed; bool _resumeFailed;
bool _isResumed; bool _isResumed;
final Logger _log; final Logger _log;
/// True if Stream Management is supported on this stream. /// True if Stream Management is supported on this stream.
@ -43,7 +43,7 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
/// True if the current stream is resumed. False if not. /// True if the current stream is resumed. False if not.
bool get isResumed => _isResumed; bool get isResumed => _isResumed;
@override @override
bool matchesFeature(List<XMLNode> features) { bool matchesFeature(List<XMLNode> features) {
final sm = attributes.getManagerById<StreamManagementManager>(smManager)!; final sm = attributes.getManagerById<StreamManagementManager>(smManager)!;
@ -54,32 +54,25 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
} else { } else {
// We cannot do a stream resumption // We cannot do a stream resumption
final br = attributes.getNegotiatorById(resourceBindingNegotiator); final br = attributes.getNegotiatorById(resourceBindingNegotiator);
return super.matchesFeature(features) && return super.matchesFeature(features) && br?.state == NegotiatorState.done && attributes.isAuthenticated();
br?.state == NegotiatorState.done &&
attributes.isAuthenticated();
} }
} }
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
// negotiate is only called when we matched the stream feature, so we know // negotiate is only called when we matched the stream feature, so we know
// that the server advertises it. // that the server advertises it.
_supported = true; _supported = true;
switch (_state) { switch (_state) {
case _StreamManagementNegotiatorState.ready: case _StreamManagementNegotiatorState.ready:
final sm = final sm = attributes.getManagerById<StreamManagementManager>(smManager)!;
attributes.getManagerById<StreamManagementManager>(smManager)!;
final srid = sm.state.streamResumptionId; final srid = sm.state.streamResumptionId;
final h = sm.state.s2c; final h = sm.state.s2c;
// Attempt stream resumption first // Attempt stream resumption first
if (srid != null) { if (srid != null) {
_log.finest( _log.finest('Found stream resumption Id. Attempting to perform stream resumption');
'Found stream resumption Id. Attempting to perform stream resumption',
);
_state = _StreamManagementNegotiatorState.resumeRequested; _state = _StreamManagementNegotiatorState.resumeRequested;
attributes.sendNonza(StreamManagementResumeNonza(srid, h)); attributes.sendNonza(StreamManagementResumeNonza(srid, h));
} else { } else {
@ -89,53 +82,46 @@ class StreamManagementNegotiator extends XmppFeatureNegotiatorBase {
} }
return const Result(NegotiatorState.ready); return const Result(NegotiatorState.ready);
case _StreamManagementNegotiatorState.resumeRequested: case _StreamManagementNegotiatorState.resumeRequested:
if (nonza.tag == 'resumed') { if (nonza.tag == 'resumed') {
_log.finest('Stream Management resumption successful'); _log.finest('Stream Management resumption successful');
assert( assert(attributes.getFullJID().resource != '', 'Resume only works when we already have a resource bound and know about it');
attributes.getFullJID().resource != '',
'Resume only works when we already have a resource bound and know about it',
);
final csi = attributes.getManagerById(csiManager) as CSIManager?; final csi = attributes.getManagerById(csiManager) as CSIManager?;
if (csi != null) { if (csi != null) {
csi.restoreCSIState(); csi.restoreCSIState();
}
final h = int.parse(nonza.attributes['h']! as String);
await attributes.sendEvent(StreamResumedEvent(h: h));
_resumeFailed = false;
_isResumed = true;
return const Result(NegotiatorState.skipRest);
} else {
// We assume it is <failed />
_log.info('Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...');
await attributes.sendEvent(StreamResumeFailedEvent());
final sm = attributes.getManagerById<StreamManagementManager>(smManager)!;
// We have to do this because we otherwise get a stanza stuck in the queue,
// thus spamming the server on every <a /> nonza we receive.
// ignore: cascade_invocations
await sm.setState(StreamManagementState(0, 0));
await sm.commitState();
_resumeFailed = true;
_isResumed = false;
_state = _StreamManagementNegotiatorState.ready;
return const Result(NegotiatorState.retryLater);
} }
final h = int.parse(nonza.attributes['h']! as String);
await attributes.sendEvent(StreamResumedEvent(h: h));
_resumeFailed = false;
_isResumed = true;
return const Result(NegotiatorState.skipRest);
} else {
// We assume it is <failed />
_log.info(
'Stream resumption failed. Expected <resumed />, got ${nonza.tag}, Proceeding with new stream...',
);
await attributes.sendEvent(StreamResumeFailedEvent());
final sm =
attributes.getManagerById<StreamManagementManager>(smManager)!;
// We have to do this because we otherwise get a stanza stuck in the queue,
// thus spamming the server on every <a /> nonza we receive.
// ignore: cascade_invocations
await sm.setState(StreamManagementState(0, 0));
await sm.commitState();
_resumeFailed = true;
_isResumed = false;
_state = _StreamManagementNegotiatorState.ready;
return const Result(NegotiatorState.retryLater);
}
case _StreamManagementNegotiatorState.enableRequested: case _StreamManagementNegotiatorState.enableRequested:
if (nonza.tag == 'enabled') { if (nonza.tag == 'enabled') {
_log.finest('Stream Management enabled'); _log.finest('Stream Management enabled');
final id = nonza.attributes['id'] as String?; final id = nonza.attributes['id'] as String?;
if (id != null && if (id != null && ['true', '1'].contains(nonza.attributes['resume'])) {
['true', '1'].contains(nonza.attributes['resume'])) {
_log.info('Stream Resumption available'); _log.info('Stream Resumption available');
} }

View File

@ -2,39 +2,41 @@ import 'package:moxxmpp/src/namespaces.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
class StreamManagementEnableNonza extends XMLNode { class StreamManagementEnableNonza extends XMLNode {
StreamManagementEnableNonza() StreamManagementEnableNonza() : super(
: super( tag: 'enable',
tag: 'enable', attributes: <String, String>{
attributes: <String, String>{'xmlns': smXmlns, 'resume': 'true'}, 'xmlns': smXmlns,
); 'resume': 'true'
},
);
} }
class StreamManagementResumeNonza extends XMLNode { class StreamManagementResumeNonza extends XMLNode {
StreamManagementResumeNonza(String id, int h) StreamManagementResumeNonza(String id, int h) : super(
: super( tag: 'resume',
tag: 'resume', attributes: <String, String>{
attributes: <String, String>{ 'xmlns': smXmlns,
'xmlns': smXmlns, 'previd': id,
'previd': id, 'h': h.toString()
'h': h.toString() },
}, );
);
} }
class StreamManagementAckNonza extends XMLNode { class StreamManagementAckNonza extends XMLNode {
StreamManagementAckNonza(int h) StreamManagementAckNonza(int h) : super(
: super( tag: 'a',
tag: 'a', attributes: <String, String>{
attributes: <String, String>{'xmlns': smXmlns, 'h': h.toString()}, 'xmlns': smXmlns,
); 'h': h.toString()
},
);
} }
class StreamManagementRequestNonza extends XMLNode { class StreamManagementRequestNonza extends XMLNode {
StreamManagementRequestNonza() StreamManagementRequestNonza() : super(
: super( tag: 'r',
tag: 'r', attributes: <String, String>{
attributes: <String, String>{ 'xmlns': smXmlns,
'xmlns': smXmlns, },
}, );
);
} }

View File

@ -7,12 +7,13 @@ part 'state.g.dart';
class StreamManagementState with _$StreamManagementState { class StreamManagementState with _$StreamManagementState {
factory StreamManagementState( factory StreamManagementState(
int c2s, int c2s,
int s2c, { int s2c,
String? streamResumptionLocation, {
String? streamResumptionId, String? streamResumptionLocation,
}) = _StreamManagementState; String? streamResumptionId,
}
) = _StreamManagementState;
// JSON // JSON
factory StreamManagementState.fromJson(Map<String, dynamic> json) => factory StreamManagementState.fromJson(Map<String, dynamic> json) => _$StreamManagementStateFromJson(json);
_$StreamManagementStateFromJson(json);
} }

View File

@ -80,16 +80,12 @@ class StreamManagementManager extends XmppManagerBase {
bool shouldTriggerAckedEvent(Stanza stanza) { bool shouldTriggerAckedEvent(Stanza stanza) {
return false; return false;
} }
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {
return getAttributes() return getAttributes().getNegotiatorById<StreamManagementNegotiator>(streamManagementNegotiator)!.isSupported;
.getNegotiatorById<StreamManagementNegotiator>(
streamManagementNegotiator,
)!
.isSupported;
} }
/// Returns the amount of stanzas waiting to get acked /// Returns the amount of stanzas waiting to get acked
int getUnackedStanzaCount() => _unackedStanzas.length; int getUnackedStanzaCount() => _unackedStanzas.length;
@ -121,40 +117,40 @@ class StreamManagementManager extends XmppManagerBase {
_pendingAcks = 0; _pendingAcks = 0;
}); });
} }
StreamManagementState get state => _state; StreamManagementState get state => _state;
bool get streamResumed => _streamResumed; bool get streamResumed => _streamResumed;
@override @override
List<NonzaHandler> getNonzaHandlers() => [ List<NonzaHandler> getNonzaHandlers() => [
NonzaHandler( NonzaHandler(
nonzaTag: 'r', nonzaTag: 'r',
nonzaXmlns: smXmlns, nonzaXmlns: smXmlns,
callback: _handleAckRequest, callback: _handleAckRequest,
), ),
NonzaHandler( NonzaHandler(
nonzaTag: 'a', nonzaTag: 'a',
nonzaXmlns: smXmlns, nonzaXmlns: smXmlns,
callback: _handleAckResponse, callback: _handleAckResponse,
) )
]; ];
@override @override
List<StanzaHandler> getIncomingPreStanzaHandlers() => [ List<StanzaHandler> getIncomingPreStanzaHandlers() => [
StanzaHandler( StanzaHandler(
callback: _onServerStanzaReceived, callback: _onServerStanzaReceived,
priority: 9999, priority: 9999,
) )
]; ];
@override @override
List<StanzaHandler> getOutgoingPostStanzaHandlers() => [ List<StanzaHandler> getOutgoingPostStanzaHandlers() => [
StanzaHandler( StanzaHandler(
callback: _onClientStanzaSent, callback: _onClientStanzaSent,
) )
]; ];
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is StreamResumedEvent) { if (event is StreamResumedEvent) {
@ -185,17 +181,17 @@ class StreamManagementManager extends XmppManagerBase {
_streamResumed = false; _streamResumed = false;
} else if (event is ConnectionStateChangedEvent) { } else if (event is ConnectionStateChangedEvent) {
switch (event.state) { switch (event.state) {
case XmppConnectionState.connected: case XmppConnectionState.connected:
// Push out all pending stanzas // Push out all pending stanzas
await onStreamResumed(0); await onStreamResumed(0);
break; break;
case XmppConnectionState.error: case XmppConnectionState.error:
case XmppConnectionState.notConnected: case XmppConnectionState.notConnected:
_stopAckTimer(); _stopAckTimer();
break; break;
case XmppConnectionState.connecting: case XmppConnectionState.connecting:
// NOOP // NOOP
break; break;
} }
} }
} }
@ -227,8 +223,7 @@ class StreamManagementManager extends XmppManagerBase {
_ackLock.synchronized(() async { _ackLock.synchronized(() async {
final now = DateTime.now().millisecondsSinceEpoch; final now = DateTime.now().millisecondsSinceEpoch;
if (now - _lastAckTimestamp >= ackTimeout.inMilliseconds && if (now - _lastAckTimestamp >= ackTimeout.inMilliseconds && _pendingAcks > 0) {
_pendingAcks > 0) {
_stopAckTimer(); _stopAckTimer();
await getAttributes().getConnection().reconnectionPolicy.onFailure(); await getAttributes().getConnection().reconnectionPolicy.onFailure();
} }
@ -247,13 +242,13 @@ class StreamManagementManager extends XmppManagerBase {
_startAckTimer(); _startAckTimer();
logger.fine('_pendingAcks is now at $_pendingAcks'); logger.fine('_pendingAcks is now at $_pendingAcks');
getAttributes().sendNonza(StreamManagementRequestNonza()); getAttributes().sendNonza(StreamManagementRequestNonza());
logger.fine('_sendAckRequest: Releasing lock...'); logger.fine('_sendAckRequest: Releasing lock...');
}); });
} }
/// Resets the enablement of stream management, but __NOT__ the internal state. /// Resets the enablement of stream management, but __NOT__ the internal state.
/// This is to prevent ack requests being sent before we resume or re-enable /// This is to prevent ack requests being sent before we resume or re-enable
/// stream management. /// stream management.
@ -261,13 +256,13 @@ class StreamManagementManager extends XmppManagerBase {
_streamManagementEnabled = false; _streamManagementEnabled = false;
logger.finest('Stream Management disabled'); logger.finest('Stream Management disabled');
} }
/// Enables support for XEP-0198 stream management /// Enables support for XEP-0198 stream management
void _enableStreamManagement() { void _enableStreamManagement() {
_streamManagementEnabled = true; _streamManagementEnabled = true;
logger.finest('Stream Management enabled'); logger.finest('Stream Management enabled');
} }
/// Returns whether XEP-0198 stream management is enabled /// Returns whether XEP-0198 stream management is enabled
bool isStreamManagementEnabled() => _streamManagementEnabled; bool isStreamManagementEnabled() => _streamManagementEnabled;
@ -300,44 +295,42 @@ class StreamManagementManager extends XmppManagerBase {
logger.fine('_pendingAcks is now at $_pendingAcks'); logger.fine('_pendingAcks is now at $_pendingAcks');
}); });
}); });
// Return early if we acked nothing. // Return early if we acked nothing.
// Taken from slixmpp's stream management code // Taken from slixmpp's stream management code
logger.fine('_handleAckResponse: Waiting to aquire lock...'); logger.fine('_handleAckResponse: Waiting to aquire lock...');
await _stateLock.synchronized(() async { await _stateLock.synchronized(() async {
logger.fine('_handleAckResponse: Done...'); logger.fine('_handleAckResponse: Done...');
if (h == _state.c2s && _unackedStanzas.isEmpty) { if (h == _state.c2s && _unackedStanzas.isEmpty) {
logger.fine('_handleAckResponse: Releasing lock...'); logger.fine('_handleAckResponse: Releasing lock...');
return; return;
}
final attrs = getAttributes();
final sequences = _unackedStanzas.keys.toList()..sort();
for (final height in sequences) {
// Do nothing if the ack does not concern this stanza
if (height > h) continue;
final stanza = _unackedStanzas[height]!;
_unackedStanzas.remove(height);
// Create a StanzaAckedEvent if the stanza is correct
if (shouldTriggerAckedEvent(stanza)) {
attrs.sendEvent(StanzaAckedEvent(stanza));
} }
}
if (h > _state.c2s) { final attrs = getAttributes();
logger.info( final sequences = _unackedStanzas.keys.toList()..sort();
'C2S height jumped from ${_state.c2s} (local) to $h (remote).', for (final height in sequences) {
); // Do nothing if the ack does not concern this stanza
// ignore: cascade_invocations if (height > h) continue;
logger.info('Proceeding with $h as local C2S counter.');
_state = _state.copyWith(c2s: h); final stanza = _unackedStanzas[height]!;
await commitState(); _unackedStanzas.remove(height);
}
logger.fine('_handleAckResponse: Releasing lock...'); // Create a StanzaAckedEvent if the stanza is correct
if (shouldTriggerAckedEvent(stanza)) {
attrs.sendEvent(StanzaAckedEvent(stanza));
}
}
if (h > _state.c2s) {
logger.info('C2S height jumped from ${_state.c2s} (local) to $h (remote).');
// ignore: cascade_invocations
logger.info('Proceeding with $h as local C2S counter.');
_state = _state.copyWith(c2s: h);
await commitState();
}
logger.fine('_handleAckResponse: Releasing lock...');
}); });
return true; return true;
@ -347,40 +340,33 @@ class StreamManagementManager extends XmppManagerBase {
Future<void> _incrementC2S() async { Future<void> _incrementC2S() async {
logger.fine('_incrementC2S: Waiting to aquire lock...'); logger.fine('_incrementC2S: Waiting to aquire lock...');
await _stateLock.synchronized(() async { await _stateLock.synchronized(() async {
logger.fine('_incrementC2S: Done'); logger.fine('_incrementC2S: Done');
_state = _state.copyWith(c2s: _state.c2s + 1 % xmlUintMax); _state = _state.copyWith(c2s: _state.c2s + 1 % xmlUintMax);
await commitState(); await commitState();
logger.fine('_incrementC2S: Releasing lock...'); logger.fine('_incrementC2S: Releasing lock...');
}); });
} }
Future<void> _incrementS2C() async { Future<void> _incrementS2C() async {
logger.fine('_incrementS2C: Waiting to aquire lock...'); logger.fine('_incrementS2C: Waiting to aquire lock...');
await _stateLock.synchronized(() async { await _stateLock.synchronized(() async {
logger.fine('_incrementS2C: Done'); logger.fine('_incrementS2C: Done');
_state = _state.copyWith(s2c: _state.s2c + 1 % xmlUintMax); _state = _state.copyWith(s2c: _state.s2c + 1 % xmlUintMax);
await commitState(); await commitState();
logger.fine('_incrementS2C: Releasing lock...'); logger.fine('_incrementS2C: Releasing lock...');
}); });
} }
/// Called whenever we receive a stanza from the server. /// Called whenever we receive a stanza from the server.
Future<StanzaHandlerData> _onServerStanzaReceived( Future<StanzaHandlerData> _onServerStanzaReceived(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
await _incrementS2C(); await _incrementS2C();
return state; return state;
} }
/// Called whenever we send a stanza. /// Called whenever we send a stanza.
Future<StanzaHandlerData> _onClientStanzaSent( Future<StanzaHandlerData> _onClientStanzaSent(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
await _incrementC2S(); await _incrementC2S();
_unackedStanzas[_state.c2s] = stanza; _unackedStanzas[_state.c2s] = stanza;
if (isStreamManagementEnabled()) { if (isStreamManagementEnabled()) {
await _sendAckRequest(); await _sendAckRequest();
} }
@ -396,7 +382,7 @@ class StreamManagementManager extends XmppManagerBase {
final stanzas = _unackedStanzas.values.toList(); final stanzas = _unackedStanzas.values.toList();
_unackedStanzas.clear(); _unackedStanzas.clear();
// Retransmit the rest of the queue // Retransmit the rest of the queue
final attrs = getAttributes(); final attrs = getAttributes();
for (final stanza in stanzas) { for (final stanza in stanzas) {

View File

@ -21,17 +21,14 @@ class DelayedDeliveryManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onIncomingMessage, callback: _onIncomingMessage,
priority: 200, priority: 200,
), ),
]; ];
Future<StanzaHandlerData> _onIncomingMessage( Future<StanzaHandlerData> _onIncomingMessage(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
final delay = stanza.firstTag('delay', xmlns: delayedDeliveryXmlns); final delay = stanza.firstTag('delay', xmlns: delayedDeliveryXmlns);
if (delay == null) return state; if (delay == null) return state;

View File

@ -24,24 +24,24 @@ class CarbonsManager extends XmppManagerBase {
/// Indicates that we know that [CarbonsManager._supported] is accurate. /// Indicates that we know that [CarbonsManager._supported] is accurate.
bool _gotSupported = false; bool _gotSupported = false;
@override @override
List<StanzaHandler> getIncomingPreStanzaHandlers() => [ List<StanzaHandler> getIncomingPreStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'received', tagName: 'received',
tagXmlns: carbonsXmlns, tagXmlns: carbonsXmlns,
callback: _onMessageReceived, callback: _onMessageReceived,
priority: -98, priority: -98,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'sent', tagName: 'sent',
tagXmlns: carbonsXmlns, tagXmlns: carbonsXmlns,
callback: _onMessageSent, callback: _onMessageSent,
priority: -98, priority: -98,
) )
]; ];
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {
@ -68,11 +68,8 @@ class CarbonsManager extends XmppManagerBase {
} }
} }
} }
Future<StanzaHandlerData> _onMessageReceived( Future<StanzaHandlerData> _onMessageReceived(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final from = JID.fromString(message.attributes['from']! as String); final from = JID.fromString(message.attributes['from']! as String);
final received = message.firstTag('received', xmlns: carbonsXmlns)!; final received = message.firstTag('received', xmlns: carbonsXmlns)!;
if (!isCarbonValid(from)) return state.copyWith(done: true); if (!isCarbonValid(from)) return state.copyWith(done: true);
@ -86,10 +83,7 @@ class CarbonsManager extends XmppManagerBase {
); );
} }
Future<StanzaHandlerData> _onMessageSent( Future<StanzaHandlerData> _onMessageSent(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final from = JID.fromString(message.attributes['from']! as String); final from = JID.fromString(message.attributes['from']! as String);
final sent = message.firstTag('sent', xmlns: carbonsXmlns)!; final sent = message.firstTag('sent', xmlns: carbonsXmlns)!;
if (!isCarbonValid(from)) return state.copyWith(done: true); if (!isCarbonValid(from)) return state.copyWith(done: true);
@ -160,14 +154,14 @@ class CarbonsManager extends XmppManagerBase {
} }
logger.fine('Successfully disabled message carbons'); logger.fine('Successfully disabled message carbons');
_isEnabled = false; _isEnabled = false;
return true; return true;
} }
/// True if Message Carbons are enabled. False, if not. /// True if Message Carbons are enabled. False, if not.
bool get isEnabled => _isEnabled; bool get isEnabled => _isEnabled;
@visibleForTesting @visibleForTesting
void forceEnable() { void forceEnable() {
_isEnabled = true; _isEnabled = true;
@ -178,10 +172,9 @@ class CarbonsManager extends XmppManagerBase {
/// ///
/// Returns true if the carbon is valid. Returns false if not. /// Returns true if the carbon is valid. Returns false if not.
bool isCarbonValid(JID senderJid) { bool isCarbonValid(JID senderJid) {
return _isEnabled && return _isEnabled && getAttributes().getFullJID().bareCompare(
getAttributes().getFullJID().bareCompare( senderJid,
senderJid, ensureBare: true,
ensureBare: true, );
);
} }
} }

View File

@ -4,10 +4,7 @@ import 'package:moxxmpp/src/stringxml.dart';
/// Extracts the message stanza from the <forwarded /> node. /// Extracts the message stanza from the <forwarded /> node.
Stanza unpackForwarded(XMLNode forwarded) { Stanza unpackForwarded(XMLNode forwarded) {
assert( assert(forwarded.attributes['xmlns'] == forwardedXmlns, 'Invalid element xmlns');
forwarded.attributes['xmlns'] == forwardedXmlns,
'Invalid element xmlns',
);
assert(forwarded.tag == 'forwarded', 'Invalid element name'); assert(forwarded.tag == 'forwarded', 'Invalid element name');
// NOTE: We only use this XEP (for now) in the context of Message Carbons // NOTE: We only use this XEP (for now) in the context of Message Carbons

View File

@ -8,7 +8,7 @@ XMLNode constructHashElement(String algo, String base64Hash) {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'hash', tag: 'hash',
xmlns: hashXmlns, xmlns: hashXmlns,
attributes: {'algo': algo}, attributes: { 'algo': algo },
text: base64Hash, text: base64Hash,
); );
} }
@ -68,18 +68,15 @@ class CryptographicHashManager extends XmppManagerBase {
@override @override
List<String> getDiscoFeatures() => [ List<String> getDiscoFeatures() => [
'$hashFunctionNameBaseXmlns:$hashSha256', '$hashFunctionNameBaseXmlns:$hashSha256',
'$hashFunctionNameBaseXmlns:$hashSha512', '$hashFunctionNameBaseXmlns:$hashSha512',
//'$hashFunctionNameBaseXmlns:$hashSha3256', //'$hashFunctionNameBaseXmlns:$hashSha3256',
//'$hashFunctionNameBaseXmlns:$hashSha3512', //'$hashFunctionNameBaseXmlns:$hashSha3512',
//'$hashFunctionNameBaseXmlns:$hashBlake2b256', //'$hashFunctionNameBaseXmlns:$hashBlake2b256',
'$hashFunctionNameBaseXmlns:$hashBlake2b512', '$hashFunctionNameBaseXmlns:$hashBlake2b512',
]; ];
static Future<List<int>> hashFromData( static Future<List<int>> hashFromData(List<int> data, HashFunction function) async {
List<int> data,
HashFunction function,
) async {
// TODO(PapaTutuWawa): Implement the others as well // TODO(PapaTutuWawa): Implement the others as well
HashAlgorithm algo; HashAlgorithm algo;
switch (function) { switch (function) {

View File

@ -20,27 +20,24 @@ class LastMessageCorrectionManager extends XmppManagerBase {
LastMessageCorrectionManager() : super(lastMessageCorrectionManager); LastMessageCorrectionManager() : super(lastMessageCorrectionManager);
@override @override
List<String> getDiscoFeatures() => [lmcXmlns]; List<String> getDiscoFeatures() => [ lmcXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'replace', tagName: 'replace',
tagXmlns: lmcXmlns, tagXmlns: lmcXmlns,
callback: _onMessage, callback: _onMessage,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
final edit = stanza.firstTag('replace', xmlns: lmcXmlns)!; final edit = stanza.firstTag('replace', xmlns: lmcXmlns)!;
return state.copyWith( return state.copyWith(
lastMessageCorrectionSid: edit.attributes['id']! as String, lastMessageCorrectionSid: edit.attributes['id']! as String,

View File

@ -16,14 +16,11 @@ XMLNode makeChatMarkerMarkable() {
} }
XMLNode makeChatMarker(String tag, String id) { XMLNode makeChatMarker(String tag, String id) {
assert( assert(['received', 'displayed', 'acknowledged'].contains(tag), 'Invalid chat marker');
['received', 'displayed', 'acknowledged'].contains(tag),
'Invalid chat marker',
);
return XMLNode.xmlns( return XMLNode.xmlns(
tag: tag, tag: tag,
xmlns: chatMarkersXmlns, xmlns: chatMarkersXmlns,
attributes: {'id': id}, attributes: { 'id': id },
); );
} }
@ -31,41 +28,36 @@ class ChatMarkerManager extends XmppManagerBase {
ChatMarkerManager() : super(chatMarkerManager); ChatMarkerManager() : super(chatMarkerManager);
@override @override
List<String> getDiscoFeatures() => [chatMarkersXmlns]; List<String> getDiscoFeatures() => [ chatMarkersXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagXmlns: chatMarkersXmlns, tagXmlns: chatMarkersXmlns,
callback: _onMessage, callback: _onMessage,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final marker = message.firstTagByXmlns(chatMarkersXmlns)!; final marker = message.firstTagByXmlns(chatMarkersXmlns)!;
// Handle the <markable /> explicitly // Handle the <markable /> explicitly
if (marker.tag == 'markable') return state.copyWith(isMarkable: true); if (marker.tag == 'markable') return state.copyWith(isMarkable: true);
if (!['received', 'displayed', 'acknowledged'].contains(marker.tag)) { if (!['received', 'displayed', 'acknowledged'].contains(marker.tag)) {
logger.warning("Unknown message marker '${marker.tag}' found."); logger.warning("Unknown message marker '${marker.tag}' found.");
} else { } else {
getAttributes().sendEvent( getAttributes().sendEvent(ChatMarkerEvent(
ChatMarkerEvent(
from: JID.fromString(message.from!), from: JID.fromString(message.from!),
type: marker.tag, type: marker.tag,
id: marker.attributes['id']! as String, id: marker.attributes['id']! as String,
), ),);
);
} }
return state.copyWith(done: true); return state.copyWith(done: true);

View File

@ -10,16 +10,12 @@ enum MessageProcessingHint {
MessageProcessingHint messageProcessingHintFromXml(XMLNode element) { MessageProcessingHint messageProcessingHintFromXml(XMLNode element) {
switch (element.tag) { switch (element.tag) {
case 'no-permanent-store': case 'no-permanent-store': return MessageProcessingHint.noPermanentStore;
return MessageProcessingHint.noPermanentStore; case 'no-store': return MessageProcessingHint.noStore;
case 'no-store': case 'no-copy': return MessageProcessingHint.noCopies;
return MessageProcessingHint.noStore; case 'store': return MessageProcessingHint.store;
case 'no-copy':
return MessageProcessingHint.noCopies;
case 'store':
return MessageProcessingHint.store;
} }
assert(false, 'Invalid Message Processing Hint: ${element.tag}'); assert(false, 'Invalid Message Processing Hint: ${element.tag}');
return MessageProcessingHint.noStore; return MessageProcessingHint.noStore;
} }

View File

@ -7,19 +7,21 @@ import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/types/result.dart'; import 'package:moxxmpp/src/types/result.dart';
class CSIActiveNonza extends XMLNode { class CSIActiveNonza extends XMLNode {
CSIActiveNonza() CSIActiveNonza() : super(
: super( tag: 'active',
tag: 'active', attributes: <String, String>{
attributes: <String, String>{'xmlns': csiXmlns}, 'xmlns': csiXmlns
); },
);
} }
class CSIInactiveNonza extends XMLNode { class CSIInactiveNonza extends XMLNode {
CSIInactiveNonza() CSIInactiveNonza() : super(
: super( tag: 'inactive',
tag: 'inactive', attributes: <String, String>{
attributes: <String, String>{'xmlns': csiXmlns}, 'xmlns': csiXmlns
); },
);
} }
/// A Stub negotiator that is just for "intercepting" the stream feature. /// A Stub negotiator that is just for "intercepting" the stream feature.
@ -29,11 +31,9 @@ class CSINegotiator extends XmppFeatureNegotiatorBase {
/// True if CSI is supported. False otherwise. /// True if CSI is supported. False otherwise.
bool _supported = false; bool _supported = false;
bool get isSupported => _supported; bool get isSupported => _supported;
@override @override
Future<Result<NegotiatorState, NegotiatorError>> negotiate( Future<Result<NegotiatorState, NegotiatorError>> negotiate(XMLNode nonza) async {
XMLNode nonza,
) async {
// negotiate is only called when the negotiator matched, meaning the server // negotiate is only called when the negotiator matched, meaning the server
// advertises CSI. // advertises CSI.
_supported = true; _supported = true;
@ -56,11 +56,9 @@ class CSIManager extends XmppManagerBase {
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {
return getAttributes() return getAttributes().getNegotiatorById<CSINegotiator>(csiNegotiator)!.isSupported;
.getNegotiatorById<CSINegotiator>(csiNegotiator)!
.isSupported;
} }
/// To be called after a stream has been resumed as CSI does not /// To be called after a stream has been resumed as CSI does not
/// survive a stream resumption. /// survive a stream resumption.
void restoreCSIState() { void restoreCSIState() {
@ -70,7 +68,7 @@ class CSIManager extends XmppManagerBase {
setInactive(); setInactive();
} }
} }
/// Tells the server to top optimizing traffic /// Tells the server to top optimizing traffic
Future<void> setActive() async { Future<void> setActive() async {
_isActive = true; _isActive = true;

View File

@ -13,7 +13,7 @@ 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;
final String? stanzaIdBy; final String? stanzaIdBy;
@ -23,7 +23,7 @@ XMLNode makeOriginIdElement(String id) {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'origin-id', tag: 'origin-id',
xmlns: stableIdXmlns, xmlns: stableIdXmlns,
attributes: {'id': id}, attributes: { 'id': id },
); );
} }
@ -31,25 +31,22 @@ class StableIdManager extends XmppManagerBase {
StableIdManager() : super(stableIdManager); StableIdManager() : super(stableIdManager);
@override @override
List<String> getDiscoFeatures() => [stableIdXmlns]; List<String> getDiscoFeatures() => [ stableIdXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onMessage, callback: _onMessage,
// Before the MessageManager // Before the MessageManager
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final from = JID.fromString(message.attributes['from']! as String); final from = JID.fromString(message.attributes['from']! as String);
String? originId; String? originId;
String? stanzaId; String? stanzaId;
@ -77,14 +74,10 @@ class StableIdManager extends XmppManagerBase {
stanzaId = stanzaIdTag.attributes['id']! as String; stanzaId = stanzaIdTag.attributes['id']! as String;
stanzaIdBy = stanzaIdTag.attributes['by']! as String; stanzaIdBy = stanzaIdTag.attributes['by']! as String;
} else { } else {
logger.finest( logger.finest('${from.toString()} does not support $stableIdXmlns. Ignoring stanza id... ');
'${from.toString()} does not support $stableIdXmlns. Ignoring stanza id... ',
);
} }
} else { } else {
logger.finest( logger.finest('Failed to find out if ${from.toString()} supports $stableIdXmlns. Ignoring... ');
'Failed to find out if ${from.toString()} supports $stableIdXmlns. Ignoring... ',
);
} }
} }

View File

@ -13,7 +13,7 @@ import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart'; import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
import 'package:moxxmpp/src/xeps/xep_0363/errors.dart'; import 'package:moxxmpp/src/xeps/xep_0363/errors.dart';
const allowedHTTPHeaders = ['authorization', 'cookie', 'expires']; const allowedHTTPHeaders = [ 'authorization', 'cookie', 'expires' ];
class HttpFileUploadSlot { class HttpFileUploadSlot {
const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers); const HttpFileUploadSlot(this.putUrl, this.getUrl, this.headers);
@ -32,12 +32,12 @@ String _stripNewlinesFromString(String value) {
@visibleForTesting @visibleForTesting
Map<String, String> prepareHeaders(Map<String, String> headers) { Map<String, String> prepareHeaders(Map<String, String> headers) {
return headers.map((key, value) { return headers.map((key, value) {
return MapEntry( return MapEntry(
_stripNewlinesFromString(key), _stripNewlinesFromString(key),
_stripNewlinesFromString(value), _stripNewlinesFromString(value),
); );
}) })
..removeWhere((key, _) => !allowedHTTPHeaders.contains(key.toLowerCase())); ..removeWhere((key, _) => !allowedHTTPHeaders.contains(key.toLowerCase()));
} }
class HttpFileUploadManager extends XmppManagerBase { class HttpFileUploadManager extends XmppManagerBase {
@ -58,10 +58,7 @@ class HttpFileUploadManager extends XmppManagerBase {
/// 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.
bool _containsFileUploadIdentity(DiscoInfo info) { bool _containsFileUploadIdentity(DiscoInfo info) {
return listContains( return listContains(info.identities, (Identity id) => id.category == 'store' && id.type == 'file');
info.identities,
(Identity id) => id.category == 'store' && id.type == 'file',
);
} }
/// Extract the maximum filesize in octets from the disco response. Returns null /// Extract the maximum filesize in octets from the disco response. Returns null
@ -90,14 +87,12 @@ class HttpFileUploadManager extends XmppManagerBase {
} }
} }
} }
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {
if (_gotSupported) return _supported; if (_gotSupported) return _supported;
final result = await getAttributes() final result = await getAttributes().getManagerById<DiscoManager>(discoManager)!.performDiscoSweep();
.getManagerById<DiscoManager>(discoManager)!
.performDiscoSweep();
if (result.isType<DiscoError>()) { if (result.isType<DiscoError>()) {
_gotSupported = false; _gotSupported = false;
_supported = false; _supported = false;
@ -107,9 +102,8 @@ class HttpFileUploadManager extends XmppManagerBase {
final infos = result.get<List<DiscoInfo>>(); final infos = result.get<List<DiscoInfo>>();
_gotSupported = true; _gotSupported = true;
for (final info in infos) { for (final info in infos) {
if (_containsFileUploadIdentity(info) && if (_containsFileUploadIdentity(info) && info.features.contains(httpFileUploadXmlns)) {
info.features.contains(httpFileUploadXmlns)) { logger.info('Discovered HTTP File Upload for ${info.jid}');
logger.info('Discovered HTTP File Upload for ${info.jid}');
_entityJid = info.jid; _entityJid = info.jid;
_maxUploadSize = _getMaxFileSize(info); _maxUploadSize = _getMaxFileSize(info);
@ -125,29 +119,19 @@ class HttpFileUploadManager extends XmppManagerBase {
/// the file's size in octets. [contentType] is optional and refers to the file's /// the file's size in octets. [contentType] is optional and refers to the file's
/// Mime type. /// Mime type.
/// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise. /// Returns an [HttpFileUploadSlot] if the request was successful; null otherwise.
Future<Result<HttpFileUploadSlot, HttpFileUploadError>> requestUploadSlot( Future<Result<HttpFileUploadSlot, HttpFileUploadError>> requestUploadSlot(String filename, int filesize, { String? contentType }) async {
String filename, if (!(await isSupported())) return Result(NoEntityKnownError());
int filesize, {
String? contentType,
}) async {
if (!(await isSupported())) {
return Result(NoEntityKnownError());
}
if (_entityJid == null) { if (_entityJid == null) {
logger.warning( logger.warning('Attempted to request HTTP File Upload slot but no entity is known to send this request to.');
'Attempted to request HTTP File Upload slot but no entity is known to send this request to.',
);
return Result(NoEntityKnownError()); return Result(NoEntityKnownError());
} }
if (_maxUploadSize != null && filesize > _maxUploadSize!) { if (_maxUploadSize != null && filesize > _maxUploadSize!) {
logger.warning( logger.warning('Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit');
'Attempted to request HTTP File Upload slot for a file that exceeds the filesize limit',
);
return Result(FileTooBigError()); return Result(FileTooBigError());
} }
final attrs = getAttributes(); final attrs = getAttributes();
final response = await attrs.sendStanza( final response = await attrs.sendStanza(
Stanza.iq( Stanza.iq(
@ -160,7 +144,7 @@ class HttpFileUploadManager extends XmppManagerBase {
attributes: { attributes: {
'filename': filename, 'filename': filename,
'size': filesize.toString(), 'size': filesize.toString(),
...contentType != null ? {'content-type': contentType} : {} ...contentType != null ? { 'content-type': contentType } : {}
}, },
) )
], ],

View File

@ -18,39 +18,25 @@ enum ExplicitEncryptionType {
String _explicitEncryptionTypeToString(ExplicitEncryptionType type) { String _explicitEncryptionTypeToString(ExplicitEncryptionType type) {
switch (type) { switch (type) {
case ExplicitEncryptionType.otr: case ExplicitEncryptionType.otr: return emeOtr;
return emeOtr; case ExplicitEncryptionType.legacyOpenPGP: return emeLegacyOpenPGP;
case ExplicitEncryptionType.legacyOpenPGP: case ExplicitEncryptionType.openPGP: return emeOpenPGP;
return emeLegacyOpenPGP; case ExplicitEncryptionType.omemo: return emeOmemo;
case ExplicitEncryptionType.openPGP: case ExplicitEncryptionType.omemo1: return emeOmemo1;
return emeOpenPGP; case ExplicitEncryptionType.omemo2: return emeOmemo2;
case ExplicitEncryptionType.omemo: case ExplicitEncryptionType.unknown: return '';
return emeOmemo;
case ExplicitEncryptionType.omemo1:
return emeOmemo1;
case ExplicitEncryptionType.omemo2:
return emeOmemo2;
case ExplicitEncryptionType.unknown:
return '';
} }
} }
ExplicitEncryptionType _explicitEncryptionTypeFromString(String str) { ExplicitEncryptionType _explicitEncryptionTypeFromString(String str) {
switch (str) { switch (str) {
case emeOtr: case emeOtr: return ExplicitEncryptionType.otr;
return ExplicitEncryptionType.otr; case emeLegacyOpenPGP: return ExplicitEncryptionType.legacyOpenPGP;
case emeLegacyOpenPGP: case emeOpenPGP: return ExplicitEncryptionType.openPGP;
return ExplicitEncryptionType.legacyOpenPGP; case emeOmemo: return ExplicitEncryptionType.omemo;
case emeOpenPGP: case emeOmemo1: return ExplicitEncryptionType.omemo1;
return ExplicitEncryptionType.openPGP; case emeOmemo2: return ExplicitEncryptionType.omemo2;
case emeOmemo: default: return ExplicitEncryptionType.unknown;
return ExplicitEncryptionType.omemo;
case emeOmemo1:
return ExplicitEncryptionType.omemo1;
case emeOmemo2:
return ExplicitEncryptionType.omemo2;
default:
return ExplicitEncryptionType.unknown;
} }
} }
@ -72,23 +58,20 @@ class EmeManager extends XmppManagerBase {
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() => [
StanzaHandler( StanzaHandler(
tagName: 'encryption', tagName: 'encryption',
tagXmlns: emeXmlns, tagXmlns: emeXmlns,
callback: _onStanzaReceived, callback: _onStanzaReceived,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
), ),
]; ];
Future<StanzaHandlerData> _onStanzaReceived( Future<StanzaHandlerData> _onStanzaReceived(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final encryption = message.firstTag('encryption', xmlns: emeXmlns)!; final encryption = message.firstTag('encryption', xmlns: emeXmlns)!;
return state.copyWith( return state.copyWith(

View File

@ -15,7 +15,6 @@ bool checkAffixElements(XMLNode envelope, String sender, JID ourJid) {
if (to == null) return false; if (to == null) return false;
final encReceiver = JID.fromString(to); final encReceiver = JID.fromString(to);
return encSender.toBare().toString() == return encSender.toBare().toString() == JID.fromString(sender).toBare().toString() &&
JID.fromString(sender).toBare().toString() && encReceiver.toBare().toString() == ourJid.toBare().toString();
encReceiver.toBare().toString() == ourJid.toBare().toString();
} }

View File

@ -46,8 +46,7 @@ XMLNode bundleToXML(OmemoBundle bundle) {
for (final pk in bundle.opksEncoded.entries) { for (final pk in bundle.opksEncoded.entries) {
prekeys.add( prekeys.add(
XMLNode( XMLNode(
tag: 'pk', tag: 'pk', attributes: <String, String>{
attributes: <String, String>{
'id': '${pk.key}', 'id': '${pk.key}',
}, },
text: pk.value, text: pk.value,

View File

@ -1,5 +1,6 @@
/// A simple wrapper class for defining elements that should not be encrypted. /// A simple wrapper class for defining elements that should not be encrypted.
class DoNotEncrypt { class DoNotEncrypt {
const DoNotEncrypt(this.tag, this.xmlns); const DoNotEncrypt(this.tag, this.xmlns);
final String tag; final String tag;
final String xmlns; final String xmlns;

View File

@ -52,42 +52,42 @@ abstract class BaseOmemoManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingPreStanzaHandlers() => [ List<StanzaHandler> getIncomingPreStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'iq', stanzaTag: 'iq',
tagXmlns: omemoXmlns, tagXmlns: omemoXmlns,
tagName: 'encrypted', tagName: 'encrypted',
callback: _onIncomingStanza, callback: _onIncomingStanza,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'presence', stanzaTag: 'presence',
tagXmlns: omemoXmlns, tagXmlns: omemoXmlns,
tagName: 'encrypted', tagName: 'encrypted',
callback: _onIncomingStanza, callback: _onIncomingStanza,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagXmlns: omemoXmlns, tagXmlns: omemoXmlns,
tagName: 'encrypted', tagName: 'encrypted',
callback: _onIncomingStanza, callback: _onIncomingStanza,
), ),
]; ];
@override @override
List<StanzaHandler> getOutgoingPreStanzaHandlers() => [ List<StanzaHandler> getOutgoingPreStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'iq', stanzaTag: 'iq',
callback: _onOutgoingStanza, callback: _onOutgoingStanza,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'presence', stanzaTag: 'presence',
callback: _onOutgoingStanza, callback: _onOutgoingStanza,
), ),
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onOutgoingStanza, callback: _onOutgoingStanza,
priority: 100, priority: 100,
), ),
]; ];
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
@ -98,8 +98,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
final ownJid = getAttributes().getFullJID().toBare().toString(); final ownJid = getAttributes().getFullJID().toBare().toString();
final jid = JID.fromString(event.from).toBare(); final jid = JID.fromString(event.from).toBare();
final ids = event.item.payload.children final ids = event.item.payload.children
.map((child) => int.parse(child.attributes['id']! as String)) .map((child) => int.parse(child.attributes['id']! as String))
.toList(); .toList();
if (event.from == ownJid) { if (event.from == ownJid) {
// Another client published to our device list node // Another client published to our device list node
@ -113,7 +113,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
} }
// Tell the OmemoManager // Tell the OmemoManager
(await getOmemoManager()).onDeviceListUpdate(jid.toString(), ids); (await getOmemoManager())
.onDeviceListUpdate(jid.toString(), ids);
// Generate an event // Generate an event
getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids)); getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids));
@ -123,6 +124,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
@visibleForOverriding @visibleForOverriding
Future<OmemoManager> getOmemoManager(); Future<OmemoManager> getOmemoManager();
/// Wrapper around using getSessionManager and then calling getDeviceId on it. /// Wrapper around using getSessionManager and then calling getDeviceId on it.
Future<int> _getDeviceId() async => (await getOmemoManager()).getDeviceId(); Future<int> _getDeviceId() async => (await getOmemoManager()).getDeviceId();
@ -152,7 +154,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
return true; return true;
} }
/// Encrypt [children] using OMEMO. This either produces an <encrypted /> element with /// Encrypt [children] using OMEMO. This either produces an <encrypted /> element with
/// an attached payload, if [children] is not null, or an empty OMEMO message if /// an attached payload, if [children] is not null, or an empty OMEMO message if
/// [children] is null. This function takes care of creating the affix elements as /// [children] is null. This function takes care of creating the affix elements as
@ -167,6 +169,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
tag: 'content', tag: 'content',
children: children, children: children,
), ),
XMLNode( XMLNode(
tag: 'rpad', tag: 'rpad',
text: generateRpad(), text: generateRpad(),
@ -198,11 +201,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
return payload.toXml(); return payload.toXml();
} }
XMLNode _buildEncryptedElement( XMLNode _buildEncryptedElement(EncryptionResult result, String recipientJid, int deviceId) {
EncryptionResult result,
String recipientJid,
int deviceId,
) {
final keyElements = <String, List<XMLNode>>{}; final keyElements = <String, List<XMLNode>>{};
for (final key in result.encryptedKeys) { for (final key in result.encryptedKeys) {
final keyElement = XMLNode( final keyElement = XMLNode(
@ -240,7 +239,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
), ),
]; ];
} }
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'encrypted', tag: 'encrypted',
xmlns: omemoXmlns, xmlns: omemoXmlns,
@ -258,10 +257,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
} }
/// For usage with omemo_dart's OmemoManager. /// For usage with omemo_dart's OmemoManager.
Future<void> sendEmptyMessageImpl( Future<void> sendEmptyMessageImpl(EncryptionResult result, String toJid) async {
EncryptionResult result,
String toJid,
) async {
await getAttributes().sendStanza( await getAttributes().sendStanza(
Stanza.message( Stanza.message(
to: toJid, to: toJid,
@ -289,7 +285,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
final om = await getOmemoManager(); final om = await getOmemoManager();
await om.sendOmemoHeartbeat(jid); await om.sendOmemoHeartbeat(jid);
} }
/// For usage with omemo_dart's OmemoManager /// For usage with omemo_dart's OmemoManager
Future<List<int>?> fetchDeviceList(String jid) async { Future<List<int>?> fetchDeviceList(String jid) async {
final result = await getDeviceList(JID.fromString(jid)); final result = await getDeviceList(JID.fromString(jid));
@ -305,11 +301,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
return result.get<OmemoBundle>(); return result.get<OmemoBundle>();
} }
Future<StanzaHandlerData> _onOutgoingStanza( Future<StanzaHandlerData> _onOutgoingStanza(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
if (state.encrypted) { if (state.encrypted) {
logger.finest('Not encrypting since state.encrypted is true'); logger.finest('Not encrypting since state.encrypted is true');
return state; return state;
@ -324,14 +317,10 @@ abstract class BaseOmemoManager extends XmppManagerBase {
final toJid = JID.fromString(stanza.to!).toBare(); final toJid = JID.fromString(stanza.to!).toBare();
final shouldEncryptResult = await shouldEncryptStanza(toJid, stanza); final shouldEncryptResult = await shouldEncryptStanza(toJid, stanza);
if (!shouldEncryptResult && !state.forceEncryption) { if (!shouldEncryptResult && !state.forceEncryption) {
logger.finest( logger.finest('Not encrypting stanza for $toJid: Both shouldEncryptStanza and forceEncryption are false.');
'Not encrypting stanza for $toJid: Both shouldEncryptStanza and forceEncryption are false.',
);
return state; return state;
} else { } else {
logger.finest( logger.finest('Encrypting stanza for $toJid: shouldEncryptResult=$shouldEncryptResult, forceEncryption=${state.forceEncryption}');
'Encrypting stanza for $toJid: shouldEncryptResult=$shouldEncryptResult, forceEncryption=${state.forceEncryption}',
);
} }
final toEncrypt = List<XMLNode>.empty(growable: true); final toEncrypt = List<XMLNode>.empty(growable: true);
@ -343,18 +332,17 @@ abstract class BaseOmemoManager extends XmppManagerBase {
toEncrypt.add(child); toEncrypt.add(child);
} }
} }
logger.finest('Beginning encryption'); logger.finest('Beginning encryption');
final carbonsEnabled = getAttributes() final carbonsEnabled = getAttributes()
.getManagerById<CarbonsManager>(carbonsManager) .getManagerById<CarbonsManager>(carbonsManager)?.isEnabled ?? false;
?.isEnabled ??
false;
final om = await getOmemoManager(); final om = await getOmemoManager();
final result = await om.onOutgoingStanza( final result = await om.onOutgoingStanza(
OmemoOutgoingStanza( OmemoOutgoingStanza(
[ [
toJid.toString(), toJid.toString(),
if (carbonsEnabled) getAttributes().getFullJID().toBare().toString(), if (carbonsEnabled)
getAttributes().getFullJID().toBare().toString(),
], ],
_buildEnvelope(toEncrypt, toJid.toString()), _buildEnvelope(toEncrypt, toJid.toString()),
), ),
@ -369,21 +357,20 @@ abstract class BaseOmemoManager extends XmppManagerBase {
other: other, other: other,
// If we have no device list for toJid, then the contact most likely does not // If we have no device list for toJid, then the contact most likely does not
// support OMEMO:2 // support OMEMO:2
cancelReason: result.jidEncryptionErrors[toJid.toString()] cancelReason: result.jidEncryptionErrors[toJid.toString()] is NoKeyMaterialAvailableException ?
is NoKeyMaterialAvailableException OmemoNotSupportedForContactException() :
? OmemoNotSupportedForContactException() UnknownOmemoError(),
: UnknownOmemoError(),
cancel: true, cancel: true,
); );
} }
final encrypted = _buildEncryptedElement( final encrypted = _buildEncryptedElement(
result, result,
toJid.toString(), toJid.toString(),
await _getDeviceId(), await _getDeviceId(),
); );
children.add(encrypted); children.add(encrypted);
// Only add message specific metadata when actually sending a message // Only add message specific metadata when actually sending a message
if (stanza.tag == 'message') { if (stanza.tag == 'message') {
children children
@ -394,7 +381,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
// https://xmpp.org/extensions/xep-0384.html#message-structure-description. // https://xmpp.org/extensions/xep-0384.html#message-structure-description.
..add(MessageProcessingHint.store.toXml()); ..add(MessageProcessingHint.store.toXml());
} }
return state.copyWith( return state.copyWith(
stanza: state.stanza.copyWith( stanza: state.stanza.copyWith(
children: children, children: children,
@ -408,11 +395,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
/// encrypted. /// encrypted.
@visibleForOverriding @visibleForOverriding
Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza); Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza);
Future<StanzaHandlerData> _onIncomingStanza( Future<StanzaHandlerData> _onIncomingStanza(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
final encrypted = stanza.firstTag('encrypted', xmlns: omemoXmlns); final encrypted = stanza.firstTag('encrypted', xmlns: omemoXmlns);
if (encrypted == null) return state; if (encrypted == null) return state;
if (stanza.from == null) return state; if (stanza.from == null) return state;
@ -443,8 +427,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
OmemoIncomingStanza( OmemoIncomingStanza(
fromJid.toString(), fromJid.toString(),
sid, sid,
state.delayedDelivery?.timestamp.millisecondsSinceEpoch ?? state.delayedDelivery?.timestamp.millisecondsSinceEpoch ?? DateTime.now().millisecondsSinceEpoch,
DateTime.now().millisecondsSinceEpoch,
keys, keys,
payloadElement?.innerText(), payloadElement?.innerText(),
), ),
@ -455,13 +438,9 @@ abstract class BaseOmemoManager extends XmppManagerBase {
if (result.error != null) { if (result.error != null) {
other['encryption_error'] = result.error; other['encryption_error'] = result.error;
} else { } else {
children = stanza.children children = stanza.children.where(
.where( (child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns,
(child) => ).toList();
child.tag != 'encrypted' ||
child.attributes['xmlns'] != omemoXmlns,
)
.toList();
} }
if (result.payload != null) { if (result.payload != null) {
@ -480,7 +459,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
final envelopeChildren = envelope.firstTag('content')?.children; final envelopeChildren = envelope.firstTag('content')?.children;
if (envelopeChildren != null) { if (envelopeChildren != null) {
children.addAll( children.addAll(
// Do not add forbidden elements from the envelope // Do not add forbidden elements from the envelope
envelopeChildren.where(shouldEncryptElement), envelopeChildren.where(shouldEncryptElement),
); );
} else { } else {
@ -511,57 +490,44 @@ abstract class BaseOmemoManager extends XmppManagerBase {
/// device list PubSub node. /// device list PubSub node.
/// ///
/// On success, returns the XML data. On failure, returns an OmemoError. /// On success, returns the XML data. On failure, returns an OmemoError.
Future<Result<OmemoError, XMLNode>> _retrieveDeviceListPayload( Future<Result<OmemoError, XMLNode>> _retrieveDeviceListPayload(JID jid) async {
JID jid,
) async {
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
final result = final result = await pm.getItems(jid.toBare().toString(), omemoDevicesXmlns);
await pm.getItems(jid.toBare().toString(), omemoDevicesXmlns);
if (result.isType<PubSubError>()) return Result(UnknownOmemoError()); if (result.isType<PubSubError>()) return Result(UnknownOmemoError());
return Result(result.get<List<PubSubItem>>().first.payload); return Result(result.get<List<PubSubItem>>().first.payload);
} }
/// Retrieves the OMEMO device list from [jid]. /// Retrieves the OMEMO device list from [jid].
Future<Result<OmemoError, List<int>>> getDeviceList(JID jid) async { Future<Result<OmemoError, List<int>>> getDeviceList(JID jid) async {
final itemsRaw = await _retrieveDeviceListPayload(jid); final itemsRaw = await _retrieveDeviceListPayload(jid);
if (itemsRaw.isType<OmemoError>()) return Result(UnknownOmemoError()); if (itemsRaw.isType<OmemoError>()) return Result(UnknownOmemoError());
final ids = itemsRaw final ids = itemsRaw.get<XMLNode>().children
.get<XMLNode>() .map((child) => int.parse(child.attributes['id']! as String))
.children .toList();
.map((child) => int.parse(child.attributes['id']! as String))
.toList();
return Result(ids); return Result(ids);
} }
/// Retrieve all device bundles for the JID [jid]. /// Retrieve all device bundles for the JID [jid].
/// ///
/// On success, returns a list of devices. On failure, returns am OmemoError. /// On success, returns a list of devices. On failure, returns am OmemoError.
Future<Result<OmemoError, List<OmemoBundle>>> retrieveDeviceBundles( Future<Result<OmemoError, List<OmemoBundle>>> retrieveDeviceBundles(JID jid) async {
JID jid,
) async {
// TODO(Unknown): Should we query the device list first? // TODO(Unknown): Should we query the device list first?
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
final bundlesRaw = await pm.getItems(jid.toString(), omemoBundlesXmlns); final bundlesRaw = await pm.getItems(jid.toString(), omemoBundlesXmlns);
if (bundlesRaw.isType<PubSubError>()) return Result(UnknownOmemoError()); if (bundlesRaw.isType<PubSubError>()) return Result(UnknownOmemoError());
final bundles = bundlesRaw final bundles = bundlesRaw.get<List<PubSubItem>>().map(
.get<List<PubSubItem>>() (bundle) => bundleFromXML(jid, int.parse(bundle.id), bundle.payload),
.map( ).toList();
(bundle) => bundleFromXML(jid, int.parse(bundle.id), bundle.payload),
)
.toList();
return Result(bundles); return Result(bundles);
} }
/// Retrieves a bundle from entity [jid] with the device id [deviceId]. /// Retrieves a bundle from entity [jid] with the device id [deviceId].
/// ///
/// On success, returns the device bundle. On failure, returns an OmemoError. /// On success, returns the device bundle. On failure, returns an OmemoError.
Future<Result<OmemoError, OmemoBundle>> retrieveDeviceBundle( Future<Result<OmemoError, OmemoBundle>> retrieveDeviceBundle(JID jid, int deviceId) async {
JID jid,
int deviceId,
) async {
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
final bareJid = jid.toBare().toString(); final bareJid = jid.toBare().toString();
final item = await pm.getItem(bareJid, omemoBundlesXmlns, '$deviceId'); final item = await pm.getItem(bareJid, omemoBundlesXmlns, '$deviceId');
@ -591,8 +557,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
); );
final ids = deviceList.children final ids = deviceList.children
.map((child) => int.parse(child.attributes['id']! as String)); .map((child) => int.parse(child.attributes['id']! as String));
if (!ids.contains(bundle.id)) { if (!ids.contains(bundle.id)) {
// Only update the device list if the device Id is not there // Only update the device list if the device Id is not there
final newDeviceList = XMLNode.xmlns( final newDeviceList = XMLNode.xmlns(
@ -608,7 +574,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
), ),
], ],
); );
final deviceListPublish = await pm.publish( final deviceListPublish = await pm.publish(
bareJid.toString(), bareJid.toString(),
omemoDevicesXmlns, omemoDevicesXmlns,
@ -619,7 +585,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
), ),
); );
if (deviceListPublish.isType<PubSubError>()) return const Result(false); if (deviceListPublish.isType<PubSubError>()) return const Result(false);
} }
final deviceBundlePublish = await pm.publish( final deviceBundlePublish = await pm.publish(
bareJid.toString(), bareJid.toString(),
@ -631,7 +597,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
maxItems: 'max', maxItems: 'max',
), ),
); );
return Result(deviceBundlePublish.isType<PubSubError>()); return Result(deviceBundlePublish.isType<PubSubError>());
} }
@ -652,8 +618,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
if (items.isType<DiscoError>()) return Result(UnknownOmemoError()); if (items.isType<DiscoError>()) return Result(UnknownOmemoError());
final nodes = items.get<List<DiscoItem>>(); final nodes = items.get<List<DiscoItem>>();
final result = nodes.any((item) => item.node == omemoDevicesXmlns) && final result = nodes.any((item) => item.node == omemoDevicesXmlns) && nodes.any((item) => item.node == omemoBundlesXmlns);
nodes.any((item) => item.node == omemoBundlesXmlns);
return Result(result); return Result(result);
} }
@ -683,8 +648,8 @@ abstract class BaseOmemoManager extends XmppManagerBase {
tag: 'devices', tag: 'devices',
xmlns: omemoDevicesXmlns, xmlns: omemoDevicesXmlns,
children: payload.children children: payload.children
.where((child) => child.attributes['id'] != '$deviceId') .where((child) => child.attributes['id'] != '$deviceId')
.toList(), .toList(),
); );
final publishResult = await pm.publish( final publishResult = await pm.publish(
jid.toString(), jid.toString(),

View File

@ -8,20 +8,13 @@ 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({ const StatelessMediaSharingData({ required this.mediaType, required this.size, required this.description, required this.hashes, required this.url, required this.thumbnails });
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;
final String description; final String description;
final Map<String, String> hashes; // algo -> hash value final Map<String, String> hashes; // algo -> hash value
final List<Thumbnail> thumbnails; final List<Thumbnail> thumbnails;
final String url; final String url;
} }
@ -36,8 +29,7 @@ StatelessMediaSharingData parseSIMSElement(XMLNode node) {
} }
var url = ''; var url = '';
final references = final references = file.firstTag('sources')!.findTags('reference', xmlns: referenceXmlns);
file.firstTag('sources')!.findTags('reference', xmlns: referenceXmlns);
for (final i in references) { for (final i in references) {
if (i.attributes['type'] != 'data') continue; if (i.attributes['type'] != 'data') continue;
@ -51,15 +43,14 @@ StatelessMediaSharingData parseSIMSElement(XMLNode node) {
final thumbnails = List<Thumbnail>.empty(growable: true); final thumbnails = List<Thumbnail>.empty(growable: true);
for (final child in file.children) { for (final child in file.children) {
// TODO(Unknown): Handle other thumbnails // TODO(Unknown): Handle other thumbnails
if (child.tag == 'file-thumbnail' && if (child.tag == 'file-thumbnail' && child.attributes['xmlns'] == fileThumbnailsXmlns) {
child.attributes['xmlns'] == fileThumbnailsXmlns) {
final thumb = parseFileThumbnailElement(child); final thumb = parseFileThumbnailElement(child);
if (thumb != null) { if (thumb != null) {
thumbnails.add(thumb); thumbnails.add(thumb);
} }
} }
} }
return StatelessMediaSharingData( return StatelessMediaSharingData(
mediaType: file.firstTag('media-type')!.innerText(), mediaType: file.firstTag('media-type')!.innerText(),
size: int.parse(file.firstTag('size')!.innerText()), size: int.parse(file.firstTag('size')!.innerText()),
@ -74,27 +65,24 @@ class SIMSManager extends XmppManagerBase {
SIMSManager() : super(simsManager); SIMSManager() : super(simsManager);
@override @override
List<String> getDiscoFeatures() => [simsXmlns]; List<String> getDiscoFeatures() => [ simsXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onMessage, callback: _onMessage,
tagName: 'reference', tagName: 'reference',
tagXmlns: referenceXmlns, tagXmlns: referenceXmlns,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final references = message.findTags('reference', xmlns: referenceXmlns); final references = message.findTags('reference', xmlns: referenceXmlns);
for (final ref in references) { for (final ref in references) {
final sims = ref.firstTag('media-sharing', xmlns: simsXmlns); final sims = ref.firstTag('media-sharing', xmlns: simsXmlns);

View File

@ -1,6 +1,7 @@
import 'package:cryptography/cryptography.dart'; import 'package:cryptography/cryptography.dart';
class InvalidHashAlgorithmException implements Exception { class InvalidHashAlgorithmException implements Exception {
InvalidHashAlgorithmException(this.name); InvalidHashAlgorithmException(this.name);
final String name; final String name;
@ -10,20 +11,16 @@ class InvalidHashAlgorithmException implements Exception {
/// Returns the hash algorithm specified by its name, according to XEP-0414. /// Returns the hash algorithm specified by its name, according to XEP-0414.
HashAlgorithm? getHashByName(String name) { HashAlgorithm? getHashByName(String name) {
switch (name) { switch (name) {
case 'sha-1': case 'sha-1': return Sha1();
return Sha1(); case 'sha-256': return Sha256();
case 'sha-256': case 'sha-512': return Sha512();
return Sha256();
case 'sha-512':
return Sha512();
// NOTE: cryptography provides an implementation of blake2b, however, // NOTE: cryptography provides an implementation of blake2b, however,
// I have no idea what it's output length is and you cannot set // I have no idea what it's output length is and you cannot set
// one. => New dependency // one. => New dependency
// TODO(Unknown): Implement // TODO(Unknown): Implement
//case "blake2b-256": ; //case "blake2b-256": ;
// hashLengthInBytes == 64 => 512? // hashLengthInBytes == 64 => 512?
case 'blake2b-512': case 'blake2b-512': Blake2b();
Blake2b();
// NOTE: cryptography does not provide SHA3 hashes => New dependency // NOTE: cryptography does not provide SHA3 hashes => New dependency
// TODO(Unknown): Implement // TODO(Unknown): Implement
//case "sha3-256": ; //case "sha3-256": ;

View File

@ -15,25 +15,22 @@ class MessageRetractionManager extends XmppManagerBase {
MessageRetractionManager() : super(messageRetractionManager); MessageRetractionManager() : super(messageRetractionManager);
@override @override
List<String> getDiscoFeatures() => [messageRetractionXmlns]; List<String> getDiscoFeatures() => [ messageRetractionXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
callback: _onMessage, callback: _onMessage,
// Before the MessageManager // Before the MessageManager
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final applyTo = message.firstTag('apply-to', xmlns: fasteningXmlns); final applyTo = message.firstTag('apply-to', xmlns: fasteningXmlns);
if (applyTo == null) { if (applyTo == null) {
return state; return state;
@ -44,13 +41,14 @@ class MessageRetractionManager extends XmppManagerBase {
return state; return state;
} }
final isFallbackBody = final isFallbackBody = message.firstTag('fallback', xmlns: fallbackIndicationXmlns) != null;
message.firstTag('fallback', xmlns: fallbackIndicationXmlns) != null;
return state.copyWith( return state.copyWith(
messageRetraction: MessageRetractionData( messageRetraction: MessageRetractionData(
applyTo.attributes['id']! as String, applyTo.attributes['id']! as String,
isFallbackBody ? message.firstTag('body')?.innerText() : null, isFallbackBody ?
message.firstTag('body')?.innerText() :
null,
), ),
); );
} }

View File

@ -32,36 +32,32 @@ class MessageReactionsManager extends XmppManagerBase {
MessageReactionsManager() : super(messageReactionsManager); MessageReactionsManager() : super(messageReactionsManager);
@override @override
List<String> getDiscoFeatures() => [messageReactionsXmlns]; List<String> getDiscoFeatures() => [ messageReactionsXmlns ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'reactions', tagName: 'reactions',
tagXmlns: messageReactionsXmlns, tagXmlns: messageReactionsXmlns,
callback: _onReactionsReceived, callback: _onReactionsReceived,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
), ),
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onReactionsReceived( Future<StanzaHandlerData> _onReactionsReceived(Stanza message, StanzaHandlerData state) async {
Stanza message, final reactionsElement = message.firstTag('reactions', xmlns: messageReactionsXmlns)!;
StanzaHandlerData state,
) async {
final reactionsElement =
message.firstTag('reactions', xmlns: messageReactionsXmlns)!;
return state.copyWith( return state.copyWith(
messageReactions: MessageReactions( messageReactions: MessageReactions(
reactionsElement.attributes['id']! as String, reactionsElement.attributes['id']! as String,
reactionsElement.children reactionsElement.children
.where((c) => c.tag == 'reaction') .where((c) => c.tag == 'reaction')
.map((c) => c.innerText()) .map((c) => c.innerText())
.toList(), .toList(),
), ),
); );
} }

View File

@ -18,18 +18,13 @@ class FileMetadataData {
/// Parse [node] as a FileMetadataData element. /// Parse [node] as a FileMetadataData element.
factory FileMetadataData.fromXML(XMLNode node) { factory FileMetadataData.fromXML(XMLNode node) {
assert( assert(node.attributes['xmlns'] == fileMetadataXmlns, 'Invalid element xmlns');
node.attributes['xmlns'] == fileMetadataXmlns,
'Invalid element xmlns',
);
assert(node.tag == 'file', 'Invalid element anme'); assert(node.tag == 'file', 'Invalid element anme');
final lengthElement = node.firstTag('length'); final lengthElement = node.firstTag('length');
final length = final length = lengthElement != null ? int.parse(lengthElement.innerText()) : null;
lengthElement != null ? int.parse(lengthElement.innerText()) : null;
final sizeElement = node.firstTag('size'); final sizeElement = node.firstTag('size');
final size = final size = sizeElement != null ? int.parse(sizeElement.innerText()) : null;
sizeElement != null ? int.parse(sizeElement.innerText()) : null;
final hashes = <String, String>{}; final hashes = <String, String>{};
for (final e in node.findTags('hash')) { for (final e in node.findTags('hash')) {
@ -56,7 +51,7 @@ class FileMetadataData {
if (heightString != null) { if (heightString != null) {
height = int.parse(heightString.innerText()); height = int.parse(heightString.innerText());
} }
return FileMetadataData( return FileMetadataData(
mediaType: node.firstTag('media-type')?.innerText(), mediaType: node.firstTag('media-type')?.innerText(),
width: width, width: width,
@ -87,27 +82,13 @@ class FileMetadataData {
children: List.empty(growable: true), children: List.empty(growable: true),
); );
if (mediaType != null) { if (mediaType != null) node.addChild(XMLNode(tag: 'media-type', text: mediaType));
node.addChild(XMLNode(tag: 'media-type', text: mediaType)); if (width != null) node.addChild(XMLNode(tag: 'width', text: '$width'));
} if (height != null) node.addChild(XMLNode(tag: 'height', text: '$height'));
if (width != null) { if (desc != null) node.addChild(XMLNode(tag: 'desc', text: desc));
node.addChild(XMLNode(tag: 'width', text: '$width')); if (length != null) node.addChild(XMLNode(tag: 'length', text: length.toString()));
} if (name != null) node.addChild(XMLNode(tag: 'name', text: name));
if (height != null) { if (size != null) node.addChild(XMLNode(tag: 'size', text: size.toString()));
node.addChild(XMLNode(tag: 'height', text: '$height'));
}
if (desc != null) {
node.addChild(XMLNode(tag: 'desc', text: desc));
}
if (length != null) {
node.addChild(XMLNode(tag: 'length', text: length.toString()));
}
if (name != null) {
node.addChild(XMLNode(tag: 'name', text: name));
}
if (size != null) {
node.addChild(XMLNode(tag: 'size', text: size.toString()));
}
for (final hash in hashes.entries) { for (final hash in hashes.entries) {
node.addChild( node.addChild(
@ -120,7 +101,7 @@ class FileMetadataData {
constructFileThumbnailElement(thumbnail), constructFileThumbnailElement(thumbnail),
); );
} }
return node; return node;
} }
} }

View File

@ -21,14 +21,9 @@ class StatelessFileSharingUrlSource extends StatelessFileSharingSource {
StatelessFileSharingUrlSource(this.url); StatelessFileSharingUrlSource(this.url);
factory StatelessFileSharingUrlSource.fromXml(XMLNode element) { factory StatelessFileSharingUrlSource.fromXml(XMLNode element) {
assert( assert(element.attributes['xmlns'] == urlDataXmlns, 'Element has the wrong xmlns');
element.attributes['xmlns'] == urlDataXmlns,
'Element has the wrong xmlns',
);
return StatelessFileSharingUrlSource( return StatelessFileSharingUrlSource(element.attributes['target']! as String);
element.attributes['target']! as String,
);
} }
final String url; final String url;
@ -49,12 +44,9 @@ class StatelessFileSharingUrlSource extends StatelessFileSharingSource {
/// StatelessFileSharingSources contained with it. /// StatelessFileSharingSources contained with it.
/// If [checkXmlns] is true, then the sources element must also have an xmlns attribute /// If [checkXmlns] is true, then the sources element must also have an xmlns attribute
/// of "urn:xmpp:sfs:0". /// of "urn:xmpp:sfs:0".
List<StatelessFileSharingSource> processStatelessFileSharingSources( List<StatelessFileSharingSource> processStatelessFileSharingSources(XMLNode node, { bool checkXmlns = true }) {
XMLNode node, {
bool checkXmlns = true,
}) {
final sources = List<StatelessFileSharingSource>.empty(growable: true); final sources = List<StatelessFileSharingSource>.empty(growable: true);
final sourcesElement = node.firstTag( final sourcesElement = node.firstTag(
'sources', 'sources',
xmlns: checkXmlns ? sfsXmlns : null, xmlns: checkXmlns ? sfsXmlns : null,
@ -96,7 +88,9 @@ class StatelessFileSharingData {
metadata.toXML(), metadata.toXML(),
XMLNode( XMLNode(
tag: 'sources', tag: 'sources',
children: sources.map((source) => source.toXml()).toList(), children: sources
.map((source) => source.toXml())
.toList(),
), ),
], ],
); );
@ -105,8 +99,7 @@ class StatelessFileSharingData {
StatelessFileSharingUrlSource? getFirstUrlSource() { StatelessFileSharingUrlSource? getFirstUrlSource() {
return firstWhereOrNull( return firstWhereOrNull(
sources, sources,
(StatelessFileSharingSource source) => (StatelessFileSharingSource source) => source is StatelessFileSharingUrlSource,
source is StatelessFileSharingUrlSource,
) as StatelessFileSharingUrlSource?; ) as StatelessFileSharingUrlSource?;
} }
} }
@ -116,29 +109,24 @@ class SFSManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'file-sharing', tagName: 'file-sharing',
tagXmlns: sfsXmlns, tagXmlns: sfsXmlns,
callback: _onMessage, callback: _onMessage,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza message, StanzaHandlerData state) async {
Stanza message,
StanzaHandlerData state,
) async {
final sfs = message.firstTag('file-sharing', xmlns: sfsXmlns)!; final sfs = message.firstTag('file-sharing', xmlns: sfsXmlns)!;
return state.copyWith( return state.copyWith(
sfs: StatelessFileSharingData.fromXML( sfs: StatelessFileSharingData.fromXML(sfs, ),
sfs,
),
); );
} }
} }

View File

@ -38,18 +38,10 @@ SFSEncryptionType encryptionTypeFromNamespace(String xmlns) {
} }
class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource { class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
StatelessFileSharingEncryptedSource(
this.encryption, StatelessFileSharingEncryptedSource(this.encryption, this.key, this.iv, this.hashes, this.source);
this.key,
this.iv,
this.hashes,
this.source,
);
factory StatelessFileSharingEncryptedSource.fromXml(XMLNode element) { factory StatelessFileSharingEncryptedSource.fromXml(XMLNode element) {
assert( assert(element.attributes['xmlns'] == sfsEncryptionXmlns, 'Element has invalid xmlns');
element.attributes['xmlns'] == sfsEncryptionXmlns,
'Element has invalid xmlns',
);
final key = base64Decode(element.firstTag('key')!.text!); final key = base64Decode(element.firstTag('key')!.text!);
final iv = base64Decode(element.firstTag('iv')!.text!); final iv = base64Decode(element.firstTag('iv')!.text!);
@ -58,8 +50,7 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
// Find the first URL source // Find the first URL source
final source = firstWhereOrNull( final source = firstWhereOrNull(
sources, sources,
(XMLNode child) => (XMLNode child) => child.tag == 'url-data' && child.attributes['xmlns'] == urlDataXmlns,
child.tag == 'url-data' && child.attributes['xmlns'] == urlDataXmlns,
)!; )!;
// Find hashes // Find hashes
@ -67,7 +58,7 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
for (final hash in element.findTags('hash', xmlns: hashXmlns)) { for (final hash in element.findTags('hash', xmlns: hashXmlns)) {
hashes[hash.attributes['algo']! as String] = hash.text!; hashes[hash.attributes['algo']! as String] = hash.text!;
} }
return StatelessFileSharingEncryptedSource( return StatelessFileSharingEncryptedSource(
encryptionTypeFromNamespace(element.attributes['cipher']! as String), encryptionTypeFromNamespace(element.attributes['cipher']! as String),
key, key,
@ -76,7 +67,7 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
StatelessFileSharingUrlSource.fromXml(source), StatelessFileSharingUrlSource.fromXml(source),
); );
} }
final List<int> key; final List<int> key;
final List<int> iv; final List<int> iv;
final SFSEncryptionType encryption; final SFSEncryptionType encryption;
@ -100,8 +91,7 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
tag: 'iv', tag: 'iv',
text: base64Encode(iv), text: base64Encode(iv),
), ),
...hashes.entries ...hashes.entries.map((hash) => constructHashElement(hash.key, hash.value)),
.map((hash) => constructHashElement(hash.key, hash.value)),
XMLNode.xmlns( XMLNode.xmlns(
tag: 'sources', tag: 'sources',
xmlns: sfsXmlns, xmlns: sfsXmlns,

View File

@ -22,9 +22,7 @@ class Sticker {
assert(node.tag == 'item', 'sticker has wrong tag'); assert(node.tag == 'item', 'sticker has wrong tag');
return Sticker( return Sticker(
FileMetadataData.fromXML( FileMetadataData.fromXML(node.firstTag('file', xmlns: fileMetadataXmlns)!),
node.firstTag('file', xmlns: fileMetadataXmlns)!,
),
processStatelessFileSharingSources(node, checkXmlns: false), processStatelessFileSharingSources(node, checkXmlns: false),
{}, {},
); );
@ -33,7 +31,7 @@ class Sticker {
final FileMetadataData metadata; final FileMetadataData metadata;
final List<StatelessFileSharingSource> sources; final List<StatelessFileSharingSource> sources;
// Language -> suggestion // Language -> suggestion
final Map<String, String> suggests; final Map<String, String> suggests;
XMLNode toPubSubXML() { XMLNode toPubSubXML() {
final suggestsElements = suggests.keys.map((suggest) { final suggestsElements = suggests.keys.map((suggest) {
@ -75,11 +73,7 @@ class StickerPack {
this.restricted, this.restricted,
); );
factory StickerPack.fromXML( factory StickerPack.fromXML(String id, XMLNode node, { bool hashAvailable = true }) {
String id,
XMLNode node, {
bool hashAvailable = true,
}) {
assert(node.tag == 'pack', 'node has wrong tag'); assert(node.tag == 'pack', 'node has wrong tag');
assert(node.attributes['xmlns'] == stickersXmlns, 'node has wrong XMLNS'); assert(node.attributes['xmlns'] == stickersXmlns, 'node has wrong XMLNS');
@ -90,7 +84,7 @@ class StickerPack {
hashAlgorithm = hashFunctionFromName(hash.attributes['algo']! as String); hashAlgorithm = hashFunctionFromName(hash.attributes['algo']! as String);
hashValue = hash.innerText(); hashValue = hash.innerText();
} }
return StickerPack( return StickerPack(
id, id,
node.firstTag('name')!.innerText(), node.firstTag('name')!.innerText(),
@ -98,9 +92,9 @@ class StickerPack {
hashAlgorithm, hashAlgorithm,
hashValue, hashValue,
node.children node.children
.where((e) => e.tag == 'item') .where((e) => e.tag == 'item')
.map<Sticker>(Sticker.fromXML) .map<Sticker>(Sticker.fromXML)
.toList(), .toList(),
node.firstTag('restricted') != null, node.firstTag('restricted') != null,
); );
} }
@ -128,7 +122,7 @@ class StickerPack {
restricted, restricted,
); );
} }
XMLNode toXML() { XMLNode toXML() {
return XMLNode.xmlns( return XMLNode.xmlns(
tag: 'pack', tag: 'pack',
@ -148,10 +142,13 @@ class StickerPack {
hashValue, hashValue,
), ),
...restricted ? [XMLNode(tag: 'restricted')] : [], ...restricted ?
[XMLNode(tag: 'restricted')] :
[],
// Stickers // Stickers
...stickers.map((sticker) => sticker.toPubSubXML()), ...stickers
.map((sticker) => sticker.toPubSubXML()),
], ],
); );
} }
@ -235,19 +232,16 @@ class StickersManager extends XmppManagerBase {
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagXmlns: stickersXmlns, tagXmlns: stickersXmlns,
tagName: 'sticker', tagName: 'sticker',
callback: _onIncomingMessage, callback: _onIncomingMessage,
priority: -99, priority: -99,
), ),
]; ];
Future<StanzaHandlerData> _onIncomingMessage( Future<StanzaHandlerData> _onIncomingMessage(Stanza stanza, StanzaHandlerData state) async {
Stanza stanza,
StanzaHandlerData state,
) async {
final sticker = stanza.firstTag('sticker', xmlns: stickersXmlns)!; final sticker = stanza.firstTag('sticker', xmlns: stickersXmlns)!;
return state.copyWith( return state.copyWith(
stickerPackId: sticker.attributes['pack']! as String, stickerPackId: sticker.attributes['pack']! as String,
@ -258,11 +252,7 @@ class StickersManager extends XmppManagerBase {
/// [accessModel] will be used as the PubSub node's access model. /// [accessModel] will be used as the PubSub node's access model.
/// ///
/// On success, returns true. On failure, returns a PubSubError. /// On success, returns true. On failure, returns a PubSubError.
Future<Result<PubSubError, bool>> publishStickerPack( Future<Result<PubSubError, bool>> publishStickerPack(JID jid, StickerPack pack, { String? accessModel }) async {
JID jid,
StickerPack pack, {
String? accessModel,
}) async {
assert(pack.id != '', 'The sticker pack must have an id'); assert(pack.id != '', 'The sticker pack must have an id');
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
@ -281,10 +271,7 @@ class StickersManager extends XmppManagerBase {
/// Removes the sticker pack with id [id] from the PubSub node of [jid]. /// Removes the sticker pack with id [id] from the PubSub node of [jid].
/// ///
/// On success, returns the true. On failure, returns a PubSubError. /// On success, returns the true. On failure, returns a PubSubError.
Future<Result<PubSubError, bool>> retractStickerPack( Future<Result<PubSubError, bool>> retractStickerPack(JID jid, String id) async {
JID jid,
String id,
) async {
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
return pm.retract( return pm.retract(
@ -297,10 +284,7 @@ class StickersManager extends XmppManagerBase {
/// Fetches the sticker pack with id [id] from [jid]. /// Fetches the sticker pack with id [id] from [jid].
/// ///
/// On success, returns the StickerPack. On failure, returns a PubSubError. /// On success, returns the StickerPack. On failure, returns a PubSubError.
Future<Result<PubSubError, StickerPack>> fetchStickerPack( Future<Result<PubSubError, StickerPack>> fetchStickerPack(JID jid, String id) async {
JID jid,
String id,
) async {
final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!; final pm = getAttributes().getManagerById<PubSubManager>(pubsubManager)!;
final stickerPackDataRaw = await pm.getItem( final stickerPackDataRaw = await pm.getItem(
jid.toBare().toString(), jid.toBare().toString(),

View File

@ -45,14 +45,17 @@ class QuoteData {
/// Takes the body of the message we want to quote [quoteBody] and the content of /// 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. /// the reply [body] and computes the fallback body and its length.
factory QuoteData.fromBodies(String quoteBody, String body) { factory QuoteData.fromBodies(String quoteBody, String body) {
final fallback = quoteBody.split('\n').map((line) => '> $line\n').join(); final fallback = quoteBody
.split('\n')
.map((line) => '> $line\n')
.join();
return QuoteData( return QuoteData(
'$fallback$body', '$fallback$body',
fallback.length, fallback.length,
); );
} }
/// The new body with fallback data at the beginning /// The new body with fallback data at the beginning
final String body; final String body;
@ -67,28 +70,25 @@ class MessageRepliesManager extends XmppManagerBase {
@override @override
List<String> getDiscoFeatures() => [ List<String> getDiscoFeatures() => [
replyXmlns, replyXmlns,
]; ];
@override @override
List<StanzaHandler> getIncomingStanzaHandlers() => [ List<StanzaHandler> getIncomingStanzaHandlers() => [
StanzaHandler( StanzaHandler(
stanzaTag: 'message', stanzaTag: 'message',
tagName: 'reply', tagName: 'reply',
tagXmlns: replyXmlns, tagXmlns: replyXmlns,
callback: _onMessage, callback: _onMessage,
// Before the message handler // Before the message handler
priority: -99, priority: -99,
) )
]; ];
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
Future<StanzaHandlerData> _onMessage( Future<StanzaHandlerData> _onMessage(Stanza stanza, StanzaHandlerData state) async {
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?;

View File

@ -1,172 +0,0 @@
import 'package:moxxmpp/moxxmpp.dart';
import 'package:test/test.dart';
import '../helpers/logging.dart';
import '../helpers/xmpp.dart';
class StubbedDiscoManager extends DiscoManager {
StubbedDiscoManager() : super([]);
@override
Future<Result<DiscoError, DiscoInfo>> discoInfoQuery(String entity, { String? node, bool shouldEncrypt = true }) async {
final result = DiscoInfo.fromQuery(
XMLNode.fromString(
'''<query xmlns='http://jabber.org/protocol/disco#info'>
<identity category='account' type='registered'/>
<identity type='service' category='pubsub' name='PubSub acs-clustered'/>
<feature var='http://jabber.org/protocol/pubsub#retrieve-default'/>
<feature var='http://jabber.org/protocol/pubsub#purge-nodes'/>
<feature var='http://jabber.org/protocol/pubsub#subscribe'/>
<feature var='http://jabber.org/protocol/pubsub#member-affiliation'/>
<feature var='http://jabber.org/protocol/pubsub#subscription-notifications'/>
<feature var='http://jabber.org/protocol/pubsub#create-nodes'/>
<feature var='http://jabber.org/protocol/pubsub#outcast-affiliation'/>
<feature var='http://jabber.org/protocol/pubsub#get-pending'/>
<feature var='http://jabber.org/protocol/pubsub#presence-notifications'/>
<feature var='urn:xmpp:ping'/>
<feature var='http://jabber.org/protocol/pubsub#delete-nodes'/>
<feature var='http://jabber.org/protocol/pubsub#config-node'/>
<feature var='http://jabber.org/protocol/pubsub#retrieve-items'/>
<feature var='http://jabber.org/protocol/pubsub#access-whitelist'/>
<feature var='http://jabber.org/protocol/pubsub#access-presence'/>
<feature var='http://jabber.org/protocol/disco#items'/>
<feature var='http://jabber.org/protocol/pubsub#meta-data'/>
<feature var='http://jabber.org/protocol/pubsub#multi-items'/>
<feature var='http://jabber.org/protocol/pubsub#item-ids'/>
<feature var='urn:xmpp:mam:1'/>
<feature var='http://jabber.org/protocol/pubsub#instant-nodes'/>
<feature var='urn:xmpp:mam:2'/>
<feature var='urn:xmpp:mam:2#extended'/>
<feature var='http://jabber.org/protocol/pubsub#modify-affiliations'/>
<feature var='http://jabber.org/protocol/pubsub#multi-collection'/>
<feature var='http://jabber.org/protocol/pubsub#persistent-items'/>
<feature var='http://jabber.org/protocol/pubsub#create-and-configure'/>
<feature var='http://jabber.org/protocol/pubsub#publisher-affiliation'/>
<feature var='http://jabber.org/protocol/pubsub#access-open'/>
<feature var='http://jabber.org/protocol/pubsub#retrieve-affiliations'/>
<feature var='http://jabber.org/protocol/pubsub#access-authorize'/>
<feature var='jabber:iq:version'/>
<feature var='http://jabber.org/protocol/pubsub#retract-items'/>
<feature var='http://jabber.org/protocol/pubsub#manage-subscriptions'/>
<feature var='http://jabber.org/protocol/commands'/>
<feature var='http://jabber.org/protocol/pubsub#auto-subscribe'/>
<feature var='http://jabber.org/protocol/pubsub#publish-options'/>
<feature var='http://jabber.org/protocol/pubsub#access-roster'/>
<feature var='http://jabber.org/protocol/pubsub#publish'/>
<feature var='http://jabber.org/protocol/pubsub#collections'/>
<feature var='http://jabber.org/protocol/pubsub#retrieve-subscriptions'/>
<feature var='http://jabber.org/protocol/disco#info'/>
<x type='result' xmlns='jabber:x:data'>
<field type='hidden' var='FORM_TYPE'>
<value>http://jabber.org/network/serverinfo</value>
</field>
<field type='list-multi' var='abuse-addresses'>
<value>mailto:support@tigase.net</value>
<value>xmpp:tigase@mix.tigase.im</value>
<value>xmpp:tigase@muc.tigase.org</value>
<value>https://tigase.net/technical-support</value>
</field>
</x>
<feature var='http://jabber.org/protocol/pubsub#auto-create'/>
<feature var='http://jabber.org/protocol/pubsub#auto-subscribe'/>
<feature var='urn:xmpp:mix:pam:2'/>
<feature var='urn:xmpp:carbons:2'/>
<feature var='urn:xmpp:carbons:rules:0'/>
<feature var='jabber:iq:auth'/>
<feature var='vcard-temp'/>
<feature var='http://jabber.org/protocol/amp'/>
<feature var='msgoffline'/>
<feature var='http://jabber.org/protocol/disco#info'/>
<feature var='http://jabber.org/protocol/disco#items'/>
<feature var='urn:xmpp:blocking'/>
<feature var='urn:xmpp:reporting:0'/>
<feature var='urn:xmpp:reporting:abuse:0'/>
<feature var='urn:xmpp:reporting:spam:0'/>
<feature var='urn:xmpp:reporting:1'/>
<feature var='urn:xmpp:ping'/>
<feature var='urn:ietf:params:xml:ns:xmpp-sasl'/>
<feature var='http://jabber.org/protocol/pubsub'/>
<feature var='http://jabber.org/protocol/pubsub#owner'/>
<feature var='http://jabber.org/protocol/pubsub#publish'/>
<identity type='pep' category='pubsub'/>
<feature var='urn:xmpp:pep-vcard-conversion:0'/>
<feature var='urn:xmpp:bookmarks-conversion:0'/>
<feature var='urn:xmpp:archive:auto'/>
<feature var='urn:xmpp:archive:manage'/>
<feature var='urn:xmpp:push:0'/>
<feature var='tigase:push:away:0'/>
<feature var='tigase:push:encrypt:0'/>
<feature var='tigase:push:encrypt:aes-128-gcm'/>
<feature var='tigase:push:filter:ignore-unknown:0'/>
<feature var='tigase:push:filter:groupchat:0'/>
<feature var='tigase:push:filter:muted:0'/>
<feature var='tigase:push:priority:0'/>
<feature var='tigase:push:jingle:0'/>
<feature var='jabber:iq:roster'/>
<feature var='jabber:iq:roster-dynamic'/>
<feature var='urn:xmpp:mam:1'/>
<feature var='urn:xmpp:mam:2'/>
<feature var='urn:xmpp:mam:2#extended'/>
<feature var='urn:xmpp:mix:pam:2#archive'/>
<feature var='jabber:iq:version'/>
<feature var='urn:xmpp:time'/>
<feature var='jabber:iq:privacy'/>
<feature var='urn:ietf:params:xml:ns:xmpp-bind'/>
<feature var='urn:xmpp:extdisco:2'/>
<feature var='http://jabber.org/protocol/commands'/>
<feature var='urn:ietf:params:xml:ns:vcard-4.0'/>
<feature var='jabber:iq:private'/>
<feature var='urn:ietf:params:xml:ns:xmpp-session'/>
</query>'''
),
JID.fromString('pubsub.server.example.org'),
);
return Result(result);
}
}
T? getDiscoManagerStub<T extends XmppManagerBase>(String id) {
return StubbedDiscoManager() as T;
}
void main() {
initLogger();
test('Test publishing with pubsub#max_items when the server does not support it', () async {
XMLNode? sent;
final manager = PubSubManager();
manager.register(
XmppManagerAttributes(
sendStanza: (stanza, { StanzaFromType addFrom = StanzaFromType.full, bool addId = true, bool awaitable = true, bool encrypted = false, bool forceEncryption = false, }) async {
sent = stanza;
return XMLNode.fromString('<iq />');
},
sendNonza: (_) {},
sendEvent: (_) {},
getManagerById: getDiscoManagerStub,
getConnectionSettings: () => ConnectionSettings(
jid: JID.fromString('hallo@example.server'),
password: 'password',
useDirectTLS: true,
allowPlainAuth: false,
),
isFeatureSupported: (_) => false,
getFullJID: () => JID.fromString('hallo@example.server/uwu'),
getSocket: () => StubTCPSocket(play: []),
getConnection: () => XmppConnection(TestingReconnectionPolicy(), AlwaysConnectedConnectivityManager(), StubTCPSocket(play: [])),
getNegotiatorById: getNegotiatorNullStub,
),
);
final result = await manager.preprocessPublishOptions(
'pubsub.server.example.org',
'example:node',
PubSubPublishOptions(
maxItems: 'max',
),
);
});
}

View File

@ -22,8 +22,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
final StreamController<String> _dataStream = StreamController.broadcast(); final StreamController<String> _dataStream = StreamController.broadcast();
/// The stream of outgoing (TCPSocketWrapper -> XmppConnection) events. /// The stream of outgoing (TCPSocketWrapper -> XmppConnection) events.
final StreamController<XmppSocketEvent> _eventStream = final StreamController<XmppSocketEvent> _eventStream = StreamController.broadcast();
StreamController.broadcast();
/// A subscription on the socket's data stream. /// A subscription on the socket's data stream.
StreamSubscription<dynamic>? _socketSubscription; StreamSubscription<dynamic>? _socketSubscription;
@ -69,7 +68,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
bool onBadCertificate(dynamic certificate, String domain) { bool onBadCertificate(dynamic certificate, String domain) {
return false; return false;
} }
Future<bool> _xep368Connect(String domain) async { Future<bool> _xep368Connect(String domain) async {
// TODO(Unknown): Maybe do DNSSEC one day // TODO(Unknown): Maybe do DNSSEC one day
final results = await srvQuery('_xmpps-client._tcp.$domain', false); final results = await srvQuery('_xmpps-client._tcp.$domain', false);
@ -81,9 +80,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
results.sort(srvRecordSortComparator); results.sort(srvRecordSortComparator);
for (final srv in results) { for (final srv in results) {
try { try {
_log.finest( _log.finest('Attempting secure connection to ${srv.target}:${srv.port}...');
'Attempting secure connection to ${srv.target}:${srv.port}...',
);
// 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
@ -96,14 +93,14 @@ class TCPSocketWrapper extends BaseSocketWrapper {
_socket = await SecureSocket.secure( _socket = await SecureSocket.secure(
sock, sock,
host: domain, host: domain,
supportedProtocols: const [xmppClientALPNId], supportedProtocols: const [ xmppClientALPNId ],
onBadCertificate: (cert) => onBadCertificate(cert, domain), onBadCertificate: (cert) => onBadCertificate(cert, domain),
); );
_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');
if (e is HandshakeException) { if (e is HandshakeException) {
@ -115,10 +112,10 @@ class TCPSocketWrapper extends BaseSocketWrapper {
if (failedDueToTLS) { if (failedDueToTLS) {
_eventStream.add(XmppSocketTLSFailedEvent()); _eventStream.add(XmppSocketTLSFailedEvent());
} }
return false; return false;
} }
Future<bool> _rfc6120Connect(String domain) async { Future<bool> _rfc6120Connect(String domain) async {
// TODO(Unknown): Maybe do DNSSEC one day // TODO(Unknown): Maybe do DNSSEC one day
final results = await srvQuery('_xmpp-client._tcp.$domain', false); final results = await srvQuery('_xmpp-client._tcp.$domain', false);
@ -135,7 +132,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
_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');
continue; continue;
} }
@ -157,7 +154,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
); );
_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');
return false; return false;
} }
@ -183,14 +180,14 @@ 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;
} }
try { try {
// The socket is closed during the entire process // The socket is closed during the entire process
_expectSocketClosure = true; _expectSocketClosure = true;
_socket = await SecureSocket.secure( _socket = await SecureSocket.secure(
_socket!, _socket!,
supportedProtocols: const [xmppClientALPNId], supportedProtocols: const [ xmppClientALPNId ],
onBadCertificate: (cert) => onBadCertificate(cert, domain), onBadCertificate: (cert) => onBadCertificate(cert, domain),
); );
@ -207,7 +204,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
return false; return false;
} }
} }
void _setupStreams() { void _setupStreams() {
if (_socket == null) { if (_socket == null) {
_log.severe('Failed to setup streams as _socket is null'); _log.severe('Failed to setup streams as _socket is null');
@ -233,9 +230,9 @@ class TCPSocketWrapper extends BaseSocketWrapper {
_expectSocketClosure = false; _expectSocketClosure = false;
}); });
} }
@override @override
Future<bool> connect(String domain, {String? host, int? port}) async { Future<bool> connect(String domain, { String? host, int? port }) async {
_expectSocketClosure = false; _expectSocketClosure = false;
_secure = false; _secure = false;
@ -283,7 +280,7 @@ class TCPSocketWrapper extends BaseSocketWrapper {
try { try {
_socket!.close(); _socket!.close();
} catch (e) { } catch(e) {
_log.warning('Closing socket threw exception: $e'); _log.warning('Closing socket threw exception: $e');
} }
} }
@ -292,11 +289,10 @@ class TCPSocketWrapper extends BaseSocketWrapper {
Stream<String> getDataStream() => _dataStream.stream.asBroadcastStream(); Stream<String> getDataStream() => _dataStream.stream.asBroadcastStream();
@override @override
Stream<XmppSocketEvent> getEventStream() => Stream<XmppSocketEvent> getEventStream() => _eventStream.stream.asBroadcastStream();
_eventStream.stream.asBroadcastStream();
@override @override
void write(Object? data, {String? redact}) { void write(Object? data, { String? redact }) {
if (_socket == null) { if (_socket == null) {
_log.severe('Failed to write to socket as _socket is null'); _log.severe('Failed to write to socket as _socket is null');
return; return;