annotate sat/plugins/plugin_xep_0054.py @ 3028:ab2696e34d29

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