annotate sat/plugins/plugin_xep_0054.py @ 3082:9211c0d8b50c

plugin manhole: fixed default value
author Goffi <goffi@goffi.org>
date Fri, 06 Dec 2019 15:19:31 +0100
parents fee60f17ebac
children 9d0df638c8b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
1 #!/usr/bin/env python3
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 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
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 765
diff changeset
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
24
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
25 log = getLogger(__name__)
939
01342bfe9f41 wix: fixed onShowProfile
Goffi <goffi@goffi.org>
parents: 916
diff changeset
26 from twisted.internet import threads, defer
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
27 from twisted.words.protocols.jabber import jid, error
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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
31 from zope.interface import implementer
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
32
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from wokkel import disco, iwokkel
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
34
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
35 from base64 import b64decode, b64encode
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37 from sat.core import exceptions
1341
6dbeb2ef966c plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents: 1317
diff changeset
38 from sat.memory import persistent
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
39 import mimetypes
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
40
1542
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1437
diff changeset
41 try:
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1437
diff changeset
42 from PIL import Image
94901070478e plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents: 1437
diff changeset
43 except:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
44 raise exceptions.MissingModule(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
45 "Missing module pillow, please download/install it from https://python-pillow.github.io"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
46 )
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
47 import io
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
48
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
49 try:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
50 from twisted.words.protocols.xmlstream import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
51 except ImportError:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
52 from wokkel.subprotocols import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
53
365
efbfccfed623 core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents: 291
diff changeset
54 AVATAR_PATH = "avatars"
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
55 # AVATAR_DIM = (64, 64) #  FIXME: dim are not adapted to modern resolutions !
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
56 AVATAR_DIM = (128, 128)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
57
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
58 IQ_GET = '/iq[@type="get"]'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
59 NS_VCARD = "vcard-temp"
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
60 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
61
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
62 PRESENCE = "/presence"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
63 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
64 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]'
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
65
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
66 CACHED_DATA = {"avatar", "nick"}
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
67 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
68
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
69 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
70 C.PI_NAME: "XEP 0054 Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
71 C.PI_IMPORT_NAME: "XEP-0054",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
72 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
73 C.PI_PROTOCOLS: ["XEP-0054", "XEP-0153"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
74 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
75 C.PI_RECOMMENDATIONS: ["XEP-0045"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
76 C.PI_MAIN: "XEP_0054",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
77 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
78 C.PI_DESCRIPTION: _("""Implementation of vcard-temp"""),
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
79 }
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
80
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
81
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
82 class XEP_0054(object):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
83 # TODO: - check that nickname is ok
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
84 # - 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
85 # - get missing values
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
86
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
88 log.info(_("Plugin XEP_0054 initialization"))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
90 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
91 "avatarGet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
92 ".plugin",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
93 in_sign="sbbs",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
94 out_sign="s",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
95 method=self._getAvatar,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
96 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
97 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
98 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
99 "avatarSet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
100 ".plugin",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
101 in_sign="ss",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
102 out_sign="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
103 method=self._setAvatar,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
104 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
105 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
106 host.trigger.add("presence_available", self.presenceAvailableTrigger)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
107 host.memory.setSignalOnUpdate("avatar")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
108 host.memory.setSignalOnUpdate("nick")
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
109
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2123
diff changeset
110 def getHandler(self, client):
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
111 return XEP_0054_handler(self)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
112
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
113 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
114 """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
115
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
116 @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
117 @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
118 """
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
119 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
120 muc_plg = self.host.plugins["XEP-0045"]
2462
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
121 except KeyError:
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
122 return False
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
123
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
124 try:
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
125 muc_plg.checkRoomJoined(client, entity_jid.userhostJID())
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
126 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
127 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
128 else:
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
129 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
130
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
131 def getBareOrFull(self, client, jid_):
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
132 """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
133
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
134 @param jid_(jid.JID): entity to test
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
135 @return (jid.JID): bare or full jid
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
136 """
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
137 if jid_.resource:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
138 if not self.isRoom(client, jid_):
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
139 return jid_.userhostJID()
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
140 return jid_
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
141
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
142 def presenceAvailableTrigger(self, presence_elt, client):
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
143 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
144 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
145 avatar_hash = client._cache_0054[client.jid.userhost()]["avatar"]
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
146 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
147 log.info("No avatar in cache for {}".format(client.jid.userhost()))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
148 return True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
149 x_elt = domish.Element((NS_VCARD_UPDATE, "x"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
150 x_elt.addElement("photo", content=avatar_hash)
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
151 presence_elt.addChild(x_elt)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
152 return True
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
153
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
154 @defer.inlineCallbacks
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2123
diff changeset
155 def profileConnecting(self, client):
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2123
diff changeset
156 client._cache_0054 = persistent.PersistentBinaryDict(NS_VCARD, client.profile)
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
157 yield client._cache_0054.load()
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2123
diff changeset
158 self._fillCachedValues(client.profile)
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
159
1293
0541cb64217e plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
160 def _fillCachedValues(self, profile):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
161 # FIXME: this may need to be reworked
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
162 # 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
163 # 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
164 # 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
165 # 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
166 # this is not possible with vcard-tmp, but it is with XEP-0084).
2581
395a3d1c2888 plugin XEP-0045: fixed joining workflow:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 # Loading avatar on demand per jid may be an option to investigate.
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
168 client = self.host.getClient(profile)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
169 for jid_s, data in client._cache_0054.items():
1293
0541cb64217e plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
170 jid_ = jid.JID(jid_s)
1341
6dbeb2ef966c plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents: 1317
diff changeset
171 for name in CACHED_DATA:
6dbeb2ef966c plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents: 1317
diff changeset
172 try:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
173 value = data[name]
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
174 if value is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
175 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
176 "{name} value for {jid_} is None, ignoring".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
177 name=name, jid_=jid_
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
178 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
179 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
180 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
181 self.host.memory.updateEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
182 jid_, name, data[name], silent=True, profile_key=profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
183 )
1341
6dbeb2ef966c plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents: 1317
diff changeset
184 except KeyError:
6dbeb2ef966c plugin XEP-0054: "nick" is now keeped in cache:
Goffi <goffi@goffi.org>
parents: 1317
diff changeset
185 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
186
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
187 def updateCache(self, client, jid_, name, value):
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
188 """update cache value
1293
0541cb64217e plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
189
0541cb64217e plugin XEP-0054: couple of fixes in VCard/avatar management:
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
190 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
191 @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
192 @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
193 @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
194 None to delete
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
195 """
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
196 jid_ = self.getBareOrFull(client, jid_)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
197 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
198
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
199 if value is None:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
200 try:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
201 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
202 except (KeyError, exceptions.UnknownEntityError):
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
203 pass
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
204 if name in CACHED_DATA:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
205 try:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
206 del client._cache_0054[jid_s][name]
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
207 except KeyError:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
208 pass
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
209 else:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
210 client._cache_0054.force(jid_s)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
211 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
212 self.host.memory.updateEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
213 jid_, name, value, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
214 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
215 if name in CACHED_DATA:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
216 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
217 client._cache_0054.force(jid_s)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
218
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
219 def getCache(self, client, entity_jid, name):
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
220 """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
221
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
222 @param entity_jid(jid.JID): target contact
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
223 @param name(unicode): name of the value ('nick' or 'avatar')
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
224 @return(unicode, None): wanted value or None"""
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
225 entity_jid = self.getBareOrFull(client, entity_jid)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
226 try:
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
227 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
228 except exceptions.UnknownEntityError:
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
229 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
230 return data.get(name)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
231
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
232 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
233 """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
234 # 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
235 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
236 mime_type = str(next(photo_elt.elements(NS_VCARD, "TYPE")))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
237 except StopIteration:
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
238 mime_type = None
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
239 else:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
240 if not mime_type:
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
241 # MIME type not know, we'll only support PNG files
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
242 # TODO: autodetection using e.g. "magic" module
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
243 # (https://pypi.org/project/python-magic/)
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
244 mime_type = None
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
245 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
246 if mime_type == "image/x-png":
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
247 # 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
248 mime_type = "image/png"
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
249 else:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
250 # TODO: handle other image formats (svg?)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
251 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
252 "following avatar image format is not handled: {type} [{jid}]".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
253 type=mime_type, jid=entity_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
254 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
255 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
256 raise Failure(exceptions.DataError())
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
257
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
258 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
259 assert ext is not None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
260 if ext == ".jpe":
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
261 ext = ".jpg"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
262 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
263 "photo of type {type} with extension {ext} found [{jid}]".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
264 type=mime_type, ext=ext, jid=entity_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
265 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
266 )
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
267 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
268 buf = str(next(photo_elt.elements(NS_VCARD, "BINVAL")))
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
269 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
270 log.warning("BINVAL element not found")
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
271 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
272 if not buf:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
273 log.warning("empty avatar for {jid}".format(jid=entity_jid.full()))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
274 raise Failure(exceptions.NotFound())
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
275 if mime_type is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
276 log.warning(_("no MIME type found for {entity}'s avatar, assuming image/png")
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
277 .format(entity=entity_jid.full()))
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
278 if buf[:8] != b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a':
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
279 log.warning("this is not a PNG file, ignoring it")
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
280 raise Failure(exceptions.DataError())
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
281 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
282 mime_type = "image/png"
3003
e624550d5c24 plugin XEP-0054: reject image without MIME type if it's not PNG
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
283
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
284 log.debug(_("Decoding binary"))
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
285 decoded = b64decode(buf)
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
286 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
287 image_hash = sha1(decoded).hexdigest()
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
288 with client.cache.cacheData(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
289 PLUGIN_INFO["import_name"],
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
290 image_hash,
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
291 mime_type,
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
292 # we keep in cache for 1 year
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
293 MAX_AGE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
294 ) as f:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
295 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
296 return image_hash
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
297
939
01342bfe9f41 wix: fixed onShowProfile
Goffi <goffi@goffi.org>
parents: 916
diff changeset
298 @defer.inlineCallbacks
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
299 def vCard2Dict(self, client, vcard, entity_jid):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
300 """Convert a VCard to a dict, and save binaries"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
301 log.debug(("parsing vcard"))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
302 vcard_dict = {}
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
303
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
304 for elem in vcard.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
305 if elem.name == "FN":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
306 vcard_dict["fullname"] = str(elem)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
307 elif elem.name == "NICKNAME":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
308 vcard_dict["nick"] = str(elem)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
309 self.updateCache(client, entity_jid, "nick", vcard_dict["nick"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
310 elif elem.name == "URL":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
311 vcard_dict["website"] = str(elem)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
312 elif elem.name == "EMAIL":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
313 vcard_dict["email"] = str(elem)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
314 elif elem.name == "BDAY":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
315 vcard_dict["birthday"] = str(elem)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
316 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
317 # TODO: handle EXTVAL
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
318 try:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
319 avatar_hash = yield threads.deferToThread(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
320 self.savePhoto, client, elem, entity_jid
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
321 )
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
322 except (exceptions.DataError, exceptions.NotFound):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
323 avatar_hash = ""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
324 vcard_dict["avatar"] = avatar_hash
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
325 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
326 log.error("avatar saving error: {}".format(e))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
327 avatar_hash = None
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
328 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
329 vcard_dict["avatar"] = avatar_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
330 self.updateCache(client, entity_jid, "avatar", avatar_hash)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
331 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
332 log.debug("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
333
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
334 # 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
335 # 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
336 # and we reset them
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
337 for datum in CACHED_DATA.difference(list(vcard_dict.keys())):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
338 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
339 "reseting vcard datum [{datum}] for {entity}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
340 datum=datum, entity=entity_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
341 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
342 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
343 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
344
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
345 defer.returnValue(vcard_dict)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
346
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
347 def _vCardCb(self, vcard_elt, to_jid, client):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
348 """Called after the first get IQ"""
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
349 log.debug(_("VCard found"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
350 iq_elt = vcard_elt.parent
2046
b99bd02ea643 plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents: 1978
diff changeset
351 try:
b99bd02ea643 plugin XEP-0045, XEP-0054, XEP-0096: deprecated bridge method are not used anymore
Goffi <goffi@goffi.org>
parents: 1978
diff changeset
352 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
353 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
354 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
355 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
356 return d
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
357
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
358 def _vCardEb(self, failure_, to_jid, client):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
359 """Called when something is wrong with registration"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
360 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
361 "Can't get vCard for {jid}: {failure}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
362 jid=to_jid.full, failure=failure_
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
363 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
364 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
365 self.updateCache(client, to_jid, "avatar", None)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
366
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
367 def _getVcardElt(self, iq_elt):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
368 return next(iq_elt.elements(NS_VCARD, "vCard"))
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
369
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
370 def getCardRaw(self, client, entity_jid):
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
371 """get raw vCard XML
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
372
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
373 params are as in [getCard]
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
374 """
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
375 entity_jid = self.getBareOrFull(client, entity_jid)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
376 log.debug("Asking for {}'s VCard".format(entity_jid.full()))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
377 reg_request = client.IQ("get")
1970
200cd707a46d plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
378 reg_request["from"] = client.jid.full()
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
379 reg_request["to"] = entity_jid.full()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
380 reg_request.addElement("vCard", NS_VCARD)
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
381 d = reg_request.send(entity_jid.full())
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
382 d.addCallback(self._getVcardElt)
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
383 return d
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
384
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
385 def getCard(self, client, entity_jid):
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
386 """Ask server for VCard
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
387
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
388 @param entity_jid(jid.JID): jid from which we want the VCard
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
389 @result: id to retrieve the profile
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
390 """
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
391 d = self.getCardRaw(client, entity_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
392 d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
393 self._vCardCb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
394 self._vCardEb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
395 callbackArgs=[entity_jid, client],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
396 errbackArgs=[entity_jid, client],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
397 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
398 return d
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
399
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
400 def _getCardCb(self, __, client, entity):
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
401 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
402 return client._cache_0054[entity.full()]["avatar"]
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
403 except KeyError:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
404 raise Failure(exceptions.NotFound())
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
405
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
406 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
407 client = self.host.getClient(profile)
2258
074c2f157dc9 plugin XEP-0054: fixed not transmitted arguments in getAvatar
Goffi <goffi@goffi.org>
parents: 2252
diff changeset
408 d = self.getAvatar(client, jid.JID(entity), cache_only, hash_only)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
409 d.addErrback(lambda __: "")
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
410
2072
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
411 return d
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
412
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
413 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
414 """get avatar full path or hash
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
415
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
416 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
417 @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
418 @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
419 not in cache
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
420 @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
421 @raise exceptions.NotFound: no avatar found
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
422 """
2462
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
423 if not entity.resource and self.isRoom(client, entity):
2cad04f38bac plugin XEP-0054: return always False in isRoom is plugin XEP-0045 is not available + raise NotFound if avatar is requested for a MUC room (not the occupants, the room itself)
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
424 raise exceptions.NotFound
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
425 entity = self.getBareOrFull(client, entity)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
426 full_path = None
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
427
2072
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
428 try:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
429 # we first check if we have avatar in cache
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
430 avatar_hash = client._cache_0054[entity.full()]["avatar"]
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
431 if avatar_hash:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
432 # avatar is known and exists
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
433 full_path = client.cache.getFilePath(avatar_hash)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
434 if full_path is None:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
435 # cache file is not available (probably expired)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
436 raise KeyError
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
437 else:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
438 # avatar has already been checked but it is not set
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
439 full_path = ""
2072
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
440 except KeyError:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
441 # avatar is not in cache
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
442 if cache_only:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
443 return defer.fail(Failure(exceptions.NotFound()))
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
444 # we request vCard to get avatar
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
445 d = self.getCard(client, entity)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
446 d.addCallback(self._getCardCb, client, entity)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
447 else:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
448 # 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
449 d = defer.succeed(avatar_hash)
2072
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
450
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
451 if not hash_only:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
452 # full path is requested
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
453 if full_path is None:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
454 d.addCallback(client.cache.getFilePath)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
455 else:
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
456 d.addCallback(lambda __: full_path)
2072
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
457 return d
11fb5f5e2f89 plugin XEP-0054(XEP-0153): added a getAvatar:
Goffi <goffi@goffi.org>
parents: 2069
diff changeset
458
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
459 @defer.inlineCallbacks
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
460 def getNick(self, client, entity):
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
461 """get nick from cache, or check vCard
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
462
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
463 @param entity(jid.JID): entity to get nick from
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
464 @return(unicode, None): nick or None if not found
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
465 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
466 nick = self.getCache(client, entity, "nick")
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
467 if nick is not None:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
468 defer.returnValue(nick)
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
469 yield self.getCard(client, entity)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
470 defer.returnValue(self.getCache(client, entity, "nick"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
471
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
472 @defer.inlineCallbacks
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
473 def setNick(self, client, nick):
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
474 """update our vCard and set a nickname
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
475
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
476 @param nick(unicode): new nickname to use
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
477 """
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
478 jid_ = client.jid.userhostJID()
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
479 try:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
480 vcard_elt = yield self.getCardRaw(client, jid_)
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
481 except error.StanzaError as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
482 if e.condition == "item-not-found":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
483 vcard_elt = domish.Element((NS_VCARD, "vCard"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
484 else:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
485 raise e
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
486 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
487 nickname_elt = next(vcard_elt.elements(NS_VCARD, "NICKNAME"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
488 except StopIteration:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
489 pass
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
490 else:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
491 vcard_elt.children.remove(nickname_elt)
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
492
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
493 nickname_elt = vcard_elt.addElement((NS_VCARD, "NICKNAME"), content=nick)
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
494 iq_elt = client.IQ()
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
495 vcard_elt = iq_elt.addChild(vcard_elt)
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
496 yield iq_elt.send()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
497 self.updateCache(client, jid_, "nick", str(nick))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
498
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
499 def _buildSetAvatar(self, client, vcard_elt, file_path):
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
500 # 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
501 try:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
502 img = Image.open(file_path)
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
503 except IOError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
504 return Failure(exceptions.DataError("Can't open image"))
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
505
1081
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
506 if img.size != AVATAR_DIM:
2506
516bf5309517 plugin XEP-0054: removed Image.ANTIALIAS filter:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
507 img.thumbnail(AVATAR_DIM)
1081
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
508 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
509 left, upper = (0, 0)
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
510 right, lower = img.size
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
511 offset = abs(right - lower) / 2
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
512 if right == min(img.size):
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
513 upper += offset
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
514 lower -= offset
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
515 else:
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
516 left += offset
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
517 right -= offset
5d89fecdf667 plugin XEP-0054: crop uploaded avatar to get a square
souliane <souliane@mailoo.org>
parents: 1075
diff changeset
518 img = img.crop((left, upper, right, lower))
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
519 img_buf = io.BytesIO()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
520 img.save(img_buf, "PNG")
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
521
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
522 photo_elt = vcard_elt.addElement("PHOTO")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
523 photo_elt.addElement("TYPE", content="image/png")
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
524 image_b64 = b64encode(img_buf.getvalue()).decode('utf-8')
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
525 photo_elt.addElement("BINVAL", content=image_b64)
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
526 image_hash = sha1(img_buf.getvalue()).hexdigest()
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
527 with client.cache.cacheData(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
528 PLUGIN_INFO["import_name"], image_hash, "image/png", MAX_AGE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
529 ) as f:
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
530 f.write(img_buf.getvalue())
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
531 return image_hash
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
532
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
533 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
534 client = self.host.getClient(profile_key)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
535 return self.setAvatar(client, file_path)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
536
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
537 @defer.inlineCallbacks
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
538 def setAvatar(self, client, file_path):
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
539 """Set avatar of the profile
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
540
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
541 @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
542 """
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
543 try:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
544 # we first check if a vcard already exists, to keep data
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
545 vcard_elt = yield self.getCardRaw(client, client.jid.userhostJID())
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
546 except error.StanzaError as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
547 if e.condition == "item-not-found":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
548 vcard_elt = domish.Element((NS_VCARD, "vCard"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
549 else:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
550 raise e
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
551 else:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
552 # the vcard exists, we need to remove PHOTO element as we'll make a new one
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
553 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
554 photo_elt = next(vcard_elt.elements(NS_VCARD, "PHOTO"))
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
555 except StopIteration:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
556 pass
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
557 else:
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
558 vcard_elt.children.remove(photo_elt)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
559
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
560 iq_elt = client.IQ()
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
561 iq_elt.addChild(vcard_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
562 image_hash = yield threads.deferToThread(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
563 self._buildSetAvatar, client, vcard_elt, file_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
564 )
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
565 # image is now at the right size/format
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
566
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
567 self.updateCache(client, client.jid.userhostJID(), "avatar", image_hash)
2252
cffa50c9f26b plugin XEP-0054: nick handling + don't remove data on avatar set
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
568 yield iq_elt.send()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
569 client.presence.available() # FIXME: should send the current presence, not always "available" !
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
570
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
571
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
572 @implementer(iwokkel.IDisco)
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
573 class XEP_0054_handler(XMPPHandler):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
574
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
575 def __init__(self, plugin_parent):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
576 self.plugin_parent = plugin_parent
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
577 self.host = plugin_parent.host
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
578
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
579 def connectionInitialized(self):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
580 self.xmlstream.addObserver(VCARD_UPDATE, self.update)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
581
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
582 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
583 return [disco.DiscoFeature(NS_VCARD)]
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
584
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
585 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
586 return []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
587
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
588 def _checkAvatarHash(self, __, client, entity, given_hash):
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
589 """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
590 # 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
591 # TODO: try to avoid re-requesting avatar in this case
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
592 computed_hash = self.plugin_parent.getCache(client, entity, "avatar")
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
593 if computed_hash != given_hash:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
594 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
595 "computed hash differs from given hash for {entity}:\n"
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
596 "computed: {computed}\ngiven: {given}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
597 entity=entity, computed=computed_hash, given=given_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
598 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
599 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
600
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
601 def update(self, presence):
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
602 """Called on <presence/> stanza with vcard data
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
603
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
604 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
605 @param presend(domish.Element): <presence/> stanza
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
606 """
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
607 client = self.parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
608 entity_jid = self.plugin_parent.getBareOrFull(client, jid.JID(presence["from"]))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
609 # 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
610 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
611 x_elt = next(presence.elements(NS_VCARD_UPDATE, "x"))
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
612 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
613 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
614
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
615 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
616 photo_elt = next(x_elt.elements(NS_VCARD_UPDATE, "photo"))
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
617 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
618 return
1317
bd69d341d969 plugin xep-0054: various improvments on avatars management:
Goffi <goffi@goffi.org>
parents: 1315
diff changeset
619
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
620 hash_ = str(photo_elt).strip()
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
621 if hash_ == C.HASH_SHA1_EMPTY:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
622 hash_ = ""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
623 old_avatar = self.plugin_parent.getCache(client, entity_jid, "avatar")
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
624
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
625 if old_avatar == hash_:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
626 # no change, we can return...
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
627 if hash_:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
628 # ...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
629 file_path = client.cache.getFilePath(hash_)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
630 if file_path is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
631 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
632 "Avatar for [{}] should be in cache but it is not! We get it".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
633 entity_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
634 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
635 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
636 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
637 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
638 log.debug("avatar for {} already in cache".format(entity_jid.full()))
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
639 return
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
640
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
641 if not hash_:
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
642 # the avatar has been removed
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
643 # 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
644 # but it is empty on purpose
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
645 self.plugin_parent.updateCache(client, entity_jid, "avatar", "")
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
646 return
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
647
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
648 file_path = client.cache.getFilePath(hash_)
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
649 if file_path is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
650 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
651 "New avatar found for [{}], it's already in cache, we use it".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
652 entity_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
653 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
654 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
655 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
656 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
657 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3003
diff changeset
658 "New avatar found for [{}], requesting vcard".format(entity_jid.full())
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2581
diff changeset
659 )
2123
c42aab22c2c0 plugin XEP-0054, quick frontend(app): various improvments:
Goffi <goffi@goffi.org>
parents: 2072
diff changeset
660 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
661 d.addCallback(self._checkAvatarHash, client, entity_jid, hash_)