public async Task sendOmemoMessageAsync(OmemoMessageMessage msg, string chatJid, string accountJid) { // Check if already trying to build a new session: if (MESSAGE_CACHE.ContainsKey(chatJid)) { MESSAGE_CACHE[chatJid].Item1.Add(msg); } else { // If not start a new session build helper: OmemoSessionBuildHelper sessionHelper = new OmemoSessionBuildHelper(chatJid, accountJid, CONNECTION.account.getFullJid(), CONNECTION, this); MESSAGE_CACHE[chatJid] = new Tuple <List <OmemoMessageMessage>, OmemoSessionBuildHelper>(new List <OmemoMessageMessage>(), sessionHelper); MESSAGE_CACHE[chatJid].Item1.Add(msg); Tuple <OmemoDeviceListSubscriptionState, DateTime> subscription = OMEMO_STORE.LoadDeviceListSubscription(chatJid); OmemoSessionBuildResult result = await sessionHelper.buildSessionAsync(subscription.Item1); if (result.SUCCESS) { OMEMO_SESSIONS[result.SESSION.CHAT_JID] = result.SESSION; await sendAllOutstandingMessagesAsync(result.SESSION); } else { OmemoSessionBuildErrorEventArgs args = new OmemoSessionBuildErrorEventArgs(chatJid, result.ERROR, MESSAGE_CACHE[chatJid]?.Item1 ?? new List <OmemoMessageMessage>()); MESSAGE_CACHE.Remove(chatJid); CONNECTION.OnOmemoSessionBuildError(args); Logger.Error("Failed to build OMEMO session for: " + chatJid + " with: " + result.ERROR); } } }
private void C_OmemoSessionBuildError(XMPPClient client, OmemoSessionBuildErrorEventArgs args) { Task.Run(() => { ChatTable chat = ChatDBManager.INSTANCE.getChat(ChatTable.generateId(args.CHAT_JID, client.getXMPPAccount().getBareJid())); if (!(chat is null)) { // Add an error chat message: ChatMessageTable msg = new ChatMessageTable() { id = ChatMessageTable.generateErrorMessageId(AbstractMessage.getRandomId(), chat.id), chatId = chat.id, date = DateTime.Now, fromUser = args.CHAT_JID, isImage = false, message = "Failed to encrypt and send " + args.MESSAGES.Count + " OMEMO message(s) with:\n" + args.ERROR, state = MessageState.UNREAD, type = MessageMessage.TYPE_ERROR }; ChatDBManager.INSTANCE.setChatMessage(msg, true, false); // Set chat messages to encrypted failed: setOmemoChatMessagesSendFailed(args.MESSAGES, chat); } }); }
public async Task sendOmemoMessageAsync(OmemoEncryptedMessage msg, string srcBareJid, string dstBareJid, bool trustedSrcKeysOnly, bool trustedDstKeysOnly) { // Check if already trying to build a new session: if (MESSAGE_CACHE.ContainsKey(dstBareJid)) { MESSAGE_CACHE[dstBareJid].Item1.Add(msg); } else { // If not start a new session build helper: OmemoSessionBuildHelper sessionHelper = new OmemoSessionBuildHelper(srcBareJid, dstBareJid, CONNECTION, this, trustedSrcKeysOnly, trustedDstKeysOnly); MESSAGE_CACHE[dstBareJid] = new Tuple <List <OmemoEncryptedMessage>, OmemoSessionBuildHelper>(new List <OmemoEncryptedMessage>(), sessionHelper); MESSAGE_CACHE[dstBareJid].Item1.Add(msg); Tuple <OmemoDeviceListSubscriptionState, DateTime> subscription = OMEMO_STORAGE.LoadDeviceListSubscription(dstBareJid); OmemoSessionBuildResult result = await sessionHelper.buildSessionAsync(subscription.Item1); if (result.SUCCESS) { OMEMO_SESSIONS[dstBareJid] = result.SESSIONS; await sendAllOutstandingMessagesAsync(result.SESSIONS); } else { OmemoSessionBuildErrorEventArgs args = new OmemoSessionBuildErrorEventArgs(dstBareJid, result.ERROR, MESSAGE_CACHE[dstBareJid]?.Item1 ?? new List <OmemoEncryptedMessage>()); MESSAGE_CACHE.Remove(dstBareJid); CONNECTION.OnOmemoSessionBuildError(args); Logger.Error("Failed to build OMEMO session for: " + dstBareJid + " with: " + result.ERROR); } } }
//--------------------------------------------------------Events:---------------------------------------------------------------------\\ #region --Events-- private void OnOmemoSessionBuildError(XMPPClient xmppClient, OmemoSessionBuildErrorEventArgs args) { Task.Run(() => { ChatModel chat; using (SemaLock semaLock = DataCache.INSTANCE.NewChatSemaLock()) { chat = DataCache.INSTANCE.GetChat(xmppClient.getXMPPAccount().getBareJid(), args.CHAT_JID, semaLock); } if (!(chat is null)) { // Add an error chat message: ChatMessageModel msg = new ChatMessageModel() { chatId = chat.id, date = DateTime.Now, fromBareJid = args.CHAT_JID, isCC = false, isDummyMessage = false, isEncrypted = false, isFavorite = false, isImage = false, message = "Failed to encrypt and send " + args.MESSAGES.Count + " OMEMO message(s) with:\n" + args.ERROR, stableId = AbstractMessage.getRandomId(), state = MessageState.UNREAD, type = MessageMessage.TYPE_ERROR }; DataCache.INSTANCE.AddChatMessage(msg, chat); // Set chat messages to encrypted failed: SetOmemoChatMessagesSendFailed(args.MESSAGES, chat); } }); }
private void Connection_OmemoSessionBuildErrorEvent(XmppConnection sender, OmemoSessionBuildErrorEventArgs args) { OmemoSessionBuildError?.Invoke(this, args); }
private void Connection_OmemoSessionBuildErrorEvent(XMPPConnection2 connection, OmemoSessionBuildErrorEventArgs args) { OmemoSessionBuildError?.Invoke(this, args); }