def __init__(self, parent, event): self.args = event.arguments()[0].split()[1:] self.parent = parent self.nick = nm_to_n(event.source()) self.name = nm_to_u(event.source()).strip('~') self.channel = event.target() self.__setup__()
def handler(self, **args): """votes on a poll.""" import database from irclib import Event from irclib import nm_to_h, nm_to_u target=args["channel"] if args["type"] == "privmsg": from irclib import nm_to_n target=nm_to_n(args["source"]) poll_num = args["text"].split()[2] option_key = " ".join(args["text"].split()[3:]) #first we check to make sure the requested poll exists. if database.doSQL("select count(poll_num) from poll where poll_num =" +poll_num)[0][0] == 0: return Event("privmsg", "", target, [ "No such poll." ]) #now we check to make sure the user hasn't voted in that poll checkmask = "%!" + nm_to_u(args["source"]) + "@%" + nm_to_h(args["source"])[nm_to_h(args["source"]).find("."):] if database.doSQL("select count(option_key) from poll_votes where poll_num =" + poll_num + " and voter_nickmask like '" + checkmask + "'")[0][0] != 0: return Event("privmsg", "", target, [ "You already voted in that poll." ]) #next we check to make sure the poll option exists. option_key = self.sqlEscape(option_key) if database.doSQL("select count(option_key) from poll_options where poll_num =" + poll_num + " and option_key = '" + option_key + "'")[0][0] == 0: return Event("privmsg", "", target, [ "No such poll option." ]) # register the vote. database.doSQL("insert into poll_votes values('" + args["source"] + "', '" + option_key + "', " + poll_num + ")") return Event("privmsg", "", target, [ "Vote registered." ])
def on_action(self, serv, ev): channel = ev.target() author = irclib.nm_to_n(ev.source()) message = ev.arguments()[0] self.__prompt('channel', '{%welcome%}*{author} {action}*{%default%}', channel = channel, author = self.__user_mode(author)+author, action = self.__safe_prompt(message)) if author in self._stat: self._stat[author].update(message) else: self._stat.update({author:Stat(irclib.nm_to_u(ev.source()), 0, 0, 0, 0, [])}) self._stat[author].update(message)
def on_pubmsg(self, serv, ev): message = ev.arguments()[0] author = irclib.nm_to_n(ev.source()) channel = ev.target() self.__prompt('pubmsg', self.__safe_prompt(message), author = self.__user_mode(author)+author, channel = channel) if author in self._stat: self._stat[author].update(message) else: self._stat.update({author:Stat(irclib.nm_to_u(ev.source()), 0, 0, 0, 0, [])}) self._stat[author].update(message)
def on_join(self, serv, ev): author = irclib.nm_to_n(ev.source()) channel = ev.target() if author == self._name: self.__connected = True self.__prompt('host', '{%welcome%}Join {channel} on {server} !{%default%}',server = self._server, channel = channel) info = self.channels.get(channel, None) if not info is None: if len(info.users()) <= 1: users = ['%s%s' % (self.__user_mode(aut),aut) for aut in info.users()] self.__prompt('host', '{users} are on line !', users = ', '.join(users)) else: self.__prompt('host', 'You have no friend !') else: self.__prompt('host', '{%welcome%}{mode}{author} enter !{%default%}', mode = self.__user_mode(author), author = author) if not author in self._stat.keys(): self._stat.update({author:Stat(irclib.nm_to_u(ev.source()), 0, 0, 0, 0, [])})
def on_pubmsg(self, c, eventlist, ispubmsg=True): arg = eventlist.arguments()[0] nick = irclib.nm_to_n(eventlist.source()) user = irclib.nm_to_u(eventlist.source()) host = irclib.nm_to_h(eventlist.source()) if arg[0] == self.cmdchar: cmd = arg[1:].split(' ')[0] info = { "nick": nick, "user": user, "host": host, "pubmsg": ispubmsg, "target": eventlist.target(), "source": eventlist.source(), "args": arg[1:].split(' ')[1:] } self.builtins.get(cmd, self.dispatch)(info) elif arg[0] == self.factchar: info = { "nick": nick, "user": user, "host": host, "pubmsg": ispubmsg, "target": eventlist.target(), "source": eventlist.source(), "factoid": arg[1:].split(' ')[0], "args": arg[1:].split(' ')[1:] } self.do_factoid_lookup(info) #update the seen database seen = open("Seen.db", "r") lines = [] updated = False for line in seen.read().split('\n'): parsed = line.split(',') if parsed[0] == eventlist.target(): if parsed[1] == nick: t = datetime.datetime.now() nline = eventlist.target() + "," + nick + "," + \ str(time.mktime(t.timetuple()) + 1e-6*t.microsecond) + "," + "<" \ + nick + "> " + arg updated = True lines.append(nline) break lines.append(line) if updated is False: #add a new line t = datetime.datetime.now() nline = eventlist.target() + "," + nick + "," + str(time.mktime(t.timetuple()) + 1e-6*t.microsecond) + "," + "<" + nick + "> " + arg lines.append(nline) seen.close() #Yes this is not the most efficient way to do this by far, but #everything is in memory so I defy someone to come up with a way that #is significantly faster in practice seen = open("Seen.db", "w") for line in lines: seen.write(line + "\n") seen.close()