]> gitweb.pimeys.fr Git - bots/hung.git/commitdiff
Code de base importé de Shadobot-hacker et légèrement fusionné avec deconnaisseur
authorVincent Le Gallic <legallic@crans.org>
Mon, 14 May 2012 10:36:25 +0000 (12:36 +0200)
committerVincent Le Gallic <legallic@crans.org>
Mon, 14 May 2012 10:36:25 +0000 (12:36 +0200)
hung.py [new file with mode: 0755]

diff --git a/hung.py b/hung.py
new file mode 100755 (executable)
index 0000000..9d640fe
--- /dev/null
+++ b/hung.py
@@ -0,0 +1,317 @@
+#!/usr/bin/python
+# -*- coding:utf8 -*-
+
+# Codé par 20-100 le 23/04/12
+
+# Un test de bot irc, parce que c'est cool
+
+import irclib
+import ircbot
+import threading
+import random
+import time
+import socket, ssl, json
+import pickle
+import re
+import os
+from commands import getstatusoutput as ex
+
+import sys
+config_debug_stdout=True
+if "--quiet" in sys.argv:
+    config_debug_stdout=False
+
+config_irc_password="I'mAHungMan"
+config_irc_pseudo="Hung"
+config_chanlist=["#flood"]
+config_stay_channels=["#flood"]
+config_quiet_channels=[]
+config_logfile_template="hung.%s.log"
+def get_config_logfile(serveur):
+    serveurs={"acoeur.crans.org":"acoeur","irc.crans.org":"crans"}
+    return config_logfile_template%(serveurs[serveur])
+config_overops=["[20-100]","[20-100]_"]
+config_ops=[]
+config_report_bugs_to=["[20-100]"]
+
+def log(serveur,channel,auteur=None,message=None):
+    f=open(get_config_logfile(serveur),"a")
+    if auteur==message==None:
+        # alors c'est que c'est pas un channel mais juste une ligne de log
+        chain="%s %s"%(time.strftime("%F %T"),channel)
+    else:
+        chain="%s [%s:%s] %s"%(time.strftime("%F %T"),channel,auteur,message)
+    f.write(chain+"\n")
+    if config_debug_stdout:
+        print chain
+    f.close()
+
+class UnicodeBotError(Exception):
+    pass
+def bot_unicode(chain):
+    try:
+        unicode(chain,"utf8")
+    except UnicodeDecodeError as exc:
+        raise UnicodeBotError
+
+class Hung(ircbot.SingleServerIRCBot):
+    def __init__(self,serveur,debug=False):
+        temporary_pseudo=config_irc_pseudo+str(random.randrange(10000,100000))
+        ircbot.SingleServerIRCBot.__init__(self, [(serveur, 6667)],
+                              temporary_pseudo,"Bot irc pour embêter Shadobot", 10)
+        self.debug=debug
+        self.serveur=serveur
+        self.overops=config_overops
+        self.ops=self.overops+config_ops
+        self.report_bugs_to=config_report_bugs_to
+        self.chanlist=config_chanlist
+        self.stay_channels=config_stay_channels
+        self.play_channels=config_play_channels
+        self.quiet_channels=config_quiet_channels
+
+
+    def give_me_my_pseudo(self,serv):
+        serv.privmsg("NickServ","RECOVER %s %s"%(config_irc_pseudo,config_irc_password))
+        serv.privmsg("NickServ","RELEASE %s %s"%(config_irc_pseudo,config_irc_password))
+        time.sleep(0.3)
+        serv.nick(config_irc_pseudo)
+    
+    def on_welcome(self, serv, ev):
+        self.give_me_my_pseudo(serv)
+        serv.privmsg("NickServ","IDENTIFY %s"%(config_irc_password))
+        log(self.serveur,"Connected")
+        if self.debug:
+            self.chanlist=["#bot"]
+            self.play_channels=["#bot"]
+        for c in self.chanlist:
+            log(self.serveur,"JOIN %s"%(c))
+            serv.join(c)
+
+    def pourmoi(self, serv, message):
+        """renvoie (False,lemessage) ou (True, le message amputé de "pseudo: ")"""
+        pseudo=serv.get_nickname()
+        size=len(pseudo)
+        if message[:size]==pseudo and len(message)>size and message[size]==":":
+            return (True,message[size+1:].lstrip(" "))
+        else:
+            return (False,message)
+
+    def on_privmsg(self, serv, ev):
+        message=ev.arguments()[0]
+        auteur = irclib.nm_to_n(ev.source())
+        try:
+            test=bot_unicode(message)
+        except UnicodeBotError:
+            serv.privmsg(auteur,
+              "Euh, tu fais de la merde avec ton encodage là, j'ai failli crasher…")
+            return
+        message=message.split()
+        cmd=message[0].lower()
+        notunderstood=False
+        if cmd=="join":
+            if auteur in self.ops:
+                if len(message)>1:
+                    if message[1] in self.chanlist:
+                        serv.privmsg(auteur,"Je suis déjà sur %s"%(message[1]))
+                    else:
+                        serv.join(message[1])
+                        self.chanlist.append(message[1])
+                        serv.privmsg(auteur,"Channels : "+" ".join(self.chanlist))
+                        log(self.serveur,"priv",auteur," ".join(message))
+                else:
+                    serv.privmsg(auteur,"Channels : "+" ".join(self.chanlist))
+            else:
+                notunderstood=True
+        elif cmd=="leave":
+            if auteur in self.ops and len(message)>1:
+                if message[1] in self.chanlist:
+                    if not (message[1] in self.stay_channels) or auteur in self.overops:
+                        serv.part(message[1])
+                        self.chanlist.remove(message[1])
+                        log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                    else:
+                        serv.privmsg(auteur,"Non, je reste !")
+                        log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
+                else:
+                    serv.privmsg(auteur,"Je ne suis pas sur %s"%(message[1]))
+            else:
+                notunderstood=True
+        elif cmd=="play":
+            if auteur in self.ops:
+                if len(message)>1:
+                    if message[1] in self.play_channels:
+                        serv.privmsg(auteur,"Je play déjà sur %s."%(message[1]))
+                        log("priv",auteur," ".join(message)+"[failed]")
+                    else:
+                        self.play_channels.append(message[1])
+                        self.play_status[message[1]]=None
+                        serv.privmsg(auteur,"Play channels : "+" ".join(self.play_channels))
+                        log("priv",auteur," ".join(message)+"[successful]")
+                else:
+                    serv.privmsg(auteur,"Play channels : "+" ".join(self.play_channels))
+            else:
+                notunderstood=True
+        elif cmd=="noplay":
+            if auteur in self.ops:
+                if len(message)>1:
+                    if message[1] in self.play_channels:
+                        self.play_channels.remove(message[1])
+                        serv.privmsg(auteur,"Play channels : "+" ".join(self.play_channels))
+                        log("priv",auteur," ".join(message)+"[successful]")
+                    else:
+                        serv.privmsg(auteur,"Je ne play pas sur %s."%(message[1]))
+                        log("priv",auteur," ".join(message)+"[failed]")
+            else:
+                notunderstood=True
+        elif cmd=="stay":
+            if auteur in self.overops:
+                if len(message)>1:
+                    if message[1] in self.stay_channels:
+                        log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
+                        serv.privmsg(auteur,"Je stay déjà sur %s."%(message[1]))
+                    else:
+                        log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                        self.stay_channels.append(message[1])
+                        serv.privmsg(auteur,"Stay channels : "+" ".join(self.stay_channels))
+                else:
+                    serv.privmsg(auteur,"Stay channels : "+" ".join(self.stay_channels))
+            else:
+                notunderstood=True
+        elif cmd=="nostay":
+            if auteur in self.overops:
+                if len(message)>1:
+                    if message[1] in self.stay_channels:
+                        log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                        self.stay_channels.remove(message[1])
+                        serv.privmsg(auteur,"Stay channels : "+" ".join(self.stay_channels))
+                    else:
+                        log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
+                        serv.privmsg(auteur,"Je ne stay pas sur %s."%(message[1]))
+
+            else:
+                notunderstood=True
+        elif cmd=="die":
+            if auteur in self.overops:
+                log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                self.die()
+            else:
+                notunderstood=True
+        elif cmd=="quiet":
+            if auteur in self.ops:
+                if len(message)>1:
+                    if message[1] in self.quiet_channels:
+                        serv.privmsg(auteur,"Je me la ferme déjà sur %s"%(message[1]))
+                        log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
+                    else:
+                        self.quiet_channels.append(message[1])
+                        serv.privmsg(auteur,"Quiet channels : "+" ".join(self.quiet_channels))
+                        log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                else:
+                    serv.privmsg(auteur,"Quiet channels : "+" ".join(self.quiet_channels))
+            else:
+                notunderstood=True
+        elif cmd=="noquiet":
+            if auteur in self.ops:
+                if len(message)>1:
+                    if message[1] in self.quiet_channels:
+                        self.quiet_channels.remove(message[1])
+                        serv.privmsg(auteur,"Quiet channels : "+" ".join(self.quiet_channels))
+                        log(self.serveur,"priv",auteur," ".join(message)+"[successful]")
+                    else:
+                        serv.privmsg(auteur,"Je ne me la ferme pas sur %s."%(message[1]))
+                        log(self.serveur,"priv",auteur," ".join(message)+"[failed]")
+            else:
+                notunderstood=True
+        elif cmd=="say":
+            if auteur in self.overops and len(message)>2:
+                serv.privmsg(message[1]," ".join(message[2:]))
+                log(self.serveur,"priv",auteur," ".join(message))
+            elif len(message)<=2:
+                serv.privmsg(auteur,"Syntaxe : SAY <channel> <message>")
+            else:
+                notunderstood=True
+        elif cmd=="do":
+            if auteur in self.overops and len(message)>2:
+                serv.action(message[1]," ".join(message[2:]))
+                log(self.serveur,"priv",auteur," ".join(message))
+            elif len(message)<=2:
+                serv.privmsg(auteur,"Syntaxe : DO <channel> <action>")
+            else:
+                notunderstood=True
+        else:
+            notunderstood=True
+        if notunderstood:
+            serv.privmsg(auteur,"Je n'ai pas compris. Essaye HELP…")
+    
+    def on_pubmsg(self, serv, ev):
+        auteur = irclib.nm_to_n(ev.source())
+        canal = ev.target()
+        message = ev.arguments()[0]
+        try:
+            test=bot_unicode(message)
+        except UnicodeBotError:
+            if not canal in self.quiet_channels:
+                serv.privmsg(canal,
+                  "%s: Euh, tu fais de la merde avec ton encodage là, j'ai failli crasher…"%(auteur))
+                return
+        pour_moi,message=self.pourmoi(serv,message)
+        if pour_moi and message.split()!=[]:
+            cmd=message.split()[0].lower()
+            try:
+                args=" ".join(message.split()[1:])
+            except:
+                args=""
+            if cmd in ["meurs","die","crève"]:
+                if auteur in self.overops:
+                    log(self.serveur,canal,auteur,message+"[successful]")
+                    self.die()
+                else:
+                    serv.privmsg(canal,"%s: crève !"%(auteur))
+                    log(self.serveur,canal,auteur,message+"[failed]")
+            elif cmd in ["part","leave","dégage"]:
+                if auteur in self.ops and (not (canal in self.stay_channels)
+                                           or auteur in self.overops):
+                    serv.part(canal,message="Éjecté par %s"%(auteur))
+                    log(self.serveur,canal,auteur,message+"[successful]")
+                    if canal in self.chanlist:
+                        self.chanlist.remove(canal)
+                else:
+                    serv.privmsg(canal,"%s: Non, je reste !"%(auteur))
+                    log(self.serveur,canal,auteur,message+"[failed]")
+            
+            if cmd in ["meur", "meurt","meurre","meurres"] and not canal in self.quiet_channels:
+                serv.privmsg(canal,'%s: Mourir, impératif, 2ème personne du singulier : "meurs" (de rien)'%(auteur))
+        else:
+            pass
+
+    def on_action(self, serv, ev):
+        action = ev.arguments()[0]
+        auteur = irclib.nm_to_n(ev.source())
+        channel = ev.target()
+        try:
+            test=bot_unicode(action)
+        except UnicodeBotError:
+            serv.privmsg(channel,
+              "%s : Euh, tu fais de la merde avec ton encodage là, j'ai failli crasher…"%(auteur))
+            return
+        mypseudo=serv.get_nickname()
+        
+if __name__=="__main__":
+    import sys
+    if len(sys.argv)==1:
+        print "Usage : hung.py <serveur> [--debug]"
+        exit(1)
+    serveur=sys.argv[1]
+    if "debug" in sys.argv or "--debug" in sys.argv:
+        debug=True
+    else:
+        debug=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"}
+    try:
+        serveur=serveurs[serveur]
+    except KeyError:
+        print "Server Unknown : %s"%(serveur)
+        exit(404)
+    hung=Hung(serveur,debug)
+    hung.start()