From f843fd7e85f56fbc3550f2a7c579f61e6c3b441f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 29 Jan 2018 23:46:46 +0200 Subject: [PATCH] Add command to forget portal room. Fixes #30 --- README.md | 2 +- example-config.yaml | 4 ++++ mautrix_telegram/commands.py | 25 ++++++++++++++++++++++++- mautrix_telegram/user.py | 9 +++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 249a48cd..ca450676 100644 --- a/README.md +++ b/README.md @@ -129,4 +129,4 @@ The bridge does not do this automatically. * [x] Creating a Telegram chat for an existing Matrix room (`create`) * [ ] Upgrading the chat of a portal room into a supergroup (`upgrade`) * [x] Getting the Telegram invite link to a Matrix room (`invitelink`) - * [ ] Clean up and forget a portal room (`deleteportal`) + * [x] Clean up and forget a portal room (`deleteportal`) diff --git a/example-config.yaml b/example-config.yaml index ad3e1e72..e481784a 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -67,6 +67,10 @@ bridge: - "internal-hs.example.com" - "@user:public.example.com" + # Admins can do things like delete portal rooms. + admins: + - "@admin:internal-hs.example.com" + # Telegram config telegram: # Get your own API keys at https://my.telegram.org/apps diff --git a/mautrix_telegram/commands.py b/mautrix_telegram/commands.py index 9e8b99f1..76cd9c64 100644 --- a/mautrix_telegram/commands.py +++ b/mautrix_telegram/commands.py @@ -16,6 +16,7 @@ # along with this program. If not, see . from contextlib import contextmanager import markdown +from matrix_client.errors import MatrixRequestError from telethon.errors import * from telethon.tl.types import * from telethon.tl.functions.contacts import SearchRequest @@ -255,6 +256,26 @@ class CommandHandler: except ChatAdminRequiredError: return self.reply("You don't have the permission to create an invite link.") + @command_handler + def delete_portal(self, sender, args): + if not sender.logged_in: + return self.reply("This command requires you to be logged in.") + elif not sender.is_admin: + return self.reply("This is command requires administrator privileges.") + + portal = po.Portal.get_by_mxid(self._room_id) + if not portal: + return self.reply("This is not a portal room.") + + for user in portal.main_intent.get_room_members(portal.mxid): + if user != portal.main_intent.mxid: + try: + portal.main_intent.kick(portal.mxid, user, "Portal deleted.") + except MatrixRequestError: + pass + portal.main_intent.leave_room(portal.mxid) + portal.delete() + def _strip_prefix(self, value, prefixes): for prefix in prefixes: if value.startswith(prefix): @@ -393,7 +414,9 @@ _**Telegram actions**: commands for using the bridge to interact with Telegram._ **join** <_link_> - Join a chat with an invite link. **create** [_type_] - Create a Telegram chat of the given type for the current Matrix room. The type is either `group`, `supergroup` or `channel` (defaults to `group`). -**upgrade** - Upgrade a normal Telegram group to a supergroup. +**upgrade** - Upgrade a normal Telegram group to a supergroup. +**invitelink** - Get a Telegram invite link to the current chat. +**deleteportal** - Forget the current portal room. """ return self.reply(management_status + help) diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index 69eb0e1f..edb67e2a 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -40,7 +40,12 @@ class User: self.command_status = None self.connected = False self.client = None - whitelist = config.get("bridge", {}).get("whitelist", None) or [self.mxid] + + bridge_config = config.get("bridge", {}) + + self.is_admin = self.mxid in bridge_config.get("admins", []) + + whitelist = bridge_config.get("whitelist", None) or [self.mxid] self.whitelisted = not whitelist or self.mxid in whitelist if not self.whitelisted: homeserver = self.mxid[self.mxid.index(":") + 1:] @@ -185,7 +190,7 @@ class User: for dialog in dialogs: entity = dialog.entity if (isinstance(entity, (TLUser, ChatForbidden, ChannelForbidden)) or ( - isinstance(entity, Chat) and entity.deactivated)): + isinstance(entity, Chat) and (entity.deactivated or entity.left))): continue portal = po.Portal.get_by_entity(entity) portal.create_matrix_room(self, entity, invites=[self.mxid])