]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/msnw.py
Hopefully fixed weird exception errors.
[pymsnt] / src / tlib / msn / msnw.py
index 332146cf9c09e9eadfbcb478762d65503040976c..cc6774c2966b439fc2b4b221fe926e8038cdbbf1 100644 (file)
@@ -7,7 +7,7 @@ from twisted.internet.defer import Deferred
 from twisted.internet.protocol import ClientFactory
 
 # System imports
-import math, base64, binascii, math
+import math, base64, binascii
 
 # Local imports
 from debug import LogEvent, INFO, WARN, ERROR
@@ -50,7 +50,10 @@ class MSNConnection:
 
        def _getNotificationReferral(self):
                def timeout():
-                       if not d.called: d.errback()
+                       self.timeout = None
+                       if not d.called:
+                               d.errback("Timeout")
+                               self.logOut() # Clean up everything
                self.timeout = reactor.callLater(30, timeout)
                dispatchFactory = msn.DispatchFactory()
                dispatchFactory.userHandle = self.username
@@ -63,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
@@ -187,6 +191,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):
@@ -196,7 +202,6 @@ 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
@@ -219,7 +224,7 @@ class MSNConnection:
        
        def logOut(self):
                """ Shuts down the whole connection. Don't try to call any
-               other methods after this one. """
+               other methods after this one. Except maybe connect() """
                if self.notificationClient:
                        self.notificationClient.logOut()
                for c in self.connectors:
@@ -231,6 +236,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)
                
        
@@ -525,13 +533,13 @@ class SwitchboardSessionBase(msn.SwitchboardClient):
                        self.messageBuffer.append((text, noerror))
                else:
                        LogEvent(INFO, self.ident)
+                       text = str(text.replace("\n", "\r\n").encode("utf-8"))
                        def failedMessage(ignored):
                                if not noerror:
                                        self.failedMessage(text)
 
                        if len(text) < MSNConnection.MAXMESSAGESIZE:
-                               message = msn.MSNMessage(message=str(text.replace("\n", "\r\n").encode("utf-8")))
-                               message.setHeader("Content-Type", "text/plain; charset=UTF-8")
+                               message = msn.MSNMessage(message=text)
                                message.ack = msn.MSNMessage.MESSAGE_NACK
 
                                d = msn.SwitchboardClient.sendMessage(self, message)
@@ -544,13 +552,14 @@ class SwitchboardSessionBase(msn.SwitchboardClient):
                                guid = msn.random_guid()
                                while chunk < chunks:
                                        offset = chunk * MSNConnection.MAXMESSAGESIZE
-                                       text = message[offset : offset + MSNConnection.MAXMESSAGESIZE]
-                                       message = msn.MSNMessage(message=str(text.replace("\n", "\r\n").encode("utf-8")))
+                                       message = msn.MSNMessage(message=text[offset : offset + MSNConnection.MAXMESSAGESIZE])
                                        message.ack = msn.MSNMessage.MESSAGE_NACK
+                                       message.setHeader("Message-ID", guid)
                                        if chunk == 0:
-                                               message.setHeader("Content-Type", "text/plain; charset=UTF-8")
                                                message.setHeader("Chunks", str(chunks))
                                        else:
+                                               message.delHeader("MIME-Version")
+                                               message.delHeader("Content-Type")
                                                message.setHeader("Chunk", str(chunk))
 
                                        d = msn.SwitchboardClient.sendMessage(self, message)
@@ -623,7 +632,7 @@ class OneSwitchboardSession(SwitchboardSessionBase):
                self.timeout = None
                for message, noerror in self.messageBuffer:
                        if not noerror:
-                               self.failedMessage(self.remoteUser, message)
+                               self.failedMessage(message)
 
        def _ready(self):
                LogEvent(INFO, self.ident)
@@ -642,9 +651,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)