diff --git a/packages/moxxmpp/lib/src/connection.dart b/packages/moxxmpp/lib/src/connection.dart index be9af39..c6dc399 100644 --- a/packages/moxxmpp/lib/src/connection.dart +++ b/packages/moxxmpp/lib/src/connection.dart @@ -94,6 +94,7 @@ class XmppConnection { _awaitingResponseLock = Lock(), _xmppManagers = {}, _incomingStanzaHandlers = List.empty(growable: true), + _incomingEncryptionStanzaHandlers = List.empty(growable: true), _outgoingPreStanzaHandlers = List.empty(growable: true), _outgoingPostStanzaHandlers = List.empty(growable: true), _reconnectionPolicy = reconnectionPolicy, @@ -133,6 +134,7 @@ class XmppConnection { /// Helpers /// final List _incomingStanzaHandlers; + final List _incomingEncryptionStanzaHandlers; final List _outgoingPreStanzaHandlers; final List _outgoingPostStanzaHandlers; final StreamController _eventStreamController; @@ -223,11 +225,13 @@ class XmppConnection { } _incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers()); + _incomingEncryptionStanzaHandlers.addAll(manager.getIncomingEncryptionStanzaHandlers()); _outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers()); _outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers()); if (sortHandlers) { _incomingStanzaHandlers.sort(stanzaHandlerSortComparator); + _incomingEncryptionStanzaHandlers.sort(stanzaHandlerSortComparator); _outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator); _outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator); } @@ -634,6 +638,10 @@ class XmppConnection { return _runStanzaHandlers(_incomingStanzaHandlers, stanza); } + Future _runIncomingEncryptionStanzaHandlers(Stanza stanza) async { + return _runStanzaHandlers(_incomingEncryptionStanzaHandlers, stanza); + } + Future _runOutgoingPreStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async { return _runStanzaHandlers(_outgoingPreStanzaHandlers, stanza, initial: initial); } @@ -673,18 +681,18 @@ class XmppConnection { // Run the incoming stanza handlers and bounce with an error if no manager handled // it. - final incomingHandlers = await _runIncomingStanzaHandlers(stanza); - final prefix = incomingHandlers.encrypted ? + final incomingEncryptionHandlers = await _runIncomingEncryptionStanzaHandlers(stanza); + final prefix = incomingEncryptionHandlers.encrypted ? '(Encrypted) ' : ''; - _log.finest('<== $prefix${incomingHandlers.stanza.toXml()}'); + _log.finest('<== $prefix${incomingEncryptionHandlers.stanza.toXml()}'); // See if we are waiting for this stanza - final id = stanza.attributes['id'] as String?; + final id = incomingEncryptionHandlers.stanza.attributes['id'] as String?; var awaited = false; await _awaitingResponseLock.synchronized(() async { if (id != null && _awaitingResponse.containsKey(id)) { - _awaitingResponse[id]!.complete(incomingHandlers.stanza); + _awaitingResponse[id]!.complete(incomingEncryptionHandlers.stanza); _awaitingResponse.remove(id); awaited = true; } @@ -695,6 +703,7 @@ class XmppConnection { } // Only bounce if the stanza has neither been awaited, nor handled. + final incomingHandlers = await _runIncomingStanzaHandlers(incomingEncryptionHandlers.stanza); if (!incomingHandlers.done) { handleUnhandledStanza(this, stanza); } diff --git a/packages/moxxmpp/lib/src/managers/base.dart b/packages/moxxmpp/lib/src/managers/base.dart index 092cbe1..35e173c 100644 --- a/packages/moxxmpp/lib/src/managers/base.dart +++ b/packages/moxxmpp/lib/src/managers/base.dart @@ -31,6 +31,10 @@ abstract class XmppManagerBase { /// Return the StanzaHandlers associated with this manager that deal with stanzas we /// receive. List getIncomingStanzaHandlers() => []; + + /// Return the StanzaHandlers associated with this manager that deal with stanzas we + /// may decrypt. + List getIncomingEncryptionStanzaHandlers() => []; /// Return the NonzaHandlers associated with this manager. List getNonzaHandlers() => []; diff --git a/packages/moxxmpp/lib/src/roster/roster.dart b/packages/moxxmpp/lib/src/roster/roster.dart index 38e542b..cc945be 100644 --- a/packages/moxxmpp/lib/src/roster/roster.dart +++ b/packages/moxxmpp/lib/src/roster/roster.dart @@ -139,6 +139,7 @@ class RosterManager extends XmppManagerBase { } final query = stanza.firstTag('query', xmlns: rosterXmlns)!; + logger.fine('Roster push: ${query.toXml()}'); final item = query.firstTag('item'); if (item == null) { @@ -199,28 +200,34 @@ class RosterManager extends XmppManagerBase { return Result(result); } - /// Requests the roster following RFC 6121 without using roster versioning. + /// Requests the roster following RFC 6121. Future> requestRoster() async { final attrs = getAttributes(); + final query = XMLNode.xmlns( + tag: 'query', + xmlns: rosterXmlns, + ); + final rosterVersion = await _stateManager.getRosterVersion(); + if (rosterVersion != null && rosterVersioningAvailable()) { + query.attributes['ver'] = rosterVersion; + } + final response = await attrs.sendStanza( Stanza.iq( type: 'get', children: [ - XMLNode.xmlns( - tag: 'query', - xmlns: rosterXmlns, - ) + query, ], ), ); if (response.attributes['type'] != 'result') { - logger.warning('Error requesting roster without roster versioning: ${response.toXml()}'); + logger.warning('Error requesting roster: ${response.toXml()}'); return Result(UnknownError()); } - final query = response.firstTag('query', xmlns: rosterXmlns); - return _handleRosterResponse(query); + final responseQuery = response.firstTag('query', xmlns: rosterXmlns); + return _handleRosterResponse(responseQuery); } /// Requests a series of roster pushes according to RFC6121. Requires that the server diff --git a/packages/moxxmpp/lib/src/roster/state.dart b/packages/moxxmpp/lib/src/roster/state.dart index 95db7ad..edf8046 100644 --- a/packages/moxxmpp/lib/src/roster/state.dart +++ b/packages/moxxmpp/lib/src/roster/state.dart @@ -173,7 +173,7 @@ abstract class BaseRosterStateManager { final added = List.empty(growable: true); await _loadRosterCache(); - + _currentVersion = result.ver; for (final item in result.items) { final result = _handleRosterItem(item); diff --git a/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart b/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart index ff5b35f..c72f093 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart @@ -53,7 +53,7 @@ abstract class BaseOmemoManager extends XmppManagerBase { Future isSupported() async => true; @override - List getIncomingStanzaHandlers() => [ + List getIncomingEncryptionStanzaHandlers() => [ StanzaHandler( stanzaTag: 'iq', tagXmlns: omemoXmlns, diff --git a/packages/moxxmpp/pubspec.yaml b/packages/moxxmpp/pubspec.yaml index c8fe584..54f2142 100644 --- a/packages/moxxmpp/pubspec.yaml +++ b/packages/moxxmpp/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=2.17.5 <3.0.0' dependencies: - collection: ^1.17.0 + collection: ^1.16.0 cryptography: ^2.0.5 freezed: ^2.1.0+1 freezed_annotation: ^2.1.0