diff --git a/.gitignore b/.gitignore index 8eaf2fe..479a4e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.venv/ maubot-htmlfile.zip diff --git a/.nix/flake.nix b/.nix/flake.nix index 01ebacd..ed596a8 100644 --- a/.nix/flake.nix +++ b/.nix/flake.nix @@ -7,13 +7,30 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; + + python = pkgs.python3; + python-with-packages = python.withPackages (p: with p; [ + python-lsp-server + rope + pyflakes + mccabe + pycodestyle + autopep8 + ]); in { devShell = pkgs.mkShell { nativeBuildInputs = [ pkgs.bashInteractive ]; buildInputs = with pkgs; [ + python-with-packages + nodePackages.pyright zip ]; + shellHook = '' + python -m venv .venv + source .venv/bin/activate + pip install --requirement requirements.txt + ''; }; } ); diff --git a/htmlfile.py b/htmlfile.py index 27082e1..38a9810 100644 --- a/htmlfile.py +++ b/htmlfile.py @@ -1,5 +1,7 @@ -from mautrix.types import EventType, MessageType -from mautrix.crypto import attachments +# 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 @@ -8,18 +10,19 @@ from maubot.handlers import event class HtmlFile(Plugin): @event.on(EventType.ROOM_MESSAGE) async def event_handler(self, event: MessageEvent) -> None: - - # self.log.debug(f"received: {event}") - if not event.content.msgtype == MessageType.FILE: return + else: + content = MediaMessageEventContent.deserialize(event.content.serialize()) - if event.content.info.mimetype == "text/html": - if event.content.url: - file_content = (await self.client.download_media(url = event.content.url)).decode("utf-8") - else: - enc_file = event.content.file - ciphertext = await self.client.download_media(url = enc_file.url) - file_content = attachments.decrypt_attachment(ciphertext, enc_file.key.key, enc_file.hashes["sha256"], enc_file.iv).decode("utf-8") - - await event.reply(file_content, allow_html = True) + 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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5860ace --- /dev/null +++ b/requirements.txt @@ -0,0 +1,30 @@ +aiohttp==3.8.3 +aiosignal==1.3.1 +aiosqlite==0.17.0 +async-timeout==4.0.2 +asyncpg==0.25.0 +attrs==22.1.0 +bcrypt==3.2.2 +cffi==1.15.1 +charset-normalizer==2.1.1 +click==8.1.3 +colorama==0.4.6 +commonmark==0.9.1 +frozenlist==1.3.3 +idna==3.4 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +maubot==0.3.1 +mautrix==0.15.8 +multidict==6.0.2 +packaging==21.3 +prompt-toolkit==3.0.33 +pycparser==2.21 +pyparsing==3.0.9 +questionary==1.10.0 +ruamel.yaml==0.17.21 +ruamel.yaml.clib==0.2.7 +SQLAlchemy==1.3.24 +typing_extensions==4.4.0 +wcwidth==0.2.5 +yarl==1.8.1