From 35271b3411974e0d78733da3850a3c9029e67da2 Mon Sep 17 00:00:00 2001 From: witchent Date: Thu, 8 Oct 2020 18:37:43 +0200 Subject: [PATCH 1/4] Added command for ghost users avatar --- mautrix/bridge/commands/admin.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/mautrix/bridge/commands/admin.py b/mautrix/bridge/commands/admin.py index de626634..fef1bff6 100644 --- a/mautrix/bridge/commands/admin.py +++ b/mautrix/bridge/commands/admin.py @@ -29,3 +29,49 @@ async def set_power_level(evt: CommandEvent) -> EventID: except (MatrixRequestError, IntentError): evt.log.exception("Failed to update power levels") return await evt.reply("Failed to update power levels (see logs for more details)") + +@command_handler(needs_admin=True, needs_auth=False, name="set-avatar", + help_section=SECTION_ADMIN, help_args="<_mxc_uri_> [_mxid_]", + help_text="Sets an avatar for a ghost user.") +async def set_ghost_avatar(evt: CommandEvent) -> EventID: + try: + mxc_uri = evt.args[0] + except (KeyError, IndexError): + return await evt.reply("**Usage:** `$cmdprefix+sp set-avatar [mxid]`") + if not mxc_uri.startswith("mxc://"): + return await evt.reply("The URI has to start with mxc://.") + if len(evt.args) > 1: + puppet = await evt.processor.bridge.get_puppet(evt.args[1]) + if puppet is None: + return await evt.reply("The given mxid was not a valid ghost user.") + intent = puppet.intent + elif evt.is_portal: + portal = await evt.processor.bridge.get_portal(evt.room_id) + intent = portal.main_intent + else: + return await evt.reply("No mxid given and not in a portal.") + try: + return await intent.set_avatar_url(mxc_uri) + except (MatrixRequestError, IntentError): + evt.log.exception("Failed to set avatar.") + return await evt.reply("Failed to set avatar (see logs for more details).") + +@command_handler(needs_admin=True, needs_auth=False, name="remove-avatar", + help_section=SECTION_ADMIN, help_args="[_mxid_]", + help_text="Removes an avatar for a ghost user.") +async def remove_ghost_avatar(evt: CommandEvent) -> EventID: + if len(evt.args) > 0: + puppet = await evt.processor.bridge.get_puppet(evt.args[0]) + if puppet is None: + return await evt.reply("The given mxid was not a valid ghost user.") + intent = puppet.intent + elif evt.is_portal: + portal = await evt.processor.bridge.get_portal(evt.room_id) + intent = portal.main_intent + else: + return await evt.reply("No mxid given and not in a portal.") + try: + return await intent.set_avatar_url("") + except (MatrixRequestError, IntentError): + evt.log.exception("Failed to remove avatar.") + return await evt.reply("Failed to remove avatar (see logs for more details).") From 7ec7b6c2d3723df358311b04c0631ba6db1aeb2a Mon Sep 17 00:00:00 2001 From: witchent Date: Thu, 8 Oct 2020 18:38:05 +0200 Subject: [PATCH 2/4] Added commands for ghost users display name --- mautrix/bridge/commands/admin.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/mautrix/bridge/commands/admin.py b/mautrix/bridge/commands/admin.py index fef1bff6..9db2b5f8 100644 --- a/mautrix/bridge/commands/admin.py +++ b/mautrix/bridge/commands/admin.py @@ -75,3 +75,46 @@ async def remove_ghost_avatar(evt: CommandEvent) -> EventID: except (MatrixRequestError, IntentError): evt.log.exception("Failed to remove avatar.") return await evt.reply("Failed to remove avatar (see logs for more details).") + +@command_handler(needs_admin=True, needs_auth=False, name="set-displayname", + help_section=SECTION_ADMIN, help_args="<_display_name_> [_mxid_]", + help_text="Sets the display name for a ghost user.") +async def set_ghost_display_name(evt: CommandEvent) -> EventID: + if len(evt.args) > 1: + #This allows whitespaces in the name + puppet = await evt.processor.bridge.get_puppet(evt.args[len(evt.args)-1]) + if puppet is None: + return await evt.reply("The given mxid was not a valid ghost user. If the display name has whitespaces mxid is required") + intent = puppet.intent + displayname = " ".join(evt.args[:-1]) + elif evt.is_portal: + portal = await evt.processor.bridge.get_portal(evt.room_id) + intent = portal.main_intent + displayname = evt.args[0] + else: + return await evt.reply("No mxid given and not in a portal.") + try: + return await intent.set_displayname(displayname) + except (MatrixRequestError, IntentError): + evt.log.exception("Failed to set display name.") + return await evt.reply("Failed to set display name (see logs for more details).") + +@command_handler(needs_admin=True, needs_auth=False, name="remove-displayname", + help_section=SECTION_ADMIN, help_args="[_mxid_]", + help_text="Removes the display name for a ghost user.") +async def set_ghost_display_name(evt: CommandEvent) -> EventID: + if len(evt.args) > 0: + puppet = await evt.processor.bridge.get_puppet(evt.args[0]) + if puppet is None: + return await evt.reply("The given mxid was not a valid ghost user.") + intent = puppet.intent + elif evt.is_portal: + portal = await evt.processor.bridge.get_portal(evt.room_id) + intent = portal.main_intent + else: + return await evt.reply("No mxid given and not in a portal (see logs for more details).") + try: + return await intent.set_displayname(" ") + except (MatrixRequestError, IntentError): + evt.log.exception("Failed to remove display name.") + return await evt.reply("Failed to remove display name (see logs for more details).") From 415fde6a9bbd28ef3ee8436619c97909c41c048f Mon Sep 17 00:00:00 2001 From: witchent Date: Thu, 8 Oct 2020 19:25:30 +0200 Subject: [PATCH 3/4] Tulir's review --- mautrix/bridge/commands/admin.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mautrix/bridge/commands/admin.py b/mautrix/bridge/commands/admin.py index 9db2b5f8..5ad97ece 100644 --- a/mautrix/bridge/commands/admin.py +++ b/mautrix/bridge/commands/admin.py @@ -31,8 +31,8 @@ async def set_power_level(evt: CommandEvent) -> EventID: return await evt.reply("Failed to update power levels (see logs for more details)") @command_handler(needs_admin=True, needs_auth=False, name="set-avatar", - help_section=SECTION_ADMIN, help_args="<_mxc_uri_> [_mxid_]", - help_text="Sets an avatar for a ghost user.") + help_section=SECTION_ADMIN, help_args="<_mxc://uri_> [_mxid_]", + help_text="Set an avatar for a ghost user.") async def set_ghost_avatar(evt: CommandEvent) -> EventID: try: mxc_uri = evt.args[0] @@ -41,6 +41,7 @@ async def set_ghost_avatar(evt: CommandEvent) -> EventID: if not mxc_uri.startswith("mxc://"): return await evt.reply("The URI has to start with mxc://.") if len(evt.args) > 1: + # TODO support parsing mention pills instead of requiring a plaintext mxid puppet = await evt.processor.bridge.get_puppet(evt.args[1]) if puppet is None: return await evt.reply("The given mxid was not a valid ghost user.") @@ -48,6 +49,8 @@ async def set_ghost_avatar(evt: CommandEvent) -> EventID: elif evt.is_portal: portal = await evt.processor.bridge.get_portal(evt.room_id) intent = portal.main_intent + if intent == evt.az.intent: + return await evt.reply("No mxid given and the main intent is not a ghost user.") else: return await evt.reply("No mxid given and not in a portal.") try: @@ -58,7 +61,7 @@ async def set_ghost_avatar(evt: CommandEvent) -> EventID: @command_handler(needs_admin=True, needs_auth=False, name="remove-avatar", help_section=SECTION_ADMIN, help_args="[_mxid_]", - help_text="Removes an avatar for a ghost user.") + help_text="Remove the avatar for a ghost user.") async def remove_ghost_avatar(evt: CommandEvent) -> EventID: if len(evt.args) > 0: puppet = await evt.processor.bridge.get_puppet(evt.args[0]) @@ -68,6 +71,8 @@ async def remove_ghost_avatar(evt: CommandEvent) -> EventID: elif evt.is_portal: portal = await evt.processor.bridge.get_portal(evt.room_id) intent = portal.main_intent + if intent == evt.az.intent: + return await evt.reply("No mxid given and the main intent is not a ghost user.") else: return await evt.reply("No mxid given and not in a portal.") try: @@ -78,7 +83,7 @@ async def remove_ghost_avatar(evt: CommandEvent) -> EventID: @command_handler(needs_admin=True, needs_auth=False, name="set-displayname", help_section=SECTION_ADMIN, help_args="<_display_name_> [_mxid_]", - help_text="Sets the display name for a ghost user.") + help_text="Set the display name for a ghost user.") async def set_ghost_display_name(evt: CommandEvent) -> EventID: if len(evt.args) > 1: #This allows whitespaces in the name @@ -90,6 +95,8 @@ async def set_ghost_display_name(evt: CommandEvent) -> EventID: elif evt.is_portal: portal = await evt.processor.bridge.get_portal(evt.room_id) intent = portal.main_intent + if intent == evt.az.intent: + return await evt.reply("No mxid given and the main intent is not a ghost user.") displayname = evt.args[0] else: return await evt.reply("No mxid given and not in a portal.") @@ -101,7 +108,7 @@ async def set_ghost_display_name(evt: CommandEvent) -> EventID: @command_handler(needs_admin=True, needs_auth=False, name="remove-displayname", help_section=SECTION_ADMIN, help_args="[_mxid_]", - help_text="Removes the display name for a ghost user.") + help_text="Remove the display name for a ghost user.") async def set_ghost_display_name(evt: CommandEvent) -> EventID: if len(evt.args) > 0: puppet = await evt.processor.bridge.get_puppet(evt.args[0]) @@ -111,6 +118,8 @@ async def set_ghost_display_name(evt: CommandEvent) -> EventID: elif evt.is_portal: portal = await evt.processor.bridge.get_portal(evt.room_id) intent = portal.main_intent + if intent == evt.az.intent: + return await evt.reply("No mxid given and the main intent is not a ghost user.") else: return await evt.reply("No mxid given and not in a portal (see logs for more details).") try: From 5ee226521f4537a39fa21dec96590e054b632209 Mon Sep 17 00:00:00 2001 From: witchent Date: Thu, 8 Oct 2020 19:51:13 +0200 Subject: [PATCH 4/4] Shortcut to portals main_intent --- mautrix/bridge/commands/admin.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mautrix/bridge/commands/admin.py b/mautrix/bridge/commands/admin.py index 5ad97ece..92bef6bc 100644 --- a/mautrix/bridge/commands/admin.py +++ b/mautrix/bridge/commands/admin.py @@ -47,8 +47,7 @@ async def set_ghost_avatar(evt: CommandEvent) -> EventID: return await evt.reply("The given mxid was not a valid ghost user.") intent = puppet.intent elif evt.is_portal: - portal = await evt.processor.bridge.get_portal(evt.room_id) - intent = portal.main_intent + intent = evt.portal.main_intent if intent == evt.az.intent: return await evt.reply("No mxid given and the main intent is not a ghost user.") else: @@ -69,8 +68,7 @@ async def remove_ghost_avatar(evt: CommandEvent) -> EventID: return await evt.reply("The given mxid was not a valid ghost user.") intent = puppet.intent elif evt.is_portal: - portal = await evt.processor.bridge.get_portal(evt.room_id) - intent = portal.main_intent + intent = evt.portal.main_intent if intent == evt.az.intent: return await evt.reply("No mxid given and the main intent is not a ghost user.") else: @@ -93,8 +91,7 @@ async def set_ghost_display_name(evt: CommandEvent) -> EventID: intent = puppet.intent displayname = " ".join(evt.args[:-1]) elif evt.is_portal: - portal = await evt.processor.bridge.get_portal(evt.room_id) - intent = portal.main_intent + intent = evt.portal.main_intent if intent == evt.az.intent: return await evt.reply("No mxid given and the main intent is not a ghost user.") displayname = evt.args[0] @@ -116,8 +113,7 @@ async def set_ghost_display_name(evt: CommandEvent) -> EventID: return await evt.reply("The given mxid was not a valid ghost user.") intent = puppet.intent elif evt.is_portal: - portal = await evt.processor.bridge.get_portal(evt.room_id) - intent = portal.main_intent + intent = evt.portal.main_intent if intent == evt.az.intent: return await evt.reply("No mxid given and the main intent is not a ghost user.") else: