# Twisted imports
from twisted.internet import reactor, task
from twisted.internet.defer import Deferred
-from twisted.internet.protocol import ClientFactory
-from twisted.internet.ssl import ClientContextFactory
+from twisted.internet.protocol import ReconnectingClientFactory, ClientFactory
+try:
+ from twisted.internet.ssl import ClientContextFactory
+except ImportError:
+ print "You must install pycrypto and pyopenssl."
+ raise
from twisted.python import failure, log
-from twisted.xish.domish import unescapeFromXml
# Compat stuff
from tlib import xmlw
# System imports
-import types, operator, os, sys, base64, random, struct, random, sha, base64, StringIO, array, codecs
+import types, operator, os, sys, base64, random, struct, random, sha, base64, StringIO, array, codecs, binascii
from urllib import quote, unquote
MSN_CVR_STR = "0x040c winnt 5.1 i386 MSNMSGR 7.0.0777 msmsgs"
MSN_AVATAR_GUID = "{A4268EEC-FEC5-49E5-95C3-F126696BDBF6}"
MSN_MSNFTP_GUID = "{5D3E02AB-6190-11D3-BBBB-00C04F795683}"
-P2PSEQ = [-3, -2, 0, -1, 1, 2, 3, 4, 5, 6, 7, 8]
# auth constants
LOGIN_SUCCESS = 1
PINGSPEED = 50.0
-DEBUGALL = True
LINEDEBUG = False
MESSAGEDEBUG = False
MSNP2PDEBUG = False
-if DEBUGALL:
- LINEDEBUG = True
- MESSAGEDEBUG = True
- MSNP2PDEBUG = True
+
+P2PSEQ = [-3, -2, 0, -1, 1, 2, 3, 4, 5, 6, 7, 8]
+def p2pseq(n):
+ if n > 5:
+ return n - 3
+ else:
+ return P2PSEQ[n]
+
def getVal(inp):
return inp.split('=')[1]
def connectionMade(self):
self.sendCommand('GET', self.path)
self.sendHeader('Authorization', 'Passport1.4 OrgVerb=GET,OrgURL=http://messenger.msn.com,' +
- 'sign-in=%s,pwd=%s,%s' % (quote(self.userHandle), self.passwd,self.authData))
+ 'sign-in=%s,pwd=%s,%s' % (quote(self.userHandle), quote(self.passwd), self.authData))
self.sendHeader('Host', self.host)
self.endHeaders()
self.headers = {}
self.screenName = screenName
self.specialMessage = specialMessage
self.message = message
- self.headers = {'MIME-Version' : '1.0', 'Content-Type' : 'text/plain'}
+ self.headers = {'MIME-Version' : '1.0', 'Content-Type' : 'text/plain; charset=UTF-8'}
self.length = length
self.readPos = 0
"""
return reduce(operator.add, [len(x[0]) + len(x[1]) + 4 for x in self.headers.items()]) + len(self.message) + 2
+ def delHeader(self, header):
+ """ delete the desired header """
+ if self.headers.has_key(header):
+ del self.headers[header]
+
def setHeader(self, header, value):
""" set the desired header """
self.headers[header] = value
"""
def __init__(self, s=""):
""" Pass a XML MSNObject string to parse it, or pass no arguments for a null MSNObject to be created. """
+ self.setNull()
if s:
self.parse(s)
- else:
- self.setNull()
def setData(self, creator, imageData):
""" Set the creator and imageData for this object """
raise NotImplementedError
def sendLine(self, line):
- if LINEDEBUG: log.msg(">> " + line)
+ if LINEDEBUG: log.msg("<< " + line)
LineReceiver.sendLine(self, line)
def lineReceived(self, line):
- if LINEDEBUG: log.msg("<< " + line)
+ if LINEDEBUG: log.msg(">> " + line)
+ if not self.connected: return
if self.currentMessage:
self.currentMessage.readPos += len(line+"\r\n")
try:
if len(cmd) != 3: raise MSNProtocolError, "Invalid Command, %s" % repr(cmd)
if cmd.isdigit():
- if self.ids.has_key(params.split(' ')[0]):
- self.ids[id].errback(int(cmd))
+ id = params.split(' ')[0]
+ if id.isdigit() and self.ids.has_key(int(id)):
+ id = int(id)
+ self.ids[id][0].errback(int(cmd))
del self.ids[id]
return
else: # we received an error which doesn't map to a sent command
self.handle_UNKNOWN(cmd, params.split(' '))
def rawDataReceived(self, data):
+ if not self.connected: return
extra = ""
self.currentMessage.readPos += len(data)
diff = self.currentMessage.readPos - self.currentMessage.length
m = self.currentMessage
self.currentMessage = None
if MESSAGEDEBUG: log.msg(m.message)
- if not self.checkMessage(m):
+ try:
+ if not self.checkMessage(m):
+ self.setLineMode(extra)
+ return
+ except Exception, e:
self.setLineMode(extra)
- return
+ raise
self.gotMessage(m)
self.setLineMode(extra)
MSNEventBase.connectionMade(self)
self._setState('CONNECTED')
self.sendLine("VER %s %s" % (self._nextTransactionID(), MSN_PROTOCOL_VERSION))
+ self.factory.resetDelay()
def connectionLost(self, reason):
self._setState('DISCONNECTED')
self.gotMSNAlert(bodytext, actionurl, subscrurl)
def _gotUBX(self, message):
+ msnContact = self.factory.contacts.getContact(message.userHandle)
+ if not msnContact: return
lm = message.message.lower()
p1 = lm.find("<psm>") + 5
p2 = lm.find("</psm>")
if p1 >= 0 and p2 >= 0:
- personal = unescapeFromXml(message.message[p1:p2])
- msnContact = self.factory.contacts.getContact(message.userHandle)
- if not msnContact: return
+ personal = xmlw.unescapeFromXml(message.message[p1:p2])
msnContact.personal = personal
self.contactPersonalChanged(message.userHandle, personal)
else:
+ msnContact.personal = ''
self.contactPersonalChanged(message.userHandle, '')
def checkMessage(self, message):
if MSNP2PDEBUG: log.msg("Updated MSNObject received!" + msnobjStr)
msnContact.msnobj = msnobj
msnContact.msnobjGot = False
- self.contactAvatarChanged(msnContact.userHandle, msnContact.msnobj.sha1d)
+ self.contactAvatarChanged(msnContact.userHandle, binascii.hexlify(b64dec(msnContact.msnobj.sha1d)))
def handle_CHL(self, params):
checkParamLen(len(params), 2, 'CHL')
self.currentMessage = MSNMessage(length=messageLen, userHandle=params[0], screenName="UBX", specialMessage=True)
self.setRawMode()
else:
- self.contactPersonalChanged(params[0], '')
+ self._gotUBX(MSNMessage(userHandle=params[0]))
def handle_UUX(self, params):
checkParamLen(len(params), 2, 'UUX')
def handle_OUT(self, params):
checkParamLen(len(params), 1, 'OUT')
+ self.factory.stopTrying()
if params[0] == "OTH": self.multipleLogin()
elif params[0] == "SSD": self.serverGoingDown()
else: raise MSNProtocolError, "Invalid Parameters received for OUT" # debug
contact.
@param userHandle: contact who's msnobj has been changed
- @param hash: sha1 hash of their avatar
+ @param hash: sha1 hash of their avatar as hex string
"""
def statusChanged(self, statusCode):
if self.pingCheckTask:
self.pingCheckTask.stop()
self.pingCheckTask = None
+ self.factory.stopTrying()
self.sendLine("OUT")
self.transport.loseConnection()
-class NotificationFactory(ClientFactory):
+class NotificationFactory(ReconnectingClientFactory):
"""
Factory for the NotificationClient protocol.
This is basically responsible for keeping
(the whole URL is required)
@ivar status: The status of the client -- this is generally kept
up to date by the default command handlers
+ @ivar maxRetries: The number of times the factory will reconnect
+ if the connection dies because of a network error.
"""
contacts = None
screenName = ''
password = ''
passportServer = 'https://nexus.passport.com/rdr/pprdr.asp'
- status = 'FLN'
+ status = 'NLN'
protocol = NotificationClient
+ maxRetries = 5
class SwitchboardClient(MSNEventBase):
cookie = info['Invitation-Cookie']
filename = info['Application-File']
filesize = int(info['Application-FileSize'])
- connectivity = (info.get('Connectivity').lower() == 'y')
+ connectivity = (info.get('Connectivity', 'n').lower() == 'y')
except KeyError:
log.msg('Received munged file transfer request ... ignoring.')
return 0
if not message.getHeader("P2P-Dest") == self.userHandle: return
packet = message.message
binaryFields = BinaryFields(packet=packet)
- if binaryFields[0] != 0:
+ if binaryFields[5] == BinaryFields.BYEGOT:
+ pass # Ignore the ACKs to SLP messages
+ elif binaryFields[0] != 0:
slpLink = self.slpLinks.get(binaryFields[0])
if not slpLink:
# Link has been killed. Ignore
return
if slpLink.remoteUser == message.userHandle:
slpLink.handlePacket(packet)
- if binaryFields[5] == BinaryFields.ACK or binaryFields[5] == BinaryFields.BYEGOT:
+ elif binaryFields[5] == BinaryFields.ACK:
pass # Ignore the ACKs to SLP messages
else:
slpMessage = MSNSLPMessage(packet)
"""
cTypes = [s.lstrip() for s in message.getHeader('Content-Type').split(';')]
if self._checkTyping(message, cTypes): return 0
- if 'text/x-msmsgsinvite' in cTypes:
+# if 'text/x-msmsgsinvite' in cTypes:
# header like info is sent as part of the message body.
- info = {}
- for line in message.message.split('\r\n'):
- try:
- key, val = line.split(':')
- info[key] = val.lstrip()
- except ValueError: continue
- if self._checkFileInvitation(message, info): return 0
+# info = {}
+# for line in message.message.split('\r\n'):
+# try:
+# key, val = line.split(':')
+# info[key] = val.lstrip()
+# except ValueError: continue
+# if self._checkFileInvitation(message, info): return 0
elif 'application/x-msnmsgrp2p' in cTypes:
self._handleP2PMessage(message)
return 0
else: id, d = self._createIDMapping()
if message.length == 0: message.length = message._calcMessageLen()
self.sendLine("MSG %s %s %s" % (id, message.ack, message.length))
- # apparently order matters with at least MIME-Version and Content-Type
- self.sendLine('MIME-Version: %s' % message.getHeader('MIME-Version'))
- self.sendLine('Content-Type: %s' % message.getHeader('Content-Type'))
+ # Apparently order matters with these
+ orderMatters = ("MIME-Version", "Content-Type", "Message-ID")
+ for header in orderMatters:
+ if message.hasHeader(header):
+ self.sendLine("%s: %s" % (header, message.getHeader(header)))
# send the rest of the headers
- for header in [h for h in message.headers.items() if h[0].lower() not in ('mime-version','content-type')]:
+ for header in [h for h in message.headers.items() if h[0] not in orderMatters]:
self.sendLine("%s: %s" % (header[0], header[1]))
self.transport.write("\r\n")
self.transport.write(message.message)
def bufferClosed(data):
d.callback((data,))
buffer = StringBuffer(bufferClosed)
+ buffer.error = lambda: None
slpLink = SLPLink_AvatarReceive(remoteUser=msnContact.userHandle, switchboard=self, consumer=buffer, context=msnContact.msnobj.text)
self.slpLinks[slpLink.sessionID] = slpLink
return d
data = data[:-1] # Uck, weird, but it works
data += utf16net(self.filename)
data = ljust(data, 570, '\0')
- data += struct.pack("<L", 0xFFFFFFFF)
+ data += struct.pack("<L", 0xFFFFFFFFL)
data = ljust(data, 638, '\0')
return data
self.pos = -1
def get(self):
- return P2PSEQ[self.pos] + self.baseID
+ return p2pseq(self.pos) + self.baseID
def next(self):
self.pos += 1
self.seqID = SeqID()
def killLink(self):
+ if MSNP2PDEBUG: log.msg("killLink")
def kill():
+ if MSNP2PDEBUG: log.msg("killLink - kill()")
if not self.switchboard: return
del self.switchboard.slpLinks[self.sessionID]
self.switchboard = None
reactor.callLater(0, kill)
def warn(self, text):
- if MSNP2PDEBUG:
- log.msg("Warning in transfer: %s %s" % (self, text))
+ log.msg("Warning in transfer: %s %s" % (self, text))
def sendP2PACK(self, ackHeaders):
binaryFields = BinaryFields()
def write(self, data):
if MSNP2PDEBUG: log.msg("write")
i = 0
+ data = self.data + data
+ self.data = ""
length = len(data)
while i < length:
if i + 1202 < length:
self._writeChunk(data[i:i+1202])
i += 1202
else:
- self.data += data[i:]
- if len(self.data) >= 1202:
- data = self.data
- self.data = ""
- self.write(data)
+ self.data = data[i:]
return
def _writeChunk(self, chunk):
- log.msg("writing chunk")
+ if MSNP2PDEBUG: log.msg("writing chunk")
binaryFields = BinaryFields()
binaryFields[0] = self.sessionID
if self.offset == 0:
else:
if slpMessage.status == "603":
self.acceptDeferred.callback((False,))
- # SLPLink is over due to decline, error or BYE
+ if MSNP2PDEBUG: log.msg("SLPLink is over due to decline, error or BYE")
+ self.data = ""
self.killLink()
def wait_data_ack(self, packet):
self.handlePacket = lambda packet: None
def handleSLPMessage(self, slpMessage):
- self.killLink() # BYE or error
+ if MSNP2PDEBUG: log.msg("BYE or error")
+ self.killLink()
def close(self):
SLPLink_Send.close(self)
if binaryFields[4] != 4:
self.warn("field4," + str(binaryFields[4]))
return
- if binaryFields[9] != 1:
- self.warn("field9," + str(binaryFields[9]))
+ # Just ignore the footer
+ #if binaryFields[9] != 1:
+ # self.warn("field9," + str(binaryFields[9]))
# return
self.sendP2PACK(binaryFields)
if binaryFields[5] != self.dataFlag:
self.warn("field5," + str(binaryFields[5]))
return
- if binaryFields[9] != 1:
- self.warn("field9," + str(binaryFields[9]))
+ # Just ignore the footer
+ #if binaryFields[9] != 1:
+ # self.warn("field9," + str(binaryFields[9]))
# return
offset = binaryFields[2]
total = binaryFields[3]
self.pos += length
- self.consumer.write(data)
+ self.consumer.write(str(data))
if self.pos == total:
self.sendP2PACK(binaryFields)
def reject(self):
# Send a 603 decline
+ if not self.switchboard: return
self.sendSLPMessage("603", "application/x-msnmsgr-sessionreqbody", {"SessionID":self.sessionID}, branch=self.initialBranch)
self.killLink()
def accept(self, consumer):
FileReceive.accept(self, consumer)
+ if not self.switchboard: return
self.sendSLPMessage("200", "application/x-msnmsgr-sessionreqbody", {"SessionID":self.sessionID}, branch=self.initialBranch)
+ self.handlePacket = self.wait_data # Moved here because sometimes the second INVITE seems to be skipped
def handleSLPMessage(self, slpMessage):
if slpMessage.method == "INVITE": # The second invite
"Listening" : "false",\
"Hashed-Nonce": "{00000000-0000-0000-0000-000000000000}"}
self.sendSLPMessage("200", "application/x-msnmsgr-transrespbody", data, branch=slpMessage.branch)
- self.handlePacket = self.wait_data
+# self.handlePacket = self.wait_data # Moved up
else:
- self.killLink() # It's either a BYE or an error
+ if MSNP2PDEBUG: log.msg("It's either a BYE or an error")
+ self.killLink()
# FIXME, do some error handling if it was an error
def doFinished(self):
"AppID" : 1,\
"Context" : context}
self.sendSLPMessage("INVITE", "application/x-msnmsgr-sessionreqbody", data)
+ self.handlePacket = self.wait_dataprep
def handleSLPMessage(self, slpMessage):
- if slpMessage.status == "200":
- self.handlePacket = self.wait_dataprep
+ if slpMessage.method == "INVITE": # The second invite
+ data = {"Bridge" : "TCPv1",\
+ "Listening" : "false",\
+ "Hashed-Nonce": "{00000000-0000-0000-0000-000000000000}"}
+ self.sendSLPMessage("200", "application/x-msnmsgr-transrespbody", data, branch=slpMessage.branch)
+ elif slpMessage.status == "200":
+ pass
+ #self.handlePacket = self.wait_dataprep # Moved upwards
else:
- # SLPLink is over due to error or BYE
+ if MSNP2PDEBUG: log.msg("SLPLink is over due to error or BYE")
self.killLink()
def doFinished(self):
301 : "Too many FND responses",
302 : "Not logged in",
+ 400 : "Message not allowed",
402 : "Error accessing contact list",
403 : "Error accessing contact list",