2021-01-27 13:18:09 +01:00
|
|
|
import asyncio
|
2021-01-11 07:44:16 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import re
|
2021-01-27 13:18:09 +01:00
|
|
|
import traceback
|
2021-01-11 07:44:16 +01:00
|
|
|
import sys
|
|
|
|
import uuid
|
2021-01-10 18:40:11 +01:00
|
|
|
import aiofiles
|
|
|
|
import aiofiles.os
|
|
|
|
import aiohttp
|
2020-11-14 16:34:30 +01:00
|
|
|
import discord
|
2021-01-10 18:40:11 +01:00
|
|
|
import discord.ext.commands
|
2020-12-01 07:38:59 +01:00
|
|
|
import nio
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def config_gen(config_file):
|
|
|
|
config_dict = {
|
|
|
|
"homeserver": "https://matrix.org",
|
|
|
|
"username": "@name:matrix.org",
|
|
|
|
"password": "my-secret-password",
|
2020-12-12 11:12:05 +01:00
|
|
|
"token": "my-secret-token",
|
2021-01-02 13:58:24 +01:00
|
|
|
"discord_prefix": "my-command-prefix",
|
2021-01-10 18:40:11 +01:00
|
|
|
"bridge": {"channel_id": "room_id"}
|
2020-11-11 16:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if not os.path.exists(config_file):
|
|
|
|
with open(config_file, "w") as f:
|
|
|
|
json.dump(config_dict, f, indent=4)
|
|
|
|
print(f"Example configuration dumped to {config_file}")
|
2021-01-11 07:44:16 +01:00
|
|
|
sys.exit()
|
2020-11-11 16:46:15 +01:00
|
|
|
|
|
|
|
with open(config_file, "r") as f:
|
|
|
|
config = json.loads(f.read())
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
config = config_gen("config.json")
|
|
|
|
|
2021-01-27 13:18:09 +01:00
|
|
|
message_store = {}
|
2020-11-14 16:14:16 +01:00
|
|
|
|
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
class MatrixClient(nio.AsyncClient):
|
2021-02-01 07:03:27 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
2021-01-10 18:40:11 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2020-12-30 15:31:18 +01:00
|
|
|
self.logger = logging.getLogger("matrix_logger")
|
2021-01-27 13:18:09 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
self.listen = False
|
2021-01-10 18:40:11 +01:00
|
|
|
self.uploaded_emotes = {}
|
2021-02-01 07:03:27 +01:00
|
|
|
self.ready = asyncio.Event()
|
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
self.start_discord()
|
|
|
|
self.add_callbacks()
|
|
|
|
|
|
|
|
def start_discord(self):
|
|
|
|
# Disable everyone and role mentions.
|
|
|
|
allowed_mentions = discord.AllowedMentions(everyone=False, roles=False)
|
|
|
|
# Set command prefix for Discord bot.
|
|
|
|
command_prefix = config["discord_prefix"]
|
|
|
|
# Intents to fetch members from guild.
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.members = True
|
|
|
|
|
|
|
|
self.discord_client = DiscordClient(
|
|
|
|
self, allowed_mentions=allowed_mentions,
|
|
|
|
command_prefix=command_prefix, intents=intents
|
|
|
|
)
|
2020-12-30 15:31:18 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
self.bg_task = self.loop.create_task(
|
|
|
|
self.discord_client.start(config["token"])
|
|
|
|
)
|
2020-11-16 11:38:23 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
def add_callbacks(self):
|
2021-01-27 13:18:09 +01:00
|
|
|
callbacks = Callbacks(self.discord_client, self)
|
2020-12-30 15:31:18 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
self.add_event_callback(
|
2020-11-30 13:35:49 +01:00
|
|
|
callbacks.message_callback,
|
2020-12-07 15:20:05 +01:00
|
|
|
(nio.RoomMessageText, nio.RoomMessageMedia,
|
2020-12-13 08:02:20 +01:00
|
|
|
nio.RoomMessageEmote)
|
|
|
|
)
|
2020-11-16 11:38:23 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
self.add_event_callback(
|
2020-12-13 08:02:20 +01:00
|
|
|
callbacks.redaction_callback, nio.RedactionEvent
|
|
|
|
)
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
self.add_ephemeral_callback(
|
2020-12-13 08:02:20 +01:00
|
|
|
callbacks.typing_callback, nio.EphemeralEvent
|
|
|
|
)
|
2020-11-13 15:04:31 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
async def upload_emote(self, emote_id):
|
|
|
|
if emote_id in self.uploaded_emotes.keys():
|
|
|
|
return self.uploaded_emotes[emote_id]
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
emote_url = f"https://cdn.discordapp.com/emojis/{emote_id}"
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
emote_file = f"/tmp/{str(uuid.uuid4())}"
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(emote_url) as resp:
|
|
|
|
emote = await resp.read()
|
|
|
|
content_type = resp.content_type
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
async with aiofiles.open(emote_file, "wb") as f:
|
|
|
|
await f.write(emote)
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
async with aiofiles.open(emote_file, "rb") as f:
|
|
|
|
resp, maybe_keys = await self.upload(
|
|
|
|
f, content_type=content_type
|
|
|
|
)
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
await aiofiles.os.remove(emote_file)
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
if type(resp) != nio.UploadResponse:
|
|
|
|
self.logger.warning(
|
|
|
|
f"Failed to upload emote {emote_id}"
|
|
|
|
)
|
|
|
|
return
|
2021-01-10 18:40:11 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
self.uploaded_emotes[emote_id] = resp.content_uri
|
|
|
|
|
|
|
|
return resp.content_uri
|
2021-01-11 07:44:16 +01:00
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
async def get_fmt_body(self, body, emotes):
|
2021-01-15 12:21:04 +01:00
|
|
|
replace_ = [
|
|
|
|
# Code blocks
|
|
|
|
("```", "<pre><code>", "</code></pre>"),
|
|
|
|
# Spoilers
|
2021-01-21 09:04:42 +01:00
|
|
|
("||", "<span data-mx-spoiler>", "</span>"),
|
|
|
|
# Strikethrough
|
|
|
|
("~~", "<del>", "</del>")
|
2021-01-15 12:21:04 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
for replace in replace_:
|
|
|
|
for i in range(body.count(replace[0])):
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if i % 2:
|
|
|
|
body = body.replace(replace[0], replace[1], 1)
|
|
|
|
else:
|
|
|
|
body = body.replace(replace[0], replace[2], 1)
|
2021-01-10 18:40:11 +01:00
|
|
|
|
|
|
|
for emote in emotes.keys():
|
2021-01-11 15:50:20 +01:00
|
|
|
emote_ = await self.upload_emote(emotes[emote])
|
2021-01-10 18:40:11 +01:00
|
|
|
if emote_:
|
|
|
|
emote = f":{emote}:"
|
2021-01-11 15:50:20 +01:00
|
|
|
body = body.replace(
|
2021-01-24 06:59:49 +01:00
|
|
|
emote, f"""<img alt=\"{emote}\" title=\"{emote}\" \
|
2021-01-12 12:50:27 +01:00
|
|
|
height=\"32\" src=\"{emote_}\" data-mx-emoticon />"""
|
2021-01-10 18:40:11 +01:00
|
|
|
)
|
|
|
|
|
2021-01-11 15:50:20 +01:00
|
|
|
return body
|
2021-01-10 18:40:11 +01:00
|
|
|
|
|
|
|
async def message_send(self, message, channel_id, emotes,
|
2020-12-12 11:12:05 +01:00
|
|
|
reply_id=None, edit_id=None):
|
|
|
|
room_id = config["bridge"][str(channel_id)]
|
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
content = {
|
2021-01-11 07:44:16 +01:00
|
|
|
"body": message,
|
2021-01-10 18:40:11 +01:00
|
|
|
"format": "org.matrix.custom.html",
|
2021-01-11 15:50:20 +01:00
|
|
|
"formatted_body": await self.get_fmt_body(message, emotes),
|
2021-01-10 18:40:11 +01:00
|
|
|
"msgtype": "m.text"
|
2020-11-30 13:35:49 +01:00
|
|
|
}
|
2020-11-16 11:38:23 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
if reply_id:
|
2020-12-01 09:27:36 +01:00
|
|
|
reply_event = await self.room_get_event(
|
2020-12-24 11:11:35 +01:00
|
|
|
room_id, reply_id
|
2020-11-30 16:14:17 +01:00
|
|
|
)
|
2020-12-24 11:11:35 +01:00
|
|
|
reply_event = reply_event.event
|
2020-11-30 16:14:17 +01:00
|
|
|
|
2021-01-11 07:44:16 +01:00
|
|
|
content["m.relates_to"] = {"m.in_reply_to": {"event_id": reply_id}}
|
2020-11-25 07:35:07 +01:00
|
|
|
|
2021-01-10 18:40:11 +01:00
|
|
|
content["formatted_body"] = f"""<mx-reply><blockquote>\
|
|
|
|
<a href="https://matrix.to/#/{room_id}/{reply_id}">In reply to</a>\
|
|
|
|
<a href="https://matrix.to/#/{reply_event.sender}">{reply_event.sender}</a>\
|
|
|
|
<br>{reply_event.body}</blockquote></mx-reply>{content["formatted_body"]}"""
|
2020-11-30 16:14:17 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
if edit_id:
|
2021-01-11 05:58:48 +01:00
|
|
|
content["body"] = f" * {content['body']}"
|
2020-11-14 17:03:06 +01:00
|
|
|
|
2021-01-11 07:44:16 +01:00
|
|
|
content["m.relates_to"] = {
|
|
|
|
"event_id": edit_id, "rel_type": "m.replace"
|
|
|
|
}
|
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
content["m.new_content"] = {
|
2021-01-11 05:58:48 +01:00
|
|
|
"body": content["body"],
|
2021-01-10 18:40:11 +01:00
|
|
|
"formatted_body": content["formatted_body"],
|
2021-01-11 05:58:48 +01:00
|
|
|
"format": content["format"],
|
|
|
|
"msgtype": content["msgtype"]
|
2020-11-30 13:35:49 +01:00
|
|
|
}
|
2020-11-14 17:03:06 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
message = await self.room_send(
|
2020-12-12 11:12:05 +01:00
|
|
|
room_id=room_id,
|
2020-11-30 13:35:49 +01:00
|
|
|
message_type="m.room.message",
|
|
|
|
content=content
|
|
|
|
)
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
return message.event_id
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2020-12-12 11:12:05 +01:00
|
|
|
async def message_redact(self, message, channel_id):
|
2020-12-01 09:27:36 +01:00
|
|
|
await self.room_redact(
|
2020-12-12 11:12:05 +01:00
|
|
|
room_id=config["bridge"][str(channel_id)],
|
2020-11-30 13:35:49 +01:00
|
|
|
event_id=message
|
|
|
|
)
|
2020-11-14 16:14:16 +01:00
|
|
|
|
2020-12-12 11:12:05 +01:00
|
|
|
async def webhook_send(self, author, avatar, message,
|
2021-01-03 11:54:01 +01:00
|
|
|
event_id, channel_id, embed=None):
|
2021-01-27 13:18:09 +01:00
|
|
|
channel = self.discord_client.channel_store[channel_id]
|
2020-12-12 11:12:05 +01:00
|
|
|
|
2021-02-06 08:20:03 +01:00
|
|
|
# Recreate hook if it was deleted.
|
|
|
|
hook = await self.discord_client.hook_create(channel)
|
2020-12-01 05:58:06 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Username must be between 1 and 80 characters in length,
|
|
|
|
# 'wait=True' allows us to store the sent message.
|
2020-12-01 05:58:06 +01:00
|
|
|
try:
|
2021-01-03 11:54:01 +01:00
|
|
|
hook = await hook.send(
|
|
|
|
username=author[:80], avatar_url=avatar,
|
|
|
|
content=message, embed=embed, wait=True
|
|
|
|
)
|
2021-01-11 05:58:48 +01:00
|
|
|
|
2020-12-01 05:58:06 +01:00
|
|
|
message_store[event_id] = hook
|
2020-12-08 11:34:04 +01:00
|
|
|
message_store[hook.id] = event_id
|
2020-12-01 05:58:06 +01:00
|
|
|
except discord.errors.HTTPException as e:
|
2020-12-30 15:31:18 +01:00
|
|
|
self.logger.warning(f"Failed to send message {event_id}: {e}")
|
2020-12-01 09:27:36 +01:00
|
|
|
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2021-01-02 13:58:24 +01:00
|
|
|
class DiscordClient(discord.ext.commands.Bot):
|
2021-02-01 07:03:27 +01:00
|
|
|
def __init__(self, matrix_client, *args, **kwargs):
|
2020-11-30 16:27:21 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2021-01-27 13:18:09 +01:00
|
|
|
self.channel_store = {}
|
|
|
|
|
2021-02-06 15:33:10 +01:00
|
|
|
self.webhook_ids = set()
|
2021-02-06 08:20:03 +01:00
|
|
|
|
2021-01-27 13:18:09 +01:00
|
|
|
self.ready = asyncio.Event()
|
|
|
|
|
|
|
|
self.add_cogs()
|
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
self.matrix_client = matrix_client
|
2020-11-30 16:27:21 +01:00
|
|
|
|
2021-01-02 13:58:24 +01:00
|
|
|
def add_cogs(self):
|
2021-01-21 11:38:04 +01:00
|
|
|
cogs_dir = "./cogs"
|
|
|
|
|
|
|
|
if not os.path.isdir(cogs_dir):
|
|
|
|
return
|
|
|
|
|
|
|
|
for cog in os.listdir(cogs_dir):
|
2021-01-02 13:58:24 +01:00
|
|
|
if cog.endswith(".py"):
|
|
|
|
cog = f"cogs.{cog[:-3]}"
|
|
|
|
self.load_extension(cog)
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2021-02-06 08:20:03 +01:00
|
|
|
async def hook_create(self, channel):
|
|
|
|
hook_name = "matrix_bridge"
|
|
|
|
|
|
|
|
hooks = await channel.webhooks()
|
|
|
|
|
|
|
|
# Check if webhook exists.
|
|
|
|
hook = discord.utils.get(hooks, name=hook_name)
|
|
|
|
if not hook:
|
|
|
|
hook = await channel.create_webhook(name=hook_name)
|
|
|
|
|
2021-02-06 15:33:10 +01:00
|
|
|
self.webhook_ids.add(hook.id)
|
2021-02-06 08:20:03 +01:00
|
|
|
|
|
|
|
return hook
|
|
|
|
|
|
|
|
async def to_return(self, channel_id, message=None):
|
2021-01-27 13:18:09 +01:00
|
|
|
await self.matrix_client.ready.wait()
|
|
|
|
|
2021-02-06 08:20:03 +01:00
|
|
|
if str(channel_id) not in config["bridge"].keys():
|
2021-01-03 08:33:30 +01:00
|
|
|
return True
|
|
|
|
|
2021-02-06 08:20:03 +01:00
|
|
|
if message:
|
|
|
|
if message.webhook_id in self.webhook_ids:
|
|
|
|
return True
|
|
|
|
|
2021-01-02 13:58:24 +01:00
|
|
|
async def on_ready(self):
|
2020-12-12 11:12:05 +01:00
|
|
|
for channel in config["bridge"].keys():
|
2021-02-06 08:20:03 +01:00
|
|
|
channel_ = self.get_channel(int(channel))
|
|
|
|
self.channel_store[channel] = channel_
|
|
|
|
|
|
|
|
await self.hook_create(channel_)
|
2021-01-27 13:18:09 +01:00
|
|
|
|
|
|
|
self.ready.set()
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def on_message(self, message):
|
2021-01-21 11:38:04 +01:00
|
|
|
# Process other stuff like cogs before ignoring the message.
|
2021-01-02 13:58:24 +01:00
|
|
|
await self.process_commands(message)
|
|
|
|
|
2021-02-06 08:20:03 +01:00
|
|
|
if await self.to_return(message.channel.id, message):
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
content = await self.process_message(message)
|
2020-11-12 15:42:23 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
matrix_message = await self.matrix_client.message_send(
|
2021-01-10 18:40:11 +01:00
|
|
|
content[0], message.channel.id,
|
|
|
|
reply_id=content[1], emotes=content[2]
|
2020-12-13 08:02:20 +01:00
|
|
|
)
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
message_store[message.id] = matrix_message
|
2020-11-12 15:42:23 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def on_message_edit(self, before, after):
|
2021-02-06 08:20:03 +01:00
|
|
|
if await self.to_return(after.channel.id, after):
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-12 15:42:23 +01:00
|
|
|
|
2020-12-01 09:27:36 +01:00
|
|
|
content = await self.process_message(after)
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Edit message only if it can be looked up in the cache.
|
2020-12-24 11:11:35 +01:00
|
|
|
if before.id in message_store:
|
|
|
|
await self.matrix_client.message_send(
|
2021-01-10 18:40:11 +01:00
|
|
|
content[0], after.channel.id,
|
|
|
|
edit_id=message_store[before.id], emotes=content[2]
|
2020-12-24 11:11:35 +01:00
|
|
|
)
|
2020-11-15 15:08:25 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def on_message_delete(self, message):
|
2021-01-21 11:38:04 +01:00
|
|
|
# Delete message only if it can be looked up in the cache.
|
2020-11-30 13:35:49 +01:00
|
|
|
if message.id in message_store:
|
2020-12-12 11:12:05 +01:00
|
|
|
await self.matrix_client.message_redact(
|
2020-12-13 08:02:20 +01:00
|
|
|
message_store[message.id], message.channel.id
|
|
|
|
)
|
2020-11-14 14:43:25 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def on_typing(self, channel, user, when):
|
2021-02-06 08:20:03 +01:00
|
|
|
if await self.to_return(channel.id) or user == self.user:
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
# Send typing event
|
2020-12-12 11:12:05 +01:00
|
|
|
await self.matrix_client.room_typing(
|
2020-12-13 08:02:20 +01:00
|
|
|
config["bridge"][str(channel.id)], timeout=0
|
|
|
|
)
|
2020-12-01 09:27:36 +01:00
|
|
|
|
|
|
|
async def process_message(self, message):
|
|
|
|
content = message.clean_content
|
|
|
|
|
2021-01-10 18:40:11 +01:00
|
|
|
regex = r"<a?:(\w+):(\d+)>"
|
|
|
|
emotes = {}
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Store all emotes in a dict to upload and insert into formatted body.
|
|
|
|
# { "emote_name": "emote_id" }
|
2021-01-10 18:40:11 +01:00
|
|
|
for emote in re.findall(regex, content):
|
|
|
|
emotes[emote[0]] = emote[1]
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get message reference for replies.
|
2020-12-01 09:27:36 +01:00
|
|
|
replied_event = None
|
|
|
|
if message.reference:
|
|
|
|
replied_message = await message.channel.fetch_message(
|
2020-12-13 08:02:20 +01:00
|
|
|
message.reference.message_id
|
|
|
|
)
|
2021-01-21 11:38:04 +01:00
|
|
|
# Try to get the corresponding event from the message cache.
|
2020-12-01 09:27:36 +01:00
|
|
|
try:
|
|
|
|
replied_event = message_store[replied_message.id]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Replace emote IDs with names.
|
2021-01-10 18:40:11 +01:00
|
|
|
content = re.sub(regex, r":\g<1>:", content)
|
2020-12-01 09:27:36 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Append attachments to message.
|
2020-12-01 09:27:36 +01:00
|
|
|
for attachment in message.attachments:
|
|
|
|
content += f"\n{attachment.url}"
|
|
|
|
|
2021-01-21 08:12:05 +01:00
|
|
|
content = f"[{message.author.display_name}] {content}"
|
2020-12-01 09:27:36 +01:00
|
|
|
|
2021-01-10 18:40:11 +01:00
|
|
|
return content, replied_event, emotes
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-29 16:33:49 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
class Callbacks(object):
|
2021-01-27 13:18:09 +01:00
|
|
|
def __init__(self, discord_client, matrix_client):
|
|
|
|
self.discord_client = discord_client
|
2020-12-30 15:31:18 +01:00
|
|
|
self.matrix_client = matrix_client
|
2020-12-01 09:27:36 +01:00
|
|
|
|
2020-12-12 11:12:05 +01:00
|
|
|
def get_channel(self, room):
|
|
|
|
channel_id = next(
|
|
|
|
(channel_id for channel_id, room_id in config["bridge"].items()
|
2020-12-13 08:02:20 +01:00
|
|
|
if room_id == room.room_id), None
|
|
|
|
)
|
2020-12-12 11:12:05 +01:00
|
|
|
|
|
|
|
return channel_id
|
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
async def to_return(self, room, event):
|
|
|
|
await self.matrix_client.discord_client.ready.wait()
|
|
|
|
|
2020-12-12 11:12:05 +01:00
|
|
|
if room.room_id not in config["bridge"].values() or \
|
2021-02-01 07:03:27 +01:00
|
|
|
event.sender == self.matrix_client.user or \
|
|
|
|
not self.matrix_client.listen:
|
2021-01-03 08:33:30 +01:00
|
|
|
return True
|
2020-11-30 07:42:05 +01:00
|
|
|
|
2021-01-03 08:33:30 +01:00
|
|
|
async def message_callback(self, room, event):
|
2020-11-30 13:35:49 +01:00
|
|
|
message = event.body
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Ignore messages having an empty body.
|
2021-02-01 07:03:27 +01:00
|
|
|
if await self.to_return(room, event) or not message:
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-30 07:42:05 +01:00
|
|
|
|
2020-12-07 14:18:45 +01:00
|
|
|
content_dict = event.source.get("content")
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get the corresponding Discord channel.
|
2020-12-12 11:12:05 +01:00
|
|
|
channel_id = self.get_channel(room)
|
|
|
|
|
2021-01-21 08:12:05 +01:00
|
|
|
author = room.user_name(event.sender)
|
2020-12-08 11:42:33 +01:00
|
|
|
avatar = None
|
|
|
|
|
2020-12-09 06:09:46 +01:00
|
|
|
homeserver = event.sender.split(":")[-1]
|
2020-12-08 11:42:33 +01:00
|
|
|
url = "https://matrix.org/_matrix/media/r0/download"
|
|
|
|
|
2020-12-10 13:26:00 +01:00
|
|
|
try:
|
|
|
|
if content_dict["m.relates_to"]["rel_type"] == "m.replace":
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get the original message's event ID.
|
2020-12-10 13:26:00 +01:00
|
|
|
edited_event = content_dict["m.relates_to"]["event_id"]
|
2020-12-10 13:35:16 +01:00
|
|
|
edited_content = await self.process_message(
|
2020-12-13 08:02:20 +01:00
|
|
|
content_dict["m.new_content"]["body"], channel_id
|
|
|
|
)
|
2021-01-21 11:38:04 +01:00
|
|
|
|
|
|
|
# Get the corresponding Discord message.
|
2020-12-10 13:26:00 +01:00
|
|
|
webhook_message = message_store[edited_event]
|
2020-12-14 11:27:08 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
await webhook_message.edit(content=edited_content)
|
2021-01-21 11:38:04 +01:00
|
|
|
# Handle exception if edited message was deleted on Discord.
|
2021-01-04 07:57:01 +01:00
|
|
|
except (
|
|
|
|
discord.errors.NotFound, discord.errors.HTTPException
|
|
|
|
) as e:
|
2020-12-30 15:31:18 +01:00
|
|
|
self.matrix_client.logger.warning(
|
2020-12-14 11:27:08 +01:00
|
|
|
f"Failed to edit message {edited_event}: {e}"
|
|
|
|
)
|
|
|
|
|
2020-12-10 13:26:00 +01:00
|
|
|
return
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2020-12-07 14:18:45 +01:00
|
|
|
try:
|
|
|
|
if content_dict["m.relates_to"]["m.in_reply_to"]["event_id"] in \
|
|
|
|
message_store.values():
|
2021-01-21 11:38:04 +01:00
|
|
|
# Remove the first occurance of our bot's username if replying.
|
|
|
|
# > <@discordbridge:something.org> [discord user]
|
2020-12-07 14:18:45 +01:00
|
|
|
message = message.replace(f"<{config['username']}>", "", 1)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# _testuser waves_ (Italics)
|
2020-12-08 11:42:33 +01:00
|
|
|
if content_dict["msgtype"] == "m.emote":
|
2020-12-09 06:09:46 +01:00
|
|
|
message = f"_{author} {message}_"
|
2020-11-15 16:32:55 +01:00
|
|
|
|
2020-12-12 11:12:05 +01:00
|
|
|
message = await self.process_message(message, channel_id)
|
2020-11-15 16:32:55 +01:00
|
|
|
|
2021-01-03 11:54:01 +01:00
|
|
|
embed = None
|
2021-01-11 05:58:48 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get attachments.
|
2020-11-30 13:35:49 +01:00
|
|
|
try:
|
|
|
|
attachment = event.url.split("/")[-1]
|
2021-01-21 11:38:04 +01:00
|
|
|
# TODO: Fix URL for attachments forwarded from other rooms.
|
2021-01-03 11:54:01 +01:00
|
|
|
attachment = f"{url}/{homeserver}/{attachment}"
|
2020-11-15 16:32:55 +01:00
|
|
|
|
2021-01-03 11:54:01 +01:00
|
|
|
embed = discord.Embed(colour=discord.Colour.blue(), title=message)
|
|
|
|
embed.set_image(url=attachment)
|
2020-11-12 14:22:48 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Send attachment URL in message along with embed,
|
|
|
|
# Just in-case the attachment is not an image.
|
2021-01-04 07:57:01 +01:00
|
|
|
message = attachment
|
2020-11-30 13:35:49 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2020-11-14 14:43:25 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get avatar.
|
2020-11-30 13:35:49 +01:00
|
|
|
for user in room.users.values():
|
|
|
|
if user.user_id == event.sender:
|
|
|
|
if user.avatar_url:
|
|
|
|
avatar = user.avatar_url.split("/")[-1]
|
|
|
|
avatar = f"{url}/{homeserver}/{avatar}"
|
|
|
|
break
|
2020-11-30 07:42:05 +01:00
|
|
|
|
2020-12-30 15:31:18 +01:00
|
|
|
await self.matrix_client.webhook_send(
|
2021-01-03 11:54:01 +01:00
|
|
|
author, avatar, message, event.event_id, channel_id, embed=embed
|
2020-12-13 08:02:20 +01:00
|
|
|
)
|
2020-11-13 15:04:31 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def redaction_callback(self, room, event):
|
2021-02-01 07:03:27 +01:00
|
|
|
if await self.to_return(room, event):
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Try to fetch the message from cache.
|
2020-11-30 13:35:49 +01:00
|
|
|
try:
|
|
|
|
message = message_store[event.redacts]
|
|
|
|
await message.delete()
|
2021-01-21 11:38:04 +01:00
|
|
|
# Handle exception if message was already deleted on Discord.
|
2020-12-13 07:55:39 +01:00
|
|
|
except discord.errors.NotFound as e:
|
2020-12-30 15:31:18 +01:00
|
|
|
self.matrix_client.logger.warning(
|
2020-12-13 08:02:20 +01:00
|
|
|
f"Failed to delete message {event.event_id}: {e}"
|
|
|
|
)
|
2020-11-30 13:35:49 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2020-11-13 11:08:28 +01:00
|
|
|
|
2020-11-30 13:35:49 +01:00
|
|
|
async def typing_callback(self, room, event):
|
2021-01-03 08:33:30 +01:00
|
|
|
if not room.typing_users \
|
|
|
|
or room.room_id not in config["bridge"].values():
|
2020-11-30 13:35:49 +01:00
|
|
|
return
|
2020-11-13 17:31:04 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Return if the event is sent by our bot.
|
2021-01-03 08:33:30 +01:00
|
|
|
if len(room.typing_users) == 1 and \
|
|
|
|
self.matrix_client.user in room.typing_users:
|
|
|
|
return
|
2020-11-14 06:03:15 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get the corresponding Discord channel.
|
2021-01-03 08:33:30 +01:00
|
|
|
channel_id = self.get_channel(room)
|
2020-12-12 11:12:05 +01:00
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Send typing event.
|
2021-01-27 13:18:09 +01:00
|
|
|
async with self.discord_client.channel_store[channel_id].typing():
|
2021-01-03 08:33:30 +01:00
|
|
|
return
|
2020-11-14 16:34:30 +01:00
|
|
|
|
2020-12-30 15:31:18 +01:00
|
|
|
async def process_message(self, message, channel_id):
|
|
|
|
mentions = re.findall(r"(^|\s)(@(\w*))", message)
|
|
|
|
emotes = re.findall(r":(\w*):", message)
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get the guild from channel ID.
|
2021-01-27 13:18:09 +01:00
|
|
|
guild = self.discord_client.channel_store[channel_id].guild
|
2020-12-30 15:31:18 +01:00
|
|
|
|
|
|
|
added_emotes = []
|
|
|
|
for emote in emotes:
|
2021-01-21 11:38:04 +01:00
|
|
|
# Don't replace emote names with IDs multiple times.
|
|
|
|
# :emote: becomes <:emote:emote_id>
|
2020-12-30 15:31:18 +01:00
|
|
|
if emote not in added_emotes:
|
|
|
|
added_emotes.append(emote)
|
|
|
|
emote_ = discord.utils.get(guild.emojis, name=emote)
|
|
|
|
if emote_:
|
|
|
|
message = message.replace(f":{emote}:", str(emote_))
|
|
|
|
|
2021-01-21 11:38:04 +01:00
|
|
|
# mentions = [('', '@name', 'name'), (' ', '@', '')]
|
2020-12-30 15:31:18 +01:00
|
|
|
for mention in mentions:
|
2021-01-21 11:38:04 +01:00
|
|
|
# Don't fetch member if mention is empty.
|
|
|
|
# Single "@" without any name.
|
2021-01-11 07:35:14 +01:00
|
|
|
if mention[2]:
|
2020-12-30 15:31:18 +01:00
|
|
|
member = await guild.query_members(query=mention[2])
|
|
|
|
if member:
|
2021-01-21 11:38:04 +01:00
|
|
|
# Get first result.
|
2020-12-30 15:31:18 +01:00
|
|
|
message = message.replace(mention[1], member[0].mention)
|
|
|
|
|
|
|
|
return message
|
|
|
|
|
2020-11-14 16:25:37 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
async def main():
|
2021-02-06 07:53:49 +01:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
handlers=[
|
|
|
|
logging.FileHandler("bot.log"),
|
|
|
|
logging.StreamHandler()
|
|
|
|
]
|
|
|
|
)
|
2020-12-30 15:31:18 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
retry = 2
|
|
|
|
|
|
|
|
matrix_client = MatrixClient(
|
|
|
|
config["homeserver"], config["username"]
|
|
|
|
)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
resp = await matrix_client.login(config["password"])
|
|
|
|
|
|
|
|
if type(resp) == nio.LoginError:
|
|
|
|
matrix_client.logger.error(f"Failed to login: {resp}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Login successful.
|
|
|
|
matrix_client.logger.info(resp)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await matrix_client.sync(full_state=True)
|
|
|
|
except Exception:
|
|
|
|
matrix_client.logger.error(
|
|
|
|
f"Initial sync failed!\n{traceback.format_exc()}"
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
matrix_client.ready.set()
|
|
|
|
matrix_client.listen = True
|
|
|
|
|
|
|
|
matrix_client.logger.info("Clients ready!")
|
|
|
|
|
|
|
|
await matrix_client.sync_forever(timeout=30000, full_state=True)
|
|
|
|
except Exception:
|
|
|
|
matrix_client.logger.error(
|
|
|
|
f"Unknown exception occured\n{traceback.format_exc()}\n"
|
|
|
|
f"Retrying in {retry} seconds..."
|
|
|
|
)
|
|
|
|
|
|
|
|
# Clear "ready" status.
|
|
|
|
matrix_client.ready.clear()
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
await matrix_client.close()
|
|
|
|
await asyncio.sleep(retry)
|
2020-11-30 16:44:34 +01:00
|
|
|
|
2021-02-01 07:03:27 +01:00
|
|
|
matrix_client.listen = False
|
|
|
|
finally:
|
|
|
|
if matrix_client.listen:
|
|
|
|
await matrix_client.close()
|
|
|
|
return False
|
2020-11-11 16:46:15 +01:00
|
|
|
|
2020-11-12 15:42:23 +01:00
|
|
|
if __name__ == "__main__":
|
2021-02-01 07:03:27 +01:00
|
|
|
asyncio.run(main())
|