Uncommitted stuff

This commit is contained in:
Paul Mathieu 2022-10-21 23:09:55 +01:00
parent e0bdc6b3dd
commit 5b30cb8bc2
7 changed files with 421 additions and 420 deletions

4
.gitignore vendored
View File

@ -1,2 +1,2 @@
.*.swp .*.swp
*.pyc *.pyc

View File

@ -1,21 +1,21 @@
client = None client = None
def mod_init(cl): def mod_init(cl):
global client global client
client = cl client = cl
def action(msg): def action(msg):
return '%cACTION %s%c' % (1, msg, 1) return '%cACTION %s%c' % (1, msg, 1)
def on_privchat(who, msg): def on_privchat(who, msg):
args = msg.split() args = msg.split()
if len(args) < 1: return if len(args) < 1: return
cmd = args[0] cmd = args[0]
if cmd == "say": if cmd == "say":
if len(args) < 3: if len(args) < 3:
return return
client.sendto(args[1], ' '.join(args[2:])) client.sendto(args[1], ' '.join(args[2:]))
if cmd == "act": if cmd == "act":
if len(args) < 3: if len(args) < 3:
return return
client.sendto(args[1], action(' '.join(args[2:]))) client.sendto(args[1], action(' '.join(args[2:])))

View File

@ -1,49 +1,49 @@
import sys import sys
client = None client = None
def mod_init(cl): def mod_init(cl):
global client global client
client = cl client = cl
def action(msg): def action(msg):
return '%cACTION %s%c' % (1, msg, 1) return '%cACTION %s%c' % (1, msg, 1)
def execbuf(nick): def execbuf(nick):
if 'edit' not in sys.modules: if 'edit' not in sys.modules:
return return
b = sys.modules['edit'].bufs b = sys.modules['edit'].bufs
if nick not in b: if nick not in b:
return return
try: try:
exec('\n'.join(b[nick].buf)) exec('\n'.join(b[nick].buf))
except Exception, e: except Exception, e:
client.sendto(nick, 'ERROR: %s' % str(e)) client.sendto(nick, 'ERROR: %s' % str(e))
def on_pubchat(who, chan, msg): def on_pubchat(who, chan, msg):
if msg[:len(client.nick)] == client.nick: if msg[:len(client.nick)] == client.nick:
nmsg = msg.split(':')[1:] nmsg = msg.split(':')[1:]
nargs = nmsg[0].split() nargs = nmsg[0].split()
if len(nargs) < 1: return if len(nargs) < 1: return
if len(nmsg) > 1: if len(nmsg) > 1:
nargs = nargs + [':' + ':'.join(nmsg[1:])] nargs = nargs + [':' + ':'.join(nmsg[1:])]
ncmd = nargs[0] ncmd = nargs[0]
if ncmd == "raw": if ncmd == "raw":
client.send(' '.join(nargs[1:])) client.send(' '.join(nargs[1:]))
if ncmd == "execbuf": if ncmd == "execbuf":
execbuf(who.split('!')[0]) execbuf(who.split('!')[0])
if ncmd == "exec": if ncmd == "exec":
try: try:
exec(' '.join(nargs[1:])) exec(' '.join(nargs[1:]))
except Exception, e: except Exception, e:
client.sendto(chan, 'ERROR: %s' % str(e)) client.sendto(chan, 'ERROR: %s' % str(e))
def on_privchat(who, msg): def on_privchat(who, msg):
on_pubchat(who, who, client.nick + ': ' + msg) on_pubchat(who, who, client.nick + ': ' + msg)

160
edit.py
View File

@ -1,80 +1,80 @@
client = None client = None
class Buffer: class Buffer:
viewsize = 8 viewsize = 8
def __init__(self): def __init__(self):
self.buf = [] self.buf = []
self.curline = 0 self.curline = 0
self.edit = False self.edit = False
bufs = {} bufs = {}
def mod_init(cl): def mod_init(cl):
global client global client
client = cl client = cl
def getnick(user): def getnick(user):
return user.split('!')[0] return user.split('!')[0]
def getwindow(buf): def getwindow(buf):
bi = buf.curline - (buf.viewsize-1)/2 bi = buf.curline - (buf.viewsize-1)/2
bs = buf.curline + buf.viewsize/2 + 1 bs = buf.curline + buf.viewsize/2 + 1
if bs > len(buf.buf): if bs > len(buf.buf):
bi = bi - (bs - len(buf.buf)) bi = bi - (bs - len(buf.buf))
bs = len(buf.buf) bs = len(buf.buf)
if bi < 0: if bi < 0:
bi = 0 bi = 0
return range(bi, bs) return range(bi, bs)
def on_privchat(who, msg): def on_privchat(who, msg):
nick = getnick(who) nick = getnick(who)
if nick not in bufs: if nick not in bufs:
bufs[nick] = Buffer() bufs[nick] = Buffer()
b = bufs[nick] b = bufs[nick]
if msg[0] == '.': if msg[0] == '.':
args = msg[1:].split() args = msg[1:].split()
cmd = args[0] cmd = args[0]
if cmd == "l": if cmd == "l":
if len(args) > 1: if len(args) > 1:
b.curline = int(args[1]) b.curline = int(args[1])
if b.curline > len(b.buf): if b.curline > len(b.buf):
b.curline = len(b.buf) b.curline = len(b.buf)
if b.curline < 0: if b.curline < 0:
b.curline = 0 b.curline = 0
for i in getwindow(b): for i in getwindow(b):
if i == b.curline: if i == b.curline:
client.sendto(nick, '----') client.sendto(nick, '----')
client.sendto(nick, '%3d: '%i + b.buf[i]) client.sendto(nick, '%3d: '%i + b.buf[i])
if b.curline == len(b.buf): if b.curline == len(b.buf):
client.sendto(nick, '----') client.sendto(nick, '----')
if cmd == "e": if cmd == "e":
if b.edit is True: if b.edit is True:
b.edit = False b.edit = False
else: else:
b.edit = True b.edit = True
if cmd == "d": if cmd == "d":
if len(args) < 2: if len(args) < 2:
return return
if len(args) > 2: if len(args) > 2:
for i in range(int(args[1]), int(args[2])+1): for i in range(int(args[1]), int(args[2])+1):
b.buf.pop(int(args[1])) b.buf.pop(int(args[1]))
else: else:
b.buf.pop(int(args[1])) b.buf.pop(int(args[1]))
else: else:
if b.edit is True: if b.edit is True:
b.buf.insert(b.curline, msg) b.buf.insert(b.curline, msg)
b.curline = b.curline + 1 b.curline = b.curline + 1

View File

@ -1,16 +1,16 @@
client = None client = None
def mod_init(cl): def mod_init(cl):
global client global client
client = cl client = cl
def on_pubchat(who, chan, msg): def on_pubchat(who, chan, msg):
if who.split('!')[0] == 'guiniolBot': if who.split('!')[0] == 'guiniolBot':
if msg.find('aurez') != -1: if msg.find('aurez') != -1:
client.sendto(chan, '%cACTION sort un fusil sniper et ajuste la visee sur guiniolBot...%c' % (1, 1)) client.sendto(chan, '%cACTION sort un fusil sniper et ajuste la visee sur guiniolBot...%c' % (1, 1))

385
ircbot.py
View File

@ -1,192 +1,193 @@
import threading import threading
import string import string
import socket import socket
import sys import sys
#server = "casimir.ponteilla.net" #server = "casimir.ponteilla.net"
server = "localhost" server = "localhost"
port = 6667 port = 6667
chan = "#pno" chan = "#pno"
nick = "PaulBot" nick = "PaulBot"
class ModNotFoundException(Exception): class ModNotFoundException(Exception):
pass pass
def getnick(user): def getnick(user):
return user.split('!')[0] return user.split('!')[0]
class monmodule: class monmodule:
def __init__(self, name, client): def __init__(self, name, client):
self.name = name self.name = name
if name in sys.modules: if name in sys.modules:
reload(sys.modules[name]) reload(sys.modules[name])
self.mod = __import__(name) self.mod = __import__(name)
self.mod.mod_init(client) self.mod.mod_init(client)
class TCPClient: class TCPClient:
cmds = ["privmsg", "ping"] #"mode", "join", "part", "quit"] cmds = ["privmsg", "ping"] #"mode", "join", "part", "quit"]
def __init__(self, sock, nick): def __init__(self, sock, nick):
self.sock = sock self.sock = sock
self.nick = nick self.nick = nick
self.modules = [] self.modules = []
def send(self, msg): def send(self, msg):
self.sock.send(":%s %s\r\n" % (self.nick, msg)) self.sock.send(":%s %s\r\n" % (self.nick, msg))
print "=== sent: :%s %s" % (self.nick, msg) print "=== sent: :%s %s" % (self.nick, msg)
def pubchat(self, chan, msg): def pubchat(self, chan, msg):
self.send("PRIVMSG %s :%s" % (chan, msg)) self.send("PRIVMSG %s :%s" % (chan, msg))
def sendto(self, who, msg): def sendto(self, who, msg):
self.pubchat(getnick(who), msg) self.pubchat(getnick(who), msg)
def handle(self, data): def handle(self, data):
#print data #print data
if data[0] != ':': if data[0] != ':':
data = ':none ' + data data = ':none ' + data
args = data.split(':')[1:] args = data.split(':')[1:]
args = args[0].split() + [':'.join(args[1:])] args = args[0].split() + [':'.join(args[1:])]
cmd = args[1].lower() cmd = args[1].lower()
# print "=== CMD(%s)" % cmd # print "=== CMD(%s)" % cmd
if cmd in self.cmds: if cmd in self.cmds:
getattr(self, "on_" + cmd)(args[0], args[2:]) getattr(self, "on_" + cmd)(args[0], args[2:])
def onconnect(self): def onconnect(self):
self.sock.send("NICK %s\r\n" % nick) self.sock.send("NICK %s\r\n" % nick)
self.sock.send("USER paulbot ponteilla.net blektr :PaulBot\r\n") self.sock.send("USER paulbot ponteilla.net blektr :PaulBot\r\n")
self.send("JOIN #pno") self.send("JOIN #pno")
def find_mod(self, name): def find_mod(self, name):
try: try:
i = [x.name for x in self.modules].index(name) i = [x.name for x in self.modules].index(name)
except ValueError: except ValueError:
raise ModNotFoundException() raise ModNotFoundException()
else: else:
return self.modules[i] return self.modules[i]
def remove_mod(self, name): def remove_mod(self, name):
mod = self.find_mod(name) mod = self.find_mod(name)
reload(mod.mod) reload(mod.mod)
self.modules.remove(mod) self.modules.remove(mod)
def handle_command(self, who, cmd): def handle_command(self, who, cmd):
if len(cmd.split()) < 1: if len(cmd.split()) < 1:
return return
ncmd = cmd.split()[0] ncmd = cmd.split()[0]
nargs = cmd.split()[1:] nargs = cmd.split()[1:]
if ncmd == "reload": if ncmd == "reload":
try: try:
mod = self.find_mod(nargs[0]) mod = self.find_mod(nargs[0])
except ModNotFoundException: except ModNotFoundException:
self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0])
else: else:
self.sendto(who, "Reloading module '%s'..." % nargs[0]) self.sendto(who, "Reloading module '%s'..." % nargs[0])
try: try:
reload(mod.mod) reload(mod.mod)
mod.mod.mod_init(self) mod.mod.mod_init(self)
except Exception, e: except Exception, e:
self.sendto(who, "ERROR: %s" % str(e)) self.sendto(who, "ERROR: %s" % str(e))
if ncmd == "unload": if ncmd == "unload":
try: try:
self.remove_mod(nargs[0]) self.remove_mod(nargs[0])
except ModNotFoundException: except ModNotFoundException:
self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0]) self.sendto(who, "ERROR: Module '%s' not loaded" % nargs[0])
else: else:
self.sendto(who, "Module '%s' unloaded." % nargs[0]) self.sendto(who, "Module '%s' unloaded." % nargs[0])
if ncmd == "load": if ncmd == "load":
if nargs[0] in [x.name for x in self.modules]: for arg in nargs:
self.sendto(who, "ERROR: Module '%s' already loaded" % str(e)) if arg in [x.name for x in self.modules]:
else: self.sendto(who, "ERROR: Module '%s' already loaded" % arg)
try: else:
mod = monmodule(nargs[0], self) try:
except Exception, e: mod = monmodule(arg, self)
self.sendto(who, "ERROR: %s" % str(e)) except Exception, e:
else: self.sendto(who, "ERROR: %s" % str(e))
self.modules.append(mod) else:
self.sendto(who, "Module '%s' loaded." % nargs[0]) 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: def on_pubchat(self, who, chan, msg):
nmsg = ':'.join(msg.split(':')[1:]).lstrip() if msg[:len(self.nick)] == self.nick:
self.handle_command(chan, nmsg) nmsg = ':'.join(msg.split(':')[1:]).lstrip()
self.handle_command(chan, nmsg)
for mod in self.modules:
if 'on_pubchat' in dir(mod.mod): for mod in self.modules:
try: if 'on_pubchat' in dir(mod.mod):
mod.mod.on_pubchat(who, chan, msg) try:
except Exception, e: mod.mod.on_pubchat(who, chan, msg)
self.sendto(chan, str(e)) except Exception, e:
self.sendto(chan, "Module '%s' unloaded" % mod.name) self.sendto(chan, str(e))
self.remove_mod(mod.name) self.sendto(chan, "Module '%s' unloaded" % mod.name)
self.remove_mod(mod.name)
def on_privchat(self, who, msg):
self.handle_command(who, msg) def on_privchat(self, who, msg):
self.handle_command(who, msg)
for mod in self.modules:
if 'on_privchat' in dir(mod.mod): for mod in self.modules:
try: if 'on_privchat' in dir(mod.mod):
mod.mod.on_privchat(who, msg) try:
except Exception, e: mod.mod.on_privchat(who, msg)
self.sendto(getnick(who), str(e)) except Exception, e:
self.sendto(getnick(who), "Module '%s' unloaded" % mod.name) self.sendto(getnick(who), str(e))
self.remove_mod(mod.name) self.sendto(getnick(who), "Module '%s' unloaded" % mod.name)
self.remove_mod(mod.name)
def on_privmsg(self, who, params):
towho = params[0] def on_privmsg(self, who, params):
msg = params[1] towho = params[0]
msg = params[1]
if getnick(towho) == self.nick:
self.on_privchat(who, msg) if getnick(towho) == self.nick:
else: self.on_privchat(who, msg)
self.on_pubchat(who, towho, msg) else:
self.on_pubchat(who, towho, msg)
print "(%s) %s: %s" % (towho, who.split('!')[0], msg)
print "(%s) %s: %s" % (towho, who.split('!')[0], msg)
def on_ping(self, who, params):
print "PING? PONG!" def on_ping(self, who, params):
self.send("PONG %s" % self.nick) print "PING? PONG!"
self.send("PONG %s" % self.nick)
if __name__ == "__main__":
if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client = TCPClient(sock, nick) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client = TCPClient(sock, nick)
print "Connecting to %s:%d" % (server, port)
try: print "Connecting to %s:%d" % (server, port)
sock.connect((server, port)) try:
except socket.error: sock.connect((server, port))
sock.close() except socket.error:
print "ERR: could not connect!" sock.close()
exit(1) print "ERR: could not connect!"
exit(1)
client.onconnect()
client.onconnect()
try:
while True: try:
data = sock.recv(2048) while True:
if not data: data = sock.recv(2048)
break if not data:
for s in data.split('\r\n'): break
if s.rstrip() != "": for s in data.split('\r\n'):
client.handle(s) if s.rstrip() != "":
except KeyboardInterrupt: client.handle(s)
client.sendto("#pno", except KeyboardInterrupt:
"Aiiieuuuh, me suis pris un C-c dans les dents :/") client.sendto("#pno",
finally: "Aiiieuuuh, me suis pris un C-c dans les dents :/")
sock.close() finally:
for m in client.modules: sock.close()
if 'on_die' in dir(m.mod): for m in client.modules:
m.mod.on_die() if 'on_die' in dir(m.mod):
m.mod.on_die()

View File

@ -1,61 +1,61 @@
import random import random
import threading import threading
import time import time
client = None client = None
next = None next = None
def on_die(): def on_die():
next.cancel() next.cancel()
def vide_blanc(): def vide_blanc():
i = random.randint(0, len(meublage)-1) i = random.randint(0, len(meublage)-1)
client.pubchat("#pno", meublage[i]) client.pubchat("#pno", meublage[i])
rearmer() rearmer()
def rearmer(): def rearmer():
global next global next
if next is not None: if next is not None:
next.cancel() next.cancel()
next = threading.Timer(random.randint(1200, 9000), vide_blanc) next = threading.Timer(random.randint(1200, 9000), vide_blanc)
next.start() next.start()
def mod_init(cl): def mod_init(cl):
global client, next global client, next
client = cl client = cl
random.seed() random.seed()
rearmer() rearmer()
reponses = [ reponses = [
"bien dit !", "bien dit !",
"pfff...", "pfff...",
"serieux ? o_O", "serieux ? o_O",
"Hello guiniolBot!", "Hello guiniolBot!",
"au fait, juste comme ca, sans vouloir balancer des fleurs ou quoi, mais mon createur est un type bien", "au fait, juste comme ca, sans vouloir balancer des fleurs ou quoi, mais mon createur est un type bien",
"oh ! mais il parle !", "oh ! mais il parle !",
"je sais que je ne fais que repeter des phrases aleatoires, mais si je peux aider a meubler un peu...", "je sais que je ne fais que repeter des phrases aleatoires, mais si je peux aider a meubler un peu...",
"bwaaAAaaHahhahahaaahaahahaaaa!!", "bwaaAAaaHahhahahaaahaahahaaaa!!",
"ou pas.", "ou pas.",
"nan mais meme si c'etait vrai, t'as vu ta tronche ?" "nan mais meme si c'etait vrai, t'as vu ta tronche ?"
] ]
meublage = [ meublage = [
"Aaa.. TCHA!", "Aaa.. TCHA!",
"Hello guiniolBot :D", "Hello guiniolBot :D",
"Celui qui lit ceci ferait mieux de lire autre chose", "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...", "mais dis donc c'est aussi anime qu'a une soiree scrabble chez les grand-parents ici...",
"bwaarggll" "bwaarggll"
] ]
def on_pubchat(who, chan, msg): def on_pubchat(who, chan, msg):
if (msg.find('bwaarggll') != -1): if (msg.find('bwaarggll') != -1):
client.pubchat(chan, "BwAaRgGlL!1!!") client.pubchat(chan, "BwAaRgGlL!1!!")
i = random.randint(0, len(reponses)-1) i = random.randint(0, len(reponses)-1)
if random.randint(0, 1024) < 42: if random.randint(0, 1024) < 42:
client.pubchat(chan, reponses[i]) client.pubchat(chan, reponses[i])
rearmer() rearmer()