]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/msnw.py
Handle disconnects better.
[pymsnt] / src / tlib / msn / msnw.py
index 7b2c22cd7af1eed67ab0211c114255356682e8a0..87ed0442623ce420d1881590cf7059ed98383447 100644 (file)
@@ -51,6 +51,7 @@ class MSNConnection:
        def _getNotificationReferral(self):
                def timeout():
                        self.timeout = None
+                       dispatchFactory.d = None
                        if not d.called:
                                d.errback(Exception("Timeout"))
                                self.logOut() # Clean up everything
@@ -193,15 +194,17 @@ class MSNConnection:
                if not statusCode: statusCode = msn.STATUS_ONLINE
                if not personal: personal = ""
                if self.notificationClient:
-                       changeCount = [0] # Hack
+                       changeCount = [0] # Hack for Python's limited scope :(
                        def cb(ignored=None):
                                changeCount[0] += 1
                                if changeCount[0] == 3:
                                        self.ourStatusChanged(statusCode, screenName, personal)
+                       def errcb(ignored=None):
+                               pass # FIXME, should we do something here?
                        LogEvent(INFO, self.ident)
-                       self.notificationClient.changeStatus(statusCode.encode("utf-8")).addCallback(cb)
-                       self.notificationClient.changeScreenName(screenName.encode("utf-8")).addCallback(cb)
-                       self.notificationClient.changePersonalMessage(personal.encode("utf-8")).addCallback(cb)
+                       self.notificationClient.changeStatus(statusCode.encode("utf-8")).addCallbacks(cb, errcb)
+                       self.notificationClient.changeScreenName(screenName.encode("utf-8")).addCallbacks(cb, errcb)
+                       self.notificationClient.changePersonalMessage(personal.encode("utf-8")).addCallbacks(cb, errcb)
                # Remember the saved status
                self.savedEvents.statusCode = statusCode
                self.savedEvents.screenName = screenName
@@ -228,9 +231,11 @@ class MSNConnection:
                        self.notificationClient.logOut()
                for c in self.connectors:
                        c.disconnect()
+               self.connectors = []
                if self.notificationFactory:
+                       self.notificationFactory.stopTrying()
                        self.notificationFactory.msncon = None
-               self.connectors = []
+                       self.notificationFactory = None
                for sbs in self.switchboardSessions.values():
                        if hasattr(sbs, "transport") and sbs.transport:
                                sbs.transport.loseConnection()
@@ -355,8 +360,11 @@ class SavedEvents:
 
 class DispatchClient(msn.DispatchClient):
        def gotNotificationReferral(self, host, port):
-               if self.factory.d.called: return # Too slow! We've already timed out
-               self.factory.d.callback((host, port))
+               d = self.factory.d
+               self.factory.d = None
+               if not d or d.called:
+                       return # Too slow! We've already timed out
+               d.callback((host, port))
 
 
 class NotificationClient(msn.NotificationClient):
@@ -381,9 +389,13 @@ class NotificationClient(msn.NotificationClient):
                # are now redundant
                self.loginFailure = self.doDisconnect
                self.loggedIn = self.doDisconnect
-               self.connectionLost = self.doDisconnect
+               self.connectionLost = lambda reason: msn.NotificationClient.connectionLost(self, reason)
        
        def connectionLost(self, reason):
+               if not self.factory.msncon:
+                       # If MSNConnection.logOut is called before _notificationClientReady
+                       return
+
                def wait():
                        LogEvent(INFO, self.factory.msncon.ident)
                        msn.NotificationClient.connectionLost(self, reason)
@@ -443,9 +455,8 @@ class NotificationClient(msn.NotificationClient):
                sb = self.factory.msncon.switchboardSessions.get(userHandle)
                if sb and sb.transport:
                        sb.transport.loseConnection()
-               else:
-                       sb = OneSwitchboardSession(self.factory.msncon, userHandle)
-                       self.factory.msncon.switchboardSessions[userHandle] = sb
+               sb = OneSwitchboardSession(self.factory.msncon, userHandle)
+               self.factory.msncon.switchboardSessions[userHandle] = sb
                sb.connectReply(host, port, key, sessionID)
        
        def multipleLogin(self):
@@ -556,7 +567,7 @@ class SwitchboardSessionBase(msn.SwitchboardClient):
 
                                d = msn.SwitchboardClient.sendMessage(self, message)
                                if not noerror:
-                                       d.addCallback(failedMessage)
+                                       d.addCallbacks(failedMessage, failedMessage)
 
                        else:
                                chunks = int(math.ceil(len(text) / float(MSNConnection.MAXMESSAGESIZE)))
@@ -576,7 +587,7 @@ class SwitchboardSessionBase(msn.SwitchboardClient):
 
                                        d = msn.SwitchboardClient.sendMessage(self, message)
                                        if not noerror:
-                                               d.addCallback(failedMessage)
+                                               d.addCallbacks(failedMessage, failedMessage)
 
                                        chunk += 1
 
@@ -645,6 +656,12 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                for message, noerror in self.messageBuffer:
                        if not noerror:
                                self.failedMessage(message)
+               self.messageBuffer = []
+
+               if self.msncon and self.msncon.switchboardSessions.has_key(self.remoteUser):
+                       # Unexpected disconnection. Must remove us from msncon
+                       self.msncon.switchboardSessions.pop(self.remoteUser)
+
                SwitchboardSessionBase.connectionLost(self, reason)
 
        def _ready(self):
@@ -674,12 +691,12 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                LogEvent(INFO, self.ident)
                if not self.reply:
                        def failCB(arg=None):
+                               self.timeout = None
+                               self.transport.loseConnection()
                                if not (self.msncon and self.msncon.switchboardSessions.has_key(self.remoteUser)):
                                        return
                                LogEvent(INFO, self.ident, "User has not joined after 30 seconds.")
                                del self.msncon.switchboardSessions[self.remoteUser]
-                               self.timeout = None
-                               self.transport.loseConnection()
                        d = self.inviteUser(self.remoteUser)
                        d.addErrback(failCB)
                        self.timeout = reactor.callLater(30.0, failCB)
@@ -725,7 +742,8 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                                else:
                                        text = message.getMessage()
                                self.msncon.gotMessage(self.remoteUser, text)
-                       except:
+                       except UnicodeDecodeError:
+                               LogEvent(WARN, self.ident, "Message lost!")
                                self.msncon.gotMessage(self.remoteUser, "A message was lost.")
                                raise
                elif "text/x-clientcaps" == cTypes[0]: