comparison src/core/sat_main.py @ 1262:f8a8434dbac7 frontends_multi_profiles

core: improved roster management + misc: - updated methods to no use anymore methods deprecated in Wokkel - use of full jid when it make sense instead of bare jid - getContacts, updateContact and delContact are now asynchronous
author Goffi <goffi@goffi.org>
date Wed, 10 Dec 2014 18:32:33 +0100
parents 9c17bd37e6e5
children faa1129559b8
comparison
equal deleted inserted replaced
1261:93bce9e4c9c8 1262:f8a8434dbac7
329 if disconnected_cb: 329 if disconnected_cb:
330 disconnected_cb(profile) 330 disconnected_cb(profile)
331 331
332 def getContacts(self, profile_key): 332 def getContacts(self, profile_key):
333 client = self.getClient(profile_key) 333 client = self.getClient(profile_key)
334 ret = [] 334 def got_roster(dummy):
335 for item in client.roster.getItems(): # we get all items for client's roster 335 ret = []
336 # and convert them to expected format 336 for item in client.roster.getItems(): # we get all items for client's roster
337 attr = client.roster.getAttributes(item) 337 # and convert them to expected format
338 ret.append([item.jid.userhost(), attr, item.groups]) 338 attr = client.roster.getAttributes(item)
339 return ret 339 ret.append([item.jid.userhost(), attr, item.groups])
340 return ret
341
342 return client.roster.got_roster.addCallback(got_roster)
340 343
341 def getContactsFromGroup(self, group, profile_key): 344 def getContactsFromGroup(self, group, profile_key):
342 client = self.getClient(profile_key) 345 client = self.getClient(profile_key)
343 return [jid_.full() for jid_ in client.roster.getJidsFromGroup(group)] 346 return [jid_.full() for jid_ in client.roster.getJidsFromGroup(group)]
344 347
454 return self.sendMessage(to_jid, msg, subject, mess_type, {unicode(key): unicode(value) for key, value in extra.items()}, profile_key=profile_key) 457 return self.sendMessage(to_jid, msg, subject, mess_type, {unicode(key): unicode(value) for key, value in extra.items()}, profile_key=profile_key)
455 458
456 def sendMessage(self, to_jid, msg, subject=None, mess_type='auto', extra={}, no_trigger=False, profile_key=C.PROF_KEY_NONE): 459 def sendMessage(self, to_jid, msg, subject=None, mess_type='auto', extra={}, no_trigger=False, profile_key=C.PROF_KEY_NONE):
457 #FIXME: check validity of recipient 460 #FIXME: check validity of recipient
458 profile = self.memory.getProfileName(profile_key) 461 profile = self.memory.getProfileName(profile_key)
459 assert(profile) 462 assert profile
460 client = self.profiles[profile] 463 client = self.profiles[profile]
461 if extra is None: 464 if extra is None:
462 extra = {} 465 extra = {}
463 mess_data = { # we put data in a dict, so trigger methods can change them 466 mess_data = { # we put data in a dict, so trigger methods can change them
464 "to": to_jid, 467 "to": to_jid,
570 def setPresence(self, to_jid=None, show="", statuses=None, profile_key=C.PROF_KEY_NONE): 573 def setPresence(self, to_jid=None, show="", statuses=None, profile_key=C.PROF_KEY_NONE):
571 """Send our presence information""" 574 """Send our presence information"""
572 if statuses is None: 575 if statuses is None:
573 statuses = {} 576 statuses = {}
574 profile = self.memory.getProfileName(profile_key) 577 profile = self.memory.getProfileName(profile_key)
575 assert(profile) 578 assert profile
576 priority = int(self.memory.getParamA("Priority", "Connection", profile_key=profile)) 579 priority = int(self.memory.getParamA("Priority", "Connection", profile_key=profile))
577 self.profiles[profile].presence.available(to_jid, show, statuses, priority) 580 self.profiles[profile].presence.available(to_jid, show, statuses, priority)
578 #XXX: FIXME: temporary fix to work around openfire 3.7.0 bug (presence is not broadcasted to generating resource) 581 #XXX: FIXME: temporary fix to work around openfire 3.7.0 bug (presence is not broadcasted to generating resource)
579 if '' in statuses: 582 if '' in statuses:
580 statuses['default'] = statuses[''] 583 statuses['default'] = statuses['']
586 """Called to manage subscription 589 """Called to manage subscription
587 @param subs_type: subsciption type (cf RFC 3921) 590 @param subs_type: subsciption type (cf RFC 3921)
588 @param raw_jid: unicode entity's jid 591 @param raw_jid: unicode entity's jid
589 @param profile_key: profile""" 592 @param profile_key: profile"""
590 profile = self.memory.getProfileName(profile_key) 593 profile = self.memory.getProfileName(profile_key)
591 assert(profile) 594 assert profile
592 to_jid = jid.JID(raw_jid) 595 to_jid = jid.JID(raw_jid)
593 log.debug(_('subsciption request [%(subs_type)s] for %(jid)s') % {'subs_type': subs_type, 'jid': to_jid.full()}) 596 log.debug(_('subsciption request [%(subs_type)s] for %(jid)s') % {'subs_type': subs_type, 'jid': to_jid.full()})
594 if subs_type == "subscribe": 597 if subs_type == "subscribe":
595 self.profiles[profile].presence.subscribe(to_jid) 598 self.profiles[profile].presence.subscribe(to_jid)
596 elif subs_type == "subscribed": 599 elif subs_type == "subscribed":
604 return self.addContact(jid.JID(to_jid_s), profile_key) 607 return self.addContact(jid.JID(to_jid_s), profile_key)
605 608
606 def addContact(self, to_jid, profile_key): 609 def addContact(self, to_jid, profile_key):
607 """Add a contact in roster list""" 610 """Add a contact in roster list"""
608 profile = self.memory.getProfileName(profile_key) 611 profile = self.memory.getProfileName(profile_key)
609 assert(profile) 612 assert profile
610 #self.profiles[profile].roster.addItem(to_jid) #XXX: disabled (cf http://wokkel.ik.nu/ticket/56)) 613 # presence is sufficient, as a roster push will be sent according to RFC 6121 ยง3.1.2
611 self.profiles[profile].presence.subscribe(to_jid) 614 self.profiles[profile].presence.subscribe(to_jid)
612 615
613 def _updateContact(self, to_jid_s, name, groups, profile_key): 616 def _updateContact(self, to_jid_s, name, groups, profile_key):
614 return self.updateContact(jid.JID(to_jid_s), name, groups, profile_key) 617 return self.updateContact(jid.JID(to_jid_s), name, groups, profile_key)
615 618
616 def updateContact(self, to_jid, name, groups, profile_key): 619 def updateContact(self, to_jid, name, groups, profile_key):
617 """update a contact in roster list""" 620 """update a contact in roster list"""
618 profile = self.memory.getProfileName(profile_key) 621 profile = self.memory.getProfileName(profile_key)
619 assert(profile) 622 assert profile
620 groups = set(groups) 623 groups = set(groups)
621 roster_item = RosterItem(to_jid) 624 roster_item = RosterItem(to_jid)
622 roster_item.name = name or None 625 roster_item.name = name or None
623 roster_item.groups = set(groups) 626 roster_item.groups = set(groups)
624 self.profiles[profile].roster.updateItem(roster_item) 627 return self.profiles[profile].roster.setItem(roster_item)
625 628
626 def _delContact(self, to_jid_s, profile_key): 629 def _delContact(self, to_jid_s, profile_key):
627 return self.delContact(jid.JID(to_jid_s), profile_key) 630 return self.delContact(jid.JID(to_jid_s), profile_key)
628 631
629 def delContact(self, to_jid, profile_key): 632 def delContact(self, to_jid, profile_key):
630 """Remove contact from roster list""" 633 """Remove contact from roster list"""
631 profile = self.memory.getProfileName(profile_key) 634 profile = self.memory.getProfileName(profile_key)
632 assert(profile) 635 assert profile
633 self.profiles[profile].roster.removeItem(to_jid) 636 d1 = self.profiles[profile].roster.removeItem(to_jid)
634 self.profiles[profile].presence.unsubscribe(to_jid) 637 d2 = self.profiles[profile].presence.unsubscribe(to_jid)
635 638 d_list = defer.DefferedList([d1, d2])
639 def check_result(list_result):
640 for success, value in list_result:
641 if not success:
642 raise value
643 d_list.addCallback(check_result)
644 return d_list
636 645
637 ## Discovery ## 646 ## Discovery ##
638 # discovery methods are shortcuts to self.memory.disco 647 # discovery methods are shortcuts to self.memory.disco
639 # the main difference with client.disco is that self.memory.disco manage cache 648 # the main difference with client.disco is that self.memory.disco manage cache
640 649
768 @param callback_id: id of the action (callback) to launch 777 @param callback_id: id of the action (callback) to launch
769 @param data: optional data 778 @param data: optional data
770 @profile_key: %(doc_profile_key)s 779 @profile_key: %(doc_profile_key)s
771 @return: a deferred which fire a dict where key can be: 780 @return: a deferred which fire a dict where key can be:
772 - xmlui: a XMLUI need to be displayed 781 - xmlui: a XMLUI need to be displayed
782 - validated: if present, can be used to launch a callback, it can have the values
783 - C.BOOL_TRUE
784 - C.BOOL_FALSE
773 """ 785 """
774 profile = self.memory.getProfileName(profile_key) 786 profile = self.memory.getProfileName(profile_key)
775 if not profile: 787 if not profile:
776 raise exceptions.ProfileUnknownError(_('trying to launch action with a non-existant profile')) 788 raise exceptions.ProfileUnknownError(_('trying to launch action with a non-existant profile'))
777 789