#!/usr/bin/env python # Doxa 0.0.1 by viper - Released under the GNU/GPL License - www.gnu.org # Usage: simply set the ircserver and the nickname and launch the script. # Then, identify yourself to the bot typing: /msg botname hello # main commands (that should be given with /msg botname command ) are: # - startpoll #channel question , to start a poll on a channel # i.e.: startpoll #foo Foo is better than foo ? # - endpoll , ends a current poll and gives results to the channel # - results , give the current results to the channel # - help (not-yet-implemented) # - quit (to make the bot quit) ## user configuration nickname = 'doxa`' # the nickname of the bot ircserver = 'irc.tin.it' # the irc server to connect to ## init stuff import socket from time import sleep ## internal variables srv = socket.socket(socket.AF_INET,socket.SOCK_STREAM) yes_voters = [] no_voters = [] owner = '' chan = '' question = '' poll = 0 ## 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': # obviously, you can change this to suit your needs if owner == '': owner = nickname print "Bot successfully owned by %s" %(owner) else: notice(nickname,"I'm a bot, don't chat with me :)") else: notice(nickname,"I'm a bot, don't chat with me :)") 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:]) if msg.lower() == "!yes": # these can be changed, too handlevote1(nickname,host) elif msg.lower() == "!no": handlevote2(nickname,host) elif msg.lower() == "!poll": give_question(nickname) else: pass 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) # if you want to override these defaults values, you can do it easily here under :) def login(nickname,server,ident='doxa',realname='Python IRC Poll by viper`',hostname='viperlabs'): srv.connect((server,6667)) raw("USER %s %s %s %s" %(ident,hostname,hostname,realname)) raw("NICK %s" % (nickname)) def handlevote1(nick,ip): if not ip in no_voters and not ip in yes_voters: yes_voters.append(ip) notice(nick,"Thanks for submitting your \'yes\' vote!") else: notice(nick,"You can\'t vote twice!") def handlevote2(nick,ip): if not ip in no_voters and not ip in yes_voters: no_voters.append(ip) notice(nick,"Thanks for submitting your \'no\' vote!") else: notice(nick,"You can\'t vote twice!") def results(): if question != '' and chan != '': raw("Poll Results for %s:" %(chan),chan) raw("Question is: %s" %(question),chan) raw("Voters for \"yes\": %s" %(len(yes_voters)),chan) raw("4,4|%s|" %('='*len(yes_voters)),chan) raw("- - - - - - - - ",chan) raw("Voters for \"no\": %s" %(len(no_voters)),chan) raw("12,12|%s|" %('='*len(no_voters)),chan) raw("- - - - - - - - ",chan) else: raw("Cannot do this, no poll was started!",owner) def ask(what,where): global poll,question,chan if poll == 1: raw("A poll is already running on %s (the question is \"%s\")" %(chan,question),owner) else: chan = where question = what join(where) raw("Doxa 0.1.0 by viper` - IRC Poll",where) raw("Today\'s Poll Is: %s" %(what),where) raw("You can vote by just typing !yes or !no here in %s" %(where),where) poll = 1 def give_question(nick): if poll == 1: notice(nick,"Today\'s Poll Is: %s" %(question)) notice(nick,"You can vote by just typing !yes or !no here in %s" %(chan)) else: notice(nick,"No Poll Running.") def endpoll(): if poll == 1: raw("Poll Ended on %s." %(chan),chan) raw("You just voted for \"%s\"." %(question),chan) sleep(2) results() reset() else: raw("Cannot do this, no poll was started!",owner) def reset(): global question,yes_voters,no_voters,chan,poll question = '' yes_voters= [] no_voters= [] part(chan) chan = '' poll = 0 def ircquit(): if poll == 1: raw("There\'s a poll running, end it before exiting with \'endpoll\'.",owner) else: raw("Bye bye!",owner) raw("QUIT :doxa - Python IRC poll by viper") def help(): pass def handleownercmd(command): print command.split() if len(command.split()) >= 3: if command.split()[0] == 'startpoll': ask(" ".join(command.split()[2:]),command.split()[1]) raw("Done.",owner) else: raw("Wrong Syntax",owner) elif command == 'help': help() elif command == 'endpoll': endpoll() raw("Done.",owner) elif command == 'results': results() raw("Done.",owner) elif command == 'quit': ircquit() raw("Done.",owner) else: raw("No Such Command, try typing \"help\" in here for a list of available commands",owner) ## runtime, brum brum! login(nickname,ircserver) ## data handling while 1: data = srv.recv(1024) text = data.split() if text: handle(text)