Bump to 1.3.1
[slixmpp.git] / sleekxmpp / plugins / xep_0202 / time.py
blobd5b3af3728269473e03de3673a42350eba3fc57b
1 """
2 SleekXMPP: The Sleek XMPP Library
3 Copyright (C) 2010 Nathanael C. Fritz
4 This file is part of SleekXMPP.
6 See the file LICENSE for copying permission.
7 """
9 import logging
11 from sleekxmpp.stanza.iq import Iq
12 from sleekxmpp.xmlstream import register_stanza_plugin
13 from sleekxmpp.xmlstream.handler import Callback
14 from sleekxmpp.xmlstream.matcher import StanzaPath
15 from sleekxmpp.plugins import BasePlugin
16 from sleekxmpp.plugins import xep_0082
17 from sleekxmpp.plugins.xep_0202 import stanza
20 log = logging.getLogger(__name__)
23 class XEP_0202(BasePlugin):
25 """
26 XEP-0202: Entity Time
27 """
29 name = 'xep_0202'
30 description = 'XEP-0202: Entity Time'
31 dependencies = set(['xep_0030', 'xep_0082'])
32 stanza = stanza
33 default_config = {
34 #: As a default, respond to time requests with the
35 #: local time returned by XEP-0082. However, a
36 #: custom function can be supplied which accepts
37 #: the JID of the entity to query for the time.
38 'local_time': None,
39 'tz_offset': 0
42 def plugin_init(self):
43 """Start the XEP-0203 plugin."""
45 if not self.local_time:
46 def default_local_time(jid):
47 return xep_0082.datetime(offset=self.tz_offset)
49 self.local_time = default_local_time
51 self.xmpp.register_handler(
52 Callback('Entity Time',
53 StanzaPath('iq/entity_time'),
54 self._handle_time_request))
55 register_stanza_plugin(Iq, stanza.EntityTime)
57 def plugin_end(self):
58 self.xmpp['xep_0030'].del_feature(feature='urn:xmpp:time')
59 self.xmpp.remove_handler('Entity Time')
61 def session_bind(self, jid):
62 self.xmpp['xep_0030'].add_feature('urn:xmpp:time')
64 def _handle_time_request(self, iq):
65 """
66 Respond to a request for the local time.
68 The time is taken from self.local_time(), which may be replaced
69 during plugin configuration with a function that maps JIDs to
70 times.
72 Arguments:
73 iq -- The Iq time request stanza.
74 """
75 iq.reply()
76 iq['entity_time']['time'] = self.local_time(iq['to'])
77 iq.send()
79 def get_entity_time(self, to, ifrom=None, **iqargs):
80 """
81 Request the time from another entity.
83 Arguments:
84 to -- JID of the entity to query.
85 ifrom -- Specifiy the sender's JID.
86 block -- If true, block and wait for the stanzas' reply.
87 timeout -- The time in seconds to block while waiting for
88 a reply. If None, then wait indefinitely.
89 callback -- Optional callback to execute when a reply is
90 received instead of blocking and waiting for
91 the reply.
92 """
93 iq = self.xmpp.Iq()
94 iq['type'] = 'get'
95 iq['to'] = to
96 iq['from'] = ifrom
97 iq.enable('entity_time')
98 return iq.send(**iqargs)