mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Update plugins that use Iq stanzas to work with new exceptions.
This commit is contained in:
parent
62230fc970
commit
004eabf809
5 changed files with 75 additions and 41 deletions
|
@ -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']
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue