diff options
author | fiaxh <git@mx.ax.lt> | 2017-09-05 23:53:18 +0200 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-09-06 00:15:18 +0200 |
commit | 8944029128e3d0f9e32b61e00e880d92fceabb31 (patch) | |
tree | 8cf872969d3349c61278b19273e76c65036c429d /plugins/openpgp | |
parent | 312372350e24d1ebd8afbb0029fac04f2b64eb83 (diff) | |
download | dino-8944029128e3d0f9e32b61e00e880d92fceabb31.tar.gz dino-8944029128e3d0f9e32b61e00e880d92fceabb31.zip |
Move encryption menu into ChatInput, PGP support for MUCs
Diffstat (limited to 'plugins/openpgp')
-rw-r--r-- | plugins/openpgp/src/database.vala | 4 | ||||
-rw-r--r-- | plugins/openpgp/src/encryption_list_entry.vala | 21 | ||||
-rw-r--r-- | plugins/openpgp/src/manager.vala | 126 | ||||
-rw-r--r-- | plugins/openpgp/src/stream_module.vala | 7 |
4 files changed, 104 insertions, 54 deletions
diff --git a/plugins/openpgp/src/database.vala b/plugins/openpgp/src/database.vala index 7d41db3c..0e4bf74c 100644 --- a/plugins/openpgp/src/database.vala +++ b/plugins/openpgp/src/database.vala @@ -46,7 +46,7 @@ public class Database : Qlite.Database { public string? get_contact_key(Jid jid) { return contact_key_table.select({contact_key_table.key}) - .with(contact_key_table.jid, "=", jid.bare_jid.to_string())[contact_key_table.key]; + .with(contact_key_table.jid, "=", jid.to_string())[contact_key_table.key]; } public void set_account_key(Account account, string key) { @@ -64,4 +64,4 @@ public class Database : Qlite.Database { public override void migrate(long oldVersion) { } } -}
\ No newline at end of file +} diff --git a/plugins/openpgp/src/encryption_list_entry.vala b/plugins/openpgp/src/encryption_list_entry.vala index 584e065b..e0a11865 100644 --- a/plugins/openpgp/src/encryption_list_entry.vala +++ b/plugins/openpgp/src/encryption_list_entry.vala @@ -1,3 +1,5 @@ +using Gee; + using Dino.Entities; namespace Dino.Plugins.OpenPgp { @@ -19,8 +21,23 @@ private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { }} public bool can_encrypt(Entities.Conversation conversation) { - string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, conversation.counterpart); - return key_id != null && GPGHelper.get_keylist(key_id).size > 0; + if (conversation.type_ == Conversation.Type.CHAT) { + string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, conversation.counterpart); + return key_id != null && GPGHelper.get_keylist(key_id).size > 0; + } else if (conversation.type_ == Conversation.Type.GROUPCHAT) { + Gee.List<Jid> muc_jids = new Gee.ArrayList<Jid>(); + Gee.List<Jid>? occupants = stream_interactor.get_module(MucManager.IDENTITY).get_occupants(conversation.counterpart, conversation.account); + if (occupants != null) muc_jids.add_all(occupants); + Gee.List<Jid>? offline_members = stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(conversation.counterpart, conversation.account); + if (occupants != null) muc_jids.add_all(offline_members); + + foreach (Jid jid in muc_jids) { + string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, jid); + if (key_id == null) return false; + } + return true; + } + return false; } } diff --git a/plugins/openpgp/src/manager.vala b/plugins/openpgp/src/manager.vala index 0a145283..4c8b6d13 100644 --- a/plugins/openpgp/src/manager.vala +++ b/plugins/openpgp/src/manager.vala @@ -6,69 +6,99 @@ using Dino.Entities; namespace Dino.Plugins.OpenPgp { - public class Manager : StreamInteractionModule, Object { - public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("pgp_manager"); - public string id { get { return IDENTITY.id; } } +public class Manager : StreamInteractionModule, Object { + public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("pgp_manager"); + public string id { get { return IDENTITY.id; } } - public const string MESSAGE_ENCRYPTED = "pgp"; + public const string MESSAGE_ENCRYPTED = "pgp"; - private StreamInteractor stream_interactor; - private Database db; - private HashMap<Jid, string> pgp_key_ids = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); + private StreamInteractor stream_interactor; + private Database db; + private HashMap<Jid, string> pgp_key_ids = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); - public static void start(StreamInteractor stream_interactor, Database db) { - Manager m = new Manager(stream_interactor, db); - stream_interactor.add_module(m); - } + public static void start(StreamInteractor stream_interactor, Database db) { + Manager m = new Manager(stream_interactor, db); + stream_interactor.add_module(m); + } - private Manager(StreamInteractor stream_interactor, Database db) { - this.stream_interactor = stream_interactor; - this.db = db; + private Manager(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; - stream_interactor.account_added.connect(on_account_added); - stream_interactor.get_module(MessageProcessor.IDENTITY).pre_message_received.connect(on_pre_message_received); - stream_interactor.get_module(MessageProcessor.IDENTITY).pre_message_send.connect(on_pre_message_send); - } + stream_interactor.account_added.connect(on_account_added); + stream_interactor.get_module(MessageProcessor.IDENTITY).pre_message_received.connect(on_pre_message_received); + stream_interactor.get_module(MessageProcessor.IDENTITY).pre_message_send.connect(check_encypt); + } - private void on_pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { - if (MessageFlag.get_flag(message_stanza) != null && MessageFlag.get_flag(message_stanza).decrypted) { - message.encryption = Encryption.PGP; - } + private void on_pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { + if (MessageFlag.get_flag(message_stanza) != null && MessageFlag.get_flag(message_stanza).decrypted) { + message.encryption = Encryption.PGP; } + } - private void on_pre_message_send(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { - if (message.encryption == Encryption.PGP) { - string? key_id = get_key_id(conversation.account, message.counterpart); - bool encrypted = false; - if (key_id != null) { - Core.XmppStream? stream = stream_interactor.get_stream(conversation.account); - if (stream != null) encrypted = stream.get_module(Module.IDENTITY).encrypt(message_stanza, key_id); - } - if (!encrypted) { - message.marked = Entities.Message.Marked.WONTSEND; - } + private void check_encypt(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { + if (message.encryption == Encryption.PGP) { + bool encrypted = false; + if (conversation.type_ == Conversation.Type.CHAT) { + encrypted = encrypt_for_chat(message, message_stanza, conversation); + } else if (conversation.type_ == Conversation.Type.GROUPCHAT) { + encrypted = encrypt_for_groupchat(message, message_stanza, conversation); } + if (!encrypted) message.marked = Entities.Message.Marked.WONTSEND; } + } - public string? get_key_id(Account account, Jid jid) { - return db.get_contact_key(jid); + private bool encrypt_for_chat(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { + Core.XmppStream? stream = stream_interactor.get_stream(conversation.account); + if (stream == null) return false; + + string? key_id = get_key_id(conversation.account, message.counterpart); + if (key_id != null) { + return stream.get_module(Module.IDENTITY).encrypt(message_stanza, new Gee.ArrayList<string>.wrap(new string[]{key_id})); } + return false; + } - private void on_account_added(Account account) { - stream_interactor.module_manager.get_module(account, Module.IDENTITY).received_jid_key_id.connect((stream, jid, key_id) => { - on_jid_key_received(account, new Jid(jid), key_id); - }); + private bool encrypt_for_groupchat(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { + Core.XmppStream? stream = stream_interactor.get_stream(conversation.account); + if (stream == null) return false; + + Gee.List<Jid> muc_jids = new Gee.ArrayList<Jid>(); + Gee.List<Jid>? occupants = stream_interactor.get_module(MucManager.IDENTITY).get_occupants(conversation.counterpart, conversation.account); + if (occupants != null) muc_jids.add_all(occupants); + Gee.List<Jid>? offline_members = stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(conversation.counterpart, conversation.account); + if (occupants != null) muc_jids.add_all(offline_members); + + Gee.List<string> keys = new Gee.ArrayList<string>(); + foreach (Jid jid in muc_jids) { + string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, jid); + if (key_id != null && GPGHelper.get_keylist(key_id).size > 0 && !keys.contains(key_id)) { + keys.add(key_id); + } } + return stream.get_module(Module.IDENTITY).encrypt(message_stanza, keys); + } - private void on_jid_key_received(Account account, Jid jid, string key_id) { - lock (pgp_key_ids) { - if (!pgp_key_ids.has_key(jid) || pgp_key_ids[jid] != key_id) { - if (!stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account)) { - db.set_contact_key(jid.bare_jid, key_id); - } - } - pgp_key_ids[jid] = key_id; + public string? get_key_id(Account account, Jid jid) { + Jid search_jid = stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account) ? jid : jid.bare_jid; + return db.get_contact_key(search_jid); + } + + private void on_account_added(Account account) { + stream_interactor.module_manager.get_module(account, Module.IDENTITY).received_jid_key_id.connect((stream, jid, key_id) => { + on_jid_key_received(account, new Jid(jid), key_id); + }); + } + + private void on_jid_key_received(Account account, Jid jid, string key_id) { + lock (pgp_key_ids) { + if (!pgp_key_ids.has_key(jid) || pgp_key_ids[jid] != key_id) { + Jid set_jid = stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account) ? jid : jid.bare_jid; + db.set_contact_key(set_jid, key_id); } + pgp_key_ids[jid] = key_id; } } -}
\ No newline at end of file +} + +} diff --git a/plugins/openpgp/src/stream_module.vala b/plugins/openpgp/src/stream_module.vala index 745f2005..6c55cdc5 100644 --- a/plugins/openpgp/src/stream_module.vala +++ b/plugins/openpgp/src/stream_module.vala @@ -33,8 +33,11 @@ namespace Dino.Plugins.OpenPgp { } } - public bool encrypt(Message.Stanza message, string key_id) { - string? enc_body = gpg_encrypt(message.body, new string[] {key_id, own_key.fpr}); + public bool encrypt(Message.Stanza message, Gee.List<string> fprs) { + string[] encrypt_to = new string[fprs.size + 1]; + for (int i = 0; i < fprs.size; i++) encrypt_to[i] = fprs[i]; + encrypt_to[encrypt_to.length - 1] = own_key.fpr; + string? enc_body = gpg_encrypt(message.body, encrypt_to); if (enc_body != null) { message.stanza.put_node(new StanzaNode.build("x", NS_URI_ENCRYPTED).add_self_xmlns().put_node(new StanzaNode.text(enc_body))); message.body = "[This message is OpenPGP encrypted (see XEP-0027)]"; |