Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0054.py @ 2134:f45250052846
primitivus: fixed display of error if a message can't be sent
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 05 Feb 2017 14:55:21 +0100 |
parents | c42aab22c2c0 |
children | 1d3f73e065e1 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
42
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 |
1766 | 5 # Copyright (C) 2009-2016 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 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
27 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
|
28 from twisted.python.failure import Failure |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from zope.interface import implements |
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 wokkel import disco, iwokkel |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
34 from base64 import b64decode, b64encode |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 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
|
36 from sat.core import exceptions |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
37 from sat.memory import persistent |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
38 import mimetypes |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
39 try: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
40 from PIL import Image |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
41 except: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
42 raise exceptions.MissingModule(u"Missing module pillow, please download/install it from https://python-pillow.github.io") |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
43 from cStringIO import StringIO |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
48 | 45 try: |
46 from twisted.words.protocols.xmlstream import XMPPHandler | |
47 except ImportError: | |
48 from wokkel.subprotocols import XMPPHandler | |
49 | |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
50 AVATAR_PATH = "avatars" |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
51 AVATAR_DIM = (64, 64) # FIXME: dim are not adapted to modern resolutions ! |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 IQ_GET = '/iq[@type="get"]' |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 NS_VCARD = 'vcard-temp' |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
55 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
|
56 |
48 | 57 PRESENCE = '/presence' |
58 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
|
59 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]' |
48 | 60 |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
61 CACHED_DATA = {'avatar', 'nick'} |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
62 MAX_AGE = 60 * 60 * 24 * 365 |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
63 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 PLUGIN_INFO = { |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
65 "name": "XEP 0054 Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
66 "import_name": "XEP-0054", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
67 "type": "XEP", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
68 "protocols": ["XEP-0054", "XEP-0153"], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
69 "dependencies": [], |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
70 "recommendations": ["XEP-0045"], |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
71 "main": "XEP_0054", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
72 "handler": "yes", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
73 "description": _("""Implementation of vcard-temp""") |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 } |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
76 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
77 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
|
78 #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
|
79 # - 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
|
80 # - get missing values |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 def __init__(self, host): |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
83 log.info(_(u"Plugin XEP_0054 initialization")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 self.host = host |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
85 host.bridge.addMethod(u"avatarGet", u".plugin", in_sign=u'sbbs', out_sign=u's', method=self._getAvatar, async=True) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
86 host.bridge.addMethod(u"avatarSet", u".plugin", in_sign=u'ss', out_sign=u'', method=self._setAvatar, async=True) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
87 host.trigger.add(u"presence_available", self.presenceAvailableTrigger) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
88 host.memory.setSignalOnUpdate(u"avatar") |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
89 host.memory.setSignalOnUpdate(u"nick") |
64 | 90 |
72 | 91 def getHandler(self, profile): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
92 return XEP_0054_handler(self) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
93 |
2060
d44360763262
plugin XEP-0054: renamed isInRoom to isRoom as the method only tell if the entity belong to a MUC
Goffi <goffi@goffi.org>
parents:
2046
diff
changeset
|
94 def isRoom(self, client, entity_jid): |
d44360763262
plugin XEP-0054: renamed isInRoom to isRoom as the method only tell if the entity belong to a MUC
Goffi <goffi@goffi.org>
parents:
2046
diff
changeset
|
95 """Tell if a jid is a MUC one |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
96 |
2060
d44360763262
plugin XEP-0054: renamed isInRoom to isRoom as the method only tell if the entity belong to a MUC
Goffi <goffi@goffi.org>
parents:
2046
diff
changeset
|
97 @param entity_jid(jid.JID): full or bare jid of the entity check |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
98 @return (bool): True if the bare jid of the entity is a room jid |
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
99 """ |
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
100 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
101 self.host.plugins['XEP-0045'].checkRoomJoined(client, entity_jid.userhostJID()) |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
102 except exceptions.NotFound: |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
103 return False |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
104 else: |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
105 return True |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
106 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
107 def getBareOrFull(self, client, jid_): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
108 """use full jid if jid_ is an occupant of a room, bare jid else |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
109 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
110 @param jid_(jid.JID): entity to test |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
111 @return (jid.JID): bare or full jid |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
112 """ |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
113 if jid_.resource: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
114 if not self.isRoom(client, jid_): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
115 return jid_.userhostJID() |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
116 return jid_ |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
117 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
118 def presenceAvailableTrigger(self, presence_elt, client): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
119 if client.jid.userhost() in client._cache_0054: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
120 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
121 avatar_hash = client._cache_0054[client.jid.userhost()]['avatar'] |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
122 except KeyError: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
123 log.info(u"No avatar in cache for {}".format(client.jid.userhost())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
124 return True |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
125 x_elt = domish.Element((NS_VCARD_UPDATE, 'x')) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
126 x_elt.addElement('photo', content=avatar_hash) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
127 presence_elt.addChild(x_elt) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
128 return True |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
129 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
130 @defer.inlineCallbacks |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
131 def profileConnecting(self, profile): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
132 client = self.host.getClient(profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
133 client._cache_0054 = persistent.PersistentBinaryDict(NS_VCARD, profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
134 yield client._cache_0054.load() |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
135 self._fillCachedValues(profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
136 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
137 def _fillCachedValues(self, profile): |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
138 #FIXME: this may need to be reworked |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
139 # the current naive approach keeps a map between all jids |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
140 # in persistent cache, then put avatar hashs in memory. |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
141 # Hashes should be shared between profiles (or not ? what |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
142 # if the avatar is different depending on who is requesting it |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
143 # this is not possible with vcard-tmp, but it is with XEP-0084). |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
144 # Loading avatar on demand per jid may be a option to investigate. |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
145 client = self.host.getClient(profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
146 for jid_s, data in client._cache_0054.iteritems(): |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
147 jid_ = jid.JID(jid_s) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
148 for name in CACHED_DATA: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
149 try: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
150 value = data[name] |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
151 if value is None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
152 log.error(u"{name} value for {jid_} is None, ignoring".format(name=name, jid_=jid_)) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
153 continue |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
154 self.host.memory.updateEntityData(jid_, name, data[name], silent=True, profile_key=profile) |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
155 except KeyError: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
156 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
157 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
158 def updateCache(self, client, jid_, name, value): |
48 | 159 """update cache value |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
160 |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
161 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
|
162 @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
|
163 @param name(str): name of the item which changed |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
164 @param value(unicode, None): new value of the item |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
165 None to delete |
48 | 166 """ |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
167 jid_ = self.getBareOrFull(client, jid_) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
168 jid_s = jid_.full() |
1710
7226280e70da
plugin XEP-0054: use full jid to manage the card/avatar if the bare jid correspond to a MUC room
Goffi <goffi@goffi.org>
parents:
1683
diff
changeset
|
169 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
170 if value is None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
171 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
172 self.host.memory.delEntityDatum(jid_, name, client.profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
173 except (KeyError, exceptions.UnknownEntityError): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
174 pass |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
175 if name in CACHED_DATA: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
176 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
177 del client._cache_0054[jid_s][name] |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
178 except KeyError: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
179 pass |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
180 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
181 client._cache_0054.force(jid_s) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
182 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
183 self.host.memory.updateEntityData(jid_, name, value, profile_key=client.profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
184 if name in CACHED_DATA: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
185 client._cache_0054.setdefault(jid_s, {})[name] = value |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
186 client._cache_0054.force(jid_s) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
187 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
188 def getCache(self, client, entity_jid, name): |
48 | 189 """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
|
190 |
1075
7b4600ad73ad
misc: remove some pylints warnings
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
191 @param entity_jid: target contact |
48 | 192 @param name: name of the value ('nick' or 'avatar') |
193 @return: wanted value or None""" | |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
194 entity_jid = self.getBareOrFull(client, entity_jid) |
48 | 195 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
196 data = self.host.memory.getEntityData(entity_jid, [name], client.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
|
197 except exceptions.UnknownEntityError: |
48 | 198 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
|
199 return data.get(name) |
48 | 200 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
201 def savePhoto(self, client, photo_elt, entity_jid): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
202 """Parse a <PHOTO> photo_elt and save the picture""" |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
203 # XXX: this method is launched in a separate thread |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
204 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
205 mime_type = unicode(photo_elt.elements(NS_VCARD, 'TYPE').next()) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
206 except StopIteration: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
207 log.warning(u"no MIME type found, assuming image/png") |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
208 mime_type = u"image/png" |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
209 else: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
210 if not mime_type: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
211 log.warning(u"empty MIME type, assuming image/png") |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
212 mime_type = u"image/png" |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
213 elif mime_type not in ("image/gif", "image/jpeg", "image/png"): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
214 if mime_type == "image/x-png": |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
215 # XXX: this old MIME type is still used by some clients |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
216 mime_type = "image/png" |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
217 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
218 # TODO: handle other image formats (svg?) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
219 log.warning(u"following avatar image format is not handled: {type} [{jid}]".format( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
220 type=mime_type, jid=entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
221 raise Failure(exceptions.DataError()) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
222 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
223 ext = mimetypes.guess_extension(mime_type, strict=False) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
224 assert ext is not None |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
225 log.debug(u'photo of type {type} with extension {ext} found [{jid}]'.format( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
226 type=mime_type, ext=ext, jid=entity_jid.full())) |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
227 try: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
228 buf = str(photo_elt.elements(NS_VCARD, 'BINVAL').next()) |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
229 except StopIteration: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
230 log.warning(u"BINVAL element not found") |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
231 raise Failure(exceptions.NotFound()) |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
232 if not buf: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
233 log.warning(u"empty avatar for {jid}".format(jid=entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
234 raise Failure(exceptions.NotFound()) |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
235 log.debug(_(u'Decoding binary')) |
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
236 decoded = b64decode(buf) |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
237 del buf |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
238 image_hash = sha1(decoded).hexdigest() |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
239 with client.cache.cacheData( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
240 PLUGIN_INFO['import_name'], |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
241 image_hash, |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
242 mime_type, |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
243 # we keep in cache for 1 year |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
244 MAX_AGE |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
245 ) as f: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
246 f.write(decoded) |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
247 return image_hash |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 |
939 | 249 @defer.inlineCallbacks |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
250 def vCard2Dict(self, client, vcard, entity_jid): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 """Convert a VCard to a dict, and save binaries""" |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
252 log.debug((u"parsing vcard")) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
253 vcard_dict = {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
254 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 for elem in vcard.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 if elem.name == 'FN': |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
257 vcard_dict['fullname'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 elif elem.name == 'NICKNAME': |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
259 vcard_dict['nick'] = unicode(elem) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
260 self.updateCache(client, entity_jid, 'nick', vcard_dict['nick']) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 elif elem.name == 'URL': |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
262 vcard_dict['website'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 elif elem.name == 'EMAIL': |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
264 vcard_dict['email'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 elif elem.name == 'BDAY': |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
266 vcard_dict['birthday'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 elif elem.name == 'PHOTO': |
2069
528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
Goffi <goffi@goffi.org>
parents:
2064
diff
changeset
|
268 # TODO: handle EXTVAL |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
269 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
270 avatar_hash = yield threads.deferToThread( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
271 self.savePhoto, client, elem, entity_jid) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
272 except (exceptions.DataError, exceptions.NotFound) as e: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
273 avatar_hash = '' |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
274 vcard_dict['avatar'] = avatar_hash |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
275 except Exception as e: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
276 log.error(u"avatar saving error: {}".format(e)) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
277 avatar_hash = None |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
278 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
279 vcard_dict['avatar'] = avatar_hash |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
280 self.updateCache(client, entity_jid, 'avatar', avatar_hash) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 else: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
282 log.debug(u'FIXME: [{}] VCard tag is not managed yet'.format(elem.name)) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
284 # if a data in cache doesn't exist anymore, we need to delete it |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
285 # so we check CACHED_DATA no gotten (i.e. not in vcard_dict keys) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
286 # and we reset them |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
287 for datum in CACHED_DATA.difference(vcard_dict.keys()): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
288 log.debug(u"reseting vcard datum [{datum}] for {entity}".format(datum=datum, entity=entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
289 self.updateCache(client, entity_jid, datum, None) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
290 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
291 defer.returnValue(vcard_dict) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
293 def _vCardCb(self, iq_elt, to_jid, client): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 """Called after the first get IQ""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
295 log.debug(_("VCard found")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 |
2046
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
297 try: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
298 vcard_elt = iq_elt.elements(NS_VCARD, "vCard").next() |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
299 except StopIteration: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
300 log.warning(u"Can't find vCard element in answer for jid {jid}", jid=to_jid.full()) |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
301 return |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
302 try: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
303 from_jid = jid.JID(iq_elt["from"]) |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
304 except KeyError: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
305 from_jid = client.jid.userhostJID() |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
306 d = self.vCard2Dict(client, vcard_elt, from_jid) |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
307 return d |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
309 def _vCardEb(self, failure_, to_jid, client): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 """Called when something is wrong with registration""" |
2046
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
311 log.warning(u"Can't get vCard for {jid}: {failure}".format(jid=to_jid.full, failure=failure_)) |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
312 self.updateCache(client, to_jid, "avatar", None) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
313 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
314 def getCard(self, client, entity_jid): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 """Ask server for VCard |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
316 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
317 @param entity_jid(jid.JID): jid from which we want the VCard |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
318 @result: id to retrieve the profile |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
319 """ |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
320 entity_jid = self.getBareOrFull(client, entity_jid) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
321 log.debug(u"Asking for {}'s VCard".format(entity_jid.full())) |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
322 reg_request = client.IQ('get') |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
323 reg_request["from"] = client.jid.full() |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
324 reg_request["to"] = entity_jid.full() |
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
|
325 reg_request.addElement('vCard', NS_VCARD) |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
326 d = reg_request.send(entity_jid.full()).addCallbacks(self._vCardCb, self._vCardEb, callbackArgs=[entity_jid, client], errbackArgs=[entity_jid, client]) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
327 return d |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
328 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
329 def _getCardCb(self, dummy, client, entity): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
330 try: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
331 return client._cache_0054[entity.full()]['avatar'] |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
332 except KeyError: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
333 raise Failure(exceptions.NotFound()) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
334 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
335 def _getAvatar(self, entity, cache_only, hash_only, profile): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
336 client = self.host.getClient(profile) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
337 d = self.getAvatar(client, jid.JID(entity)) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
338 d.addErrback(lambda dummy: '') |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
339 |
2072
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
340 return d |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
342 def getAvatar(self, client, entity, cache_only=True, hash_only=False): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
343 """get avatar full path or hash |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
344 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
345 if avatar is not in local cache, it will be requested to the server |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
346 @param entity(jid.JID): entity to get avatar from |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
347 @param cache_only(bool): if False, will request vCard if avatar is |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
348 not in cache |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
349 @param hash_only(bool): if True only return hash, not full path |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
350 @raise exceptions.NotFound: no avatar found |
43 | 351 """ |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
352 entity = self.getBareOrFull(client, entity) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
353 full_path = None |
43 | 354 |
2072
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
355 try: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
356 # we first check if we have avatar in cache |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
357 avatar_hash = client._cache_0054[entity.full()]['avatar'] |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
358 if avatar_hash: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
359 # avatar is known and exists |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
360 full_path = client.cache.getFilePath(avatar_hash) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
361 if full_path is None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
362 # cache file is not available (probably expired) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
363 raise KeyError |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
364 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
365 # avatar has already been checked but it is not set |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
366 full_path = u'' |
2072
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
367 except KeyError: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
368 # avatar is not in cache |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
369 if cache_only: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
370 return defer.fail(Failure(exceptions.NotFound())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
371 # we request vCard to get avatar |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
372 d = self.getCard(client, entity) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
373 d.addCallback(self._getCardCb, client, entity) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
374 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
375 # avatar is in cache, we can return hash |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
376 d = defer.succeed(avatar_hash) |
2072
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
377 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
378 if not hash_only: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
379 # full path is requested |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
380 if full_path is None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
381 d.addCallback(client.cache.getFilePath) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
382 else: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
383 d.addCallback(lambda dummy: full_path) |
2072
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
384 return d |
11fb5f5e2f89
plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents:
2069
diff
changeset
|
385 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
386 def _buildSetAvatar(self, client, vcard_set, file_path): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
387 # XXX: this method is executed in a separate thread |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
388 try: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
389 img = Image.open(file_path) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
390 except IOError: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
391 return Failure(exceptions.DataError(u"Can't open image")) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
392 |
1081
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
393 if img.size != AVATAR_DIM: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
394 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
|
395 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
|
396 left, upper = (0, 0) |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
397 right, lower = img.size |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
398 offset = abs(right - lower) / 2 |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
399 if right == min(img.size): |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
400 upper += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
401 lower -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
402 else: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
403 left += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
404 right -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
405 img = img.crop((left, upper, right, lower)) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
406 img_buf = StringIO() |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
407 img.save(img_buf, 'PNG') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
408 |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
409 vcard_elt = vcard_set.addElement('vCard', NS_VCARD) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
410 photo_elt = vcard_elt.addElement('PHOTO') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
411 photo_elt.addElement('TYPE', content='image/png') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
412 photo_elt.addElement('BINVAL', content=b64encode(img_buf.getvalue())) |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
413 image_hash = sha1(img_buf.getvalue()).hexdigest() |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
414 with client.cache.cacheData( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
415 PLUGIN_INFO['import_name'], |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
416 image_hash, |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
417 "image/png", |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
418 MAX_AGE |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
419 ) as f: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
420 f.write(img_buf.getvalue()) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
421 return vcard_set, image_hash |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
422 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
423 def _setAvatar(self, file_path, profile_key=C.PROF_KEY_NONE): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
424 client = self.host.getClient(profile_key) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
425 return self.setAvatar(client, file_path) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
426 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
427 def setAvatar(self, client, file_path): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
428 """Set avatar of the profile |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
429 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
430 @param file_path: path of the image of the avatar |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
431 """ |
1199 | 432 #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
|
433 # A proper full VCard management should be done (and more generaly a public/private profile) |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
434 vcard_set = client.IQ() |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
435 d = threads.deferToThread(self._buildSetAvatar, client, vcard_set, file_path) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
436 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
437 def elementBuilt(result): |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
438 """Called once the image is at the right size/format, and the vcard set element is build""" |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
439 set_avatar_elt, image_hash = result |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
440 self.updateCache(client, client.jid.userhostJID(), 'avatar', image_hash) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
441 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
|
442 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
443 d.addCallback(elementBuilt) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
444 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
445 return d |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
446 |
64 | 447 |
448 class XEP_0054_handler(XMPPHandler): | |
449 implements(iwokkel.IDisco) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
450 |
64 | 451 def __init__(self, plugin_parent): |
452 self.plugin_parent = plugin_parent | |
453 self.host = plugin_parent.host | |
454 | |
455 def connectionInitialized(self): | |
456 self.xmlstream.addObserver(VCARD_UPDATE, self.update) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
457 |
64 | 458 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
459 return [disco.DiscoFeature(NS_VCARD)] | |
460 | |
461 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
462 return [] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
463 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
464 def _checkAvatarHash(self, dummy, client, entity, given_hash): |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
465 """check that hash in cash (i.e. computed hash) is the same as given one""" |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
466 # XXX: if they differ, the avater will be requested on each connection |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
467 # TODO: try to avoid re-requesting avatar in this case |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
468 computed_hash = self.plugin_parent.getCache(client, entity, 'avatar') |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
469 if computed_hash != given_hash: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
470 log.warning(u"computed hash differs from given hash for {entity}:\n" |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
471 "computed: {computed}\ngiven: {given}".format( |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
472 entity=entity, computed=computed_hash, given=given_hash)) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
473 |
48 | 474 def update(self, presence): |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
475 """Called on <presence/> stanza with vcard data |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
476 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
477 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
|
478 @param presend(domish.Element): <presence/> stanza |
48 | 479 """ |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
480 client = self.parent |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
481 entity_jid = self.plugin_parent.getBareOrFull(client, jid.JID(presence['from'])) |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
482 #FIXME: wokkel's data_form should be used here |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
483 try: |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
484 x_elt = presence.elements(NS_VCARD_UPDATE, 'x').next() |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
485 except StopIteration: |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
486 return |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
487 |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
488 try: |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
489 photo_elt = x_elt.elements(NS_VCARD_UPDATE, 'photo').next() |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
490 except StopIteration: |
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
491 return |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
492 |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
493 hash_ = unicode(photo_elt).strip() |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
494 if hash_ == C.HASH_SHA1_EMPTY: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
495 hash_ = u'' |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
496 old_avatar = self.plugin_parent.getCache(client, entity_jid, 'avatar') |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
497 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
498 if old_avatar == hash_: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
499 # no change, we can return... |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
500 if hash_: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
501 # ...but we double check that avatar is in cache |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
502 file_path = client.cache.getFilePath(hash_) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
503 if file_path is None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
504 log.error(u"Avatar for [{}] should be in cache but it is not! We get it".format(entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
505 self.plugin_parent.getCard(client, entity_jid) |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
506 else: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
507 log.debug(u"avatar for {} already in cache".format(entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
508 return |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
509 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
510 if not hash_: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
511 # the avatar has been removed |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
512 # XXX: we use empty string instead of None to indicate that we took avatar |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
513 # but it is empty on purpose |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
514 self.plugin_parent.updateCache(client, entity_jid, 'avatar', '') |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
515 return |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
516 |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
517 file_path = client.cache.getFilePath(hash_) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
518 if file_path is not None: |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
519 log.debug(u"New avatar found for [{}], it's already in cache, we use it".format(entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
520 self.plugin_parent.updateCache(client, entity_jid, 'avatar', hash_) |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
521 else: |
2123
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
522 log.debug(u'New avatar found for [{}], requesting vcard'.format(entity_jid.full())) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
523 d = self.plugin_parent.getCard(client, entity_jid) |
c42aab22c2c0
plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents:
2072
diff
changeset
|
524 d.addCallback(self._checkAvatarHash, client, entity_jid, hash_) |