Commit 8c064615 authored by Sébastien Dufromentel's avatar Sébastien Dufromentel
Browse files

Rewrited some badly working parts.

parent 0a8a9bab
......@@ -22,27 +22,37 @@
######
import ssl
import sys
import json
import time
import signal
import irc.bot
import threading
import collections
import urllib.request
chan = "#play.it"
chan, bots = "#play.it", {}
ips = collections.defaultdict(lambda:
"2a00:5884:8300::01"+hex(len(ips))[2:].zfill(2))
ignore = set(irc.strings.IRCFoldedCase(n) for n in
("BabelBot", "C", "ChanServ"))
servers = { "fr": "irc.oftc.net", "en": "irc.geeknode.org" }
ignore = set(irc.strings.IRCFoldedCase(n) for n in ("C", "ChanServ"))
trans = "https://libretranslate.de/translate"
def botnick(usernick, lang):
if usernick.endswith("[m]"):
return irc.strings.IRCFoldedCase(usernick[:-1]+"-"+lang+"]")
if usernick in ignore:
return None # Nope.
if usernick.endswith("]"):
if usernick.endswith("fr]") or usernick.endswith("en]"):
return None # It was already a bot nick, not needed.
return irc.strings.IRCFoldedCase(usernick[:-1]+"|"+lang+"]")
return irc.strings.IRCFoldedCase(usernick+"["+lang+"]")
def translate(message, origin, target):
def usernick(botnick, lang):
if not botnick.endswith(lang+"]"):
return None # Not a bot nick.
nick = botnick[:-len(lang)-1] # Cleaning…
return irc.strings.IRCFoldedCase(nick[:-1]+"]"
if usernick.endswith("|") else nick)
def translate(message, target):
try: # Don't interrupt.
if message == "\o/":
return message
......@@ -53,37 +63,21 @@ def translate(message, origin, target):
except Exception: # Probably a server error.
return "�[ "+message+" ]�"
def check(main, *args):
if main.conn: # It's alive.
for nick, bot in main.bots.items():
if not bot.conn and time.time()-bot.stamp > 1:
main.bots.pop(nick).die() # Cleaning first…
main.bots[nick] = Bot(botnick(nick, main.origin),
nick, main.server, target=main.target)
elif time.time()-main.stamp > 2: # Need to relaunch it.
main.die() # Let's try this again with another bot.
return Babel(*args, bots=main.bots, server=main.server,
origin=main.origin, target=main.target)
return main # And that's done.
class data(object):
def __init__(self, **args):
for name, arg in args.items():
setattr(self, name, arg)
class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, nick, name, url, **args):
self.conn, self.stamp = False, time.time()
irc.bot.SingleServerIRCBot.__init__(self, [(url, 6697)], nick, name,
def __init__(self, nick, name):
bots[nick], sockinfos = self, [ (servers[nick[-3:-1]], 6697) ]
irc.bot.SingleServerIRCBot.__init__(self, sockinfos, nick, name, 5,
connect_factory=irc.connection.Factory(bind_address=(ips[nick],
6697), wrapper=ssl.SSLContext().wrap_socket, ipv6=True))
threading.Thread(None, self.start, daemon=True).start()
for name, arg in args.items():
setattr(self, name, arg)
def on_welcome(self, conn, ev):
self.conn = conn
conn.join(chan)
self.connection.join(chan)
def on_privmsg(self, conn, ev):
pass #TODO answer that I'm just a bot.
......@@ -94,19 +88,19 @@ class Babel(Bot):
self.on_join(conn, data(source=data(nick=nick)))
def on_join(self, conn, ev):
nick = irc.strings.IRCFoldedCase(ev.source.nick)
if nick not in ignore.union(self.bots.keys()):
if not nick.endswith(self.target+"]"):
self.bots[nick] = Bot(botnick(nick, self.origin),
nick, self.server, target=self.target)
nick = botnick(ev.source.nick, self.botslang)
if nick is not None: # Let's work with it.
if nick in bots.keys(): # Already here.
return bots[nick].connection.join(chan)
Bot(nick, ev.source.nick) #FIXME read name?
def on_nick(self, conn, ev):
pass #TODO quit + join?
def on_quit(self, conn, ev):
nick = irc.strings.IRCFoldedCase(ev.source.nick)
if nick in self.bots.keys():
self.bots.pop(nick).die()
nick = botnick(ev.source.nick, self.botslang)
if nick in bots.keys():
bots[nick].connection.part((chan,))
def on_part(self, conn, ev):
self.on_quit(conn, ev)
......@@ -115,20 +109,13 @@ class Babel(Bot):
self.on_quit(conn, ev)
def on_pubmsg(self, conn, ev):
nick = irc.strings.IRCFoldedCase(ev.source.nick)
if nick in self.bots.keys():
message = translate(ev.arguments[0], self.origin, self.target)
if self.bots[nick].conn:
self.bots[nick].conn.privmsg(chan, message)
nick = botnick(ev.source.nick, self.botslang)
if nick in bots.keys(): # Let's propagate the message…
bots[nick].connection.privmsg(chan, translate(ev.arguments[0],
"fr" if self.botslang == "en" else "en"))
frbot = data(bots={}, conn=False, stamp=-1, die=lambda: None,
origin="en", target="fr", server="irc.geeknode.org")
enbot = data(bots={}, conn=False, stamp=-1, die=lambda: None,
origin="fr", target="en", server="irc.oftc.net")
Babel("BabelBot[fr]", "Cool translating bot").botslang = "en"
Babel("BabelBot[en]", "Chouette bot traducteur").botslang = "fr"
while True:
frbot = check(frbot, "BabelBot[fr]",
"Cool translating bot", "irc.oftc.net")
enbot = check(enbot, "BabelBot[en]",
"Chouette bot traducteur", "irc.geeknode.org")
time.sleep(0.5)
if __name__ == "__main__":
signal.pause()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment