mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
tweaked stanzas for easy use
This commit is contained in:
parent
007b04dd30
commit
a031dd24a6
6 changed files with 80 additions and 30 deletions
|
@ -14,9 +14,8 @@ class Example(sleekxmpp.ClientXMPP):
|
||||||
self.getRoster()
|
self.getRoster()
|
||||||
self.sendPresence()
|
self.sendPresence()
|
||||||
|
|
||||||
def message(self, event):
|
def message(self, msg):
|
||||||
print("******got a message")
|
msg.reply("Thanks for sending\n%(body)s" % msg).send()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
#parse command line arguements
|
#parse command line arguements
|
||||||
|
|
|
@ -30,6 +30,8 @@ from . xmlstream.matcher.many import MatchMany
|
||||||
from . xmlstream.handler.callback import Callback
|
from . xmlstream.handler.callback import Callback
|
||||||
from . xmlstream.stanzabase import StanzaBase
|
from . xmlstream.stanzabase import StanzaBase
|
||||||
from . xmlstream import xmlstream as xmlstreammod
|
from . xmlstream import xmlstream as xmlstreammod
|
||||||
|
from . stanza.message import Message
|
||||||
|
from . stanza.iq import Iq
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import base64
|
import base64
|
||||||
|
|
|
@ -354,24 +354,26 @@ class basexmpp(object):
|
||||||
return fulljid.split('/', 1)[0]
|
return fulljid.split('/', 1)[0]
|
||||||
|
|
||||||
def _handleMessage(self, msg):
|
def _handleMessage(self, msg):
|
||||||
xml = msg.xml
|
self.event('message', msg)
|
||||||
ns = xml.tag.split('}')[0]
|
#xml = msg.xml
|
||||||
if ns == 'message':
|
#ns = xml.tag.split('}')[0]
|
||||||
ns = ''
|
#if ns == 'message':
|
||||||
else:
|
# ns = ''
|
||||||
ns = "%s}" % ns
|
#else:
|
||||||
mfrom = xml.attrib['from']
|
# ns = "%s}" % ns
|
||||||
message = xml.find('%sbody' % ns).text
|
#mfrom = xml.attrib['from']
|
||||||
subject = xml.find('%ssubject' % ns)
|
#message = xml.find('%sbody' % ns).text
|
||||||
if subject is not None:
|
#subject = xml.find('%ssubject' % ns)
|
||||||
subject = subject.text
|
#if subject is not None:
|
||||||
else:
|
# subject = subject.text
|
||||||
subject = ''
|
#else:
|
||||||
resource = self.getjidresource(mfrom)
|
# subject = ''
|
||||||
mfrom = self.getjidbare(mfrom)
|
#resource = self.getjidresource(mfrom)
|
||||||
mtype = xml.attrib.get('type', 'normal')
|
#mfrom = self.getjidbare(mfrom)
|
||||||
name = self.roster.get('name', '')
|
#mtype = xml.attrib.get('type', 'normal')
|
||||||
self.event("message", {'jid': mfrom, 'resource': resource, 'name': name, 'type': mtype, 'subject': subject, 'message': message, 'to': xml.attrib.get('to', '')})
|
#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):
|
def _handlePresence(self, presence):
|
||||||
xml = presence.xml
|
xml = presence.xml
|
||||||
|
|
|
@ -19,6 +19,12 @@ class Message(StanzaBase):
|
||||||
self['type'] = 'normal'
|
self['type'] = 'normal'
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def reply(self, body=None):
|
||||||
|
StanzaBase.reply(self)
|
||||||
|
if body is not None:
|
||||||
|
self['body'] = body
|
||||||
|
return self
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
m = Message()
|
m = Message()
|
||||||
m['to'] = 'me'
|
m['to'] = 'me'
|
||||||
|
|
|
@ -1,5 +1,22 @@
|
||||||
from xml.etree import cElementTree as ET
|
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):
|
class StanzaBase(object):
|
||||||
name = 'stanza'
|
name = 'stanza'
|
||||||
namespace = 'jabber:client'
|
namespace = 'jabber:client'
|
||||||
|
@ -22,7 +39,7 @@ class StanzaBase(object):
|
||||||
|
|
||||||
def match(self, xml):
|
def match(self, xml):
|
||||||
return xml.tag == self.tag
|
return xml.tag == self.tag
|
||||||
|
|
||||||
def __getitem__(self, attrib):
|
def __getitem__(self, attrib):
|
||||||
if attrib in self.interfaces:
|
if attrib in self.interfaces:
|
||||||
if hasattr(self, "get%s" % attrib.title()):
|
if hasattr(self, "get%s" % attrib.title()):
|
||||||
|
@ -85,11 +102,35 @@ class StanzaBase(object):
|
||||||
|
|
||||||
def reply(self):
|
def reply(self):
|
||||||
self['from'], self['to'] = self['to'], self['from']
|
self['from'], self['to'] = self['to'], self['from']
|
||||||
|
self.clear()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def error(self):
|
def error(self):
|
||||||
self['type'] = 'error'
|
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):
|
def _setAttr(self, name, value):
|
||||||
self.xml.attrib[name] = value
|
self.xml.attrib[name] = value
|
||||||
|
|
||||||
|
@ -138,18 +179,19 @@ class StanzaBase(object):
|
||||||
else:
|
else:
|
||||||
ixmlns = ''
|
ixmlns = ''
|
||||||
nsbuffer = ''
|
nsbuffer = ''
|
||||||
#if xmlns != ixmlns and ixmlns != '':
|
if xmlns != ixmlns and ixmlns != '' and ixmlns != self.stream.default_ns:
|
||||||
# if ixmlns in self.namespace_map:
|
if ixmlns in self.stream.namespace_map:
|
||||||
# if self.namespace_map[ixmlns] != '':
|
if self.stream.namespace_map[ixmlns] != '':
|
||||||
# itag = "%s:%s" % (self.namespace_map[ixmlns], itag)
|
itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag)
|
||||||
# else:
|
else:
|
||||||
# nsbuffer = """ xmlns="%s\"""" % ixmlns
|
nsbuffer = """ xmlns="%s\"""" % ixmlns
|
||||||
if ixmlns not in (xmlns, self.namespace):
|
if ixmlns not in (xmlns, self.namespace):
|
||||||
nsbuffer = """ xmlns="%s\"""" % ixmlns
|
nsbuffer = """ xmlns="%s\"""" % ixmlns
|
||||||
newoutput.append("<%s" % itag)
|
newoutput.append("<%s" % itag)
|
||||||
newoutput.append(nsbuffer)
|
newoutput.append(nsbuffer)
|
||||||
for attrib in xml.attrib:
|
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:
|
if len(xml) or xml.text or xml.tail:
|
||||||
newoutput.append(">")
|
newoutput.append(">")
|
||||||
if xml.text:
|
if xml.text:
|
||||||
|
|
|
@ -257,7 +257,6 @@ class XMLStream(object):
|
||||||
break
|
break
|
||||||
if stanza is None:
|
if stanza is None:
|
||||||
stanza = StanzaBase(self, xmlobj)
|
stanza = StanzaBase(self, xmlobj)
|
||||||
logging.debug(self.__handlers)
|
|
||||||
for handler in self.__handlers:
|
for handler in self.__handlers:
|
||||||
if handler.match(xmlobj):
|
if handler.match(xmlobj):
|
||||||
handler.prerun(stanza)
|
handler.prerun(stanza)
|
||||||
|
|
Loading…
Reference in a new issue