Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- To start off our python bot, we will need to import socket, and sys aswell as add our hashbang
- Code:
- """
- #!/usr/bin/env python
- import socket, sys
- """
- Now that we have imported the needed modules we can start on the real code, first off we need
- to specify a few variables for the bot to work and function how we want it to.
- Code:
- """
- irc_host = "irc.malvager.com"
- irc_port = 6667
- irc_channel = "#Botland"
- irc_nick = "MalvagerBot"
- """
- You should replace the variables for your own settings, these are essential to the bot's function
- aswell as connecting to the server, which is the next step.
- Code:
- """
- try:
- irc_sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
- irc_sock.connect ((irc_host, irc_port))
- except:
- print ("[Malvager Bot]Error: Could not connect to IRC; Host: %s Port: %i") % (irc_host, int(irc_port))
- sys.exit(1)
- print ("[Malvager Bot]Connected to the IRC; Host: %s Port: %i") % (irc_host, int(irc_port))
- """
- This small snippet creates our TCP socket and attempts to connect to the IRC, if it encounters any
- errors connecting, it will display an error message and exit, if all goes well it will tell you so
- and continue on. Our next step is to set some server side IRC variables, like your user and nick.
- Code:
- """
- irc_sock.send (( "NICK %s \r\n") % (irc_nick))
- print ("...Setting bot nick to %s") % (irc_nick)
- irc_sock.send (( "USER %s 8 * :X\r\n" ) % (irc_nick))
- print ("...Setting User")
- irc_sock.send (( "JOIN %s \r\n" ) % (irc_channel))
- print ("...Joining channel %s") % (irc_channel)
- """
- This sends information back to the IRC, to set our bot's nickname, user, and the channel we
- would like our bot to join. Now onto the hardest part, the IRC loop and parsing IRC messages.
- When a user talks into a channel the IRC server sends back messages to all its clients in
- the form of
- :nick!host PRIVMSG #channel: message
- so the next snippet will be used to sort out who is talking, what they are saying and
- where are they connecting from ^__^
- Code:
- """
- while True:
- recv = irc_sock.recv( 4096 )
- if recv.find ( 'PING' ) != -1:
- irc_sock.send ( 'PONG ' + recv.split() [ 1 ] + '\r\n' )
- if recv.find ( 'PRIVMSG' ) != -1:
- irc_user_nick = recv.split ( '!' ) [ 0 ] . replace ( ':', '' )
- irc_user_host = recv.split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
- irc_user_message = ''.join ( recv.split ( ':' ) [ 2: ] ).replace("\r\n", "")
- print ( "Nick:%s \nHost:%s \nMessage:%s" ) % (irc_user_nick, irc_user_host, irc_user_message)
- """
- This snippet may look intimidating to someone who is new to the IRC protocol and how IRC really
- works, so for the sake of this tutorial we will not be explaining how and why
- Code:
- irc_user_nick = recv.split ( '!' ) [ 0 ] . replace ( ':', '' )
- irc_user_host = recv.split ( '@' ) [ 1 ] . split ( ' ' ) [ 0 ]
- irc_user_message = ''.join ( recv.split ( ':' ) [ 2: ] ).replace("\r\n", "")
- Works, but we will be going over the rest of it.
- As I mentioned previously,
- When a user talks into a channel the IRC server sends back messages to all its clients in the form of
- :nick!host PRIVMSG #channel: message
- so the snippet aboves recieves responses from the IRC server, and then looks for the string "PING"
- as our bot must respond to a server ping to stay connected to the server. Then our bot checks to see
- if the response holds the string "PRIVMSG" which means someone is talking, and then uses that
- semi-confusing snippet to parse the nick of the person talking, irc_user_nick, the host,
- irc_user_host and the message they had sent, irc_user_message. Then after getting the information
- the bot prints the nick, host, and message back out to the console.
- Next we will be checking if our bot's nickname was said, if so we will send the person a greeting :D
- Code:
- """
- if recv.find( irc_nick ) != -1:
- irc_sock.send(("PRIVMSG %s :Hello %s!\r\n") % (irc_channel, irc_user_nick))
- """
Add Comment
Please, Sign In to add comment