annotate src/plugins/plugin_xep_0054.py @ 916:1a759096ccbd

core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
author Goffi <goffi@goffi.org>
date Fri, 21 Mar 2014 16:27:09 +0100
parents 1fe00f0c9a91
children 01342bfe9f41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
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
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 788
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from logging import debug, info, error
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
24 from twisted.internet import threads
485
ee95ff721b68 plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents: 480
diff changeset
25 from twisted.internet.defer import inlineCallbacks, returnValue
504
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
26 from twisted.words.protocols.jabber import jid
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
28 from twisted.words.xish import domish
765
787ee59dba9c plugins radiocol, xep-0054: better handling of upload errors:
souliane <souliane@mailoo.org>
parents: 639
diff changeset
29 from twisted.python.failure import Failure
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import os.path
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
31
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from zope.interface import implements
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
33
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from wokkel import disco, iwokkel
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
35
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
36 from base64 import b64decode, b64encode
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from hashlib import sha1
504
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
38 from sat.core import exceptions
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
39 from sat.memory.persistent import PersistentDict
788
d6652683c572 plugin XEP-0054: also work with python2-pillow
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 771
diff changeset
40 from PIL import Image
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
41 from cStringIO import StringIO
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
42
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
43 try:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
44 from twisted.words.protocols.xmlstream import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
45 except ImportError:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
46 from wokkel.subprotocols import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
47
365
efbfccfed623 core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents: 291
diff changeset
48 AVATAR_PATH = "avatars"
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
49
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
50 IQ_GET = '/iq[@type="get"]'
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
51 NS_VCARD = 'vcard-temp'
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
52 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
53
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
54 PRESENCE = '/presence'
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
55 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
56 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]'
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
57
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
58 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
59 "name": "XEP 0054 Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
60 "import_name": "XEP-0054",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
61 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
62 "protocols": ["XEP-0054", "XEP-0153"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
63 "dependencies": [],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
64 "main": "XEP_0054",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
65 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
66 "description": _("""Implementation of vcard-temp""")
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
67 }
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
68
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
69
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
70 class XEP_0054(object):
504
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
71 #TODO: - check that nickname is ok
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
72 # - 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
73 # - get missing values
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
74
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def __init__(self, host):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 66
diff changeset
76 info(_("Plugin XEP_0054 initialization"))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.host = host
365
efbfccfed623 core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents: 291
diff changeset
78 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if not os.path.exists(self.avatar_path):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
80 os.makedirs(self.avatar_path)
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
81 self.avatars_cache = PersistentDict(NS_VCARD)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
82 self.avatars_cache.load() # FIXME: resulting deferred must be correctly managed
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
83 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
84 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
85 host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True)
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
86 host.trigger.add("presence_available", self.presenceTrigger)
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
87
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 69
diff changeset
88 def getHandler(self, profile):
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
89 return XEP_0054_handler(self)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
90
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
91 def presenceTrigger(self, presence_elt, client):
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
92 if client.jid.userhost() in self.avatars_cache:
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
93 x_elt = domish.Element((NS_VCARD_UPDATE, 'x'))
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
94 x_elt.addElement('photo', content=self.avatars_cache[client.jid.userhost()])
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
95 presence_elt.addChild(x_elt)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
96
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
97 return True
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
98
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
99 def _fillCachedValues(self, result, client):
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
100 #FIXME: this is really suboptimal, need to be reworked
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
101 # the current naive approach keeps a map between all jids of all profiles
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
102 # in persistent cache, and check if cached jid are in roster, then put avatar
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
103 # hashs in memory.
560
7ffae708b176 plugin XEP-0054: added profile own jid to _fillCachedValues (needed to have its own avatar)
Goffi <goffi@goffi.org>
parents: 551
diff changeset
104 for _jid in client.roster.getBareJids() + [client.jid.userhost()]:
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
105 if _jid in self.avatars_cache:
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
106 self.host.memory.updateEntityData(jid.JID(_jid), "avatar", self.avatars_cache[_jid], client.profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
107
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
108 def profileConnected(self, profile):
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
109 client = self.host.getClient(profile)
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
110 client.roster.got_roster.addCallback(self._fillCachedValues, client)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
111
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
112 def update_cache(self, jid, name, value, profile):
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
113 """update cache value
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
114 - save value in memory in case of change
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
115 @param jid: jid of the owner of the vcard
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
116 @param name: name of the item which changed
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
117 @param value: new value of the item
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
118 @param profile: profile which received the update
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
119 """
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
120 try:
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
121 cached = self.host.memory.getEntityData(jid, [name], profile)
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
122 except exceptions.UnknownEntityError:
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
123 cached = {}
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
124 if not name in cached or cached[name] != value:
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
125 self.host.memory.updateEntityData(jid, name, value, profile)
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
126 if name == "avatar":
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
127 self.avatars_cache[jid.userhost()] = value
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
128
435
c243f4cb2ad9 plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents: 372
diff changeset
129 def get_cache(self, jid, name, profile):
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
130 """return cached value for jid
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
131 @param jid: target contact
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
132 @param name: name of the value ('nick' or 'avatar')
435
c243f4cb2ad9 plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents: 372
diff changeset
133 @param profile: %(doc_profile)s
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
134 @return: wanted value or None"""
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
135 try:
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
136 data = self.host.memory.getEntityData(jid, [name], profile)
65ecbb473cbb core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents: 485
diff changeset
137 except exceptions.UnknownEntityError:
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
138 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
139 return data.get(name)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
140
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def save_photo(self, photo_xml):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """Parse a <PHOTO> elem and save the picture"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
143 for elem in photo_xml.elements():
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if elem.name == 'TYPE':
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 66
diff changeset
145 info(_('Photo of type [%s] found') % str(elem))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
146 if elem.name == 'BINVAL':
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 66
diff changeset
147 debug(_('Decoding binary'))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
148 decoded = b64decode(str(elem))
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
149 image_hash = sha1(decoded).hexdigest()
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
150 filename = self.avatar_path + '/' + image_hash
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if not os.path.exists(filename):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
152 with open(filename, 'wb') as file:
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
153 file.write(decoded)
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
154 debug(_("file saved to %s") % image_hash)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
155 else:
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
156 debug(_("file [%s] already in cache") % image_hash)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
157 return image_hash
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
158
485
ee95ff721b68 plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents: 480
diff changeset
159 @inlineCallbacks
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
160 def vCard2Dict(self, vcard, target, profile):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
161 """Convert a VCard to a dict, and save binaries"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
162 debug(_("parsing vcard"))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
163 dictionary = {}
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
164
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
165 for elem in vcard.elements():
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if elem.name == 'FN':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
167 dictionary['fullname'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
168 elif elem.name == 'NICKNAME':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
169 dictionary['nick'] = unicode(elem)
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
170 self.update_cache(target, 'nick', dictionary['nick'], profile)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
171 elif elem.name == 'URL':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
172 dictionary['website'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
173 elif elem.name == 'EMAIL':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
174 dictionary['email'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
175 elif elem.name == 'BDAY':
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
176 dictionary['birthday'] = unicode(elem)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
177 elif elem.name == 'PHOTO':
485
ee95ff721b68 plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents: 480
diff changeset
178 dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
179 if not dictionary["avatar"]: # can happen in case of e.g. empty photo elem
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
180 del dictionary['avatar']
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
181 else:
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
182 self.update_cache(target, 'avatar', dictionary['avatar'], profile)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
183 else:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
184 info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
185
485
ee95ff721b68 plugin xep-0054: changed deprecated deferredGenerator for inlineCallbacks
Goffi <goffi@goffi.org>
parents: 480
diff changeset
186 returnValue(dictionary)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
187
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
188 def vcard_ok(self, answer, profile):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """Called after the first get IQ"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
190 debug(_("VCard found"))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
191
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if answer.firstChildElement().name == "vCard":
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
193 _jid, steam = self.host.getJidNStream(profile)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
194 try:
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
195 from_jid = jid.JID(answer["from"])
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
196 except KeyError:
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
197 from_jid = _jid.userhostJID()
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
198 d = self.vCard2Dict(answer.firstChildElement(), from_jid, profile)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 504
diff changeset
199 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, profile))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
200 else:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
201 error(_("FIXME: vCard not found as first child element"))
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
202 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) # FIXME: maybe an error message would be better
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
203
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 504
diff changeset
204 def vcard_err(self, failure, profile):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
205 """Called when something is wrong with registration"""
639
99eee75ec1b7 core: better handling of profile_key and don't write the param file anymore
souliane <souliane@mailoo.org>
parents: 609
diff changeset
206 if failure.value.stanza.hasAttribute("from"):
99eee75ec1b7 core: better handling of profile_key and don't write the param file anymore
souliane <souliane@mailoo.org>
parents: 609
diff changeset
207 error(_("Can't find VCard of %s") % failure.value.stanza['from'])
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
208 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) # FIXME: maybe an error message would be better
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
209
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
210 def getCard(self, target_s, profile_key=C.PROF_KEY_NONE):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
211 """Ask server for VCard
560
7ffae708b176 plugin XEP-0054: added profile own jid to _fillCachedValues (needed to have its own avatar)
Goffi <goffi@goffi.org>
parents: 551
diff changeset
212 @param target_s: jid from which we want the VCard
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
213 @result: id to retrieve the profile"""
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
214 current_jid, xmlstream = self.host.getJidNStream(profile_key)
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
215 if not xmlstream:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
216 error(_('Asking vcard for a non-existant or not connected profile'))
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
217 return ""
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 365
diff changeset
218 profile = self.host.memory.getProfileName(profile_key)
560
7ffae708b176 plugin XEP-0054: added profile own jid to _fillCachedValues (needed to have its own avatar)
Goffi <goffi@goffi.org>
parents: 551
diff changeset
219 to_jid = jid.JID(target_s)
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 66
diff changeset
220 debug(_("Asking for %s's VCard") % to_jid.userhost())
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
221 reg_request = IQ(xmlstream, 'get')
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
222 reg_request["from"] = current_jid.full()
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
223 reg_request["to"] = to_jid.userhost()
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
224 reg_request.addElement('vCard', NS_VCARD)
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 504
diff changeset
225 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, callbackArgs=[profile], errbackArgs=[profile])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
226 return reg_request["id"]
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
227
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
228 def getAvatarFile(self, avatar_hash):
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
229 """Give the full path of avatar from hash
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
230 @param hash: SHA1 hash
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
231 @return full_path
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
232 """
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
233 filename = self.avatar_path + '/' + avatar_hash
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
234 if not os.path.exists(filename):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
235 error(_("Asking for an uncached avatar [%s]") % avatar_hash)
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
236 return ""
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
237 return filename
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
238
570
bf1505df088c plugin XEP-0054: fixed bad image resizing
Goffi <goffi@goffi.org>
parents: 562
diff changeset
239 def _buildSetAvatar(self, vcard_set, filepath):
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
240 try:
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
241 img = Image.open(filepath)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
242 except IOError:
765
787ee59dba9c plugins radiocol, xep-0054: better handling of upload errors:
souliane <souliane@mailoo.org>
parents: 639
diff changeset
243 return Failure(exceptions.DataError("Can't open image"))
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
244
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
245 if img.size != (64, 64):
570
bf1505df088c plugin XEP-0054: fixed bad image resizing
Goffi <goffi@goffi.org>
parents: 562
diff changeset
246 img = img.resize((64, 64))
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
247 img_buf = StringIO()
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
248 img.save(img_buf, 'PNG')
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
249
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
250 vcard_elt = vcard_set.addElement('vCard', NS_VCARD)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
251 photo_elt = vcard_elt.addElement('PHOTO')
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
252 photo_elt.addElement('TYPE', content='image/png')
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
253 photo_elt.addElement('BINVAL', content=b64encode(img_buf.getvalue()))
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
254 img_hash = sha1(img_buf.getvalue()).hexdigest()
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
255 return (vcard_set, img_hash)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
256
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
257 def setAvatar(self, filepath, profile_key=C.PROF_KEY_NONE):
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
258 """Set avatar of the profile
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
259 @param filepath: path of the image of the avatar"""
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
260 #TODO: This is a temporary way of setting avatar, as other VCard informations are not managed.
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
261 # A proper full VCard management should be done (and more generaly a public/private profile)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
262 client = self.host.getClient(profile_key)
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
263 if not client:
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
264 raise exceptions.NotConnectedProfileError(_('Trying to set avatar for a non-existant or not connected profile'))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
265
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
266 vcard_set = IQ(client.xmlstream, 'set')
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
267 d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
268
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
269 def elementBuilt(result):
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
270 """Called once the image is at the right size/format, and the vcard set element is build"""
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
271 set_avatar_elt, img_hash = result
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
272 self.avatars_cache[client.jid.userhost()] = img_hash # we need to update the hash, so we can send a new presence
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
273 # element with the right hash
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
274 return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available())
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
275
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
276 d.addCallback(elementBuilt)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
277
562
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
278 return d
0bb2e0d1c878 core, plugin XEP-0054: avatar upload:
Goffi <goffi@goffi.org>
parents: 560
diff changeset
279
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
280
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
281 class XEP_0054_handler(XMPPHandler):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
282 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
283
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
284 def __init__(self, plugin_parent):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
285 self.plugin_parent = plugin_parent
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
286 self.host = plugin_parent.host
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
287
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
288 def connectionInitialized(self):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
289 self.xmlstream.addObserver(VCARD_UPDATE, self.update)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
290
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
291 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
292 return [disco.DiscoFeature(NS_VCARD)]
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
293
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
294 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 57
diff changeset
295 return []
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
296
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
297 def update(self, presence):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
298 """Request for VCard's nickname
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
299 return the cached nickname if exists, else get VCard
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
300 """
281
Goffi <goffi@goffi.org>
parents: 228
diff changeset
301 from_jid = jid.JID(presence['from'])
435
c243f4cb2ad9 plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents: 372
diff changeset
302 #FIXME: wokkel's data_form should be used here
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
303 x_elem = filter(lambda x: x.name == "x", presence.elements())[0] # We only want the "x" element
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
304 for elem in x_elem.elements():
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
305 if elem.name == 'photo':
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
306 _hash = str(elem)
435
c243f4cb2ad9 plugin XEP-0054: cache now use storage
Goffi <goffi@goffi.org>
parents: 372
diff changeset
307 old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar', self.parent.profile)
551
dcec4ba8e72c plugin XEP-0054: naive approach to have a persistent cache of avatars
Goffi <goffi@goffi.org>
parents: 538
diff changeset
308 if not old_avatar or old_avatar != _hash:
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 66
diff changeset
309 debug(_('New avatar found, requesting vcard'))
281
Goffi <goffi@goffi.org>
parents: 228
diff changeset
310 self.plugin_parent.getCard(from_jid.userhost(), self.parent.profile)