Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0054.py @ 2069:528e5fafc11b
plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 10 Sep 2016 18:03:24 +0200 |
parents | ee9621d92cb9 |
children | 11fb5f5e2f89 |
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 |
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 |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
39 from sat.memory import persistent |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
40 try: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
41 from PIL import Image |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
42 except: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1437
diff
changeset
|
43 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
|
44 from cStringIO import StringIO |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
48 | 46 try: |
47 from twisted.words.protocols.xmlstream import XMPPHandler | |
48 except ImportError: | |
49 from wokkel.subprotocols import XMPPHandler | |
50 | |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
51 AVATAR_PATH = "avatars" |
1081
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
52 AVATAR_DIM = (64, 64) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 IQ_GET = '/iq[@type="get"]' |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 NS_VCARD = 'vcard-temp' |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
56 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
|
57 |
48 | 58 PRESENCE = '/presence' |
59 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
|
60 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]' |
48 | 61 |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
62 CACHED_DATA = {'avatar', 'nick'} |
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): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
83 log.info(_("Plugin XEP_0054 initialization")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 self.host = host |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
85 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
|
86 if not os.path.exists(self.avatar_path): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 os.makedirs(self.avatar_path) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
88 self.cache = {} |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
89 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
|
90 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
|
91 host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True) |
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
|
92 host.trigger.add("presence_available", self.presenceAvailableTrigger) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
93 host.memory.setSignalOnUpdate("avatar") |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
94 host.memory.setSignalOnUpdate("nick") |
64 | 95 |
72 | 96 def getHandler(self, profile): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
97 return XEP_0054_handler(self) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
98 |
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
|
99 def presenceAvailableTrigger(self, presence_elt, client): |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
100 if client.jid.userhost() in self.cache[client.profile]: |
1683
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
101 try: |
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
102 avatar_hash = self.cache[client.profile][client.jid.userhost()]['avatar'] |
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
103 except KeyError: |
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
104 log.info(u"No avatar in cache for {}".format(client.jid.userhost())) |
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
105 return True |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
106 x_elt = domish.Element((NS_VCARD_UPDATE, 'x')) |
1683
9bf1262297f2
plugin XEP-0054: fixed presence issue when avatar is not in cache
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
107 x_elt.addElement('photo', content=avatar_hash) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
108 presence_elt.addChild(x_elt) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
109 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
110 return True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
111 |
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
|
112 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
|
113 """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
|
114 |
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
|
115 @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
|
116 @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
|
117 """ |
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
|
118 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 else: |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
123 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
|
124 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
125 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
|
126 #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
|
127 # the current naive approach keeps a map between all jids of all profiles |
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
|
128 # in persistent cache, then put avatar hashs in memory. |
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
|
129 # Hashes should be shared between profiles |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
130 for jid_s, data in self.cache[profile].iteritems(): |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
131 jid_ = jid.JID(jid_s) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
132 for name in CACHED_DATA: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
133 try: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
134 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
|
135 except KeyError: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
136 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
137 |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
138 @defer.inlineCallbacks |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
139 def profileConnecting(self, profile): |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
140 self.cache[profile] = persistent.PersistentBinaryDict(NS_VCARD, profile) |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
141 yield self.cache[profile].load() |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
142 self._fillCachedValues(profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
143 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
144 def profileDisconnected(self, profile): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
145 log.debug(u"Deleting profile cache for avatars") |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
146 del self.cache[profile] |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
147 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
148 def updateCache(self, client, jid_, name, value): |
48 | 149 """update cache value |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
150 |
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
151 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
|
152 @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
|
153 @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
|
154 @param value(unicode): new value of the item |
48 | 155 """ |
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
|
156 if jid_.resource: |
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
|
157 if not self.isRoom(client, jid_): |
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
|
158 # VCard are retrieved with bare 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
|
159 # but MUC room is a special case |
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
|
160 jid_ = jid.userhostJID() |
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
|
161 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
162 self.host.memory.updateEntityData(jid_, name, value, profile_key=client.profile) |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
163 if name in CACHED_DATA: |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
164 jid_s = jid_.userhost() |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
165 self.cache[client.profile].setdefault(jid_s, {})[name] = value |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
166 self.cache[client.profile].force(jid_s) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
167 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
168 def getCache(self, client, entity_jid, name): |
48 | 169 """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
|
170 |
1075
7b4600ad73ad
misc: remove some pylints warnings
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
171 @param entity_jid: target contact |
48 | 172 @param name: name of the value ('nick' or 'avatar') |
173 @return: wanted value or None""" | |
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
|
174 if entity_jid.resource: |
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
|
175 if not self.isRoom(client, entity_jid): |
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
|
176 # VCard are retrieved with bare 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
|
177 # but MUC room is a special case |
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
|
178 entity_jid = jid.userhostJID() |
48 | 179 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
180 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
|
181 except exceptions.UnknownEntityError: |
48 | 182 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
|
183 return data.get(name) |
48 | 184 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
185 def _getFilename(self, hash_): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
186 """Get filename from hash |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
187 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
188 @param hash_: hash of the avatar |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
189 @return (str): absolute filename of the avatar |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
190 """ |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
191 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
|
192 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
193 def saveAvatarFile(self, data, hash_): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
194 """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
|
195 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
196 @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
|
197 @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
|
198 """ |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
199 filename = self._getFilename(hash_) |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
200 if not os.path.exists(filename): |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
201 with open(filename, 'wb') as file_: |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
202 file_.write(data) |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
203 log.debug(_(u"file saved to %s") % hash_) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
204 else: |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
205 log.debug(_(u"file [%s] already in cache") % hash_) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
206 |
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
|
207 def savePhoto(self, photo_elt, target): |
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
|
208 """Parse a <PHOTO> photo_elt and save the picture""" |
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
|
209 try: |
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
|
210 type_ = unicode(photo_elt.elements(NS_VCARD, 'TYPE').next()) |
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
|
211 except StopIteration: |
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
|
212 type_ = None |
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
|
213 else: |
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
|
214 if type_ and type_ not in ("image/gif", "image/jpeg", "image/png"): |
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
|
215 # TODO: handle other image formats (svg?) |
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
|
216 log.warning(u"following avatar image format is not handled: {type} [{jid}]".format( |
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
|
217 type=type_, jid=target.full())) |
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
|
218 return |
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
|
219 log.debug(u'Photo of type {type} found [{jid}]'.format( |
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
|
220 type=type_, jid=target.full())) |
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
|
221 try: |
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
|
222 bin_elt = photo_elt.elements(NS_VCARD, 'BINVAL').next() |
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
|
223 except StopIteration: |
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
|
224 return |
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
|
225 buf = str(bin_elt) |
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
|
226 if not buf: |
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 log.warning(u"empty avatar for {jid}".format(jid=target.full())) |
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
|
228 return |
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 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
|
230 decoded = b64decode(buf) |
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
|
231 image_hash = sha1(decoded).hexdigest() |
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 self.saveAvatarFile(decoded, image_hash) |
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
|
233 return image_hash |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 |
939 | 235 @defer.inlineCallbacks |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
236 def vCard2Dict(self, client, vcard, target): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 """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
|
238 log.debug(_("parsing vcard")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 dictionary = {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
240 |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 for elem in vcard.elements(): |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 if elem.name == 'FN': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 dictionary['fullname'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 elif elem.name == 'NICKNAME': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 dictionary['nick'] = unicode(elem) |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
246 self.updateCache(client, target, 'nick', dictionary['nick']) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 elif elem.name == 'URL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 dictionary['website'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 elif elem.name == 'EMAIL': |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 dictionary['email'] = unicode(elem) |
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 elif elem.name == 'BDAY': |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
252 dictionary['birthday'] = unicode(elem) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 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
|
254 # TODO: handle EXTVAL |
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
|
255 avatar_hash = yield threads.deferToThread(self.savePhoto, elem, target) |
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
|
256 if avatar_hash is None: # can happen e.g. in case of empty photo elem |
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
|
257 continue |
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
|
258 dictionary['avatar'] = avatar_hash |
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
|
259 self.updateCache(client, target, 'avatar', dictionary['avatar']) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 else: |
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
|
261 log.debug(_('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
|
262 |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
263 # if a data in cache doesn't exist anymore, we need to reset it |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
264 # so we check CACHED_DATA no gotten (i.e. not in dictionary keys) |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
265 # and we reset them |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
266 for datum in CACHED_DATA.difference(dictionary.keys()): |
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
267 log.debug(u"reseting vcard datum [{datum}] for {entity}".format(datum=datum, entity=target.full())) |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
268 self.updateCache(client, target, datum, '') |
1341
6dbeb2ef966c
plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents:
1317
diff
changeset
|
269 |
939 | 270 defer.returnValue(dictionary) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 |
2046
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
272 def _getCardCb(self, iq_elt, to_jid, client): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 """Called after the first get IQ""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
274 log.debug(_("VCard found")) |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 |
2046
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
276 try: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
277 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
|
278 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
|
279 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
|
280 return |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
281 try: |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
282 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
|
283 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
|
284 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
|
285 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
|
286 return d |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 |
2046
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
288 def _getCardEb(self, failure_, to_jid, client): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 """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
|
290 log.warning(u"Can't get vCard for {jid}: {failure}".format(jid=to_jid.full, failure=failure_)) |
b99bd02ea643
plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents:
1978
diff
changeset
|
291 self.updateCache(client, to_jid, "avatar", '') |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
292 |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
293 def _getCard(self, target_s, profile_key=C.PROF_KEY_NONE): |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
294 client = self.host.getClient(profile_key) |
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
295 return self.getCard(client, jid.JID(target_s)) |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
296 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
297 def getCard(self, client, target): |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 """Ask server for VCard |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
299 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
300 @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
|
301 @result: id to retrieve the profile |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
302 """ |
2064
ee9621d92cb9
plugin XEP-0054: fix use of full jid for room occupants
Goffi <goffi@goffi.org>
parents:
2060
diff
changeset
|
303 target_bare = target.userhostJID() |
ee9621d92cb9
plugin XEP-0054: fix use of full jid for room occupants
Goffi <goffi@goffi.org>
parents:
2060
diff
changeset
|
304 to_jid = target if self.isRoom(client, target_bare) else target_bare |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
305 log.debug(_(u"Asking for %s's VCard") % to_jid.userhost()) |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
306 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
|
307 reg_request["from"] = client.jid.full() |
2064
ee9621d92cb9
plugin XEP-0054: fix use of full jid for room occupants
Goffi <goffi@goffi.org>
parents:
2060
diff
changeset
|
308 reg_request["to"] = to_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
|
309 reg_request.addElement('vCard', NS_VCARD) |
2064
ee9621d92cb9
plugin XEP-0054: fix use of full jid for room occupants
Goffi <goffi@goffi.org>
parents:
2060
diff
changeset
|
310 reg_request.send(to_jid.full()).addCallbacks(self._getCardCb, self._getCardEb, callbackArgs=[to_jid, client], errbackArgs=[to_jid, client]) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
311 return reg_request["id"] |
42
874de3020e1c
Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
313 def getAvatarFile(self, avatar_hash): |
43 | 314 """Give the full path of avatar from hash |
315 @param hash: SHA1 hash | |
316 @return full_path | |
317 """ | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
318 filename = self.avatar_path + '/' + avatar_hash |
43 | 319 if not os.path.exists(filename): |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
320 log.error(_(u"Asking for an uncached avatar [%s]") % avatar_hash) |
43 | 321 return "" |
322 return filename | |
323 | |
570
bf1505df088c
plugin XEP-0054: fixed bad image resizing
Goffi <goffi@goffi.org>
parents:
562
diff
changeset
|
324 def _buildSetAvatar(self, vcard_set, filepath): |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
325 try: |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
326 img = Image.open(filepath) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
327 except IOError: |
765
787ee59dba9c
plugins radiocol, xep-0054: better handling of upload errors:
souliane <souliane@mailoo.org>
parents:
639
diff
changeset
|
328 return Failure(exceptions.DataError("Can't open image")) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
329 |
1081
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
330 if img.size != AVATAR_DIM: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
331 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
|
332 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
|
333 left, upper = (0, 0) |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
334 right, lower = img.size |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
335 offset = abs(right - lower) / 2 |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
336 if right == min(img.size): |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
337 upper += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
338 lower -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
339 else: |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
340 left += offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
341 right -= offset |
5d89fecdf667
plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents:
1075
diff
changeset
|
342 img = img.crop((left, upper, right, lower)) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
343 img_buf = StringIO() |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
344 img.save(img_buf, 'PNG') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
345 |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
346 vcard_elt = vcard_set.addElement('vCard', NS_VCARD) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
347 photo_elt = vcard_elt.addElement('PHOTO') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
348 photo_elt.addElement('TYPE', content='image/png') |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
349 photo_elt.addElement('BINVAL', content=b64encode(img_buf.getvalue())) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
350 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
|
351 self.saveAvatarFile(img_buf.getvalue(), img_hash) |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
352 return (vcard_set, img_hash) |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
353 |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
354 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
|
355 """Set avatar of the profile |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
356 @param filepath: path of the image of the avatar""" |
1199 | 357 #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
|
358 # 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
|
359 client = self.host.getClient(profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
360 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
361 vcard_set = IQ(client.xmlstream, 'set') |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
362 d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
363 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
364 def elementBuilt(result): |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
365 """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
|
366 set_avatar_elt, img_hash = result |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
367 self.updateCache(client, client.jid.userhostJID(), 'avatar', img_hash) |
1293
0541cb64217e
plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
368 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
|
369 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
370 d.addCallback(elementBuilt) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
371 |
562
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
372 return d |
0bb2e0d1c878
core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents:
560
diff
changeset
|
373 |
64 | 374 |
375 class XEP_0054_handler(XMPPHandler): | |
376 implements(iwokkel.IDisco) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
377 |
64 | 378 def __init__(self, plugin_parent): |
379 self.plugin_parent = plugin_parent | |
380 self.host = plugin_parent.host | |
381 | |
382 def connectionInitialized(self): | |
383 self.xmlstream.addObserver(VCARD_UPDATE, self.update) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
384 |
64 | 385 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
386 return [disco.DiscoFeature(NS_VCARD)] | |
387 | |
388 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
389 return [] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
390 |
48 | 391 def update(self, presence): |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
392 """Called on <presence/> stanza with vcard data |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
393 |
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
394 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
|
395 @param presend(domish.Element): <presence/> stanza |
48 | 396 """ |
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
|
397 from_jid = jid.JID(presence['from']) |
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
|
398 if from_jid.resource and not self.plugin_parent.isRoom(self.parent, from_jid): |
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
|
399 from_jid = from_jid.userhostJID() |
435
c243f4cb2ad9
plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents:
372
diff
changeset
|
400 #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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 |
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
|
406 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
|
407 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
|
408 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
|
409 return |
1317
bd69d341d969
plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
410 |
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
|
411 hash_ = str(photo_elt) |
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
|
412 if not hash_: |
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
|
413 return |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
414 old_avatar = self.plugin_parent.getCache(self.parent, from_jid, 'avatar') |
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
|
415 filename = self.plugin_parent._getFilename(hash_) |
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
|
416 if not old_avatar or old_avatar != hash_: |
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
|
417 if os.path.exists(filename): |
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
|
418 log.debug(u"New avatar found for [{}], it's already in cache, we use it".format(from_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
|
419 self.plugin_parent.updateCache(self.parent, from_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
|
420 else: |
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
|
421 log.debug(u'New avatar found for [{}], requesting vcard'.format(from_jid.full())) |
1978
8156f2116dc9
plugin XEP-0054: fixed getCard calls
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
422 self.plugin_parent.getCard(self.parent, from_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
|
423 else: |
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
|
424 if os.path.exists(filename): |
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
|
425 log.debug(u"avatar for {} already in cache".format(from_jid.full())) |
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
|
426 else: |
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
|
427 log.error(u"Avatar for [{}] should be in cache but it is not ! We get it".format(from_jid.full())) |
1978
8156f2116dc9
plugin XEP-0054: fixed getCard calls
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
428 self.plugin_parent.getCard(self.parent, from_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
|
429 |