From 3161f104c77f4dc37c17525ab6c9222b5f350bf1 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 2 Apr 2012 21:57:10 -0700 Subject: Update XEP-0012 plugin to use new api. --- sleekxmpp/plugins/xep_0012.py | 122 ------------------------------------------ 1 file changed, 122 deletions(-) delete mode 100644 sleekxmpp/plugins/xep_0012.py (limited to 'sleekxmpp/plugins/xep_0012.py') diff --git a/sleekxmpp/plugins/xep_0012.py b/sleekxmpp/plugins/xep_0012.py deleted file mode 100644 index 01fb60a8..00000000 --- a/sleekxmpp/plugins/xep_0012.py +++ /dev/null @@ -1,122 +0,0 @@ -""" - SleekXMPP: The Sleek XMPP Library - Copyright (C) 2010 Nathanael C. Fritz - This file is part of SleekXMPP. - - See the file LICENSE for copying permission. -""" - -from datetime import datetime -import logging - -from sleekxmpp.plugins import BasePlugin, register_plugin -from sleekxmpp import Iq -from sleekxmpp.xmlstream.handler.callback import Callback -from sleekxmpp.xmlstream.matcher.xpath import MatchXPath -from sleekxmpp.xmlstream import ElementBase, ET, JID, register_stanza_plugin - - -log = logging.getLogger(__name__) - - -class LastActivity(ElementBase): - name = 'query' - namespace = 'jabber:iq:last' - plugin_attrib = 'last_activity' - interfaces = set(('seconds', 'status')) - - def get_seconds(self): - return int(self._get_attr('seconds')) - - def set_seconds(self, value): - self._set_attr('seconds', str(value)) - - def get_status(self): - return self.xml.text - - def set_status(self, value): - self.xml.text = str(value) - - def del_status(self): - self.xml.text = '' - - -class XEP_0012(BasePlugin): - - """ - XEP-0012 Last Activity - """ - - name = 'xep_0012' - description = 'XEP-0012: Last Activity' - dependencies = set(['xep_0030']) - - def plugin_init(self): - self.xep = "0012" - - self.xmpp.registerHandler( - Callback('Last Activity', - MatchXPath('{%s}iq/{%s}query' % (self.xmpp.default_ns, - LastActivity.namespace)), - self.handle_last_activity_query)) - register_stanza_plugin(Iq, LastActivity) - - self.xmpp.add_event_handler('last_activity_request', self.handle_last_activity) - - if self.xmpp.is_component: - # We are a component, so we track the uptime - self.xmpp.add_event_handler("session_start", self._reset_uptime) - self._start_datetime = datetime.now() - self.xmpp.plugin['xep_0030'].add_feature('jabber:iq:last') - - def _reset_uptime(self, event): - self._start_datetime = datetime.now() - - def handle_last_activity_query(self, iq): - if iq['type'] == 'get': - log.debug("Last activity requested by %s", iq['from']) - self.xmpp.event('last_activity_request', iq) - elif iq['type'] == 'result': - log.debug("Last activity result from %s", iq['from']) - self.xmpp.event('last_activity', iq) - - def handle_last_activity(self, iq): - jid = iq['from'] - - if self.xmpp.is_component: - # Send the uptime - result = LastActivity() - td = (datetime.now() - self._start_datetime) - result['seconds'] = td.seconds + td.days * 24 * 3600 - reply = iq.reply().setPayload(result.xml).send() - else: - barejid = JID(jid).bare - if barejid in self.xmpp.roster and ( self.xmpp.roster[barejid]['subscription'] in ('from', 'both') or - barejid == self.xmpp.boundjid.bare ): - # We don't know how to calculate it - iq.reply().error().setPayload(iq['last_activity'].xml) - iq['error']['code'] = '503' - iq['error']['type'] = 'cancel' - iq['error']['condition'] = 'service-unavailable' - iq.send() - else: - iq.reply().error().setPayload(iq['last_activity'].xml) - iq['error']['code'] = '403' - iq['error']['type'] = 'auth' - iq['error']['condition'] = 'forbidden' - iq.send() - - def get_last_activity(self, jid): - """Query the LastActivity of jid and return it in seconds""" - iq = self.xmpp.makeIqGet() - query = LastActivity() - iq.append(query.xml) - iq.attrib['to'] = jid - iq.attrib['from'] = self.xmpp.boundjid.full - id = iq.get('id') - result = iq.send() - return result['last_activity']['seconds'] - - -xep_0012 = XEP_0012 -register_plugin(XEP_0012) -- cgit v1.2.3