Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ghost profile changing commands #30

Merged
merged 4 commits into from
Oct 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions mautrix/bridge/commands/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,97 @@ 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="Set 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 <mxc_uri> [mxid]`")
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.")
intent = puppet.intent
elif evt.is_portal:
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:
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="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])
if puppet is None:
return await evt.reply("The given mxid was not a valid ghost user.")
intent = puppet.intent
elif evt.is_portal:
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:
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).")

@command_handler(needs_admin=True, needs_auth=False, name="set-displayname",
help_section=SECTION_ADMIN, help_args="<_display_name_> [_mxid_]",
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
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:
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]
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="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])
if puppet is None:
return await evt.reply("The given mxid was not a valid ghost user.")
intent = puppet.intent
elif evt.is_portal:
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:
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).")