From 5b30cb8bc2ddd449c1f6e342cfde2d8f26e2eb19 Mon Sep 17 00:00:00 2001 From: Paul Mathieu Date: Fri, 21 Oct 2022 23:09:55 +0100 Subject: [PATCH] Uncommitted stuff --- .gitignore | 4 +- basic.py | 42 +++--- debug.py | 98 +++++++------- edit.py | 160 +++++++++++----------- guiniol.py | 30 ++--- ircbot.py | 385 +++++++++++++++++++++++++++-------------------------- testmod.py | 122 ++++++++--------- 7 files changed, 421 insertions(+), 420 deletions(-) diff --git a/.gitignore b/.gitignore index 3bb2efd..519f9ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -.*.swp -*.pyc +.*.swp +*.pyc diff --git a/basic.py b/basic.py index b89f78b..1663b15 100644 --- a/basic.py +++ b/basic.py @@ -1,21 +1,21 @@ -client = None - -def mod_init(cl): - global client - client = cl - -def action(msg): - return '%cACTION %s%c' % (1, msg, 1) - -def on_privchat(who, msg): - args = msg.split() - if len(args) < 1: return - cmd = args[0] - if cmd == "say": - if len(args) < 3: - return - client.sendto(args[1], ' '.join(args[2:])) - if cmd == "act": - if len(args) < 3: - return - client.sendto(args[1], action(' '.join(args[2:]))) +client = None + +def mod_init(cl): + global client + client = cl + +def action(msg): + return '%cACTION %s%c' % (1, msg, 1) + +def on_privchat(who, msg): + args = msg.split() + if len(args) < 1: return + cmd = args[0] + if cmd == "say": + if len(args) < 3: + return + client.sendto(args[1], ' '.join(args[2:])) + if cmd == "act": + if len(args) < 3: + return + client.sendto(args[1], action(' '.join(args[2:]))) diff --git a/debug.py b/debug.py index 79ffcc6..ccadbc1 100644 --- a/debug.py +++ b/debug.py @@ -1,49 +1,49 @@ -import sys - -client = None - -def mod_init(cl): - global client - client = cl - -def action(msg): - return '%cACTION %s%c' % (1, msg, 1) - - -def execbuf(nick): - if 'edit' not in sys.modules: - return - b = sys.modules['edit'].bufs - if nick not in b: - return - try: - exec('\n'.join(b[nick].buf)) - except Exception, e: - client.sendto(nick, 'ERROR: %s' % str(e)) - - -def on_pubchat(who, chan, msg): - if msg[:len(client.nick)] == client.nick: - nmsg = msg.split(':')[1:] - nargs = nmsg[0].split() - if len(nargs) < 1: return - if len(nmsg) > 1: - nargs = nargs + [':' + ':'.join(nmsg[1:])] - ncmd = nargs[0] - - if ncmd == "raw": - client.send(' '.join(nargs[1:])) - - if ncmd == "execbuf": - execbuf(who.split('!')[0]) - - if ncmd == "exec": - try: - exec(' '.join(nargs[1:])) - except Exception, e: - client.sendto(chan, 'ERROR: %s' % str(e)) - - -def on_privchat(who, msg): - on_pubchat(who, who, client.nick + ': ' + msg) - +import sys + +client = None + +def mod_init(cl): + global client + client = cl + +def action(msg): + return '%cACTION %s%c' % (1, msg, 1) + + +def execbuf(nick): + if 'edit' not in sys.modules: + return + b = sys.modules['edit'].bufs + if nick not in b: + return + try: + exec('\n'.join(b[nick].buf)) + except Exception, e: + client.sendto(nick, 'ERROR: %s' % str(e)) + + +def on_pubchat(who, chan, msg): + if msg[:len(client.nick)] == client.nick: + nmsg = msg.split(':')[1:] + nargs = nmsg[0].split() + if len(nargs) < 1: return + if len(nmsg) > 1: + nargs = nargs + [':' + ':'.join(nmsg[1:])] + ncmd = nargs[0] + + if ncmd == "raw": + client.send(' '.join(nargs[1:])) + + if ncmd == "execbuf": + execbuf(who.split('!')[0]) + + if ncmd == "exec": + try: + exec(' '.join(nargs[1:])) + except Exception, e: + client.sendto(chan, 'ERROR: %s' % str(e)) + + +def on_privchat(who, msg): + on_pubchat(who, who, client.nick + ': ' + msg) + diff --git a/edit.py b/edit.py index b97a440..d63986e 100644 --- a/edit.py +++ b/edit.py @@ -1,80 +1,80 @@ -client = None - -class Buffer: - viewsize = 8 - - def __init__(self): - self.buf = [] - self.curline = 0 - self.edit = False - -bufs = {} - -def mod_init(cl): - global client - client = cl - -def getnick(user): - return user.split('!')[0] - -def getwindow(buf): - bi = buf.curline - (buf.viewsize-1)/2 - bs = buf.curline + buf.viewsize/2 + 1 - - if bs > len(buf.buf): - bi = bi - (bs - len(buf.buf)) - bs = len(buf.buf) - if bi < 0: - bi = 0 - - return range(bi, bs) - - -def on_privchat(who, msg): - nick = getnick(who) - if nick not in bufs: - bufs[nick] = Buffer() - b = bufs[nick] - - if msg[0] == '.': - args = msg[1:].split() - cmd = args[0] - - if cmd == "l": - if len(args) > 1: - b.curline = int(args[1]) - - if b.curline > len(b.buf): - b.curline = len(b.buf) - if b.curline < 0: - b.curline = 0 - - for i in getwindow(b): - if i == b.curline: - client.sendto(nick, '----') - client.sendto(nick, '%3d: '%i + b.buf[i]) - if b.curline == len(b.buf): - client.sendto(nick, '----') - - if cmd == "e": - if b.edit is True: - b.edit = False - else: - b.edit = True - - if cmd == "d": - if len(args) < 2: - return - if len(args) > 2: - for i in range(int(args[1]), int(args[2])+1): - b.buf.pop(int(args[1])) - else: - b.buf.pop(int(args[1])) - - else: - if b.edit is True: - b.buf.insert(b.curline, msg) - b.curline = b.curline + 1 - - - +client = None + +class Buffer: + viewsize = 8 + + def __init__(self): + self.buf = [] + self.curline = 0 + self.edit = False + +bufs = {} + +def mod_init(cl): + global client + client = cl + +def getnick(user): + return user.split('!')[0] + +def getwindow(buf): + bi = buf.curline - (buf.viewsize-1)/2 + bs = buf.curline + buf.viewsize/2 + 1 + + if bs > len(buf.buf): + bi = bi - (bs - len(buf.buf)) + bs = len(buf.buf) + if bi < 0: + bi = 0 + + return range(bi, bs) + + +def on_privchat(who, msg): + nick = getnick(who) + if nick not in bufs: + bufs[nick] = Buffer() + b = bufs[nick] + + if msg[0] == '.': + args = msg[1:].split() + cmd = args[0] + + if cmd == "l": + if len(args) > 1: + b.curline = int(args[1]) + + if b.curline > len(b.buf): + b.curline = len(b.buf) + if b.curline < 0: + b.curline = 0 + + for i in getwindow(b): + if i == b.curline: + client.sendto(nick, '----') + client.sendto(nick, '%3d: '%i + b.buf[i]) + if b.curline == len(b.buf): + client.sendto(nick, '----') + + if cmd == "e": + if b.edit is True: + b.edit = False + else: + b.edit = True + + if cmd == "d": + if len(args) < 2: + return + if len(args) > 2: + for i in range(int(args[1]), int(args[2])+1): + b.buf.pop(int(args[1])) + else: + b.buf.pop(int(args[1])) + + else: + if b.edit is True: + b.buf.insert(b.curline, msg) + b.curline = b.curline + 1 + + + diff --git a/guiniol.py b/guiniol.py index ae4d0c0..605894c 100644 --- a/guiniol.py +++ b/guiniol.py @@ -1,16 +1,16 @@ -client = None - - - -def mod_init(cl): - - global client - - client = cl - - - -def on_pubchat(who, chan, msg): - if who.split('!')[0] == 'guiniolBot': - if msg.find('aurez') != -1: +client = None + + + +def mod_init(cl): + + global client + + client = cl + + + +def on_pubchat(who, chan, msg): + if who.split('!')[0] == 'guiniolBot': + if msg.find('aurez') != -1: client.sendto(chan, '%cACTION sort un fusil sniper et ajuste la visee sur guiniolBot...%c' % (1, 1)) \ No newline at end of file diff --git a/ircbot.py b/ircbot.py index 03d3c10..420d228 100644 --- a/ircbot.py +++ b/ircbot.py @@ -1,192 +1,193 @@ -import threading -import string -import socket -import sys - -#server = "casimir.ponteilla.net" -server = "localhost" -port = 6667 -chan = "#pno" -nick = "PaulBot" - - -class ModNotFoundException(Exception): - pass - -def getnick(user): - return user.split('!')[0] - -class monmodule: - def __init__(self, name, client): - self.name = name - if name in sys.modules: - reload(sys.modules[name]) - self.mod = __import__(name) - self.mod.mod_init(client) - -class TCPClient: - - cmds = ["privmsg", "ping"] #"mode", "join", "part", "quit"] - - def __init__(self, sock, nick): - self.sock = sock - self.nick = nick - self.modules = [] - - def send(self, msg): - self.sock.send(":%s %s\r\n" % (self.nick, msg)) - print "=== sent: :%s %s" % (self.nick, msg) - - def pubchat(self, chan, msg): - self.send("PRIVMSG %s :%s" % (chan, msg)) - - def sendto(self, who, msg): - self.pubchat(getnick(who), msg) - - def handle(self, data): - #print data - if data[0] != ':': - data = ':none ' + data - args = data.split(':')[1:] - args = args[0].split() + [':'.join(args[1:])] - cmd = args[1].lower() - -# print "=== CMD(%s)" % cmd - - if cmd in self.cmds: - getattr(self, "on_" + cmd)(args[0], args[2:]) - - - def onconnect(self): - self.sock.send("NICK %s\r\n" % nick) - self.sock.send("USER paulbot ponteilla.net blektr :PaulBot\r\n") - - self.send("JOIN #pno") - - def find_mod(self, name): - try: - i = [x.name for x in self.modules].index(name) - except ValueError: - raise ModNotFoundException() - else: - return self.modules[i] - - def remove_mod(self, name): - mod = self.find_mod(name) - reload(mod.mod) - self.modules.remove(mod) - - def handle_command(self, who, cmd): - if len(cmd.split()) < 1: - return - ncmd = cmd.split()[0] - nargs = cmd.split()[1:] - if ncmd == "reload": - try: - mod = self.find_mod(nargs[0]) - except ModNotFoundException: - self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) - else: - self.sendto(who, "Reloading module '%s'..." % nargs[0]) - try: - reload(mod.mod) - mod.mod.mod_init(self) - except Exception, e: - self.sendto(who, "ERROR: %s" % str(e)) - - if ncmd == "unload": - try: - self.remove_mod(nargs[0]) - except ModNotFoundException: - self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) - else: - self.sendto(who, "Module '%s' unloaded." % nargs[0]) - - if ncmd == "load": - if nargs[0] in [x.name for x in self.modules]: - self.sendto(who, "ERROR: Module '%s' already loaded" % str(e)) - else: - try: - mod = monmodule(nargs[0], self) - except Exception, e: - self.sendto(who, "ERROR: %s" % str(e)) - else: - self.modules.append(mod) - self.sendto(who, "Module '%s' loaded." % nargs[0]) - - def on_pubchat(self, who, chan, msg): - if msg[:len(self.nick)] == self.nick: - nmsg = ':'.join(msg.split(':')[1:]).lstrip() - self.handle_command(chan, nmsg) - - for mod in self.modules: - if 'on_pubchat' in dir(mod.mod): - try: - mod.mod.on_pubchat(who, chan, msg) - except Exception, e: - self.sendto(chan, str(e)) - self.sendto(chan, "Module '%s' unloaded" % mod.name) - self.remove_mod(mod.name) - - def on_privchat(self, who, msg): - self.handle_command(who, msg) - - for mod in self.modules: - if 'on_privchat' in dir(mod.mod): - try: - mod.mod.on_privchat(who, msg) - except Exception, e: - self.sendto(getnick(who), str(e)) - self.sendto(getnick(who), "Module '%s' unloaded" % mod.name) - self.remove_mod(mod.name) - - - def on_privmsg(self, who, params): - towho = params[0] - msg = params[1] - - if getnick(towho) == self.nick: - self.on_privchat(who, msg) - else: - self.on_pubchat(who, towho, msg) - - print "(%s) %s: %s" % (towho, who.split('!')[0], msg) - - - def on_ping(self, who, params): - print "PING? PONG!" - self.send("PONG %s" % self.nick) - - -if __name__ == "__main__": - - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - client = TCPClient(sock, nick) - - print "Connecting to %s:%d" % (server, port) - try: - sock.connect((server, port)) - except socket.error: - sock.close() - print "ERR: could not connect!" - exit(1) - - client.onconnect() - - try: - while True: - data = sock.recv(2048) - if not data: - break - for s in data.split('\r\n'): - if s.rstrip() != "": - client.handle(s) - except KeyboardInterrupt: - client.sendto("#pno", - "Aiiieuuuh, me suis pris un C-c dans les dents :/") - finally: - sock.close() - for m in client.modules: - if 'on_die' in dir(m.mod): - m.mod.on_die() - +import threading +import string +import socket +import sys + +#server = "casimir.ponteilla.net" +server = "localhost" +port = 6667 +chan = "#pno" +nick = "PaulBot" + + +class ModNotFoundException(Exception): + pass + +def getnick(user): + return user.split('!')[0] + +class monmodule: + def __init__(self, name, client): + self.name = name + if name in sys.modules: + reload(sys.modules[name]) + self.mod = __import__(name) + self.mod.mod_init(client) + +class TCPClient: + + cmds = ["privmsg", "ping"] #"mode", "join", "part", "quit"] + + def __init__(self, sock, nick): + self.sock = sock + self.nick = nick + self.modules = [] + + def send(self, msg): + self.sock.send(":%s %s\r\n" % (self.nick, msg)) + print "=== sent: :%s %s" % (self.nick, msg) + + def pubchat(self, chan, msg): + self.send("PRIVMSG %s :%s" % (chan, msg)) + + def sendto(self, who, msg): + self.pubchat(getnick(who), msg) + + def handle(self, data): + #print data + if data[0] != ':': + data = ':none ' + data + args = data.split(':')[1:] + args = args[0].split() + [':'.join(args[1:])] + cmd = args[1].lower() + +# print "=== CMD(%s)" % cmd + + if cmd in self.cmds: + getattr(self, "on_" + cmd)(args[0], args[2:]) + + + def onconnect(self): + self.sock.send("NICK %s\r\n" % nick) + self.sock.send("USER paulbot ponteilla.net blektr :PaulBot\r\n") + + self.send("JOIN #pno") + + def find_mod(self, name): + try: + i = [x.name for x in self.modules].index(name) + except ValueError: + raise ModNotFoundException() + else: + return self.modules[i] + + def remove_mod(self, name): + mod = self.find_mod(name) + reload(mod.mod) + self.modules.remove(mod) + + def handle_command(self, who, cmd): + if len(cmd.split()) < 1: + return + ncmd = cmd.split()[0] + nargs = cmd.split()[1:] + if ncmd == "reload": + try: + mod = self.find_mod(nargs[0]) + except ModNotFoundException: + self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) + else: + self.sendto(who, "Reloading module '%s'..." % nargs[0]) + try: + reload(mod.mod) + mod.mod.mod_init(self) + except Exception, e: + self.sendto(who, "ERROR: %s" % str(e)) + + if ncmd == "unload": + try: + self.remove_mod(nargs[0]) + except ModNotFoundException: + self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) + else: + self.sendto(who, "Module '%s' unloaded." % nargs[0]) + + if ncmd == "load": + for arg in nargs: + if arg in [x.name for x in self.modules]: + self.sendto(who, "ERROR: Module '%s' already loaded" % arg) + else: + try: + mod = monmodule(arg, self) + except Exception, e: + self.sendto(who, "ERROR: %s" % str(e)) + else: + self.modules.append(mod) + self.sendto(who, "Module '%s' loaded." % arg) + + def on_pubchat(self, who, chan, msg): + if msg[:len(self.nick)] == self.nick: + nmsg = ':'.join(msg.split(':')[1:]).lstrip() + self.handle_command(chan, nmsg) + + for mod in self.modules: + if 'on_pubchat' in dir(mod.mod): + try: + mod.mod.on_pubchat(who, chan, msg) + except Exception, e: + self.sendto(chan, str(e)) + self.sendto(chan, "Module '%s' unloaded" % mod.name) + self.remove_mod(mod.name) + + def on_privchat(self, who, msg): + self.handle_command(who, msg) + + for mod in self.modules: + if 'on_privchat' in dir(mod.mod): + try: + mod.mod.on_privchat(who, msg) + except Exception, e: + self.sendto(getnick(who), str(e)) + self.sendto(getnick(who), "Module '%s' unloaded" % mod.name) + self.remove_mod(mod.name) + + + def on_privmsg(self, who, params): + towho = params[0] + msg = params[1] + + if getnick(towho) == self.nick: + self.on_privchat(who, msg) + else: + self.on_pubchat(who, towho, msg) + + print "(%s) %s: %s" % (towho, who.split('!')[0], msg) + + + def on_ping(self, who, params): + print "PING? PONG!" + self.send("PONG %s" % self.nick) + + +if __name__ == "__main__": + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client = TCPClient(sock, nick) + + print "Connecting to %s:%d" % (server, port) + try: + sock.connect((server, port)) + except socket.error: + sock.close() + print "ERR: could not connect!" + exit(1) + + client.onconnect() + + try: + while True: + data = sock.recv(2048) + if not data: + break + for s in data.split('\r\n'): + if s.rstrip() != "": + client.handle(s) + except KeyboardInterrupt: + client.sendto("#pno", + "Aiiieuuuh, me suis pris un C-c dans les dents :/") + finally: + sock.close() + for m in client.modules: + if 'on_die' in dir(m.mod): + m.mod.on_die() + diff --git a/testmod.py b/testmod.py index eedf4e3..9dee5d1 100644 --- a/testmod.py +++ b/testmod.py @@ -1,61 +1,61 @@ -import random -import threading -import time - -client = None -next = None - -def on_die(): - next.cancel() - -def vide_blanc(): - i = random.randint(0, len(meublage)-1) - client.pubchat("#pno", meublage[i]) - rearmer() - -def rearmer(): - global next - if next is not None: - next.cancel() - next = threading.Timer(random.randint(1200, 9000), vide_blanc) - next.start() - -def mod_init(cl): - global client, next - client = cl - random.seed() - rearmer() - - -reponses = [ - "bien dit !", - "pfff...", - "serieux ? o_O", - "Hello guiniolBot!", - "au fait, juste comme ca, sans vouloir balancer des fleurs ou quoi, mais mon createur est un type bien", - "oh ! mais il parle !", - "je sais que je ne fais que repeter des phrases aleatoires, mais si je peux aider a meubler un peu...", - "bwaaAAaaHahhahahaaahaahahaaaa!!", - "ou pas.", - "nan mais meme si c'etait vrai, t'as vu ta tronche ?" - ] - -meublage = [ - "Aaa.. TCHA!", - "Hello guiniolBot :D", - "Celui qui lit ceci ferait mieux de lire autre chose", - "mais dis donc c'est aussi anime qu'a une soiree scrabble chez les grand-parents ici...", - "bwaarggll" - ] - -def on_pubchat(who, chan, msg): - if (msg.find('bwaarggll') != -1): - client.pubchat(chan, "BwAaRgGlL!1!!") - - i = random.randint(0, len(reponses)-1) - if random.randint(0, 1024) < 42: - client.pubchat(chan, reponses[i]) - - rearmer() - - +import random +import threading +import time + +client = None +next = None + +def on_die(): + next.cancel() + +def vide_blanc(): + i = random.randint(0, len(meublage)-1) + client.pubchat("#pno", meublage[i]) + rearmer() + +def rearmer(): + global next + if next is not None: + next.cancel() + next = threading.Timer(random.randint(1200, 9000), vide_blanc) + next.start() + +def mod_init(cl): + global client, next + client = cl + random.seed() + rearmer() + + +reponses = [ + "bien dit !", + "pfff...", + "serieux ? o_O", + "Hello guiniolBot!", + "au fait, juste comme ca, sans vouloir balancer des fleurs ou quoi, mais mon createur est un type bien", + "oh ! mais il parle !", + "je sais que je ne fais que repeter des phrases aleatoires, mais si je peux aider a meubler un peu...", + "bwaaAAaaHahhahahaaahaahahaaaa!!", + "ou pas.", + "nan mais meme si c'etait vrai, t'as vu ta tronche ?" + ] + +meublage = [ + "Aaa.. TCHA!", + "Hello guiniolBot :D", + "Celui qui lit ceci ferait mieux de lire autre chose", + "mais dis donc c'est aussi anime qu'a une soiree scrabble chez les grand-parents ici...", + "bwaarggll" + ] + +def on_pubchat(who, chan, msg): + if (msg.find('bwaarggll') != -1): + client.pubchat(chan, "BwAaRgGlL!1!!") + + i = random.randint(0, len(reponses)-1) + if random.randint(0, 1024) < 42: + client.pubchat(chan, reponses[i]) + + rearmer() + +