summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0256.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-06-22 23:17:15 -0700
committerLance Stout <lancestout@gmail.com>2012-06-22 23:17:15 -0700
commit5d6019a962fd1a30ed04520892836cdecc7fe19f (patch)
tree6ee7b956a813af2f0507867dc861f4aead3479e2 /sleekxmpp/plugins/xep_0256.py
parenteb5df1aa3722e6e85b9683805b4088f4340918aa (diff)
parent85ef2d8d0bcc92cd20d857d01bcf1bba56bc8edf (diff)
downloadslixmpp-5d6019a962fd1a30ed04520892836cdecc7fe19f.tar.gz
slixmpp-5d6019a962fd1a30ed04520892836cdecc7fe19f.tar.bz2
slixmpp-5d6019a962fd1a30ed04520892836cdecc7fe19f.tar.xz
slixmpp-5d6019a962fd1a30ed04520892836cdecc7fe19f.zip
Merge branch 'master' into develop
Diffstat (limited to 'sleekxmpp/plugins/xep_0256.py')
-rw-r--r--sleekxmpp/plugins/xep_0256.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/sleekxmpp/plugins/xep_0256.py b/sleekxmpp/plugins/xep_0256.py
new file mode 100644
index 00000000..265a5da8
--- /dev/null
+++ b/sleekxmpp/plugins/xep_0256.py
@@ -0,0 +1,67 @@
+"""
+ SleekXMPP: The Sleek XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of SleekXMPP.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from sleekxmpp import Presence
+from sleekxmpp.plugins import BasePlugin, register_plugin
+from sleekxmpp.xmlstream import register_stanza_plugin
+
+from sleekxmpp.plugins.xep_0012 import stanza, LastActivity
+
+
+log = logging.getLogger(__name__)
+
+
+class XEP_0256(BasePlugin):
+
+ name = 'xep_0256'
+ description = 'XEP-0256: Last Activity in Presence'
+ dependencies = set(['xep_0012'])
+ stanza = stanza
+
+ def plugin_init(self):
+ self.auto_last_activity = self.config.get('auto_last_activity', False)
+
+ register_stanza_plugin(Presence, LastActivity)
+
+ self.xmpp.add_filter('out', self._initial_presence_activity)
+ self.xmpp.add_event_handler('connected', self._reset_presence_activity)
+
+ self._initial_presence = set()
+
+ def _reset_presence_activity(self, e):
+ self._initial_presence = set()
+
+ def _initial_presence_activity(self, stanza):
+ if isinstance(stanza, Presence):
+ use_last_activity = False
+
+ if self.auto_last_activity and stanza['show'] in ('xa', 'away'):
+ use_last_activity = True
+
+ if stanza['from'] not in self._initial_presence:
+ self._initial_presence.add(stanza['from'])
+ use_last_activity = True
+
+ if use_last_activity:
+ plugin = self.xmpp['xep_0012']
+ try:
+ result = plugin.api['get_last_activity'](stanza['from'],
+ None,
+ stanza['to'])
+ seconds = result['last_activity']['seconds']
+ except XMPPError:
+ seconds = None
+
+ if seconds is not None:
+ stanza['last_activity']['seconds'] = seconds
+ return stanza
+
+
+register_plugin(XEP_0256)