# Slixmpp: The Slick XMPP Library# Copyright (C) 2010 Nathanael C. Fritz# This file is part of Slixmpp.# See the file LICENSE for copying permission.importloggingfromtypingimportOptionalfromslixmppimportJIDfromslixmpp.stanza.iqimportIqfromslixmpp.xmlstreamimportregister_stanza_pluginfromslixmpp.xmlstream.handlerimportCallbackfromslixmpp.xmlstream.matcherimportStanzaPathfromslixmpp.pluginsimportBasePluginfromslixmpp.pluginsimportxep_0082fromslixmpp.plugins.xep_0202importstanzalog=logging.getLogger(__name__)
[docs]classXEP_0202(BasePlugin):""" XEP-0202: Entity Time """name='xep_0202'description='XEP-0202: Entity Time'dependencies={'xep_0030','xep_0082'}stanza=stanzadefault_config={#: As a default, respond to time requests with the#: local time returned by XEP-0082. However, a#: custom function can be supplied which accepts#: the JID of the entity to query for the time.'local_time':None,'tz_offset':0}defplugin_init(self):"""Start the XEP-0202 plugin."""ifnotself.local_time:defdefault_local_time(jid):returnxep_0082.datetime(offset=self.tz_offset)self.local_time=default_local_timeself.xmpp.register_handler(Callback('Entity Time',StanzaPath('iq@type=get/entity_time'),self._handle_time_request))register_stanza_plugin(Iq,stanza.EntityTime)defplugin_end(self):self.xmpp['xep_0030'].del_feature(feature='urn:xmpp:time')self.xmpp.remove_handler('Entity Time')defsession_bind(self,jid):self.xmpp['xep_0030'].add_feature('urn:xmpp:time')def_handle_time_request(self,iq:Iq):""" Respond to a request for the local time. The time is taken from self.local_time(), which may be replaced during plugin configuration with a function that maps JIDs to times. :param iq: The Iq time request stanza. """iq=iq.reply()iq['entity_time']['time']=self.local_time(iq['to'])iq.send()
[docs]defget_entity_time(self,to:JID,ifrom:Optional[JID]=None,**iqargs):""" Request the time from another entity. :param to: JID of the entity to query. """iq=self.xmpp.Iq()iq['type']='get'iq['to']=toiq['from']=ifromiq.enable('entity_time')returniq.send(**iqargs)