]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/msnw.py
Handle MSN error conditions
[pymsnt] / src / tlib / msn / msnw.py
index 1060293e93834dffa8d052a9b7c696b324106f56..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,10 +387,15 @@ class NotificationClient(msn.NotificationClient):
                self.connectionLost = self.doDisconnect
        
        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)
-                       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)
@@ -442,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):
@@ -468,17 +475,13 @@ class SwitchboardSessionBase(msn.SwitchboardClient):
                self.funcBuffer = []
                self.ready = False
 
-       def __del__(self):
-               self.ready = False
-               LogEvent(INFO, self.ident)
-               del self.msncon
-               self.transport.disconnect()
-       
        def connectionLost(self, reason):
+               msn.SwitchboardClient.connectionLost(self, reason)
+               LogEvent(INFO, self.ident)
                self.ready = False
                self.msncon = None
                self.msnobj = None
-               self.ident = (self.ident[0], self.ident[1] + " Disconnected!")
+               self.ident = (self.ident[0], self.ident[1], "Disconnected!")
 
        def loggedIn(self):
                LogEvent(INFO, self.ident)
@@ -559,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)))
@@ -590,7 +593,7 @@ class MultiSwitchboardSession(SwitchboardSessionBase):
        def __init__(self, msncon):
                """ Automatically creates a new switchboard connection to the server """
                SwitchboardSessionBase.__init__(self, msncon)
-               self.ident = (self.msncon.ident, self)
+               self.ident = (self.msncon.ident, repr(self))
                self.contactCount = 0
                self.groupchat = None
                self.connect()
@@ -641,13 +644,15 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                self.chattingUsers = []
                self.timeout = None
        
-       def __del__(self):
+       def connectionLost(self, reason):
                if self.timeout:
                        self.timeout.cancel()
                self.timeout = None
                for message, noerror in self.messageBuffer:
                        if not noerror:
                                self.failedMessage(message)
+               self.messageBuffer = []
+               SwitchboardSessionBase.connectionLost(self, reason)
 
        def _ready(self):
                LogEvent(INFO, self.ident)
@@ -681,6 +686,7 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                                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)