maubot-htmlfile/htmlfile.py

29 lines
1.2 KiB
Python

# pyright: reportPrivateImportUsage=false
from mautrix.types import EventType, MediaMessageEventContent, MessageType
from mautrix.crypto.attachments import decrypt_attachment
from maubot import Plugin, MessageEvent
from maubot.handlers import event
class HtmlFile(Plugin):
@event.on(EventType.ROOM_MESSAGE)
async def event_handler(self, event: MessageEvent) -> None:
if not event.content.msgtype == MessageType.FILE:
return
else:
content = MediaMessageEventContent.deserialize(event.content.serialize())
file_content: str = ""
if content.info and content.info.mimetype == "text/html":
if content.url:
file_content = (await self.client.download_media(url=content.url)).decode("utf-8")
elif content.file and content.file.url:
enc_file = content.file
ciphertext = await self.client.download_media(url=content.file.url)
file_content = decrypt_attachment(ciphertext, enc_file.key.key,
enc_file.hashes["sha256"], enc_file.iv).decode("utf-8")
if file_content:
await event.reply(file_content, allow_html=True)