private void CLIENT_NewDiscoResponseMessage(XMPPClient client, XMPP_API.Classes.Network.Events.NewDiscoResponseMessageEventArgs args) { Task.Run(async() => { string from = args.DISCO.getFrom(); // Only store direct server results: if (from != null && !from.Contains("@") && messageIdCache.getTimed(args.DISCO.ID) != null) { switch (args.DISCO.DISCO_TYPE) { case DiscoType.ITEMS: await addItemsAsync(args.DISCO.ITEMS, from, client, true); break; case DiscoType.INFO: addFeatures(args.DISCO.FEATURES, from); addIdentities(args.DISCO.IDENTITIES, from); break; default: throw new InvalidOperationException("Unexpected value for DISCO_TYPE: " + args.DISCO.DISCO_TYPE); } } }); }
/// <summary> /// Parses the given messages and invokes the ConnectionNewValidMessage event for each message. /// </summary> /// <param name="data">The messages string to parse.</param> private async Task parseMessageAsync(string data) { // Parse message: List <AbstractMessage> messages = null; try { messages = PARSER.parseMessages(ref data); } catch (Exception e) { Logger.Error("[XMPPConnection2]: Error during message parsing." + e); return; } // Process messages: foreach (AbstractMessage msg in messages) { // Stream error messages: if (msg is StreamErrorMessage errorMessage) { Logger.Warn("[XMPPConnection2]: Received stream error message: " + errorMessage.ToString()); lastConnectionError = new ConnectionError(ConnectionErrorCode.SERVER_ERROR, errorMessage.ToString()); await onConnectionErrorAsync(); await sendStreamCloseMessageAsync(); return; } // Filter IQ messages which ids are not valid: if (msg is IQMessage iq) { if (iq.GetType().Equals(IQMessage.RESULT) && messageIdCache.getTimed(iq.ID) != null) { Logger.Warn("[XMPPConnection2]: Invalid message id received!"); return; } } // Invoke message processors: NewValidMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); // Should restart connection? if (msg.getRestartConnection() != AbstractMessage.NO_RESTART) { if (msg.getRestartConnection() == AbstractMessage.SOFT_RESTART) { await softRestartAsync(); } else if (msg.getRestartConnection() == AbstractMessage.HARD_RESTART) { await hardRestartAsync(); } else { throw new ArgumentException("[XMPPConnection2]: Invalid restart type: " + msg.getRestartConnection()); } } // Filter already processed messages: if (msg.isProcessed()) { return; } // -------------------------------------------------------------------- // Open stream: if (msg is OpenStreamAnswerMessage oA) { if (oA.ID is null) { // TODO Handle OpenStreamAnswerMessage id is null //Error throw exception?! return; } streamId = oA.ID; } // Close stream message: else if (msg is CloseStreamMessage) { switch (state) { case ConnectionState.CONNECTING: case ConnectionState.CONNECTED: await internalDisconnectAsync(); break; } } // Rooster: else if (msg is RosterResultMessage) { NewRoosterMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Presence: else if (msg is PresenceMessage presenceMessage && !(presenceMessage.getFrom() is null)) { NewPresenceMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); }
/// <summary> /// Parses the given messages and invokes the ConnectionNewValidMessage event for each message. /// </summary> /// <param name="data">The messages string to parse.</param> private async Task parseMessageAsync(string data) { // Parse message: List <AbstractMessage> messages = null; try { messages = parser.parseMessages(data); } catch (Exception e) { Logger.Error("Error during message parsing." + e); return; } // Process messages: foreach (AbstractMessage msg in messages) { // Filter IQ messages which ids are not valid: if (msg is IQMessage) { IQMessage iq = msg as IQMessage; if (iq.GetType().Equals(IQMessage.RESULT) && messageIdCache.getTimed(iq.ID) != null) { Logger.Warn("Invalid message id received!"); return; } } // Invoke message processors: NewValidMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); // Should restart connection? if (msg.getRestartConnection() != AbstractMessage.NO_RESTART) { if (msg.getRestartConnection() == AbstractMessage.SOFT_RESTART) { await softRestartAsync(); } else if (msg.getRestartConnection() == AbstractMessage.HARD_RESTART) { await hardRestartAsync(); } else { throw new ArgumentException("Invalid restart type: " + msg.getRestartConnection()); } } // Filter already processed messages: if (msg.isProcessed()) { return; } // -------------------------------------------------------------------- // Open stream: if (msg is OpenStreamAnswerMessage) { OpenStreamAnswerMessage oA = msg as OpenStreamAnswerMessage; if (oA.ID == null) { // TODO Handle OpenStreamAnswerMessage id == null //Error throw exception?! return; } streamId = oA.ID; } // Close stream message: else if (msg is CloseStreamMessage) { switch (state) { case ConnectionState.CONNECTING: case ConnectionState.CONNECTED: await internalDisconnectAsync(); break; } } // Rooster: else if (msg is RosterMessage) { NewRoosterMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Presence: else if (msg is PresenceMessage && (msg as PresenceMessage).getFrom() != null) { NewPresenceMessage?.Invoke(this, new NewValidMessageEventArgs(msg)); } // Bookmarks: else if (msg is BookmarksResultMessage) { NewBookmarksResultMessage?.Invoke(this, new NewBookmarksResultMessageEventArgs(msg as BookmarksResultMessage)); } else if (msg is OmemoDeviceListEventMessage deviceListEvent) { OMEMO_HELPER.onOmemoDeviceListEventMessage(deviceListEvent); } } }