From 651611b28bba4e1f60016bc0e929b3a68e45cb1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 30 Jul 2017 23:20:20 +0200 Subject: [PATCH] Improve timestamp usage for MAM - Use new parse_datetime() method - Drop message with error if MAM doesnt supply a timestamp. - If the user supplys an own timestamp, save it so we can decide in the future how to display it. --- gajim/common/connection_handlers_events.py | 30 ++++++++++++++-------- gajim/common/helpers.py | 28 -------------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index a5425d35e..8bdc03cf7 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -1059,7 +1059,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): if frm.bareMatch(own_jid): self.stanza_id = self.msg_.getOriginID() - if not self.stanza_id: + if self.stanza_id is None: self.stanza_id = self.msg_.getID() self.with_ = to @@ -1073,20 +1073,30 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): self.with_ = frm self.kind = KindConstant.CHAT_MSG_RECV - if not self.stanza_id: + if self.stanza_id is None: log.debug('Could not retrieve stanza-id') - # 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: + delay = self.forwarded.getTagAttr( + 'delay', 'stamp', namespace=nbxmpp.NS_DELAY2) + if delay is None: log.error('Received MAM message without timestamp') return - self.timestamp = helpers.parse_delay(delay) + self.timestamp = helpers.parse_datetime( + delay, check_utc=True, epoch=True) + if self.timestamp is None: + log.error('Received MAM message with invalid timestamp: %s', delay) + return + + # Save timestamp added by the user + user_delay = self.msg_.getTagAttr( + 'delay', 'stamp', namespace=nbxmpp.NS_DELAY2) + if user_delay is not None: + self.user_timestamp = helpers.parse_datetime( + user_delay, check_utc=True, epoch=True) + if self.user_timestamp is None: + log.warning('Received MAM message with ' + 'invalid user timestamp: %s', user_delay) log.debug('Received mam-message: stanza id: %s', self.stanza_id) return True diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py index 9bc28900d..fb5e076ff 100644 --- a/gajim/common/helpers.py +++ b/gajim/common/helpers.py @@ -673,34 +673,6 @@ def datetime_tuple(timestamp): tim = tim.timetuple() return tim -def parse_delay(timestamp): - ''' - Parse a timestamp - https://xmpp.org/extensions/xep-0203.html - Note: Not all delay tags should be parsed with this method - see https://xmpp.org/extensions/xep-0082.html for more information - - :param timestamp: a XEP-0203 fomated timestring string or a delay Node - - Examples: - '2017-11-05T01:41:20Z' - '2017-11-05T01:41:20.123Z' - - return epoch UTC timestamp - ''' - if isinstance(timestamp, nbxmpp.protocol.Node): - timestamp = timestamp.getAttr('stamp') - timestamp += '+0000' - try: - datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ%z') - except ValueError: - try: - datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ%z') - except ValueError: - log.error('Could not parse delay timestamp: %s', timestamp) - raise - return datetime_.timestamp() - def parse_datetime(timestring, check_utc=False, convert='utc', epoch=False): ''' Parse a XEP-0082 DateTime Profile String