summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0256.py
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2014-07-17 14:19:04 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-07-17 14:19:04 +0200
commit5ab77c745270d7d5c016c1dc7ef2a82533a4b16e (patch)
tree259377cc666f8b9c7954fc4e7b8f7a912bcfe101 /slixmpp/plugins/xep_0256.py
parente5582694c07236e6830c20361840360a1dde37f3 (diff)
downloadslixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.gz
slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.bz2
slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.tar.xz
slixmpp-5ab77c745270d7d5c016c1dc7ef2a82533a4b16e.zip
Rename to slixmpp
Diffstat (limited to 'slixmpp/plugins/xep_0256.py')
-rw-r--r--slixmpp/plugins/xep_0256.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/slixmpp/plugins/xep_0256.py b/slixmpp/plugins/xep_0256.py
new file mode 100644
index 00000000..4ad4f0ea
--- /dev/null
+++ b/slixmpp/plugins/xep_0256.py
@@ -0,0 +1,73 @@
+"""
+ Slixmpp: The Slick XMPP Library
+ Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ This file is part of Slixmpp.
+
+ See the file LICENSE for copying permission.
+"""
+
+import logging
+
+from slixmpp import Presence
+from slixmpp.exceptions import XMPPError
+from slixmpp.plugins import BasePlugin, register_plugin
+from slixmpp.xmlstream import register_stanza_plugin
+
+from slixmpp.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
+ default_config = {
+ 'auto_last_activity': False
+ }
+
+ def plugin_init(self):
+ 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 plugin_end(self):
+ self.xmpp.del_filter('out', self._initial_presence_activity)
+ self.xmpp.del_event_handler('connected', self._reset_presence_activity)
+
+ 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)