2018-06-24 17:00:13 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# XEP-0202: Entity Time
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import nbxmpp
|
2019-03-09 23:16:27 +01:00
|
|
|
from nbxmpp.structs import StanzaHandler
|
2019-03-11 23:07:19 +01:00
|
|
|
from nbxmpp.modules.date_and_time import parse_datetime
|
|
|
|
from nbxmpp.modules.date_and_time import create_tzinfo
|
2018-06-24 17:00:13 +02:00
|
|
|
|
|
|
|
from gajim.common import app
|
2018-11-16 23:11:05 +01:00
|
|
|
from gajim.common.nec import NetworkEvent
|
2019-03-09 23:16:27 +01:00
|
|
|
from gajim.common.modules.base import BaseModule
|
2018-06-24 17:00:13 +02:00
|
|
|
|
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
class EntityTime(BaseModule):
|
2018-06-24 17:00:13 +02:00
|
|
|
def __init__(self, con):
|
2019-03-09 23:16:27 +01:00
|
|
|
BaseModule.__init__(self, con)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
|
|
|
self.handlers = [
|
2019-03-09 23:16:27 +01:00
|
|
|
StanzaHandler(name='iq',
|
|
|
|
callback=self._answer_request,
|
|
|
|
typ='get',
|
|
|
|
ns=nbxmpp.NS_TIME_REVISED),
|
2018-06-24 17:00:13 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def request_entity_time(self, jid, resource):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
# If we are invisible, do not request
|
|
|
|
if self._con.connected == app.SHOW_LIST.index('invisible'):
|
|
|
|
return
|
|
|
|
|
|
|
|
if resource:
|
|
|
|
jid += '/' + resource
|
|
|
|
iq = nbxmpp.Iq(to=jid, typ='get')
|
|
|
|
iq.addChild('time', namespace=nbxmpp.NS_TIME_REVISED)
|
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Requested: %s', jid)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
|
|
|
self._con.connection.SendAndCallForResponse(iq, self._result_received)
|
|
|
|
|
|
|
|
def _result_received(self, stanza):
|
|
|
|
time_info = None
|
|
|
|
if not nbxmpp.isResultNode(stanza):
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Error: %s', stanza.getError())
|
2018-06-24 17:00:13 +02:00
|
|
|
else:
|
|
|
|
time_info = self._extract_info(stanza)
|
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Received: %s %s', stanza.getFrom(), time_info)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2018-11-16 23:11:05 +01:00
|
|
|
app.nec.push_incoming_event(NetworkEvent('time-result-received',
|
|
|
|
conn=self._con,
|
|
|
|
jid=stanza.getFrom(),
|
|
|
|
time_info=time_info))
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
def _extract_info(self, stanza):
|
2018-06-24 17:00:13 +02:00
|
|
|
time_ = stanza.getTag('time')
|
|
|
|
if not time_:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.warning('No time node: %s', stanza)
|
2018-06-24 17:00:13 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
tzo = time_.getTag('tzo').getData()
|
2018-11-16 23:11:05 +01:00
|
|
|
if not tzo:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.warning('Wrong tzo node: %s', stanza)
|
2018-06-24 17:00:13 +02:00
|
|
|
return
|
|
|
|
|
2018-11-16 23:11:05 +01:00
|
|
|
remote_tz = create_tzinfo(tz_string=tzo)
|
|
|
|
if remote_tz is None:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.warning('Wrong tzo node: %s', stanza)
|
2018-11-16 23:11:05 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
utc_time = time_.getTag('utc').getData()
|
|
|
|
date_time = parse_datetime(utc_time, check_utc=True)
|
|
|
|
if date_time is None:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.warning('Wrong timezone defintion: %s %s',
|
|
|
|
utc_time, stanza.getFrom())
|
2018-06-24 17:00:13 +02:00
|
|
|
return
|
|
|
|
|
2018-11-16 23:11:05 +01:00
|
|
|
date_time = date_time.astimezone(remote_tz)
|
|
|
|
return date_time.strftime('%c %Z')
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2019-03-09 23:16:27 +01:00
|
|
|
def _answer_request(self, _con, stanza, _properties):
|
|
|
|
self._log.info('%s asked for the time', stanza.getFrom())
|
2018-06-24 17:00:13 +02:00
|
|
|
if app.config.get_per('accounts', self._account, 'send_time_info'):
|
|
|
|
iq = stanza.buildReply('result')
|
|
|
|
time_ = iq.setTag('time', namespace=nbxmpp.NS_TIME_REVISED)
|
|
|
|
formated_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
|
|
|
|
time_.setTagData('utc', formated_time)
|
|
|
|
isdst = time.localtime().tm_isdst
|
|
|
|
zone = -(time.timezone, time.altzone)[isdst] / 60.0
|
|
|
|
tzo = (zone / 60, abs(zone % 60))
|
|
|
|
time_.setTagData('tzo', '%+03d:%02d' % (tzo))
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Answer: %s %s', formated_time, '%+03d:%02d' % (tzo))
|
2018-06-24 17:00:13 +02:00
|
|
|
else:
|
|
|
|
iq = stanza.buildReply('error')
|
|
|
|
err = nbxmpp.ErrorNode(nbxmpp.ERR_SERVICE_UNAVAILABLE)
|
|
|
|
iq.addChild(node=err)
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Send service-unavailable')
|
2018-06-24 17:00:13 +02:00
|
|
|
self._con.connection.send(iq)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
|
|
|
|
2018-07-07 13:52:44 +02:00
|
|
|
def get_instance(*args, **kwargs):
|
|
|
|
return EntityTime(*args, **kwargs), 'EntityTime'
|