-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
set-pl command and fixed reply #29
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cc1ec9
Lock olm sessions between encrypting and sending
tulir d9a10b3
Fixed reply and read marker if bot is not in the room
witchent ec0bed5
Add set-pl command + handling
witchent c8b1b36
formating
witchent d6b1a49
Code reduction
witchent aed40fa
Forgot await
witchent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = "0.7.10" | ||
__version__ = "0.7.11" | ||
__author__ = "Tulir Asokan <[email protected]>" | ||
__all__ = ["api", "appservice", "bridge", "client", "crypto", "errors", "util", "types"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from .handler import (HelpSection, HelpCacheKey, command_handler, CommandHandler, CommandProcessor, | ||
CommandHandlerFunc, CommandEvent, SECTION_GENERAL) | ||
CommandHandlerFunc, CommandEvent, SECTION_GENERAL, SECTION_ADMIN) | ||
from .meta import cancel, unknown_command, help_cmd | ||
from . import admin | ||
|
||
__all__ = ["HelpSection", "HelpCacheKey", "command_handler", "CommandHandler", "CommandProcessor", | ||
"CommandHandlerFunc", "CommandEvent", "SECTION_GENERAL"] | ||
"CommandHandlerFunc", "CommandEvent", "SECTION_GENERAL", "SECTION_ADMIN"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) 2020 Tulir Asokan | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
from mautrix.types import EventID | ||
|
||
from mautrix.errors import (MatrixRequestError, IntentError) | ||
|
||
from .handler import (command_handler, CommandEvent, SECTION_ADMIN) | ||
|
||
|
||
@command_handler(needs_admin=True, needs_auth=False, name="set-pl", | ||
help_section=SECTION_ADMIN, | ||
help_args="<_level_> [_mxid_]", | ||
help_text="Set a temporary power level without affecting the bridge.") | ||
async def set_power_level(evt: CommandEvent) -> EventID: | ||
try: | ||
level = int(evt.args[0]) | ||
except (KeyError, IndexError): | ||
return await evt.reply("**Usage:** `$cmdprefix+sp set-pl <level> [mxid]`") | ||
except ValueError: | ||
return await evt.reply("The level must be an integer.") | ||
if evt.is_portal: | ||
portal = await evt.processor.bridge.get_portal(evt.room_id) | ||
intent = portal.main_intent | ||
else: | ||
intent = evt.az.intent | ||
levels = await intent.get_power_levels(evt.room_id) | ||
mxid = evt.args[1] if len(evt.args) > 1 else evt.sender.mxid | ||
levels.users[mxid] = level | ||
try: | ||
return await intent.set_power_levels(evt.room_id, levels) | ||
except (MatrixRequestError, IntentError): | ||
evt.log.exception("Failed to set power level.") | ||
return await evt.reply("Failed to set power level.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,7 @@ | |||||||||
HelpCacheKey = NamedTuple('HelpCacheKey', is_management=bool, is_portal=bool) | ||||||||||
|
||||||||||
SECTION_GENERAL = HelpSection("General", 0, "") | ||||||||||
SECTION_ADMIN = HelpSection("Administration", 50, "") | ||||||||||
|
||||||||||
|
||||||||||
def ensure_trailing_newline(s: str) -> str: | ||||||||||
|
@@ -112,7 +113,7 @@ def print_error_traceback(self) -> bool: | |||||||||
""" | ||||||||||
return self.is_management | ||||||||||
|
||||||||||
def reply(self, message: str, allow_html: bool = False, render_markdown: bool = True | ||||||||||
async def reply(self, message: str, allow_html: bool = False, render_markdown: bool = True | ||||||||||
) -> Awaitable[EventID]: | ||||||||||
"""Write a reply to the room in which the command was issued. | ||||||||||
|
||||||||||
|
@@ -136,11 +137,16 @@ def reply(self, message: str, allow_html: bool = False, render_markdown: bool = | |||||||||
html = self._render_message(message, allow_html=allow_html, | ||||||||||
render_markdown=render_markdown) | ||||||||||
|
||||||||||
return self.az.intent.send_notice(self.room_id, message, html=html) | ||||||||||
if self.is_portal: | ||||||||||
portal = await self.processor.bridge.get_portal(self.room_id) | ||||||||||
return await portal.main_intent.send_notice(self.room_id, message, html=html) | ||||||||||
else: | ||||||||||
return await self.az.intent.send_notice(self.room_id, message, html=html) | ||||||||||
|
||||||||||
def mark_read(self) -> Awaitable[None]: | ||||||||||
"""Marks the command as read by the bot.""" | ||||||||||
return self.az.intent.mark_read(self.room_id, self.event_id) | ||||||||||
if not self.is_portal: | ||||||||||
return self.az.intent.mark_read(self.room_id, self.event_id) | ||||||||||
Comment on lines
+148
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def _replace_command_prefix(self, message: str) -> str: | ||||||||||
"""Returns the string with the proper command prefix entered.""" | ||||||||||
|
@@ -184,20 +190,26 @@ class CommandHandler: | |||||||||
name: The name of this command. | ||||||||||
help_section: Section of the help in which this command will appear. | ||||||||||
""" | ||||||||||
management_only: bool | ||||||||||
name: str | ||||||||||
|
||||||||||
management_only: bool | ||||||||||
needs_admin: bool | ||||||||||
needs_auth: bool | ||||||||||
|
||||||||||
_help_text: str | ||||||||||
_help_args: str | ||||||||||
help_section: HelpSection | ||||||||||
|
||||||||||
def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str, | ||||||||||
help_text: str, help_args: str, help_section: HelpSection, **kwargs) -> None: | ||||||||||
help_text: str, help_args: str, help_section: HelpSection, | ||||||||||
needs_auth: bool, needs_admin: bool, **kwargs) -> None: | ||||||||||
""" | ||||||||||
Args: | ||||||||||
handler: The function handling the execution of this command. | ||||||||||
management_only: Whether the command can exclusively be issued | ||||||||||
in a management room. | ||||||||||
needs_auth: Whether the command needs the bridge to be authed already | ||||||||||
needs_admin: Whether the command needs the issuer to be bridge admin | ||||||||||
name: The name of this command. | ||||||||||
help_text: The text displayed in the help for this command. | ||||||||||
help_args: Help text for the arguments of this command. | ||||||||||
|
@@ -207,6 +219,8 @@ def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str | |||||||||
setattr(self, key, value) | ||||||||||
self._handler = handler | ||||||||||
self.management_only = management_only | ||||||||||
self.needs_admin = needs_admin | ||||||||||
self.needs_auth = needs_auth | ||||||||||
self.name = name | ||||||||||
self._help_text = help_text | ||||||||||
self._help_args = help_args | ||||||||||
|
@@ -224,6 +238,10 @@ async def get_permission_error(self, evt: CommandEvent) -> Optional[str]: | |||||||||
if self.management_only and not evt.is_management: | ||||||||||
return (f"`{evt.command}` is a restricted command: " | ||||||||||
"you may only run it in management rooms.") | ||||||||||
elif self.needs_admin and not evt.sender.is_admin: | ||||||||||
return "This command requires administrator privileges." | ||||||||||
elif self.needs_auth and not await evt.sender.is_logged_in(): | ||||||||||
return "This command requires you to be logged in." | ||||||||||
return None | ||||||||||
|
||||||||||
def has_permission(self, key: HelpCacheKey) -> bool: | ||||||||||
|
@@ -236,7 +254,9 @@ def has_permission(self, key: HelpCacheKey) -> bool: | |||||||||
True if a user with the given state is allowed to issue the | ||||||||||
command. | ||||||||||
""" | ||||||||||
return not self.management_only or key.is_management | ||||||||||
return ((not self.management_only or key.is_management) and | ||||||||||
(not self.needs_admin or key.is_admin) and | ||||||||||
(not self.needs_auth or key.is_logged_in)) | ||||||||||
|
||||||||||
async def __call__(self, evt: CommandEvent) -> Any: | ||||||||||
"""Executes the command if evt was issued with proper rights. | ||||||||||
|
@@ -267,13 +287,14 @@ def command_handler(_func: Optional[CommandHandlerFunc] = None, *, management_on | |||||||||
name: Optional[str] = None, help_text: str = "", help_args: str = "", | ||||||||||
help_section: HelpSection = None, aliases: Optional[List[str]] = None, | ||||||||||
_handler_class: Type[CommandHandler] = CommandHandler, | ||||||||||
needs_auth: bool = True, needs_admin: bool = False, | ||||||||||
**kwargs) -> Callable[[CommandHandlerFunc], CommandHandler]: | ||||||||||
"""Decorator to create CommandHandlers""" | ||||||||||
|
||||||||||
def decorator(func: CommandHandlerFunc) -> CommandHandler: | ||||||||||
actual_name = name or func.__name__.replace("_", "-") | ||||||||||
handler = _handler_class(func, management_only, actual_name, help_text, help_args, | ||||||||||
help_section, **kwargs) | ||||||||||
help_section, needs_auth, needs_admin, **kwargs) | ||||||||||
command_handlers[handler.name] = handler | ||||||||||
if aliases: | ||||||||||
for alias in aliases: | ||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.