Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits: c2e09adc by lovetox at 2020-10-26T22:50:56+01:00 Ping: Handle ping requests
- - - - - 6e0399b5 by lovetox at 2020-10-26T23:03:36+01:00 Task: Display timeout error
- - - - -
3 changed files:
- nbxmpp/errors.py - nbxmpp/modules/ping.py - nbxmpp/task.py
Changes:
===================================== nbxmpp/errors.py ===================================== @@ -27,6 +27,10 @@ def is_error(error): class BaseError(Exception): def __init__(self, is_fatal=False): self.is_fatal = is_fatal + self.text = '' + + def __str__(self): + return self.text
class StanzaError(BaseError): @@ -141,9 +145,9 @@ class TimeoutStanzaError(BaseError):
log_level = logging.INFO
- def __init__(self, id_): - BaseError.__init__(self, is_fatal=True) - self.text = 'IQ with id %s reached timeout' % id_ + def __init__(self): + BaseError.__init__(self) + self.text = 'Timeout reached'
class RegisterStanzaError(StanzaError):
===================================== nbxmpp/modules/ping.py ===================================== @@ -16,7 +16,9 @@ # along with this program; If not, see http://www.gnu.org/licenses/.
from nbxmpp.protocol import Iq +from nbxmpp.protocol import NodeProcessed from nbxmpp.namespaces import Namespace +from nbxmpp.structs import StanzaHandler from nbxmpp.task import iq_request_task from nbxmpp.modules.base import BaseModule from nbxmpp.modules.util import process_response @@ -27,7 +29,19 @@ class Ping(BaseModule): BaseModule.__init__(self, client)
self._client = client - self.handlers = [] + self.handlers = [ + StanzaHandler(name='iq', + callback=self._process_ping, + typ='get', + ns=Namespace.PING, + priority=15), + ] + + def _process_ping(self, _client, stanza, properties): + self._log.info('Send pong to %s', stanza.getFrom()) + iq = stanza.buildSimpleReply('result') + self._client.send_stanza(iq) + raise NodeProcessed
@iq_request_task def ping(self, jid):
===================================== nbxmpp/task.py ===================================== @@ -309,7 +309,7 @@ class IqRequestTask(Task): return
if result is None: - self._error = TimeoutStanzaError(self._iq_id) + self._error = TimeoutStanzaError() self._set_finished() return
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/-/compare/355e3076fdcf231b467a01f1...