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 threading
import urllib.request
bots, chan, name = {}, "", "BabelBot"
servers = { "fr": "", "en": "" }
trans = ""
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(
def __init__(self, name, url):, [(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):
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)
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():
post = urllib.parse.urlencode({ "source": "auto",
"target": target, "q": ev.arguments[0] })
with urllib.request.urlopen(trans, post.encode()) as f:
response = json.loads(
if "translatedText" in response.keys():
bots[nick].say(, response["translatedText"])
except Exception:
bots[nick].say(, "�[ "+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