summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/stanza_pubsub.py
blob: b407b79973360e9ae4520df670276ead471de2c0 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from .. xmlstream.stanzabase import ElementBase, ET
from .. stanza.iq import Iq
from .. basexmpp import basexmpp
from .. xmlstream.xmlstream import XMLStream
from . import xep_0004


def stanzaPlugin(stanza, plugin):                                                                       
    stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin                                             
    stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin 

class Pubsub(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'pubsub'
	plugin_attrib = 'pubsub'
	interfaces = set((
		'create',
		'configure',
		'subscribe',
		'options',
		'default',
		'items',
		'publish',
		'retract',
		'subscription',
		'subscriptions',
		'unsubscribe',
	))

stanzaPlugin(Iq, Pubsub)

class Affiliations(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'affiliations'
	plugin_attrib = 'affiliations'
	interfaces = set(tuple())

	def __init__(self, *args, **kwargs):
		ElementBase.__init__(self, *args, **kwargs)
		self.affiliations = []
		self.idx = 0

	def __iter__(self):
		self.idx = 0
		return self
	
	def __next__(self):
		self.idx += 1
		if self.idx + 1 > len(self.affilations):
			self.idx = 0
			raise StopIteration
		return self.affiliations[self.idx]
	
	def __len__(self):
		return len(self.affiliations)
	
	def append(self, affiliation):
		if not isinstance(affiliation, Affiliation):
			raise TypeError
		self.xml.append(affiliation.xml)
		return self.affiliations.append(affiliation)
	
	def pop(self, idx=0):
		aff = self.affiliations.pop(idx)
		self.xml.remove(aff.xml)
		return aff
	
	def find(self, affilation):
		return self.affilations.find(affiliation)

stanzaPlugin(Pubsub, Affiliations)

class Affiliation(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'affiliation'
	plugin_attrib = name
	interfaces = set(('node', 'affiliation'))

class Items(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'items'
	plugin_attrib = 'items'
	interfaces = set(tuple())

	def __init__(self, *args, **kwargs):
		ElementBase.__init__(self, *args, **kwargs)
		self.items = []
		self.idx = 0

	def __iter__(self):
		self.idx = 0
		return self
	
	def __next__(self):
		self.idx += 1
		if self.idx + 1 > len(self.items):
			self.idx = 0
			raise StopIteration
		return self.items[self.idx]
	
	def __len__(self):
		return len(self.items)
	
	def append(self, item):
		if not isinstance(item, Item):
			raise TypeError
		self.xml.append(item.xml)
		return self.items.append(item)
	
	def pop(self, idx=0):
		aff = self.items.pop(idx)
		self.xml.remove(aff.xml)
		return aff
	
	def find(self, item):
		return self.items.find(item)

stanzaPlugin(Pubsub, Items)

class Item(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'affiliation'
	plugin_attrib = name
	interfaces = set(('node', 'affiliation'))
class DefaultConfig(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub'
	name = 'default'
	plugin_attrib = 'defaultconfig'
	interfaces = set(('node', 'type', 'config'))
	
	def __init__(self, *args, **kwargs):
		ElementBase.__init__(self, *args, **kwargs)
		
	def getConfig(self):
		config = self.xml.find('{jabber:x:data}x')
		form = xep_0004.Form()
		if config is not None:
			form.fromXML(config)
		return form
	
	def setConfig(self, value):
		self.xml.append(value.getXML())
		return self
	
	def delConfig(self):
		config = self.xml.find('{jabber:x:data}x')
		self.xml.remove(config)

stanzaPlugin(Pubsub, DefaultConfig)

iq = Iq()
aff1 = Affiliation()
aff1['node'] = 'testnode'
aff1['affiliation'] = 'owner'
aff2 = Affiliation()
aff2['node'] = 'testnode2'
aff2['affiliation'] = 'publisher'
iq['pubsub']['affiliations'].append(aff1)
iq['pubsub']['affiliations'].append(aff2)
print(iq)
iq['pubsub']['affiliations'].pop(0)
print(iq)

iq = Iq()
iq['pubsub']['defaultconfig']
print(iq)
	
class OwnerAffiliations(Affiliations):
	pass

class OwnerAffiation(Affiliation):
	namespace = 'http://jabber.org/protocol/pubsub#owner'
	interfaces = set(('node', 'affiliation', 'jid'))

class PubSubOwner(ElementBase):
	namespace = 'http://jabber.org/protocol/pubsub#owner'
	nick = 'pubsubowner'