]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/msnw.py
Handle MSN error conditions
[pymsnt] / src / tlib / msn / msnw.py
index a23d213a15e21ae52e948ee67879b4ff37edf6a7..b5d47caf93b9c188bc6183aabd0cf7a0242b16f9 100644 (file)
@@ -193,15 +193,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
@@ -229,6 +231,7 @@ class MSNConnection:
                for c in self.connectors:
                        c.disconnect()
                if self.notificationFactory:
+                       self.notificationFactory.stopTrying()
                        self.notificationFactory.msncon = None
                self.connectors = []
                for sbs in self.switchboardSessions.values():
@@ -384,11 +387,15 @@ class NotificationClient(msn.NotificationClient):
                self.connectionLost = self.doDisconnect
        
        def connectionLost(self, reason):
-               if not self.ready: return
+               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)
-                       if self.factory.maxRetries >= self.factory.retries:
+                       if self.factory.maxRetries > self.factory.retries:
+                               self.factory.stopTrying()
                                self.factory.msncon.connectionLost(reason)
                # Make sure this event is handled after any others
                reactor.callLater(0, wait)
@@ -443,9 +450,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 +562,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)))
@@ -645,6 +651,7 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                for message, noerror in self.messageBuffer:
                        if not noerror:
                                self.failedMessage(message)
+               self.messageBuffer = []
                SwitchboardSessionBase.connectionLost(self, reason)
 
        def _ready(self):