Move XEP-0033 to new system.

Still needs updating to the new format.
This commit is contained in:
Lance Stout 2012-03-11 22:35:26 -07:00
parent 6f337b5425
commit d43cd9fa54

View file

@ -7,155 +7,160 @@
""" """
import logging import logging
from . import base from sleekxmpp import Message
from .. xmlstream.handler.callback import Callback from sleekxmpp.xmlstream.handler.callback import Callback
from .. xmlstream.matcher.xpath import MatchXPath from sleekxmpp.xmlstream.matcher.xpath import MatchXPath
from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET, JID from sleekxmpp.xmlstream import register_stanza_plugin, ElementBase, ET, JID
from .. stanza.message import Message from sleekxmpp.plugins import BasePlugin, register_plugin
class Addresses(ElementBase): class Addresses(ElementBase):
namespace = 'http://jabber.org/protocol/address' namespace = 'http://jabber.org/protocol/address'
name = 'addresses' name = 'addresses'
plugin_attrib = 'addresses' plugin_attrib = 'addresses'
interfaces = set(('addresses', 'bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to')) interfaces = set(('addresses', 'bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to'))
def addAddress(self, atype='to', jid='', node='', uri='', desc='', delivered=False): def addAddress(self, atype='to', jid='', node='', uri='', desc='', delivered=False):
address = Address(parent=self) address = Address(parent=self)
address['type'] = atype address['type'] = atype
address['jid'] = jid address['jid'] = jid
address['node'] = node address['node'] = node
address['uri'] = uri address['uri'] = uri
address['desc'] = desc address['desc'] = desc
address['delivered'] = delivered address['delivered'] = delivered
return address return address
def getAddresses(self, atype=None): def getAddresses(self, atype=None):
addresses = [] addresses = []
for addrXML in self.xml.findall('{%s}address' % Address.namespace): for addrXML in self.xml.findall('{%s}address' % Address.namespace):
# ElementTree 1.2.6 does not support [@attr='value'] in findall # ElementTree 1.2.6 does not support [@attr='value'] in findall
if atype is None or addrXML.attrib.get('type') == atype: if atype is None or addrXML.attrib.get('type') == atype:
addresses.append(Address(xml=addrXML, parent=None)) addresses.append(Address(xml=addrXML, parent=None))
return addresses return addresses
def setAddresses(self, addresses, set_type=None): def setAddresses(self, addresses, set_type=None):
self.delAddresses(set_type) self.delAddresses(set_type)
for addr in addresses: for addr in addresses:
addr = dict(addr) addr = dict(addr)
# Remap 'type' to 'atype' to match the add method # Remap 'type' to 'atype' to match the add method
if set_type is not None: if set_type is not None:
addr['type'] = set_type addr['type'] = set_type
curr_type = addr.get('type', None) curr_type = addr.get('type', None)
if curr_type is not None: if curr_type is not None:
del addr['type'] del addr['type']
addr['atype'] = curr_type addr['atype'] = curr_type
self.addAddress(**addr) self.addAddress(**addr)
def delAddresses(self, atype=None): def delAddresses(self, atype=None):
if atype is None: if atype is None:
return return
for addrXML in self.xml.findall('{%s}address' % Address.namespace): for addrXML in self.xml.findall('{%s}address' % Address.namespace):
# ElementTree 1.2.6 does not support [@attr='value'] in findall # ElementTree 1.2.6 does not support [@attr='value'] in findall
if addrXML.attrib.get('type') == atype: if addrXML.attrib.get('type') == atype:
self.xml.remove(addrXML) self.xml.remove(addrXML)
# -------------------------------------------------------------- # --------------------------------------------------------------
def delBcc(self): def delBcc(self):
self.delAddresses('bcc') self.delAddresses('bcc')
def delCc(self): def delCc(self):
self.delAddresses('cc') self.delAddresses('cc')
def delNoreply(self): def delNoreply(self):
self.delAddresses('noreply') self.delAddresses('noreply')
def delReplyroom(self): def delReplyroom(self):
self.delAddresses('replyroom') self.delAddresses('replyroom')
def delReplyto(self): def delReplyto(self):
self.delAddresses('replyto') self.delAddresses('replyto')
def delTo(self): def delTo(self):
self.delAddresses('to') self.delAddresses('to')
# -------------------------------------------------------------- # --------------------------------------------------------------
def getBcc(self): def getBcc(self):
return self.getAddresses('bcc') return self.getAddresses('bcc')
def getCc(self): def getCc(self):
return self.getAddresses('cc') return self.getAddresses('cc')
def getNoreply(self): def getNoreply(self):
return self.getAddresses('noreply') return self.getAddresses('noreply')
def getReplyroom(self): def getReplyroom(self):
return self.getAddresses('replyroom') return self.getAddresses('replyroom')
def getReplyto(self): def getReplyto(self):
return self.getAddresses('replyto') return self.getAddresses('replyto')
def getTo(self): def getTo(self):
return self.getAddresses('to') return self.getAddresses('to')
# -------------------------------------------------------------- # --------------------------------------------------------------
def setBcc(self, addresses): def setBcc(self, addresses):
self.setAddresses(addresses, 'bcc') self.setAddresses(addresses, 'bcc')
def setCc(self, addresses): def setCc(self, addresses):
self.setAddresses(addresses, 'cc') self.setAddresses(addresses, 'cc')
def setNoreply(self, addresses): def setNoreply(self, addresses):
self.setAddresses(addresses, 'noreply') self.setAddresses(addresses, 'noreply')
def setReplyroom(self, addresses): def setReplyroom(self, addresses):
self.setAddresses(addresses, 'replyroom') self.setAddresses(addresses, 'replyroom')
def setReplyto(self, addresses): def setReplyto(self, addresses):
self.setAddresses(addresses, 'replyto') self.setAddresses(addresses, 'replyto')
def setTo(self, addresses): def setTo(self, addresses):
self.setAddresses(addresses, 'to') self.setAddresses(addresses, 'to')
class Address(ElementBase): class Address(ElementBase):
namespace = 'http://jabber.org/protocol/address' namespace = 'http://jabber.org/protocol/address'
name = 'address' name = 'address'
plugin_attrib = 'address' plugin_attrib = 'address'
interfaces = set(('delivered', 'desc', 'jid', 'node', 'type', 'uri')) interfaces = set(('delivered', 'desc', 'jid', 'node', 'type', 'uri'))
address_types = set(('bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to')) address_types = set(('bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to'))
def getDelivered(self): def getDelivered(self):
return self.xml.attrib.get('delivered', False) return self.xml.attrib.get('delivered', False)
def setDelivered(self, delivered): def setDelivered(self, delivered):
if delivered: if delivered:
self.xml.attrib['delivered'] = "true" self.xml.attrib['delivered'] = "true"
else: else:
del self['delivered'] del self['delivered']
def setUri(self, uri): def setUri(self, uri):
if uri: if uri:
del self['jid'] del self['jid']
del self['node'] del self['node']
self.xml.attrib['uri'] = uri self.xml.attrib['uri'] = uri
elif 'uri' in self.xml.attrib: elif 'uri' in self.xml.attrib:
del self.xml.attrib['uri'] del self.xml.attrib['uri']
class xep_0033(base.base_plugin): class XEP_0033(BasePlugin):
"""
XEP-0033: Extended Stanza Addressing
"""
def plugin_init(self): """
self.xep = '0033' XEP-0033: Extended Stanza Addressing
self.description = 'Extended Stanza Addressing' """
registerStanzaPlugin(Message, Addresses) name = 'xep_0033'
description = 'XEP-0033: Extended Stanza Addressing'
dependencies = set(['xep_0033'])
def post_init(self): def plugin_init(self):
base.base_plugin.post_init(self) self.xep = '0033'
self.xmpp.plugin['xep_0030'].add_feature(Addresses.namespace)
register_stanza_plugin(Message, Addresses)
self.xmpp.plugin['xep_0030'].add_feature(Addresses.namespace)
register_plugin(XEP_0033)