aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service
diff options
context:
space:
mode:
authorMarvin W <git@larma.de>2017-03-11 01:25:45 +0100
committerMarvin W <git@larma.de>2017-03-11 02:04:38 +0100
commit4c48bdc07291f55d7320721a5b0a29c092f7daa0 (patch)
tree2b7debd52adbbf25ebceb2721014e5dc6c61679f /libdino/src/service
parent1ccad732b9a783d8f0140373602b7c1b84d04198 (diff)
downloaddino-4c48bdc07291f55d7320721a5b0a29c092f7daa0.tar.gz
dino-4c48bdc07291f55d7320721a5b0a29c092f7daa0.zip
Improve Plugin API (allow to move encryption into plugin)
Diffstat (limited to 'libdino/src/service')
-rw-r--r--libdino/src/service/conversation_manager.vala2
-rw-r--r--libdino/src/service/database.vala4
-rw-r--r--libdino/src/service/message_manager.vala64
-rw-r--r--libdino/src/service/muc_manager.vala2
-rw-r--r--libdino/src/service/pgp_manager.vala43
-rw-r--r--libdino/src/service/stream_interactor.vala2
6 files changed, 76 insertions, 41 deletions
diff --git a/libdino/src/service/conversation_manager.vala b/libdino/src/service/conversation_manager.vala
index 716c9b39..df4300e2 100644
--- a/libdino/src/service/conversation_manager.vala
+++ b/libdino/src/service/conversation_manager.vala
@@ -70,7 +70,7 @@ public class ConversationManager : StreamInteractionModule, Object {
}
}
- private void on_message_received(Entities.Message message, Conversation conversation) {
+ private void on_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) {
ensure_start_conversation(conversation.counterpart, conversation.account);
}
diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala
index 13be6222..83686424 100644
--- a/libdino/src/service/database.vala
+++ b/libdino/src/service/database.vala
@@ -337,7 +337,7 @@ public class Database : Qlite.Database {
new_message.body = row[message.body];
new_message.account = get_account_by_id(row[message.account_id]); // TODO dont have to generate acc new
new_message.marked = (Message.Marked) row[message.marked];
- new_message.encryption = (Message.Encryption) row[message.encryption];
+ new_message.encryption = (Encryption) row[message.encryption];
new_message.real_jid = get_real_jid_for_message(new_message);
new_message.notify.connect(on_message_update);
@@ -396,7 +396,7 @@ public class Database : Qlite.Database {
int64? last_active = row[conversation.last_active];
if (last_active != null) new_conversation.last_active = new DateTime.from_unix_utc(last_active);
new_conversation.type_ = (Conversation.Type) row[conversation.type_];
- new_conversation.encryption = (Conversation.Encryption) row[conversation.encryption];
+ new_conversation.encryption = (Encryption) row[conversation.encryption];
int? read_up_to = row[conversation.read_up_to];
if (read_up_to != null) new_conversation.read_up_to = get_message_by_id(read_up_to);
diff --git a/libdino/src/service/message_manager.vala b/libdino/src/service/message_manager.vala
index 07a35a49..33330856 100644
--- a/libdino/src/service/message_manager.vala
+++ b/libdino/src/service/message_manager.vala
@@ -8,8 +8,10 @@ namespace Dino {
public class MessageManager : StreamInteractionModule, Object {
public const string ID = "message_manager";
- public signal void pre_message_received(Entities.Message message, Conversation conversation);
+ public signal void pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation);
public signal void message_received(Entities.Message message, Conversation conversation);
+ public signal void out_message_created(Entities.Message message, Conversation conversation);
+ public signal void pre_message_send(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation);
public signal void message_sent(Entities.Message message, Conversation conversation);
private StreamInteractor stream_interactor;
@@ -78,7 +80,6 @@ public class MessageManager : StreamInteractionModule, Object {
stream_interactor.module_manager.get_module(account, Xmpp.Message.Module.IDENTITY).received_message.connect( (stream, message) => {
on_message_received(account, message);
});
- stream_interactor.stream_negotiated.connect(send_unsent_messages);
}
private void send_unsent_messages(Account account) {
@@ -110,11 +111,8 @@ public class MessageManager : StreamInteractionModule, Object {
Xep.DelayedDelivery.MessageFlag? deleyed_delivery_flag = Xep.DelayedDelivery.MessageFlag.get_flag(message);
new_message.time = deleyed_delivery_flag != null ? deleyed_delivery_flag.datetime : new DateTime.now_utc();
new_message.local_time = new DateTime.now_utc();
- if (Xep.Pgp.MessageFlag.get_flag(message) != null) {
- new_message.encryption = Entities.Message.Encryption.PGP;
- }
Conversation conversation = ConversationManager.get_instance(stream_interactor).get_add_conversation(new_message.counterpart, account);
- pre_message_received(new_message, conversation);
+ pre_message_received(new_message, message, conversation);
bool is_uuid = new_message.stanza_id != null && Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", new_message.stanza_id);
if ((is_uuid && !db.contains_message_by_stanza_id(new_message.stanza_id)) ||
@@ -149,43 +147,37 @@ public class MessageManager : StreamInteractionModule, Object {
message.direction = Entities.Message.DIRECTION_SENT;
message.counterpart = conversation.counterpart;
message.ourpart = new Jid(conversation.account.bare_jid.to_string() + "/" + conversation.account.resourcepart);
+ message.marked = Entities.Message.Marked.UNSENT;
+ message.encryption = conversation.encryption;
- if (conversation.encryption == Conversation.Encryption.PGP) {
- message.encryption = Entities.Message.Encryption.PGP;
- }
+ out_message_created(message, conversation);
return message;
}
private void send_xmpp_message(Entities.Message message, Conversation conversation, bool delayed = false) {
- Core.XmppStream stream = stream_interactor.get_stream(conversation.account);
- message.marked = Entities.Message.Marked.NONE;
- if (stream != null) {
- Xmpp.Message.Stanza new_message = new Xmpp.Message.Stanza(message.stanza_id);
- new_message.to = message.counterpart.to_string();
- new_message.body = message.body;
- if (conversation.type_ == Conversation.Type.GROUPCHAT) {
- new_message.type_ = Xmpp.Message.Stanza.TYPE_GROUPCHAT;
- } else {
- new_message.type_ = Xmpp.Message.Stanza.TYPE_CHAT;
- }
- if (message.encryption == Entities.Message.Encryption.PGP) {
- string? key_id = PgpManager.get_instance(stream_interactor).get_key_id(conversation.account, message.counterpart);
- if (key_id != null) {
- bool encrypted = stream.get_module(Xep.Pgp.Module.IDENTITY).encrypt(new_message, key_id);
- if (!encrypted) {
- message.marked = Entities.Message.Marked.WONTSEND;
- return;
- }
+ lock (messages) {
+ Core.XmppStream stream = stream_interactor.get_stream(conversation.account);
+ message.marked = Entities.Message.Marked.NONE;
+ if (stream != null) {
+ Xmpp.Message.Stanza new_message = new Xmpp.Message.Stanza(message.stanza_id);
+ new_message.to = message.counterpart.to_string();
+ new_message.body = message.body;
+ if (conversation.type_ == Conversation.Type.GROUPCHAT) {
+ new_message.type_ = Xmpp.Message.Stanza.TYPE_GROUPCHAT;
+ } else {
+ new_message.type_ = Xmpp.Message.Stanza.TYPE_CHAT;
}
+ pre_message_send(message, new_message, conversation);
+ if (message.marked == Entities.Message.Marked.UNSENT || message.marked == Entities.Message.Marked.WONTSEND) return;
+ if (delayed) {
+ stream.get_module(Xmpp.Xep.DelayedDelivery.Module.IDENTITY).set_message_delay(new_message, message.time);
+ }
+ stream.get_module(Xmpp.Message.Module.IDENTITY).send_message(stream, new_message);
+ message.stanza_id = new_message.id;
+ message.stanza = new_message;
+ } else {
+ message.marked = Entities.Message.Marked.UNSENT;
}
- if (delayed) {
- stream.get_module(Xmpp.Xep.DelayedDelivery.Module.IDENTITY).set_message_delay(new_message, message.time);
- }
- stream.get_module(Xmpp.Message.Module.IDENTITY).send_message(stream, new_message);
- message.stanza_id = new_message.id;
- message.stanza = new_message;
- } else {
- message.marked = Entities.Message.Marked.UNSENT;
}
}
}
diff --git a/libdino/src/service/muc_manager.vala b/libdino/src/service/muc_manager.vala
index 8e894211..c3aaa727 100644
--- a/libdino/src/service/muc_manager.vala
+++ b/libdino/src/service/muc_manager.vala
@@ -161,7 +161,7 @@ public class MucManager : StreamInteractionModule, Object {
if (stream != null) stream.get_module(Xep.Bookmarks.Module.IDENTITY).get_conferences(stream, new BookmarksRetrieveResponseListener(this, account));
}
- private void on_pre_message_received(Entities.Message message, Conversation conversation) {
+ private void on_pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) {
if (conversation.type_ != Conversation.Type.GROUPCHAT) return;
Core.XmppStream stream = stream_interactor.get_stream(conversation.account);
if (stream == null) return;
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) {
diff --git a/libdino/src/service/stream_interactor.vala b/libdino/src/service/stream_interactor.vala
index 5965cdd3..c085401a 100644
--- a/libdino/src/service/stream_interactor.vala
+++ b/libdino/src/service/stream_interactor.vala
@@ -64,7 +64,7 @@ public class StreamInteractor {
}
public interface StreamInteractionModule : Object {
- internal abstract string get_id();
+ public abstract string get_id();
}
} \ No newline at end of file