Update plugins that use Iq stanzas to work with new exceptions.

This commit is contained in:
Lance Stout 2011-08-17 21:30:47 -07:00
parent 62230fc970
commit 004eabf809
5 changed files with 75 additions and 41 deletions

View file

@ -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

View file

@ -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)
# For now, swallow errors to preserve existing API
try:
result = iq.send()
if result['type'] == 'error':
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)
# For now, swallow errors to preserve existing API
try:
result = iq.send()
if result['type'] == 'error':
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)
# For now, swallow errors to preserve existing API
try:
r = iq.send()
if r is False or r['type'] == 'error':
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
# For now, swallow errors to preserve existing API
try:
result = iq.send()
if result is False or result['type'] != 'result':
raise ValueError
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
# For now, swallow errors to preserve existing API
try:
result = iq.send()
if result is None or result['type'] != 'result':
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:

View file

@ -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,8 +97,17 @@ class xep_0078(base_plugin):
iq['auth']['password'] = self.xmpp.password
# Step 3: Send credentials
try:
result = iq.send(now=True)
if result is not None and result.attrib['type'] == 'result':
except IqError as err:
log.info("Authentication failed")
self.xmpp.disconnect()
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
@ -100,9 +115,5 @@ class xep_0078(base_plugin):
self.xmpp.sessionstarted = True
self.xmpp.session_started_event.set()
self.xmpp.event('session_start')
else:
log.info("Authentication failed")
self.xmpp.disconnect()
self.xmpp.event("failed_auth")
return True

View file

@ -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()
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

View file

@ -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):
"""