X-Git-Url: https://code.delx.au/pymsnt/blobdiff_plain/d0d3bdaa1df2564c178541869f16f5837f15f1c6..cdc1223ee42d056ecf7b7334aa921f6c1583b56e:/src/legacy/glue.py diff --git a/src/legacy/glue.py b/src/legacy/glue.py index 4c84a12..2655360 100644 --- a/src/legacy/glue.py +++ b/src/legacy/glue.py @@ -2,11 +2,11 @@ # Licensed for distribution under the GPL version 2, check COPYING for details import os.path -import utils -from twisted.internet import task -from tlib.xmlw import Element -from tlib import msn +from twisted.internet import task, error +from twisted.words.xish.domish import Element + from debug import LogEvent, INFO, WARN, ERROR +from legacy import msn import disco import groupchat import ft @@ -17,9 +17,8 @@ import lang -name = "MSN Transport" # The name of the transport url = "http://msn-transport.jabberstudio.org" -version = "0.11-dev" # The transport version +version = "0.11.2-dev" # The transport version mangle = True # XDB '@' -> '%' mangling id = "msn" # The transport identifier @@ -37,6 +36,7 @@ defaultAvatar = avatar.AvatarCache().setAvatar(defaultAvatarData) def reloadConfig(): msn.MSNConnection.GETALLAVATARS = config.getAllAvatars + msn.setDebug(config._debugLevel >= 4) def isGroupJID(jid): """ Returns True if the JID passed is a valid groupchat JID (for MSN, does not contain '%') """ @@ -95,16 +95,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 + msn2jid_cache[msnid] = jid + jid2msn_cache[jid] = msnid + return msn2jid(msnid, withResource) # 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): @@ -117,6 +146,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): @@ -306,9 +336,9 @@ class LegacyConnection(msn.MSNConnection): global defaultJabberAvatarData if av: - msn.MSNConnection.changeAvatar(self, av.getImageData()) + msn.MSNConnection.changeAvatar(self, av.getImageData) else: - msn.MSNConnection.changeAvatar(self, defaultJabberAvatarData) + msn.MSNConnection.changeAvatar(self, lambda: defaultJabberAvatarData) def sendTypingNotifications(self): if not self.session: return @@ -349,8 +379,9 @@ class LegacyConnection(msn.MSNConnection): def connectionLost(self, reason): LogEvent(INFO, self.jabberID) - text = lang.get(self.session.lang).msnDisconnected % reason - self.session.sendMessage(to=self.jabberID, fro=config.jid, body=text) + if reason.type != error.ConnectionDone: + text = lang.get(self.session.lang).msnDisconnected % reason + self.session.sendMessage(to=self.jabberID, fro=config.jid, body=text) self.session.removeMe() # Tear down the session def multipleLogin(self): @@ -374,13 +405,13 @@ class LegacyConnection(msn.MSNConnection): def loggedIn(self): LogEvent(INFO, self.jabberID) - self.session.ready = True def listSynchronized(self): LogEvent(INFO, self.jabberID) self.session.sendPresence(to=self.jabberID, fro=config.jid) self.legacyList.syncJabberLegacyLists() self.listSynced = True + self.session.ready = True #self.legacyList.flushSubscriptionBuffer() def ourStatusChanged(self, statusCode, screenName, personal): @@ -394,6 +425,8 @@ class LegacyConnection(msn.MSNConnection): def gotMessage(self, remoteUser, text): LogEvent(INFO, self.jabberID) source = msn2jid(remoteUser, True) + if self.contactTyping.has_key(remoteUser): + del self.contactTyping[remoteUser] self.session.sendMessage(self.jabberID, fro=source, body=text, mtype="chat") self.session.pytrans.statistics.stats["MessageCount"] += 1 @@ -519,16 +552,20 @@ class LegacyList: def __init__(self, session): self.jabberID = session.jabberID self.session = session - self.subscriptionBuffer = [] def removeMe(self): - self.subscriptionBuffer = None self.session = None def addContact(self, jid): 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) + if msnContact and msnContact.lists & msn.REVERSE_LIST: + self.session.legacycon.contactAddedMe(userHandle) + self.authContact(jid) self.session.contactList.getContact(jid).contactGrantsAuth() def removeContact(self, jid): @@ -539,17 +576,18 @@ class LegacyList: def authContact(self, jid): LogEvent(INFO, self.jabberID) - jid = jid2msn(jid) - d = self.session.legacycon.remContact(msn.PENDING_LIST, jid) + userHandle = jid2msn(jid) + d = self.session.legacycon.remContact(msn.PENDING_LIST, userHandle) if d: - self.session.legacycon.addContact(msn.REVERSE_LIST, jid) - self.session.legacycon.remContact(msn.BLOCK_LIST, jid) - self.session.legacycon.addContact(msn.ALLOW_LIST, jid) + self.session.legacycon.addContact(msn.REVERSE_LIST, userHandle) + self.session.legacycon.remContact(msn.BLOCK_LIST, userHandle) + self.session.legacycon.addContact(msn.ALLOW_LIST, userHandle) def deauthContact(self, jid): LogEvent(INFO, self.jabberID) jid = jid2msn(jid) self.session.legacycon.remContact(msn.ALLOW_LIST, jid) + self.session.legacycon.remContact(msn.PENDING_LIST, jid) self.session.legacycon.addContact(msn.BLOCK_LIST, jid)