comparison src/plugins/plugin_xep_0054.py @ 372:f964dcec1611

core: plugins refactored according to bridge + updatedValue now use profile
author Goffi <goffi@goffi.org>
date Wed, 06 Jul 2011 01:06:18 +0200
parents efbfccfed623
children c243f4cb2ad9
comparison
equal deleted inserted replaced
371:3ea41a199b36 372:f964dcec1611
69 self.host = host 69 self.host = host
70 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH) 70 self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
71 self.vcard_cache = host.memory.getPrivate("vcard_cache") or {} #used to store nicknames and avatar, key = jid 71 self.vcard_cache = host.memory.getPrivate("vcard_cache") or {} #used to store nicknames and avatar, key = jid
72 if not os.path.exists(self.avatar_path): 72 if not os.path.exists(self.avatar_path):
73 os.makedirs(self.avatar_path) 73 os.makedirs(self.avatar_path)
74 host.bridge.addMethod("getCard", ".communication", in_sign='ss', out_sign='s', method=self.getCard) 74 host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
75 host.bridge.addMethod("getAvatarFile", ".communication", in_sign='s', out_sign='s', method=self.getAvatarFile) 75 host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile)
76 host.bridge.addMethod("getCardCache", ".communication", in_sign='s', out_sign='a{ss}', method=self.getCardCache) 76 host.bridge.addMethod("getCardCache", ".plugin", in_sign='s', out_sign='a{ss}', method=self.getCardCache)
77 77
78 def getHandler(self, profile): 78 def getHandler(self, profile):
79 return XEP_0054_handler(self) 79 return XEP_0054_handler(self)
80 80
81 def update_cache(self, jid, name, value): 81 def update_cache(self, jid, name, value, profile):
82 """update cache value 82 """update cache value
83 - save value in memory in case of change 83 - save value in memory in case of change
84 - send updatedValue signal if the value is new or updated 84 - send updatedValue signal if the value is new or updated
85 @param jid: jid of the owner of the vcard
86 @param name: name of the item which changed
87 @param value: new value of the item
88 @param profile: profile which received the update
85 """ 89 """
86 if not self.vcard_cache.has_key(jid.userhost()): 90 if not self.vcard_cache.has_key(jid.userhost()):
87 self.vcard_cache[jid.userhost()] = {} 91 self.vcard_cache[jid.userhost()] = {}
88 92
89 cache = self.vcard_cache[jid.userhost()] 93 cache = self.vcard_cache[jid.userhost()]
90 old_value = cache[name] if cache.has_key(name) else None 94 old_value = cache[name] if cache.has_key(name) else None
91 if not old_value or value != old_value: 95 if not old_value or value != old_value:
92 cache[name] = value 96 cache[name] = value
93 self.host.memory.setPrivate("vcard_cache", self.vcard_cache) 97 self.host.memory.setPrivate("vcard_cache", self.vcard_cache)
94 self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}) 98 self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}, profile)
95 99
96 def get_cache(self, jid, name): 100 def get_cache(self, jid, name):
97 """return cached value for jid 101 """return cached value for jid
98 @param jid: target contact 102 @param jid: target contact
99 @param name: name of the value ('nick' or 'avatar') 103 @param name: name of the value ('nick' or 'avatar')
121 else: 125 else:
122 debug(_("file [%s] already in cache") % hash) 126 debug(_("file [%s] already in cache") % hash)
123 return hash 127 return hash
124 128
125 @defer.deferredGenerator 129 @defer.deferredGenerator
126 def vCard2Dict(self, vcard, target): 130 def vCard2Dict(self, vcard, target, profile):
127 """Convert a VCard to a dict, and save binaries""" 131 """Convert a VCard to a dict, and save binaries"""
128 debug (_("parsing vcard")) 132 debug (_("parsing vcard"))
129 dictionary = {} 133 dictionary = {}
130 d = defer.Deferred() 134 d = defer.Deferred()
131 135
132 for elem in vcard.elements(): 136 for elem in vcard.elements():
133 if elem.name == 'FN': 137 if elem.name == 'FN':
134 dictionary['fullname'] = unicode(elem) 138 dictionary['fullname'] = unicode(elem)
135 elif elem.name == 'NICKNAME': 139 elif elem.name == 'NICKNAME':
136 dictionary['nick'] = unicode(elem) 140 dictionary['nick'] = unicode(elem)
137 self.update_cache(target, 'nick', dictionary['nick']) 141 self.update_cache(target, 'nick', dictionary['nick'], profile)
138 elif elem.name == 'URL': 142 elif elem.name == 'URL':
139 dictionary['website'] = unicode(elem) 143 dictionary['website'] = unicode(elem)
140 elif elem.name == 'EMAIL': 144 elif elem.name == 'EMAIL':
141 dictionary['email'] = unicode(elem) 145 dictionary['email'] = unicode(elem)
142 elif elem.name == 'BDAY': 146 elif elem.name == 'BDAY':
147 yield d2 151 yield d2
148 dictionary["avatar"] = d2.getResult() 152 dictionary["avatar"] = d2.getResult()
149 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem 153 if not dictionary["avatar"]: #can happen in case of e.g. empty photo elem
150 del dictionary['avatar'] 154 del dictionary['avatar']
151 else: 155 else:
152 self.update_cache(target, 'avatar', dictionary['avatar']) 156 self.update_cache(target, 'avatar', dictionary['avatar'], profile)
153 else: 157 else:
154 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name) 158 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
155 159
156 yield dictionary 160 yield dictionary
157 161
158 def vcard_ok(self, answer): 162 def vcard_ok(self, answer, profile):
159 """Called after the first get IQ""" 163 """Called after the first get IQ"""
160 debug (_("VCard found")) 164 debug (_("VCard found"))
161 165
162 if answer.firstChildElement().name == "vCard": 166 if answer.firstChildElement().name == "vCard":
163 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"])) 167 d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile)
164 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data)) 168 d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data))
165 else: 169 else:
166 error (_("FIXME: vCard not found as first child element")) 170 error (_("FIXME: vCard not found as first child element"))
167 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be best 171 self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be best
168 172
177 @result: id to retrieve the profile""" 181 @result: id to retrieve the profile"""
178 current_jid, xmlstream = self.host.getJidNStream(profile_key) 182 current_jid, xmlstream = self.host.getJidNStream(profile_key)
179 if not xmlstream: 183 if not xmlstream:
180 error (_('Asking vcard for an non-existant or not connected profile')) 184 error (_('Asking vcard for an non-existant or not connected profile'))
181 return "" 185 return ""
186 profile = self.host.memory.getProfileName(profile_key)
182 to_jid = jid.JID(target) 187 to_jid = jid.JID(target)
183 debug(_("Asking for %s's VCard") % to_jid.userhost()) 188 debug(_("Asking for %s's VCard") % to_jid.userhost())
184 reg_request=IQ(xmlstream,'get') 189 reg_request=IQ(xmlstream,'get')
185 reg_request["from"]=current_jid.full() 190 reg_request["from"]=current_jid.full()
186 reg_request["to"] = to_jid.userhost() 191 reg_request["to"] = to_jid.userhost()
187 query=reg_request.addElement('vCard', NS_VCARD) 192 query=reg_request.addElement('vCard', NS_VCARD)
188 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err) 193 reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, [profile])
189 return reg_request["id"] 194 return reg_request["id"]
190 195
191 def getAvatarFile(self, hash): 196 def getAvatarFile(self, hash):
192 """Give the full path of avatar from hash 197 """Give the full path of avatar from hash
193 @param hash: SHA1 hash 198 @param hash: SHA1 hash