views:

472

answers:

5

Hi every one. I need help writing a basic IRC bot that just connects to a channel.. is anyone able to explain me this? I have managed to get it to connect to the IRC server but i am unable to join a channel and log on. The code i have thus far is:

import sockethost = 'irc.freenode.org'
port = 6667
join_sock = socket.socket()
join_sock.connect((host, port))
<code here> 

Any help would be greatly appreciated.

+5  A: 

It'd probably be easiest to base it on twisted's implementation of the IRC protocol. Take a look at : http://github.com/brosner/bosnobot for inspiration.

Alex Gaynor
http://www.habnabit.org/twistedex.html is a tutorial basic IRC bot using t.w.p
Mike Graham
A: 

That will open a socket, but you also need to tell the IRCd who you are. I've done something similar in perl ages ago, and I found the IRC RFCs to be very helpful.

Main RFC: http://irchelp.org/irchelp/rfc/rfc.html

Other RFCs: http://irchelp.org/irchelp/rfc/index.html

Daenyth
A: 

A little googling will help a lot I found this tutorial and several others like it.

Hope this helps, Gale

GEShafer
Yeah found that one too... And lots of other ones.. Even looked for perl ones to get an idea of the whole thing.. but for some reason it doesnt work.. But thanks. Am raelly happy because of all the feedback.
Jake
This one seems terrible
nosklo
+1  A: 

This article shows you how to make a simple IRC bot in Python:
http://www.osix.net/modules/article/?id=780

Corey Goldberg
+2  A: 

To connect to an IRC channel, you must send certain IRC protocol specific commands to the IRC server before you can do it.

When you connect to the server you must wait until the server has sent all data (MOTD and whatnot), then you must send the PASS command.

PASS <some_secret_password>

What follows is the NICK command.

NICK <username>

Then you must send the USER command.

USER <username> <hostname> <servername> :<realname>

Both are mandatory.

Then you're likely to see the PING message from server, you must reply to the server with PONG command every time the server sends PING message to you. The server might ask for PONG between NICK and USER commands too.

PING :12345678

Reply with the exact same text after "PING" with PONG command:

PONG :12345678

What's after PING is unique to every server I believe so make sure you reply with the value that the server sent you.

Now you can join a channel with JOIN command:

JOIN <#channel>

Now you can send messages to channels and users with PRIVMSG command:

PRIVMSG <#channel>|<nick> :<message>

Quit with

QUIT :<optional_quit_msg>

Experiment with Telnet! Start with

telnet irc.example.com 6667

See the IRC RFC for more commands and options.

Hope this helps!

TheMagician
Thanks, this is GREAT! Especially the tip about telnet.. Didn't even think of it :) Thanks.. I might have a few more questions.. Let me try out the telnet thing then i will get back!
Jake
This is my session:NOTICE AUTH :*** Processing connection to irc.mzima.netNOTICE AUTH :*** Looking up your hostname...NOTICE AUTH :*** Checking IdentNOTICE AUTH :*** Found your hostnameNOTICE AUTH :*** No Ident responseNICK PYIRC\r\nUSER PYIRC PYIRC PYIRC :Python\r\nJOIN #pytest\r\n:irc.mzima.net 451 * :You have not registeredIt seems to be the registration.. How do i regisster? Or do you know an IRC server that does not require that?i am lost................
Jake
I don't think you can send the commands simultaneously like that. Try sending them separately, because the server might send you something in between the commands, thus you get the notice on registration, that actually refers to connection registration. Apparently there is a PASS <some_password> command you must also send before NICK/USER, I have never had to use that command myself before, so try that, I updated my post.
TheMagician
I see. I am sorry for the sloppy session paste. I was sending them one at a time.. eg:NICK Test\r\nUSER ....\r\nand so on. I wonder how to find the password..
Jake
How to i register my connection??
Jake
Jake, the password can be anything. You register the connection by sending the PASS, NICK and USER commands to the server. Try it with telnet, irc.quakenet.org 6667 works fine.
TheMagician
Yeah the telnet connection works fine. So at least i got a bit further. But the problem is, if i try to do the same in python. it doesnt work. Maybe python isnt the best language to do this in. though my first though was "python can do this!". It seems perl might be more appropriate for this task, but i really want to use python.But thank you because now i can use IRC with telnet!:b Which is very nice as well.it seems like there is a step missing (maybe the ping pong?) when i try to recreate it in python it goes wrong. If i were to post my new python code would you be willing to look at it?
Jake
Jake, last time I did anything with Python was a couple of years ago. You basically want to have an infinite loop that reads from the server after you have connected. And the first thing you always want to check for is the PING. And make sure you "continue" in the loop after every command or you might miss PINGs or other data that must be read before you can send another command. Try creating a simple server and client script first on your machine and then modify the client script.
TheMagician
Thanks alot. You have helped me greatly :)
Jake