Guest User

Untitled

a guest
Jan 25th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Name: msnmsg.py -by Ryan Kulla
  4. #
  5. # Description: Connects to the MSN messenger service and sends a predefined
  6. # message to a predefined user.
  7. #
  8. # Note: This code will no longer work since Microsoft has closed the MSN chat
  9. # in 2006 in favor of the new Live Messenger.
  10.  
  11. from string import join, split, find, rfind, replace
  12. import socket
  13. from select import select
  14. import time
  15. import md5
  16.  
  17. ### Variables you should edit: ###
  18. MESSAGE = "Hello this is a test from Ryan's msn messenger client"
  19. MY_USERNAME = "you@hotmail.com" # Your MSN handle
  20. MY_PASSWORD = "yourpass" # Your MSN password
  21. THEIR_USERNAME = "them@hotmail.com" # A person you want to send a message to
  22. ### Stop editing ###
  23.  
  24. SERVER = "messenger.hotmail.com" # The MSN chat server address
  25. PORT = 1863 # The MSN chat port
  26. DISPLAY_NAME = "" # Your friendly name as shown to other users on MSN
  27. MIME_HEADER = "MIME-Version: 1.0\r\n" \
  28. "Content-Type: text/plain; charset=UTF-8\r\n" \
  29. "X-MMS-IM-Format: FN=MS%20Sans%20Serif;" \
  30. "EF=; CO=0; CS=0; PF=0\r\n\r\n"
  31. MESSAGE_SIZE = len(MIME_HEADER) + len(MESSAGE.encode('utf-8'))
  32. CHATTING = 0 # Toggle this if you're using an SB (Switchboard) for chatting
  33. TRAN_ID = 0 # MSN Transaction ID
  34. S = "" # The connection socket
  35. SBS = "" # The Switchboard socket
  36.  
  37. def msn_connect():
  38. """Connect to initial MSN server."""
  39. global S
  40. S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  41. try:
  42. S.connect((SERVER, PORT))
  43. except:
  44. print "Operation Timed Out.\n"
  45. S.setblocking(0) # Use non-blocking sockets.
  46. S.send("VER %d MSNP5\r\n" % trid()) # Send the protocol version.
  47.  
  48. def network_IO():
  49. """Handle network input and output."""
  50. data = sb_data = ""
  51.  
  52. ready_to_read_sb = int()
  53. ready_to_read, ready_to_write, in_error = select([S], [S], [S], 30)
  54. if CHATTING:
  55. ready_to_read_sb, ready_to_write_sb, in_error_sb = \
  56. select([SBS], [SBS], [SBS], 30)
  57. if ready_to_read: # There is data waiting on the S socket, so recv()
  58. try:
  59. data = S.recv(1024)
  60. except:
  61. print "Failed to receive data.\n"
  62. lines = split(data, "\r\n") # Lines of server data to parse.
  63. check_incoming(lines)
  64. for line in lines:
  65. print line # Print messages sent on the S socket to the console.
  66. if ready_to_read_sb: # There is data waiting on the SBS socket, so recv().
  67. try:
  68. sb_data = SBS.recv(1024)
  69. except:
  70. print "Failed to receive sb data.\n"
  71. if CHATTING and sb_data:
  72. sb_lines = split(sb_data, "\r\n")
  73. check_incoming_sb(sb_lines)
  74. for sb_line in sb_lines:
  75. print sb_line # Print messages sent on the SBS socket.
  76.  
  77. def check_incoming(lines):
  78. """Parse data received on the global S socket."""
  79. global SERVER
  80. global PORT
  81. global S
  82. global DISPLAY_NAME
  83. for line in lines:
  84. try:
  85. if line.startswith("VER"):
  86. # Check if MSNP5 is in the list, then send INF:
  87. if line.find("MSNP5") != -1:
  88. S.send("INF %d\r\n" % trid()) # Check security package.
  89. elif line.startswith("INF"):
  90. # Check to make sure we can use md5:
  91. if line.find("MD5") != -1:
  92. print "Logging in as %s" % MY_USERNAME
  93. # Send our username:
  94. S.send("USR %d MD5 I %s\r\n" % (trid(), MY_USERNAME))
  95. elif line.startswith("XFR"):
  96. if line.find("SB") != -1:
  97. send_message(line)
  98. if line.find("NS") != -1:
  99. print "Transferring to another Notification Server."
  100.  
  101. SERVER = line.split(' ')[3].split(':')[0]
  102. PORT = int(line.split(' ')[3].split(':')[1])
  103. S.close() # Close the current socket.
  104. msn_connect() # Register under the referred server/port.
  105. elif line.startswith("USR"):
  106. # Accepted the password.
  107. if line.split(' ')[2] == "OK":
  108. DISPLAY_NAME = line.split(' ')[4]
  109. print "Welcome: %s" % DISPLAY_NAME
  110. # Send the synchronization command:
  111. S.send("SYN %d 0\r\n" % trid())
  112. # Put the client online:
  113. S.send("CHG %d NLN\r\n" % trid())
  114. # Request referral to a Switchboard:
  115. S.send("XFR %d SB\r\n" % trid())
  116. elif line.find("MD5") != -1:
  117. # Received challenge info.
  118. m = md5.new()
  119. # Concatenate the password to the challenge string.
  120. AuthResponseInfo = line.split(' ')[-1] + MY_PASSWORD
  121. m.update(AuthResponseInfo) # Make an MD5 hash.
  122. # send MD5 AuthReponse and Password
  123. S.send("USR %d MD5 S %s%s" % \
  124. (trid(), m.hexdigest(), "\r\n"))
  125. else:
  126. pass
  127. except:
  128. print "Unknown Error.\n"
  129.  
  130. def check_incoming_sb(lines):
  131. """Parse the data received on the global SBS socket."""
  132. for line in lines:
  133. if line.find("CAL") != -1:
  134. if line.split(' ')[2] == "RINGING":
  135. session_id = int(line.split(' ')[3])
  136. # Send an Instant Message to someone:
  137. print "Sending test message to %s\n" % THEIR_USERNAME
  138. SBS.send("MSG %d N %d\r\n%s%s" % \
  139. (session_id, MESSAGE_SIZE, MIME_HEADER,
  140. MESSAGE.encode('utf-8')))
  141.  
  142. def send_message(line):
  143. """Send a message."""
  144. global SBS
  145. global CHATTING
  146. (sb_server, sb_port) = line.split(' ')[3].split(':')
  147. cookie = line.split(' ')[5]
  148. # Open a new socket to a SB server for chatting:
  149.  
  150. SBS = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  151. try:
  152. SBS.connect((sb_server, int(sb_port)))
  153. CHATTING = 1
  154. except:
  155. print "Couldn't connect to send Instant Message.\n"
  156. SBS.setblocking(0) # Use non-blocking sockets.
  157. # Authenicate with the Switchboard:
  158. SBS.send("USR %d %s %s\r\n" % (trid(), MY_USERNAME, cookie))
  159. # Invite the user to chat:
  160. SBS.send("CAL %d %s\r\n" % (trid(), THEIR_USERNAME))
  161.  
  162. def trid():
  163. """Create a unique msn transaction id."""
  164. global TRAN_ID
  165. TRAN_ID = TRAN_ID + 1
  166. return TRAN_ID
  167.  
  168. def main():
  169. """Main Loop."""
  170. print "Connecting to MSN..."
  171. msn_connect()
  172.  
  173. while 1:
  174. network_IO()
  175. time.sleep(1) # Check select every second.
  176.  
  177. if __name__=='__main__':
  178. main()
Add Comment
Please, Sign In to add comment