Skip to content

Instantly share code, notes, and snippets.

@sanderfoobar
Created November 27, 2022 23:13
Show Gist options
  • Save sanderfoobar/f0ed4a1181d9d966266f1975e14c4724 to your computer and use it in GitHub Desktop.
Save sanderfoobar/f0ed4a1181d9d966266f1975e14c4724 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# IRC channel Bulgarian->English translation
# pip install bottom kotki -v
import re, time, logging, asyncio, os
import bottom, kotki
IRC_NICK = "bgen"
IRC_HOST = "localhost"
IRC_PORT = 6667
IRC_TLS = False
IRC_SOURCE_CHANNEL = "#lol"
IRC_TARGET_CHANNEL = "#lol-en"
def has_cyrillic(text):
return bool(re.search('[а-яА-Я]', text))
IGNORE_NICKS = ['initLabNotifier']
bot = bottom.Client(host=IRC_HOST, port=IRC_PORT, ssl=IRC_TLS, loop=asyncio.get_event_loop())
kotki.loadRegistry(os.path.abspath("models/registry.json"))
@bot.on('CLIENT_CONNECT')
async def connect(**kwargs):
bot.send('NICK', nick=IRC_NICK)
bot.send('USER', user=IRC_NICK, realname=IRC_NICK)
done, pending = await asyncio.wait(
[bot.wait("RPL_ENDOFMOTD"), bot.wait("ERR_NOMOTD")],
loop=bot.loop,
return_when=asyncio.FIRST_COMPLETED
)
for future in pending:
future.cancel()
bot.send('JOIN', channel=IRC_SOURCE_CHANNEL)
bot.send('JOIN', channel=IRC_TARGET_CHANNEL)
@bot.on('PING')
def keepalive(message, **kwargs):
bot.send('PONG', message=message)
@bot.on('client_disconnect')
def reconnect(**kwargs):
logging.warning("Lost IRC server connection")
time.sleep(3)
bot.loop.create_task(bot.connect())
logging.warning("Reconnecting to IRC server")
@bot.on('PRIVMSG')
async def message_received(nick, target, message, **kwargs):
if nick == IRC_NICK:
return
if nick in IGNORE_NICKS:
return
if target == IRC_NICK:
target = nick
if target != IRC_SOURCE_CHANNEL:
return
if "telegram" in nick.lower() and message.strip().startswith("<"):
nick = message[1:]
nick = nick[:nick.find(">")]
message = message[message.find(">") + 1:].strip()
if has_cyrillic(message):
message = kotki.translate(message, "bgen")
bot.send("PRIVMSG", target=IRC_TARGET_CHANNEL, message=f"<{nick}> {message}")
async def main():
bot.loop.create_task(bot.connect())
while True:
await asyncio.sleep(60) # dirty hack
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment