diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index c27012b58..867b8f726 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -893,6 +893,8 @@ class ConnectionHandlersBase: self._nec_gc_presence_received) gajim.ged.register_event_handler('message-received', ged.CORE, self._nec_message_received) + gajim.ged.register_event_handler('mam-message-received', ged.CORE, + self._nec_mam_message_received) gajim.ged.register_event_handler('decrypted-message-received', ged.CORE, self._nec_decrypted_message_received) @@ -905,6 +907,8 @@ class ConnectionHandlersBase: self._nec_gc_presence_received) gajim.ged.remove_event_handler('message-received', ged.CORE, self._nec_message_received) + gajim.ged.remove_event_handler('mam-message-received', ged.CORE, + self._nec_mam_message_received) gajim.ged.remove_event_handler('decrypted-message-received', ged.CORE, self._nec_decrypted_message_received) @@ -1116,14 +1120,46 @@ class ConnectionHandlersBase: gajim.nec.push_incoming_event(DecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) + def _nec_mam_message_received(self, obj): + if obj.conn.name != self.name: + return + if obj.enc_tag and self.USE_GPG: + encmsg = obj.enc_tag.getData() + + keyID = gajim.config.get_per('accounts', self.name, 'keyid') + if keyID: + self.gpg_messages_to_decrypt.append([encmsg, keyID, obj]) + if len(self.gpg_messages_to_decrypt) == 1: + gajim.thread_interface(self.decrypt_thread, [encmsg, keyID, + obj], self._on_mam_message_decrypted, [obj]) + return + gajim.nec.push_incoming_event(MamDecryptedMessageReceivedEvent(None, + conn=self, msg_obj=obj)) + def _on_message_decrypted(self, output, obj): if len(self.gpg_messages_to_decrypt): encmsg, keyID, obj2 = self.gpg_messages_to_decrypt[0] + if type(obj2) == MessageReceivedEvent: + cb = self._on_message_decrypted + else: + cb = self._on_mam_message_decrypted gajim.thread_interface(self.decrypt_thread, [encmsg, keyID, obj2], - self._on_message_decrypted, [obj2]) + cb, [obj2]) gajim.nec.push_incoming_event(DecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) + def _on_mam_message_decrypted(self, output, obj): + if len(self.gpg_messages_to_decrypt): + encmsg, keyID, obj2 = self.gpg_messages_to_decrypt[0] + if type(obj2) == MessageReceivedEvent: + cb = self._on_message_decrypted + else: + cb = self._on_mam_message_decrypted + gajim.thread_interface(self.decrypt_thread, [encmsg, keyID, obj2], + cb, [obj2]) + gajim.nec.push_incoming_event(MamDecryptedMessageReceivedEvent(None, + conn=self, msg_obj=obj)) + def _nec_decrypted_message_received(self, obj): if obj.conn.name != self.name: return diff --git a/src/common/connection_handlers_events.py b/src/common/connection_handlers_events.py index d6bad755f..16257c982 100644 --- a/src/common/connection_handlers_events.py +++ b/src/common/connection_handlers_events.py @@ -1011,6 +1011,77 @@ class BeforeChangeShowEvent(nec.NetworkIncomingEvent): name = 'before-change-show' base_network_events = [] +class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): + name = 'mam-message-received' + base_network_events = [] + + def generate(self): + if not self.stanza: + return + account = self.conn.name + delay = self.stanza.getTag('delay', namespace=nbxmpp.NS_DELAY2) + if not delay: + return + tim = delay.getAttr('stamp') + tim = helpers.datetime_tuple(tim) + self.tim = localtime(timegm(tim)) + self.msg_ = self.stanza.getTag('message') + to_ = self.msg_.getAttr('to') + if to_: + to_ = gajim.get_jid_without_resource(to_) + else: + to_ = gajim.get_jid_from_account(account) + frm_ = gajim.get_jid_without_resource(self.msg_.getAttr('from')) + self.msgtxt = self.msg_.getTagData('body') + if to_ == gajim.get_jid_from_account(account): + self.with_ = frm_ + self.direction = 'from' + self.resource = gajim.get_resource_from_jid( + self.msg_.getAttr('from')) + else: + self.with_ = to_ + self.direction = 'to' + self.resource = gajim.get_resource_from_jid(self.msg_.getAttr('to')) + self.enc_tag = self.msg_.getTag('x', namespace=nbxmpp.NS_ENCRYPTED) + return True + +class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): + name = 'mam-decrypted-message-received' + base_network_events = [] + + def generate(self): + self.nick = None + msg_ = self.msg_obj.msg_ + self.with_ = self.msg_obj.with_ + self.direction = self.msg_obj.direction + self.tim = self.msg_obj.tim + res = self.msg_obj.resource + self.msgtxt = self.msg_obj.msgtxt + is_pm = gajim.logger.jid_is_room_jid(self.with_) + if msg_.getAttr('type') == 'groupchat': + if is_pm == False: + log.warn('JID %s is marked as normal contact in database ' + 'but we got a groupchat message from it.') + return + if is_pm == None: + gajim.logger.get_jid_id(self.with_, 'ROOM') + self.nick = res + else: + if is_pm == None: + # we don't know this JID, we need to disco it. + server = gajim.get_server_from_jid(self.with_) + if server not in self.conn.mam_awaiting_disco_result: + self.conn.mam_awaiting_disco_result[server] = [ + [self.with_, self.direction, self.tim, self.msgtxt, + res]] + self.conn.discoverInfo(server) + else: + self.conn.mam_awaiting_disco_result[server].append( + [self.with_, self.direction, self.tim, self.msgtxt, + res]) + return + return True + class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): name = 'message-received' base_network_events = ['raw-message-received'] @@ -1092,55 +1163,8 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): result = self.stanza.getTag('result', namespace=nbxmpp.NS_MAM) if result: forwarded = result.getTag('forwarded', namespace=nbxmpp.NS_FORWARD) - if not forwarded: - return - delay = forwarded.getTag('delay', namespace=nbxmpp.NS_DELAY2) - if not delay: - return - tim = delay.getAttr('stamp') - tim = helpers.datetime_tuple(tim) - tim = localtime(timegm(tim)) - msg_ = forwarded.getTag('message') - to_ = msg_.getAttr('to') - if to_: - to_ = gajim.get_jid_without_resource(to_) - else: - to_ = gajim.get_jid_from_account(account) - frm_ = gajim.get_jid_without_resource(msg_.getAttr('from')) - nick = None - msg_txt = msg_.getTagData('body') - if to_ == gajim.get_jid_from_account(account): - with_ = frm_ - direction = 'from' - res = gajim.get_resource_from_jid(msg_.getAttr('from')) - else: - with_ = to_ - direction = 'to' - res = gajim.get_resource_from_jid(msg_.getAttr('to')) - is_pm = gajim.logger.jid_is_room_jid(with_) - if msg_.getAttr('type') == 'groupchat': - if is_pm == False: - log.warn('JID %s is marked as normal contact in database ' - 'but we got a groupchat message from it.') - return - if is_pm == None: - gajim.logger.get_jid_id(with_, 'ROOM') - nick = res - else: - if is_pm == None: - # we don't know this JID, we need to disco it. - server = gajim.get_server_from_jid(with_) - if server not in self.conn.mam_awaiting_disco_result: - self.conn.mam_awaiting_disco_result[server] = [ - [with_, direction, tim, msg_txt, res]] - self.conn.discoverInfo(server) - else: - self.conn.mam_awaiting_disco_result[server].append( - [with_, direction, tim, msg_txt, res]) - return - - gajim.logger.save_if_not_exists(with_, direction, tim, - msg=msg_txt, nick=nick) + gajim.nec.push_incoming_event(MamMessageReceivedEvent(None, + conn=self.conn, stanza=forwarded)) return self.enc_tag = self.stanza.getTag('x', namespace=nbxmpp.NS_ENCRYPTED) diff --git a/src/common/message_archiving.py b/src/common/message_archiving.py index 2761601a1..63fadfa60 100644 --- a/src/common/message_archiving.py +++ b/src/common/message_archiving.py @@ -47,10 +47,18 @@ class ConnectionArchive313(ConnectionArchive): self._nec_agent_info_error) gajim.ged.register_event_handler('agent-info-received', ged.CORE, self._nec_agent_info) + gajim.ged.register_event_handler('mam-decrypted-message-received', + ged.CORE, self._nec_mam_decrypted_message_received) def cleanup(self): gajim.ged.remove_event_handler('raw-message-received', ged.CORE, self._nec_raw_message_313_received) + gajim.ged.remove_event_handler('agent-info-error-received', ged.CORE, + self._nec_agent_info_error) + gajim.ged.remove_event_handler('agent-info-received', ged.CORE, + self._nec_agent_info) + gajim.ged.remove_event_handler('mam-decrypted-message-received', + ged.CORE, self._nec_mam_decrypted_message_received) def _nec_agent_info_error(self, obj): if obj.jid in self.mam_awaiting_disco_result: @@ -81,7 +89,7 @@ class ConnectionArchive313(ConnectionArchive): if obj.conn.name != self.name: return - fin_ = obj.stanza.getTag('fin', namespace=nbxmpp.NS_MAM) + fin_ = obj.stanza.getTag('fin', namespace=nbxmpp.NS_MAM) if fin_: queryid_ = fin_.getAttr('queryid') if queryid_ not in self.awaiting_answers: @@ -99,6 +107,14 @@ class ConnectionArchive313(ConnectionArchive): del self.awaiting_answers[queryid_] + def _nec_mam_decrypted_message_received(self, obj): + if obj.conn.name != self.name: + return + print 'ici' + print obj.msgtxt + gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.tim, + msg=obj.msgtxt, nick=obj.nick) + def request_archive(self, start=None, end=None, with_=None, after=None, max=30): iq_ = nbxmpp.Iq('set')