annotate plugins/plugin_xep_0054.py @ 56:01ea5680207e

removed debug comments
author Goffi <goffi@goffi.org>
date Sun, 10 Jan 2010 17:35:41 +1100
parents 4392f1fdb064
children a5b5fb5fc9fd
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
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing xep-0054
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
7
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
12
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
17
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
21
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, error
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os.path
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import pdb
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
30
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from zope.interface import implements
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
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from base64 import b64decode
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from hashlib import sha1
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from time import sleep
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
38
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
39 try:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
40 from twisted.words.protocols.xmlstream import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
41 except ImportError:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
42 from wokkel.subprotocols import XMPPHandler
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
43
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
44 AVATAR_PATH = "/avatars"
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
45
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
46 IQ_GET = '/iq[@type="get"]'
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
47 NS_VCARD = 'vcard-temp'
44
bfa7086d26d6 plugin XEP-0054: fixed forgotten copy/paste change
Goffi <goffi@goffi.org>
parents: 43
diff changeset
48 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
49
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
50 PRESENCE = '/presence'
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
51 NS_VCARD_UPDATE = 'vcard-temp:x:update'
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
52 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]'
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
53
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
54 PLUGIN_INFO = {
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "name": "XEP 0054 Plugin",
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "import_name": "XEP_0054",
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "type": "XEP",
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
58 "protocols": ["XEP-0054", "XEP-0153"],
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "dependencies": [],
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
60 "main": "XEP_0054",
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "description": """Implementation of vcard-temp"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
62 }
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
63
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
64 class XEP_0054(XMPPHandler):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
65 implements(iwokkel.IDisco)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
66
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, host):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
68 info("Plugin XEP_0054 initialization")
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.host = host
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.avatar_path = os.path.expanduser(self.host.get_const('local_dir') + AVATAR_PATH)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
71 self.vcard_cache = host.memory.getPrivate("vcard_cache") or {} #used to store nicknames and avatar, key = jid
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if not os.path.exists(self.avatar_path):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
73 os.makedirs(self.avatar_path)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
74 host.bridge.addMethod("getProfile", ".communication", in_sign='s', out_sign='s', method=self.getProfile)
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
75 host.bridge.addMethod("getAvatarFile", ".communication", in_sign='s', out_sign='s', method=self.getAvatarFile)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
76 host.bridge.addMethod("getProfileCache", ".communication", in_sign='s', out_sign='a{ss}', method=self.getProfileCache)
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
77
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
78 def update_cache(self, jid, name, value):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
79 """update cache value
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
80 - save value in memory in case of change
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
81 - send updatedValue signal if the value is new or updated
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
82 """
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
83 if not self.vcard_cache.has_key(jid.userhost()):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
84 self.vcard_cache[jid.userhost()] = {}
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
85
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
86 cache = self.vcard_cache[jid.userhost()]
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
87 old_value = cache[name] if cache.has_key(name) else None
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
88 if not old_value or value != old_value:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
89 cache[name] = value
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
90 self.host.memory.setPrivate("vcard_cache", self.vcard_cache)
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
91 self.host.bridge.updatedValue('profile_'+name, {'jid':jid.userhost(), name:value})
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
92
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
93 def get_cache(self, jid, name):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
94 """return cached value for jid
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
95 @param jid: target contact
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
96 @param name: name of the value ('nick' or 'avatar')
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
97 @return: wanted value or None"""
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
98 try:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
99 return self.vcard_cache[jid.userhost()][name]
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
100 except KeyError:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
101 return None
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
102
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
103
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
104 def connectionInitialized(self):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
105 self.xmlstream.addObserver(VCARD_UPDATE, self.update)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
106
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return [disco.DiscoFeature(NS_VCARD)]
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
109
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return []
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
112
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def save_photo(self, photo_xml):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """Parse a <PHOTO> elem and save the picture"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
115 for elem in photo_xml.elements():
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if elem.name == 'TYPE':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
117 info('Photo of type [%s] found' % str(elem))
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if elem.name == 'BINVAL':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
119 debug('Decoding binary')
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
120 decoded = b64decode(str(elem))
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
121 hash = sha1(decoded).hexdigest()
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
122 filename = self.avatar_path+'/'+hash
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if not os.path.exists(filename):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
124 with open(filename,'wb') as file:
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
125 file.write(decoded)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
126 debug("file saved to %s" % hash)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
127 else:
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
128 debug("file [%s] already in cache" % hash)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return hash
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
130
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @defer.deferredGenerator
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
132 def vCard2Dict(self, vcard, target):
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
133 """Convert a VCard to a dict, and save binaries"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
134 debug ("parsing vcard")
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
135 dictionary = {}
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
136 d = defer.Deferred()
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
137
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
138 for elem in vcard.elements():
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if elem.name == 'FN':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
140 dictionary['fullname'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
141 elif elem.name == 'NICKNAME':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
142 dictionary['nick'] = unicode(elem)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
143 self.update_cache(target, 'nick', dictionary['nick'])
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
144 elif elem.name == 'URL':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
145 dictionary['website'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
146 elif elem.name == 'EMAIL':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
147 dictionary['email'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
148 elif elem.name == 'BDAY':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
149 dictionary['birthday'] = unicode(elem)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
150 elif elem.name == 'PHOTO':
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
151 d2 = defer.waitForDeferred(
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
152 threads.deferToThread(self.save_photo, elem))
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
153 yield d2
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
154 dictionary["avatar"] = d2.getResult()
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
155 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
156 del dictionary['avatar']
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
157 else:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
158 self.update_cache(target, 'avatar', dictionary['avatar'])
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
159 else:
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
160 info ('FIXME: [%s] VCard tag is not managed yet' % elem.name)
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
161
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
162 yield dictionary
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
163
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
164 def vcard_ok(self, answer):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
165 """Called after the first get IQ"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
166 debug ("VCard found")
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
167
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if answer.firstChildElement().name == "vCard":
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
169 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]))
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
170 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data))
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
171 else:
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
172 error ("FIXME: vCard not found as first child element")
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be best
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
174
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
175 def vcard_err(self, failure):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
176 """Called when something is wrong with registration"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
177 error ("Can't find VCard of %s" % failure.value.stanza['from'])
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
178 self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be best
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
179
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def getProfile(self, target):
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """Ask server for VCard
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param target: jid from which we want the VCard
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
183 @result: id to retrieve the profile"""
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
184 to_jid = jid.JID(target)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
185 debug("Asking for %s's VCard" % to_jid.userhost())
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
186 reg_request=IQ(self.host.xmlstream,'get')
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
187 reg_request["from"]=self.host.me.full()
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
188 reg_request["to"] = to_jid.userhost()
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
189 query=reg_request.addElement('vCard', NS_VCARD)
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
190 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err)
42
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
191 return reg_request["id"]
874de3020e1c Initial VCard (XEP-0054) support + misc fixes
Goffi <goffi@goffi.org>
parents:
diff changeset
192
43
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
193 def getAvatarFile(self, hash):
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
194 """Give the full path of avatar from hash
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
195 @param hash: SHA1 hash
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
196 @return full_path
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
197 """
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
198 filename = self.avatar_path+'/'+hash
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
199 if not os.path.exists(filename):
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
200 error ("Asking for an uncached avatar [%s]" % hash)
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
201 return ""
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
202 return filename
8a438a6ff587 Wix: added avatar in profile
Goffi <goffi@goffi.org>
parents: 42
diff changeset
203
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
204 def getProfileCache(self, target):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
205 """Request for cached values of profile
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
206 return the cached nickname and avatar if exists, else get VCard
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
207 """
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
208 to_jid = jid.JID(target)
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
209 result = {}
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
210 nick = self.get_cache(to_jid, 'nick')
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
211 if nick:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
212 result['nick'] = nick
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
213 avatar = self.get_cache(to_jid, 'avatar')
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
214 if avatar:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
215 result['avatar'] = avatar
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
216 return result
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
217
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
218 def update(self, presence):
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
219 """Request for VCard's nickname
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
220 return the cached nickname if exists, else get VCard
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
221 """
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
222 to_jid = jid.JID(presence['from'])
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
223 x_elem = filter (lambda x:x.name == "x", presence.elements())[0] #We only want the "x" element
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
224 for elem in x_elem.elements():
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
225 if elem.name == 'photo':
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
226 hash = str(elem)
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
227 old_avatar = self.get_cache(to_jid, 'avatar')
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
228 if not old_avatar or old_avatar != hash:
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
229 debug('New avatar found, requesting vcard')
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
230 self.getProfile(to_jid.userhost())
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 44
diff changeset
231