···1import logging
0023from slixmpp.exceptions import IqError
45class AvatarManager:
000006 def __init__(self, xmpp, config):
7 self._xmpp = xmpp
8 self._path = config["avatars"]["path"]
···1213 self._logger = logging.getLogger("xmpp.avatar")
1415- def save_avatar(self, jid, data, type_):
16 filename = hashlib.sha1(data).hexdigest() + "." + type_.split("/")[1]
17 path = os.path.join(self._path, filename)
18···31 ifrom=self._xmpp._bot_jid_full)
32 type_ = iq["vcard_temp"]["PHOTO"]["TYPE"]
33 data = iq["vcard_temp"]["PHOTO"]["BINVAL"]
34- self.save_avatar(jid, data, type_)
35 return True
36 except IqError:
37 self._logger.debug("Avatar retrieval via XEP-0054/XEP-0153 failed. Probably no vCard for XEP-0054 published")
···43 node="urn:xmpp:avatar:data",
44 max_items=1,
45 ifrom=self._xmpp._bot_jid_full)
00046 except IqError:
47 self._logger.debug("Avatar retrieval via XEP-0084 failed. Probably no avatar published or subscription model not fulfilled.")
48 return False
4950 async def aquire_avatar(self, jid):
51- # First try vCard via 0054/0153
0052 for f in [self.try_xep_0153, self.try_xep_0084]:
53 if await f(jid):
54 self._logger.debug("Avatar retrieval successful for %s",
···58 self._logger.debug("Avatar retrieval failed for %s. Giving up.",
59 jid)
6061- def get_avatar(self, jid):
00062 return self._avatars.get(jid, None)
···1import logging
2+import hashlib
3+import os
45from slixmpp.exceptions import IqError
67class AvatarManager:
8+ """
9+ The AvatarManager class is responsible for aquiring the XMPP avatars
10+ using the means of either XEP-0153 (XEP-0054), XEP-0084 or (in the future)
11+ XEP-0292, storing and providing a publicly available URL to those avatars.
12+ """
13 def __init__(self, xmpp, config):
14 self._xmpp = xmpp
15 self._path = config["avatars"]["path"]
···1920 self._logger = logging.getLogger("xmpp.avatar")
2122+ def _save_avatar(self, jid, data, type_):
23 filename = hashlib.sha1(data).hexdigest() + "." + type_.split("/")[1]
24 path = os.path.join(self._path, filename)
25···38 ifrom=self._xmpp._bot_jid_full)
39 type_ = iq["vcard_temp"]["PHOTO"]["TYPE"]
40 data = iq["vcard_temp"]["PHOTO"]["BINVAL"]
41+ self._save_avatar(jid, data, type_)
42 return True
43 except IqError:
44 self._logger.debug("Avatar retrieval via XEP-0054/XEP-0153 failed. Probably no vCard for XEP-0054 published")
···50 node="urn:xmpp:avatar:data",
51 max_items=1,
52 ifrom=self._xmpp._bot_jid_full)
53+ data = iq["pubsub"]["items"]["substanzas"][0]["data"]
54+ self._save_avatar(jid, data, "image/png")
55+ return True
56 except IqError:
57 self._logger.debug("Avatar retrieval via XEP-0084 failed. Probably no avatar published or subscription model not fulfilled.")
58 return False
5960 async def aquire_avatar(self, jid):
61+ """
62+ Attempt to request the avatar of @jid
63+ """
64 for f in [self.try_xep_0153, self.try_xep_0084]:
65 if await f(jid):
66 self._logger.debug("Avatar retrieval successful for %s",
···70 self._logger.debug("Avatar retrieval failed for %s. Giving up.",
71 jid)
7273+ def get_avatar_url(self, jid):
74+ """
75+ Returns either the URL to the avatar of @jid or None.
76+ """
77 return self._avatars.get(jid, None)