]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/msnw.py
Added auto-reconnect.
[pymsnt] / src / tlib / msn / msnw.py
index 7f72f2df5945bf81f3d81587fe41a169b1ef321a..b0edb3066831e03cb6e99b5afc363fb5b49b7e64 100644 (file)
@@ -50,8 +50,9 @@ class MSNConnection:
 
        def _getNotificationReferral(self):
                def timeout():
+                       self.timeout = None
                        if not d.called:
-                               d.errback()
+                               d.errback("Timeout")
                                self.logOut() # Clean up everything
                self.timeout = reactor.callLater(30, timeout)
                dispatchFactory = msn.DispatchFactory()
@@ -65,6 +66,7 @@ class MSNConnection:
        
        def _gotNotificationReferral(self, (host, port)):
                self.timeout.cancel()
+               self.timeout = None
                # Create the NotificationClient
                self.notificationFactory = msn.NotificationFactory()
                self.notificationFactory.userHandle = self.username
@@ -76,7 +78,6 @@ class MSNConnection:
        
        def _sendSavedEvents(self):
                self.savedEvents.send(self)
-               self.savedEvents = None
        
        def _notificationClientReady(self, notificationClient):
                self.notificationClient = notificationClient
@@ -175,8 +176,8 @@ class MSNConnection:
                if self.notificationClient:
                        LogEvent(INFO, self.ident)
                        self.notificationClient.changeAvatar(imageData, push=True)
-               else:
-                       self.savedEvents.avatarImageData = imageData
+               # Save the avatar for reuse on disconnection
+               self.savedEvents.avatarImageData = imageData
        
        def changeStatus(self, statusCode, screenName, personal):
                """
@@ -189,6 +190,8 @@ class MSNConnection:
                """
 
                if not screenName: screenName = self.username
+               if not statusCode: statusCode = msn.STATUS_ONLINE
+               if not personal: personal = ""
                if self.notificationClient:
                        changeCount = [0] # Hack
                        def cb(ignored=None):
@@ -198,12 +201,11 @@ class MSNConnection:
                        LogEvent(INFO, self.ident)
                        self.notificationClient.changeStatus(statusCode.encode("utf-8")).addCallback(cb)
                        self.notificationClient.changeScreenName(screenName.encode("utf-8")).addCallback(cb)
-                       if not personal: personal = ""
                        self.notificationClient.changePersonalMessage(personal.encode("utf-8")).addCallback(cb)
-               else:
-                       self.savedEvents.statusCode = statusCode
-                       self.savedEvents.screenName = screenName
-                       self.savedEvents.personal = personal
+               # Remember the saved status
+               self.savedEvents.statusCode = statusCode
+               self.savedEvents.screenName = screenName
+               self.savedEvents.personal = personal
                                
        def addContact(self, listType, userHandle):
                """ See msn.NotificationClient.addContact """
@@ -233,6 +235,9 @@ class MSNConnection:
                        if hasattr(sbs, "transport") and sbs.transport:
                                sbs.transport.loseConnection()
                self.switchboardSessions = {}
+               if self.timeout:
+                       self.timeout.cancel()
+                       self.timeout = None
                LogEvent(INFO, self.ident)
                
        
@@ -370,11 +375,13 @@ class NotificationClient(msn.NotificationClient):
                msn.NotificationClient.logOut(self)
        
        def connectionLost(self, reason):
+               print "NotificationClient.connectionLost!!!"
                if not self.factory.msncon: return # If we called logOut
                def wait():
                        LogEvent(INFO, self.factory.msncon.ident)
                        msn.NotificationClient.connectionLost(self, reason)
-                       self.factory.msncon.connectionLost(reason)
+                       if self.factory.maxRetries >= self.factory.retries:
+                               self.factory.msncon.connectionLost(reason)
                # Make sure this event is handled after any others
                reactor.callLater(0, wait)
        
@@ -645,9 +652,7 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                del self.remoteUser
                self.contactCount = 0
                self.msncon.gotGroupchat(self, userHandle)
-               if not self.groupchat:
-                       LogEvent(ERROR, self.ident)
-                       raise Exception("YouNeedAGroupchat-WeHaveAProblemError") # FIXME
+               assert self.groupchat
        
        def failedMessage(self, text):
                self.msncon.failedMessage(self.remoteUser, text)