2017-03-12 01:49:53 +00:00
|
|
|
using Gee;
|
|
|
|
using Xmpp;
|
|
|
|
|
|
|
|
using Xmpp;
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino.Plugins.OpenPgp {
|
|
|
|
|
|
|
|
public class Manager : StreamInteractionModule, Object {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("pgp_manager");
|
|
|
|
public string id { get { return IDENTITY.id; } }
|
2017-03-12 01:49:53 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
stream_interactor.account_added.connect(on_account_added);
|
2017-04-04 13:47:00 +00:00
|
|
|
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);
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-03-19 11:55:36 +00:00
|
|
|
Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
|
|
|
|
if (stream != null) encrypted = stream.get_module(Module.IDENTITY).encrypt(message_stanza, key_id);
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
if (!encrypted) {
|
|
|
|
message.marked = Entities.Message.Marked.WONTSEND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string? get_key_id(Account account, Jid jid) {
|
2017-03-12 13:44:09 +00:00
|
|
|
return db.get_contact_key(jid);
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-03-16 15:28:32 +00:00
|
|
|
lock (pgp_key_ids) {
|
|
|
|
if (!pgp_key_ids.has_key(jid) || pgp_key_ids[jid] != key_id) {
|
2017-03-19 11:55:36 +00:00
|
|
|
if (!stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account)) {
|
2017-03-16 15:28:32 +00:00
|
|
|
db.set_contact_key(jid.bare_jid, key_id);
|
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
2017-03-16 15:28:32 +00:00
|
|
|
pgp_key_ids[jid] = key_id;
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|