-1

I Am trying to make a very simple irc bot in python, but can't figure out how to do that with sockets. The only thing i want it to is: Connect to irc server, set nick (and name), join a channel, and write a message to the irc channel.

Anyone who can lead me in the right direction or small example of code?

import socket

name = raw_input("Enter your name: ")
nick = raw_input("IRC Handle: ")

irc_serv = "irc.freenode.net"
port = 6667
conn = socket.socket()
conn.connect((irc_serv, port))

conn.send("NICK "), nick
conn.send("NAME "), nick
conn.send("JOIN #king")
conn.send("PRIVMSG #king test message")

I Run it, enters name and nick, but it doesnt join the channel (checking with xchat connected to the channel)

Stian Olsen
  • 1,939
  • 3
  • 13
  • 9
  • @J.Steen I Have tried, give me a second and i'll paste what i have so far. – Stian Olsen Jul 24 '12 at 08:36
  • 2
    _Specific_ issues. Like, why isn't this socket connecting, or why is my encoding returning wonky characters. =) – J. Steen Jul 24 '12 at 08:37
  • You're not actually sending `nick`? `conn.send('NICK {}').format(nick)` is probably what you want. My RFC1459 is rusty though (been 15 years since I had to write a bot) – Jon Clements Jul 24 '12 at 08:43
  • @JonClements you probably mean `conn.send('NICK {}'.format(nick))`. However, this is the place I would check the content of `nick` to prevent malicious input. – eumiro Jul 24 '12 at 08:47
  • possible duplicate of [IRC Python Bot: Best Way](http://stackoverflow.com/questions/1100840/irc-python-bot-best-way) – sloth Jul 24 '12 at 08:47
  • @eumiro Yep, I did mean that. Although you could validate `nick` per RFC, I might be tempted to just do a 'sensible' length check, and let the server reject it (in case it's a non-compliant server) – Jon Clements Jul 24 '12 at 08:53

1 Answers1

2

As J. Steen pointed out, your question is too broad for this site and there is no single answer.

Here is some starters advice :

You are using python. Python has a lot of libraries made to help you with a lot of stuff. First try to find out if what you require is already done by some one. Don't reinvent the wheel.

In your case I can point you to the Twisted which has a implementation of the IRC protocol. Twisted is a well written, well maintained, modular and asynchronous framework and I would suggest you use it.

Here is the Twisted IRC Client API Docmentation.

Here is an example IRC bot implemented with twised. You can look into it to start out.

It seems you are starting out so I helped out, but next time you can try google. This site generally doesn't support spoon feeding and user are advised to try stuff on there own before asking for help, this leads to better learning.

Best of luck for you project.

PS : This question might already have been asked and answered here, all the more reason you should search first.

Community
  • 1
  • 1
Akshet
  • 533
  • 5
  • 16