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.
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
):
30 description
= 'XEP-0202: Entity Time'
31 dependencies
= set(['xep_0030', 'xep_0082'])
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.
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
)
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
):
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
73 iq -- The Iq time request stanza.
76 iq
['entity_time']['time'] = self
.local_time(iq
['to'])
79 def get_entity_time(self
, to
, ifrom
=None, **iqargs
):
81 Request the time from another entity.
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
97 iq
.enable('entity_time')
98 return iq
.send(**iqargs
)