summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0319
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2013-06-07 19:00:22 -0400
committerLance Stout <lancestout@gmail.com>2013-06-07 19:00:22 -0400
commited3a4fb8d443fc4a56419be9624631b6d42356c7 (patch)
treedd1aa40a192f7ba57e4b894cb12b832c2c26508b /sleekxmpp/plugins/xep_0319
parent4a590d1497e332da8b85a6807ad0ba9f277e2cbe (diff)
downloadslixmpp-ed3a4fb8d443fc4a56419be9624631b6d42356c7.tar.gz
slixmpp-ed3a4fb8d443fc4a56419be9624631b6d42356c7.tar.bz2
slixmpp-ed3a4fb8d443fc4a56419be9624631b6d42356c7.tar.xz
slixmpp-ed3a4fb8d443fc4a56419be9624631b6d42356c7.zip
Add support for XEP-0319, idle presence
Diffstat (limited to 'sleekxmpp/plugins/xep_0319')
-rw-r--r--sleekxmpp/plugins/xep_0319/__init__.py16
-rw-r--r--sleekxmpp/plugins/xep_0319/idle.py70
-rw-r--r--sleekxmpp/plugins/xep_0319/stanza.py28
3 files changed, 114 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/xep_0319/__init__.py b/sleekxmpp/plugins/xep_0319/__init__.py
new file mode 100644
index 00000000..4756e63e
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0319/__init__.py
@@ -0,0 +1,16 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+from sleekxmpp.plugins.base import register_plugin
+
+from sleekxmpp.plugins.xep_0319 import stanza
+from sleekxmpp.plugins.xep_0319.stanza import Idle
+from sleekxmpp.plugins.xep_0319.idle import XEP_0319
+
+
+register_plugin(XEP_0319)
diff --git a/sleekxmpp/plugins/xep_0319/idle.py b/sleekxmpp/plugins/xep_0319/idle.py
new file mode 100644
index 00000000..8f3d104b
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0319/idle.py
@@ -0,0 +1,70 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+from datetime import datetime, timedelta
+
+from sleekxmpp.stanza import Presence
+from sleekxmpp.plugins import BasePlugin
+from sleekxmpp.xmlstream import register_stanza_plugin
+from sleekxmpp.xmlstream.handler import Callback
+from sleekxmpp.xmlstream.matcher import StanzaPath
+from sleekxmpp.plugins.xep_0319 import stanza
+
+
+class XEP_0319(BasePlugin):
+ name = 'xep_0319'
+ description = 'XEP-0319: Last User Interaction in Presence'
+ dependencies = set()
+ stanza = stanza
+
+ def plugin_init(self):
+ self._idle_stamps = {}
+ register_stanza_plugin(Presence, stanza.Idle)
+ self.api.register(self._set_idle,
+ 'set_idle',
+ default=True)
+ self.api.register(self._get_idle,
+ 'get_idle',
+ default=True)
+ self.xmpp.register_handler(
+ Callback('Idle Presence',
+ StanzaPath('presence/idle'),
+ self._idle_presence))
+ self.xmpp.add_filter('out', self._stamp_idle_presence)
+
+ def session_bind(self, jid):
+ self.xmpp['xep_0030'].add_feature('urn:xmpp:idle:0')
+
+ def plugin_end(self):
+ self.xmpp['xep_0030'].del_feature(feature='urn:xmpp:idle:0')
+ self.xmpp.del_filter('out', self._stamp_idle_presence)
+ self.xmpp.remove_handler('Idle Presence')
+
+ def idle(self, jid=None, since=None):
+ if since is None:
+ since = datetime.now()
+ self.api['set_idle'](jid, None, None, since)
+
+ def active(self, jid=None):
+ self.api['set_idle'](jid, None, None, None)
+
+ def _set_idle(self, jid, node, ifrom, data):
+ self._idle_stamps[jid] = data
+
+ def _get_idle(self, jid, node, ifrom, data):
+ return self._idle_stamps.get(jid, None)
+
+ def _idle_presence(self, pres):
+ self.xmpp.event('presence_idle', pres)
+
+ def _stamp_idle_presence(self, stanza):
+ if isinstance(stanza, Presence):
+ since = self.api['get_idle'](stanza['from'] or self.xmpp.boundjid)
+ if since:
+ stanza['idle']['since'] = since
+ return stanza
diff --git a/sleekxmpp/plugins/xep_0319/stanza.py b/sleekxmpp/plugins/xep_0319/stanza.py
new file mode 100644
index 00000000..a0bb7272
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0319/stanza.py
@@ -0,0 +1,28 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import datetime as dt
+
+from sleekxmpp.xmlstream import ElementBase
+from sleekxmpp.plugins import xep_0082
+
+
+class Idle(ElementBase):
+ name = 'idle'
+ namespace = 'urn:xmpp:idle:0'
+ plugin_attrib = 'idle'
+ interfaces = set(['since'])
+
+ def get_since(self):
+ timestamp = self._get_attr('since')
+ return xep_0082.parse(timestamp)
+
+ def set_since(self, value):
+ if isinstance(value, dt.datetime):
+ value = xep_0082.format_datetime(value)
+ self._set_attr('since', value)