Advertisement
Aha2Y

Untitled

Jul 29th, 2011
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 3.98 KB | None | 0 0
  1.  
  2. To start off our python bot, we will need to import socket, and sys aswell as add our hashbang
  3.  
  4. Code:
  5. """
  6. #!/usr/bin/env python
  7. import socket, sys
  8. """
  9.  
  10. Now that we have imported the needed modules we can start on the real code, first off we need
  11. to specify a few variables for the bot to work and function how we want it to.
  12.  
  13. Code:
  14. """
  15.  
  16. irc_host = "irc.malvager.com"
  17. irc_port = 6667
  18. irc_channel = "#Botland"
  19. irc_nick = "MalvagerBot"
  20.  
  21. """
  22.  
  23. You should replace the variables for your own settings, these are essential to the bot's function
  24. aswell as connecting to the server, which is the next step.
  25.  
  26. Code:
  27. """
  28. try:
  29.     irc_sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  30.     irc_sock.connect ((irc_host, irc_port))
  31. except:
  32.     print ("[Malvager Bot]Error: Could not connect to IRC; Host: %s Port: %i") % (irc_host, int(irc_port))
  33.     sys.exit(1)
  34. print ("[Malvager Bot]Connected to the IRC; Host: %s Port: %i") % (irc_host, int(irc_port))
  35.  
  36. """
  37.  
  38. This small snippet creates our TCP socket and attempts to connect to the IRC, if it encounters any
  39. errors connecting, it will display an error message and exit, if all goes well it will tell you so
  40. and continue on. Our next step is to set some server side IRC variables, like your user and nick.
  41.  
  42. Code:
  43. """
  44.  
  45. irc_sock.send (( "NICK %s \r\n") % (irc_nick))
  46. print ("...Setting bot nick to %s") % (irc_nick)
  47. irc_sock.send (( "USER %s 8 * :X\r\n" ) % (irc_nick))
  48. print ("...Setting User")
  49. irc_sock.send (( "JOIN %s \r\n" ) % (irc_channel))
  50. print ("...Joining channel %s") % (irc_channel)
  51.  
  52. """
  53.  
  54. This sends information back to the IRC, to set our bot's nickname, user, and the channel we
  55. would like our bot to join. Now onto the hardest part, the IRC loop and parsing IRC messages.
  56.  
  57. When a user talks into a channel the IRC server sends back messages to all its clients in
  58. the form of
  59.  
  60. :nick!host PRIVMSG #channel: message
  61.  
  62. so the next snippet will be used to sort out who is talking, what they are saying and
  63. where are they connecting from ^__^
  64.  
  65. Code:
  66. """
  67.  
  68. while True:
  69.     recv = irc_sock.recv( 4096 )
  70.     if recv.find ( 'PING' ) != -1:
  71.         irc_sock.send ( 'PONG ' + recv.split() [ 1 ] + '\r\n' )
  72.     if recv.find ( 'PRIVMSG' ) != -1:
  73.         irc_user_nick = recv.split ( '!' ) [ 0 ] . replace ( ':', '' )
  74.         irc_user_host = recv.split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
  75.         irc_user_message = ''.join ( recv.split ( ':' ) [ 2: ] ).replace("\r\n", "")
  76.         print ( "Nick:%s \nHost:%s \nMessage:%s" ) % (irc_user_nick, irc_user_host, irc_user_message)
  77.         """
  78.  
  79. This snippet may look intimidating to someone who is new to the IRC protocol and how IRC really
  80. works, so for the sake of this tutorial we will not be explaining how and why
  81.  
  82. Code:
  83. irc_user_nick = recv.split ( '!' ) [ 0 ] . replace ( ':', '' )
  84.         irc_user_host = recv.split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
  85.         irc_user_message = ''.join ( recv.split ( ':' ) [ 2: ] ).replace("\r\n", "")
  86.  
  87. Works, but we will be going over the rest of it.
  88.  
  89. As I mentioned previously,
  90.  
  91. When a user talks into a channel the IRC server sends back messages to all its clients in the form of
  92.  
  93. :nick!host PRIVMSG #channel: message
  94.  
  95. so the snippet aboves recieves responses from the IRC server, and then looks for the string "PING"
  96. as our bot must respond to a server ping to stay connected to the server. Then our bot checks to see
  97. if the response holds the string "PRIVMSG" which means someone is talking, and then uses that
  98. semi-confusing snippet to parse the nick of the person talking, irc_user_nick, the host,
  99. irc_user_host and the message they had sent, irc_user_message. Then after getting the information
  100. the bot prints the nick, host, and message back out to the console.
  101.  
  102. Next we will be checking if our bot's nickname was said, if so we will send the person a greeting :D
  103.  
  104. Code:
  105. """
  106.  
  107.         if recv.find( irc_nick ) != -1:
  108.             irc_sock.send(("PRIVMSG %s :Hello %s!\r\n") % (irc_channel, irc_user_nick))
  109.            
  110.         """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement