After my first attempt at writing a IRC bot purely in Python sort of worked, i decided to rewrite it and tried to add some better error handling etc. Well it won't work at all now! And i don't now why! So i'm wondering if ya'll could give it a quick glance over and see if i made any silly mistakes?
Don't worry if you've never done anything with IRC before neither have i and it's easy to understand!
- Code: Select all
HOST = "ice.coldfront.net"
PORT = 6667
CHANNEL = "#CN-Informatist"
USER = "HBot HBot HBot HBot"
NICK = "PoloBot"
import socket
e = "\r\n" # Endline string
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_buffer = [] # Stream buffer
attempts = 0
connected = 0
while connected < 4 and attempts < 5: #Start attempt connection loop
try:
irc.connect((HOST,PORT))
connect = connect+1
except:
irc.close()
print "Connection failed @ INIT"
try:
irc.send("NICK "+NICK+e)
connect = connect+1
except:
irc.close()
print "Connection failed @ send:NICK"
try:
irc.send("USER "+USER+e)
connect = connect+1
except:
irc.close()
print "Connection failed @ send:USER"
try:
irc.send("JOIN "+CHANNEL+e)
connected = connected+1
except:
irc.close()
print "Connection failed @ send:JOIN"
attempts = attempts+1
while True:
getStream()
checkPing(_buffer[0])
readBuffer(_buffer)
clearBuffer(_buffer)
def getStream():
global irc
global _buffer
_buffer.append(irc.recv(4096))
return None
def checkPing(s=str):
global irc
if s.find("PING") != -1:
irc.send("PONG" + s.split()[1]+e)
return True
else:
return False
def readBuffer(buf=list):
for item in buf:
return item
def clearBuffer(buf=list):
while len(buf) >= 0:
del buf[0]
return None
It starts by just declaring some global varibles like 'HOST', 'NICK' etc. And also created the buffer to store each 'line'.
After that it enters the 'connection loop', where it attempts to connect to the server. This is where my problems are, i think... The out put it's currently returning is:
- Code: Select all
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
Connection failed @ INIT
Connection failed @ send:NICK
Connection failed @ send:USER
Connection failed @ send:JOIN
