Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0054.py @ 501:e9634d2e7b38
core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:
- QuickContactManagement is not used anymore and will be removed, ContactList + Core are used instead
- disconnected contacts are now displayed in Primitivus (M-d to show/hide them)
- avatars are temporary unavailable in wix
- new bridge method: getContactsFromGroup
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 25 Sep 2012 00:58:34 +0200 |
parents | ee95ff721b68 |
children | 65ecbb473cbb |
rev | line source |
---|---|
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for managing xep-0054 |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, error |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.xish import domish |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import protocol, defer, threads, reactor |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
25 from twisted.internet.defer import inlineCallbacks, returnValue |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber import client, jid, xmlstream |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.jabber import error as jab_error |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.words.protocols.jabber.xmlstream import IQ |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 import os.path |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from zope.interface import implements |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from wokkel import disco, iwokkel |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from base64 import b64decode |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from hashlib import sha1 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 from time import sleep |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
38 from sat.memory.persistent import PersistentBinaryDict |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
48 | 40 try: |
41 from twisted.words.protocols.xmlstream import XMPPHandler | |
42 except ImportError: | |
43 from wokkel.subprotocols import XMPPHandler | |
44 | |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
45 AVATAR_PATH = "avatars" |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 IQ_GET = '/iq[@type="get"]' |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 NS_VCARD = 'vcard-temp' |
44
bfa7086d26d6
plugin XEP-0054: fixed forgotten copy/paste change
Goffi <goffi@goffi.org>
parents:
43
diff
changeset
|
49 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' #TODO: manage requests |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
48 | 51 PRESENCE = '/presence' |
52 NS_VCARD_UPDATE = 'vcard-temp:x:update' | |
53 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]' | |
54 | |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 PLUGIN_INFO = { |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 "name": "XEP 0054 Plugin", |
291 | 57 "import_name": "XEP-0054", |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 "type": "XEP", |
48 | 59 "protocols": ["XEP-0054", "XEP-0153"], |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 "dependencies": [], |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 "main": "XEP_0054", |
64 | 62 "handler": "yes", |
69 | 63 "description": _("""Implementation of vcard-temp""") |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 } |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
64 | 66 class XEP_0054(): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 def __init__(self, host): |
69 | 69 info(_("Plugin XEP_0054 initialization")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 self.host = host |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
71 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 if not os.path.exists(self.avatar_path): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 os.makedirs(self.avatar_path) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
74 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard) |
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
75 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile) |
444
d67cae85c2f9
plugin xep 0054: fixed bad bridge signature
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
76 host.bridge.addMethod("getCardCache", ".plugin", in_sign='ss', out_sign='a{ss}', method=self.getCardCache) |
64 | 77 |
72 | 78 def getHandler(self, profile): |
64 | 79 return XEP_0054_handler(self) |
48 | 80 |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
81 def update_cache(self, jid, name, value, profile): |
48 | 82 """update cache value |
83 - save value in memory in case of change | |
84 - send updatedValue signal if the value is new or updated | |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
85 @param jid: jid of the owner of the vcard |
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
86 @param name: name of the item which changed |
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
87 @param value: new value of the item |
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
88 @param profile: profile which received the update |
48 | 89 """ |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
90 client = self.host.getClient(profile) |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
91 if not jid.userhost() in client._vcard_cache: |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
92 client._vcard_cache[jid.userhost()] = {} |
48 | 93 |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
94 cache = client._vcard_cache[jid.userhost()] |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
95 old_value = cache[name] if name in cache else None |
48 | 96 if not old_value or value != old_value: |
97 cache[name] = value | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
98 client._vcard_cache.force(jid.userhost()) #we force saving of data to storage |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
99 self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}, profile) |
48 | 100 |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
101 def get_cache(self, jid, name, profile): |
48 | 102 """return cached value for jid |
103 @param jid: target contact | |
104 @param name: name of the value ('nick' or 'avatar') | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
105 @param profile: %(doc_profile)s |
48 | 106 @return: wanted value or None""" |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
107 client = self.host.getClient(profile) |
48 | 108 try: |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
109 return client._vcard_cache[jid.userhost()][name] |
48 | 110 except KeyError: |
111 return None | |
112 | |
113 | |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 def save_photo(self, photo_xml): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 """Parse a <PHOTO> elem and save the picture""" |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 for elem in photo_xml.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 if elem.name == 'TYPE': |
69 | 118 info(_('Photo of type [%s] found') % str(elem)) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 if elem.name == 'BINVAL': |
69 | 120 debug(_('Decoding binary')) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 decoded = b64decode(str(elem)) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 hash = sha1(decoded).hexdigest() |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 filename = self.avatar_path+'/'+hash |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 if not os.path.exists(filename): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 with open(filename,'wb') as file: |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 file.write(decoded) |
69 | 127 debug(_("file saved to %s") % hash) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 else: |
69 | 129 debug(_("file [%s] already in cache") % hash) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 return hash |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
132 @inlineCallbacks |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
133 def vCard2Dict(self, vcard, target, profile): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 """Convert a VCard to a dict, and save binaries""" |
69 | 135 debug (_("parsing vcard")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 dictionary = {} |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 d = defer.Deferred() |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 for elem in vcard.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 if elem.name == 'FN': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 dictionary['fullname'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 elif elem.name == 'NICKNAME': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 dictionary['nick'] = unicode(elem) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
144 self.update_cache(target, 'nick', dictionary['nick'], profile) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 elif elem.name == 'URL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 dictionary['website'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 elif elem.name == 'EMAIL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 dictionary['email'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 elif elem.name == 'BDAY': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 dictionary['birthday'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 elif elem.name == 'PHOTO': |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
152 dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem) |
48 | 153 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem |
154 del dictionary['avatar'] | |
155 else: | |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
156 self.update_cache(target, 'avatar', dictionary['avatar'], profile) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 else: |
69 | 158 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
160 returnValue(dictionary) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
162 def vcard_ok(self, answer, profile): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 """Called after the first get IQ""" |
69 | 164 debug (_("VCard found")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 if answer.firstChildElement().name == "vCard": |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
167 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data)) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 else: |
69 | 170 error (_("FIXME: vCard not found as first child element")) |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
171 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be better |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 def vcard_err(self, failure): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 """Called when something is wrong with registration""" |
69 | 175 error (_("Can't find VCard of %s") % failure.value.stanza['from']) |
485
ee95ff721b68
plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
176 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be better |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
64 | 178 def getCard(self, target, profile_key='@DEFAULT@'): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 """Ask server for VCard |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @param target: jid from which we want the VCard |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 @result: id to retrieve the profile""" |
64 | 182 current_jid, xmlstream = self.host.getJidNStream(profile_key) |
183 if not xmlstream: | |
69 | 184 error (_('Asking vcard for an non-existant or not connected profile')) |
64 | 185 return "" |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
186 profile = self.host.memory.getProfileName(profile_key) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 to_jid = jid.JID(target) |
69 | 188 debug(_("Asking for %s's VCard") % to_jid.userhost()) |
64 | 189 reg_request=IQ(xmlstream,'get') |
190 reg_request["from"]=current_jid.full() | |
48 | 191 reg_request["to"] = to_jid.userhost() |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 query=reg_request.addElement('vCard', NS_VCARD) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
193 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, [profile]) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 return reg_request["id"] |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 |
43 | 196 def getAvatarFile(self, hash): |
197 """Give the full path of avatar from hash | |
198 @param hash: SHA1 hash | |
199 @return full_path | |
200 """ | |
201 filename = self.avatar_path+'/'+hash | |
202 if not os.path.exists(filename): | |
69 | 203 error (_("Asking for an uncached avatar [%s]") % hash) |
43 | 204 return "" |
205 return filename | |
206 | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
207 def getCardCache(self, target, profile_key): |
48 | 208 """Request for cached values of profile |
209 return the cached nickname and avatar if exists, else get VCard | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
210 @param target: target's jid |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
211 @param profile_key: %(doc_profile_key)s |
48 | 212 """ |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
213 profile = self.host.memory.getProfileName(profile_key) |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
214 if not profile: |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
215 error(_("Profile not found")) |
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
216 return {} |
48 | 217 to_jid = jid.JID(target) |
218 result = {} | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
219 nick = self.get_cache(to_jid, 'nick', profile) |
48 | 220 if nick: |
221 result['nick'] = nick | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
222 avatar = self.get_cache(to_jid, 'avatar', profile) |
48 | 223 if avatar: |
224 result['avatar'] = avatar | |
225 return result | |
226 | |
64 | 227 |
228 | |
229 class XEP_0054_handler(XMPPHandler): | |
230 implements(iwokkel.IDisco) | |
231 | |
232 def __init__(self, plugin_parent): | |
233 self.plugin_parent = plugin_parent | |
234 self.host = plugin_parent.host | |
235 | |
236 def connectionInitialized(self): | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
237 self.parent._vcard_cache = PersistentBinaryDict(NS_VCARD, self.parent.profile) |
437
02c633a0ddcf
plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
Goffi <goffi@goffi.org>
parents:
435
diff
changeset
|
238 self.parent._vcard_cache.load() |
64 | 239 self.xmlstream.addObserver(VCARD_UPDATE, self.update) |
240 | |
241 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
242 return [disco.DiscoFeature(NS_VCARD)] | |
243 | |
244 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
245 return [] | |
246 | |
48 | 247 def update(self, presence): |
248 """Request for VCard's nickname | |
249 return the cached nickname if exists, else get VCard | |
250 """ | |
281 | 251 from_jid = jid.JID(presence['from']) |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
252 #FIXME: wokkel's data_form should be used here |
48 | 253 x_elem = filter (lambda x:x.name == "x", presence.elements())[0] #We only want the "x" element |
254 for elem in x_elem.elements(): | |
255 if elem.name == 'photo': | |
256 hash = str(elem) | |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
257 old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar', self.parent.profile) |
48 | 258 if not old_avatar or old_avatar != hash: |
69 | 259 debug(_('New avatar found, requesting vcard')) |
281 | 260 self.plugin_parent.getCard(from_jid.userhost(), self.parent.profile) |