Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0054.py @ 1330:22fce2e51c70 frontends_multi_profiles
plugin XEP-0085: fixes the calls to memory.deleteEntityDatum and self.updateCache (was self.updateEntityData, has been renamed to avoid confusion with the memory method)
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 22 Feb 2015 11:43:11 +0100 |
parents | bd69d341d969 |
children | 6dbeb2ef966c |
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 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for managing xep-0054 |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) |
788
d6652683c572
plugin XEP-0054: also work with python2-pillow
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
771
diff
changeset
|
6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
9 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
10 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
11 # (at your option) any later version. |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
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 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
771 | 21 from sat.core.i18n import _ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
22 from sat.core.constants import Const as C |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
23 from sat.core.log import getLogger |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
24 log = getLogger(__name__) |
939 | 25 from twisted.internet import threads, defer |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.jabber.xmlstream import IQ |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
28 from twisted.words.xish import domish |
765
787ee59dba9c
plugins radiocol, xep-0054: better handling of upload errors:
souliane <souliane@mailoo.org>
parents:
639
diff
changeset
|
29 from twisted.python.failure import Failure |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 import os.path |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from zope.interface import implements |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from wokkel import disco, iwokkel |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
36 from base64 import b64decode, b64encode |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 from hashlib import sha1 |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
38 from sat.core import exceptions |
551
dcec4ba8e72c
plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
39 from sat.memory.persistent import PersistentDict |
788
d6652683c572
plugin XEP-0054: also work with python2-pillow
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
771
diff
changeset
|
40 from PIL import Image |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
41 from cStringIO import StringIO |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
48 | 43 try: |
44 from twisted.words.protocols.xmlstream import XMPPHandler | |
45 except ImportError: | |
46 from wokkel.subprotocols import XMPPHandler | |
47 | |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
48 AVATAR_PATH = "avatars" |
1081
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
49 AVATAR_DIM = (64, 64) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 IQ_GET = '/iq[@type="get"]' |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 NS_VCARD = 'vcard-temp' |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
53 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
|
54 |
48 | 55 PRESENCE = '/presence' |
56 NS_VCARD_UPDATE = 'vcard-temp:x:update' | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
57 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]' |
48 | 58 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 PLUGIN_INFO = { |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
60 "name": "XEP 0054 Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
61 "import_name": "XEP-0054", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
62 "type": "XEP", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
63 "protocols": ["XEP-0054", "XEP-0153"], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
64 "dependencies": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
65 "main": "XEP_0054", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
66 "handler": "yes", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
67 "description": _("""Implementation of vcard-temp""") |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 } |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
70 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
71 class XEP_0054(object): |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
72 #TODO: - check that nickname is ok |
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
73 # - refactor the code/better use of Wokkel |
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
74 # - get missing values |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
77 log.info(_("Plugin XEP_0054 initialization")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 self.host = host |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
79 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
|
80 if not os.path.exists(self.avatar_path): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 os.makedirs(self.avatar_path) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
82 self.avatars_cache = {} |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
83 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self._getCard) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
84 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
85 host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
86 host.trigger.add("presence_available", self.presenceTrigger) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
87 host.memory.setSignalOnUpdate("avatar") |
64 | 88 |
72 | 89 def getHandler(self, profile): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
90 return XEP_0054_handler(self) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
91 |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
92 def presenceTrigger(self, presence_elt, client): |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
93 if client.jid.userhost() in self.avatars_cache[client.profile]: |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
94 x_elt = domish.Element((NS_VCARD_UPDATE, 'x')) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
95 x_elt.addElement('photo', content=self.avatars_cache[client.profile][client.jid.userhost()]) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
96 presence_elt.addChild(x_elt) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
97 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
98 return True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
99 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
100 def _fillCachedValues(self, profile): |
551
dcec4ba8e72c
plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
101 #FIXME: this is really suboptimal, need to be reworked |
dcec4ba8e72c
plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
102 # the current naive approach keeps a map between all jids of all profiles |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
103 # in persistent cache, then put avatar |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
104 # hashs in memory. Hashed should be shared between profiles |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
105 for jid_s, avatar_hash in self.avatars_cache[profile].iteritems(): |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
106 jid_ = jid.JID(jid_s) |
1315
be3a301540c0
core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents:
1293
diff
changeset
|
107 self.host.memory.updateEntityData(jid_, "avatar", avatar_hash, silent=True, profile_key=profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
108 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
109 @defer.inlineCallbacks |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
110 def profileConnecting(self, profile): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
111 self.avatars_cache[profile] = PersistentDict(NS_VCARD, profile) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
112 yield self.avatars_cache[profile].load() |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
113 self._fillCachedValues(profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
114 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
115 def profileDisconnected(self, profile): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
116 log.debug(u"Deleting profile cache for avatars") |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
117 del self.avatars_cache[profile] |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
118 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
119 def updateCache(self, jid_, name, value, profile): |
48 | 120 """update cache value |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
121 |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
122 save value in memory in case of change |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
123 @param jid_(jid.JID): jid of the owner of the vcard |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
124 @param name(str): name of the item which changed |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
125 @param value(unicode): new value of the item |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
126 @param profile(unicode): profile which received the update |
48 | 127 """ |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
128 assert not jid_.resource # VCard are retrieved with bare jid |
1315
be3a301540c0
core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents:
1293
diff
changeset
|
129 self.host.memory.updateEntityData(jid_, name, value, profile_key=profile) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
130 if name == "avatar": |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
131 self.avatars_cache[profile][jid_.userhost()] = value |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
132 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
133 def getCache(self, entity_jid, name, profile): |
48 | 134 """return cached value for jid |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
135 |
1075
7b4600ad73ad
misc: remove some pylints warnings
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
136 @param entity_jid: target contact |
48 | 137 @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
|
138 @param profile: %(doc_profile)s |
48 | 139 @return: wanted value or None""" |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
140 assert not entity_jid.resource # VCard are retrieved with bare jid |
48 | 141 try: |
1075
7b4600ad73ad
misc: remove some pylints warnings
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
142 data = self.host.memory.getEntityData(entity_jid, [name], profile) |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
143 except exceptions.UnknownEntityError: |
48 | 144 return None |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
145 return data.get(name) |
48 | 146 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
147 def _getFilename(self, hash_): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
148 """Get filename from hash |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
149 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
150 @param hash_: hash of the avatar |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
151 @return (str): absolute filename of the avatar |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
152 """ |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
153 return os.path.join(self.avatar_path, hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
154 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
155 def saveAvatarFile(self, data, hash_): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
156 """Save the avatar picture if it doesn't already exists |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
157 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
158 @param data(str): binary image of the avatar |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
159 @param hash_(str): hash of the binary data (will be used for the filename) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
160 """ |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
161 filename = self._getFilename(hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
162 if not os.path.exists(filename): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
163 with open(filename, 'wb') as file_: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
164 file_.write(data) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
165 log.debug(_("file saved to %s") % hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
166 else: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
167 log.debug(_("file [%s] already in cache") % hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
168 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
169 def savePhoto(self, photo_xml): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 """Parse a <PHOTO> elem and save the picture""" |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 for elem in photo_xml.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 if elem.name == 'TYPE': |
1246
8b891f9be183
core, plugins: improve a bit some log messages
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
173 log.debug(_('Photo of type [%s] found') % str(elem)) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 if elem.name == 'BINVAL': |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
175 log.debug(_('Decoding binary')) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 decoded = b64decode(str(elem)) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
177 image_hash = sha1(decoded).hexdigest() |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
178 self.saveAvatarFile(decoded, image_hash) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
179 return image_hash |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 |
939 | 181 @defer.inlineCallbacks |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
182 def vCard2Dict(self, vcard, target, profile): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 """Convert a VCard to a dict, and save binaries""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
184 log.debug(_("parsing vcard")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 dictionary = {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
186 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 for elem in vcard.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 if elem.name == 'FN': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 dictionary['fullname'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 elif elem.name == 'NICKNAME': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 dictionary['nick'] = unicode(elem) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
192 self.updateCache(target, 'nick', dictionary['nick'], profile) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 elif elem.name == 'URL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 dictionary['website'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 elif elem.name == 'EMAIL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 dictionary['email'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 elif elem.name == 'BDAY': |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
198 dictionary['birthday'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 elif elem.name == 'PHOTO': |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
200 dictionary["avatar"] = yield threads.deferToThread(self.savePhoto, elem) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
201 if not dictionary["avatar"]: # can happen in case of e.g. empty photo elem |
48 | 202 del dictionary['avatar'] |
203 else: | |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
204 self.updateCache(target, 'avatar', dictionary['avatar'], profile) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
206 log.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
|
207 |
939 | 208 defer.returnValue(dictionary) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
210 def _VCardCb(self, answer, profile): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 """Called after the first get IQ""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
212 log.debug(_("VCard found")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 if answer.firstChildElement().name == "vCard": |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
215 _jid, steam = self.host.getJidNStream(profile) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
216 try: |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
217 from_jid = jid.JID(answer["from"]) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
218 except KeyError: |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
219 from_jid = _jid.userhostJID() |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
220 d = self.vCard2Dict(answer.firstChildElement(), from_jid, profile) |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
504
diff
changeset
|
221 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, profile)) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
223 log.error(_("FIXME: vCard not found as first child element")) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
224 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) # FIXME: maybe an error message would be better |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
226 def _VCardEb(self, failure, profile): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 """Called when something is wrong with registration""" |
1248
77a4592816f6
plugin XEP-0054: fixes error message when we get a ConnectionLost failure
souliane <souliane@mailoo.org>
parents:
1246
diff
changeset
|
228 try: |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
229 log.warning(_("Can't find VCard of %s") % failure.value.stanza['from']) |
1255
38fb8823cee8
plugin XEP-0054: the fix by revision 1248 (77a4592816f6) was not complete
souliane <souliane@mailoo.org>
parents:
1248
diff
changeset
|
230 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) # FIXME: maybe an error message would be better |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
231 self.updateCache(jid.JID(failure.value.stanza['from']), "avatar", '', profile) |
1255
38fb8823cee8
plugin XEP-0054: the fix by revision 1248 (77a4592816f6) was not complete
souliane <souliane@mailoo.org>
parents:
1248
diff
changeset
|
232 except AttributeError: # 'ConnectionLost' object has no attribute 'stanza' |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
233 log.warning(_("Can't find VCard: %s") % failure.getErrorMessage()) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
234 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
235 def _getCard(self, target_s, profile_key=C.PROF_KEY_NONE): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
236 return self.getCard(jid.JID(target_s), profile_key) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
237 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
238 def getCard(self, target, profile_key=C.PROF_KEY_NONE): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 """Ask server for VCard |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
240 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
241 @param target(jid.JID): jid from which we want the VCard |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
242 @result: id to retrieve the profile |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
243 """ |
64 | 244 current_jid, xmlstream = self.host.getJidNStream(profile_key) |
245 if not xmlstream: | |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
246 raise exceptions.ProfileUnknownError('Asking vcard for a non-existant or not connected profile ({})'.format(profile_key)) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
365
diff
changeset
|
247 profile = self.host.memory.getProfileName(profile_key) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
248 to_jid = target.userhostJID() |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
249 log.debug(_("Asking for %s's VCard") % to_jid.userhost()) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
250 reg_request = IQ(xmlstream, 'get') |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
251 reg_request["from"] = current_jid.full() |
48 | 252 reg_request["to"] = to_jid.userhost() |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
485
diff
changeset
|
253 reg_request.addElement('vCard', NS_VCARD) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
254 reg_request.send(to_jid.userhost()).addCallbacks(self._VCardCb, self._VCardEb, callbackArgs=[profile], errbackArgs=[profile]) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
255 return reg_request["id"] |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
257 def getAvatarFile(self, avatar_hash): |
43 | 258 """Give the full path of avatar from hash |
259 @param hash: SHA1 hash | |
260 @return full_path | |
261 """ | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
262 filename = self.avatar_path + '/' + avatar_hash |
43 | 263 if not os.path.exists(filename): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
264 log.error(_("Asking for an uncached avatar [%s]") % avatar_hash) |
43 | 265 return "" |
266 return filename | |
267 | |
570
bf1505df088c
plugin XEP-0054: fixed bad image resizing
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
268 def _buildSetAvatar(self, vcard_set, filepath): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
269 try: |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
270 img = Image.open(filepath) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
271 except IOError: |
765
787ee59dba9c
plugins radiocol, xep-0054: better handling of upload errors:
souliane <souliane@mailoo.org>
parents:
639
diff
changeset
|
272 return Failure(exceptions.DataError("Can't open image")) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
273 |
1081
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
274 if img.size != AVATAR_DIM: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
275 img.thumbnail(AVATAR_DIM, Image.ANTIALIAS) |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
276 if img.size[0] != img.size[1]: # we need to crop first |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
277 left, upper = (0, 0) |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
278 right, lower = img.size |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
279 offset = abs(right - lower) / 2 |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
280 if right == min(img.size): |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
281 upper += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
282 lower -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
283 else: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
284 left += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
285 right -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
286 img = img.crop((left, upper, right, lower)) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
287 img_buf = StringIO() |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
288 img.save(img_buf, 'PNG') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
289 |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
290 vcard_elt = vcard_set.addElement('vCard', NS_VCARD) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
291 photo_elt = vcard_elt.addElement('PHOTO') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
292 photo_elt.addElement('TYPE', content='image/png') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
293 photo_elt.addElement('BINVAL', content=b64encode(img_buf.getvalue())) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
294 img_hash = sha1(img_buf.getvalue()).hexdigest() |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
295 self.saveAvatarFile(img_buf.getvalue(), img_hash) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
296 return (vcard_set, img_hash) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
297 |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
298 def setAvatar(self, filepath, profile_key=C.PROF_KEY_NONE): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
299 """Set avatar of the profile |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
300 @param filepath: path of the image of the avatar""" |
1199 | 301 #TODO: This is a temporary way of setting the avatar, as other VCard information is not managed. |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
302 # A proper full VCard management should be done (and more generaly a public/private profile) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
303 client = self.host.getClient(profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
304 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
305 vcard_set = IQ(client.xmlstream, 'set') |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
306 d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
307 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
308 def elementBuilt(result): |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
309 """Called once the image is at the right size/format, and the vcard set element is build""" |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
310 set_avatar_elt, img_hash = result |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
311 self.updateCache(client.jid.userhostJID(), 'avatar', img_hash, client.profile) |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
312 return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available()) # FIXME: should send the current presence, not always "available" ! |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
313 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
314 d.addCallback(elementBuilt) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
315 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
316 return d |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
317 |
64 | 318 |
319 class XEP_0054_handler(XMPPHandler): | |
320 implements(iwokkel.IDisco) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
321 |
64 | 322 def __init__(self, plugin_parent): |
323 self.plugin_parent = plugin_parent | |
324 self.host = plugin_parent.host | |
325 | |
326 def connectionInitialized(self): | |
327 self.xmlstream.addObserver(VCARD_UPDATE, self.update) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
328 |
64 | 329 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
330 return [disco.DiscoFeature(NS_VCARD)] | |
331 | |
332 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
333 return [] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
334 |
48 | 335 def update(self, presence): |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
336 """Called on <presence/> stanza with vcard data |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
337 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
338 Check for avatar information, and get VCard if needed |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
339 @param presend(domish.Element): <presence/> stanza |
48 | 340 """ |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
341 # FIXME: doesn't manage MUC correctly |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
342 from_jid = jid.JID(presence['from']).userhostJID() |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
343 #FIXME: wokkel's data_form should be used here |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
344 x_elem = filter(lambda x: x.name == "x", presence.elements())[0] # We only want the "x" element |
48 | 345 for elem in x_elem.elements(): |
346 if elem.name == 'photo': | |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
347 hash_ = str(elem) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
348 old_avatar = self.plugin_parent.getCache(from_jid, 'avatar', self.parent.profile) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
349 filename = self.plugin_parent._getFilename(hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
350 if not old_avatar or old_avatar != hash_: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
351 if os.path.exists(filename): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
352 log.debug(u"New avatar found for [{}], it's already in cache, we use it".format(from_jid.full())) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
353 self.plugin_parent.updateCache(from_jid, 'avatar', hash_, self.parent.profile) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
354 else: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
355 log.debug(u'New avatar found for [{}], requesting vcard'.format(from_jid.full())) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
356 self.plugin_parent.getCard(from_jid, self.parent.profile) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
357 else: |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
358 if os.path.exists(filename): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
359 log.debug(u"avatar for {} already in cache".format(from_jid.full())) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
360 else: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
361 log.error(u"Avatar for [{}] should be in cache but it is not ! We get it".format(from_jid.full())) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
362 self.plugin_parent.getCard(from_jid, self.parent.profile) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
363 |