summaryrefslogtreecommitdiff
path: root/sleekxmpp/stanza
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/stanza')
-rw-r--r--sleekxmpp/stanza/iq.py18
-rw-r--r--sleekxmpp/stanza/message.py11
-rw-r--r--sleekxmpp/stanza/presence.py12
-rw-r--r--sleekxmpp/stanza/rootstanza.py25
4 files changed, 35 insertions, 31 deletions
diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py
index a9c9c4be..cec0f8bc 100644
--- a/sleekxmpp/stanza/iq.py
+++ b/sleekxmpp/stanza/iq.py
@@ -3,8 +3,9 @@ from xml.etree import cElementTree as ET
from . error import Error
from .. xmlstream.handler.waiter import Waiter
from .. xmlstream.matcher.id import MatcherId
+from . rootstanza import RootStanza
-class Iq(StanzaBase):
+class Iq(RootStanza):
interfaces = set(('type', 'to', 'from', 'id','query'))
types = set(('get', 'result', 'set', 'error'))
name = 'iq'
@@ -13,13 +14,10 @@ class Iq(StanzaBase):
def __init__(self, *args, **kwargs):
StanzaBase.__init__(self, *args, **kwargs)
if self['id'] == '':
- self['id'] = self.stream.getNewId()
-
- def exception(self, text):
- self.reply()
- self['error']['condition'] = 'undefined-condition'
- self['error']['text'] = text
- self.send()
+ if self.stream is not None:
+ self['id'] = self.stream.getNewId()
+ else:
+ self['id'] = '0'
def unhandled(self):
self.reply()
@@ -84,7 +82,3 @@ class Iq(StanzaBase):
return waitfor.wait(timeout)
else:
return StanzaBase.send(self)
-
-
-Iq.plugin_attrib_map['error'] = Error
-Iq.plugin_tag_map["{%s}%s" % (Error.namespace, Error.name)] = Error
diff --git a/sleekxmpp/stanza/message.py b/sleekxmpp/stanza/message.py
index d75421c8..c8d54f13 100644
--- a/sleekxmpp/stanza/message.py
+++ b/sleekxmpp/stanza/message.py
@@ -1,8 +1,9 @@
from .. xmlstream.stanzabase import StanzaBase
from xml.etree import cElementTree as ET
from . error import Error
+from . rootstanza import RootStanza
-class Message(StanzaBase):
+class Message(RootStanza):
interfaces = set(('type', 'to', 'from', 'id', 'body', 'subject'))
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
sub_interfaces = set(('body', 'subject'))
@@ -27,11 +28,3 @@ class Message(StanzaBase):
self['body'] = body
return self
- def exception(self, text):
- self.reply()
- self['error']['condition'] = 'undefined-condition'
- self['error']['text'] = text
- self.send()
-
-Message.plugin_attrib_map['error'] = Error
-Message.plugin_tag_map["{%s}%s" % (Error.namespace, Error.name)] = Error
diff --git a/sleekxmpp/stanza/presence.py b/sleekxmpp/stanza/presence.py
index cb6bd6ce..6a8247c5 100644
--- a/sleekxmpp/stanza/presence.py
+++ b/sleekxmpp/stanza/presence.py
@@ -1,8 +1,9 @@
from .. xmlstream.stanzabase import StanzaBase
from xml.etree import cElementTree as ET
from . error import Error
+from . rootstanza import RootStanza
-class Presence(StanzaBase):
+class Presence(RootStanza):
interfaces = set(('type', 'to', 'from', 'id', 'status', 'priority'))
types = set(('available', 'unavailable', 'error', 'probe', 'subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'))
showtypes = set(('dnd', 'ffc', 'xa', 'away'))
@@ -52,12 +53,3 @@ class Presence(StanzaBase):
elif self['type'] == 'subscribe':
self['type'] = 'subscribed'
return StanzaBase.reply(self)
-
- def exception(self, text):
- self.reply()
- self['error']['condition'] = 'undefined-condition'
- self['error']['text'] = text
- self.send()
-
-Presence.plugin_attrib_map['error'] = Error
-Presence.plugin_tag_map["{%s}%s" % (Error.namespace, Error.name)] = Error
diff --git a/sleekxmpp/stanza/rootstanza.py b/sleekxmpp/stanza/rootstanza.py
new file mode 100644
index 00000000..72ba5ca9
--- /dev/null
+++ b/sleekxmpp/stanza/rootstanza.py
@@ -0,0 +1,25 @@
+from .. xmlstream.stanzabase import StanzaBase
+from xml.etree import cElementTree as ET
+from . error import Error
+from .. exceptions import XMPPError
+import traceback
+
+class RootStanza(StanzaBase):
+
+ def exception(self, e): #called when a handler raises an exception
+ self.reply()
+ if isinstance(e, XMPPError): # we raised this deliberately
+ self['error']['condition'] = e.condition
+ self['error']['text'] = e.text
+ if e.extension is not None: # extended error tag
+ extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension), e.extension_args)
+ self['error'].xml.append(extxml)
+ self['error']['type'] = e.etype
+ else: # we probably didn't raise this on purpose, so send back a traceback
+ self['error']['condition'] = 'undefined-condition'
+ self['error']['text'] = traceback.format_tb(e.__traceback__)
+ self.send()
+
+# all jabber:client root stanzas should have the error plugin
+RootStanza.plugin_attrib_map['error'] = Error
+RootStanza.plugin_tag_map["{%s}%s" % (Error.namespace, Error.name)] = Error