Beginners Programming Challenge #21
To create an automated RFC-2812 client... more commonly known as an IRC Bot.
This challenge will be an exercise primarily in client-server communications and reading a standards document. Using the protocal defined in RFC 2812 your objective will be to create a client (a bot) that responds to messages sent out by the server. For example, you could create a simple bot that greets people as they enter a channel, or a complicated bot that generates somewhat-coherent phrases using markov chains and machine learning. Whether simple or complicated, all IRC bots need to be able to connect to a server, set a nick, and join a channel. For non-passworded servers, this is as simple as sending a NICK immediately followed by a JOIN once a connection to a server has been opened.
For the purposes of this contest, have your bot hang out on the #ufpc channel on the chat.freenode.net server. You should see either me (Queue29) or my bot (cbot) once you connect (and maybe a few other challengers as well).
Intermediate and Advanced programmers: don't read this, read RFC 2812. It contains everything you need to know. This section is for beginners who maybe have never opened a connection or even heard of Internet Chat Relay. Instead of having you guys scour the 'net for old sample code that conforms to the old standard, I am including a working template that demonstrates the fundamentals of the protocal:
# original source: http://www.osix.net/modules/article/?id=780
# (updated to the new standard)
s = socket.socket()
s.send('NICK ' + NICK + '\r\n')
s.send('USER ' + IDENT + ' 0 * :' + RNAME + '\r\n')
line = s.recv(1024)
print ">> " + line
if 'MOTD command' in line:
print '<< sending join...'
s.send('JOIN ' + CHANNEL + '\r\n')
elif 'JOIN' in line:
print '<< sending welcome...'
s.send('PRIVMSG ' + CHANNEL + ' :Welcome!\r\n')
elif 'PING' in line:
print'<< sending pong...'
s.send('PONG ' + CHANNEL + '\r\n')
So what's going on? First up, we open a connection to the server using plain sockets. You can think of the HOST as the url of the server, in our case it will be chat.freenode.irc . The port is the port number- for non SSL, pick one of 6665, 6666, 6667, 8000, 8001, 8002. Then we create the connection (super easy in python!). The first two messages we send to the server are very important. As designated in RFC 2812 section 3.1, the first two messages to be sent to the server should be 'NICK <params>' followed by 'USER <params>' unless there is a password, in which case 'PASS <password>' goes first. The server/channel we are using is not password protected. Now this is important, and many of you are going to be confused by this: all messages sent to the server must end in Windows style carriage returns: '\r\n' instead of the usual '\n' . It's a part of the standard, and your bot really won't work if you forget to do that!
You should note that all messages contain 1, 2, or 3 parts, delimted (awkardly, in my opinion) by space characters and : characters. Section 2.3.1 covers this in detail, but you can get away with reading the format examples in the document under each command's explanation. For example, in the NICK section, you are given the examples:
So now that the bot is connected to the server, we need to join an actual channel. First, we should wait until the server stops spamming us with messages. A good indication of this is when the server sends a message like: :holmes.freenode.net 376 ezbot :End of /MOTD command. In my example bot, when the variable line contains "MOTD command", it sends a JOIN command. The join command will ultimately look like "JOIN #ufpc \r\n" .
NICK Wiz ; Introducing new nick "Wiz" if session is
still unregistered, or user changing his
nickname to "Wiz"
:WiZemail@example.com NICK Kilroy
; Server telling that WiZ changed his
nickname to Kilroy.
Lastly in my example bot is a mechanism for letting the server know the bot is still alive. Every once in a while, the server will send a PING message (section 3.7.2) and you must send a PONG message in response. If you don't, the server will disconnect you.
- Have your bot connect using SSL: chat.freenode.net SSL ports include 6697, 7000, and 7070.
- Be creative! There are many things you can get a bot to do, so make yours do useful and interesting things to
- Have your bot send messages in COLOR
First, try to read and understand RFC 2812. That really is one of the goals of this challenge. But of course you can always find help here, or on #ufpc! You can also check out the source code for cbot here.
Judging will be based mostly on good design: This project is great because there is _tons_ of room for modularizing your code. Giant python scripts with no classes and functions make me sad. You don't want me to be sad, do you? Usefulness and Creativity will also be factors. There are numerous 'sample' bots that kind of work but don't really do much. You should make your bot stand out by doing purposeful things like.. replying with weather reports, detecting floods, keeping track of karma, and whatever else you can think of.
Have fun! Judging will commence in a few weeks, or when the entries stop coming in.