summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0004.py
blob: 86963632cb44288e7ff4e8ef7b8e05905f65b901 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
	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 for copying permission.
"""

import logging
import copy
from . import base
from .. xmlstream.handler.callback import Callback
from .. xmlstream.matcher.xpath import MatchXPath
from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET, JID
from .. stanza.message import Message


class Form(ElementBase):
	namespace = 'jabber:x:data'
	name = 'x'
	plugin_attrib = 'form'
	interfaces = set(('fields', 'instructions', 'items', 'reported', 'title', 'type', 'values'))
	sub_interfaces = set(('title',))
	form_types = set(('cancel', 'form', 'result', 'submit'))

	def __init__(self, *args, **kwargs):
		title = None
		if 'title' in kwargs:
			title = kwargs['title']
			del kwargs['title']
		ElementBase.__init__(self, *args, **kwargs)
		if title is not None:
			self['title'] = title
	
	def setup(self, xml=None):
		if ElementBase.setup(self, xml): #if we had to generate xml
			self['type'] = 'form'

	def addField(self, var='', ftype=None, label='', desc='', required=False, value=None, options=None, **kwargs):
		kwtype = kwargs.get('type', None)
		if kwtype is None:
			kwtype = ftype

		field = FormField(parent=self)
		field['var'] = var
		field['type'] = kwtype
		field['label'] = label
		field['desc'] = desc
		field['required'] = required
		field['value'] = value
		if options is not None:
			field['options'] = options
		return field

	def getXML(self):
		logging.warning("Form.getXML() is deprecated API compatibility with plugins/old_0004.py")
		return self.xml
	
	def fromXML(self, xml):
		logging.warning("Form.fromXML() is deprecated API compatibility with plugins/old_0004.py")
		n = Form(xml=xml)
		return n

	def addItem(self, values):
		itemXML = ET.Element('{%s}item' % self.namespace)
		self.xml.append(itemXML)
		reported_vars = self['reported'].keys()
		for var in reported_vars:
			fieldXML = ET.Element('{%s}field' % FormField.namespace)
			itemXML.append(fieldXML)
			field = FormField(xml=fieldXML)
			field['var'] = var
			field['value'] = values.get(var, None)

	def addReported(self, var, ftype=None, label='', desc='', **kwargs):
		kwtype = kwargs.get('type', None)
		if kwtype is None:
			kwtype = ftype
		reported = self.xml.find('{%s}reported' % self.namespace)
		if reported is None:
			reported = ET.Element('{%s}reported' % self.namespace)
			self.xml.append(reported)
		fieldXML = ET.Element('{%s}field' % FormField.namespace)
		reported.append(fieldXML)
		field = FormField(xml=fieldXML)
		field['var'] = var
		field['type'] = kwtype
		field['label'] = label
		field['desc'] = desc
		return field

	def cancel(self):
		self['type'] = 'cancel'

	def delFields(self):
		fieldsXML = self.xml.findall('{%s}field' % FormField.namespace)
		for fieldXML in fieldsXML:
			self.xml.remove(fieldXML)

	def delInstructions(self):
		instsXML = self.xml.findall('{%s}instructions')
		for instXML in instsXML:
			self.xml.remove(instXML)

	def delItems(self):
		itemsXML = self.xml.find('{%s}item' % self.namespace)
		for itemXML in itemsXML:
			self.xml.remove(itemXML)

	def delReported(self):
		reportedXML = self.xml.find('{%s}reported' % self.namespace)
		if reportedXML is not None:
			self.xml.remove(reportedXML)

	def getFields(self, use_dict=False):
		fields = {} if use_dict else []
		fieldsXML = self.xml.findall('{%s}field' % FormField.namespace)    
		for fieldXML in fieldsXML:
			field = FormField(xml=fieldXML)
			if use_dict:
				fields[field['var']] = field
			else:
				fields.append((field['var'], field))
		return fields

	def getInstructions(self):
		instructions = ''
		instsXML = self.xml.findall('{%s}instructions' % self.namespace)
		return "\n".join([instXML.text for instXML in instsXML])

	def getItems(self):
		items = []
		itemsXML = self.xml.findall('{%s}item' % self.namespace)
		for itemXML in itemsXML:
			item = {}
			fieldsXML = itemXML.findall('{%s}field' % FormField.namespace)
			for fieldXML in fieldsXML:
				field = FormField(xml=fieldXML)
				item[field['var']] = field['value']
			items.append(item)
		return items

	def getReported(self):
		fields = {}
		fieldsXML = self.xml.findall('{%s}reported/{%s}field' % (self.namespace, 
									 FormField.namespace))
		for fieldXML in fieldsXML:
			field = FormField(xml=fieldXML)
			fields[field['var']] = field
		return fields

	def getValues(self):
		values = {}
		fields = self.getFields(use_dict=True)
		for var in fields:
			values[var] = fields[var]['value']
		return values

	def reply(self):
		if self['type'] == 'form':
			self['type'] = 'submit'
		elif self['type'] == 'submit':
			self['type'] = 'result'

	def setFields(self, fields, default=None):
		del self['fields']
		for field_data in fields:
			var = field_data[0]
			field = field_data[1]
			field['var'] = var

			self.addField(**field)

	def setInstructions(self, instructions):
		del self['instructions']
		if instructions in [None, '']:
			return
		instructions = instructions.split('\n')
		for instruction in instructions:
			inst = ET.Element('{%s}instructions' % self.namespace)
			inst.text = instruction
			self.xml.append(inst)

	def setItems(self, items):
		for item in items:
			self.addItem(item)

	def setReported(self, reported, default=None):
		for var in reported:
			field = reported[var]
			field['var'] = var
			self.addReported(var, **field)

	def setValues(self, values):
		fields = self.getFields(use_dict=True)
		for field in values:
			fields[field]['value'] = values[field]


class FormField(ElementBase):
	namespace = 'jabber:x:data'
	name = 'field'
	plugin_attrib = 'field'
	interfaces = set(('answer', 'desc', 'required', 'value', 'options', 'label', 'type', 'var'))
	sub_interfaces = set(('desc',))
	field_types = set(('boolean', 'fixed', 'hidden', 'jid-multi', 'jid-single', 'list-multi',
			   'list-single', 'text-multi', 'text-private', 'text-single'))
	multi_value_types = set(('hidden', 'jid-multi', 'list-multi', 'text-multi'))
	multi_line_types = set(('hidden', 'text-multi'))
	option_types = set(('list-multi', 'list-single'))
	true_values = set((True, '1', 'true'))

	def addOption(self, label='', value=''):
		if self['type'] in self.option_types:
			opt = FieldOption(parent=self)
			opt['label'] = label
			opt['value'] = value
		else:
			raise ValueError("Cannot add options to a %s field." % self['type'])

	def delOptions(self):
		optsXML = self.xml.findall('{%s}option' % self.namespace)
		for optXML in optsXML:
			self.xml.remove(optXML)

	def delRequired(self):
		reqXML = self.xml.find('{%s}required' % self.namespace)
		if reqXML is not None:
			self.xml.remove(reqXML)

	def delValue(self):
		valsXML = self.xml.findall('{%s}value' % self.namespace)
		for valXML in valsXML:
			self.xml.remove(valXML)

	def getAnswer(self):
		return self.getValue()

	def getOptions(self):
		options = []
		optsXML = self.xml.findall('{%s}option' % self.namespace)
		for optXML in optsXML:
			opt = FieldOption(xml=optXML)
			options.append({'label': opt['label'], 'value':opt['value']})
		return options

	def getRequired(self):
		reqXML = self.xml.find('{%s}required' % self.namespace)
		return reqXML is not None

	def getValue(self):
		valsXML = self.xml.findall('{%s}value' % self.namespace)
		if len(valsXML) == 0:
			return None
		elif self['type'] == 'boolean':
			return valsXML[0].text in self.true_values
		elif self['type'] in self.multi_value_types:
			values = []
			for valXML in valsXML:
				if valXML.text is None:
					valXML.text = ''
				values.append(valXML.text)
			if self['type'] == 'text-multi':
				values = "\n".join(values)
			return values
		else:
			return valsXML[0].text

	def setAnswer(self, answer):
		self.setValue(answer)

	def setFalse(self):
		self.setValue(False)

	def setOptions(self, options):
		for value in options:
			if isinstance(value, dict):
				self.addOption(**value)
			else:
				self.addOption(value=value)

	def setRequired(self, required):
		exists = self.getRequired()
		if not exists and required:
			self.xml.append(ET.Element('{%s}required' % self.namespace))
		elif exists and not required:
			self.delRequired()

	def setTrue(self):
		self.setValue(True)

	def setValue(self, value):
		self.delValue()
		valXMLName = '{%s}value' % self.namespace

		if self['type'] == 'boolean':
			if value in self.true_values:
				valXML = ET.Element(valXMLName)
				valXML.text = '1'
				self.xml.append(valXML)
			else:
				valXML = ET.Element(valXMLName)
				valXML.text = '0'
				self.xml.append(valXML)
		elif self['type'] in self.multi_value_types or self['type'] in ['', None]:
			if self['type'] in self.multi_line_types and isinstance(value, str):
				value = value.split('\n')
			if not isinstance(value, list):
				value = [value]
			for val in value:
				if self['type'] in ['', None] and val in self.true_values:
					val = '1'
				valXML = ET.Element(valXMLName)
				valXML.text = val
				self.xml.append(valXML)
		else:
			if isinstance(value, list):
				raise ValueError("Cannot add multiple values to a %s field." % self['type'])
			valXML = ET.Element(valXMLName)
			valXML.text = value
			self.xml.append(valXML)


class FieldOption(ElementBase):
	namespace = 'jabber:x:data'
	name = 'option'
	plugin_attrib = 'option'
	interfaces = set(('label', 'value'))
	sub_interfaces = set(('value',))


class xep_0004(base.base_plugin):
	"""
	XEP-0004: Data Forms
	"""

	def plugin_init(self):
		self.xep = '0004'
		self.description = 'Data Forms'

		self.xmpp.registerHandler(
			Callback('Data Form',
				 MatchXPath('{%s}message/{%s}x' % (self.xmpp.default_ns, 
								   Form.namespace)),
				 self.handle_form))

		registerStanzaPlugin(FormField, FieldOption)
		registerStanzaPlugin(Form, FormField)
		registerStanzaPlugin(Message, Form)
	
	def makeForm(self, ftype='form', title='', instructions=''):
		f = Form()
		f['type'] = ftype
		f['title'] = title
		f['instructions'] = instructions
		return f
	
	def post_init(self):
		base.base_plugin.post_init(self)
		self.xmpp.plugin['xep_0030'].add_feature('jabber:x:data')

	def handle_form(self, message):
		self.xmpp.event("message_xform", message)