Commit 5a982591 authored by Sébastien Dufromentel's avatar Sébastien Dufromentel
Browse files

Proof of Concept

parent ea6ab3cb
#! /usr/bin/python3
# coding: utf-8
import sys
import ssl
import json
import time
import irc.bot
import threading
import urllib.request
bots, chan, name = {}, "#play.it", "BabelBot"
servers = { "fr": "irc.geeknode.org", "en": "irc.oftc.net" }
trans = "https://libretranslate.de/translate"
origin = sys.argv[1]
target = tuple(x for x in servers.keys() if x != origin)[0]
def ip():
base = len(bots)
if origin == "fr":
base = 0xff-base
return "2a00:5884:8300::01"+hex(base)[2:].zfill(2)
class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, name, url):
irc.bot.SingleServerIRCBot.__init__(self, [(url, 6697)], name, name,
connect_factory=irc.connection.Factory(bind_address=(ip(), 6697),
wrapper=ssl.SSLContext().wrap_socket, ipv6=True))
self.pendings = []
def on_welcome(self, conn, ev):
conn.join(chan)
self.conn = conn
while len(self.pendings) > 0:
self.say(chan, self.pendings.pop(0))
def say(self, chan, message):
if not hasattr(self, "conn"):
return self.pendings.append(message)
self.conn.privmsg(chan, message)
class Babel(Bot):
def on_join(self, conn, nick):
if hasattr(nick, "source"):
nick = nick.source.nick
nick = irc.strings.IRCFoldedCase(nick)
if nick != name and not nick.endswith("["+target+"]"):
if nick not in bots.keys():
bots[nick] = Bot(nick+"["+origin+"]", servers[target])
threading.Thread(None, bots[nick].start, daemon=True).start()
def on_endofnames(self, conn, ev):
def joining():
for nick in tuple(self.channels[chan].users()):
self.on_join(conn, nick)
time.sleep(0.2)
threading.Thread(None, joining, daemon=True).start()
def on_pubmsg(self, conn, ev):
nick = irc.strings.IRCFoldedCase(ev.source.nick)
if nick in bots.keys():
try:
post = urllib.parse.urlencode({ "source": "auto",
"target": target, "q": ev.arguments[0] })
with urllib.request.urlopen(trans, post.encode()) as f:
response = json.loads(f.read())
if "translatedText" in response.keys():
bots[nick].say(ev.target, response["translatedText"])
except Exception:
bots[nick].say(ev.target, "�[ "+ev.arguments[0]+" ]�")
Babel(name, servers[origin]).start()
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