comparison frontends/src/quick_frontend/quick_app.py @ 510:886754295efe

quick frontend, primitivus, wix: MUC private messages management /!\ not fully finished, backend part is not done yet /!\ - as resources are discarded to manage chat windows lists, a pretty dirty hack is done to work around this: full jid is escaped using a prefix (it becomes invalid and resource is preserved). - new quick_utils module, with helper methods. escapePrivate and unescapePrivate implementations - MUC private messages are not managed in Wix yet
author Goffi <goffi@goffi.org>
date Thu, 11 Oct 2012 00:48:35 +0200
parents f98bef71a918
children 8ee9113d307b
comparison
equal deleted inserted replaced
509:64ff046dc201 510:886754295efe
20 """ 20 """
21 21
22 from logging import debug, info, warning, error 22 from logging import debug, info, warning, error
23 from sat.tools.jid import JID 23 from sat.tools.jid import JID
24 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService 24 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
25 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
25 from optparse import OptionParser 26 from optparse import OptionParser
27
28 import sat_frontends.quick_frontend.constants
26 29
27 import gettext 30 import gettext
28 gettext.install('sat_frontend', "../i18n", unicode=True) 31 gettext.install('sat_frontend', "../i18n", unicode=True)
29 32
30 class QuickApp(): 33 class QuickApp():
44 sys.exit(1) 47 sys.exit(1)
45 self.bridge.register("connected", self.connected) 48 self.bridge.register("connected", self.connected)
46 self.bridge.register("disconnected", self.disconnected) 49 self.bridge.register("disconnected", self.disconnected)
47 self.bridge.register("connectionError", self.connectionError) 50 self.bridge.register("connectionError", self.connectionError)
48 self.bridge.register("newContact", self.newContact) 51 self.bridge.register("newContact", self.newContact)
49 self.bridge.register("newMessage", self.newMessage) 52 self.bridge.register("newMessage", self._newMessage)
50 self.bridge.register("newAlert", self.newAlert) 53 self.bridge.register("newAlert", self.newAlert)
51 self.bridge.register("presenceUpdate", self.presenceUpdate) 54 self.bridge.register("presenceUpdate", self.presenceUpdate)
52 self.bridge.register("subscribe", self.subscribe) 55 self.bridge.register("subscribe", self.subscribe)
53 self.bridge.register("paramUpdate", self.paramUpdate) 56 self.bridge.register("paramUpdate", self.paramUpdate)
54 self.bridge.register("contactDeleted", self.contactDeleted) 57 self.bridge.register("contactDeleted", self.contactDeleted)
228 def newContact(self, JabberId, attributes, groups, profile): 231 def newContact(self, JabberId, attributes, groups, profile):
229 if not self.check_profile(profile): 232 if not self.check_profile(profile):
230 return 233 return
231 entity=JID(JabberId) 234 entity=JID(JabberId)
232 _groups = list(groups) 235 _groups = list(groups)
233 self.contact_list._replace(entity, _groups, attributes) 236 self.contact_list.replace(entity, _groups, attributes)
234 237
235 def newMessage(self, from_jid, msg, type, to_jid, profile): 238 def _newMessage(self, from_jid_s, msg, _type, to_jid_s, profile):
236 if not self.check_profile(profile): 239 """newMessage premanagement: a dirty hack to manage private messages
237 return 240 if a private MUC message is detected, from_jid or to_jid is prefixed and resource is escaped"""
238 sender=JID(from_jid) 241 if not self.check_profile(profile):
239 addr=JID(to_jid) 242 return
240 win = addr if sender.short == self.profiles[profile]['whoami'].short else sender 243 from_jid = JID(from_jid_s)
244 to_jid = JID(to_jid_s)
245
246 from_me = from_jid.short == self.profiles[profile]['whoami'].short
247 win = to_jid if from_me else from_jid
248
249 if _type != "groupchat" and self.contact_list.getSpecial(win) == "MUC":
250 #we have a private message in a MUC room
251 #XXX: normaly we use bare jid as key, here we need the full jid
252 # so we cheat by replacing the "/" before the resource by
253 # a "@", so the jid is invalid,
254 new_jid = escapePrivate(win)
255 if from_me:
256 to_jid = new_jid
257 else:
258 from_jid = new_jid
259 if new_jid not in self.contact_list:
260 self.contact_list.add(new_jid)
261
262 self.newMessage(from_jid, to_jid, msg, _type, profile)
263
264 def newMessage(self, from_jid, to_jid, msg, _type, profile):
265 from_me = from_jid.short == self.profiles[profile]['whoami'].short
266 win = to_jid if from_me else from_jid
267
241 self.current_action_ids = set() 268 self.current_action_ids = set()
242 self.current_action_ids_cb = {} 269 self.current_action_ids_cb = {}
243 self.chat_wins[win.short].printMessage(sender, msg, profile) 270
271 self.chat_wins[win.short].printMessage(from_jid, msg, profile)
272
273 def sendMessage(self, to_jid, message, subject='', mess_type="auto", profile_key="@DEFAULT@"):
274 if to_jid.startswith(const_PRIVATE_PREFIX):
275 to_jid = unescapePrivate(to_jid)
276 mess_type = "chat"
277 self.bridge.sendMessage(to_jid, message, subject, mess_type, profile_key)
244 278
245 def newAlert(self, msg, title, alert_type, profile): 279 def newAlert(self, msg, title, alert_type, profile):
246 if not self.check_profile(profile): 280 if not self.check_profile(profile):
247 return 281 return
248 assert alert_type in ['INFO','ERROR'] 282 assert alert_type in ['INFO','ERROR']
290 self.chat_wins[room_jid].setType("group") 324 self.chat_wins[room_jid].setType("group")
291 self.chat_wins[room_jid].id = room_jid 325 self.chat_wins[room_jid].id = room_jid
292 self.chat_wins[room_jid].setPresents(list(set([user_nick]+room_nicks))) 326 self.chat_wins[room_jid].setPresents(list(set([user_nick]+room_nicks)))
293 self.contact_list.setSpecial(JID(room_jid), "MUC") 327 self.contact_list.setSpecial(JID(room_jid), "MUC")
294 328
295 def roomLeft(self, room_jid, profile): 329 def roomLeft(self, room_jid_s, profile):
296 """Called when a MUC room is left""" 330 """Called when a MUC room is left"""
297 if not self.check_profile(profile): 331 if not self.check_profile(profile):
298 return 332 return
299 debug (_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid':room_jid, 'profile': profile}) 333 debug (_("Room [%(room_jid)s] left by %(profile)s") % {'room_jid':room_jid_s, 'profile': profile})
300 del self.chat_wins[room_jid] 334 del self.chat_wins[room_jid_s]
301 self.contact_list.remove(room_jid) 335 self.contact_list.remove(JID(room_jid_s))
302 336
303 def roomUserJoined(self, room_jid, user_nick, user_data, profile): 337 def roomUserJoined(self, room_jid, user_nick, user_data, profile):
304 """Called when an user joined a MUC room""" 338 """Called when an user joined a MUC room"""
305 if not self.check_profile(profile): 339 if not self.check_profile(profile):
306 return 340 return
502 return 536 return
503 jid = JID(jid_str) 537 jid = JID(jid_str)
504 if key == "nick": 538 if key == "nick":
505 if jid in self.contact_list: 539 if jid in self.contact_list:
506 self.contact_list.setCache(jid, 'nick', value) 540 self.contact_list.setCache(jid, 'nick', value)
507 self.contact_list._replace(jid) 541 self.contact_list.replace(jid)
508 elif key == "avatar": 542 elif key == "avatar":
509 if jid in self.contact_list: 543 if jid in self.contact_list:
510 filename = self.bridge.getAvatarFile(value) 544 filename = self.bridge.getAvatarFile(value)
511 self.contact_list.setCache(jid, 'avatar', filename) 545 self.contact_list.setCache(jid, 'avatar', filename)
512 self.contact_list._replace(jid) 546 self.contact_list.replace(jid)
513 547
514 def askConfirmation(self, type, id, data): 548 def askConfirmation(self, type, id, data):
515 raise NotImplementedError 549 raise NotImplementedError
516 550
517 def actionResult(self, type, id, data): 551 def actionResult(self, type, id, data):