From 2002c3cf74dafc24e949b32c67b7086295f0e841 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <philipp@hoerist.com>
Date: Fri, 19 Oct 2018 20:37:18 +0200
Subject: [PATCH] Use parse_datetime for parsing idle time

Fixes #9393
---
 gajim/common/connection_handlers_events.py |  8 ++------
 gajim/common/modules/misc.py               | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py
index 78711cdb00..40c0bb6961 100644
--- a/gajim/common/connection_handlers_events.py
+++ b/gajim/common/connection_handlers_events.py
@@ -30,6 +30,7 @@ from gajim.common import app
 from gajim.common import i18n
 from gajim.common.i18n import _
 from gajim.common.modules import dataforms
+from gajim.common.modules.misc import parse_idle
 from gajim.common.const import KindConstant, SSLError
 from gajim.common.pep import SUPPORTED_PERSONAL_USER_EVENTS
 from gajim.common.jingle_transport import JingleTransportSocks5
@@ -254,12 +255,7 @@ PresenceHelperEvent):
         if delay_tag:
             self._generate_timestamp(self.stanza.timestamp)
         # XEP-0319
-        self.idle_time = None
-        idle_tag = self.stanza.getTag('idle', namespace=nbxmpp.NS_IDLE)
-        if idle_tag:
-            time_str = idle_tag.getAttr('since')
-            tim = helpers.datetime_tuple(time_str)
-            self.idle_time = timegm(tim)
+        self.idle_time = parse_idle(self.stanza)
 
         xtags = self.stanza.getTags('x')
         for x in xtags:
diff --git a/gajim/common/modules/misc.py b/gajim/common/modules/misc.py
index b0719caa72..1c8633e109 100644
--- a/gajim/common/modules/misc.py
+++ b/gajim/common/modules/misc.py
@@ -139,3 +139,24 @@ def parse_xhtml(stanza):
     if app.config.get('ignore_incoming_xhtml'):
         return None
     return stanza.getXHTML()
+
+
+# XEP-0319: Last User Interaction in Presence
+
+def parse_idle(stanza):
+    idle_tag = stanza.getTag('idle', namespace=nbxmpp.NS_IDLE)
+    if idle_tag is None:
+        return
+
+    since = idle_tag.getAttr('since')
+    if since is None:
+        log.warning('No since attr in idle node')
+        log.warning(stanza)
+        return
+
+    timestamp = parse_datetime(since, convert='utc', epoch=True)
+    if timestamp is None:
+        log.warning('Invalid timestamp received: %s', since)
+        log.warning(stanza)
+
+    return timestamp
-- 
GitLab