mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-24 03:00:15 +00:00
Issue 26. Only set from address in reply() for components
This commit is contained in:
parent
66e92c6c9f
commit
d70a6e6f32
3 changed files with 635 additions and 628 deletions
|
@ -30,231 +30,232 @@ from . import plugins
|
|||
#from . import stanza
|
||||
srvsupport = True
|
||||
try:
|
||||
import dns.resolver
|
||||
import dns.resolver
|
||||
except ImportError:
|
||||
srvsupport = False
|
||||
srvsupport = False
|
||||
|
||||
|
||||
|
||||
#class PresenceStanzaType(object):
|
||||
#
|
||||
# def fromXML(self, xml):
|
||||
# self.ptype = xml.get('type')
|
||||
# def fromXML(self, xml):
|
||||
# self.ptype = xml.get('type')
|
||||
|
||||
|
||||
class ClientXMPP(basexmpp, XMLStream):
|
||||
"""SleekXMPP's client class. Use only for good, not evil."""
|
||||
"""SleekXMPP's client class. Use only for good, not evil."""
|
||||
|
||||
def __init__(self, jid, password, ssl=False, plugin_config = {}, plugin_whitelist=[], escape_quotes=True):
|
||||
global srvsupport
|
||||
XMLStream.__init__(self)
|
||||
self.default_ns = 'jabber:client'
|
||||
basexmpp.__init__(self)
|
||||
self.plugin_config = plugin_config
|
||||
self.escape_quotes = escape_quotes
|
||||
self.set_jid(jid)
|
||||
self.plugin_whitelist = plugin_whitelist
|
||||
self.auto_reconnect = True
|
||||
self.srvsupport = srvsupport
|
||||
self.password = password
|
||||
self.registered_features = []
|
||||
self.stream_header = """<stream:stream to='%s' xmlns:stream='http://etherx.jabber.org/streams' xmlns='%s' version='1.0'>""" % (self.server,self.default_ns)
|
||||
self.stream_footer = "</stream:stream>"
|
||||
#self.map_namespace('http://etherx.jabber.org/streams', 'stream')
|
||||
#self.map_namespace('jabber:client', '')
|
||||
self.features = []
|
||||
#TODO: Use stream state here
|
||||
self.authenticated = False
|
||||
self.sessionstarted = False
|
||||
self.bound = False
|
||||
self.bindfail = False
|
||||
self.registerHandler(Callback('Stream Features', MatchXPath('{http://etherx.jabber.org/streams}features'), self._handleStreamFeatures, thread=True))
|
||||
self.registerHandler(Callback('Roster Update', MatchXPath('{%s}iq/{jabber:iq:roster}query' % self.default_ns), self._handleRoster, thread=True))
|
||||
#self.registerHandler(Callback('Roster Update', MatchXMLMask("<presence xmlns='%s' type='subscribe' />" % self.default_ns), self._handlePresenceSubscribe, thread=True))
|
||||
self.registerFeature("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />", self.handler_starttls, True)
|
||||
self.registerFeature("<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_sasl_auth, True)
|
||||
self.registerFeature("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind' />", self.handler_bind_resource)
|
||||
self.registerFeature("<session xmlns='urn:ietf:params:xml:ns:xmpp-session' />", self.handler_start_session)
|
||||
def __init__(self, jid, password, ssl=False, plugin_config = {}, plugin_whitelist=[], escape_quotes=True):
|
||||
global srvsupport
|
||||
XMLStream.__init__(self)
|
||||
self.default_ns = 'jabber:client'
|
||||
basexmpp.__init__(self)
|
||||
self.plugin_config = plugin_config
|
||||
self.escape_quotes = escape_quotes
|
||||
self.set_jid(jid)
|
||||
self.plugin_whitelist = plugin_whitelist
|
||||
self.auto_reconnect = True
|
||||
self.srvsupport = srvsupport
|
||||
self.password = password
|
||||
self.registered_features = []
|
||||
self.stream_header = """<stream:stream to='%s' xmlns:stream='http://etherx.jabber.org/streams' xmlns='%s' version='1.0'>""" % (self.server,self.default_ns)
|
||||
self.stream_footer = "</stream:stream>"
|
||||
#self.map_namespace('http://etherx.jabber.org/streams', 'stream')
|
||||
#self.map_namespace('jabber:client', '')
|
||||
self.features = []
|
||||
#TODO: Use stream state here
|
||||
self.authenticated = False
|
||||
self.sessionstarted = False
|
||||
self.bound = False
|
||||
self.bindfail = False
|
||||
self.is_component = False
|
||||
self.registerHandler(Callback('Stream Features', MatchXPath('{http://etherx.jabber.org/streams}features'), self._handleStreamFeatures, thread=True))
|
||||
self.registerHandler(Callback('Roster Update', MatchXPath('{%s}iq/{jabber:iq:roster}query' % self.default_ns), self._handleRoster, thread=True))
|
||||
#self.registerHandler(Callback('Roster Update', MatchXMLMask("<presence xmlns='%s' type='subscribe' />" % self.default_ns), self._handlePresenceSubscribe, thread=True))
|
||||
self.registerFeature("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />", self.handler_starttls, True)
|
||||
self.registerFeature("<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_sasl_auth, True)
|
||||
self.registerFeature("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind' />", self.handler_bind_resource)
|
||||
self.registerFeature("<session xmlns='urn:ietf:params:xml:ns:xmpp-session' />", self.handler_start_session)
|
||||
|
||||
#self.registerStanzaExtension('PresenceStanza', PresenceStanzaType)
|
||||
#self.register_plugins()
|
||||
#self.registerStanzaExtension('PresenceStanza', PresenceStanzaType)
|
||||
#self.register_plugins()
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key in self.plugin:
|
||||
return self.plugin[key]
|
||||
else:
|
||||
logging.warning("""Plugin "%s" is not loaded.""" % key)
|
||||
return False
|
||||
def __getitem__(self, key):
|
||||
if key in self.plugin:
|
||||
return self.plugin[key]
|
||||
else:
|
||||
logging.warning("""Plugin "%s" is not loaded.""" % key)
|
||||
return False
|
||||
|
||||
def get(self, key, default):
|
||||
return self.plugin.get(key, default)
|
||||
def get(self, key, default):
|
||||
return self.plugin.get(key, default)
|
||||
|
||||
def connect(self, address=tuple()):
|
||||
"""Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses
|
||||
the JID server."""
|
||||
if not address or len(address) < 2:
|
||||
if not self.srvsupport:
|
||||
logging.debug("Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using server hostname from JID.")
|
||||
else:
|
||||
logging.debug("Since no address is supplied, attempting SRV lookup.")
|
||||
try:
|
||||
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.server, dns.rdatatype.SRV)
|
||||
except dns.resolver.NXDOMAIN:
|
||||
logging.debug("No appropriate SRV record found. Using JID server name.")
|
||||
else:
|
||||
# pick a random answer, weighted by priority
|
||||
# there are less verbose ways of doing this (random.choice() with answer * priority), but I chose this way anyway
|
||||
# suggestions are welcome
|
||||
addresses = {}
|
||||
intmax = 0
|
||||
priorities = []
|
||||
for answer in answers:
|
||||
intmax += answer.priority
|
||||
addresses[intmax] = (answer.target.to_text()[:-1], answer.port)
|
||||
priorities.append(intmax) # sure, I could just do priorities = addresses.keys()\n priorities.sort()
|
||||
picked = random.randint(0, intmax)
|
||||
for priority in priorities:
|
||||
if picked <= priority:
|
||||
address = addresses[priority]
|
||||
break
|
||||
if not address:
|
||||
# if all else fails take server from JID.
|
||||
address = (self.server, 5222)
|
||||
result = XMLStream.connect(self, address[0], address[1], use_tls=True)
|
||||
if result:
|
||||
self.event("connected")
|
||||
else:
|
||||
logging.warning("Failed to connect")
|
||||
self.event("disconnected")
|
||||
return result
|
||||
def connect(self, address=tuple()):
|
||||
"""Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses
|
||||
the JID server."""
|
||||
if not address or len(address) < 2:
|
||||
if not self.srvsupport:
|
||||
logging.debug("Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using server hostname from JID.")
|
||||
else:
|
||||
logging.debug("Since no address is supplied, attempting SRV lookup.")
|
||||
try:
|
||||
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.server, dns.rdatatype.SRV)
|
||||
except dns.resolver.NXDOMAIN:
|
||||
logging.debug("No appropriate SRV record found. Using JID server name.")
|
||||
else:
|
||||
# pick a random answer, weighted by priority
|
||||
# there are less verbose ways of doing this (random.choice() with answer * priority), but I chose this way anyway
|
||||
# suggestions are welcome
|
||||
addresses = {}
|
||||
intmax = 0
|
||||
priorities = []
|
||||
for answer in answers:
|
||||
intmax += answer.priority
|
||||
addresses[intmax] = (answer.target.to_text()[:-1], answer.port)
|
||||
priorities.append(intmax) # sure, I could just do priorities = addresses.keys()\n priorities.sort()
|
||||
picked = random.randint(0, intmax)
|
||||
for priority in priorities:
|
||||
if picked <= priority:
|
||||
address = addresses[priority]
|
||||
break
|
||||
if not address:
|
||||
# if all else fails take server from JID.
|
||||
address = (self.server, 5222)
|
||||
result = XMLStream.connect(self, address[0], address[1], use_tls=True)
|
||||
if result:
|
||||
self.event("connected")
|
||||
else:
|
||||
logging.warning("Failed to connect")
|
||||
self.event("disconnected")
|
||||
return result
|
||||
|
||||
# overriding reconnect and disconnect so that we can get some events
|
||||
# should events be part of or required by xmlstream? Maybe that would be cleaner
|
||||
def reconnect(self):
|
||||
logging.info("Reconnecting")
|
||||
self.event("disconnected")
|
||||
XMLStream.reconnect(self)
|
||||
# overriding reconnect and disconnect so that we can get some events
|
||||
# should events be part of or required by xmlstream? Maybe that would be cleaner
|
||||
def reconnect(self):
|
||||
logging.info("Reconnecting")
|
||||
self.event("disconnected")
|
||||
XMLStream.reconnect(self)
|
||||
|
||||
def disconnect(self, init=True, close=False, reconnect=False):
|
||||
self.event("disconnected")
|
||||
XMLStream.disconnect(self, reconnect)
|
||||
def disconnect(self, init=True, close=False, reconnect=False):
|
||||
self.event("disconnected")
|
||||
XMLStream.disconnect(self, reconnect)
|
||||
|
||||
def registerFeature(self, mask, pointer, breaker = False):
|
||||
"""Register a stream feature."""
|
||||
self.registered_features.append((MatchXMLMask(mask), pointer, breaker))
|
||||
def registerFeature(self, mask, pointer, breaker = False):
|
||||
"""Register a stream feature."""
|
||||
self.registered_features.append((MatchXMLMask(mask), pointer, breaker))
|
||||
|
||||
def updateRoster(self, jid, name=None, subscription=None, groups=[]):
|
||||
"""Add or change a roster item."""
|
||||
iq = self.Iq().setStanzaValues({'type': 'set'})
|
||||
iq['roster']['items'] = {jid: {'name': name, 'subscription': subscription, 'groups': groups}}
|
||||
#self.send(iq, self.Iq().setValues({'id': iq['id']}))
|
||||
r = iq.send()
|
||||
return r['type'] == 'result'
|
||||
def updateRoster(self, jid, name=None, subscription=None, groups=[]):
|
||||
"""Add or change a roster item."""
|
||||
iq = self.Iq().setStanzaValues({'type': 'set'})
|
||||
iq['roster']['items'] = {jid: {'name': name, 'subscription': subscription, 'groups': groups}}
|
||||
#self.send(iq, self.Iq().setValues({'id': iq['id']}))
|
||||
r = iq.send()
|
||||
return r['type'] == 'result'
|
||||
|
||||
def delRosterItem(self, jid):
|
||||
iq = self.Iq()
|
||||
iq['type'] = 'set'
|
||||
iq['roster']['items'] = {jid: {'subscription': 'remove'}}
|
||||
return iq.send()['type'] == 'result'
|
||||
def delRosterItem(self, jid):
|
||||
iq = self.Iq()
|
||||
iq['type'] = 'set'
|
||||
iq['roster']['items'] = {jid: {'subscription': 'remove'}}
|
||||
return iq.send()['type'] == 'result'
|
||||
|
||||
def getRoster(self):
|
||||
"""Request the roster be sent."""
|
||||
iq = self.Iq().setStanzaValues({'type': 'get'}).enable('roster').send()
|
||||
self._handleRoster(iq, request=True)
|
||||
def getRoster(self):
|
||||
"""Request the roster be sent."""
|
||||
iq = self.Iq().setStanzaValues({'type': 'get'}).enable('roster').send()
|
||||
self._handleRoster(iq, request=True)
|
||||
|
||||
def _handleStreamFeatures(self, features):
|
||||
self.features = []
|
||||
for sub in features.xml:
|
||||
self.features.append(sub.tag)
|
||||
for subelement in features.xml:
|
||||
for feature in self.registered_features:
|
||||
if feature[0].match(subelement):
|
||||
#if self.maskcmp(subelement, feature[0], True):
|
||||
if feature[1](subelement) and feature[2]: #if breaker, don't continue
|
||||
return True
|
||||
def _handleStreamFeatures(self, features):
|
||||
self.features = []
|
||||
for sub in features.xml:
|
||||
self.features.append(sub.tag)
|
||||
for subelement in features.xml:
|
||||
for feature in self.registered_features:
|
||||
if feature[0].match(subelement):
|
||||
#if self.maskcmp(subelement, feature[0], True):
|
||||
if feature[1](subelement) and feature[2]: #if breaker, don't continue
|
||||
return True
|
||||
|
||||
def handler_starttls(self, xml):
|
||||
if not self.authenticated and self.ssl_support:
|
||||
self.add_handler("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls' />", self.handler_tls_start, name='TLS Proceed', instream=True)
|
||||
self.sendXML(xml)
|
||||
return True
|
||||
else:
|
||||
logging.warning("The module tlslite is required in to some servers, and has not been found.")
|
||||
return False
|
||||
def handler_starttls(self, xml):
|
||||
if not self.authenticated and self.ssl_support:
|
||||
self.add_handler("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls' />", self.handler_tls_start, name='TLS Proceed', instream=True)
|
||||
self.sendXML(xml)
|
||||
return True
|
||||
else:
|
||||
logging.warning("The module tlslite is required in to some servers, and has not been found.")
|
||||
return False
|
||||
|
||||
def handler_tls_start(self, xml):
|
||||
logging.debug("Starting TLS")
|
||||
if self.startTLS():
|
||||
raise RestartStream()
|
||||
def handler_tls_start(self, xml):
|
||||
logging.debug("Starting TLS")
|
||||
if self.startTLS():
|
||||
raise RestartStream()
|
||||
|
||||
def handler_sasl_auth(self, xml):
|
||||
if '{urn:ietf:params:xml:ns:xmpp-tls}starttls' in self.features:
|
||||
return False
|
||||
logging.debug("Starting SASL Auth")
|
||||
self.add_handler("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_auth_success, name='SASL Sucess', instream=True)
|
||||
self.add_handler("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_auth_fail, name='SASL Failure', instream=True)
|
||||
sasl_mechs = xml.findall('{urn:ietf:params:xml:ns:xmpp-sasl}mechanism')
|
||||
if len(sasl_mechs):
|
||||
for sasl_mech in sasl_mechs:
|
||||
self.features.append("sasl:%s" % sasl_mech.text)
|
||||
if 'sasl:PLAIN' in self.features:
|
||||
if sys.version_info < (3,0):
|
||||
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8'))
|
||||
else:
|
||||
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'))
|
||||
else:
|
||||
logging.error("No appropriate login method.")
|
||||
self.disconnect()
|
||||
#if 'sasl:DIGEST-MD5' in self.features:
|
||||
# self._auth_digestmd5()
|
||||
return True
|
||||
def handler_sasl_auth(self, xml):
|
||||
if '{urn:ietf:params:xml:ns:xmpp-tls}starttls' in self.features:
|
||||
return False
|
||||
logging.debug("Starting SASL Auth")
|
||||
self.add_handler("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_auth_success, name='SASL Sucess', instream=True)
|
||||
self.add_handler("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", self.handler_auth_fail, name='SASL Failure', instream=True)
|
||||
sasl_mechs = xml.findall('{urn:ietf:params:xml:ns:xmpp-sasl}mechanism')
|
||||
if len(sasl_mechs):
|
||||
for sasl_mech in sasl_mechs:
|
||||
self.features.append("sasl:%s" % sasl_mech.text)
|
||||
if 'sasl:PLAIN' in self.features:
|
||||
if sys.version_info < (3,0):
|
||||
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8'))
|
||||
else:
|
||||
self.send("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'))
|
||||
else:
|
||||
logging.error("No appropriate login method.")
|
||||
self.disconnect()
|
||||
#if 'sasl:DIGEST-MD5' in self.features:
|
||||
# self._auth_digestmd5()
|
||||
return True
|
||||
|
||||
def handler_auth_success(self, xml):
|
||||
self.authenticated = True
|
||||
self.features = []
|
||||
raise RestartStream()
|
||||
def handler_auth_success(self, xml):
|
||||
self.authenticated = True
|
||||
self.features = []
|
||||
raise RestartStream()
|
||||
|
||||
def handler_auth_fail(self, xml):
|
||||
logging.info("Authentication failed.")
|
||||
self.disconnect()
|
||||
self.event("failed_auth")
|
||||
def handler_auth_fail(self, xml):
|
||||
logging.info("Authentication failed.")
|
||||
self.disconnect()
|
||||
self.event("failed_auth")
|
||||
|
||||
def handler_bind_resource(self, xml):
|
||||
logging.debug("Requesting resource: %s" % self.resource)
|
||||
xml.clear()
|
||||
iq = self.Iq(stype='set')
|
||||
if self.resource:
|
||||
res = ET.Element('resource')
|
||||
res.text = self.resource
|
||||
xml.append(res)
|
||||
iq.append(xml)
|
||||
response = iq.send()
|
||||
#response = self.send(iq, self.Iq(sid=iq['id']))
|
||||
self.set_jid(response.xml.find('{urn:ietf:params:xml:ns:xmpp-bind}bind/{urn:ietf:params:xml:ns:xmpp-bind}jid').text)
|
||||
self.bound = True
|
||||
logging.info("Node set to: %s" % self.fulljid)
|
||||
if "{urn:ietf:params:xml:ns:xmpp-session}session" not in self.features or self.bindfail:
|
||||
logging.debug("Established Session")
|
||||
self.sessionstarted = True
|
||||
self.event("session_start")
|
||||
def handler_bind_resource(self, xml):
|
||||
logging.debug("Requesting resource: %s" % self.resource)
|
||||
xml.clear()
|
||||
iq = self.Iq(stype='set')
|
||||
if self.resource:
|
||||
res = ET.Element('resource')
|
||||
res.text = self.resource
|
||||
xml.append(res)
|
||||
iq.append(xml)
|
||||
response = iq.send()
|
||||
#response = self.send(iq, self.Iq(sid=iq['id']))
|
||||
self.set_jid(response.xml.find('{urn:ietf:params:xml:ns:xmpp-bind}bind/{urn:ietf:params:xml:ns:xmpp-bind}jid').text)
|
||||
self.bound = True
|
||||
logging.info("Node set to: %s" % self.fulljid)
|
||||
if "{urn:ietf:params:xml:ns:xmpp-session}session" not in self.features or self.bindfail:
|
||||
logging.debug("Established Session")
|
||||
self.sessionstarted = True
|
||||
self.event("session_start")
|
||||
|
||||
def handler_start_session(self, xml):
|
||||
if self.authenticated and self.bound:
|
||||
iq = self.makeIqSet(xml)
|
||||
response = iq.send()
|
||||
logging.debug("Established Session")
|
||||
self.sessionstarted = True
|
||||
self.event("session_start")
|
||||
else:
|
||||
#bind probably hasn't happened yet
|
||||
self.bindfail = True
|
||||
def handler_start_session(self, xml):
|
||||
if self.authenticated and self.bound:
|
||||
iq = self.makeIqSet(xml)
|
||||
response = iq.send()
|
||||
logging.debug("Established Session")
|
||||
self.sessionstarted = True
|
||||
self.event("session_start")
|
||||
else:
|
||||
#bind probably hasn't happened yet
|
||||
self.bindfail = True
|
||||
|
||||
def _handleRoster(self, iq, request=False):
|
||||
if iq['type'] == 'set' or (iq['type'] == 'result' and request):
|
||||
for jid in iq['roster']['items']:
|
||||
if not jid in self.roster:
|
||||
self.roster[jid] = {'groups': [], 'name': '', 'subscription': 'none', 'presence': {}, 'in_roster': True}
|
||||
self.roster[jid].update(iq['roster']['items'][jid])
|
||||
if iq['type'] == 'set':
|
||||
self.send(self.Iq().setStanzaValues({'type': 'result', 'id': iq['id']}).enable('roster'))
|
||||
self.event("roster_update", iq)
|
||||
def _handleRoster(self, iq, request=False):
|
||||
if iq['type'] == 'set' or (iq['type'] == 'result' and request):
|
||||
for jid in iq['roster']['items']:
|
||||
if not jid in self.roster:
|
||||
self.roster[jid] = {'groups': [], 'name': '', 'subscription': 'none', 'presence': {}, 'in_roster': True}
|
||||
self.roster[jid].update(iq['roster']['items'][jid])
|
||||
if iq['type'] == 'set':
|
||||
self.send(self.Iq().setStanzaValues({'type': 'result', 'id': iq['id']}).enable('roster'))
|
||||
self.event("roster_update", iq)
|
||||
|
|
|
@ -30,59 +30,60 @@ from . import stanza
|
|||
import hashlib
|
||||
srvsupport = True
|
||||
try:
|
||||
import dns.resolver
|
||||
import dns.resolver
|
||||
except ImportError:
|
||||
srvsupport = False
|
||||
srvsupport = False
|
||||
|
||||
|
||||
class ComponentXMPP(basexmpp, XMLStream):
|
||||
"""SleekXMPP's client class. Use only for good, not evil."""
|
||||
"""SleekXMPP's client class. Use only for good, not evil."""
|
||||
|
||||
def __init__(self, jid, secret, host, port, plugin_config = {}, plugin_whitelist=[], use_jc_ns=False):
|
||||
XMLStream.__init__(self)
|
||||
if use_jc_ns:
|
||||
self.default_ns = 'jabber:client'
|
||||
else:
|
||||
self.default_ns = 'jabber:component:accept'
|
||||
basexmpp.__init__(self)
|
||||
self.auto_authorize = None
|
||||
self.stream_header = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='%s'>" % jid
|
||||
self.stream_footer = "</stream:stream>"
|
||||
self.server_host = host
|
||||
self.server_port = port
|
||||
self.set_jid(jid)
|
||||
self.secret = secret
|
||||
self.registerHandler(Callback('Handshake', MatchXPath('{jabber:component:accept}handshake'), self._handleHandshake))
|
||||
def __init__(self, jid, secret, host, port, plugin_config = {}, plugin_whitelist=[], use_jc_ns=False):
|
||||
XMLStream.__init__(self)
|
||||
if use_jc_ns:
|
||||
self.default_ns = 'jabber:client'
|
||||
else:
|
||||
self.default_ns = 'jabber:component:accept'
|
||||
basexmpp.__init__(self)
|
||||
self.auto_authorize = None
|
||||
self.stream_header = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='%s'>" % jid
|
||||
self.stream_footer = "</stream:stream>"
|
||||
self.server_host = host
|
||||
self.server_port = port
|
||||
self.set_jid(jid)
|
||||
self.secret = secret
|
||||
self.is_component = True
|
||||
self.registerHandler(Callback('Handshake', MatchXPath('{jabber:component:accept}handshake'), self._handleHandshake))
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key in self.plugin:
|
||||
return self.plugin[key]
|
||||
else:
|
||||
logging.warning("""Plugin "%s" is not loaded.""" % key)
|
||||
return False
|
||||
def __getitem__(self, key):
|
||||
if key in self.plugin:
|
||||
return self.plugin[key]
|
||||
else:
|
||||
logging.warning("""Plugin "%s" is not loaded.""" % key)
|
||||
return False
|
||||
|
||||
def get(self, key, default):
|
||||
return self.plugin.get(key, default)
|
||||
def get(self, key, default):
|
||||
return self.plugin.get(key, default)
|
||||
|
||||
def incoming_filter(self, xmlobj):
|
||||
if xmlobj.tag.startswith('{jabber:client}'):
|
||||
xmlobj.tag = xmlobj.tag.replace('jabber:client', self.default_ns)
|
||||
for sub in xmlobj:
|
||||
self.incoming_filter(sub)
|
||||
return xmlobj
|
||||
def incoming_filter(self, xmlobj):
|
||||
if xmlobj.tag.startswith('{jabber:client}'):
|
||||
xmlobj.tag = xmlobj.tag.replace('jabber:client', self.default_ns)
|
||||
for sub in xmlobj:
|
||||
self.incoming_filter(sub)
|
||||
return xmlobj
|
||||
|
||||
def start_stream_handler(self, xml):
|
||||
sid = xml.get('id', '')
|
||||
handshake = ET.Element('{jabber:component:accept}handshake')
|
||||
if sys.version_info < (3,0):
|
||||
handshake.text = hashlib.sha1("%s%s" % (sid, self.secret)).hexdigest().lower()
|
||||
else:
|
||||
handshake.text = hashlib.sha1(bytes("%s%s" % (sid, self.secret), 'utf-8')).hexdigest().lower()
|
||||
self.sendXML(handshake)
|
||||
def start_stream_handler(self, xml):
|
||||
sid = xml.get('id', '')
|
||||
handshake = ET.Element('{jabber:component:accept}handshake')
|
||||
if sys.version_info < (3,0):
|
||||
handshake.text = hashlib.sha1("%s%s" % (sid, self.secret)).hexdigest().lower()
|
||||
else:
|
||||
handshake.text = hashlib.sha1(bytes("%s%s" % (sid, self.secret), 'utf-8')).hexdigest().lower()
|
||||
self.sendXML(handshake)
|
||||
|
||||
def _handleHandshake(self, xml):
|
||||
self.event("session_start")
|
||||
def _handleHandshake(self, xml):
|
||||
self.event("session_start")
|
||||
|
||||
def connect(self):
|
||||
logging.debug("Connecting to %s:%s" % (self.server_host, self.server_port))
|
||||
return xmlstreammod.XMLStream.connect(self, self.server_host, self.server_port)
|
||||
def connect(self):
|
||||
logging.debug("Connecting to %s:%s" % (self.server_host, self.server_port))
|
||||
return xmlstreammod.XMLStream.connect(self, self.server_host, self.server_port)
|
||||
|
|
|
@ -13,393 +13,398 @@ import weakref
|
|||
import copy
|
||||
|
||||
if sys.version_info < (3,0):
|
||||
from . import tostring26 as tostring
|
||||
from . import tostring26 as tostring
|
||||
else:
|
||||
from . import tostring
|
||||
from . import tostring
|
||||
|
||||
xmltester = type(ET.Element('xml'))
|
||||
|
||||
|
||||
def registerStanzaPlugin(stanza, plugin):
|
||||
"""
|
||||
Associate a stanza object as a plugin for another stanza.
|
||||
"""
|
||||
tag = "{%s}%s" % (plugin.namespace, plugin.name)
|
||||
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
|
||||
stanza.plugin_tag_map[tag] = plugin
|
||||
"""
|
||||
Associate a stanza object as a plugin for another stanza.
|
||||
"""
|
||||
tag = "{%s}%s" % (plugin.namespace, plugin.name)
|
||||
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
|
||||
stanza.plugin_tag_map[tag] = plugin
|
||||
|
||||
|
||||
class JID(object):
|
||||
def __init__(self, jid):
|
||||
self.jid = jid
|
||||
def __init__(self, jid):
|
||||
self.jid = jid
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'resource':
|
||||
return self.jid.split('/', 1)[-1]
|
||||
elif name == 'user':
|
||||
if '@' in self.jid:
|
||||
return self.jid.split('@', 1)[0]
|
||||
else:
|
||||
return ''
|
||||
elif name == 'server':
|
||||
return self.jid.split('@', 1)[-1].split('/', 1)[0]
|
||||
elif name == 'full':
|
||||
return self.jid
|
||||
elif name == 'bare':
|
||||
return self.jid.split('/', 1)[0]
|
||||
def __getattr__(self, name):
|
||||
if name == 'resource':
|
||||
return self.jid.split('/', 1)[-1]
|
||||
elif name == 'user':
|
||||
if '@' in self.jid:
|
||||
return self.jid.split('@', 1)[0]
|
||||
else:
|
||||
return ''
|
||||
elif name == 'server':
|
||||
return self.jid.split('@', 1)[-1].split('/', 1)[0]
|
||||
elif name == 'full':
|
||||
return self.jid
|
||||
elif name == 'bare':
|
||||
return self.jid.split('/', 1)[0]
|
||||
|
||||
def __str__(self):
|
||||
return self.jid
|
||||
def __str__(self):
|
||||
return self.jid
|
||||
|
||||
class ElementBase(tostring.ToString):
|
||||
name = 'stanza'
|
||||
plugin_attrib = 'plugin'
|
||||
namespace = 'jabber:client'
|
||||
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
||||
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
||||
sub_interfaces = tuple()
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
subitem = None
|
||||
name = 'stanza'
|
||||
plugin_attrib = 'plugin'
|
||||
namespace = 'jabber:client'
|
||||
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
||||
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
||||
sub_interfaces = tuple()
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
subitem = None
|
||||
|
||||
def __init__(self, xml=None, parent=None):
|
||||
if parent is None:
|
||||
self.parent = None
|
||||
else:
|
||||
self.parent = weakref.ref(parent)
|
||||
self.xml = xml
|
||||
self.plugins = {}
|
||||
self.iterables = []
|
||||
self.idx = 0
|
||||
if not self.setup(xml):
|
||||
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)
|
||||
if self.subitem is not None:
|
||||
for sub in self.subitem:
|
||||
if child.tag == "{%s}%s" % (sub.namespace, sub.name):
|
||||
self.iterables.append(sub(xml=child, parent=self))
|
||||
break
|
||||
def __init__(self, xml=None, parent=None):
|
||||
if parent is None:
|
||||
self.parent = None
|
||||
else:
|
||||
self.parent = weakref.ref(parent)
|
||||
self.xml = xml
|
||||
self.plugins = {}
|
||||
self.iterables = []
|
||||
self.idx = 0
|
||||
if not self.setup(xml):
|
||||
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)
|
||||
if self.subitem is not None:
|
||||
for sub in self.subitem:
|
||||
if child.tag == "{%s}%s" % (sub.namespace, sub.name):
|
||||
self.iterables.append(sub(xml=child, parent=self))
|
||||
break
|
||||
|
||||
|
||||
@property
|
||||
def attrib(self): #backwards compatibility
|
||||
return self
|
||||
@property
|
||||
def attrib(self): #backwards compatibility
|
||||
return self
|
||||
|
||||
def __iter__(self):
|
||||
self.idx = 0
|
||||
return self
|
||||
def __iter__(self):
|
||||
self.idx = 0
|
||||
return self
|
||||
|
||||
def __bool__(self):
|
||||
return True
|
||||
def __bool__(self):
|
||||
return True
|
||||
|
||||
def __next__(self):
|
||||
self.idx += 1
|
||||
if self.idx > len(self.iterables):
|
||||
self.idx = 0
|
||||
raise StopIteration
|
||||
return self.iterables[self.idx - 1]
|
||||
def __next__(self):
|
||||
self.idx += 1
|
||||
if self.idx > len(self.iterables):
|
||||
self.idx = 0
|
||||
raise StopIteration
|
||||
return self.iterables[self.idx - 1]
|
||||
|
||||
def next(self):
|
||||
return self.__next__()
|
||||
def next(self):
|
||||
return self.__next__()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.iterables)
|
||||
def __len__(self):
|
||||
return len(self.iterables)
|
||||
|
||||
def append(self, item):
|
||||
if not isinstance(item, ElementBase):
|
||||
if type(item) == xmltester:
|
||||
return self.appendxml(item)
|
||||
else:
|
||||
raise TypeError
|
||||
self.xml.append(item.xml)
|
||||
self.iterables.append(item)
|
||||
return self
|
||||
def append(self, item):
|
||||
if not isinstance(item, ElementBase):
|
||||
if type(item) == xmltester:
|
||||
return self.appendxml(item)
|
||||
else:
|
||||
raise TypeError
|
||||
self.xml.append(item.xml)
|
||||
self.iterables.append(item)
|
||||
return self
|
||||
|
||||
def pop(self, idx=0):
|
||||
aff = self.iterables.pop(idx)
|
||||
self.xml.remove(aff.xml)
|
||||
return aff
|
||||
def pop(self, idx=0):
|
||||
aff = self.iterables.pop(idx)
|
||||
self.xml.remove(aff.xml)
|
||||
return aff
|
||||
|
||||
def get(self, key, defaultvalue=None):
|
||||
value = self[key]
|
||||
if value is None or value == '':
|
||||
return defaultvalue
|
||||
return value
|
||||
def get(self, key, defaultvalue=None):
|
||||
value = self[key]
|
||||
if value is None or value == '':
|
||||
return defaultvalue
|
||||
return value
|
||||
|
||||
def keys(self):
|
||||
out = []
|
||||
out += [x for x in self.interfaces]
|
||||
out += [x for x in self.plugins]
|
||||
if self.iterables:
|
||||
out.append('substanzas')
|
||||
return tuple(out)
|
||||
def keys(self):
|
||||
out = []
|
||||
out += [x for x in self.interfaces]
|
||||
out += [x for x in self.plugins]
|
||||
if self.iterables:
|
||||
out.append('substanzas')
|
||||
return tuple(out)
|
||||
|
||||
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.plugin_attrib): return False
|
||||
founditerable = False
|
||||
for iterable in self.iterables:
|
||||
if nodes[1:] == []:
|
||||
break
|
||||
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
|
||||
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.plugin_attrib): return False
|
||||
founditerable = False
|
||||
for iterable in self.iterables:
|
||||
if nodes[1:] == []:
|
||||
break
|
||||
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
|
||||
|
||||
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
||||
return self.xml.find(xpath)
|
||||
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
||||
return self.xml.find(xpath)
|
||||
|
||||
def findall(self, xpath):
|
||||
return self.xml.findall(xpath)
|
||||
def findall(self, xpath):
|
||||
return self.xml.findall(xpath)
|
||||
|
||||
def setup(self, xml=None):
|
||||
if self.xml is None:
|
||||
self.xml = xml
|
||||
if self.xml is None:
|
||||
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)
|
||||
if self.parent is not None:
|
||||
self.parent().xml.append(self.xml)
|
||||
return True #had to generate XML
|
||||
else:
|
||||
return False
|
||||
def setup(self, xml=None):
|
||||
if self.xml is None:
|
||||
self.xml = xml
|
||||
if self.xml is None:
|
||||
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)
|
||||
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 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)
|
||||
def initPlugin(self, attrib):
|
||||
if attrib not in self.plugins:
|
||||
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
||||
|
||||
def __getitem__(self, attrib):
|
||||
if attrib == 'substanzas':
|
||||
return self.iterables
|
||||
elif attrib in self.interfaces:
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||
return self.plugins[attrib]
|
||||
else:
|
||||
return ''
|
||||
def __getitem__(self, attrib):
|
||||
if attrib == 'substanzas':
|
||||
return self.iterables
|
||||
elif attrib in self.interfaces:
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||
return self.plugins[attrib]
|
||||
else:
|
||||
return ''
|
||||
|
||||
def __setitem__(self, attrib, value):
|
||||
if attrib in self.interfaces:
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||
self.initPlugin(attrib)
|
||||
self.plugins[attrib][attrib] = value
|
||||
return self
|
||||
def __setitem__(self, attrib, value):
|
||||
if attrib in self.interfaces:
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||
self.initPlugin(attrib)
|
||||
self.plugins[attrib][attrib] = value
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib in self.plugins:
|
||||
del self.plugins[attrib]
|
||||
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)
|
||||
elif attrib in self.plugin_attrib_map:
|
||||
if attrib in self.plugins:
|
||||
del self.plugins[attrib]
|
||||
return self
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, ElementBase):
|
||||
return False
|
||||
values = self.getStanzaValues()
|
||||
for key in other:
|
||||
if key not in values or values[key] != other[key]:
|
||||
return False
|
||||
return True
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, ElementBase):
|
||||
return False
|
||||
values = self.getStanzaValues()
|
||||
for key in other:
|
||||
if key not in values or values[key] != other[key]:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _setAttr(self, name, value):
|
||||
if value is None or value == '':
|
||||
self.__delitem__(name)
|
||||
else:
|
||||
self.xml.attrib[name] = value
|
||||
def _setAttr(self, name, value):
|
||||
if value is None or value == '':
|
||||
self.__delitem__(name)
|
||||
else:
|
||||
self.xml.attrib[name] = value
|
||||
|
||||
def _delAttr(self, name):
|
||||
if name in self.xml.attrib:
|
||||
del self.xml.attrib[name]
|
||||
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 _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 _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):
|
||||
if text is None or text == '':
|
||||
return self.__delitem__(name)
|
||||
stanza = self.xml.find("{%s}%s" % (self.namespace, name))
|
||||
if stanza is None:
|
||||
#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
|
||||
return stanza
|
||||
def _setSubText(self, name, attrib={}, text=None):
|
||||
if text is None or text == '':
|
||||
return self.__delitem__(name)
|
||||
stanza = self.xml.find("{%s}%s" % (self.namespace, name))
|
||||
if stanza is None:
|
||||
#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
|
||||
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 _delSub(self, name):
|
||||
for child in self.xml.getchildren():
|
||||
if child.tag == "{%s}%s" % (self.namespace, name):
|
||||
self.xml.remove(child)
|
||||
|
||||
def getStanzaValues(self):
|
||||
out = {}
|
||||
for interface in self.interfaces:
|
||||
out[interface] = self[interface]
|
||||
for pluginkey in self.plugins:
|
||||
out[pluginkey] = self.plugins[pluginkey].getStanzaValues()
|
||||
if self.iterables:
|
||||
iterables = []
|
||||
for stanza in self.iterables:
|
||||
iterables.append(stanza.getStanzaValues())
|
||||
iterables[-1].update({'__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)})
|
||||
out['substanzas'] = iterables
|
||||
return out
|
||||
def getStanzaValues(self):
|
||||
out = {}
|
||||
for interface in self.interfaces:
|
||||
out[interface] = self[interface]
|
||||
for pluginkey in self.plugins:
|
||||
out[pluginkey] = self.plugins[pluginkey].getStanzaValues()
|
||||
if self.iterables:
|
||||
iterables = []
|
||||
for stanza in self.iterables:
|
||||
iterables.append(stanza.getStanzaValues())
|
||||
iterables[-1].update({'__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)})
|
||||
out['substanzas'] = iterables
|
||||
return out
|
||||
|
||||
def setStanzaValues(self, attrib):
|
||||
for interface in attrib:
|
||||
if interface == 'substanzas':
|
||||
for subdict in attrib['substanzas']:
|
||||
if '__childtag__' in subdict:
|
||||
for subclass in self.subitem:
|
||||
if subdict['__childtag__'] == "{%s}%s" % (subclass.namespace, subclass.name):
|
||||
sub = subclass(parent=self)
|
||||
sub.setStanzaValues(subdict)
|
||||
self.iterables.append(sub)
|
||||
break
|
||||
elif interface in self.interfaces:
|
||||
self[interface] = attrib[interface]
|
||||
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
||||
self.initPlugin(interface)
|
||||
if interface in self.plugins:
|
||||
self.plugins[interface].setStanzaValues(attrib[interface])
|
||||
return self
|
||||
def setStanzaValues(self, attrib):
|
||||
for interface in attrib:
|
||||
if interface == 'substanzas':
|
||||
for subdict in attrib['substanzas']:
|
||||
if '__childtag__' in subdict:
|
||||
for subclass in self.subitem:
|
||||
if subdict['__childtag__'] == "{%s}%s" % (subclass.namespace, subclass.name):
|
||||
sub = subclass(parent=self)
|
||||
sub.setStanzaValues(subdict)
|
||||
self.iterables.append(sub)
|
||||
break
|
||||
elif interface in self.interfaces:
|
||||
self[interface] = attrib[interface]
|
||||
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
||||
self.initPlugin(interface)
|
||||
if interface in self.plugins:
|
||||
self.plugins[interface].setStanzaValues(attrib[interface])
|
||||
return self
|
||||
|
||||
def appendxml(self, xml):
|
||||
self.xml.append(xml)
|
||||
return self
|
||||
def appendxml(self, xml):
|
||||
self.xml.append(xml)
|
||||
return self
|
||||
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
||||
|
||||
#def __del__(self): #prevents garbage collection of reference cycle
|
||||
# if self.parent is not None:
|
||||
# self.parent.xml.remove(self.xml)
|
||||
#def __del__(self): #prevents garbage collection of reference cycle
|
||||
# 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()
|
||||
name = 'stanza'
|
||||
namespace = 'jabber:client'
|
||||
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
||||
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
||||
sub_interfaces = tuple()
|
||||
|
||||
def __init__(self, stream=None, xml=None, stype=None, sto=None, sfrom=None, sid=None):
|
||||
self.stream = stream
|
||||
if stream is not None:
|
||||
self.namespace = stream.default_ns
|
||||
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
|
||||
self.tag = "{%s}%s" % (self.namespace, self.name)
|
||||
def __init__(self, stream=None, xml=None, stype=None, sto=None, sfrom=None, sid=None):
|
||||
self.stream = stream
|
||||
if stream is not None:
|
||||
self.namespace = stream.default_ns
|
||||
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
|
||||
self.tag = "{%s}%s" % (self.namespace, self.name)
|
||||
|
||||
def setType(self, value):
|
||||
if value in self.types:
|
||||
self.xml.attrib['type'] = value
|
||||
return self
|
||||
def setType(self, value):
|
||||
if value in self.types:
|
||||
self.xml.attrib['type'] = value
|
||||
return self
|
||||
|
||||
def getPayload(self):
|
||||
return self.xml.getchildren()
|
||||
def getPayload(self):
|
||||
return self.xml.getchildren()
|
||||
|
||||
def setPayload(self, value):
|
||||
self.xml.append(value)
|
||||
return self
|
||||
def setPayload(self, value):
|
||||
self.xml.append(value)
|
||||
return self
|
||||
|
||||
def delPayload(self):
|
||||
self.clear()
|
||||
return self
|
||||
def delPayload(self):
|
||||
self.clear()
|
||||
return self
|
||||
|
||||
def clear(self):
|
||||
for child in self.xml.getchildren():
|
||||
self.xml.remove(child)
|
||||
for plugin in list(self.plugins.keys()):
|
||||
del self.plugins[plugin]
|
||||
return self
|
||||
def clear(self):
|
||||
for child in self.xml.getchildren():
|
||||
self.xml.remove(child)
|
||||
for plugin in list(self.plugins.keys()):
|
||||
del self.plugins[plugin]
|
||||
return self
|
||||
|
||||
def reply(self):
|
||||
self['from'], self['to'] = self['to'], self['from']
|
||||
self.clear()
|
||||
return self
|
||||
def reply(self):
|
||||
# if it's a component, use from
|
||||
if self.stream and hasattr(self.stream, "is_component") and self.stream.is_component:
|
||||
self['from'], self['to'] = self['to'], self['from']
|
||||
else:
|
||||
self['to'] = self['from']
|
||||
del self['from']
|
||||
self.clear()
|
||||
return self
|
||||
|
||||
def error(self):
|
||||
self['type'] = 'error'
|
||||
return self
|
||||
def error(self):
|
||||
self['type'] = 'error'
|
||||
return self
|
||||
|
||||
def getTo(self):
|
||||
return JID(self._getAttr('to'))
|
||||
def getTo(self):
|
||||
return JID(self._getAttr('to'))
|
||||
|
||||
def setTo(self, value):
|
||||
return self._setAttr('to', str(value))
|
||||
def setTo(self, value):
|
||||
return self._setAttr('to', str(value))
|
||||
|
||||
def getFrom(self):
|
||||
return JID(self._getAttr('from'))
|
||||
def getFrom(self):
|
||||
return JID(self._getAttr('from'))
|
||||
|
||||
def setFrom(self, value):
|
||||
return self._setAttr('from', str(value))
|
||||
def setFrom(self, value):
|
||||
return self._setAttr('from', str(value))
|
||||
|
||||
def unhandled(self):
|
||||
pass
|
||||
def unhandled(self):
|
||||
pass
|
||||
|
||||
def exception(self, e):
|
||||
logging.error(traceback.format_tb(e))
|
||||
def exception(self, e):
|
||||
logging.error(traceback.format_tb(e))
|
||||
|
||||
def send(self):
|
||||
self.stream.sendRaw(self.__str__())
|
||||
def send(self):
|
||||
self.stream.sendRaw(self.__str__())
|
||||
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), stream=self.stream)
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), stream=self.stream)
|
||||
|
||||
|
|
Loading…
Reference in a new issue