fix: Somewhat fix OMEMO
This commit is contained in:
		
							parent
							
								
									678564dbb3
								
							
						
					
					
						commit
						d8de093e4d
					
				| @ -94,7 +94,7 @@ class XmppConnection { | ||||
|     _awaitingResponseLock = Lock(), | ||||
|     _xmppManagers = {}, | ||||
|     _incomingStanzaHandlers = List.empty(growable: true), | ||||
|     _incomingEncryptionStanzaHandlers = List.empty(growable: true), | ||||
|     _incomingPreStanzaHandlers = List.empty(growable: true), | ||||
|     _outgoingPreStanzaHandlers = List.empty(growable: true), | ||||
|     _outgoingPostStanzaHandlers = List.empty(growable: true), | ||||
|     _reconnectionPolicy = reconnectionPolicy, | ||||
| @ -134,7 +134,7 @@ class XmppConnection { | ||||
|   /// Helpers | ||||
|   /// | ||||
|   final List<StanzaHandler> _incomingStanzaHandlers; | ||||
|   final List<StanzaHandler> _incomingEncryptionStanzaHandlers; | ||||
|   final List<StanzaHandler> _incomingPreStanzaHandlers; | ||||
|   final List<StanzaHandler> _outgoingPreStanzaHandlers; | ||||
|   final List<StanzaHandler> _outgoingPostStanzaHandlers; | ||||
|   final StreamController<XmppEvent> _eventStreamController; | ||||
| @ -225,13 +225,13 @@ class XmppConnection { | ||||
|     } | ||||
| 
 | ||||
|     _incomingStanzaHandlers.addAll(manager.getIncomingStanzaHandlers()); | ||||
|     _incomingEncryptionStanzaHandlers.addAll(manager.getIncomingEncryptionStanzaHandlers()); | ||||
|     _incomingPreStanzaHandlers.addAll(manager.getIncomingPreStanzaHandlers()); | ||||
|     _outgoingPreStanzaHandlers.addAll(manager.getOutgoingPreStanzaHandlers()); | ||||
|     _outgoingPostStanzaHandlers.addAll(manager.getOutgoingPostStanzaHandlers()); | ||||
|      | ||||
|     if (sortHandlers) { | ||||
|       _incomingStanzaHandlers.sort(stanzaHandlerSortComparator); | ||||
|       _incomingEncryptionStanzaHandlers.sort(stanzaHandlerSortComparator); | ||||
|       _incomingPreStanzaHandlers.sort(stanzaHandlerSortComparator); | ||||
|       _outgoingPreStanzaHandlers.sort(stanzaHandlerSortComparator); | ||||
|       _outgoingPostStanzaHandlers.sort(stanzaHandlerSortComparator); | ||||
|     } | ||||
| @ -634,12 +634,12 @@ class XmppConnection { | ||||
|     return state; | ||||
|   } | ||||
| 
 | ||||
|   Future<StanzaHandlerData> _runIncomingStanzaHandlers(Stanza stanza) async { | ||||
|     return _runStanzaHandlers(_incomingStanzaHandlers, stanza); | ||||
|   Future<StanzaHandlerData> _runIncomingStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async { | ||||
|     return _runStanzaHandlers(_incomingStanzaHandlers, stanza, initial: initial); | ||||
|   } | ||||
| 
 | ||||
|   Future<StanzaHandlerData> _runIncomingEncryptionStanzaHandlers(Stanza stanza) async { | ||||
|     return _runStanzaHandlers(_incomingEncryptionStanzaHandlers, stanza); | ||||
|   Future<StanzaHandlerData> _runIncomingPreStanzaHandlers(Stanza stanza) async { | ||||
|     return _runStanzaHandlers(_incomingPreStanzaHandlers, stanza); | ||||
|   } | ||||
| 
 | ||||
|   Future<StanzaHandlerData> _runOutgoingPreStanzaHandlers(Stanza stanza, { StanzaHandlerData? initial }) async { | ||||
| @ -681,18 +681,18 @@ class XmppConnection { | ||||
| 
 | ||||
|     // Run the incoming stanza handlers and bounce with an error if no manager handled | ||||
|     // it. | ||||
|     final incomingEncryptionHandlers = await _runIncomingEncryptionStanzaHandlers(stanza); | ||||
|     final prefix = incomingEncryptionHandlers.encrypted ? | ||||
|     final incomingPreHandlers = await _runIncomingPreStanzaHandlers(stanza); | ||||
|     final prefix = incomingPreHandlers.encrypted && incomingPreHandlers.other['encryption_error'] == null ? | ||||
|       '(Encrypted) ' : | ||||
|       ''; | ||||
|     _log.finest('<== $prefix${incomingEncryptionHandlers.stanza.toXml()}'); | ||||
|     _log.finest('<== $prefix${incomingPreHandlers.stanza.toXml()}'); | ||||
| 
 | ||||
|     // See if we are waiting for this stanza | ||||
|     final id = incomingEncryptionHandlers.stanza.attributes['id'] as String?; | ||||
|     final id = incomingPreHandlers.stanza.attributes['id'] as String?; | ||||
|     var awaited = false; | ||||
|     await _awaitingResponseLock.synchronized(() async { | ||||
|       if (id != null && _awaitingResponse.containsKey(id)) { | ||||
|         _awaitingResponse[id]!.complete(incomingEncryptionHandlers.stanza); | ||||
|         _awaitingResponse[id]!.complete(incomingPreHandlers.stanza); | ||||
|         _awaitingResponse.remove(id); | ||||
|         awaited = true; | ||||
|       } | ||||
| @ -703,9 +703,18 @@ class XmppConnection { | ||||
|     } | ||||
| 
 | ||||
|     // Only bounce if the stanza has neither been awaited, nor handled. | ||||
|     final incomingHandlers = await _runIncomingStanzaHandlers(incomingEncryptionHandlers.stanza); | ||||
|     final incomingHandlers = await _runIncomingStanzaHandlers( | ||||
|       incomingPreHandlers.stanza, | ||||
|       initial: StanzaHandlerData( | ||||
|         false, | ||||
|         incomingPreHandlers.cancel, | ||||
|         incomingPreHandlers.cancelReason, | ||||
|         incomingPreHandlers.stanza, | ||||
|         other: incomingPreHandlers.other, | ||||
|       ), | ||||
|     ); | ||||
|     if (!incomingHandlers.done) { | ||||
|       handleUnhandledStanza(this, stanza); | ||||
|       handleUnhandledStanza(this, incomingPreHandlers.stanza); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -32,9 +32,10 @@ abstract class XmppManagerBase { | ||||
|   /// receive. | ||||
|   List<StanzaHandler> getIncomingStanzaHandlers() => []; | ||||
| 
 | ||||
|   /// Return the StanzaHandlers associated with this manager that deal with stanzas we | ||||
|   /// may decrypt. | ||||
|   List<StanzaHandler> getIncomingEncryptionStanzaHandlers() => []; | ||||
|   /// Return the StanzaHandlers associated with this manager that deal with stanza handlers | ||||
|   /// that have to run before the main ones run. This is useful, for example, for OMEMO | ||||
|   /// as we have to decrypt the stanza before we do anything else. | ||||
|   List<StanzaHandler> getIncomingPreStanzaHandlers() => []; | ||||
|    | ||||
|   /// Return the NonzaHandlers associated with this manager. | ||||
|   List<NonzaHandler> getNonzaHandlers() => []; | ||||
|  | ||||
| @ -25,13 +25,12 @@ class CarbonsManager extends XmppManagerBase { | ||||
|   String getName() => 'CarbonsManager'; | ||||
| 
 | ||||
|   @override | ||||
|   List<StanzaHandler> getIncomingStanzaHandlers() => [ | ||||
|   List<StanzaHandler> getIncomingPreStanzaHandlers() => [ | ||||
|     StanzaHandler( | ||||
|       stanzaTag: 'message', | ||||
|       tagName: 'received', | ||||
|       tagXmlns: carbonsXmlns, | ||||
|       callback: _onMessageReceived, | ||||
|       // Before all managers the message manager depends on | ||||
|       priority: -98, | ||||
|     ), | ||||
|     StanzaHandler( | ||||
| @ -39,7 +38,6 @@ class CarbonsManager extends XmppManagerBase { | ||||
|       tagName: 'sent', | ||||
|       tagXmlns: carbonsXmlns, | ||||
|       callback: _onMessageSent, | ||||
|       // Before all managers the message manager depends on | ||||
|       priority: -98, | ||||
|     ) | ||||
|   ]; | ||||
|  | ||||
| @ -7,3 +7,5 @@ class InvalidAffixElementsException with Exception {} | ||||
| class OmemoNotSupportedForContactException extends OmemoError {} | ||||
| 
 | ||||
| class EncryptionFailedException with Exception {} | ||||
| 
 | ||||
| class InvalidEnvelopePayloadException with Exception {} | ||||
|  | ||||
| @ -24,6 +24,7 @@ import 'package:moxxmpp/src/xeps/xep_0384/errors.dart'; | ||||
| import 'package:moxxmpp/src/xeps/xep_0384/helpers.dart'; | ||||
| import 'package:moxxmpp/src/xeps/xep_0384/types.dart'; | ||||
| import 'package:omemo_dart/omemo_dart.dart'; | ||||
| import 'package:xml/xml.dart'; | ||||
| 
 | ||||
| const _doNotEncryptList = [ | ||||
|   // XEP-0033 | ||||
| @ -53,27 +54,24 @@ abstract class BaseOmemoManager extends XmppManagerBase { | ||||
|   Future<bool> isSupported() async => true; | ||||
| 
 | ||||
|   @override | ||||
|   List<StanzaHandler> getIncomingEncryptionStanzaHandlers() => [ | ||||
|   List<StanzaHandler> getIncomingPreStanzaHandlers() => [ | ||||
|     StanzaHandler( | ||||
|       stanzaTag: 'iq', | ||||
|       tagXmlns: omemoXmlns, | ||||
|       tagName: 'encrypted', | ||||
|       callback: _onIncomingStanza, | ||||
|       priority: 9999, | ||||
|     ), | ||||
|     StanzaHandler( | ||||
|       stanzaTag: 'presence', | ||||
|       tagXmlns: omemoXmlns, | ||||
|       tagName: 'encrypted', | ||||
|       callback: _onIncomingStanza, | ||||
|       priority: 9999, | ||||
|     ), | ||||
|     StanzaHandler( | ||||
|       stanzaTag: 'message', | ||||
|       tagXmlns: omemoXmlns, | ||||
|       tagName: 'encrypted', | ||||
|       callback: _onIncomingStanza, | ||||
|       priority: -98, | ||||
|     ), | ||||
|   ]; | ||||
| 
 | ||||
| @ -432,16 +430,29 @@ abstract class BaseOmemoManager extends XmppManagerBase { | ||||
|       ), | ||||
|     ); | ||||
| 
 | ||||
|     final children = stanza.children.where( | ||||
|       (child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns, | ||||
|     ).toList(); | ||||
|     final other = Map<String, dynamic>.from(state.other); | ||||
|     var children = stanza.children; | ||||
|     if (result.error != null) { | ||||
|       other['encryption_error'] = result.error; | ||||
|     } else { | ||||
|       children = stanza.children.where( | ||||
|         (child) => child.tag != 'encrypted' || child.attributes['xmlns'] != omemoXmlns, | ||||
|       ).toList(); | ||||
|     } | ||||
| 
 | ||||
|     if (result.payload != null) { | ||||
|       final envelope = XMLNode.fromString(result.payload!); | ||||
|       XMLNode envelope; | ||||
|       try { | ||||
|         envelope = XMLNode.fromString(result.payload!); | ||||
|       } on XmlParserException catch (_) { | ||||
|         logger.warning('Failed to parse envelope payload: ${result.payload!}'); | ||||
|         other['encryption_error'] = InvalidEnvelopePayloadException(); | ||||
|         return state.copyWith( | ||||
|           encrypted: true, | ||||
|           other: other, | ||||
|         ); | ||||
|       } | ||||
| 
 | ||||
|       children.addAll( | ||||
|         envelope.firstTag('content')!.children, | ||||
|       ); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user