diff options
author | Nathan Fritz <fritzy@netflint.net> | 2009-12-10 07:33:59 +0000 |
---|---|---|
committer | Nathan Fritz <fritzy@netflint.net> | 2009-12-10 07:33:59 +0000 |
commit | a031dd24a6c82745b7272d98229ebc69dd5f4811 (patch) | |
tree | 6fa65ae73a2faafe98cf8bfab24f92ed6bcc1ec0 /sleekxmpp | |
parent | 007b04dd3086a0b02865c924022db70bbd47445e (diff) | |
download | slixmpp-a031dd24a6c82745b7272d98229ebc69dd5f4811.tar.gz slixmpp-a031dd24a6c82745b7272d98229ebc69dd5f4811.tar.bz2 slixmpp-a031dd24a6c82745b7272d98229ebc69dd5f4811.tar.xz slixmpp-a031dd24a6c82745b7272d98229ebc69dd5f4811.zip |
tweaked stanzas for easy use
Diffstat (limited to 'sleekxmpp')
-rw-r--r-- | sleekxmpp/__init__.py | 2 | ||||
-rw-r--r-- | sleekxmpp/basexmpp.py | 38 | ||||
-rw-r--r-- | sleekxmpp/stanza/message.py | 6 | ||||
-rw-r--r-- | sleekxmpp/xmlstream/stanzabase.py | 58 | ||||
-rw-r--r-- | sleekxmpp/xmlstream/xmlstream.py | 1 |
5 files changed, 78 insertions, 27 deletions
diff --git a/sleekxmpp/__init__.py b/sleekxmpp/__init__.py index 49c9a66b..f0be089c 100644 --- a/sleekxmpp/__init__.py +++ b/sleekxmpp/__init__.py @@ -30,6 +30,8 @@ from . xmlstream.matcher.many import MatchMany from . xmlstream.handler.callback import Callback from . xmlstream.stanzabase import StanzaBase from . xmlstream import xmlstream as xmlstreammod +from . stanza.message import Message +from . stanza.iq import Iq import time import logging import base64 diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index 0f4f799c..0eeb52e8 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -354,24 +354,26 @@ class basexmpp(object): return fulljid.split('/', 1)[0] def _handleMessage(self, msg): - xml = msg.xml - ns = xml.tag.split('}')[0] - if ns == 'message': - ns = '' - else: - ns = "%s}" % ns - mfrom = xml.attrib['from'] - message = xml.find('%sbody' % ns).text - subject = xml.find('%ssubject' % ns) - if subject is not None: - subject = subject.text - else: - subject = '' - resource = self.getjidresource(mfrom) - mfrom = self.getjidbare(mfrom) - mtype = xml.attrib.get('type', 'normal') - name = self.roster.get('name', '') - self.event("message", {'jid': mfrom, 'resource': resource, 'name': name, 'type': mtype, 'subject': subject, 'message': message, 'to': xml.attrib.get('to', '')}) + self.event('message', msg) + #xml = msg.xml + #ns = xml.tag.split('}')[0] + #if ns == 'message': + # ns = '' + #else: + # ns = "%s}" % ns + #mfrom = xml.attrib['from'] + #message = xml.find('%sbody' % ns).text + #subject = xml.find('%ssubject' % ns) + #if subject is not None: + # subject = subject.text + #else: + # subject = '' + #resource = self.getjidresource(mfrom) + #mfrom = self.getjidbare(mfrom) + #mtype = xml.attrib.get('type', 'normal') + #name = self.roster.get('name', '') + #self.event("message", {'jid': mfrom, 'resource': resource, 'name': name, 'type': mtype, 'subject': subject, 'message': message, 'to': xml.attrib.get('to', '')}) + def _handlePresence(self, presence): xml = presence.xml diff --git a/sleekxmpp/stanza/message.py b/sleekxmpp/stanza/message.py index 37e2e5f3..3f9eb2ba 100644 --- a/sleekxmpp/stanza/message.py +++ b/sleekxmpp/stanza/message.py @@ -19,6 +19,12 @@ class Message(StanzaBase): self['type'] = 'normal' return self + def reply(self, body=None): + StanzaBase.reply(self) + if body is not None: + self['body'] = body + return self + if __name__ == '__main__': m = Message() m['to'] = 'me' diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 787ae5e3..b21354e4 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -1,5 +1,22 @@ from xml.etree import cElementTree as ET +class JID(object): + def __init__(self, jid): + self.jid = jid + + def __getattr__(self, name): + if name == 'resource': + return self.jid.split('/', 1)[-1] + elif name == 'user': + return self.jid.split('@', 1)[0] + elif name == 'server': + return self.jid.split('@', 1)[-1].split('/', 1)[0] + elif name == 'full': + return self.jid + + def __str__(self): + return self.jid + class StanzaBase(object): name = 'stanza' namespace = 'jabber:client' @@ -22,7 +39,7 @@ class StanzaBase(object): def match(self, xml): return xml.tag == self.tag - + def __getitem__(self, attrib): if attrib in self.interfaces: if hasattr(self, "get%s" % attrib.title()): @@ -85,11 +102,35 @@ class StanzaBase(object): def reply(self): self['from'], self['to'] = self['to'], self['from'] + self.clear() return self def error(self): self['type'] = 'error' + def getTo(self): + return JID(self._getAttr('to')) + + def setTo(self, value): + return self._setAttr('to', str(value)) + + def getFrom(self): + return JID(self._getAttr('from')) + + def setFrom(self, value): + return self._setAttr('from', str(value)) + + def getValues(self): + out = {} + for interface in self.interfaces: + out[interface] = self[interface] + return out + + def setValues(self, attrib): + for interface in attrib: + if interface in self.interfaces: + self[interface] = attrib[interface] + def _setAttr(self, name, value): self.xml.attrib[name] = value @@ -138,18 +179,19 @@ class StanzaBase(object): else: ixmlns = '' nsbuffer = '' - #if xmlns != ixmlns and ixmlns != '': - # if ixmlns in self.namespace_map: - # if self.namespace_map[ixmlns] != '': - # itag = "%s:%s" % (self.namespace_map[ixmlns], itag) - # else: - # nsbuffer = """ xmlns="%s\"""" % ixmlns + if xmlns != ixmlns and ixmlns != '' and ixmlns != self.stream.default_ns: + if ixmlns in self.stream.namespace_map: + if self.stream.namespace_map[ixmlns] != '': + itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag) + else: + nsbuffer = """ xmlns="%s\"""" % ixmlns if ixmlns not in (xmlns, self.namespace): nsbuffer = """ xmlns="%s\"""" % ixmlns newoutput.append("<%s" % itag) newoutput.append(nsbuffer) for attrib in xml.attrib: - newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) + if '{' not in attrib: + newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) if len(xml) or xml.text or xml.tail: newoutput.append(">") if xml.text: diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 8971a02c..e6107642 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -257,7 +257,6 @@ class XMLStream(object): break if stanza is None: stanza = StanzaBase(self, xmlobj) - logging.debug(self.__handlers) for handler in self.__handlers: if handler.match(xmlobj): handler.prerun(stanza) |