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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
import base64
import datetime as dt
from sleekxmpp.thirdparty.suelta.util import bytes
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin, JID
from sleekxmpp.plugins import xep_0082
class VCardTemp(ElementBase):
name = 'vCard'
namespace = 'vcard-temp'
plugin_attrib = 'vcard_temp'
interfaces = set(['FN', 'VERSION'])
sub_interfaces = set(['FN', 'VERSION'])
class Name(ElementBase):
name = 'N'
namespace = 'vcard-temp'
plugin_attrib = name
interfaces = set(['FAMILY', 'GIVEN', 'MIDDLE', 'PREFIX', 'SUFFIX'])
sub_interfaces = interfaces
def _set_component(self, name, value):
if isinstance(value, list):
value = ','.join(value)
if value is not None:
self._set_sub_text(name, value, keep=True)
else:
self._del_sub(name)
def _get_component(self, name):
value = self._get_sub_text(name, '')
if ',' in value:
value = [v.strip() for v in value.split(',')]
return value
def set_family(self, value):
self._set_component('FAMILY', value)
def get_family(self):
return self._get_component('FAMILY')
def set_given(self, value):
self._set_component('GIVEN', value)
def get_given(self):
return self._get_component('GIVEN')
def set_middle(self, value):
print(value)
self._set_component('MIDDLE', value)
def get_middle(self):
return self._get_component('MIDDLE')
def set_prefix(self, value):
self._set_component('PREFIX', value)
def get_prefix(self):
return self._get_component('PREFIX')
def set_suffix(self, value):
self._set_component('SUFFIX', value)
def get_suffix(self):
return self._get_component('SUFFIX')
class Nickname(ElementBase):
name = 'NICKNAME'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'nicknames'
interfaces = set([name])
is_extension = True
def set_nickname(self, value):
if not value:
self.xml.text = ''
return
if not isinstance(value, list):
value = [value]
self.xml.text = ','.join(value)
def get_nickname(self):
if self.xml.text:
return self.xml.text.split(',')
class Email(ElementBase):
name = 'EMAIL'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'emails'
interfaces = set(['HOME', 'WORK', 'INTERNET', 'PREF', 'X400', 'USERID'])
sub_interfaces = set(['USERID'])
bool_interfaces = set(['HOME', 'WORK', 'INTERNET', 'PREF', 'X400'])
class Address(ElementBase):
name = 'ADR'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'addresses'
interfaces = set(['HOME', 'WORK', 'POSTAL', 'PARCEL', 'DOM', 'INTL',
'PREF', 'POBOX', 'EXTADD', 'STREET', 'LOCALITY',
'REGION', 'PCODE', 'CTRY'])
sub_interfaces = set(['POBOX', 'EXTADD', 'STREET', 'LOCALITY',
'REGION', 'PCODE', 'CTRY'])
bool_interfaces = set(['HOME', 'WORK', 'DOM', 'INTL', 'PREF'])
class Telephone(ElementBase):
name = 'TEL'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'telephone_numbers'
interfaces = set(['HOME', 'WORK', 'VOICE', 'FAX', 'PAGER', 'MSG',
'CELL', 'VIDEO', 'BBS', 'MODEM', 'ISDN', 'PCS',
'PREF', 'NUMBER'])
sub_interfaces = set(['NUMBER'])
bool_interfaces = set(['HOME', 'WORK', 'VOICE', 'FAX', 'PAGER',
'MSG', 'CELL', 'VIDEO', 'BBS', 'MODEM',
'ISDN', 'PCS', 'PREF'])
def setup(self, xml=None):
super(Telephone, self).setup(xml=xml)
self._set_sub_text('NUMBER', '', keep=True)
def set_number(self, value):
self._set_sub_text('NUMBER', value, keep=True)
def del_number(self):
self._set_sub_text('NUMBER', '', keep=True)
class Label(ElementBase):
name = 'LABEL'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'labels'
interfaces = set(['HOME', 'WORK', 'POSTAL', 'PARCEL', 'DOM', 'INT',
'PREF', 'lines'])
bool_interfaces = set(['HOME', 'WORK', 'POSTAL', 'PARCEL', 'DOM',
'INT', 'PREF'])
def add_line(self, value):
line = ET.Element('{%s}LINE' % self.namespace)
line.text = value
self.xml.append(line)
def get_lines(self):
lines = self.xml.find('{%s}LINE' % self.namespace)
if lines is None:
return []
return [line.text for line in lines]
def set_lines(self, values):
self.del_lines()
for line in values:
self.add_line(line)
def del_lines(self):
lines = self.xml.find('{%s}LINE' % self.namespace)
if lines is None:
return
for line in lines:
self.xml.remove(line)
class Geo(ElementBase):
name = 'GEO'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'geolocations'
interfaces = set(['LAT', 'LON'])
sub_interfaces = interfaces
class Org(ElementBase):
name = 'ORG'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'organizations'
interfaces = set(['ORGNAME', 'ORGUNIT', 'orgunits'])
sub_interfaces = set(['ORGNAME', 'ORGUNIT'])
def add_orgunit(self, value):
orgunit = ET.Element('{%s}ORGUNIT' % self.namespace)
orgunit.text = value
self.xml.append(orgunit)
def get_orgunits(self):
orgunits = self.xml.find('{%s}ORGUNIT' % self.namespace)
if orgunits is None:
return []
return [orgunit.text for orgunit in orgunits]
def set_orgunits(self, values):
self.del_orgunits()
for orgunit in values:
self.add_orgunit(orgunit)
def del_orgunits(self):
orgunits = self.xml.find('{%s}ORGUNIT' % self.namespace)
if orgunits is None:
return
for orgunit in orgunits:
self.xml.remove(orgunit)
class Photo(ElementBase):
name = 'PHOTO'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'photos'
interfaces = set(['TYPE', 'EXTVAL'])
sub_interfaces = interfaces
class Logo(ElementBase):
name = 'LOGO'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'logos'
interfaces = set(['TYPE', 'EXTVAL'])
sub_interfaces = interfaces
class Sound(ElementBase):
name = 'SOUND'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'sounds'
interfaces = set(['PHONETC', 'EXTVAL'])
sub_interfaces = interfaces
class BinVal(ElementBase):
name = 'BINVAL'
namespace = 'vcard-temp'
plugin_attrib = name
interfaces = set(['BINVAL'])
is_extension = True
def setup(self, xml=None):
self.xml = ET.Element('')
return True
def set_binval(self, value):
self.del_binval()
parent = self.parent()
if value:
xml = ET.Element('{%s}BINVAL' % self.namespace)
xml.text = bytes(base64.b64encode(value)).decode('utf-8')
parent.append(xml)
def get_binval(self):
parent = self.parent()
xml = parent.find('{%s}BINVAL' % self.namespace)
if xml is not None:
return base64.b64decode(bytes(xml.text))
return b''
def del_binval(self):
self.parent()._del_sub('{%s}BINVAL' % self.namespace)
class Classification(ElementBase):
name = 'CLASS'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'classifications'
interfaces = set(['PUBLIC', 'PRIVATE', 'CONFIDENTIAL'])
bool_interfaces = interfaces
class Categories(ElementBase):
name = 'CATEGORIES'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'categories'
interfaces = set([name])
is_extension = True
def set_categories(self, values):
self.del_categories()
for keyword in values:
item = ET.Element('{%s}KEYWORD' % self.namespace)
item.text = keyword
self.xml.append(item)
def get_categories(self):
items = self.xml.findall('{%s}KEYWORD' % self.namespace)
if items is None:
return []
keywords = []
for item in items:
keywords.append(item.text)
return keywords
def del_categories(self):
items = self.xml.findall('{%s}KEYWORD' % self.namespace)
for item in items:
self.xml.remove(item)
class Birthday(ElementBase):
name = 'BDAY'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'birthdays'
interfaces = set([name])
is_extension = True
def set_bday(self, value):
if isinstance(value, dt.datetime):
value = xep_0082.format_datetime(value)
self.xml.text = value
def get_bday(self):
if not self.xml.text:
return None
return xep_0082.parse(self.xml.text)
class Rev(ElementBase):
name = 'REV'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'revision_dates'
interfaces = set([name])
is_extension = True
def set_rev(self, value):
if isinstance(value, dt.datetime):
value = xep_0082.format_datetime(value)
self.xml.text = value
def get_rev(self):
if not self.xml.text:
return None
return xep_0082.parse(self.xml.text)
class Title(ElementBase):
name = 'TITLE'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'titles'
interfaces = set([name])
is_extension = True
def set_title(self, value):
self.xml.text = value
def get_title(self):
return self.xml.text
class Role(ElementBase):
name = 'ROLE'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'roles'
interfaces = set([name])
is_extension = True
def set_role(self, value):
self.xml.text = value
def get_role(self):
return self.xml.text
class Note(ElementBase):
name = 'NOTE'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'notes'
interfaces = set([name])
is_extension = True
def set_note(self, value):
self.xml.text = value
def get_note(self):
return self.xml.text
class Desc(ElementBase):
name = 'DESC'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'descriptions'
interfaces = set([name])
is_extension = True
def set_desc(self, value):
self.xml.text = value
def get_desc(self):
return self.xml.text
class URL(ElementBase):
name = 'URL'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'urls'
interfaces = set([name])
is_extension = True
def set_url(self, value):
self.xml.text = value
def get_url(self):
return self.xml.text
class UID(ElementBase):
name = 'UID'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'uids'
interfaces = set([name])
is_extension = True
def set_uid(self, value):
self.xml.text = value
def get_uid(self):
return self.xml.text
class ProdID(ElementBase):
name = 'PRODID'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'product_ids'
interfaces = set([name])
is_extension = True
def set_prodid(self, value):
self.xml.text = value
def get_prodid(self):
return self.xml.text
class Mailer(ElementBase):
name = 'MAILER'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'mailers'
interfaces = set([name])
is_extension = True
def set_mailer(self, value):
self.xml.text = value
def get_mailer(self):
return self.xml.text
class SortString(ElementBase):
name = 'SORT-STRING'
namespace = 'vcard-temp'
plugin_attrib = 'SORT_STRING'
plugin_multi_attrib = 'sort_strings'
interfaces = set([name])
is_extension = True
def set_sort_string(self, value):
self.xml.text = value
def get_sort_string(self):
return self.xml.text
class Agent(ElementBase):
name = 'AGENT'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'agents'
interfaces = set(['EXTVAL'])
sub_interfaces = interfaces
class JabberID(ElementBase):
name = 'JABBERID'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'jids'
interfaces = set([name])
is_extension = True
def set_jabberid(self, value):
self.xml.text = JID(value).bare
def get_jabberid(self):
return JID(self.xml.text)
class TimeZone(ElementBase):
name = 'TZ'
namespace = 'vcard-temp'
plugin_attrib = name
plugin_multi_attrib = 'timezones'
interfaces = set([name])
is_extension = True
def set_tz(self, value):
time = xep_0082.time(offset=value)
if time[-1] == 'Z':
self.xml.text = 'Z'
else:
self.xml.text = time[-6:]
def get_tz(self):
if not self.xml.text:
return xep_0082.tzutc()
time = xep_0082.parse('00:00:00%s' % self.xml.text)
return time.tzinfo
register_stanza_plugin(VCardTemp, Name)
register_stanza_plugin(VCardTemp, Address, iterable=True)
register_stanza_plugin(VCardTemp, Agent, iterable=True)
register_stanza_plugin(VCardTemp, Birthday, iterable=True)
register_stanza_plugin(VCardTemp, Categories, iterable=True)
register_stanza_plugin(VCardTemp, Desc, iterable=True)
register_stanza_plugin(VCardTemp, Email, iterable=True)
register_stanza_plugin(VCardTemp, Geo, iterable=True)
register_stanza_plugin(VCardTemp, JabberID, iterable=True)
register_stanza_plugin(VCardTemp, Label, iterable=True)
register_stanza_plugin(VCardTemp, Logo, iterable=True)
register_stanza_plugin(VCardTemp, Mailer, iterable=True)
register_stanza_plugin(VCardTemp, Note, iterable=True)
register_stanza_plugin(VCardTemp, Nickname, iterable=True)
register_stanza_plugin(VCardTemp, Photo, iterable=True)
register_stanza_plugin(VCardTemp, ProdID, iterable=True)
register_stanza_plugin(VCardTemp, Rev, iterable=True)
register_stanza_plugin(VCardTemp, Role, iterable=True)
register_stanza_plugin(VCardTemp, SortString, iterable=True)
register_stanza_plugin(VCardTemp, Sound, iterable=True)
register_stanza_plugin(VCardTemp, Telephone, iterable=True)
register_stanza_plugin(VCardTemp, Title, iterable=True)
register_stanza_plugin(VCardTemp, TimeZone, iterable=True)
register_stanza_plugin(VCardTemp, UID, iterable=True)
register_stanza_plugin(VCardTemp, URL, iterable=True)
register_stanza_plugin(Photo, BinVal)
register_stanza_plugin(Logo, BinVal)
register_stanza_plugin(Sound, BinVal)
register_stanza_plugin(Agent, VCardTemp)
|