]> code.delx.au - pymsnt/blobdiff - src/legacy/glue.py
Make adding/removing contacts symmetric.
[pymsnt] / src / legacy / glue.py
index 176932f247e1a6f743bd5e0d1d48cfaf9cfa9d81..866f3da69f6832deb92c404aa177f4fdf2832a50 100644 (file)
@@ -94,16 +94,45 @@ def updateStats(statistics):
        #stats["FailedAvatarCount"] = msnp2p.MSNP2P_Avatar.ERROR_COUNT
 
 
+msn2jid_cache = {}
 def msn2jid(msnid, withResource):
        """ Converts a MSN passport into a JID representation to be used with the transport """
-       return msnid.replace('@', '%') + "@" + config.jid + (withResource and "/msn" or "")
+       global msn2jid_cache
+       global jid2msn_cache
+
+       if msn2jid_cache.has_key(msnid):
+               jid = msn2jid_cache[msnid]
+               if withResource:
+                       jid += "/msn"
+               return jid
+       else:
+               if msnid.startswith("tel:+"):
+                       msnid = msnid.replace("tel:+", "") + "%tel"
+               jid = msnid.replace('@', '%') + "@" + config.jid + (withResource and "/msn" or "")
+               msn2jid_cache[msnid] = jid
+               jid2msn_cache[jid] = msnid
+               return jid
 
 # Marks this as the function to be used in jabber:iq:gateway (Service ID Translation)
-translateAccount = lambda a: msn2jid(a, False)
+def translateAccount(msnid):
+       return msn2jid(msnid, False)
 
+jid2msn_cache = {}
 def jid2msn(jid):
        """ Converts a JID representation of a MSN passport into the original MSN passport """
-       return unicode(jid[:jid.find('@')].replace('%', '@')).split("/")[0]
+       global jid2msn_cache
+       global msn2jid_cache
+
+       if jid2msn_cache.has_key(jid):
+               msnid = jid2msn_cache[jid]
+               return msnid
+       else:
+               if jid.find("%tel@") > 0:
+                       jid = "tel:+" + jid.replace("%tel@", "@")
+               msnid = unicode(jid[:jid.find('@')].replace('%', '@')).split("/")[0]
+               jid2msn_cache[jid] = msnid
+               msn2jid_cache[msnid] = jid
+               return msnid
 
 
 def presence2state(show, ptype): 
@@ -116,6 +145,7 @@ def presence2state(show, ptype):
                return msn.STATUS_BUSY
        elif show == "away" or show == "xa":
                return msn.STATUS_AWAY
+       return msn.STATUS_ONLINE
 
 
 def state2presence(state):
@@ -530,6 +560,12 @@ class LegacyList:
                LogEvent(INFO, self.jabberID)
                userHandle = jid2msn(jid)
                self.session.legacycon.addContact(msn.FORWARD_LIST, userHandle)
+
+               # Handle adding a contact that has previously been removed
+               msnContact = self.session.legacycon.getContacts().getContact(userHandle)
+               self.session.legacycon.remContact(msn.BLOCK_LIST, jid)
+               if msnContact.lists & msn.REVERSE_LIST:
+                       self.session.legacycon.contactAddedMe(userHandle)
                self.session.contactList.getContact(jid).contactGrantsAuth()
        
        def removeContact(self, jid):