summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0033.py
blob: 80d58a4085ebe14df62645744c65f70b1a98357b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
    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 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
			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):
                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:
				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.xml.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)