transbot: connecting to the IRC

Hey guys, I’m here again, now with some code on how I do to connect to some IRC server using Python, on the Translation Bot (trully I found another bot with the same name, but I’ll not change it ;p ):

First, we need to add these modules:
import sys
import socket

With it, we can define some constants:

NETWORK = 'some.irc.server' # server to connect
PORT = 6667
NICK = 'transbot' # the nick used for the bot
PASSWORD = 'bot_password' # used if you register the bot nick
IDENT = 'transbot'
REALNAME = ':MDK's Translation Bot'
OWNER = 'xMDKx' # yeah, it's me on anonnet and on freenode
CHANNEL = '#some_channel' # the initial channel to join

Cool! Right now we can connect to IRC simply doing:

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((NETWORK, PORT))

And now, just to enter on some channel (and identify):

send('NICK %s' % NICK)
send('USER %s %s %s %s' % (IDENT, NICK, NICK, REALNAME))
send('JOIN %s' % CHANNEL)

Now your bot is connected to the NETWOR (server) and joined on the CHANNEL that you specify.

For just grab the messages on the channel, do it:

while True:
data = irc.recv(SOME_SIZE) # with size like 512 bytes, for ex.
print data # just print server messages

And it’s all for today, folks! Soon I’ll post some tips on how to parse the messages grabbed from the channel. See ya!