commit 27a29a604a2d03e852bf923161fbdaeba4536346 Author: Michael Mandl Date: Tue Nov 29 16:58:54 2022 +0100 feat: reply to all html files with their content diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..db72d2a --- /dev/null +++ b/.envrc @@ -0,0 +1,9 @@ +use flake .nix +dotenv_if_exists + +if on_git_branch; then + echo + git status --short --branch + echo + git fetch +fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8eaf2fe --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +maubot-htmlfile.zip diff --git a/.nix/flake.lock b/.nix/flake.lock new file mode 100644 index 0000000..8834bce --- /dev/null +++ b/.nix/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1669597967, + "narHash": "sha256-R+2NaDkXsYkOpFOhmVR8jBZ77Pq55Z6ilaqwFLLn000=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "be9e3762e719211368d186f547f847737baad720", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/.nix/flake.nix b/.nix/flake.nix new file mode 100644 index 0000000..01ebacd --- /dev/null +++ b/.nix/flake.nix @@ -0,0 +1,20 @@ +{ + description = "A basic flake with a shell"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShell = pkgs.mkShell { + nativeBuildInputs = [ pkgs.bashInteractive ]; + buildInputs = with pkgs; [ + zip + ]; + }; + } + ); +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5500607 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +plugin: maubot.yaml htmlfile.py + zip -9r maubot-htmlfile.zip maubot.yaml htmlfile.py + +clean: + rm maubot-htmlfile.zip + +all: plugin diff --git a/htmlfile.py b/htmlfile.py new file mode 100644 index 0000000..27082e1 --- /dev/null +++ b/htmlfile.py @@ -0,0 +1,25 @@ +from mautrix.types import EventType, MessageType +from mautrix.crypto import attachments + +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: + + # self.log.debug(f"received: {event}") + + if not event.content.msgtype == MessageType.FILE: + return + + 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) diff --git a/maubot.yaml b/maubot.yaml new file mode 100644 index 0000000..2e6535b --- /dev/null +++ b/maubot.yaml @@ -0,0 +1,7 @@ +maubot: 0.1.0 +id: net.molez.maubot.htmlfile +version: 0.1.0 +license: MIT +modules: + - htmlfile +main_class: HtmlFile