X-Git-Url: http://gitweb.pimeys.fr/?a=blobdiff_plain;f=parrot.py;h=dd5053023f4d0bb2ccd0943077de43bd5ad78e28;hb=ed0fde998214ef99e6c77fc9be5a51b165e98d53;hp=6229e8d4a5c4189d82a0a6945fe5d8c3a52e4e50;hpb=9cdf643cd7cb6713b470f54a09f963816101fca0;p=bots%2Fparrot.git diff --git a/parrot.py b/parrot.py index 6229e8d..dd50530 100755 --- a/parrot.py +++ b/parrot.py @@ -87,6 +87,9 @@ class Parrot(ircbot.SingleServerIRCBot): self.quotedb = quotes.QuoteDB() self.reload_quotes() + + # Pour agir à la réception des whois 307 = registered nick + self.ircobj.add_global_handler("307", self.on_registered_nick) ### Utilitaires @@ -180,6 +183,9 @@ class Parrot(ircbot.SingleServerIRCBot): """Restaure les quotes à partir du dump. ``asked_where=None`` signifie en privé.""" self.quotedb = quotes.restore() self.acknowledge(asked_by, asked_where, "Quotes restaurées à partir du dump (pas de backup effectué).") + many = self.quotedb.get_clash_authors() + if many: + self.acknowledge(asked_by, asked_where, "Auteurs de casse différente : %s" % (many)) ### Surcharge des events du Bot def on_welcome(self, serv, ev): @@ -429,7 +435,9 @@ class Parrot(ircbot.SingleServerIRCBot): match = self.quote_pattern.match(message) if match: d = match.groupdict() - if self.quotedb.store(d["author"], d["content"]): + # On n'autorise pas les gens à déclarer le quoter + d["quoter"] = auteur.decode("utf-8") + if self.quotedb.store(**d): serv.privmsg(canal, (u"%s: Ce sera retenu, répété, amplifié" % (auteur,)).encode("utf-8")) self.quotedb.save() else: @@ -446,18 +454,34 @@ class Parrot(ircbot.SingleServerIRCBot): serv.privmsg(canal, (u"Pas de quote de %s en mémoire." % author).encode("utf-8")) return serv.privmsg(canal, str(q)) - elif message.startswith(u"!author"): + elif message.startswith(u"!author") or message.startswith(u"!from"): words = message.split() cmd = words[0].lstrip("!") - regexp = cmd in ["author-r", "authorreg", "author-reg", "author-regex"] + regexp = any([cmd.endswith(suffix) for suffix in config.regex_suffixes]) search = u" ".join(words[1:]) authors = self.quotedb.search_authors(search, regexp) - if len(authors) > config.search_max_authors: - authors = authors[:config.search_max_authors+1] + [unicode(len(authors) - config.search_max_authors)] - if authors: - serv.privmsg(canal, "%s: %s" % (auteur, (u" ".join(authors)).encode("utf-8"))) + if not authors: + serv.privmsg(canal, "%s: Pas d'auteur correspondant à la recherche." % (auteur,)) + return + if cmd.startswith("author"): + if len(authors) > config.search_max_authors: + authors = authors[:config.search_max_authors+1] + ["+%s" % (len(authors) - config.search_max_authors)] + serv.privmsg(canal, "%s: %s" % (auteur, (u", ".join(authors)).encode("utf-8"))) + elif cmd.startswith("from"): + quotes = sum([self.quotedb.quotesfrom(a) for a in authors], []) + q = random.choice(quotes) + serv.privmsg(canal, str(q)) + elif message.startswith(u"!search"): + words = message.split() + cmd = words[0].lstrip("!") + regexp = cmd in ["search" + suffix for suffix in config.regex_suffixes] + search = u" ".join(words[1:]) + quotes = self.quotedb.search(inquote=search, regexp=regexp) + if quotes: + q = random.choice(quotes) + serv.privmsg(canal, str(q)) else: - serv.privmsg(canal, "%s: Pas d'auteur correspondant à la recherche.") + serv.privmsg(canal, "%s: Pas de quotes correspondant à la recherche." % (auteur,)) def on_action(self, serv, ev): """À la réception d'une action.""" @@ -485,6 +509,12 @@ class Parrot(ircbot.SingleServerIRCBot): time.sleep(2) serv.join(channel) + def on_registered_nick(self, serv, ev): + """À la réception d'un résultat de whois.""" + pseudo, regis = ev.arguments() + if regis == 'is a registered nick': + print "%s est enregistré \o/" % pseudo + ### .fork trick def start_as_daemon(self, outfile): sys.stderr = Logger(outfile)