Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Name: msnmsg.py -by Ryan Kulla
- #
- # Description: Connects to the MSN messenger service and sends a predefined
- # message to a predefined user.
- #
- # Note: This code will no longer work since Microsoft has closed the MSN chat
- # in 2006 in favor of the new Live Messenger.
- from string import join, split, find, rfind, replace
- import socket
- from select import select
- import time
- import md5
- ### Variables you should edit: ###
- MESSAGE = "Hello this is a test from Ryan's msn messenger client"
- MY_USERNAME = "you@hotmail.com" # Your MSN handle
- MY_PASSWORD = "yourpass" # Your MSN password
- THEIR_USERNAME = "them@hotmail.com" # A person you want to send a message to
- ### Stop editing ###
- SERVER = "messenger.hotmail.com" # The MSN chat server address
- PORT = 1863 # The MSN chat port
- DISPLAY_NAME = "" # Your friendly name as shown to other users on MSN
- MIME_HEADER = "MIME-Version: 1.0\r\n" \
- "Content-Type: text/plain; charset=UTF-8\r\n" \
- "X-MMS-IM-Format: FN=MS%20Sans%20Serif;" \
- "EF=; CO=0; CS=0; PF=0\r\n\r\n"
- MESSAGE_SIZE = len(MIME_HEADER) + len(MESSAGE.encode('utf-8'))
- CHATTING = 0 # Toggle this if you're using an SB (Switchboard) for chatting
- TRAN_ID = 0 # MSN Transaction ID
- S = "" # The connection socket
- SBS = "" # The Switchboard socket
- def msn_connect():
- """Connect to initial MSN server."""
- global S
- S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- S.connect((SERVER, PORT))
- except:
- print "Operation Timed Out.\n"
- S.setblocking(0) # Use non-blocking sockets.
- S.send("VER %d MSNP5\r\n" % trid()) # Send the protocol version.
- def network_IO():
- """Handle network input and output."""
- data = sb_data = ""
- ready_to_read_sb = int()
- ready_to_read, ready_to_write, in_error = select([S], [S], [S], 30)
- if CHATTING:
- ready_to_read_sb, ready_to_write_sb, in_error_sb = \
- select([SBS], [SBS], [SBS], 30)
- if ready_to_read: # There is data waiting on the S socket, so recv()
- try:
- data = S.recv(1024)
- except:
- print "Failed to receive data.\n"
- lines = split(data, "\r\n") # Lines of server data to parse.
- check_incoming(lines)
- for line in lines:
- print line # Print messages sent on the S socket to the console.
- if ready_to_read_sb: # There is data waiting on the SBS socket, so recv().
- try:
- sb_data = SBS.recv(1024)
- except:
- print "Failed to receive sb data.\n"
- if CHATTING and sb_data:
- sb_lines = split(sb_data, "\r\n")
- check_incoming_sb(sb_lines)
- for sb_line in sb_lines:
- print sb_line # Print messages sent on the SBS socket.
- def check_incoming(lines):
- """Parse data received on the global S socket."""
- global SERVER
- global PORT
- global S
- global DISPLAY_NAME
- for line in lines:
- try:
- if line.startswith("VER"):
- # Check if MSNP5 is in the list, then send INF:
- if line.find("MSNP5") != -1:
- S.send("INF %d\r\n" % trid()) # Check security package.
- elif line.startswith("INF"):
- # Check to make sure we can use md5:
- if line.find("MD5") != -1:
- print "Logging in as %s" % MY_USERNAME
- # Send our username:
- S.send("USR %d MD5 I %s\r\n" % (trid(), MY_USERNAME))
- elif line.startswith("XFR"):
- if line.find("SB") != -1:
- send_message(line)
- if line.find("NS") != -1:
- print "Transferring to another Notification Server."
- SERVER = line.split(' ')[3].split(':')[0]
- PORT = int(line.split(' ')[3].split(':')[1])
- S.close() # Close the current socket.
- msn_connect() # Register under the referred server/port.
- elif line.startswith("USR"):
- # Accepted the password.
- if line.split(' ')[2] == "OK":
- DISPLAY_NAME = line.split(' ')[4]
- print "Welcome: %s" % DISPLAY_NAME
- # Send the synchronization command:
- S.send("SYN %d 0\r\n" % trid())
- # Put the client online:
- S.send("CHG %d NLN\r\n" % trid())
- # Request referral to a Switchboard:
- S.send("XFR %d SB\r\n" % trid())
- elif line.find("MD5") != -1:
- # Received challenge info.
- m = md5.new()
- # Concatenate the password to the challenge string.
- AuthResponseInfo = line.split(' ')[-1] + MY_PASSWORD
- m.update(AuthResponseInfo) # Make an MD5 hash.
- # send MD5 AuthReponse and Password
- S.send("USR %d MD5 S %s%s" % \
- (trid(), m.hexdigest(), "\r\n"))
- else:
- pass
- except:
- print "Unknown Error.\n"
- def check_incoming_sb(lines):
- """Parse the data received on the global SBS socket."""
- for line in lines:
- if line.find("CAL") != -1:
- if line.split(' ')[2] == "RINGING":
- session_id = int(line.split(' ')[3])
- # Send an Instant Message to someone:
- print "Sending test message to %s\n" % THEIR_USERNAME
- SBS.send("MSG %d N %d\r\n%s%s" % \
- (session_id, MESSAGE_SIZE, MIME_HEADER,
- MESSAGE.encode('utf-8')))
- def send_message(line):
- """Send a message."""
- global SBS
- global CHATTING
- (sb_server, sb_port) = line.split(' ')[3].split(':')
- cookie = line.split(' ')[5]
- # Open a new socket to a SB server for chatting:
- SBS = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- SBS.connect((sb_server, int(sb_port)))
- CHATTING = 1
- except:
- print "Couldn't connect to send Instant Message.\n"
- SBS.setblocking(0) # Use non-blocking sockets.
- # Authenicate with the Switchboard:
- SBS.send("USR %d %s %s\r\n" % (trid(), MY_USERNAME, cookie))
- # Invite the user to chat:
- SBS.send("CAL %d %s\r\n" % (trid(), THEIR_USERNAME))
- def trid():
- """Create a unique msn transaction id."""
- global TRAN_ID
- TRAN_ID = TRAN_ID + 1
- return TRAN_ID
- def main():
- """Main Loop."""
- print "Connecting to MSN..."
- msn_connect()
- while 1:
- network_IO()
- time.sleep(1) # Check select every second.
- if __name__=='__main__':
- main()
Add Comment
Please, Sign In to add comment