# Un bot IRC qui sort des déconnaissances
-import irclib
-import ircbot
import threading
import random
import time
import pickle
import re
+import signal
+import sys
from remplace_accents import remplace_accents
+# 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
+
config_password="Yamoussoukro"
config_pseudo="salesman"
config_chanlist=["#bot","#flood"]
self.last_activity[canal]=time.time()
def is_active(self,canal):
return time.time()-self.last_activity[canal]<config_idle_time
+
+ 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="salesman.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 : Salesman.py <serveur> [--debug]"
+ print "Usage : salesman.py <serveur> [--debug] [--no-output] [--daemon [--pidfile]] [--outfile]"
+ print " --outfile sans --no-output ni --daemon n'a aucun effet"
exit(1)
serveur=sys.argv[1]
+
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/salesman.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)
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"}
try:
print "Server Unknown : %s"%(serveur)
exit(404)
salesman=Salesman(serveur,debug)
- salesman.start()
+ if daemon:
+ child_pid = os.fork()
+ if child_pid == 0:
+ os.setsid()
+ salesman.start_as_daemon(outfile)
+ else:
+ # on enregistre le pid de salesman
+ pidfile = "/var/run/bots/salesman.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:
+ salesman.start()
+