2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
using Xmpp;
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino {
|
|
|
|
|
2017-04-04 13:47:00 +00:00
|
|
|
public class MessageProcessor : StreamInteractionModule, Object {
|
|
|
|
public static ModuleIdentity<MessageProcessor> IDENTITY = new ModuleIdentity<MessageProcessor>("message_manager");
|
2017-03-19 11:55:36 +00:00
|
|
|
public string id { get { return IDENTITY.id; } }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-11 00:25:45 +00:00
|
|
|
public signal void pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
public signal void message_received(Entities.Message message, Conversation conversation);
|
2017-03-11 00:25:45 +00:00
|
|
|
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);
|
2017-03-02 14:37:32 +00:00
|
|
|
public signal void message_sent(Entities.Message message, Conversation conversation);
|
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
private Database db;
|
2017-04-04 13:47:00 +00:00
|
|
|
private Object lock_send_unsent;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public static void start(StreamInteractor stream_interactor, Database db) {
|
2017-04-04 13:47:00 +00:00
|
|
|
MessageProcessor m = new MessageProcessor(stream_interactor, db);
|
2017-03-02 14:37:32 +00:00
|
|
|
stream_interactor.add_module(m);
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:47:00 +00:00
|
|
|
private MessageProcessor(StreamInteractor stream_interactor, Database db) {
|
2017-03-02 14:37:32 +00:00
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
this.db = db;
|
|
|
|
stream_interactor.account_added.connect(on_account_added);
|
2017-03-09 14:34:32 +00:00
|
|
|
stream_interactor.connection_manager.connection_state_changed.connect((account, state) => {
|
|
|
|
if (state == ConnectionManager.ConnectionState.CONNECTED) send_unsent_messages(account);
|
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void send_message(string text, Conversation conversation) {
|
2017-03-09 14:34:32 +00:00
|
|
|
Entities.Message message = create_out_message(text, conversation);
|
2017-04-04 13:47:00 +00:00
|
|
|
stream_interactor.get_module(MessageStorage.IDENTITY).add_message(message, conversation);
|
2017-03-09 14:34:32 +00:00
|
|
|
send_xmpp_message(message, conversation);
|
2017-03-02 14:37:32 +00:00
|
|
|
message_sent(message, conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void on_account_added(Account account) {
|
2017-03-10 20:45:56 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xmpp.Message.Module.IDENTITY).received_message.connect( (stream, message) => {
|
2017-03-02 14:37:32 +00:00
|
|
|
on_message_received(account, message);
|
|
|
|
});
|
2017-08-16 09:44:42 +00:00
|
|
|
stream_interactor.module_manager.get_module(account, Xmpp.Xep.MessageArchiveManagement.Module.IDENTITY).feature_available.connect( (stream) => {
|
2017-08-17 22:46:40 +00:00
|
|
|
DateTime start_time = account.mam_earliest_synced.to_unix() > 60 ? account.mam_earliest_synced.add_minutes(-1) : account.mam_earliest_synced;
|
|
|
|
stream.get_module(Xep.MessageArchiveManagement.Module.IDENTITY).query_archive(stream, null, start_time, null);
|
2017-08-16 09:44:42 +00:00
|
|
|
});
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void send_unsent_messages(Account account) {
|
|
|
|
Gee.List<Entities.Message> unsend_messages = db.get_unsend_messages(account);
|
|
|
|
foreach (Entities.Message message in unsend_messages) {
|
2017-03-19 11:55:36 +00:00
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(message.counterpart, account);
|
2017-03-13 20:54:12 +00:00
|
|
|
if (conversation != null) {
|
|
|
|
send_xmpp_message(message, conversation, true);
|
|
|
|
}
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void on_message_received(Account account, Xmpp.Message.Stanza message) {
|
|
|
|
if (message.body == null) return;
|
|
|
|
|
2017-03-30 23:17:01 +00:00
|
|
|
Entities.Message new_message = create_in_message(account, message);
|
|
|
|
|
|
|
|
determine_message_type(account, message, new_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Entities.Message create_in_message(Account account, Xmpp.Message.Stanza message) {
|
|
|
|
Entities.Message new_message = new Entities.Message(message.body);
|
2017-03-02 14:37:32 +00:00
|
|
|
new_message.account = account;
|
|
|
|
new_message.stanza_id = message.id;
|
|
|
|
Jid from_jid = new Jid(message.from);
|
|
|
|
if (!account.bare_jid.equals_bare(from_jid) ||
|
2017-06-11 11:59:24 +00:00
|
|
|
from_jid.equals(stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(from_jid.bare_jid, account))) {
|
2017-03-02 14:37:32 +00:00
|
|
|
new_message.direction = Entities.Message.DIRECTION_RECEIVED;
|
|
|
|
} else {
|
|
|
|
new_message.direction = Entities.Message.DIRECTION_SENT;
|
|
|
|
}
|
|
|
|
new_message.counterpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.to) : new Jid(message.from);
|
|
|
|
new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.from) : new Jid(message.to);
|
|
|
|
new_message.stanza = message;
|
2017-08-16 09:44:42 +00:00
|
|
|
|
|
|
|
Xep.MessageArchiveManagement.MessageFlag? mam_message_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message);
|
|
|
|
if (mam_message_flag != null) new_message.local_time = mam_message_flag.server_time;
|
|
|
|
if (new_message.local_time == null || new_message.local_time.compare(new DateTime.now_local()) > 0) new_message.local_time = new DateTime.now_local();
|
|
|
|
|
|
|
|
Xep.DelayedDelivery.MessageFlag? delayed_message_flag = Xep.DelayedDelivery.MessageFlag.get_flag(message);
|
|
|
|
if (delayed_message_flag != null) new_message.time = delayed_message_flag.datetime;
|
|
|
|
if (new_message.time == null || new_message.time.compare(new_message.local_time) > 0) new_message.time = new_message.local_time;
|
|
|
|
|
2017-03-30 23:17:01 +00:00
|
|
|
return new_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void process_message(Entities.Message new_message, Xmpp.Message.Stanza stanza) {
|
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_for_message(new_message);
|
|
|
|
if (conversation != null) {
|
|
|
|
pre_message_received(new_message, stanza, 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, conversation.account)) ||
|
|
|
|
(!is_uuid && !db.contains_message(new_message, conversation.account))) {
|
2017-04-04 13:47:00 +00:00
|
|
|
stream_interactor.get_module(MessageStorage.IDENTITY).add_message(new_message, conversation);
|
2017-08-17 22:46:40 +00:00
|
|
|
|
|
|
|
bool is_mam_message = Xep.MessageArchiveManagement.MessageFlag.get_flag(stanza) != null;
|
|
|
|
bool is_recent = new_message.local_time.compare(new DateTime.now_utc().add_hours(-24)) > 0;
|
|
|
|
if (!is_mam_message || is_recent) {
|
|
|
|
if (new_message.direction == Entities.Message.DIRECTION_SENT) {
|
|
|
|
message_sent(new_message, conversation);
|
|
|
|
} else {
|
|
|
|
message_received(new_message, conversation);
|
|
|
|
}
|
2017-03-30 23:17:01 +00:00
|
|
|
}
|
2017-08-16 09:44:42 +00:00
|
|
|
|
|
|
|
Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
|
|
|
|
Xep.MessageArchiveManagement.Flag? mam_flag = stream != null ? stream.get_flag(Xep.MessageArchiveManagement.Flag.IDENTITY) : null;
|
2017-08-17 22:46:40 +00:00
|
|
|
if (is_mam_message || (mam_flag != null && mam_flag.cought_up == true)) {
|
2017-08-16 09:44:42 +00:00
|
|
|
conversation.account.mam_earliest_synced = new_message.local_time;
|
|
|
|
}
|
2017-03-30 23:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void determine_message_type(Account account, Xmpp.Message.Stanza message_stanza, Entities.Message message) {
|
|
|
|
if (message_stanza.type_ == Xmpp.Message.Stanza.TYPE_GROUPCHAT) {
|
|
|
|
message.type_ = Entities.Message.Type.GROUPCHAT;
|
|
|
|
process_message(message, message_stanza);
|
|
|
|
} else if (message_stanza.type_ == Xmpp.Message.Stanza.TYPE_CHAT) {
|
|
|
|
Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(message.counterpart.bare_jid, account);
|
|
|
|
if (conversation != null) {
|
|
|
|
if (conversation.type_ == Conversation.Type.CHAT) {
|
|
|
|
message.type_ = Entities.Message.Type.CHAT;
|
|
|
|
} else if (conversation.type_ == Conversation.Type.GROUPCHAT) {
|
|
|
|
message.type_ = Entities.Message.Type.GROUPCHAT_PM;
|
|
|
|
}
|
2017-05-02 21:21:00 +00:00
|
|
|
process_message(message, message_stanza);
|
2017-03-02 14:37:32 +00:00
|
|
|
} else {
|
2017-03-30 23:17:01 +00:00
|
|
|
Core.XmppStream stream = stream_interactor.get_stream(account);
|
2017-06-13 16:14:59 +00:00
|
|
|
if (stream != null) stream.get_module(Xep.ServiceDiscovery.Module.IDENTITY).get_entity_categories(stream, message.counterpart.bare_jid.to_string(), (stream, identities) => {
|
2017-03-30 23:17:01 +00:00
|
|
|
if (identities == null) {
|
2017-06-13 16:14:59 +00:00
|
|
|
message.type_ = Entities.Message.Type.CHAT;
|
|
|
|
process_message(message, message_stanza);
|
2017-03-30 23:17:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach (Xep.ServiceDiscovery.Identity identity in identities) {
|
|
|
|
if (identity.category == Xep.ServiceDiscovery.Identity.CATEGORY_CONFERENCE) {
|
2017-06-13 16:14:59 +00:00
|
|
|
message.type_ = Entities.Message.Type.GROUPCHAT_PM;
|
2017-03-30 23:17:01 +00:00
|
|
|
} else {
|
2017-06-13 16:14:59 +00:00
|
|
|
message.type_ = Entities.Message.Type.CHAT;
|
2017-03-30 23:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-02 21:21:00 +00:00
|
|
|
process_message(message, message_stanza);
|
2017-06-13 16:14:59 +00:00
|
|
|
});
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 14:34:32 +00:00
|
|
|
private Entities.Message create_out_message(string text, Conversation conversation) {
|
2017-03-30 23:17:01 +00:00
|
|
|
Entities.Message message = new Entities.Message(text);
|
2017-04-04 13:47:00 +00:00
|
|
|
message.type_ = Util.get_message_type_for_conversation(conversation);
|
2017-03-10 15:02:32 +00:00
|
|
|
message.stanza_id = random_uuid();
|
2017-03-09 14:34:32 +00:00
|
|
|
message.account = conversation.account;
|
|
|
|
message.body = text;
|
2017-03-24 21:57:05 +00:00
|
|
|
message.time = new DateTime.now_local();
|
|
|
|
message.local_time = new DateTime.now_local();
|
2017-03-09 14:34:32 +00:00
|
|
|
message.direction = Entities.Message.DIRECTION_SENT;
|
|
|
|
message.counterpart = conversation.counterpart;
|
2017-08-23 18:30:44 +00:00
|
|
|
if (conversation.type_ in new Conversation.Type[]{Conversation.Type.GROUPCHAT, Conversation.Type.GROUPCHAT_PM}) {
|
|
|
|
message.ourpart = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account);
|
|
|
|
message.real_jid = conversation.account.bare_jid;
|
|
|
|
} else {
|
|
|
|
message.ourpart = new Jid.with_resource(conversation.account.bare_jid.to_string(), conversation.account.resourcepart);
|
|
|
|
}
|
2017-03-11 00:25:45 +00:00
|
|
|
message.marked = Entities.Message.Marked.UNSENT;
|
|
|
|
message.encryption = conversation.encryption;
|
2017-03-09 14:34:32 +00:00
|
|
|
|
2017-03-11 00:25:45 +00:00
|
|
|
out_message_created(message, conversation);
|
2017-03-09 14:34:32 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2017-03-11 00:29:38 +00:00
|
|
|
public void send_xmpp_message(Entities.Message message, Conversation conversation, bool delayed = false) {
|
2017-04-04 13:47:00 +00:00
|
|
|
lock (lock_send_unsent) {
|
2017-03-11 00:25:45 +00:00
|
|
|
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;
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
2017-03-11 00:25:45 +00:00
|
|
|
pre_message_send(message, new_message, conversation);
|
|
|
|
if (message.marked == Entities.Message.Marked.UNSENT || message.marked == Entities.Message.Marked.WONTSEND) return;
|
|
|
|
if (delayed) {
|
2017-03-12 12:19:04 +00:00
|
|
|
Xmpp.Xep.DelayedDelivery.Module.set_message_delay(new_message, message.time);
|
2017-03-11 00:25:45 +00:00
|
|
|
}
|
|
|
|
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;
|
2017-03-09 14:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 16:14:59 +00:00
|
|
|
}
|