Saturday, July 21, 2012

Making an IRC Bot

A common question I get from people I know is about making an IRC bot. The problem I run into with people trying to do this is they are usually very new to programming and half the time they are struggling with the basics of simply how to write a program. So before getting into more detail, there are some prerequisites you need to know first.
  • Understanding the programming language syntax you want to use
  • Basic programming
  • Networking knowledge of basic sockets (nothing in depth but more so how network communication should be modeled)
  • Knowledge of the IRC protocol raw (access to an RFC is very useful)

Now f you are still reading, I will assume you at the very least plan on doing the research before actually trying anything. For this, I will be writing it in Python as it is very simple and easy to do. So let us work through this piece by piece.

The first thing we want to do is run our imports so we can have all the modules we need to work with. Rather than using some third party IRC module, we will work with raw sockets because I can and it gives more insight as to what is going on. We need the module for sockets, and regular expressions. We will also import time and I'll get to why later.

import socket
impor re
import time

So now that we have the packages we need, now we need to create a socket object. With a socket object, we can establish a connection over TCP/IP, among other types of socket connections. For our purposes we will use IPv4 and a stream. For this we use the variables AF_INET and SOCK_STREAM. The AF in AF_INET stands for "address family." Now let's create the socket.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Now we have the socket object s. In another post I will explain how to put an SSL wrapper on it to use a secure connection. As for now, just a socket will be used. Now we need to connect to a server.

s.connect((HOST, PORT))

Here the connection info is passed as a tuple, so yes the parentheses are on purpose. In this, HOST will be a string of either the IP address or domain and PORT is the port number you will connect to. Now that the connection is established, we need to send identifying information for the IRC. To simplify things we will have the program sleep for a second as some servers do not let you authenticate immediately and instead send a signal for it. However I do not feel like writing out something to check for that signal right away.

s.send("NICK MyIRCbotNick\r\n")
s.send("USER MyIRCbotNick PY :Python Bot\r\n")

Okay, so now that we've authenticated, we will sleep again then join a channel because yet again, cannot join right away.

s.send("JOIN #somechannel\r\n")

Now we set up the loop to read the server data. Because this will go on indefinitely, we will use an infinite loop.

while 1:
    data = "" # Reset data each time
    while data.find("\n") == -1:
        data += s.recv(1) # Receives data 1 byte at a time
    data = data.strip() # No need for leading or trailing whitespace
    print data
    # Watch for and respond to server pings
    if"^PING", data):
        s.send("PONG" + data[4:])

Now you have all the code needed to connect to an IRC server, join a channel and stay connected. A few things to keep in mind. The socket is blocking, so the bot will wait on the recv() until it gets some data. Without blocking the program would be too CPU intensive without some extra work. Also note that printing data will print raw IRC data, which is a lot different than the messages you see in a client, and will include control characters like char 1 and 3. Furthermore, there is no error handling or autojoining should the bot be kicked. This is simply a small and poorly made IRC bot that is to demonstrate what is needed to connect to an IRC and I do not recommend this design as a base for a larger scale bot that will be used more often. Sometime later I will divulge a more robust bot that has the purpose of logging an IRC channel or channels.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)