How to write a Python Program to Simple IRC Bot ?
Solution:
#!/usr/bin/env python
import sys
import socket
import string
import time
HOST="irc.freenode.net"
PORT=6667
NICK="metalbot"
IDENT="metalbot"
REALNAME="MetalBot"
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))
while 1:
readbuffer=readbuffer+s.recv(1024)
temp=string.split(readbuffer, "\n")
readbuffer=temp.pop( )
for line in temp:
print line
line=string.rstrip(line)
line=string.split(line)
if "/MOTD" in line:
print "Connecting..."
time.sleep(5)
s.send("JOIN #filmsbykris \r\n")
if "metalbot" in line:
print "<===========metabot==============>"
if "Linux" in line:
print "<===============Linux==========>"
s.send("PRIVMSG #filmsbykris : I think Linux is cool!!! \r\n")
if(line[0]=="PING"):
s.send("PONG %s\r\n" % line[1])