Code:
# Lena - IRCbot
# by Danilo Vidovic
import os, sys, socket, string, random
debug = 1
PARSE_MSG_LEFT = 0
PARSE_MSG_RIGHT = 1
PARSE_MSG_FULL = 2
class bot:
sock = None
host = ''
port = 6667
gnum = None
def parseMessage(self, data, r):
loc = string.index(data,':') + 1
if r == PARSE_MSG_LEFT: return data[:loc-1]
if r == PARSE_MSG_RIGHT: return data[loc:-2]
if r == PARSE_MSG_FULL: return data[:-2]
def connect(self, host = '', port = 6667):
if host: self.host = host
if port: self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
if debug: print "I've connected to",host,port
def register(self,nick,user):
file = self.sock.makefile()
line=':'
while(self.parseMessage(line, PARSE_MSG_RIGHT) != '*** Checking Ident'):
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
if(self.parseMessage(line, PARSE_MSG_LEFT)=='ERROR'):
exit
self.sock.send('PASS 123\n')
self.sock.send('NICK '+nick+'\n')
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
self.sock.send('PONG '+self.parseMessage(line, PARSE_MSG_RIGHT)+'\n')
self.sock.send('USER '+user+'\n')
while(line[-8:-3] != '/MOTD'):
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
file.close();
def join(self,channel):
self.sock.send('JOIN '+channel+'\n')
self.sock.send('PRIVMSG '+channel+' :Cao!\n')
def perform(self, line, loc):
#Commands and Parameters
usr = line[1:string.index(line,"!")]
lasti=loc+1
clist=[]
tempstr=''
for i in line[loc+1:-2]:
if(i==' '):
clist.append(tempstr)
tempstr=''
else:
tempstr+=i
clist.append(tempstr)
command=''
#SAY
if(clist[0]=="say"):
command='PRIVMSG '+clist[1]+' :'
for i in range(2,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#LEAVE
if(clist[0]=="quit"):
command='QUIT :'
for i in range(1,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#JOIN
if(clist[0]=="join"):
command='JOIN '
for i in range(1,len(clist)): command+=clist[i]
command+='\n'
self.sock.send(command)
#PART
if(clist[0]=="leave"):
command='PART '
for i in range(1,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#GUESS
if(clist[0]=="guess"):
if(clist[2])=="restart":
self.gnum = random.randint(1,1000)
self.sock.send('PRIVMSG '+clist[1]+' :E, zamislila sam jedan broj od 1 do 1000...pogodite koji :)\n')
else:
if(clist[2]==str(self.gnum)):
self.sock.send('PRIVMSG '+clist[1]+' :Bravo '+usr+'!!! Vec sam zamislila novi broj :)\n')
self.gnum = random.randint(1,1000)
else:
if(int(clist[2])>self.gnum):
self.sock.send('PRIVMSG '+clist[1]+' :'+usr+', probaj manji broj od '+str(clist[2])+' :)\n')
else:
self.sock.send('PRIVMSG '+clist[1]+' :'+usr+', probaj veci broj od '+str(clist[2])+' :)\n')
def wait(self):
file = self.sock.makefile()
while(1):
line = file.readline()
#PING PONG
if(line[:4] == 'PING'):
if debug: print line
if debug: print 'PONG '+line[5:]
self.sock.send('PONG '+line[5:]+'\n')
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
loc = -1
try:
loc = string.index(line, '`')
except:
pass
if(loc > -1): self.perform(line, loc)
file.close()
lena = bot()
lena.connect('chat.mojhost.org')
lena.register('Lena','Lena Lena Lena :Cute female bot :)')
lena.join('#deveta')
lena.wait()
# Lena - IRCbot
# by Danilo Vidovic
import os, sys, socket, string, random
debug = 1
PARSE_MSG_LEFT = 0
PARSE_MSG_RIGHT = 1
PARSE_MSG_FULL = 2
class bot:
sock = None
host = ''
port = 6667
gnum = None
def parseMessage(self, data, r):
loc = string.index(data,':') + 1
if r == PARSE_MSG_LEFT: return data[:loc-1]
if r == PARSE_MSG_RIGHT: return data[loc:-2]
if r == PARSE_MSG_FULL: return data[:-2]
def connect(self, host = '', port = 6667):
if host: self.host = host
if port: self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
if debug: print "I've connected to",host,port
def register(self,nick,user):
file = self.sock.makefile()
line=':'
while(self.parseMessage(line, PARSE_MSG_RIGHT) != '*** Checking Ident'):
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
if(self.parseMessage(line, PARSE_MSG_LEFT)=='ERROR'):
exit
self.sock.send('PASS 123\n')
self.sock.send('NICK '+nick+'\n')
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
self.sock.send('PONG '+self.parseMessage(line, PARSE_MSG_RIGHT)+'\n')
self.sock.send('USER '+user+'\n')
while(line[-8:-3] != '/MOTD'):
line = file.readline()
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
file.close();
def join(self,channel):
self.sock.send('JOIN '+channel+'\n')
self.sock.send('PRIVMSG '+channel+' :Cao!\n')
def perform(self, line, loc):
#Commands and Parameters
usr = line[1:string.index(line,"!")]
lasti=loc+1
clist=[]
tempstr=''
for i in line[loc+1:-2]:
if(i==' '):
clist.append(tempstr)
tempstr=''
else:
tempstr+=i
clist.append(tempstr)
command=''
#SAY
if(clist[0]=="say"):
command='PRIVMSG '+clist[1]+' :'
for i in range(2,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#LEAVE
if(clist[0]=="quit"):
command='QUIT :'
for i in range(1,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#JOIN
if(clist[0]=="join"):
command='JOIN '
for i in range(1,len(clist)): command+=clist[i]
command+='\n'
self.sock.send(command)
#PART
if(clist[0]=="leave"):
command='PART '
for i in range(1,len(clist)): command+=clist[i]+' '
command+='\n'
self.sock.send(command)
#GUESS
if(clist[0]=="guess"):
if(clist[2])=="restart":
self.gnum = random.randint(1,1000)
self.sock.send('PRIVMSG '+clist[1]+' :E, zamislila sam jedan broj od 1 do 1000...pogodite koji :)\n')
else:
if(clist[2]==str(self.gnum)):
self.sock.send('PRIVMSG '+clist[1]+' :Bravo '+usr+'!!! Vec sam zamislila novi broj :)\n')
self.gnum = random.randint(1,1000)
else:
if(int(clist[2])>self.gnum):
self.sock.send('PRIVMSG '+clist[1]+' :'+usr+', probaj manji broj od '+str(clist[2])+' :)\n')
else:
self.sock.send('PRIVMSG '+clist[1]+' :'+usr+', probaj veci broj od '+str(clist[2])+' :)\n')
def wait(self):
file = self.sock.makefile()
while(1):
line = file.readline()
#PING PONG
if(line[:4] == 'PING'):
if debug: print line
if debug: print 'PONG '+line[5:]
self.sock.send('PONG '+line[5:]+'\n')
if debug: print self.parseMessage(line, PARSE_MSG_FULL)
loc = -1
try:
loc = string.index(line, '`')
except:
pass
if(loc > -1): self.perform(line, loc)
file.close()
lena = bot()
lena.connect('chat.mojhost.org')
lena.register('Lena','Lena Lena Lena :Cute female bot :)')
lena.join('#deveta')
lena.wait()