handler.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Awaitable, Callable, List, Optional, NamedTuple, TYPE_CHECKING
  17. from mautrix.types import RoomID, EventID, MessageEventContent
  18. from mautrix.bridge.commands import (HelpSection, CommandEvent as BaseCommandEvent,
  19. CommandHandler as BaseCommandHandler,
  20. CommandProcessor as BaseCommandProcessor,
  21. CommandHandlerFunc, command_handler as base_command_handler)
  22. from .. import user as u
  23. if TYPE_CHECKING:
  24. from ..__main__ import SignalBridge
  25. HelpCacheKey = NamedTuple('HelpCacheKey', is_management=bool, is_portal=bool, is_admin=bool,
  26. is_logged_in=bool)
  27. SECTION_AUTH = HelpSection("Authentication", 10, "")
  28. SECTION_CONNECTION = HelpSection("Connection management", 15, "")
  29. class CommandEvent(BaseCommandEvent):
  30. sender: u.User
  31. bridge: 'SignalBridge'
  32. def __init__(self, processor: 'CommandProcessor', room_id: RoomID, event_id: EventID,
  33. sender: u.User, command: str, args: List[str], content: MessageEventContent,
  34. is_management: bool, is_portal: bool) -> None:
  35. super().__init__(processor, room_id, event_id, sender, command, args, content,
  36. is_management, is_portal)
  37. self.bridge = processor.bridge
  38. self.config = processor.config
  39. @property
  40. def print_error_traceback(self) -> bool:
  41. return self.sender.is_admin
  42. async def get_help_key(self) -> HelpCacheKey:
  43. return HelpCacheKey(self.is_management, self.is_portal, self.sender.is_admin,
  44. await self.sender.is_logged_in())
  45. class CommandHandler(BaseCommandHandler):
  46. name: str
  47. management_only: bool
  48. needs_auth: bool
  49. needs_admin: bool
  50. def __init__(self, handler: Callable[[CommandEvent], Awaitable[EventID]],
  51. management_only: bool, name: str, help_text: str, help_args: str,
  52. help_section: HelpSection, needs_auth: bool, needs_admin: bool) -> None:
  53. super().__init__(handler, management_only, name, help_text, help_args, help_section,
  54. needs_auth=needs_auth, needs_admin=needs_admin)
  55. async def get_permission_error(self, evt: CommandEvent) -> Optional[str]:
  56. if self.management_only and not evt.is_management:
  57. return (f"`{evt.command}` is a restricted command: "
  58. "you may only run it in management rooms.")
  59. elif self.needs_admin and not evt.sender.is_admin:
  60. return "This command requires administrator privileges."
  61. elif self.needs_auth and not await evt.sender.is_logged_in():
  62. return "This command requires you to be logged in."
  63. return None
  64. def has_permission(self, key: HelpCacheKey) -> bool:
  65. return ((not self.management_only or key.is_management) and
  66. (not self.needs_admin or key.is_admin) and
  67. (not self.needs_auth or key.is_logged_in))
  68. def command_handler(_func: Optional[CommandHandlerFunc] = None, *, needs_auth: bool = True,
  69. needs_admin: bool = False, management_only: bool = False,
  70. name: Optional[str] = None, help_text: str = "", help_args: str = "",
  71. help_section: HelpSection = None) -> Callable[[CommandHandlerFunc],
  72. CommandHandler]:
  73. return base_command_handler(_func, _handler_class=CommandHandler, name=name,
  74. help_text=help_text, help_args=help_args,
  75. help_section=help_section, management_only=management_only,
  76. needs_auth=needs_auth, needs_admin=needs_admin)
  77. class CommandProcessor(BaseCommandProcessor):
  78. def __init__(self, bridge: 'SignalBridge') -> None:
  79. super().__init__(bridge=bridge, event_class=CommandEvent)