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 iq.attrib['from'] = self.xmpp.boundjid.full
id = iq.get('id') id = iq.get('id')
result = iq.send() result = iq.send()
if result and result is not None and result.get('type', 'error') != 'error':
return result['last_activity']['seconds'] return result['last_activity']['seconds']
else:
return False

View file

@ -188,8 +188,12 @@ class xep_0045(base.base_plugin):
iq['from'] = ifrom iq['from'] = ifrom
query = ET.Element('{http://jabber.org/protocol/muc#owner}query') query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
iq.append(query) iq.append(query)
# For now, swallow errors to preserve existing API
try:
result = iq.send() result = iq.send()
if result['type'] == 'error': except IqError:
return False
except IqTimeout:
return False return False
xform = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x') xform = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x')
if xform is None: return False if xform is None: return False
@ -209,8 +213,12 @@ class xep_0045(base.base_plugin):
form = form.getXML('submit') form = form.getXML('submit')
query.append(form) query.append(form)
iq.append(query) iq.append(query)
# For now, swallow errors to preserve existing API
try:
result = iq.send() result = iq.send()
if result['type'] == 'error': except IqError:
return False
except IqTimeout:
return False return False
return True return True
@ -254,8 +262,12 @@ class xep_0045(base.base_plugin):
destroy.append(xreason) destroy.append(xreason)
query.append(destroy) query.append(destroy)
iq.append(query) iq.append(query)
# For now, swallow errors to preserve existing API
try:
r = iq.send() r = iq.send()
if r is False or r['type'] == 'error': except IqError:
return False
except IqTimeout:
return False return False
return True return True
@ -271,9 +283,13 @@ class xep_0045(base.base_plugin):
query.append(item) query.append(item)
iq = self.xmpp.makeIqSet(query) iq = self.xmpp.makeIqSet(query)
iq['to'] = room iq['to'] = room
# For now, swallow errors to preserve existing API
try:
result = iq.send() result = iq.send()
if result is False or result['type'] != 'result': except IqError:
raise ValueError return False
except IqTimeout:
return False
return True return True
def invite(self, room, jid, reason='', mfrom=''): 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 = self.xmpp.makeIqGet('http://jabber.org/protocol/muc#owner')
iq['to'] = room iq['to'] = room
iq['from'] = ifrom iq['from'] = ifrom
# For now, swallow errors to preserve existing API
try:
result = iq.send() result = iq.send()
if result is None or result['type'] != 'result': except IqError:
raise ValueError
except IqTimeout:
raise ValueError raise ValueError
form = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x') form = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x')
if form is None: if form is None:

View file

@ -56,11 +56,17 @@ class xep_0078(base_plugin):
iq['type'] = 'get' iq['type'] = 'get'
iq['to'] = self.xmpp.boundjid.host iq['to'] = self.xmpp.boundjid.host
iq['auth']['username'] = self.xmpp.boundjid.user 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']) 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() self.xmpp.disconnect()
return True return True
@ -91,8 +97,17 @@ class xep_0078(base_plugin):
iq['auth']['password'] = self.xmpp.password iq['auth']['password'] = self.xmpp.password
# Step 3: Send credentials # Step 3: Send credentials
try:
result = iq.send(now=True) 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.features.add('auth')
self.xmpp.authenticated = True self.xmpp.authenticated = True
@ -100,9 +115,5 @@ class xep_0078(base_plugin):
self.xmpp.sessionstarted = True self.xmpp.sessionstarted = True
self.xmpp.session_started_event.set() self.xmpp.session_started_event.set()
self.xmpp.event('session_start') self.xmpp.event('session_start')
else:
log.info("Authentication failed")
self.xmpp.disconnect()
self.xmpp.event("failed_auth")
return True return True

View file

@ -11,6 +11,7 @@ import logging
import sleekxmpp import sleekxmpp
from sleekxmpp import Iq from sleekxmpp import Iq
from sleekxmpp.exceptions import IqError, IqTimeout
from sleekxmpp.xmlstream import register_stanza_plugin from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.xmlstream.matcher import StanzaPath from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.xmlstream.handler import Callback from sleekxmpp.xmlstream.handler import Callback
@ -89,8 +90,13 @@ class xep_0199(base_plugin):
def scheduled_ping(): def scheduled_ping():
"""Send ping request to the server.""" """Send ping request to the server."""
log.debug("Pinging...") log.debug("Pinging...")
resp = self.send_ping(self.xmpp.boundjid.host, self.timeout) try:
if resp is None or resp is False: 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." + \ log.debug("Did not recieve ping back in time." + \
"Requesting Reconnect.") "Requesting Reconnect.")
self.xmpp.reconnect() self.xmpp.reconnect()
@ -142,9 +148,14 @@ class xep_0199(base_plugin):
iq.enable('ping') iq.enable('ping')
start_time = time.clock() start_time = time.clock()
try:
resp = iq.send(block=block, resp = iq.send(block=block,
timeout=timeout, timeout=timeout,
callback=callback) callback=callback)
except IqError as err:
resp = err.iq
end_time = time.clock() end_time = time.clock()
delay = end_time - start_time delay = end_time - start_time
@ -152,9 +163,6 @@ class xep_0199(base_plugin):
if not block: if not block:
return None return None
if not resp or resp['type'] == 'error':
return False
log.debug("Pong: %s %f" % (jid, delay)) log.debug("Pong: %s %f" % (jid, delay))
return delay return delay

View file

@ -204,10 +204,8 @@ class RosterNode(object):
iq['roster']['items'] = {jid: {'name': name, iq['roster']['items'] = {jid: {'name': name,
'subscription': subscription, 'subscription': subscription,
'groups': groups}} 'groups': groups}}
response = iq.send(block, timeout, callback)
if response in [False, None] or isinstance(response, Iq): return iq.send(block, timeout, callback)
return response
return response and response['type'] == 'result'
def presence(self, jid, resource=None): def presence(self, jid, resource=None):
""" """