X-Git-Url: http://gitweb.pimeys.fr/?a=blobdiff_plain;f=saturnin.py;h=4903ec32892e35832c00df7351d9205f36b349e2;hb=015c05016255df0dc06e6ebb2e21c7b8dbe34de4;hp=a99fb15cc175011ac8b36d11d8c6a3625bab98a5;hpb=b216d6663a2ed6b814b5b3b43867653aa959cca8;p=bots%2Fsaturnin.git diff --git a/saturnin.py b/saturnin.py index a99fb15..4903ec3 100755 --- a/saturnin.py +++ b/saturnin.py @@ -6,8 +6,6 @@ # Un bot IRC pour remplacer le canard. # parce que le canard, c'est le bien et que braice ne pong pas -import irclib -import ircbot import threading import random import time @@ -15,8 +13,15 @@ import socket, ssl, json import pickle import re import os +import signal +import sys from commands import getstatusoutput as ex +# Oui, j'ai recodé ma version d'irclib pour pouvoir rattrapper les SIGHUP +sys.path.insert(0, "/home/vincent/scripts/python-myirclib") +import irclib +import ircbot + # on récupère la config import config @@ -51,6 +56,11 @@ def is_something(chain,matches,avant=u".*(?:^| )",apres=u"(?:$|\.| |,|;).*",case regexp_pan = re.compile(u".*(" + "|".join(config.killwords) + u").*") def is_pan(chain): return regexp_pan.match(unicode(chain,"utf8").lower()) +def ignore_pan(serv, ev): + """Retourne ``True`` si il faut ignorer ce pan.""" + for (blackmask, exceptmask) in config.ignored_pan_masks: + usermask = ev.source() + return bool(irclib.mask_matches(usermask, blackmask) and not irclib.mask_matches(usermask, exceptmask)) class UnicodeBotError(Exception): pass @@ -301,6 +311,11 @@ class Saturnin(ircbot.SingleServerIRCBot): log(self.serveur,"priv",auteur," ".join(message)+"[failed]") else: notunderstood=True + elif cmd=="status": + if auteur in self.ops: + serv.privmsg(auteur,"Status : %s" % (self.status, )) + else: + notunderstood=True elif cmd=="spawn": if auteur in self.ops: if len(message)>1: @@ -350,8 +365,8 @@ class Saturnin(ircbot.SingleServerIRCBot): notunderstood=True elif cmd=="reload": if auteur in self.ops: - reload(config) - serv.privmsg(auteur,"done") + self.reload(auteur) + log(self.serveur,"priv",auteur," ".join(message)+"[successful]") else: notunderstood=True elif cmd=="score": @@ -440,6 +455,8 @@ class Saturnin(ircbot.SingleServerIRCBot): def on_pubmsg(self, serv, ev): auteur = irclib.nm_to_n(ev.source()) + if re.match(config.ignored_pseudos, auteur): + return channel = ev.target() message = ev.arguments()[0] try: @@ -462,6 +479,10 @@ class Saturnin(ircbot.SingleServerIRCBot): else: serv.privmsg(channel,("%s: %s"%(auteur,random.choice(config.quit_fail_messages))).encode("utf8")) log(self.serveur,channel,auteur,message+"[failed]") + elif cmd == "reload": + if auteur in self.ops: + log(self.serveur, channel, auteur, message+"[successful]") + self.reload(channel) elif cmd in ["part","leave","dégage","va-t-en","tut'tiresailleurs,c'estmesgalets"]: if auteur in self.ops and (not (channel in self.stay_channels) or auteur in self.overops): @@ -478,6 +499,8 @@ class Saturnin(ircbot.SingleServerIRCBot): self.sendscores(auteur) else: if is_pan(message): + if ignore_pan(serv, ev): + return self.shot(channel, auteur) def on_action(self, serv, ev): @@ -577,26 +600,88 @@ class Saturnin(ircbot.SingleServerIRCBot): def _getnick(self): return self.serv.get_nickname() nick=property(_getnick) + + def reload(self, auteur=None): + reload(config) + if auteur in [None, "SIGHUP"]: + towrite = "Config reloaded" + " (SIGHUP received)"*(auteur == "SIGHUP") + for to in config.report_bugs_to: + self.serv.privmsg(to, towrite) + log(self.serveur, towrite) + else: + self.serv.privmsg(auteur,"Config reloaded") + + def start_as_daemon(self, outfile): + sys.stderr = Logger(outfile) + self.start() + + +class Logger(object): + """Pour écrire ailleurs que sur stdout""" + def __init__(self, filename="saturnin.full.log"): + self.filename = filename + + def write(self, message): + f = open(self.filename, "a") + f.write(message) + f.close() if __name__=="__main__": import sys if len(sys.argv)==1: - print "Usage : saturnin.py [--debug]" + print "Usage : saturnin.py [--debug] [--no-output] [--daemon [--pidfile]] [--outfile]" + print " --outfile sans --no-output ni --daemon n'a aucun effet" exit(1) serveur=sys.argv[1] + if "--daemon" in sys.argv: + thisfile = os.path.realpath(__file__) + thisdirectory = thisfile.rsplit("/", 1)[0] + os.chdir(thisdirectory) + daemon = True + else: + daemon = False if "debug" in sys.argv or "--debug" in sys.argv: debug=True else: debug=False + if "--no-output" in sys.argv or "--daemon" in sys.argv: + outfile = "/var/log/bots/saturnin.full.log" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["out", "outfile", "logfile"]: + outfile = arg[1] + sys.stdout = Logger(outfile) if "--quiet" in sys.argv: config.debug_stdout=False serveurs={"a♡":"acoeur.crans.org","acoeur":"acoeur.crans.org","acoeur.crans.org":"acoeur.crans.org", - "irc":"irc.crans.org","crans":"irc.crans.org","irc.crans.org":"irc.crans.org"} + "irc":"irc.crans.org","crans":"irc.crans.org","irc.crans.org":"irc.crans.org", + "local":"localhost"} try: serveur=serveurs[serveur] except KeyError: print "Server Unknown : %s"%(serveur) exit(404) - bot = Saturnin(serveur,debug) - bot.start() + saturnin = Saturnin(serveur,debug) + # Si on reçoit un SIGHUP, on reload la config + def sighup_handler(signum, frame): + saturnin.reload("SIGHUP") + signal.signal(signal.SIGHUP, sighup_handler) + if daemon: + child_pid = os.fork() + if child_pid == 0: + os.setsid() + saturnin.start_as_daemon(outfile) + else: + # on enregistre le pid de saturnin + pidfile = "/var/run/bots/saturnin.pid" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["pidfile"]: + pidfile = arg[1] + f = open(pidfile, "w") + f.write("%s\n" % child_pid) + f.close() + else: + saturnin.start() +