3
import socket

irc = 'irc.hack3r.com'
port = 6667
channel = '#chat'
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((irc, port))
sck.send('NICK supaBOT\r\n')
sck.send('USER supaBOT supaBOT supaBOT :supaBOT Script\r\n')
sck.send('JOIN #chat' + '\r\n')
data = ''
while True:
     data = sck.recv(4096)
     if data.find('PING') != -1:
        sck.send('PONG ' + data.split() [1] + '\r\n')
        print data

print sck.recv(4096)

When I connect to the server I can't JOIN a channel, I get this Error:

"451 JOIN :You have not registered"

Adam B
  • 3,775
  • 3
  • 32
  • 42
sourD
  • 457
  • 3
  • 8
  • 14

4 Answers4

11

Mike Graham is wrong. What's wrong is you send the JOIN command too early. It takes a while for the server to register your NICK and USER commands, hence the error "Nick not registered". See this reply: Python IRC bot won't join.

I would also like to encourage you to do continue learning and discovering the IRC protocol by making bots with bare sockets. Who cares your code doesn't comply entirely with RFC 1459. Hardly any server, client or bot complies 100% with the standard. But if it works, it works!

And unlike what Daenyth sais, it isn't too hard to get some great results with a bare socket IRC bot. Just read through the RFC a little and experiment!

Community
  • 1
  • 1
Synapsis
  • 111
  • 1
  • 2
  • +1 I agree with the hands on / hands in approach to learning. You'll have a deeper understanding as a result. Also bear in mind that quite a few server commands have been modified for security reasons since the RFC was written. – MattH Apr 01 '12 at 16:29
  • You usually have to wait for `422` (error MOTD missing) or `376` (end of MOTD), and then send the join. – Johannes Kuhn Mar 28 '16 at 13:17
2

It sounds like You are not registered and that is a requirement for joining that channel. You will have to register your nick and then identify before joining.

Also, trying to make an irc bot with bare sockets is not a good idea. This code does not implement RFC 1459 to a useful level and it conflates the logic of your program with your networking. Consider using a networking library (Like Twisted. twisted.words has a great implementation of the IRC protocol) or writing code that is equivalent to one. (Hint, the former is easier and quicker and less bug prone.)

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • @sourD: Mike is right. I've done the irc-bot-with-sockets thing and it's very painful. I did it as a learning experience, but even as that it's not very valuable. Stick with the library – Daenyth Sep 30 '10 at 21:21
0

Python twisted irc client

Installation

sudo yum install python-twisted-words

or

sudo apt-get install python-twisted-words

API Documentation

http://twistedmatrix.com/documents/8.2.0/api/twisted.words.protocols.irc.IRCClient.html

Example

#!/usr/bin/env python2.7

from twisted.internet import reactor, protocol
from twisted.words.protocols import irc

class IRCLogger(irc.IRCClient):
    logfile = file('/tmp/freenode.txt', 'a+')

    nick = 'davey_jones_logger'

    def signedOn(self):
        self.join('#scala')

    def privmsg(self, user, channel, message):
        print "Got msg %s " % message
        self.logfile.write(" %s said %s \n" % ( user.split('!')[0], message ))
        self.logfile.flush()

def main():
    f = protocol.ReconnectingClientFactory()
    f.protocol = IRCLogger
    reactor.connectTCP('irc.freenode.net', 6667, f)
    reactor.run()

if __name__ == '__main__':
    main()
Bryan Hunt
  • 3,685
  • 2
  • 24
  • 36
0

The particular channel you're trying to join requires you to be registered with the nickserv for that server. Try going on the server with a regular IRC client and creating a channel yourself, and tell the bot to join that.

Mike Trpcic
  • 25,305
  • 8
  • 78
  • 114