diff src/core/xmpp.py @ 346:ca3a041fed30

core: fixed several subscription scheme issues + removed most of profile_key default value in core.sat_main and core.xmmp (source of bugs) + contact update
author Goffi <goffi@goffi.org>
date Sat, 28 May 2011 20:28:21 +0200
parents 9eebdc655b8b
children ea3e1b82dd79
line wrap: on
line diff
--- a/src/core/xmpp.py	Sat May 28 20:24:02 2011 +0200
+++ b/src/core/xmpp.py	Sat May 28 20:28:21 2011 +0200
@@ -139,6 +139,22 @@
         #xmppim.RosterClientProtocol.addItem(self, to)
         #TODO: check IQ result"""
 
+    def updateItem(self, roster_item):
+        """
+        Update an item of the contact list.
+
+        @param roster_item: item to update
+        """
+        iq = compat.IQ(self.xmlstream, 'set')
+        iq.addElement((xmppim.NS_ROSTER, 'query'))
+        item = iq.query.addElement('item')
+        item['jid'] = roster_item.jid.userhost()
+        if roster_item.name:
+            item['name'] = roster_item.name
+        for group in roster_item.groups:
+            item.addElement('group', content=group)
+        return iq.send()
+    
     def onRosterSet(self, item):
         """Called when a new/update roster item is received"""
         #TODO: send a signal to frontends
@@ -206,25 +222,44 @@
             statuses[None] = statuses['default']
             del statuses['default']
         xmppim.PresenceClientProtocol.available(self, entity, show, statuses, priority)
-    
+   
+    def subscribed(self, entity):
+        xmppim.PresenceClientProtocol.subscribed(self, entity)
+        self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
+        contact = self.host.memory.getContact(entity, self.parent.profile) 
+        if not contact or contact[0]['to'] == 'False': #we automatically subscribe to 'to' presence
+            debug(_('sending automatic "from" subscription request'))
+            self.subscribe(entity)
+
+    def unsubscribed(self, entity):
+        xmppim.PresenceClientProtocol.unsubscribed(self, entity)
+        self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
+
     def subscribedReceived(self, entity):
         debug (_("subscription approved for [%s]") % entity.userhost())
-        self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
         self.host.bridge.subscribe('subscribed', entity.userhost(), self.parent.profile)
 
     def unsubscribedReceived(self, entity):
         debug (_("unsubscription confirmed for [%s]") % entity.userhost())
-        self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
         self.host.bridge.subscribe('unsubscribed', entity.userhost(), self.parent.profile)
 
     def subscribeReceived(self, entity):
-        debug (_("subscription request for [%s]") % entity.userhost())
-        self.host.memory.addWaitingSub('subscribe', entity.userhost(), self.parent.profile)
-        self.host.bridge.subscribe('subscribe', entity.userhost(), self.parent.profile)
+        debug (_("subscription request from [%s]") % entity.userhost())
+        contact = self.host.memory.getContact(entity, self.parent.profile) 
+        if contact and contact[0]['to'] == 'True':
+            #We automatically accept subscription if we are already subscribed to contact presence
+            debug(_('sending automatic subscription acceptance'))
+            self.subscribed(entity)
+        else:
+            self.host.memory.addWaitingSub('subscribe', entity.userhost(), self.parent.profile)
+            self.host.bridge.subscribe('subscribe', entity.userhost(), self.parent.profile)
 
     def unsubscribeReceived(self, entity):
         debug (_("unsubscription asked for [%s]") % entity.userhost())
-        self.host.memory.addWaitingSub('unsubscribe', entity.userhost(), self.parent.profile)
+        contact = self.host.memory.getContact(entity, self.parent.profile) 
+        if contact and contact[0]['from'] == 'True': #we automatically remove contact
+            debug(_('automatic contact deletion'))
+            self.host.delContact(entity.userhost(), self.parent.profile)
         self.host.bridge.subscribe('unsubscribe', entity.userhost(), self.parent.profile)
 
 class SatDiscoProtocol(disco.DiscoClientProtocol):