import brathuhn, datetime, time from StringIO import StringIO from persistent import PersistentContainer import config pcontainer = PersistentContainer(__name__,config.PCACHE) if not "vote_pending" in pcontainer: pcontainer["vote_pending"] = False def new_vote(args): if pcontainer["vote_pending"]: brathuhn.sendMsg(brathuhn.ROOM,"There's already a voting going on!",'groupchat') return #args[0] = "!vote", args[1] = new, args[2] = topic, args[3...n] = options pcontainer["topic"] = args[2] pcontainer["votes"] = dict() tmp = dict() for i in args[3:]: tmp[i] = 0 pcontainer["options"] = tmp pcontainer["vote_pending"] = True brathuhn.sendMsg(brathuhn.ROOM,'Hear, hear folks of %s! Thou\'ve been called to a voting on "%s".' % (brathuhn.ROOM, args[2]),'groupchat' ) def attend_vote( mfrom ,args): #args[0] = "!vote", args[1] = attend, args[2] = option if not pcontainer["vote_pending"]: brathuhn.sendMsg(brathuhn.ROOM,"There is no vote at the moment.",'groupchat') return if mfrom in pcontainer["votes"]: brathuhn.sendMsg(brathuhn.ROOM,"You already voted!",'groupchat') return tmp = pcontainer["options"] if not args[2] in tmp: brathuhn.sendMsg(brathuhn.ROOM,'I don\'t understand your option!','groupchat') return tmp[args[2]] += 1 pcontainer["options"] = tmp tmp = pcontainer["votes"] tmp[mfrom] = True pcontainer["votes"] = tmp brathuhn.sendMsg(brathuhn.ROOM,'Thank you for your vote!','groupchat') def vote_statistics(): if not pcontainer["vote_pending"]: brathuhn.sendMsg(brathuhn.ROOM,"There is no vote at the moment.",'groupchat') return tmp = pcontainer["options"] brathuhn.sendMsg(brathuhn.ROOM, 'There is a vote on "'+pcontainer["topic"]+'"','groupchat' ) brathuhn.sendMsg(brathuhn.ROOM, 'Votes so far:','groupchat' ) for k in tmp: brathuhn.sendMsg(brathuhn.ROOM,' %s: %d' % (k, tmp[k]),'groupchat' ) def end(): vote_statistics() pcontainer["topic"] = None pcontainer["options"] = None pcontainer["vote_pending"] = False def usage(args): brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" (new topic option1 option2 ... option_n|attend num|show|end)",'groupchat') def status_logger(mfrom,msg,status): if pcontainer["vote_pending"] and status==brathuhn.STATUS_RENAME and mfrom.getResource() in pcontainer["votes"]: tmp = pcontainer["votes"] tmp[msg.getNick()] = tmp[mfrom.getResource()] del tmp[mfrom.getResource()] pcontainer["votes"] = tmp brathuhn.sendMsg(brathuhn.ROOM,"%s, the vote comissioner is watching you!" % msg.getNick() ,'groupchat' ) def vote(msg,args): if len(args)<2: return usage(args) if args[1] == "new": if len(args)<5: return usage(args) new_vote(args) elif args[1] == "attend": if len(args)!=3: return usage(args) attend_vote( unicode(msg.getFrom().getResource()) ,args) elif args[1] == "show": if len(args)!=2: return usage(args) vote_statistics() elif args[1] == "end": if len(args)!=2: return usage(args) end() else: return usage(args) brathuhn.addCommand("vote","Usage: !vote (new option1 option2 ... option_n|attend num|show|stats)",vote) brathuhn.addStatusListener(status_logger)