Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0054.py @ 587:952322b1d490
Remove trailing whitespaces.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:34 +0100 |
parents | ca13633d3b6b |
children | beaf6bec2fcd |
comparison
equal
deleted
inserted
replaced
586:6a718ede8be1 | 587:952322b1d490 |
---|---|
88 def presenceTrigger(self, presence_elt, client): | 88 def presenceTrigger(self, presence_elt, client): |
89 if client.jid.userhost() in self.avatars_cache: | 89 if client.jid.userhost() in self.avatars_cache: |
90 x_elt = domish.Element((NS_VCARD_UPDATE, 'x')) | 90 x_elt = domish.Element((NS_VCARD_UPDATE, 'x')) |
91 x_elt.addElement('photo', content=self.avatars_cache[client.jid.userhost()]) | 91 x_elt.addElement('photo', content=self.avatars_cache[client.jid.userhost()]) |
92 presence_elt.addChild(x_elt) | 92 presence_elt.addChild(x_elt) |
93 | 93 |
94 return True | 94 return True |
95 | 95 |
96 def _fillCachedValues(self, result, client): | 96 def _fillCachedValues(self, result, client): |
97 #FIXME: this is really suboptimal, need to be reworked | 97 #FIXME: this is really suboptimal, need to be reworked |
98 # the current naive approach keeps a map between all jids of all profiles | 98 # the current naive approach keeps a map between all jids of all profiles |
99 # in persistent cache, and check if cached jid are in roster, then put avatar | 99 # in persistent cache, and check if cached jid are in roster, then put avatar |
100 # hashs in memory. | 100 # hashs in memory. |
101 for _jid in client.roster.getBareJids() + [client.jid.userhost()]: | 101 for _jid in client.roster.getBareJids() + [client.jid.userhost()]: |
102 if _jid in self.avatars_cache: | 102 if _jid in self.avatars_cache: |
103 self.host.memory.updateEntityData(jid.JID(_jid), "avatar", self.avatars_cache[_jid], client.profile) | 103 self.host.memory.updateEntityData(jid.JID(_jid), "avatar", self.avatars_cache[_jid], client.profile) |
104 | 104 |
105 def profileConnected(self, profile): | 105 def profileConnected(self, profile): |
106 client = self.host.getClient(profile) | 106 client = self.host.getClient(profile) |
107 client.roster.got_roster.addCallback(self._fillCachedValues, client) | 107 client.roster.got_roster.addCallback(self._fillCachedValues, client) |
108 | 108 |
109 def update_cache(self, jid, name, value, profile): | 109 def update_cache(self, jid, name, value, profile): |
110 """update cache value | 110 """update cache value |
111 - save value in memory in case of change | 111 - save value in memory in case of change |
112 @param jid: jid of the owner of the vcard | 112 @param jid: jid of the owner of the vcard |
113 @param name: name of the item which changed | 113 @param name: name of the item which changed |
120 cached = {} | 120 cached = {} |
121 if not name in cached or cached[name] != value: | 121 if not name in cached or cached[name] != value: |
122 self.host.memory.updateEntityData(jid, name, value, profile) | 122 self.host.memory.updateEntityData(jid, name, value, profile) |
123 if name == "avatar": | 123 if name == "avatar": |
124 self.avatars_cache[jid.userhost()] = value | 124 self.avatars_cache[jid.userhost()] = value |
125 | 125 |
126 def get_cache(self, jid, name, profile): | 126 def get_cache(self, jid, name, profile): |
127 """return cached value for jid | 127 """return cached value for jid |
128 @param jid: target contact | 128 @param jid: target contact |
129 @param name: name of the value ('nick' or 'avatar') | 129 @param name: name of the value ('nick' or 'avatar') |
130 @param profile: %(doc_profile)s | 130 @param profile: %(doc_profile)s |
156 @inlineCallbacks | 156 @inlineCallbacks |
157 def vCard2Dict(self, vcard, target, profile): | 157 def vCard2Dict(self, vcard, target, profile): |
158 """Convert a VCard to a dict, and save binaries""" | 158 """Convert a VCard to a dict, and save binaries""" |
159 debug (_("parsing vcard")) | 159 debug (_("parsing vcard")) |
160 dictionary = {} | 160 dictionary = {} |
161 | 161 |
162 for elem in vcard.elements(): | 162 for elem in vcard.elements(): |
163 if elem.name == 'FN': | 163 if elem.name == 'FN': |
164 dictionary['fullname'] = unicode(elem) | 164 dictionary['fullname'] = unicode(elem) |
165 elif elem.name == 'NICKNAME': | 165 elif elem.name == 'NICKNAME': |
166 dictionary['nick'] = unicode(elem) | 166 dictionary['nick'] = unicode(elem) |
168 elif elem.name == 'URL': | 168 elif elem.name == 'URL': |
169 dictionary['website'] = unicode(elem) | 169 dictionary['website'] = unicode(elem) |
170 elif elem.name == 'EMAIL': | 170 elif elem.name == 'EMAIL': |
171 dictionary['email'] = unicode(elem) | 171 dictionary['email'] = unicode(elem) |
172 elif elem.name == 'BDAY': | 172 elif elem.name == 'BDAY': |
173 dictionary['birthday'] = unicode(elem) | 173 dictionary['birthday'] = unicode(elem) |
174 elif elem.name == 'PHOTO': | 174 elif elem.name == 'PHOTO': |
175 dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem) | 175 dictionary["avatar"] = yield threads.deferToThread(self.save_photo, elem) |
176 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem | 176 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem |
177 del dictionary['avatar'] | 177 del dictionary['avatar'] |
178 else: | 178 else: |
200 | 200 |
201 def vcard_err(self, failure, profile): | 201 def vcard_err(self, failure, profile): |
202 """Called when something is wrong with registration""" | 202 """Called when something is wrong with registration""" |
203 error (_("Can't find VCard of %s") % failure.value.stanza['from']) | 203 error (_("Can't find VCard of %s") % failure.value.stanza['from']) |
204 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) #FIXME: maybe an error message would be better | 204 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) #FIXME: maybe an error message would be better |
205 | 205 |
206 def getCard(self, target_s, profile_key='@DEFAULT@'): | 206 def getCard(self, target_s, profile_key='@DEFAULT@'): |
207 """Ask server for VCard | 207 """Ask server for VCard |
208 @param target_s: jid from which we want the VCard | 208 @param target_s: jid from which we want the VCard |
209 @result: id to retrieve the profile""" | 209 @result: id to retrieve the profile""" |
210 current_jid, xmlstream = self.host.getJidNStream(profile_key) | 210 current_jid, xmlstream = self.host.getJidNStream(profile_key) |
217 reg_request=IQ(xmlstream,'get') | 217 reg_request=IQ(xmlstream,'get') |
218 reg_request["from"]=current_jid.full() | 218 reg_request["from"]=current_jid.full() |
219 reg_request["to"] = to_jid.userhost() | 219 reg_request["to"] = to_jid.userhost() |
220 reg_request.addElement('vCard', NS_VCARD) | 220 reg_request.addElement('vCard', NS_VCARD) |
221 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, callbackArgs=[profile], errbackArgs=[profile]) | 221 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, callbackArgs=[profile], errbackArgs=[profile]) |
222 return reg_request["id"] | 222 return reg_request["id"] |
223 | 223 |
224 def getAvatarFile(self, avatar_hash): | 224 def getAvatarFile(self, avatar_hash): |
225 """Give the full path of avatar from hash | 225 """Give the full path of avatar from hash |
226 @param hash: SHA1 hash | 226 @param hash: SHA1 hash |
227 @return full_path | 227 @return full_path |
256 #TODO: This is a temporary way of setting avatar, as other VCard informations are not managed. | 256 #TODO: This is a temporary way of setting avatar, as other VCard informations are not managed. |
257 # A proper full VCard management should be done (and more generaly a public/private profile) | 257 # A proper full VCard management should be done (and more generaly a public/private profile) |
258 client = self.host.getClient(profile_key) | 258 client = self.host.getClient(profile_key) |
259 if not client: | 259 if not client: |
260 raise exceptions.NotConnectedProfileError(_('Trying to set avatar for a non-existant or not connected profile')) | 260 raise exceptions.NotConnectedProfileError(_('Trying to set avatar for a non-existant or not connected profile')) |
261 | 261 |
262 vcard_set = IQ(client.xmlstream,'set') | 262 vcard_set = IQ(client.xmlstream,'set') |
263 d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath) | 263 d = threads.deferToThread(self._buildSetAvatar, vcard_set, filepath) |
264 | 264 |
265 def elementBuilt(result): | 265 def elementBuilt(result): |
266 """Called once the image is at the right size/format, and the vcard set element is build""" | 266 """Called once the image is at the right size/format, and the vcard set element is build""" |
267 set_avatar_elt, img_hash = result | 267 set_avatar_elt, img_hash = result |
268 self.avatars_cache[client.jid.userhost()] = img_hash # we need to update the hash, so we can send a new presence | 268 self.avatars_cache[client.jid.userhost()] = img_hash # we need to update the hash, so we can send a new presence |
269 # element with the right hash | 269 # element with the right hash |
270 return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available()) | 270 return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available()) |
271 | 271 |
272 d.addCallback(elementBuilt) | 272 d.addCallback(elementBuilt) |
273 | 273 |
274 return d | 274 return d |
275 | 275 |
276 | 276 |
277 class XEP_0054_handler(XMPPHandler): | 277 class XEP_0054_handler(XMPPHandler): |
278 implements(iwokkel.IDisco) | 278 implements(iwokkel.IDisco) |
279 | 279 |
280 def __init__(self, plugin_parent): | 280 def __init__(self, plugin_parent): |
281 self.plugin_parent = plugin_parent | 281 self.plugin_parent = plugin_parent |
282 self.host = plugin_parent.host | 282 self.host = plugin_parent.host |
283 | 283 |
284 def connectionInitialized(self): | 284 def connectionInitialized(self): |
285 self.xmlstream.addObserver(VCARD_UPDATE, self.update) | 285 self.xmlstream.addObserver(VCARD_UPDATE, self.update) |
286 | 286 |
287 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 287 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
288 return [disco.DiscoFeature(NS_VCARD)] | 288 return [disco.DiscoFeature(NS_VCARD)] |
289 | 289 |
290 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 290 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
291 return [] | 291 return [] |
292 | 292 |
293 def update(self, presence): | 293 def update(self, presence): |
294 """Request for VCard's nickname | 294 """Request for VCard's nickname |
295 return the cached nickname if exists, else get VCard | 295 return the cached nickname if exists, else get VCard |
296 """ | 296 """ |
297 from_jid = jid.JID(presence['from']) | 297 from_jid = jid.JID(presence['from']) |