Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Switched all the OMEMO stuff to the AsyncMessageResponseHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Mar 14, 2019
1 parent c4aa9d9 commit cdd26c9
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 564 deletions.
4 changes: 2 additions & 2 deletions Component_Tests/Classes/Crypto/Omemo/Test_Omemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void Test_Omemo_Enc_Dec_1()

[TestCategory("Crypto")]
[TestMethod]
public void Test_Omemo_Enc_Dec_2()
public async void Test_Omemo_Enc_Dec_2()
{
// Generate Alices keys:
IdentityKeyPair aliceIdentKey = CryptoUtils.generateOmemoIdentityKeyPair();
Expand Down Expand Up @@ -249,7 +249,7 @@ public void Test_Omemo_Enc_Dec_2()
// Bob decrypts the message:
SignalProtocolAddress aliceAddress = new SignalProtocolAddress(Utils.getBareJidFromFullJid(bobOmemoMessage.getFrom()), bobOmemoMessage.SOURCE_DEVICE_ID);
SessionCipher bobSessionCipher = new SessionCipher(bobSessionStore, bobPreKeyStore, bobSignedPreKeyStore, bobIdentStore, aliceAddress);
bobOmemoMessage.decrypt(bobSessionCipher, aliceAddress, BOB_ADDRESS.getDeviceId(), null);
await bobOmemoMessage.decryptAsync(bobSessionCipher, aliceAddress, BOB_ADDRESS.getDeviceId(), null);
Assert.IsFalse(bobOmemoMessage.ENCRYPTED);
Assert.AreEqual(aliceOrigMsg, bobOmemoMessage.MESSAGE);
}
Expand Down
8 changes: 4 additions & 4 deletions Data_Manager2/Classes/ConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private void C_NewRoosterMessage(IMessageSender sender, NewValidMessageEventArgs
}
}

private void C_NewChatMessage(XMPPClient client, XMPP_API.Classes.Network.Events.NewChatMessageEventArgs args)
private async void C_NewChatMessage(XMPPClient client, XMPP_API.Classes.Network.Events.NewChatMessageEventArgs args)
{
MessageMessage msg = args.getMessage();

Expand Down Expand Up @@ -526,7 +526,7 @@ private void C_NewChatMessage(XMPPClient client, XMPP_API.Classes.Network.Events
Logger.Error("Failed to decrypt OMEMO message - keys are corrupted");
return;
}
else if (!omemoMessage.decrypt(client.getOmemoHelper(), client.getXMPPAccount().omemoDeviceId))
else if (!await omemoMessage.decryptAsync(client.getOmemoHelper(), client.getXMPPAccount().omemoDeviceId))
{
return;
}
Expand Down Expand Up @@ -622,7 +622,7 @@ private void C_NewChatMessage(XMPPClient client, XMPP_API.Classes.Network.Events
// Send XEP-0184 (Message Delivery Receipts) reply:
if (msg.RECIPT_REQUESTED && id != null && !Settings.getSettingBoolean(SettingsConsts.DONT_SEND_CHAT_MESSAGE_RECEIVED_MARKERS))
{
Task.Run(async () =>
await Task.Run(async () =>
{
DeliveryReceiptMessage receiptMessage = new DeliveryReceiptMessage(client.getXMPPAccount().getFullJid(), from, msg.ID);
await client.sendAsync(receiptMessage, true);
Expand All @@ -634,7 +634,7 @@ private void C_NewChatMessage(XMPPClient client, XMPP_API.Classes.Network.Events
// Show toast:
if (!doesMessageExist && !chat.muted)
{
Task.Run(() =>
await Task.Run(() =>
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion Data_Manager2/Classes/DBManager/DiscoDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected override void dropTables()
#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--
private async void INSTANCE_ClientConnected(ConnectionHandler handler, Events.ClientConnectedEventArgs args)
private void INSTANCE_ClientConnected(ConnectionHandler handler, Events.ClientConnectedEventArgs args)
{
//messageIdCache.addTimed(await args.CLIENT.createDiscoAsync(args.CLIENT.getXMPPAccount().user.domain, DiscoType.ITEMS));
//messageIdCache.addTimed(await args.CLIENT.createDiscoAsync(args.CLIENT.getXMPPAccount().user.domain, DiscoType.INFO));
Expand Down
8 changes: 4 additions & 4 deletions UWPX_UI/Controls/OMEMO/OmemoDeviceListControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ private void UpdateView(DependencyPropertyChangedEventArgs e)
#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--
private void Reset_ibtn_Click(object sender, RoutedEventArgs args)
private async void Reset_ibtn_Click(object sender, RoutedEventArgs args)
{
VIEW_MODEL.ResetOmemoDevices(Account.Client);
await VIEW_MODEL.ResetOmemoDevicesAsync(Account.Client);
}

private void Refresh_ibtn_Click(object sender, RoutedEventArgs args)
private async void Refresh_ibtn_Click(object sender, RoutedEventArgs args)
{
VIEW_MODEL.RefreshOmemoDevices(Account.Client);
await VIEW_MODEL.RefreshOmemoDevicesAsync(Account.Client);
}

private static void OnAccountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down
6 changes: 2 additions & 4 deletions UWPX_UI_Context/Classes/AppBackgroundHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Data_Manager2.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UWPX_UI_Context.Classes
Expand All @@ -26,7 +22,9 @@ public static class AppBackgroundHelper
#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public static async Task InitAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
TrySetDefaultBackground();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ await Task.Run(() =>
// Send the message:
if (toSendMsg is OmemoMessageMessage toSendOmemoMsg)
{
chat.Client.sendOmemoMessage(toSendOmemoMsg, chat.Chat.chatJabberId, chat.Client.getXMPPAccount().getBareJid());
await chat.Client.sendOmemoMessageAsync(toSendOmemoMsg, chat.Chat.chatJabberId, chat.Client.getXMPPAccount().getBareJid());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Linq;
using Logging;
using System.Linq;
using System.Threading.Tasks;
using UWPX_UI_Context.Classes.DataTemplates;
using UWPX_UI_Context.Classes.DataTemplates.Controls;
using Windows.UI.Xaml;
using XMPP_API.Classes;
using XMPP_API.Classes.Network;
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Helper;
using XMPP_API.Classes.Network.XML.Messages.XEP_0384;

namespace UWPX_UI_Context.Classes.DataContext.Controls
Expand All @@ -27,37 +30,41 @@ public sealed class OmemoDeviceListControlContext
#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
public void ResetOmemoDevices(XMPPClient client)
public async Task ResetOmemoDevicesAsync(XMPPClient client)
{
OmemoHelper helper = client.getOmemoHelper();
if (!MODEL.ResettingDevices && !(helper is null))
{
MODEL.ResettingDevices = true;
helper.resetDeviceListStateless(OnResetDeviceListResult);
}
MODEL.ResettingDevices = true;
OmemoDevices devices = new OmemoDevices();
devices.IDS.Add(client.getXMPPAccount().omemoDeviceId);
await client.OMEMO_COMMAND_HELPER.setDeviceListAsync(devices);
MODEL.ResettingDevices = false;
}

private void OnResetDeviceListResult(bool success)
{
MODEL.ResettingDevices = false;
}

public void RefreshOmemoDevices(XMPPClient client)
public async Task RefreshOmemoDevicesAsync(XMPPClient client)
{
OmemoHelper helper = client.getOmemoHelper();
if (!MODEL.RefreshingDevices && !(helper is null))
MODEL.RefreshingDevices = true;
MessageResponseHelperResult<IQMessage> result = await client.OMEMO_COMMAND_HELPER.requestDeviceListAsync(client.getXMPPAccount().getBareJid());
if (result.STATE == MessageResponseHelperResultState.SUCCESS)
{
helper.requestDeviceListStateless(OnRequestDeviceListResult);
if (result.RESULT is OmemoDeviceListResultMessage deviceListResultMessage)
{
MODEL.DEVICES.Clear();
MODEL.DEVICES.AddRange(deviceListResultMessage.DEVICES.IDS.Select(x => new UintDataTemplate { Value = x }));
}
else
{
Logger.Warn("Failed to request device list (" + result.RESULT.ToString() + ").");
}
}
}

private void OnRequestDeviceListResult(bool success, OmemoDevices devices)
{
if (success)
else
{
MODEL.DEVICES.Clear();
MODEL.DEVICES.AddRange(devices.IDS.Select(x => new UintDataTemplate { Value = x }));
Logger.Warn("Failed to request device list (" + result.STATE.ToString() + ").");
}
MODEL.RefreshingDevices = false;
}

public void UpdateView(DependencyPropertyChangedEventArgs e)
Expand Down
47 changes: 24 additions & 23 deletions XMPP_API/Classes/GeneralCommandHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Logging;
using System;
using System.Threading.Tasks;
using XMPP_API.Classes.Network;
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Helper;
using XMPP_API.Classes.Network.XML.Messages.XEP_0030;
Expand All @@ -12,7 +13,7 @@ public class GeneralCommandHelper
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
private readonly XMPPClient CLIENT;
private readonly XMPPConnection2 CONNECTION;

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
Expand All @@ -23,9 +24,9 @@ public class GeneralCommandHelper
/// <history>
/// 02/11/2018 Created [Fabian Sauter]
/// </history>
public GeneralCommandHelper(XMPPClient client)
public GeneralCommandHelper(XMPPConnection2 connection)
{
this.CLIENT = client;
this.CONNECTION = connection;
}

#endregion
Expand Down Expand Up @@ -53,7 +54,7 @@ public async Task<string> setPreseceAsync(Presence presence, string status)
public async Task<string> setPreseceAsync(string from, string to, Presence presence, string status)
{
PresenceMessage presenceMessage = new PresenceMessage(from, to, presence, status, int.MinValue);
await CLIENT.sendAsync(presenceMessage);
await CONNECTION.sendAsync(presenceMessage);
return presenceMessage.ID;
}

Expand All @@ -67,8 +68,8 @@ public async Task<string> setPreseceAsync(string from, string to, Presence prese
public async Task<MessageResponseHelperResult<IQMessage>> requestRosterAsync()
{
Predicate<IQMessage> predicate = (x) => { return true; };
AsyncMessageResponseHelper<IQMessage> helper = new AsyncMessageResponseHelper<IQMessage>(CLIENT, predicate);
RosterRequestMessage msg = new RosterRequestMessage(CLIENT.getXMPPAccount().getFullJid(), CLIENT.getXMPPAccount().getBareJid());
AsyncMessageResponseHelper<IQMessage> helper = new AsyncMessageResponseHelper<IQMessage>(CONNECTION, predicate);
RosterRequestMessage msg = new RosterRequestMessage(CONNECTION.account.getFullJid(), CONNECTION.account.getBareJid());
return await helper.startAsync(msg);
}

Expand All @@ -78,8 +79,8 @@ public async Task<MessageResponseHelperResult<IQMessage>> requestRosterAsync()
/// <returns>True if sending the message succeeded.</returns>
public async Task<bool> sendRequestRosterMessageAsync()
{
RosterRequestMessage msg = new RosterRequestMessage(CLIENT.getXMPPAccount().getFullJid(), CLIENT.getXMPPAccount().getBareJid());
return await CLIENT.sendAsync(msg);
RosterRequestMessage msg = new RosterRequestMessage(CONNECTION.account.getFullJid(), CONNECTION.account.getBareJid());
return await CONNECTION.sendAsync(msg);
}

/// <summary>
Expand All @@ -90,7 +91,7 @@ public async Task<bool> sendRequestRosterMessageAsync()
public async Task sendPresenceProbeAsync(string fromFullJid, string toBareJid)
{
PresenceProbeMessage msg = new PresenceProbeMessage(fromFullJid, toBareJid);
await CLIENT.sendAsync(msg);
await CONNECTION.sendAsync(msg);
Logger.Info("Send presence probe from " + fromFullJid + " to " + toBareJid);
}

Expand All @@ -102,8 +103,8 @@ public async Task sendPresenceProbeAsync(string fromFullJid, string toBareJid)
/// <returns>The id of the send AddToRosterMessage.</returns>
public async Task<string> addToRosterAsync(string bareJid)
{
AddToRosterMessage msg = new AddToRosterMessage(CLIENT.getXMPPAccount().getFullJid(), bareJid);
await CLIENT.sendAsync(msg, true);
AddToRosterMessage msg = new AddToRosterMessage(CONNECTION.account.getFullJid(), bareJid);
await CONNECTION.sendAsync(msg, true);
return msg.ID;
}

Expand All @@ -115,8 +116,8 @@ public async Task<string> addToRosterAsync(string bareJid)
/// <returns>The id of the send RemoveFromRosterMessage.</returns>
public async Task<string> removeFromRosterAsync(string bareJid)
{
RemoveFromRosterMessage msg = new RemoveFromRosterMessage(CLIENT.getXMPPAccount().getFullJid(), bareJid);
await CLIENT.sendAsync(msg, true);
RemoveFromRosterMessage msg = new RemoveFromRosterMessage(CONNECTION.account.getFullJid(), bareJid);
await CONNECTION.sendAsync(msg, true);
return msg.ID;
}

Expand All @@ -127,8 +128,8 @@ public async Task<string> removeFromRosterAsync(string bareJid)
/// <param name="state">The chat state.</param>
public async Task sendChatStateAsync(string target, ChatState state)
{
ChatStateMessage chatStateMessage = new ChatStateMessage(target, CLIENT.getXMPPAccount().getFullJid(), state);
await CLIENT.sendAsync(chatStateMessage);
ChatStateMessage chatStateMessage = new ChatStateMessage(target, CONNECTION.account.getFullJid(), state);
await CONNECTION.sendAsync(chatStateMessage);
}

/// <summary>
Expand All @@ -141,8 +142,8 @@ public async Task sendChatStateAsync(string target, ChatState state)
/// <returns>Returns a MessageResponseHelper listening for DiscoRequestMessage answers.</returns>
public MessageResponseHelper<IQMessage> createDisco(string target, DiscoType type, MessageResponseHelper<IQMessage>.OnMessageHandler onMessage, MessageResponseHelper<IQMessage>.OnTimeoutHandler onTimeout)
{
MessageResponseHelper<IQMessage> helper = new MessageResponseHelper<IQMessage>(CLIENT, onMessage, onTimeout);
DiscoRequestMessage disco = new DiscoRequestMessage(CLIENT.getXMPPAccount().getFullJid(), target, type);
MessageResponseHelper<IQMessage> helper = new MessageResponseHelper<IQMessage>(CONNECTION, onMessage, onTimeout);
DiscoRequestMessage disco = new DiscoRequestMessage(CONNECTION.account.getFullJid(), target, type);
helper.start(disco);
return helper;
}
Expand All @@ -153,8 +154,8 @@ public MessageResponseHelper<IQMessage> createDisco(string target, DiscoType typ
/// <param name="bareJid">The bare JID of the target e.g. '[email protected]'.</param>
public async Task requestPresenceSubscriptionAsync(string bareJid)
{
PresenceMessage msg = new PresenceMessage(CLIENT.getXMPPAccount().getBareJid(), bareJid, "subscribe");
await CLIENT.sendAsync(msg, true);
PresenceMessage msg = new PresenceMessage(CONNECTION.account.getBareJid(), bareJid, "subscribe");
await CONNECTION.sendAsync(msg, true);
}

/// <summary>
Expand All @@ -163,8 +164,8 @@ public async Task requestPresenceSubscriptionAsync(string bareJid)
/// <param name="bareJid">The bare JID of the target e.g. '[email protected]'.</param>
public async Task unsubscribeFromPresenceAsync(string bareJid)
{
PresenceMessage msg = new PresenceMessage(CLIENT.getXMPPAccount().getBareJid(), bareJid, "unsubscribe");
await CLIENT.sendAsync(msg, true);
PresenceMessage msg = new PresenceMessage(CONNECTION.account.getBareJid(), bareJid, "unsubscribe");
await CONNECTION.sendAsync(msg, true);
}

/// <summary>
Expand All @@ -174,8 +175,8 @@ public async Task unsubscribeFromPresenceAsync(string bareJid)
/// <param name="accept">Whether the request was accepted or not.</param>
public async Task answerPresenceSubscriptionRequestAsync(string bareJid, bool accept)
{
PresenceMessage msg = new PresenceMessage(CLIENT.getXMPPAccount().getBareJid(), bareJid, accept ? "subscribed" : "unsubscribed");
await CLIENT.sendAsync(msg, true);
PresenceMessage msg = new PresenceMessage(CONNECTION.account.getBareJid(), bareJid, accept ? "subscribed" : "unsubscribed");
await CONNECTION.sendAsync(msg, true);
}

#endregion
Expand Down
Loading

0 comments on commit cdd26c9

Please sign in to comment.