Philipp Hörist pushed to branch master at gajim / gajim
Commits: 8fa1ee46 by Philipp Hörist at 2018-06-25T23:00:40+02:00 Refactor last activity code into own module
- - - - - 129f72bf by Philipp Hörist at 2018-06-25T23:47:06+02:00 Refactor http auth code into own module
- - - - -
6 changed files:
- gajim/common/connection.py - gajim/common/connection_handlers.py - gajim/common/connection_handlers_events.py - + gajim/common/modules/http_auth.py - + gajim/common/modules/last_activity.py - gajim/gui_interface.py
Changes:
===================================== gajim/common/connection.py ===================================== --- a/gajim/common/connection.py +++ b/gajim/common/connection.py @@ -69,6 +69,8 @@ from gajim.common.modules.ping import Ping from gajim.common.modules.search import Search from gajim.common.modules.annotations import Annotations from gajim.common.modules.roster_item_exchange import RosterItemExchange +from gajim.common.modules.last_activity import LastActivity +from gajim.common.modules.http_auth import HTTPAuth from gajim.common.connection_handlers import * from gajim.common.contacts import GC_Contact from gajim.gtkgui_helpers import get_action @@ -662,6 +664,8 @@ class Connection(CommonConnection, ConnectionHandlers): self.register_module('Search', Search, self) self.register_module('Annotations', Annotations, self) self.register_module('RosterItemExchange', RosterItemExchange, self) + self.register_module('LastActivity', LastActivity, self) + self.register_module('HTTPAuth', HTTPAuth, self)
app.ged.register_event_handler('privacy-list-received', ged.CORE, self._nec_privacy_list_received)
===================================== gajim/common/connection_handlers.py ===================================== --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -48,7 +48,6 @@ from gajim.common import app from gajim.common import dataforms from gajim.common import jingle_xtls from gajim.common import configpaths -from gajim.common import idle from gajim.common.caps_cache import muc_caps_cache from gajim.common.commands import ConnectionCommands from gajim.common.pubsub import ConnectionPubSub @@ -1343,10 +1342,6 @@ ConnectionHTTPUpload): Archiving313PreferencesChangedReceivedEvent) app.nec.register_incoming_event(NotificationEvent)
- app.ged.register_event_handler('http-auth-received', ged.CORE, - self._nec_http_auth_received) - app.ged.register_event_handler('last-request-received', ged.CORE, - self._nec_last_request_received) app.ged.register_event_handler('roster-set-received', ged.CORE, self._nec_roster_set_received) app.ged.register_event_handler('private-storage-bookmarks-received', @@ -1378,10 +1373,6 @@ ConnectionHTTPUpload): ConnectionArchive313.cleanup(self) ConnectionPubSub.cleanup(self) ConnectionHTTPUpload.cleanup(self) - app.ged.remove_event_handler('http-auth-received', ged.CORE, - self._nec_http_auth_received) - app.ged.remove_event_handler('last-request-received', ged.CORE, - self._nec_last_request_received) app.ged.remove_event_handler('roster-set-received', ged.CORE, self._nec_roster_set_received) app.ged.remove_event_handler('private-storage-bookmarks-received', @@ -1424,32 +1415,6 @@ ConnectionHTTPUpload): c.setAttr('ver', app.caps_hash[self.name]) return p
- def build_http_auth_answer(self, iq_obj, answer): - if not self.connection or self.connected < 2: - return - if answer == 'yes': - confirm = iq_obj.getTag('confirm') - reply = iq_obj.buildReply('result') - if iq_obj.getName() == 'message': - reply.addChild(node=confirm) - self.connection.send(reply) - elif answer == 'no': - err = nbxmpp.Error(iq_obj, nbxmpp.protocol.ERR_NOT_AUTHORIZED) - self.connection.send(err) - - def _nec_http_auth_received(self, obj): - if obj.conn.name != self.name: - return - if obj.opt in ('yes', 'no'): - obj.conn.build_http_auth_answer(obj.stanza, obj.opt) - return True - - def _HttpAuthCB(self, con, iq_obj): - log.debug('HttpAuthCB') - app.nec.push_incoming_event(HttpAuthReceivedEvent(None, conn=self, - stanza=iq_obj)) - raise nbxmpp.NodeProcessed - def _ErrorCB(self, con, iq_obj): log.debug('ErrorCB') app.nec.push_incoming_event(IqErrorReceivedEvent(None, conn=self, @@ -1640,29 +1605,6 @@ ConnectionHTTPUpload): app.config.set_per('accounts', self.name, 'roster_version', obj.version)
- def _LastCB(self, con, iq_obj): - log.debug('LastCB') - if not self.connection or self.connected < 2: - return - app.nec.push_incoming_event(LastRequestEvent(None, conn=self, - stanza=iq_obj)) - raise nbxmpp.NodeProcessed - - def _nec_last_request_received(self, obj): - if obj.conn.name != self.name: - return - if app.is_installed('IDLE') and app.config.get_per('accounts', self.name, - 'send_idle_time'): - iq_obj = obj.stanza.buildReply('result') - qp = iq_obj.setQuery() - qp.attrs['seconds'] = idle.Monitor.get_idle_sec() - else: - iq_obj = obj.stanza.buildReply('error') - err = nbxmpp.ErrorNode(name=nbxmpp.NS_STANZAS + \ - ' service-unavailable') - iq_obj.addChild(node=err) - self.connection.send(iq_obj) - def _messageCB(self, con, msg): """ Called when we receive a message @@ -2086,7 +2028,6 @@ ConnectionHTTPUpload): nbxmpp.NS_DISCO_INFO) con.RegisterHandler('iq', self._DiscoverInfoErrorCB, 'error', nbxmpp.NS_DISCO_INFO) - con.RegisterHandler('iq', self._LastCB, 'get', nbxmpp.NS_LAST) con.RegisterHandler('iq', self._MucOwnerCB, 'result', nbxmpp.NS_MUC_OWNER) con.RegisterHandler('iq', self._MucAdminCB, 'result', @@ -2094,7 +2035,6 @@ ConnectionHTTPUpload): con.RegisterHandler('iq', self._PrivateCB, 'result', nbxmpp.NS_PRIVATE) con.RegisterHandler('iq', self._SecLabelCB, 'result', nbxmpp.NS_SECLABEL_CATALOG) - con.RegisterHandler('iq', self._HttpAuthCB, 'get', nbxmpp.NS_HTTP_AUTH) con.RegisterHandler('iq', self._CommandExecuteCB, 'set', nbxmpp.NS_COMMANDS) con.RegisterHandler('iq', self._DiscoverInfoGetCB, 'get',
===================================== gajim/common/connection_handlers_events.py ===================================== --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -180,23 +180,6 @@ class HelperEvent: self.muc_pm = muc_user.getChildren() == [] return self.muc_pm
-class HttpAuthReceivedEvent(nec.NetworkIncomingEvent): - name = 'http-auth-received' - base_network_events = [] - - def generate(self): - self.opt = app.config.get_per('accounts', self.conn.name, 'http_auth') - self.iq_id = self.stanza.getTagAttr('confirm', 'id') - self.method = self.stanza.getTagAttr('confirm', 'method') - self.url = self.stanza.getTagAttr('confirm', 'url') - # In case it's a message with a body - self.msg = self.stanza.getTagData('body') - return True - -class LastRequestEvent(nec.NetworkIncomingEvent): - name = 'last-request-received' - base_network_events = [] - class RosterReceivedEvent(nec.NetworkIncomingEvent): name = 'roster-received' base_network_events = [] @@ -1064,8 +1047,8 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
# check if the message is a XEP-0070 confirmation request if self.stanza.getTag('confirm', namespace=nbxmpp.NS_HTTP_AUTH): - app.nec.push_incoming_event(HttpAuthReceivedEvent(None, - conn=self.conn, stanza=self.stanza)) + self.conn.get_module('HTTPAuth').answer_request( + self.conn, self.stanza) return
try:
===================================== gajim/common/modules/http_auth.py ===================================== --- /dev/null +++ b/gajim/common/modules/http_auth.py @@ -0,0 +1,76 @@ +# 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-0070: Verifying HTTP Requests via XMPP + +import logging + +import nbxmpp + +from gajim.common import app +from gajim.common.nec import NetworkIncomingEvent + +log = logging.getLogger('gajim.c.m.http_auth') + + +class HTTPAuth: + def __init__(self, con): + self._con = con + self._account = con.name + + self.handlers = [ + ('iq', self.answer_request, 'get', nbxmpp.NS_HTTP_AUTH) + ] + + def answer_request(self, con, stanza): + log.info('Auth request received') + auto_answer = app.config.get_per( + 'accounts', self._account, 'http_auth') + if auto_answer in ('yes', 'no'): + self.build_http_auth_answer(stanza, auto_answer) + raise nbxmpp.NodeProcessed + + iq_id = stanza.getTagAttr('confirm', 'id') + method = stanza.getTagAttr('confirm', 'method') + url = stanza.getTagAttr('confirm', 'url') + # In case it's a message with a body + msg = stanza.getTagData('body') + + app.nec.push_incoming_event( + HttpAuthReceivedEvent(None, conn=self._con, + iq_id=iq_id, + method=method, + url=url, + msg=msg, + stanza=stanza)) + + raise nbxmpp.NodeProcessed + + def build_http_auth_answer(self, stanza, answer): + if answer == 'yes': + log.info('Auth request approved') + confirm = stanza.getTag('confirm') + reply = stanza.buildReply('result') + if stanza.getName() == 'message': + reply.addChild(node=confirm) + self._con.connection.send(reply) + elif answer == 'no': + log.info('Auth request denied') + err = nbxmpp.Error(stanza, nbxmpp.protocol.ERR_NOT_AUTHORIZED) + self._con.connection.send(err) + + +class HttpAuthReceivedEvent(NetworkIncomingEvent): + name = 'http-auth-received' + base_network_events = []
===================================== gajim/common/modules/last_activity.py ===================================== --- /dev/null +++ b/gajim/common/modules/last_activity.py @@ -0,0 +1,52 @@ +# 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-0012: Last Activity + +import logging + +import nbxmpp + +from gajim.common import app +from gajim.common import idle + +log = logging.getLogger('gajim.c.m.last_activity') + + +class LastActivity: + def __init__(self, con): + self._con = con + self._account = con.name + + self.handlers = [('iq', self._answer_request, 'get', nbxmpp.NS_LAST)] + + def _answer_request(self, con, stanza): + log.info('Request from %s', stanza.getFrom()) + if not app.account_is_connected(self._account): + return + + allow_send = app.config.get_per( + 'accounts', self._account, 'send_idle_time') + if app.is_installed('IDLE') and allow_send: + iq = stanza.buildReply('result') + query = iq.setQuery() + query.attrs['seconds'] = idle.Monitor.get_idle_sec() + else: + iq = stanza.buildReply('error') + err = nbxmpp.ErrorNode(nbxmpp.ERR_SERVICE_UNAVAILABLE) + iq.addChild(node=err) + + self._con.connection.send(iq) + + raise nbxmpp.NodeProcessed
===================================== gajim/gui_interface.py ===================================== --- a/gajim/gui_interface.py +++ b/gajim/gui_interface.py @@ -173,7 +173,8 @@ class Interface: def handle_event_http_auth(obj): #('HTTP_AUTH', account, (method, url, transaction_id, iq_obj, msg)) def response(account, answer): - obj.conn.build_http_auth_answer(obj.stanza, answer) + obj.conn.get_module('HTTPAuth').build_http_auth_answer( + obj.stanza, answer)
def on_yes(is_checked, obj): response(obj, 'yes')
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/b74706afcf41cd9d4efc48f032705e16bc...