2010-03-26 14:32:16 -07:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 11:19:49 -04:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 14:32:16 -07:00
|
|
|
"""
|
2010-07-28 13:14:41 -04:00
|
|
|
|
2010-07-26 21:02:25 -04:00
|
|
|
import logging
|
2010-01-05 21:56:48 +00:00
|
|
|
import traceback
|
2010-04-22 21:24:28 -07:00
|
|
|
import sys
|
2010-01-05 21:56:48 +00:00
|
|
|
|
2011-08-19 00:08:47 -07:00
|
|
|
from sleekxmpp.exceptions import XMPPError, IqError, IqTimeout
|
2010-07-28 13:14:41 -04:00
|
|
|
from sleekxmpp.stanza import Error
|
2010-10-17 21:38:22 -04:00
|
|
|
from sleekxmpp.xmlstream import ET, StanzaBase, register_stanza_plugin
|
2010-07-28 13:14:41 -04:00
|
|
|
|
|
|
|
|
2010-11-06 01:28:59 -04:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class RootStanza(StanzaBase):
|
|
|
|
|
2010-07-28 13:14:41 -04:00
|
|
|
"""
|
|
|
|
A top-level XMPP stanza in an XMLStream.
|
|
|
|
|
|
|
|
The RootStanza class provides a more XMPP specific exception
|
|
|
|
handler than provided by the generic StanzaBase class.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
exception -- Overrides StanzaBase.exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
def exception(self, e):
|
|
|
|
"""
|
|
|
|
Create and send an error reply.
|
|
|
|
|
|
|
|
Typically called when an event handler raises an exception.
|
|
|
|
The error's type and text content are based on the exception
|
|
|
|
object's type and content.
|
|
|
|
|
|
|
|
Overrides StanzaBase.exception.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
e -- Exception object
|
|
|
|
"""
|
2011-08-19 00:08:47 -07:00
|
|
|
if isinstance(e, IqError):
|
|
|
|
# We received an Iq error reply, but it wasn't caught
|
|
|
|
# locally. Using the condition/text from that error
|
|
|
|
# response could leak too much information, so we'll
|
|
|
|
# only use a generic error here.
|
|
|
|
self.reply()
|
|
|
|
self['error']['condition'] = 'undefined-condition'
|
|
|
|
self['error']['text'] = 'External error'
|
|
|
|
self['error']['type'] = 'cancel'
|
|
|
|
log.warning('You should catch IqError exceptions')
|
|
|
|
self.send()
|
|
|
|
elif isinstance(e, IqTimeout):
|
|
|
|
self.reply()
|
|
|
|
self['error']['condition'] = 'remote-server-timeout'
|
|
|
|
self['error']['type'] = 'wait'
|
|
|
|
log.warning('You should catch IqTimeout exceptions')
|
|
|
|
self.send()
|
|
|
|
elif isinstance(e, XMPPError):
|
2010-07-28 13:14:41 -04:00
|
|
|
# We raised this deliberately
|
2011-08-19 00:08:47 -07:00
|
|
|
self.reply(clear=e.clear)
|
2010-07-28 13:14:41 -04:00
|
|
|
self['error']['condition'] = e.condition
|
|
|
|
self['error']['text'] = e.text
|
2011-08-19 00:08:47 -07:00
|
|
|
self['error']['type'] = e.etype
|
2010-07-29 11:02:42 -04:00
|
|
|
if e.extension is not None:
|
2010-07-28 13:14:41 -04:00
|
|
|
# Extended error tag
|
2010-07-29 11:02:42 -04:00
|
|
|
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),
|
|
|
|
e.extension_args)
|
2010-07-28 13:14:41 -04:00
|
|
|
self['error'].append(extxml)
|
2010-12-17 23:33:13 +08:00
|
|
|
self.send()
|
2010-07-29 11:02:42 -04:00
|
|
|
else:
|
2010-12-17 23:33:13 +08:00
|
|
|
# We probably didn't raise this on purpose, so send an error stanza
|
2011-08-19 00:08:47 -07:00
|
|
|
self.reply()
|
2010-07-28 13:14:41 -04:00
|
|
|
self['error']['condition'] = 'undefined-condition'
|
2010-12-17 23:33:13 +08:00
|
|
|
self['error']['text'] = "SleekXMPP got into trouble."
|
2011-08-19 00:08:47 -07:00
|
|
|
self['error']['type'] = 'cancel'
|
2010-12-17 23:33:13 +08:00
|
|
|
self.send()
|
|
|
|
# log the error
|
2011-11-20 03:30:44 +08:00
|
|
|
log.exception('Error handling {%s}%s stanza' , self.namespace, self.name)
|
2011-07-01 15:15:13 -07:00
|
|
|
# Finally raise the exception to a global exception handler
|
|
|
|
self.stream.exception(e)
|
2010-07-28 13:14:41 -04:00
|
|
|
|
2010-10-17 21:38:22 -04:00
|
|
|
register_stanza_plugin(RootStanza, Error)
|