diff options
author | Marvin W <git@larma.de> | 2017-03-11 01:25:45 +0100 |
---|---|---|
committer | Marvin W <git@larma.de> | 2017-03-11 02:04:38 +0100 |
commit | 4c48bdc07291f55d7320721a5b0a29c092f7daa0 (patch) | |
tree | 2b7debd52adbbf25ebceb2721014e5dc6c61679f /libdino/src/service/pgp_manager.vala | |
parent | 1ccad732b9a783d8f0140373602b7c1b84d04198 (diff) | |
download | dino-4c48bdc07291f55d7320721a5b0a29c092f7daa0.tar.gz dino-4c48bdc07291f55d7320721a5b0a29c092f7daa0.zip |
Improve Plugin API (allow to move encryption into plugin)
Diffstat (limited to 'libdino/src/service/pgp_manager.vala')
-rw-r--r-- | libdino/src/service/pgp_manager.vala | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/libdino/src/service/pgp_manager.vala b/libdino/src/service/pgp_manager.vala index c8bf2180..76f70b84 100644 --- a/libdino/src/service/pgp_manager.vala +++ b/libdino/src/service/pgp_manager.vala @@ -1,4 +1,5 @@ using Gee; +using Xmpp; using Xmpp; using Dino.Entities; @@ -16,6 +17,27 @@ namespace Dino { public static void start(StreamInteractor stream_interactor, Database db) { PgpManager m = new PgpManager(stream_interactor, db); stream_interactor.add_module(m); + (GLib.Application.get_default() as Application).plugin_registry.register_encryption_list_entry(new EncryptionListEntry(m)); + } + + private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { + private PgpManager pgp_manager; + + public EncryptionListEntry(PgpManager pgp_manager) { + this.pgp_manager = pgp_manager; + } + + public Entities.Encryption encryption { get { + return Encryption.PGP; + }} + + public string name { get { + return "OpenPGP"; + }} + + public bool can_encrypt(Entities.Conversation conversation) { + return pgp_manager.pgp_key_ids.has_key(conversation.counterpart); + } } private PgpManager(StreamInteractor stream_interactor, Database db) { @@ -23,6 +45,27 @@ namespace Dino { this.db = db; stream_interactor.account_added.connect(on_account_added); + MessageManager.get_instance(stream_interactor).pre_message_received.connect(on_pre_message_received); + MessageManager.get_instance(stream_interactor).pre_message_send.connect(on_pre_message_send); + } + + private void on_pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) { + if (Xep.Pgp.MessageFlag.get_flag(message_stanza) != null && Xep.Pgp.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) { + encrypted = stream_interactor.get_stream(conversation.account).get_module(Xep.Pgp.Module.IDENTITY).encrypt(message_stanza, key_id); + } + if (!encrypted) { + message.marked = Entities.Message.Marked.WONTSEND; + } + } } public string? get_key_id(Account account, Jid jid) { |