Today I am going to teach you how to program a muzzle to IRC. First of all, for those who do not know, a bot is a program that connects to an IRC channel and interacts with it, which allows us, for example, to moderate the channel without that we are connected and thus avoid spam, or that it recognizes a series of orders and executes the corresponding code.
Although there are already bots ready, I am honestly one of those who like to make their own programs to learn and the great satisfaction it gives us after seeing that it works ^^
With that said, let's move on to the tutorial.
To program the bot we will need a plain text editor any (nano, gedit, medit, etc) and the python interpreter (2.6 or 2.7 will be necessary, does not work with python 3.x).
First we import the required modules, in this case we will only need two:
[code] #! / usr / bin / env python# - * - coding: utf-8 - * -
import socket
importstring
[/ Code]
Now we proceed to set up the bot:
[code] HOST=»irc.desdelinux.net"PORT = 6667
NICK = »CalicoBot»
IDENT = »CalicoBot»
REALNAME = »CalicoBot»
CHAN = »# Home»
readbuffer = »»
[/ Code]
I will explain each variable:
- HOST: The URL of the server to which we will connect
- PORT: The server port. By default it is 6667.
- NICK, IDENT and REALNAME: They correspond to the nickname of the bot, its identification and the real name.
- CHAN: The channel the bot will enter
- readbuffer: In this variable the data sent by the server will be saved.
Once our bot is configured we proceed to the connection
[code] s = socket.socket ()s.connect ((HOST, PORT))
s.send ("NICK% s \ r \ n"% NICK)
s.send ("USER% s% s bla:% s \ r \ n"% (IDENT, HOST, REALNAME))
s.send ("JOIN:% s \ r \ n"% CHAN)
[/ Code]
The first line does not have much mystery, the second creates the server connection and the last three send the bot's data to the server to proceed with the login.
Once connected we will create a Infinite loop in which we will go receiving and sending data from / to the server:
[code] while 1:readbuffer = readbuffer + s.recv (1024)
temp = string.split (readbuffer, "\ n")
readbuffer = temp.pop ()
for line in temp:
line = string.rstrip (line)
line = line.split (CHAN + ':')
if line [0] .find ("PING")! = -1:
pingid = line [0] .split () [1] s.send ("PONG% s \ r \ n"% pingid)
[/ Code]
Of all the lines written above, I will only comment on the important ones.
With line = line.split (CHAN + ':') what we do is divide what the server sends us when let's get something from the channel.
For example the following line indicates that someone wrote something on the channel:
:[email protected] PRIVMSG #Home :Hola ^^
The first is the nickname of the user and his connection data (separated by!), The command (in this case it indicates that he wrote), the channel and finally, after the colon, the message sent. I will not explain many more commands since that does not fall within this tutorial.
The other important lines are the ones are after the if. The server every so often sends the PING command to check if the user is still connected. In that case, the bot sends the PONG command with an ID that PING sent to indicate to the server that it is still connected.
With this we already have the base of the bot. Now I will proceed to explain how to make the bot respond according to what we want to certain commands, whether they are from IRC itself or from users.
Responding to IRC commands:
PING and PRIVMSG are examples of IRC commands. There are many commands, but as I said before, it is something that I will not go into detail about.
For example, we can make the bot say hello to users who connect:
name = line [0] .split ('!') [0] .split (':') [1] if name! = NICK and name.find (HOST) == -1:
s.send ("PRIVMSG% s: Welcome @% s ^^ \ n"% (CHAN, name))
[/ Code]
First we check if the server sends the command JOIN which indicates that someone connected to the server. Then we extract the nick, we check that the nick is not the IRC url (if not as soon as we run the bot it will greet the url) and finally we send the greeting message.
Bot commands:
Now how do I make my bot respond to my own commands? Let's better look at an example:
[code] if line [1] == '$ version':s.send («PRIVMSG% s: CalicoBot 0.1.2 (c) 2012 Son Link \ n»% CHAN)
[/ Code]
In this example if someone writes $version the bot will show the message indicating its name, version and author. The complete code of the example is this:
[code] import socketimportstring
HOST = »localhost»
PORT = 6667
NICK = »CalicoBot»
IDENT = »CalicoBot»
REALNAME = »CalicoBot»
CHAN = »# Home»
readbuffer = »»
s = socket.socket ()
s.connect ((HOST, PORT))
s.send ("NICK% s \ r \ n"% NICK)
s.send ("USER% s% s bla:% s \ r \ n"% (IDENT, HOST, REALNAME))
s.send ("JOIN:% s \ r \ n"% CHAN)
while 1:
readbuffer = readbuffer + s.recv (1024)
temp = string.split (readbuffer, "\ n")
readbuffer = temp.pop ()
for line in temp:
printing line
line = string.rstrip (line)
line = line.split (CHAN + ':')
if line [0] .find ("PING")! = -1:
pingid = line [0] .split () [1] s.send ("PONG% s \ r \ n"% pingid)
if line [0] .find ('JOIN')! = -1:
name = line [0] .split ('!') [0] .split (':') [1] if name! = NICK and name.find (HOST) == -1:
s.send ("PRIVMSG% s: Welcome @% s ^^ \ n"% (CHAN, name))
if len (line) <1:
if line [1] == '$ version':
s.send («PRIVMSG% s: CalicoBot 0.1.2 (c) 2012 Son Link \ n»% CHAN)
[/ Code]
I hope you liked this tutorial, and of course, I leave you the link to the code of my bot so that you can see its code in full and you can see better how it works (although I have removed some commands for personal use).