From 004eabf80959ebcaeddf2e15065bd4f50ad10974 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 17 Aug 2011 21:30:47 -0700 Subject: [PATCH] Update plugins that use Iq stanzas to work with new exceptions. --- sleekxmpp/plugins/xep_0012.py | 5 +-- sleekxmpp/plugins/xep_0045.py | 42 +++++++++++++++++------- sleekxmpp/plugins/xep_0078/legacyauth.py | 39 ++++++++++++++-------- sleekxmpp/plugins/xep_0199/ping.py | 24 +++++++++----- sleekxmpp/roster/single.py | 6 ++-- 5 files changed, 75 insertions(+), 41 deletions(-) diff --git a/sleekxmpp/plugins/xep_0012.py b/sleekxmpp/plugins/xep_0012.py index d636d4d..8fe818b 100644 --- a/sleekxmpp/plugins/xep_0012.py +++ b/sleekxmpp/plugins/xep_0012.py @@ -112,7 +112,4 @@ class xep_0012(base.base_plugin): iq.attrib['from'] = self.xmpp.boundjid.full id = iq.get('id') result = iq.send() - if result and result is not None and result.get('type', 'error') != 'error': - return result['last_activity']['seconds'] - else: - return False + return result['last_activity']['seconds'] diff --git a/sleekxmpp/plugins/xep_0045.py b/sleekxmpp/plugins/xep_0045.py index 364fbbd..338ed15 100644 --- a/sleekxmpp/plugins/xep_0045.py +++ b/sleekxmpp/plugins/xep_0045.py @@ -188,8 +188,12 @@ class xep_0045(base.base_plugin): iq['from'] = ifrom query = ET.Element('{http://jabber.org/protocol/muc#owner}query') iq.append(query) - result = iq.send() - if result['type'] == 'error': + # For now, swallow errors to preserve existing API + try: + result = iq.send() + except IqError: + return False + except IqTimeout: return False xform = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x') if xform is None: return False @@ -209,8 +213,12 @@ class xep_0045(base.base_plugin): form = form.getXML('submit') query.append(form) iq.append(query) - result = iq.send() - if result['type'] == 'error': + # For now, swallow errors to preserve existing API + try: + result = iq.send() + except IqError: + return False + except IqTimeout: return False return True @@ -254,8 +262,12 @@ class xep_0045(base.base_plugin): destroy.append(xreason) query.append(destroy) iq.append(query) - r = iq.send() - if r is False or r['type'] == 'error': + # For now, swallow errors to preserve existing API + try: + r = iq.send() + except IqError: + return False + except IqTimeout: return False return True @@ -271,9 +283,13 @@ class xep_0045(base.base_plugin): query.append(item) iq = self.xmpp.makeIqSet(query) iq['to'] = room - result = iq.send() - if result is False or result['type'] != 'result': - raise ValueError + # For now, swallow errors to preserve existing API + try: + result = iq.send() + except IqError: + return False + except IqTimeout: + return False return True def invite(self, room, jid, reason='', mfrom=''): @@ -303,8 +319,12 @@ class xep_0045(base.base_plugin): iq = self.xmpp.makeIqGet('http://jabber.org/protocol/muc#owner') iq['to'] = room iq['from'] = ifrom - result = iq.send() - if result is None or result['type'] != 'result': + # For now, swallow errors to preserve existing API + try: + result = iq.send() + except IqError: + raise ValueError + except IqTimeout: raise ValueError form = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x') if form is None: diff --git a/sleekxmpp/plugins/xep_0078/legacyauth.py b/sleekxmpp/plugins/xep_0078/legacyauth.py index bdd2df6..edb8f31 100644 --- a/sleekxmpp/plugins/xep_0078/legacyauth.py +++ b/sleekxmpp/plugins/xep_0078/legacyauth.py @@ -56,11 +56,17 @@ class xep_0078(base_plugin): iq['type'] = 'get' iq['to'] = self.xmpp.boundjid.host iq['auth']['username'] = self.xmpp.boundjid.user - resp = iq.send(now=True) - if resp is None or resp['type'] != 'result': + try: + resp = iq.send(now=True) + except IqError: log.info("Authentication failed: %s" % resp['error']['condition']) - self.xmpp.event('failed_auth', resp, direct=True) + self.xmpp.event('failed_auth', direct=True) + self.xmpp.disconnect() + return True + except IqTimeout: + log.info("Authentication failed: %s" % 'timeout') + self.xmpp.event('failed_auth', direct=True) self.xmpp.disconnect() return True @@ -91,18 +97,23 @@ class xep_0078(base_plugin): iq['auth']['password'] = self.xmpp.password # Step 3: Send credentials - result = iq.send(now=True) - if result is not None and result.attrib['type'] == 'result': - self.xmpp.features.add('auth') - - self.xmpp.authenticated = True - log.debug("Established Session") - self.xmpp.sessionstarted = True - self.xmpp.session_started_event.set() - self.xmpp.event('session_start') - else: + try: + result = iq.send(now=True) + except IqError as err: log.info("Authentication failed") self.xmpp.disconnect() - self.xmpp.event("failed_auth") + self.xmpp.event("failed_auth", direct=True) + except IqTimeout: + log.info("Authentication failed") + self.xmpp.disconnect() + self.xmpp.event("failed_auth", direct=True) + + self.xmpp.features.add('auth') + + self.xmpp.authenticated = True + log.debug("Established Session") + self.xmpp.sessionstarted = True + self.xmpp.session_started_event.set() + self.xmpp.event('session_start') return True diff --git a/sleekxmpp/plugins/xep_0199/ping.py b/sleekxmpp/plugins/xep_0199/ping.py index 0fa22f8..b0304b0 100644 --- a/sleekxmpp/plugins/xep_0199/ping.py +++ b/sleekxmpp/plugins/xep_0199/ping.py @@ -11,6 +11,7 @@ import logging import sleekxmpp from sleekxmpp import Iq +from sleekxmpp.exceptions import IqError, IqTimeout from sleekxmpp.xmlstream import register_stanza_plugin from sleekxmpp.xmlstream.matcher import StanzaPath from sleekxmpp.xmlstream.handler import Callback @@ -89,8 +90,13 @@ class xep_0199(base_plugin): def scheduled_ping(): """Send ping request to the server.""" log.debug("Pinging...") - resp = self.send_ping(self.xmpp.boundjid.host, self.timeout) - if resp is None or resp is False: + try: + self.send_ping(self.xmpp.boundjid.host, self.timeout) + except IqError: + log.debug("Ping response was an error." + \ + "Requesting Reconnect.") + self.xmpp.reconnect() + except IqTimeout: log.debug("Did not recieve ping back in time." + \ "Requesting Reconnect.") self.xmpp.reconnect() @@ -142,9 +148,14 @@ class xep_0199(base_plugin): iq.enable('ping') start_time = time.clock() - resp = iq.send(block=block, - timeout=timeout, - callback=callback) + + try: + resp = iq.send(block=block, + timeout=timeout, + callback=callback) + except IqError as err: + resp = err.iq + end_time = time.clock() delay = end_time - start_time @@ -152,9 +163,6 @@ class xep_0199(base_plugin): if not block: return None - if not resp or resp['type'] == 'error': - return False - log.debug("Pong: %s %f" % (jid, delay)) return delay diff --git a/sleekxmpp/roster/single.py b/sleekxmpp/roster/single.py index deb1ac8..411c5d9 100644 --- a/sleekxmpp/roster/single.py +++ b/sleekxmpp/roster/single.py @@ -204,10 +204,8 @@ class RosterNode(object): iq['roster']['items'] = {jid: {'name': name, 'subscription': subscription, 'groups': groups}} - response = iq.send(block, timeout, callback) - if response in [False, None] or isinstance(response, Iq): - return response - return response and response['type'] == 'result' + + return iq.send(block, timeout, callback) def presence(self, jid, resource=None): """