diff options
author | fiaxh <git@mx.ax.lt> | 2017-11-11 21:29:13 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-11-16 17:43:00 +0100 |
commit | 3f531d6b91edab6c79fa232143db828bad13853c (patch) | |
tree | 1083046c94e0b4a43cf16ac4a388fcea8ef91e84 /plugins/openpgp/src | |
parent | 1d0745177e7a116455811dfd26e07b848cb89b75 (diff) | |
download | dino-3f531d6b91edab6c79fa232143db828bad13853c.tar.gz dino-3f531d6b91edab6c79fa232143db828bad13853c.zip |
Read+(write) stream async
Diffstat (limited to 'plugins/openpgp/src')
-rw-r--r-- | plugins/openpgp/src/stream_module.vala | 100 |
1 files changed, 61 insertions, 39 deletions
diff --git a/plugins/openpgp/src/stream_module.vala b/plugins/openpgp/src/stream_module.vala index 068370fd..6264ce49 100644 --- a/plugins/openpgp/src/stream_module.vala +++ b/plugins/openpgp/src/stream_module.vala @@ -43,22 +43,16 @@ namespace Dino.Plugins.OpenPgp { return false; } - public string? get_cyphertext(Message.Stanza message) { - StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_ENCRYPTED); - return x_node == null ? null : x_node.get_string_content(); - } - public override void attach(XmppStream stream) { stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence); stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.connect(on_pre_send_presence_stanza); - stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message); + stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineDecryptListener()); stream.add_flag(new Flag()); } public override void detach(XmppStream stream) { stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence); stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.disconnect(on_pre_send_presence_stanza); - stream.get_module(Message.Module.IDENTITY).pre_received_message.disconnect(on_pre_received_message); } public static void require(XmppStream stream) { @@ -69,18 +63,24 @@ namespace Dino.Plugins.OpenPgp { public override string get_id() { return IDENTITY.id; } private void on_received_presence(XmppStream stream, Presence.Stanza presence) { - StanzaNode x_node = presence.stanza.get_subnode("x", NS_URI_SIGNED); - if (x_node != null) { - string? sig = x_node.get_string_content(); - if (sig != null) { - string signed_data = presence.status == null ? "" : presence.status; - string? key_id = get_sign_key(sig, signed_data); - if (key_id != null) { - stream.get_flag(Flag.IDENTITY).set_key_id(presence.from, key_id); - received_jid_key_id(stream, presence.from, key_id); + new Thread<void*> (null, () => { + StanzaNode x_node = presence.stanza.get_subnode("x", NS_URI_SIGNED); + if (x_node != null) { + string? sig = x_node.get_string_content(); + if (sig != null) { + string signed_data = presence.status == null ? "" : presence.status; + string? key_id = get_sign_key(sig, signed_data); + if (key_id != null) { + stream.get_flag(Flag.IDENTITY).set_key_id(presence.from, key_id); + Idle.add(() => { + received_jid_key_id(stream, presence.from, key_id); + return false; + }); + } } } - } + return null; + }); } private void on_pre_send_presence_stanza(XmppStream stream, Presence.Stanza presence) { @@ -89,19 +89,6 @@ namespace Dino.Plugins.OpenPgp { } } - private void on_pre_received_message(XmppStream stream, Message.Stanza message) { - string? encrypted = get_cyphertext(message); - if (encrypted != null) { - MessageFlag flag = new MessageFlag(); - message.add_flag(flag); - string? decrypted = gpg_decrypt(encrypted); - if (decrypted != null) { - flag.decrypted = true; - message.body = decrypted; - } - } - } - private static string? gpg_encrypt(string plain, GPG.Key[] keys) { string encr; try { @@ -113,15 +100,6 @@ namespace Dino.Plugins.OpenPgp { return encr.substring(encryption_start, encr.length - "\n-----END PGP MESSAGE-----".length - encryption_start); } - private static string? gpg_decrypt(string enc) { - string armor = "-----BEGIN PGP MESSAGE-----\n\n" + enc + "\n-----END PGP MESSAGE-----"; - string? decr = null; - try { - decr = GPGHelper.decrypt(armor); - } catch (Error e) { } - return decr; - } - private static string? get_sign_key(string sig, string signed_text) { string armor = "-----BEGIN PGP MESSAGE-----\n\n" + sig + "\n-----END PGP MESSAGE-----"; string? sign_key = null; @@ -156,4 +134,48 @@ namespace Dino.Plugins.OpenPgp { public override string get_ns() { return NS_URI; } public override string get_id() { return id; } } + +public class ReceivedPipelineDecryptListener : StanzaListener<Message.Stanza> { + + private const string[] after_actions_const = {"MODIFY_BODY"}; + + public override string action_group { get { return "ENCRYPT_BODY"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async void run(Core.XmppStream stream, Message.Stanza message) { + string? encrypted = get_cyphertext(message); + if (encrypted != null) { + MessageFlag flag = new MessageFlag(); + message.add_flag(flag); + string? decrypted = yield gpg_decrypt(encrypted); + if (decrypted != null) { + flag.decrypted = true; + message.body = decrypted; + } + } + } + + private static async string? gpg_decrypt(string enc) { + SourceFunc callback = gpg_decrypt.callback; + string? res = null; + new Thread<void*> (null, () => { + string armor = "-----BEGIN PGP MESSAGE-----\n\n" + enc + "\n-----END PGP MESSAGE-----"; + try { + res = GPGHelper.decrypt(armor); + } catch (Error e) { + res = null; + } + Idle.add((owned) callback); + return null; + }); + yield; + return res; + } + + private string? get_cyphertext(Message.Stanza message) { + StanzaNode? x_node = message.stanza.get_subnode("x", NS_URI_ENCRYPTED); + return x_node == null ? null : x_node.get_string_content(); + } +} + } |