Preparation for MUC Archive querys
- Refactor and clean up code around MamMessageReceivedEvent - Goal is to add a GcMamMessageReceivedEvent later on - For that cause added a raw-mam-message-received base event
This commit is contained in:
parent
aefb571168
commit
33a51f3180
2 changed files with 75 additions and 38 deletions
|
@ -44,6 +44,7 @@ from common.logger import LOG_DB_PATH
|
||||||
from common.pep import SUPPORTED_PERSONAL_USER_EVENTS
|
from common.pep import SUPPORTED_PERSONAL_USER_EVENTS
|
||||||
from common.jingle_transport import JingleTransportSocks5
|
from common.jingle_transport import JingleTransportSocks5
|
||||||
from common.file_props import FilesProp
|
from common.file_props import FilesProp
|
||||||
|
from common.nec import NetworkEvent
|
||||||
|
|
||||||
if gajim.HAVE_PYOPENSSL:
|
if gajim.HAVE_PYOPENSSL:
|
||||||
import OpenSSL.crypto
|
import OpenSSL.crypto
|
||||||
|
@ -1037,43 +1038,70 @@ class BeforeChangeShowEvent(nec.NetworkIncomingEvent):
|
||||||
|
|
||||||
class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
||||||
name = 'mam-message-received'
|
name = 'mam-message-received'
|
||||||
base_network_events = []
|
base_network_events = ['raw-mam-message-received']
|
||||||
|
|
||||||
def init(self):
|
def __init__(self, name, base_event):
|
||||||
|
'''
|
||||||
|
Pre-Generated attributes on self:
|
||||||
|
|
||||||
|
:conn: Connection instance
|
||||||
|
:stanza: Complete stanza Node
|
||||||
|
:forwarded: Forwarded Node
|
||||||
|
:result: Result Node
|
||||||
|
'''
|
||||||
|
self._set_base_event_vars_as_attributes(base_event)
|
||||||
self.additional_data = {}
|
self.additional_data = {}
|
||||||
self.encrypted = False
|
self.encrypted = False
|
||||||
|
self.groupchat = False
|
||||||
|
|
||||||
def generate(self):
|
def generate(self):
|
||||||
if not self.stanza:
|
archive_jid = self.stanza.getFrom()
|
||||||
return
|
own_jid = self.conn.get_own_jid()
|
||||||
account = self.conn.name
|
if archive_jid and not archive_jid.bareMatch(own_jid):
|
||||||
self.msg_ = self.stanza.getTag('message')
|
# MAM Message not from our Archive
|
||||||
# use timestamp of archived message, if available and archive timestamp otherwise
|
log.info('MAM message not from our user archive')
|
||||||
delay = self.stanza.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
return False
|
||||||
delay2 = self.msg_.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
|
||||||
if delay2:
|
self.msg_ = self.forwarded.getTag('message')
|
||||||
delay = delay2
|
|
||||||
if not delay:
|
if self.msg_.getType() == 'groupchat':
|
||||||
return
|
log.info('Received groupchat message from user archive')
|
||||||
tim = delay.getAttr('stamp')
|
return False
|
||||||
tim = helpers.datetime_tuple(tim)
|
|
||||||
self.tim = timegm(tim)
|
|
||||||
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')
|
self.msgtxt = self.msg_.getTagData('body')
|
||||||
if to_ == gajim.get_jid_from_account(account):
|
self.stanza_id = self.msg_.getID()
|
||||||
self.with_ = frm_
|
self.mam_id = self.result.getID()
|
||||||
|
self.query_id = self.result.getAttr('queryid')
|
||||||
|
|
||||||
|
# Use timestamp provided by archive,
|
||||||
|
# Fallback: Use timestamp provided by user and issue a warning
|
||||||
|
delay = self.forwarded.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
||||||
|
if not delay:
|
||||||
|
log.warning('No timestamp on archive Message, try fallback')
|
||||||
|
delay = self.msg_.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
||||||
|
if not delay:
|
||||||
|
log.error('Received MAM message without timestamp')
|
||||||
|
return
|
||||||
|
|
||||||
|
self.timestamp = helpers.parse_delay(delay)
|
||||||
|
|
||||||
|
frm = self.msg_.getFrom()
|
||||||
|
to = self.msg_.getTo()
|
||||||
|
|
||||||
|
if not to or to.bareMatch(own_jid):
|
||||||
|
self.with_ = str(frm)
|
||||||
self.direction = 'from'
|
self.direction = 'from'
|
||||||
self.resource = gajim.get_resource_from_jid(
|
self.resource = frm.getResource()
|
||||||
self.msg_.getAttr('from'))
|
|
||||||
else:
|
else:
|
||||||
self.with_ = to_
|
self.with_ = str(to)
|
||||||
self.direction = 'to'
|
self.direction = 'to'
|
||||||
self.resource = gajim.get_resource_from_jid(self.msg_.getAttr('to'))
|
self.resource = to.getResource()
|
||||||
|
|
||||||
|
log_message = \
|
||||||
|
'received: mam-message: ' \
|
||||||
|
'stanza id: {:15} - mam id: {:15} - query id: {}'.format(
|
||||||
|
self.stanza_id, self.mam_id, self.query_id)
|
||||||
|
log.debug(log_message)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
||||||
|
@ -1087,14 +1115,14 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
||||||
self.additional_data = self.msg_obj.additional_data
|
self.additional_data = self.msg_obj.additional_data
|
||||||
self.with_ = self.msg_obj.with_
|
self.with_ = self.msg_obj.with_
|
||||||
self.direction = self.msg_obj.direction
|
self.direction = self.msg_obj.direction
|
||||||
self.tim = self.msg_obj.tim
|
self.timestamp = self.msg_obj.timestamp
|
||||||
res = self.msg_obj.resource
|
res = self.msg_obj.resource
|
||||||
self.msgtxt = self.msg_obj.msgtxt
|
self.msgtxt = self.msg_obj.msgtxt
|
||||||
is_pm = gajim.logger.jid_is_room_jid(self.with_)
|
is_pm = gajim.logger.jid_is_room_jid(self.with_)
|
||||||
if msg_.getAttr('type') == 'groupchat':
|
if msg_.getAttr('type') == 'groupchat':
|
||||||
if is_pm == False:
|
if is_pm == False:
|
||||||
log.warn('JID %s is marked as normal contact in database '
|
log.warn('JID %s is marked as normal contact in database '
|
||||||
'but we got a groupchat message from it.')
|
'but we got a groupchat message from it.', self.with_)
|
||||||
return
|
return
|
||||||
if is_pm == None:
|
if is_pm == None:
|
||||||
gajim.logger.get_jid_id(self.with_, 'ROOM')
|
gajim.logger.get_jid_id(self.with_, 'ROOM')
|
||||||
|
@ -1105,12 +1133,12 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
||||||
server = gajim.get_server_from_jid(self.with_)
|
server = gajim.get_server_from_jid(self.with_)
|
||||||
if server not in self.conn.mam_awaiting_disco_result:
|
if server not in self.conn.mam_awaiting_disco_result:
|
||||||
self.conn.mam_awaiting_disco_result[server] = [
|
self.conn.mam_awaiting_disco_result[server] = [
|
||||||
[self.with_, self.direction, self.tim, self.msgtxt,
|
[self.with_, self.direction, self.timestamp, self.msgtxt,
|
||||||
res]]
|
res]]
|
||||||
self.conn.discoverInfo(server)
|
self.conn.discoverInfo(server)
|
||||||
else:
|
else:
|
||||||
self.conn.mam_awaiting_disco_result[server].append(
|
self.conn.mam_awaiting_disco_result[server].append(
|
||||||
[self.with_, self.direction, self.tim, self.msgtxt,
|
[self.with_, self.direction, self.timestamp, self.msgtxt,
|
||||||
res])
|
res])
|
||||||
return
|
return
|
||||||
return True
|
return True
|
||||||
|
@ -1218,8 +1246,16 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
||||||
nbxmpp.NS_MAM_1,
|
nbxmpp.NS_MAM_1,
|
||||||
nbxmpp.NS_MAM_2):
|
nbxmpp.NS_MAM_2):
|
||||||
forwarded = result.getTag('forwarded', namespace=nbxmpp.NS_FORWARD)
|
forwarded = result.getTag('forwarded', namespace=nbxmpp.NS_FORWARD)
|
||||||
gajim.nec.push_incoming_event(MamMessageReceivedEvent(None,
|
if not forwarded:
|
||||||
conn=self.conn, stanza=forwarded, query_id=result.getAttr('queryid')))
|
log.warning('Invalid MAM Message: no forwarded child')
|
||||||
|
return
|
||||||
|
|
||||||
|
gajim.nec.push_incoming_event(
|
||||||
|
NetworkEvent('raw-mam-message-received',
|
||||||
|
conn=self.conn,
|
||||||
|
stanza=self.stanza,
|
||||||
|
forwarded=forwarded,
|
||||||
|
result=result))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Mediated invitation?
|
# Mediated invitation?
|
||||||
|
|
|
@ -22,7 +22,7 @@ import nbxmpp
|
||||||
from common import gajim
|
from common import gajim
|
||||||
from common import ged
|
from common import ged
|
||||||
from common import helpers
|
from common import helpers
|
||||||
from common.connection_handlers_events import ArchivingReceivedEvent
|
import common.connection_handlers_events as ev
|
||||||
|
|
||||||
from calendar import timegm
|
from calendar import timegm
|
||||||
from time import localtime
|
from time import localtime
|
||||||
|
@ -48,6 +48,7 @@ class ConnectionArchive313(ConnectionArchive):
|
||||||
self.iq_answer = []
|
self.iq_answer = []
|
||||||
self.mam_query_date = None
|
self.mam_query_date = None
|
||||||
self.mam_query_id = None
|
self.mam_query_id = None
|
||||||
|
gajim.nec.register_incoming_event(ev.MamMessageReceivedEvent)
|
||||||
gajim.ged.register_event_handler('archiving-finished-legacy', ged.CORE,
|
gajim.ged.register_event_handler('archiving-finished-legacy', ged.CORE,
|
||||||
self._nec_result_finished)
|
self._nec_result_finished)
|
||||||
gajim.ged.register_event_handler('archiving-finished', ged.CORE,
|
gajim.ged.register_event_handler('archiving-finished', ged.CORE,
|
||||||
|
@ -132,7 +133,7 @@ class ConnectionArchive313(ConnectionArchive):
|
||||||
def _nec_mam_decrypted_message_received(self, obj):
|
def _nec_mam_decrypted_message_received(self, obj):
|
||||||
if obj.conn.name != self.name:
|
if obj.conn.name != self.name:
|
||||||
return
|
return
|
||||||
gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.tim,
|
gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.timestamp,
|
||||||
msg=obj.msgtxt, nick=obj.nick, additional_data=obj.additional_data)
|
msg=obj.msgtxt, nick=obj.nick, additional_data=obj.additional_data)
|
||||||
|
|
||||||
def get_query_id(self):
|
def get_query_id(self):
|
||||||
|
@ -399,7 +400,7 @@ class ConnectionArchive136(ConnectionArchive):
|
||||||
return ['may']
|
return ['may']
|
||||||
|
|
||||||
def _ArchiveCB(self, con, iq_obj):
|
def _ArchiveCB(self, con, iq_obj):
|
||||||
gajim.nec.push_incoming_event(ArchivingReceivedEvent(None, conn=self,
|
gajim.nec.push_incoming_event(ev.ArchivingReceivedEvent(None, conn=self,
|
||||||
stanza=iq_obj))
|
stanza=iq_obj))
|
||||||
raise nbxmpp.NodeProcessed
|
raise nbxmpp.NodeProcessed
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue