From 646a609c0b4e19203b4e2190df9b58fbe40c43fb Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 22 Jun 2010 23:09:50 -0400 Subject: Added plugin and tests for XEP-0033, Extended Stanza Addresses. XEP-0033 can be useful for interacting with XMPP<->Email gateways. --- sleekxmpp/plugins/xep_0033.py | 159 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 sleekxmpp/plugins/xep_0033.py (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py new file mode 100644 index 00000000..09d17ae0 --- /dev/null +++ b/sleekxmpp/plugins/xep_0033.py @@ -0,0 +1,159 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file license.txt for copying permission. +""" + +import logging +from . import base +from .. xmlstream.handler.callback import Callback +from .. xmlstream.matcher.xpath import MatchXPath +from .. xmlstream.stanzabase import ElementBase, ET, JID +from .. stanza.message import Message + + +class Addresses(ElementBase): + namespace = 'http://jabber.org/protocol/address' + name = 'addresses' + plugin_attrib = 'addresses' + interfaces = set(('addresses', 'bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to')) + + def addAddress(self, atype='to', jid='', node='', uri='', desc='', delivered=False): + address = Address(parent=self) + address['type'] = atype + address['jid'] = jid + address['node'] = node + address['uri'] = uri + address['desc'] = desc + address['delivered'] = delivered + return address + + def getAddresses(self, atype=None): + addresses = [] + for addrXML in self.xml.findall('{%s}address' % Address.namespace): + # ElementTree 1.2.6 does not support [@attr='value'] in findall + if atype is not None and addrXML.get('type') == atype: + self.xml.remove(addrXML) + addresses.append(Address(xml=addrXML, parent=None)) + return addresses + + def setAddresses(self, addresses, set_type=None): + self.delAddresses(set_type) + for addr in addresses: + # Remap 'type' to 'atype' to match the add method + if set_type is not None: + addr['type'] = set_type + curr_type = addr.get('type', None) + if curr_type is not None: + del addr['type'] + addr['atype'] = curr_type + self.addAddress(**addr) + + def delAddresses(self, atype=None): + for addrXML in self.xml.findall('{%s}address' % Address.namespace): + # ElementTree 1.2.6 does not support [@attr='value'] in findall + if atype is not None and addrXML.get('type') == atype: + self.xml.remove(addrXML) + + # -------------------------------------------------------------- + + def delBcc(self): + self.delAddresses('bcc') + + def delCc(self): + self.delAddresses('cc') + + def delNoreply(self): + self.delAddresses('noreply') + + def delReplyroom(self): + self.delAddresses('replyroom') + + def delReplyto(self): + self.delAddresses('replyto') + + def delTo(self): + self.delAddresses('to') + + # -------------------------------------------------------------- + + def getBcc(self): + return self.getAddresses('bcc') + + def getCc(self): + return self.getAddresses('cc') + + def getNoreply(self): + return self.getAddresses('noreply') + + def getReplyroom(self): + return self.getAddresses('replyroom') + + def getReplyto(self): + return self.getAddresses('replyto') + + def getTo(self): + return self.getAddresses('to') + + # -------------------------------------------------------------- + + def setBcc(self, addresses): + self.setAddresses(addresses, 'bcc') + + def setCc(self, addresses): + self.setAddresses(addresses, 'cc') + + def setNoreply(self, addresses): + self.setAddresses(addresses, 'noreply') + + def setReplyroom(self, addresses): + self.setAddresses(addresses, 'replyroom') + + def setReplyto(self, addresses): + self.setAddresses(addresses, 'replyto') + + def setTo(self, addresses): + self.setAddresses(addresses, 'to') + + +class Address(ElementBase): + namespace = 'http://jabber.org/protocol/address' + name = 'address' + plugin_attrib = 'address' + interfaces = set(('delivered', 'desc', 'jid', 'node', 'type', 'uri')) + address_types = set(('bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to')) + + def getDelivered(self): + return self.attrib.get('delivered', False) + + def setDelivered(self, delivered): + if delivered: + self.xml.attrib['delivered'] = "true" + else: + del self['delivered'] + + def setUri(self, uri): + if uri: + del self['jid'] + del self['node'] + self.xml.attrib['uri'] = uri + elif 'uri' in self.xml.attrib: + del self.xml.attrib['uri'] + + +class xep_0030(base.base_plugin): + """ + XEP-0033: Extended Stanza Addressing + """ + + def plugin_init(self): + self.xep = '0033' + self.description = 'Extended Stanza Addressing' + + self.xmpp.stanzaPlugin(Message, Addresses) + + def post_init(self): + base.base_plugin.post_init(self) + self.xmpp.plugin['xep_0030'].add_feature(Addresses.namespace) -- cgit v1.2.3 From acb53ba371952e70a53e96d7c7b71961b68b36a8 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 27 Jun 2010 10:14:21 -0400 Subject: Fixed tab and spacing issue to please the Tab Nanny during unit tests. --- sleekxmpp/plugins/xep_0033.py | 44 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index 09d17ae0..76bea6ab 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -30,7 +30,7 @@ class Addresses(ElementBase): address['delivered'] = delivered return address - def getAddresses(self, atype=None): + def getAddresses(self, atype=None): addresses = [] for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall @@ -39,7 +39,7 @@ class Addresses(ElementBase): addresses.append(Address(xml=addrXML, parent=None)) return addresses - def setAddresses(self, addresses, set_type=None): + def setAddresses(self, addresses, set_type=None): self.delAddresses(set_type) for addr in addresses: # Remap 'type' to 'atype' to match the add method @@ -51,7 +51,7 @@ class Addresses(ElementBase): addr['atype'] = curr_type self.addAddress(**addr) - def delAddresses(self, atype=None): + def delAddresses(self, atype=None): for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall if atype is not None and addrXML.get('type') == atype: @@ -62,59 +62,59 @@ class Addresses(ElementBase): def delBcc(self): self.delAddresses('bcc') - def delCc(self): + def delCc(self): self.delAddresses('cc') - def delNoreply(self): + def delNoreply(self): self.delAddresses('noreply') - def delReplyroom(self): + def delReplyroom(self): self.delAddresses('replyroom') - def delReplyto(self): + def delReplyto(self): self.delAddresses('replyto') - def delTo(self): + def delTo(self): self.delAddresses('to') # -------------------------------------------------------------- - def getBcc(self): + def getBcc(self): return self.getAddresses('bcc') def getCc(self): return self.getAddresses('cc') - def getNoreply(self): + def getNoreply(self): return self.getAddresses('noreply') - def getReplyroom(self): + def getReplyroom(self): return self.getAddresses('replyroom') - def getReplyto(self): + def getReplyto(self): return self.getAddresses('replyto') - def getTo(self): + def getTo(self): return self.getAddresses('to') # -------------------------------------------------------------- - def setBcc(self, addresses): + def setBcc(self, addresses): self.setAddresses(addresses, 'bcc') - def setCc(self, addresses): + def setCc(self, addresses): self.setAddresses(addresses, 'cc') - def setNoreply(self, addresses): + def setNoreply(self, addresses): self.setAddresses(addresses, 'noreply') - def setReplyroom(self, addresses): + def setReplyroom(self, addresses): self.setAddresses(addresses, 'replyroom') - def setReplyto(self, addresses): + def setReplyto(self, addresses): self.setAddresses(addresses, 'replyto') - def setTo(self, addresses): + def setTo(self, addresses): self.setAddresses(addresses, 'to') @@ -123,9 +123,9 @@ class Address(ElementBase): name = 'address' plugin_attrib = 'address' 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.attrib.get('delivered', False) def setDelivered(self, delivered): @@ -134,7 +134,7 @@ class Address(ElementBase): else: del self['delivered'] - def setUri(self, uri): + def setUri(self, uri): if uri: del self['jid'] del self['node'] -- cgit v1.2.3 From 059cc9ccc4c41942f6a9b2ba3baab443a91d2a60 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 27 Jun 2010 17:32:16 -0400 Subject: Fixed several errors in xep_0033 plugin. The method getAddresses was removing addresses by mistake. Several instances of using self.attrib instead of self.xml.attrib. --- sleekxmpp/plugins/xep_0033.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index 76bea6ab..80d58a40 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -34,14 +34,14 @@ class Addresses(ElementBase): addresses = [] for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall - if atype is not None and addrXML.get('type') == atype: - self.xml.remove(addrXML) - addresses.append(Address(xml=addrXML, parent=None)) + if atype is None or addrXML.attrib.get('type') == atype: + addresses.append(Address(xml=addrXML, parent=None)) return addresses def setAddresses(self, addresses, set_type=None): self.delAddresses(set_type) for addr in addresses: + addr = dict(addr) # Remap 'type' to 'atype' to match the add method if set_type is not None: addr['type'] = set_type @@ -52,9 +52,11 @@ class Addresses(ElementBase): self.addAddress(**addr) def delAddresses(self, atype=None): + if atype is None: + return for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall - if atype is not None and addrXML.get('type') == atype: + if addrXML.attrib.get('type') == atype: self.xml.remove(addrXML) # -------------------------------------------------------------- @@ -126,7 +128,7 @@ class Address(ElementBase): address_types = set(('bcc', 'cc', 'noreply', 'replyroom', 'replyto', 'to')) def getDelivered(self): - return self.attrib.get('delivered', False) + return self.xml.attrib.get('delivered', False) def setDelivered(self, delivered): if delivered: -- cgit v1.2.3 From 37ada498029c12459e5724ed27db92c828ce2a38 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 27 Jun 2010 17:39:16 -0400 Subject: Fixed indentation to please tab nanny during unit tests. --- sleekxmpp/plugins/xep_0033.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index 80d58a40..89770dc1 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -35,7 +35,7 @@ class Addresses(ElementBase): for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall 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 def setAddresses(self, addresses, set_type=None): -- cgit v1.2.3 From d0cb400c544abb432fa9b67038381be8172c2efb Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 11 Jul 2010 21:43:51 -0400 Subject: Fixed tabs to please tab nanny. --- sleekxmpp/plugins/xep_0033.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index 89770dc1..df8bb88d 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -41,7 +41,7 @@ class Addresses(ElementBase): def setAddresses(self, addresses, set_type=None): self.delAddresses(set_type) for addr in addresses: - addr = dict(addr) + addr = dict(addr) # Remap 'type' to 'atype' to match the add method if set_type is not None: addr['type'] = set_type @@ -52,8 +52,8 @@ class Addresses(ElementBase): self.addAddress(**addr) def delAddresses(self, atype=None): - if atype is None: - return + if atype is None: + return for addrXML in self.xml.findall('{%s}address' % Address.namespace): # ElementTree 1.2.6 does not support [@attr='value'] in findall if addrXML.attrib.get('type') == atype: -- cgit v1.2.3 From d5e42ac0e7282500583bf17f21eb2f944600ce76 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 19 Jul 2010 13:58:53 -0400 Subject: Condensed all of the stanzaPlugin functions into a single registerStanzaPlugin function. Updated plugins and tests to use new function. --- sleekxmpp/plugins/xep_0033.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index df8bb88d..9af27e32 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -10,7 +10,7 @@ import logging from . import base from .. xmlstream.handler.callback import Callback from .. xmlstream.matcher.xpath import MatchXPath -from .. xmlstream.stanzabase import ElementBase, ET, JID +from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET, JID from .. stanza.message import Message @@ -154,7 +154,7 @@ class xep_0030(base.base_plugin): self.xep = '0033' self.description = 'Extended Stanza Addressing' - self.xmpp.stanzaPlugin(Message, Addresses) + registerStanzaPlugin(Message, Addresses) def post_init(self): base.base_plugin.post_init(self) -- cgit v1.2.3 From 690eaf8d3c3856c6242612da22e6c6d323f193ed Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 20 Jul 2010 11:19:49 -0400 Subject: Updated license notices to use the correct MIT format. Also corrected references to nonexistant license.txt to LICENSE. --- sleekxmpp/plugins/xep_0033.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sleekxmpp/plugins/xep_0033.py') diff --git a/sleekxmpp/plugins/xep_0033.py b/sleekxmpp/plugins/xep_0033.py index 9af27e32..ea0b10b7 100644 --- a/sleekxmpp/plugins/xep_0033.py +++ b/sleekxmpp/plugins/xep_0033.py @@ -3,7 +3,7 @@ Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout This file is part of SleekXMPP. - See the file license.txt for copying permission. + See the file LICENSE for copying permission. """ import logging -- cgit v1.2.3