2009-12-10 01:23:03 +00:00
|
|
|
from xml.etree import cElementTree as ET
|
2009-12-17 01:54:22 +00:00
|
|
|
import logging
|
2010-01-05 21:56:48 +00:00
|
|
|
import traceback
|
2010-01-29 10:04:15 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info < (3,0):
|
|
|
|
from . import tostring26 as tostring
|
|
|
|
else:
|
|
|
|
from . import tostring
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-01-14 15:37:44 +00:00
|
|
|
xmltester = type(ET.Element('xml'))
|
|
|
|
|
2009-12-10 07:33:59 +00:00
|
|
|
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':
|
2010-02-25 01:12:15 +00:00
|
|
|
if '@' in self.jid:
|
|
|
|
return self.jid.split('@', 1)[0]
|
|
|
|
else:
|
|
|
|
return ''
|
2009-12-10 07:33:59 +00:00
|
|
|
elif name == 'server':
|
|
|
|
return self.jid.split('@', 1)[-1].split('/', 1)[0]
|
|
|
|
elif name == 'full':
|
|
|
|
return self.jid
|
2009-12-11 01:29:46 +00:00
|
|
|
elif name == 'bare':
|
|
|
|
return self.jid.split('/', 1)[0]
|
2009-12-10 07:33:59 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.jid
|
|
|
|
|
2010-01-29 10:04:15 +00:00
|
|
|
class ElementBase(tostring.ToString):
|
2009-12-10 01:23:03 +00:00
|
|
|
name = 'stanza'
|
2009-12-11 01:29:46 +00:00
|
|
|
plugin_attrib = 'plugin'
|
2009-12-10 01:23:03 +00:00
|
|
|
namespace = 'jabber:client'
|
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
|
|
|
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
|
|
|
sub_interfaces = tuple()
|
2009-12-11 01:29:46 +00:00
|
|
|
plugin_attrib_map = {}
|
|
|
|
plugin_tag_map = {}
|
2010-01-08 01:45:11 +00:00
|
|
|
subitem = None
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-11 01:29:46 +00:00
|
|
|
def __init__(self, xml=None, parent=None):
|
2010-01-05 21:56:48 +00:00
|
|
|
self.attrib = self # backwards compatibility hack
|
2009-12-11 01:29:46 +00:00
|
|
|
self.parent = parent
|
2009-12-10 01:23:03 +00:00
|
|
|
self.xml = xml
|
2009-12-11 01:29:46 +00:00
|
|
|
self.plugins = {}
|
2010-01-08 01:45:11 +00:00
|
|
|
self.iterables = []
|
|
|
|
self.idx = 0
|
|
|
|
if not self.setup(xml):
|
2009-12-11 01:29:46 +00:00
|
|
|
for child in self.xml.getchildren():
|
|
|
|
if child.tag in self.plugin_tag_map:
|
|
|
|
self.plugins[self.plugin_tag_map[child.tag].plugin_attrib] = self.plugin_tag_map[child.tag](xml=child, parent=self)
|
2010-01-08 01:45:11 +00:00
|
|
|
if self.subitem is not None and child.tag == "{%s}%s" % (self.subitem.namespace, self.subitem.name):
|
|
|
|
self.iterables.append(self.subitem(xml=child, parent=self))
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
self.idx = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
self.idx += 1
|
|
|
|
if self.idx + 1 > len(self.iterables):
|
|
|
|
self.idx = 0
|
|
|
|
raise StopIteration
|
|
|
|
return self.affiliations[self.idx]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.iterables)
|
|
|
|
|
|
|
|
def append(self, item):
|
|
|
|
if not isinstance(item, ElementBase):
|
2010-01-14 15:37:44 +00:00
|
|
|
if type(item) == xmltester:
|
|
|
|
return self.appendxml(item)
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2010-01-08 01:45:11 +00:00
|
|
|
self.xml.append(item.xml)
|
2010-01-13 17:04:05 +00:00
|
|
|
self.iterables.append(item)
|
|
|
|
return self
|
2010-01-08 01:45:11 +00:00
|
|
|
|
|
|
|
def pop(self, idx=0):
|
|
|
|
aff = self.iterables.pop(idx)
|
|
|
|
self.xml.remove(aff.xml)
|
|
|
|
return aff
|
|
|
|
|
2010-01-09 21:17:08 +00:00
|
|
|
def get(self, key, defaultvalue=None):
|
|
|
|
value = self[key]
|
|
|
|
if value is None or value == '':
|
|
|
|
return defaultvalue
|
|
|
|
return value
|
|
|
|
|
2010-01-08 01:45:11 +00:00
|
|
|
def keys(self):
|
|
|
|
out = []
|
|
|
|
out += [x for x in self.interfaces]
|
|
|
|
out += [x for x in self.plugins]
|
|
|
|
if self.iterables:
|
|
|
|
out.append('substanzas')
|
2010-01-08 05:26:37 +00:00
|
|
|
return tuple(out)
|
2010-01-08 01:45:11 +00:00
|
|
|
|
2010-01-16 05:07:28 +00:00
|
|
|
def match(self, matchstring):
|
|
|
|
if isinstance(matchstring, str):
|
|
|
|
nodes = matchstring.split('/')
|
|
|
|
else:
|
|
|
|
nodes = matchstring
|
|
|
|
tagargs = nodes[0].split('@')
|
|
|
|
if tagargs[0] not in (self.plugins, self.name): return False
|
|
|
|
founditerable = False
|
|
|
|
for iterable in self.iterables:
|
|
|
|
founditerable = iterable.match(nodes[1:])
|
|
|
|
if founditerable: break;
|
|
|
|
for evals in tagargs[1:]:
|
|
|
|
x,y = evals.split('=')
|
|
|
|
if self[x] != y: return False
|
|
|
|
if not founditerable and len(nodes) > 1:
|
|
|
|
next = nodes[1].split('@')[0]
|
|
|
|
if next in self.plugins:
|
|
|
|
return self.plugins[next].match(nodes[1:])
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|
2009-12-10 07:33:59 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
|
|
|
return self.xml.find(xpath)
|
|
|
|
|
2009-12-11 01:29:46 +00:00
|
|
|
def setup(self, xml=None):
|
|
|
|
if self.xml is None:
|
|
|
|
self.xml = xml
|
|
|
|
if self.xml is None:
|
2009-12-17 01:54:22 +00:00
|
|
|
for ename in self.name.split('/'):
|
|
|
|
new = ET.Element("{%(namespace)s}%(name)s" % {'name': self.name, 'namespace': self.namespace})
|
|
|
|
if self.xml is None:
|
|
|
|
self.xml = new
|
|
|
|
else:
|
|
|
|
self.xml.append(new)
|
2009-12-11 01:29:46 +00:00
|
|
|
if self.parent is not None:
|
|
|
|
self.parent.xml.append(self.xml)
|
|
|
|
return True #had to generate XML
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def enable(self, attrib):
|
|
|
|
self.initPlugin(attrib)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def initPlugin(self, attrib):
|
|
|
|
if attrib not in self.plugins:
|
|
|
|
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def __getitem__(self, attrib):
|
2010-01-08 01:45:11 +00:00
|
|
|
if attrib == 'substanzas':
|
|
|
|
return self.iterables
|
|
|
|
elif attrib in self.interfaces:
|
2009-12-10 01:23:03 +00:00
|
|
|
if hasattr(self, "get%s" % attrib.title()):
|
|
|
|
return getattr(self, "get%s" % attrib.title())()
|
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
|
|
|
return self._getSubText(attrib)
|
|
|
|
else:
|
|
|
|
return self._getAttr(attrib)
|
2009-12-11 01:29:46 +00:00
|
|
|
elif attrib in self.plugin_attrib_map:
|
2009-12-17 01:54:22 +00:00
|
|
|
if attrib not in self.plugins: self.initPlugin(attrib)
|
2009-12-11 01:29:46 +00:00
|
|
|
return self.plugins[attrib]
|
2009-12-10 01:23:03 +00:00
|
|
|
else:
|
|
|
|
return ''
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def __setitem__(self, attrib, value):
|
2009-12-11 01:29:46 +00:00
|
|
|
if attrib in self.interfaces:
|
2009-12-10 01:23:03 +00:00
|
|
|
if value is not None:
|
|
|
|
if hasattr(self, "set%s" % attrib.title()):
|
|
|
|
getattr(self, "set%s" % attrib.title())(value,)
|
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
|
|
|
return self._setSubText(attrib, text=value)
|
|
|
|
else:
|
|
|
|
self._setAttr(attrib, value)
|
|
|
|
else:
|
|
|
|
self.__delitem__(attrib)
|
2009-12-17 01:54:22 +00:00
|
|
|
elif attrib in self.plugin_attrib_map:
|
|
|
|
if attrib not in self.plugins: self.initPlugin(attrib)
|
2009-12-11 01:29:46 +00:00
|
|
|
self.initPlugin(attrib)
|
2009-12-17 01:54:22 +00:00
|
|
|
self.plugins[attrib][attrib] = value
|
2009-12-10 01:23:03 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __delitem__(self, attrib):
|
|
|
|
if attrib.lower() in self.interfaces:
|
|
|
|
if hasattr(self, "del%s" % attrib.title()):
|
|
|
|
getattr(self, "del%s" % attrib.title())()
|
|
|
|
else:
|
|
|
|
if attrib in self.sub_interfaces:
|
|
|
|
return self._delSub(attrib)
|
|
|
|
else:
|
|
|
|
self._delAttr(attrib)
|
2009-12-17 01:54:22 +00:00
|
|
|
elif attrib in self.plugin_attrib_map:
|
2009-12-11 01:29:46 +00:00
|
|
|
if attrib in self.plugins:
|
|
|
|
del self.plugins[attrib]
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
values = self.getValues()
|
|
|
|
for key in other:
|
|
|
|
if key not in values or values[key] != other[key]:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _setAttr(self, name, value):
|
2009-12-17 01:54:22 +00:00
|
|
|
if value is None or value == '':
|
|
|
|
self.__delitem__(name)
|
|
|
|
else:
|
|
|
|
self.xml.attrib[name] = value
|
2009-12-11 01:29:46 +00:00
|
|
|
|
|
|
|
def _delAttr(self, name):
|
|
|
|
if name in self.xml.attrib:
|
|
|
|
del self.xml.attrib[name]
|
|
|
|
|
|
|
|
def _getAttr(self, name):
|
|
|
|
return self.xml.attrib.get(name, '')
|
|
|
|
|
|
|
|
def _getSubText(self, name):
|
|
|
|
stanza = self.xml.find("{%s}%s" % (self.namespace, name))
|
|
|
|
if stanza is None or stanza.text is None:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return stanza.text
|
|
|
|
|
|
|
|
def _setSubText(self, name, attrib={}, text=None):
|
2009-12-17 01:54:22 +00:00
|
|
|
if text is None or text == '':
|
|
|
|
return self.__delitem__(name)
|
2009-12-11 01:29:46 +00:00
|
|
|
stanza = self.xml.find("{%s}%s" % (self.namespace, name))
|
|
|
|
if stanza is None:
|
2009-12-17 01:54:22 +00:00
|
|
|
#self.xml.append(ET.Element("{%s}%s" % (self.namespace, name), attrib))
|
|
|
|
stanza = ET.Element("{%s}%s" % (self.namespace, name))
|
|
|
|
self.xml.append(stanza)
|
|
|
|
stanza.text = text
|
2009-12-11 01:29:46 +00:00
|
|
|
return stanza
|
|
|
|
|
|
|
|
def _delSub(self, name):
|
|
|
|
for child in self.xml.getchildren():
|
|
|
|
if child.tag == "{%s}%s" % (self.namespace, name):
|
|
|
|
self.xml.remove(child)
|
|
|
|
|
|
|
|
def getValues(self):
|
|
|
|
out = {}
|
|
|
|
for interface in self.interfaces:
|
|
|
|
out[interface] = self[interface]
|
2010-01-08 01:45:11 +00:00
|
|
|
for pluginkey in self.plugins:
|
|
|
|
out[pluginkey] = self.plugins[pluginkey].getValues()
|
|
|
|
if self.iterables:
|
|
|
|
iterables = [x.getValues() for x in self.iterables]
|
|
|
|
out['substanzas'] = iterables
|
2009-12-11 01:29:46 +00:00
|
|
|
return out
|
|
|
|
|
|
|
|
def setValues(self, attrib):
|
|
|
|
for interface in attrib:
|
2010-01-08 01:45:11 +00:00
|
|
|
if interface == 'substanzas':
|
|
|
|
for subdict in attrib['substanzas']:
|
|
|
|
sub = self.subitem(parent=self)
|
|
|
|
sub.setValues(subdict)
|
|
|
|
self.iterables.append(sub)
|
|
|
|
elif interface in self.interfaces:
|
2009-12-11 01:29:46 +00:00
|
|
|
self[interface] = attrib[interface]
|
2010-01-08 01:45:11 +00:00
|
|
|
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
|
|
|
self.initPlugin(interface)
|
|
|
|
if interface in self.plugins:
|
|
|
|
self.plugins[interface].setValues(attrib[interface])
|
2009-12-11 01:29:46 +00:00
|
|
|
return self
|
|
|
|
|
2010-01-13 17:04:05 +00:00
|
|
|
def appendxml(self, xml):
|
2009-12-11 01:29:46 +00:00
|
|
|
self.xml.append(xml)
|
2009-12-10 01:23:03 +00:00
|
|
|
return self
|
2009-12-11 01:29:46 +00:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self.parent is not None:
|
|
|
|
self.parent.xml.remove(self.xml)
|
|
|
|
|
|
|
|
class StanzaBase(ElementBase):
|
|
|
|
name = 'stanza'
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
|
|
|
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
|
|
|
sub_interfaces = tuple()
|
2009-12-10 01:23:03 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
def __init__(self, stream=None, xml=None, stype=None, sto=None, sfrom=None, sid=None):
|
2009-12-11 01:29:46 +00:00
|
|
|
self.stream = stream
|
|
|
|
ElementBase.__init__(self, xml)
|
|
|
|
if stype is not None:
|
|
|
|
self['type'] = stype
|
|
|
|
if sto is not None:
|
|
|
|
self['to'] = sto
|
|
|
|
if sfrom is not None:
|
|
|
|
self['from'] = sfrom
|
2010-01-05 21:56:48 +00:00
|
|
|
if stream is not None:
|
|
|
|
self.namespace = stream.default_ns
|
|
|
|
self.tag = "{%s}%s" % (self.namespace, self.name)
|
2009-12-11 01:29:46 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def setType(self, value):
|
|
|
|
if value in self.types:
|
|
|
|
self.xml.attrib['type'] = value
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getPayload(self):
|
|
|
|
return self.xml.getchildren()
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def setPayload(self, value):
|
|
|
|
self.xml.append(value)
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def delPayload(self):
|
|
|
|
self.clear()
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def clear(self):
|
|
|
|
for child in self.xml.getchildren():
|
|
|
|
self.xml.remove(child)
|
2009-12-22 10:05:53 +00:00
|
|
|
#for plugin in list(self.plugins.keys()):
|
|
|
|
# del self.plugins[plugin]
|
2009-12-10 01:23:03 +00:00
|
|
|
|
|
|
|
def reply(self):
|
|
|
|
self['from'], self['to'] = self['to'], self['from']
|
2009-12-10 07:33:59 +00:00
|
|
|
self.clear()
|
2009-12-10 01:23:03 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def error(self):
|
|
|
|
self['type'] = 'error'
|
|
|
|
|
2009-12-10 07:33:59 +00:00
|
|
|
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))
|
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def unhandled(self):
|
|
|
|
pass
|
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
def exception(self, e):
|
|
|
|
logging.error(traceback.format_tb(e))
|
2009-12-17 01:54:22 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
def send(self):
|
2010-01-29 10:04:15 +00:00
|
|
|
self.stream.sendRaw(self.__str__())
|
2009-12-10 01:23:03 +00:00
|
|
|
|