#!/usr/bin/env python nickname = 'k-witts' # the nickname of the bot ircserver = 'irc.kuht.it' # the irc server to connect to sleeptime = 45 # how much time passes between game phases ## init stuff import socket,time,thread ## internal variables , don't change them geek :> srv = socket.socket(socket.AF_INET,socket.SOCK_STREAM) owner = '' chan = '' question = '' running = 0 # 0 = not in game , 1 = waiting for phrases , 2 = waiting for votes db = [[],[],[]] phrase_hosts = [] vote_hosts = [] ## functions, do not try this at home def raw(what,who=None): if not who: srv.send(what + '\n') else: srv.send("PRIVMSG %s :%s\n" %(who,what)) def notice(nick,data): raw("NOTICE %s :%s" %(nick,data)) def join(where): raw("JOIN %s" %(where)) def part(where): raw("PART %s" %(where)) def handleprivmsg(text): global owner nickname = text[0][1:text[0].find('!')] host = text[0][text[0].find("@")+1:] if len(text[3:]) == 1: msg = text[3][1:] elif len(text[3:]) > 1: msg = text[3][text[3].find(":")+1:] + ' ' + ' '.join(text[4:]) if nickname == owner: handleownercmd(msg) else: if msg == 'hello' and owner == '': owner = nickname print "Bot successfully owned by %s" %(owner) raw("Hi %s. You are now the owner of this bot" %(nickname),nickname) else: handleusermsg(nickname,host,msg) def handlechanmsg(text): nickname = text[0][1:text[0].find('!')] host = text[0][text[0].find("@")+1:] if len(text[3:]) == 1: msg = text[3][1:] elif len(text[3:]) > 1: msg = text[3][text[3].find(":")+1:] + ' ' + ' '.join(text[4:]) def handle(what): if what: if what[0] == "PING": raw("PONG %s" %(what[1][1:])) elif len(what) > 2: if what[1] == "PRIVMSG" and what[2].lower() == nickname.lower(): handleprivmsg(what) elif what[1] == "PRIVMSG" and what[2].lower() == chan.lower(): handlechanmsg(what) def put(nick,host,phrase): global db,phrase_hosts if not host in phrase_hosts: db[0].append(nick) db[1].append(phrase) db[2].append(0) phrase_hosts.append(host) else: notice(nick,"You have already submitted a phrase") def addvote(nick,host,number): global db,vote_hosts try: n = int(number)-1 except ValueError: pass print 'valueerror' if n: print 'ok1' if not host in vote_hosts: print 'ok2' if phrase_hosts.index(host) == n: notice(nick,"you can\'t vote yourself, moron") print 'autovoto' else: print 'ok3' try: db[2][n] = db[2][n] + 1 vote_hosts.append(host) print 'voto accettato' except IndexError: print 'indexerror' pass else: notice(nick,"You have already voted!") else: pass # if you want to override these defaults values, you can do it easily here under :) def login(nickname,server,ident='doxa',realname='K.U.H.T. Witts GameBot',hostname='viperlabs'): print 'enter' srv.connect((server,6667)) raw("USER %s %s %s :%s" %(ident,hostname,hostname,realname)) raw("NICK %s" % (nickname)) def ircquit(): if running == 1: raw("There\'s a quiz running, end it before exiting with \'endpoll\'.",owner) else: raw("Bye bye!",owner) raw("QUIT :Kwitts") def gametimer(): global running,sleeptime running = 1 time.sleep(sleeptime) running = 2 beginvoting() time.sleep(sleeptime) running = 0 endquiz() # mythread = threading.Thread(target=gametimer) def ask(what,where): global question,chan chan = where question = what join(where) raw("Game started!! Question is ---> %s" %(what),where) raw("Give me your answer in query!",where) raw("You've got %s secs to answer. Enjoy!" %(str(sleeptime)),where) def beginvoting(): global db,nickname raw("Time's up. Now you have to vote these sentences typing their number to me.",chan) raw("i.e.: to vote the first sentence , just type /msg %s 1 " %(nickname),chan) raw("Sentences are :",chan) for n in range(len(db[1])): raw("4 %s  >> %s" %( str(n+1) , db[1][n] ),chan) time.sleep(0.2) raw("Good luck!",chan) def results(): global db,chan newdb = zip(db[2],db[1],db[0]) newdb.sort() newdb.reverse() raw("Game ended here on %s. Final results are: " %(chan),chan) for tupl in newdb: raw("4 %s  scored 4 %d  point(s) by writing: %s " %(tupl[2],tupl[0],tupl[1]),chan) time.sleep(0.2) raw("See you next time folks!",chan) def endquiz(): global db,running,chan,question,vote_hosts,phrase_hosts results() part(chan) running = 0 db = [[],[],[]] chan = '' question = '' vote_hosts = [] phrase_hosts = [] def start(question,chan): thread.start_new_thread(gametimer,()) ask(question,chan) def help(): pass def handleownercmd(command): if len(command.split()) >= 3: if command.split()[0] == 'startquiz': start(" ".join(command.split()[2:]),command.split()[1]) raw("Done.",owner) def handleusermsg(nick,host,text): global running if running == 0: pass elif running == 1: put(nick,host,text) elif running == 2: if len(text) == 1: addvote(nick,host,text) else: pass else: notice(nick,"Not In Game") ## runtime, brum brum! login(nickname,ircserver) ## data handling while 1: data = srv.recv(1024) text = data.split() if text: handle(text)