From f10459521715bb6eddb1d9ea5ae19617231d7561 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Dec 2018 00:52:54 +0200 Subject: [PATCH 1/5] Changes related to rewriting the command handling system --- example-plugin/maubot.yaml | 6 ++ maubot/__init__.py | 2 +- maubot/command_spec.py | 155 ------------------------------------ maubot/handlers/__init__.py | 1 + maubot/handlers/command.py | 15 ++++ maubot/handlers/event.py | 116 +++++++++++++++++++++++++++ maubot/instance.py | 12 ++- maubot/loader/abc.py | 6 +- maubot/matrix.py | 60 +------------- maubot/plugin_base.py | 52 ++++++------ 10 files changed, 182 insertions(+), 243 deletions(-) delete mode 100644 maubot/command_spec.py create mode 100644 maubot/handlers/__init__.py create mode 100644 maubot/handlers/command.py create mode 100644 maubot/handlers/event.py diff --git a/example-plugin/maubot.yaml b/example-plugin/maubot.yaml index c173d37..0665091 100644 --- a/example-plugin/maubot.yaml +++ b/example-plugin/maubot.yaml @@ -1,6 +1,9 @@ # This is an example maubot plugin definition file. # All plugins must include a file like this named "maubot.yaml" in their root directory. +# Target maubot version +maubot: 0.1.0 + # The unique ID for the plugin. Java package naming style. (i.e. use your own domain, not xyz.maubot) id: xyz.maubot.example @@ -24,6 +27,9 @@ modules: # The main class must extend maubot.Plugin main_class: HelloWorldBot +# Whether or not instances need a database +database: false + # Extra files that the upcoming build tool should include in the mbp file. #extra_files: #- base-config.yaml diff --git a/maubot/__init__.py b/maubot/__init__.py index c3148ae..366b951 100644 --- a/maubot/__init__.py +++ b/maubot/__init__.py @@ -1,3 +1,3 @@ from .plugin_base import Plugin -from .command_spec import CommandSpec, Command, PassiveCommand, Argument from .matrix import MaubotMatrixClient as Client, MaubotMessageEvent as MessageEvent +from .handlers import event, command diff --git a/maubot/command_spec.py b/maubot/command_spec.py deleted file mode 100644 index 55c3279..0000000 --- a/maubot/command_spec.py +++ /dev/null @@ -1,155 +0,0 @@ -# maubot - A plugin-based Matrix bot system. -# Copyright (C) 2018 Tulir Asokan -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -from typing import List, Dict, Pattern, Union, Tuple, Optional, Any -from attr import dataclass -import re - -from mautrix.types import MessageEvent, MatchedCommand, MatchedPassiveCommand -from mautrix.client.api.types.util import SerializableAttrs - - -@dataclass -class Argument(SerializableAttrs['Argument']): - matches: str - required: bool = False - description: str = None - - -@dataclass -class Command(SerializableAttrs['Command']): - syntax: str - arguments: Dict[str, Argument] = {} - description: str = None - - -@dataclass -class PassiveCommand(SerializableAttrs['PassiveCommand']): - name: str - matches: str - match_against: str - match_event: MessageEvent = None - - -class ParsedCommand: - name: str - is_passive: bool - arguments: List[str] - starts_with: str - matches: Pattern - match_against: str - match_event: MessageEvent - - def __init__(self, command: Union[PassiveCommand, Command]) -> None: - if isinstance(command, PassiveCommand): - self._init_passive(command) - elif isinstance(command, Command): - self._init_active(command) - else: - raise ValueError("Command parameter must be a Command or a PassiveCommand.") - - def _init_passive(self, command: PassiveCommand) -> None: - self.name = command.name - self.is_passive = True - self.match_against = command.match_against - self.matches = re.compile(command.matches, re.UNICODE) - self.match_event = command.match_event - - def _init_active(self, command: Command) -> None: - self.name = command.syntax - self.is_passive = False - self.arguments = [] - - regex_builder = [] - sw_builder = [] - argument_encountered = False - - for word in command.syntax.split(" "): - arg = command.arguments.get(word, None) - if arg is not None and len(word) > 0: - argument_encountered = True - regex = f"({arg.matches})" - if not arg.required: - regex += "?" - self.arguments.append(word) - regex_builder.append(regex) - else: - if not argument_encountered: - sw_builder.append(word) - regex_builder.append(re.escape(word)) - self.starts_with = "!" + " ".join(sw_builder) - self.matches = re.compile("^!" + " ".join(regex_builder) + "$", re.UNICODE) - self.match_against = "body" - - def match(self, evt: MessageEvent) -> bool: - return self._match_passive(evt) if self.is_passive else self._match_active(evt) - - @staticmethod - def _parse_key(key: str) -> Tuple[str, Optional[str]]: - if '.' not in key: - return key, None - key, next_key = key.split('.', 1) - if len(key) > 0 and key[0] == "[": - end_index = next_key.index("]") - key = key[1:] + "." + next_key[:end_index] - next_key = next_key[end_index + 2:] if len(next_key) > end_index + 1 else None - return key, next_key - - @classmethod - def _recursive_get(cls, data: Any, key: str) -> Any: - if not data: - return None - key, next_key = cls._parse_key(key) - if next_key is not None: - return cls._recursive_get(data[key], next_key) - return data[key] - - def _match_passive(self, evt: MessageEvent) -> bool: - try: - match_against = self._recursive_get(evt.content, self.match_against) - except KeyError: - match_against = None - match_against = match_against or evt.content.body - matches = [[match.string[match.start():match.end()]] + list(match.groups()) - for match in self.matches.finditer(match_against)] - if not matches: - return False - if evt.unsigned.passive_command is None: - evt.unsigned.passive_command = {} - evt.unsigned.passive_command[self.name] = MatchedPassiveCommand(captured=matches) - return True - - def _match_active(self, evt: MessageEvent) -> bool: - if not evt.content.body.startswith(self.starts_with): - return False - match = self.matches.match(evt.content.body) - if not match: - return False - evt.content.command = MatchedCommand(matched=self.name, - arguments=dict(zip(self.arguments, match.groups()))) - return True - - -@dataclass -class CommandSpec(SerializableAttrs['CommandSpec']): - commands: List[Command] = [] - passive_commands: List[PassiveCommand] = [] - - def __add__(self, other: 'CommandSpec') -> 'CommandSpec': - return CommandSpec(commands=self.commands + other.commands, - passive_commands=self.passive_commands + other.passive_commands) - - def parse(self) -> List[ParsedCommand]: - return [ParsedCommand(command) for command in self.commands + self.passive_commands] diff --git a/maubot/handlers/__init__.py b/maubot/handlers/__init__.py new file mode 100644 index 0000000..be2d03e --- /dev/null +++ b/maubot/handlers/__init__.py @@ -0,0 +1 @@ +from . import event, command diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py new file mode 100644 index 0000000..2dbc58d --- /dev/null +++ b/maubot/handlers/command.py @@ -0,0 +1,15 @@ +# maubot - A plugin-based Matrix bot system. +# Copyright (C) 2018 Tulir Asokan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . diff --git a/maubot/handlers/event.py b/maubot/handlers/event.py new file mode 100644 index 0000000..2562bf3 --- /dev/null +++ b/maubot/handlers/event.py @@ -0,0 +1,116 @@ +# maubot - A plugin-based Matrix bot system. +# Copyright (C) 2018 Tulir Asokan +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from typing import Callable, Union, NewType, Any, Tuple, Optional +import functools +import re + +from mautrix.types import EventType, Event, EventContent, MessageEvent, MessageEventContent +from mautrix.client import EventHandler + +EventHandlerDecorator = NewType("EventHandlerDecorator", Callable[[EventHandler], EventHandler]) + + +def handler(var: Union[EventType, EventHandler]) -> Union[EventHandlerDecorator, EventHandler]: + def decorator(func: EventHandler) -> EventHandler: + func.__mb_event_handler__ = True + if isinstance(var, EventType): + func.__mb_event_type__ = var + else: + func.__mb_event_type__ = EventType.ALL + + return func + + if isinstance(var, EventType): + return decorator + else: + decorator(var) + + +class Field: + body: Callable[[MessageEventContent], str] = lambda content: content.body + msgtype: Callable[[MessageEventContent], str] = lambda content: content.msgtype + + +def _parse_key(key: str) -> Tuple[str, Optional[str]]: + if '.' not in key: + return key, None + key, next_key = key.split('.', 1) + if len(key) > 0 and key[0] == "[": + end_index = next_key.index("]") + key = key[1:] + "." + next_key[:end_index] + next_key = next_key[end_index + 2:] if len(next_key) > end_index + 1 else None + return key, next_key + + +def _recursive_get(data: EventContent, key: str) -> Any: + key, next_key = _parse_key(key) + if next_key is not None: + next_data = data.get(key, None) + if next_data is None: + return None + return _recursive_get(next_data, next_key) + return data.get(key, None) + + +def _find_content_field(content: EventContent, field: str) -> Any: + val = _recursive_get(content, field) + if not val and hasattr(content, "unrecognized_"): + val = _recursive_get(content.unrecognized_, field) + return val + + +def handle_own_events(func: EventHandler) -> EventHandler: + func.__mb_handle_own_events__ = True + + +def filter_content(field: Union[str, Callable[[EventContent], Any]], substr: str = None, + pattern: str = None, exact: bool = False): + if substr and pattern: + raise ValueError("You can only provide one of substr or pattern.") + elif not substr and not pattern: + raise ValueError("You must provide either substr or pattern.") + + if not callable(field): + field = functools.partial(_find_content_field, field=field) + + if substr: + def func(evt: MessageEvent) -> bool: + val = field(evt.content) + if val is None: + return False + elif substr in val: + return True + else: + pattern = re.compile(pattern) + + def func(evt: MessageEvent) -> bool: + val = field(evt.content) + if val is None: + return False + elif pattern.match(val): + return True + + return filter(func) + + +def filter(func: Callable[[MessageEvent], bool]) -> EventHandlerDecorator: + def decorator(func: EventHandler) -> EventHandler: + if not hasattr(func, "__mb_event_filters__"): + func.__mb_event_filters__ = [] + func.__mb_event_filters__.append(func) + return func + + return decorator diff --git a/maubot/instance.py b/maubot/instance.py index 3809c5c..15ef6ba 100644 --- a/maubot/instance.py +++ b/maubot/instance.py @@ -15,12 +15,14 @@ # along with this program. If not, see . from typing import Dict, List, Optional from asyncio import AbstractEventLoop +import os.path import logging import io -from sqlalchemy.orm import Session from ruamel.yaml.comments import CommentedMap from ruamel.yaml import YAML +from sqlalchemy.orm import Session +import sqlalchemy as sql from mautrix.util.config import BaseProxyConfig, RecursiveDict from mautrix.types import UserID @@ -133,8 +135,12 @@ class PluginInstance: except (FileNotFoundError, KeyError): self.base_cfg = None self.config = config_class(self.load_config, lambda: self.base_cfg, self.save_config) - self.plugin = cls(self.client.client, self.loop, self.client.http_client, self.id, - self.log, self.config, self.mb_config["plugin_directories.db"]) + db = None + if self.loader.meta.database: + db_path = os.path.join(self.mb_config["plugin_directories.db"], self.id) + db = sql.create_engine(f"sqlite:///{db_path}.db") + self.plugin = cls(client=self.client.client, loop=self.loop, http=self.client.http_client, + instance_id=self.id, log=self.log, config=self.config, database=db) try: await self.plugin.start() except Exception: diff --git a/maubot/loader/abc.py b/maubot/loader/abc.py index 24bd622..f4b62a7 100644 --- a/maubot/loader/abc.py +++ b/maubot/loader/abc.py @@ -22,6 +22,7 @@ from packaging.version import Version, InvalidVersion from mautrix.client.api.types.util import (SerializableAttrs, SerializerError, serializer, deserializer) +from ..__meta__ import __version__ from ..plugin_base import Plugin if TYPE_CHECKING: @@ -51,9 +52,12 @@ def deserialize_version(version: str) -> Version: class PluginMeta(SerializableAttrs['PluginMeta']): id: str version: Version - license: str modules: List[str] main_class: str + + maubot: Version = Version(__version__) + database: bool = False + license: str = "" extra_files: List[str] = [] dependencies: List[str] = [] soft_dependencies: List[str] = [] diff --git a/maubot/matrix.py b/maubot/matrix.py index c152af9..2969c9e 100644 --- a/maubot/matrix.py +++ b/maubot/matrix.py @@ -13,19 +13,16 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Dict, List, Union, Callable, Awaitable, Optional, Tuple +from typing import Union, Awaitable, Optional, Tuple from markdown.extensions import Extension import markdown as md import attr from mautrix import Client as MatrixClient from mautrix.util.formatter import parse_html -from mautrix.client import EventHandler from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent, MessageType, TextMessageEventContent, Format, RelatesTo) -from .command_spec import ParsedCommand, CommandSpec - class EscapeHTML(Extension): def extendMarkdown(self, md): @@ -71,14 +68,6 @@ class MaubotMessageEvent(MessageEvent): class MaubotMatrixClient(MatrixClient): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.command_handlers: Dict[str, List[EventHandler]] = {} - self.commands: List[ParsedCommand] = [] - self.command_specs: Dict[str, CommandSpec] = {} - - self.add_event_handler(self._command_event_handler, EventType.ROOM_MESSAGE) - def send_markdown(self, room_id: RoomID, markdown: str, msgtype: MessageType = MessageType.TEXT, relates_to: Optional[RelatesTo] = None, **kwargs) -> Awaitable[EventID]: content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML) @@ -87,53 +76,6 @@ class MaubotMatrixClient(MatrixClient): content.relates_to = relates_to return self.send_message(room_id, content, **kwargs) - def set_command_spec(self, plugin_id: str, spec: CommandSpec) -> None: - self.command_specs[plugin_id] = spec - self._reparse_command_specs() - - def _reparse_command_specs(self) -> None: - self.commands = [parsed_command - for spec in self.command_specs.values() - for parsed_command in spec.parse()] - - def remove_command_spec(self, plugin_id: str) -> None: - try: - del self.command_specs[plugin_id] - self._reparse_command_specs() - except KeyError: - pass - - async def _command_event_handler(self, evt: MessageEvent) -> None: - if evt.sender == self.mxid or evt.content.msgtype == MessageType.NOTICE: - return - for command in self.commands: - if command.match(evt): - await self._trigger_command(command, evt) - return - - async def _trigger_command(self, command: ParsedCommand, evt: MessageEvent) -> None: - for handler in self.command_handlers.get(command.name, []): - await handler(evt) - - def on(self, var: Union[EventHandler, EventType, str] - ) -> Union[EventHandler, Callable[[EventHandler], EventHandler]]: - if isinstance(var, str): - def decorator(func: EventHandler) -> EventHandler: - self.add_command_handler(var, func) - return func - - return decorator - return super().on(var) - - def add_command_handler(self, command: str, handler: EventHandler) -> None: - self.command_handlers.setdefault(command, []).append(handler) - - def remove_command_handler(self, command: str, handler: EventHandler) -> None: - try: - self.command_handlers[command].remove(handler) - except (KeyError, ValueError): - pass - async def call_handlers(self, event: Event) -> None: if isinstance(event, MessageEvent): event = MaubotMessageEvent(event, self) diff --git a/maubot/plugin_base.py b/maubot/plugin_base.py index 3925513..d41ae9c 100644 --- a/maubot/plugin_base.py +++ b/maubot/plugin_base.py @@ -14,21 +14,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from typing import Type, Optional, TYPE_CHECKING +from abc import ABC from logging import Logger -from abc import ABC, abstractmethod from asyncio import AbstractEventLoop -from aiohttp import ClientSession -import os.path +import functools from sqlalchemy.engine.base import Engine -import sqlalchemy as sql +from aiohttp import ClientSession if TYPE_CHECKING: - from .client import MaubotMatrixClient - from .command_spec import CommandSpec + from mautrix.types import Event from mautrix.util.config import BaseProxyConfig - -DatabaseNotConfigured = ValueError("A database for this maubot instance has not been configured.") + from .client import MaubotMatrixClient class Plugin(ABC): @@ -37,33 +34,40 @@ class Plugin(ABC): log: Logger loop: AbstractEventLoop config: Optional['BaseProxyConfig'] + database: Optional[Engine] def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession, - plugin_instance_id: str, log: Logger, config: Optional['BaseProxyConfig'], - db_base_path: str) -> None: + instance_id: str, log: Logger, config: Optional['BaseProxyConfig'], + database: Optional[Engine]) -> None: self.client = client self.loop = loop self.http = http - self.id = plugin_instance_id + self.id = instance_id self.log = log self.config = config - self.__db_base_path = db_base_path + self.database = database + self._handlers_at_startup = [] - def request_db_engine(self) -> Optional[Engine]: - if not self.__db_base_path: - raise DatabaseNotConfigured - return sql.create_engine(f"sqlite:///{os.path.join(self.__db_base_path, self.id)}.db") - - def set_command_spec(self, spec: 'CommandSpec') -> None: - self.client.set_command_spec(self.id, spec) - - @abstractmethod async def start(self) -> None: - pass + for key in dir(self): + val = getattr(self, key) + if hasattr(val, "__mb_event_handler__"): + handle_own_events = hasattr(val, "__mb_handle_own_events__") + + @functools.wraps(val) + async def handler(event: Event) -> None: + if not handle_own_events and getattr(event, "sender", "") == self.client.mxid: + return + for filter in val.__mb_event_filters__: + if not filter(event): + return + await val(event) + self._handlers_at_startup.append((handler, val.__mb_event_type__)) + self.client.add_event_handler(val.__mb_event_type__, handler) - @abstractmethod async def stop(self) -> None: - pass + for func, event_type in self._handlers_at_startup: + self.client.remove_event_handler(event_type, func) @classmethod def get_config_class(cls) -> Optional[Type['BaseProxyConfig']]: From 682eab348d77cdf3da5b71e5b9dab1a7459464f8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 18 Dec 2018 00:53:39 +0200 Subject: [PATCH 2/5] Some sort of command handling system --- maubot/__init__.py | 1 - maubot/handlers/command.py | 86 ++++++++++++++++++++++++++++++++++++++ maubot/handlers/event.py | 19 +++++---- maubot/matrix.py | 14 ++++--- maubot/plugin_base.py | 14 +------ 5 files changed, 107 insertions(+), 27 deletions(-) diff --git a/maubot/__init__.py b/maubot/__init__.py index 366b951..3e2ffac 100644 --- a/maubot/__init__.py +++ b/maubot/__init__.py @@ -1,3 +1,2 @@ from .plugin_base import Plugin from .matrix import MaubotMatrixClient as Client, MaubotMessageEvent as MessageEvent -from .handlers import event, command diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index 2dbc58d..c2e8199 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -13,3 +13,89 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from typing import Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any +import functools +import re + +from mautrix.client import EventHandler +from mautrix.types import MessageType + +from ..matrix import MaubotMessageEvent +from .event import EventHandlerDecorator + +PrefixType = Union[str, Callable[[], str]] +CommandDecorator = Callable[[PrefixType, str], EventHandlerDecorator] + + +def _get_subcommand_decorator(parent: EventHandler) -> CommandDecorator: + def subcommand(name: PrefixType, help: str = None) -> EventHandlerDecorator: + cmd_decorator = new(name=f"{parent.__mb_name__} {name}", help=help) + + def decorator(func: EventHandler) -> EventHandler: + func = cmd_decorator(func) + parent.__mb_subcommands__.append(func) + return func + + return decorator + + return subcommand + + +def new(name: Union[str, Callable[[], str]], help: str = None) -> EventHandlerDecorator: + def decorator(func: EventHandler) -> EventHandler: + func.__mb_subcommands__ = [] + func.__mb_help__ = help + func.__mb_name__ = name or func.__name__ + func.subcommand = _get_subcommand_decorator(func) + return func + + return decorator + + +PassiveCommandHandler = Callable[[MaubotMessageEvent, ...], Awaitable[None]] +PassiveCommandHandlerDecorator = NewType("PassiveCommandHandlerDecorator", + Callable[[PassiveCommandHandler], PassiveCommandHandler]) + + +def passive(regex: Union[str, Pattern], msgtypes: Sequence[MessageType] = (MessageType.TEXT,), + field: Callable[[MaubotMessageEvent], str] = lambda event: event.content.body + ) -> PassiveCommandHandlerDecorator: + if not isinstance(regex, Pattern): + regex = re.compile(regex) + + def decorator(func: PassiveCommandHandler) -> PassiveCommandHandler: + @functools.wraps(func) + async def replacement(event: MaubotMessageEvent) -> None: + if event.sender == event.client.mxid: + return + elif msgtypes and event.content.msgtype not in msgtypes: + return + match = regex.match(field(event)) + if match: + await func(event, *list(match.groups())) + + return replacement + + return decorator + + +class _Argument: + def __init__(self, name: str, required: bool, matches: Optional[str], + parser: Optional[Callable[[str], Any]]) -> None: + pass + + +def argument(name: str, *, required: bool = True, matches: Optional[str] = None, + parser: Optional[Callable[[str], Any]] = None) -> EventHandlerDecorator: + def decorator(func: EventHandler) -> EventHandler: + if not hasattr(func, "__mb_arguments__"): + func.__mb_arguments__ = [] + func.__mb_arguments__.append(_Argument(name, required, matches, parser)) + return func + + return decorator + + +def vararg(func: EventHandler) -> EventHandler: + func.__mb_vararg__ = True + return func diff --git a/maubot/handlers/event.py b/maubot/handlers/event.py index 2562bf3..7bec17e 100644 --- a/maubot/handlers/event.py +++ b/maubot/handlers/event.py @@ -23,20 +23,21 @@ from mautrix.client import EventHandler EventHandlerDecorator = NewType("EventHandlerDecorator", Callable[[EventHandler], EventHandler]) -def handler(var: Union[EventType, EventHandler]) -> Union[EventHandlerDecorator, EventHandler]: +def on(var: Union[EventType, EventHandler]) -> Union[EventHandlerDecorator, EventHandler]: def decorator(func: EventHandler) -> EventHandler: - func.__mb_event_handler__ = True + @functools.wraps(func) + async def wrapper(event: Event) -> None: + pass + + wrapper.__mb_event_handler__ = True if isinstance(var, EventType): - func.__mb_event_type__ = var + wrapper.__mb_event_type__ = var else: - func.__mb_event_type__ = EventType.ALL + wrapper.__mb_event_type__ = EventType.ALL - return func + return wrapper - if isinstance(var, EventType): - return decorator - else: - decorator(var) + return decorator if isinstance(var, EventType) else decorator(var) class Field: diff --git a/maubot/matrix.py b/maubot/matrix.py index 2969c9e..1cb2d6e 100644 --- a/maubot/matrix.py +++ b/maubot/matrix.py @@ -21,7 +21,7 @@ import attr from mautrix import Client as MatrixClient from mautrix.util.formatter import parse_html from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent, - MessageType, TextMessageEventContent, Format, RelatesTo) + MessageType, TextMessageEventContent, Format, RelatesTo, StateEvent) class EscapeHTML(Extension): @@ -39,12 +39,12 @@ def parse_markdown(markdown: str, allow_html: bool = False) -> Tuple[str, str]: class MaubotMessageEvent(MessageEvent): - _client: MatrixClient + client: MatrixClient def __init__(self, base: MessageEvent, client: MatrixClient): super().__init__(**{a.name.lstrip("_"): getattr(base, a.name) for a in attr.fields(MessageEvent)}) - self._client = client + self.client = client def respond(self, content: Union[str, MessageEventContent], event_type: EventType = EventType.ROOM_MESSAGE, @@ -56,7 +56,7 @@ class MaubotMessageEvent(MessageEvent): content.body, content.formatted_body = parse_markdown(content.body) if reply: content.set_reply(self) - return self._client.send_message_event(self.room_id, event_type, content) + return self.client.send_message_event(self.room_id, event_type, content) def reply(self, content: Union[str, MessageEventContent], event_type: EventType = EventType.ROOM_MESSAGE, @@ -64,7 +64,7 @@ class MaubotMessageEvent(MessageEvent): return self.respond(content, event_type, markdown, reply=True) def mark_read(self) -> Awaitable[None]: - return self._client.send_receipt(self.room_id, self.event_id, "m.read") + return self.client.send_receipt(self.room_id, self.event_id, "m.read") class MaubotMatrixClient(MatrixClient): @@ -79,10 +79,14 @@ class MaubotMatrixClient(MatrixClient): async def call_handlers(self, event: Event) -> None: if isinstance(event, MessageEvent): event = MaubotMessageEvent(event, self) + else: + event.client = self return await super().call_handlers(event) async def get_event(self, room_id: RoomID, event_id: EventID) -> Event: event = await super().get_event(room_id, event_id) if isinstance(event, MessageEvent): return MaubotMessageEvent(event, self) + else: + event.client = self return event diff --git a/maubot/plugin_base.py b/maubot/plugin_base.py index d41ae9c..2835b66 100644 --- a/maubot/plugin_base.py +++ b/maubot/plugin_base.py @@ -52,18 +52,8 @@ class Plugin(ABC): for key in dir(self): val = getattr(self, key) if hasattr(val, "__mb_event_handler__"): - handle_own_events = hasattr(val, "__mb_handle_own_events__") - - @functools.wraps(val) - async def handler(event: Event) -> None: - if not handle_own_events and getattr(event, "sender", "") == self.client.mxid: - return - for filter in val.__mb_event_filters__: - if not filter(event): - return - await val(event) - self._handlers_at_startup.append((handler, val.__mb_event_type__)) - self.client.add_event_handler(val.__mb_event_type__, handler) + self._handlers_at_startup.append((val, val.__mb_event_type__)) + self.client.add_event_handler(val.__mb_event_type__, val) async def stop(self) -> None: for func, event_type in self._handlers_at_startup: From 5ff5eae3c69c219ecc1dc37729169c54768e0108 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 24 Dec 2018 00:31:01 +0200 Subject: [PATCH 3/5] Make new command handling actually somewhat work --- maubot/cli/commands/build.py | 2 + maubot/client.py | 6 +- maubot/config.py | 3 +- maubot/handlers/command.py | 207 ++++++++++++++++++++++++++--------- maubot/handlers/event.py | 95 +--------------- maubot/plugin_base.py | 2 +- 6 files changed, 171 insertions(+), 144 deletions(-) diff --git a/maubot/cli/commands/build.py b/maubot/cli/commands/build.py index 0f3201c..823cb2e 100644 --- a/maubot/cli/commands/build.py +++ b/maubot/cli/commands/build.py @@ -118,6 +118,8 @@ def upload_plugin(output: Union[str, IO]) -> None: default=False) def build(path: str, output: str, upload: bool) -> None: meta = read_meta(path) + if not meta: + return if output or not upload: output = read_output_path(output, meta) if not output: diff --git a/maubot/client.py b/maubot/client.py index 5b91d8c..1ec6d4f 100644 --- a/maubot/client.py +++ b/maubot/client.py @@ -55,7 +55,7 @@ class Client: token=self.access_token, client_session=self.http_client, log=self.log, loop=self.loop, store=self.db_instance) if self.autojoin: - self.client.add_event_handler(self._handle_invite, EventType.ROOM_MEMBER) + self.client.add_event_handler(EventType.ROOM_MEMBER, self._handle_invite) async def start(self, try_n: Optional[int] = 0) -> None: try: @@ -260,9 +260,9 @@ class Client: if value == self.db_instance.autojoin: return if value: - self.client.add_event_handler(self._handle_invite, EventType.ROOM_MEMBER) + self.client.add_event_handler(EventType.ROOM_MEMBER, self._handle_invite) else: - self.client.remove_event_handler(self._handle_invite, EventType.ROOM_MEMBER) + self.client.remove_event_handler(EventType.ROOM_MEMBER, self._handle_invite) self.db_instance.autojoin = value @property diff --git a/maubot/config.py b/maubot/config.py index ab3e080..b36a5c5 100644 --- a/maubot/config.py +++ b/maubot/config.py @@ -29,7 +29,8 @@ class Config(BaseFileConfig): return "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(64)) def do_update(self, helper: ConfigUpdateHelper) -> None: - base, copy, _ = helper + base = helper.base + copy = helper.copy copy("database") copy("plugin_directories.upload") copy("plugin_directories.load") diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index c2e8199..5e1b894 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -13,89 +13,196 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any +from typing import Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any, List, Dict import functools import re -from mautrix.client import EventHandler -from mautrix.types import MessageType +from mautrix.types import MessageType, EventType from ..matrix import MaubotMessageEvent -from .event import EventHandlerDecorator +from . import event -PrefixType = Union[str, Callable[[], str]] -CommandDecorator = Callable[[PrefixType, str], EventHandlerDecorator] +PrefixType = Optional[Union[str, Callable[[], str]]] +CommandHandlerFunc = NewType("CommandHandlerFunc", + Callable[[MaubotMessageEvent, Any], Awaitable[Any]]) +CommandHandlerDecorator = NewType("CommandHandlerDecorator", + Callable[[Union['CommandHandler', CommandHandlerFunc]], + 'CommandHandler']) +PassiveCommandHandlerDecorator = NewType("PassiveCommandHandlerDecorator", + Callable[[CommandHandlerFunc], CommandHandlerFunc]) -def _get_subcommand_decorator(parent: EventHandler) -> CommandDecorator: - def subcommand(name: PrefixType, help: str = None) -> EventHandlerDecorator: - cmd_decorator = new(name=f"{parent.__mb_name__} {name}", help=help) +class CommandHandler: + def __init__(self, func: CommandHandlerFunc) -> None: + self.__mb_func__: CommandHandlerFunc = func + self.__mb_subcommands__: Dict[str, CommandHandler] = {} + self.__mb_arguments__: List[Argument] = [] + self.__mb_help__: str = None + self.__mb_name__: str = None + self.__mb_prefix__: str = None + self.__mb_require_subcommand__: bool = True + self.__mb_event_handler__: bool = True + self.__mb_event_type__: EventType = EventType.ROOM_MESSAGE + self.__class_instance: Any = None - def decorator(func: EventHandler) -> EventHandler: - func = cmd_decorator(func) - parent.__mb_subcommands__.append(func) + async def __call__(self, evt: MaubotMessageEvent, *, + _existing_args: Dict[str, Any] = None) -> Any: + body = evt.content.body + if evt.sender == evt.client.mxid or not body.startswith(self.__mb_prefix__): + return + call_args: Dict[str, Any] = {**_existing_args} if _existing_args else {} + remaining_val = body[len(self.__mb_prefix__) + 1:] + # TODO update remaining_val somehow + for arg in self.__mb_arguments__: + try: + call_args[arg.name] = arg.match(remaining_val) + if arg.required and not call_args[arg.name]: + raise ValueError("Argument required") + except ArgumentSyntaxError as e: + await evt.reply(e.message + (f"\n{self.__mb_usage__}" if e.show_usage else "")) + return + except ValueError as e: + await evt.reply(self.__mb_usage__) + return + + if len(self.__mb_subcommands__) > 0: + split = remaining_val.split(" ") if len(remaining_val) > 0 else [] + try: + subcommand = self.__mb_subcommands__[split[0]] + return await subcommand(evt, _existing_args=call_args) + except (KeyError, IndexError): + if self.__mb_require_subcommand__: + await evt.reply(self.__mb_full_help__) + return + return (await self.__mb_func__(self.__class_instance, evt, **call_args) + if self.__class_instance + else await self.__mb_func__(evt, **call_args)) + + def __get__(self, instance, instancetype): + self.__class_instance = instance + return self + + @property + def __mb_full_help__(self) -> str: + basic = self.__mb_usage__ + usage = f"{basic} [...]\n\n" + usage += "\n".join(f"* {cmd.__mb_name__} {cmd.__mb_usage_args__} - {cmd.__mb_help__}" + for cmd in self.__mb_subcommands__.values()) + return usage + + @property + def __mb_usage_args__(self) -> str: + return " ".join(f"<{arg.label}>" if arg.required else f"[{arg.label}]" + for arg in self.__mb_arguments__) + + @property + def __mb_usage__(self) -> str: + return f"**Usage:** {self.__mb_prefix__} {self.__mb_usage_args__}" + + def subcommand(self, name: PrefixType = None, help: str = None + ) -> CommandHandlerDecorator: + def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: + if not isinstance(func, CommandHandler): + func = CommandHandler(func) + func.__mb_name__ = name or func.__name__ + func.__mb_prefix__ = f"{self.__mb_prefix__} {func.__mb_name__}" + func.__mb_help__ = help + func.__mb_event_handler__ = False + self.__mb_subcommands__[func.__mb_name__] = func return func return decorator - return subcommand + +class ArgumentSyntaxError(ValueError): + def __init__(self, message: str, show_usage: bool = True) -> None: + super().__init__(message) + self.message = message + self.show_usage = show_usage -def new(name: Union[str, Callable[[], str]], help: str = None) -> EventHandlerDecorator: - def decorator(func: EventHandler) -> EventHandler: - func.__mb_subcommands__ = [] +class Argument: + def __init__(self, name: str, label: str = None, *, required: bool = False, + matches: Optional[str] = None, parser: Optional[Callable[[str], Any]] = None, + pass_raw: bool = False) -> None: + self.name = name + self.required = required + self.label = label or name + + if not parser: + if matches: + regex = re.compile(matches) + + def parser(val: str) -> Optional[Sequence[str]]: + match = regex.match(val) + return match.groups() if match else None + else: + def parser(val: str) -> str: + return val + + if not pass_raw: + o_parser = parser + + def parser(val: str) -> Any: + val = val.strip().split(" ") + return o_parser(val[0]) + + self.parser = parser + + def match(self, val: str) -> Any: + return self.parser(val) + + def __call__(self, func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: + if not isinstance(func, CommandHandler): + func = CommandHandler(func) + func.__mb_arguments__.append(self) + return func + + +def new(name: PrefixType, *, help: str = None, event_type: EventType = EventType.ROOM_MESSAGE, + require_subcommand: bool = True) -> CommandHandlerDecorator: + def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: + if not isinstance(func, CommandHandler): + func = CommandHandler(func) func.__mb_help__ = help func.__mb_name__ = name or func.__name__ - func.subcommand = _get_subcommand_decorator(func) + func.__mb_require_subcommand__ = require_subcommand + func.__mb_prefix__ = f"!{func.__mb_name__}" + func.__mb_event_type__ = event_type return func return decorator -PassiveCommandHandler = Callable[[MaubotMessageEvent, ...], Awaitable[None]] -PassiveCommandHandlerDecorator = NewType("PassiveCommandHandlerDecorator", - Callable[[PassiveCommandHandler], PassiveCommandHandler]) +def argument(name: str, label: str = None, *, required: bool = True, matches: Optional[str] = None, + parser: Optional[Callable[[str], Any]] = None) -> CommandHandlerDecorator: + return Argument(name, label, required=required, matches=matches, parser=parser) def passive(regex: Union[str, Pattern], msgtypes: Sequence[MessageType] = (MessageType.TEXT,), - field: Callable[[MaubotMessageEvent], str] = lambda event: event.content.body - ) -> PassiveCommandHandlerDecorator: + field: Callable[[MaubotMessageEvent], str] = lambda event: event.content.body, + event_type: EventType = EventType.ROOM_MESSAGE) -> PassiveCommandHandlerDecorator: if not isinstance(regex, Pattern): regex = re.compile(regex) - def decorator(func: PassiveCommandHandler) -> PassiveCommandHandler: + def decorator(func: CommandHandlerFunc) -> CommandHandlerFunc: + @event.on(event_type) @functools.wraps(func) - async def replacement(event: MaubotMessageEvent) -> None: - if event.sender == event.client.mxid: + async def replacement(self, evt: MaubotMessageEvent) -> None: + if isinstance(self, MaubotMessageEvent): + evt = self + self = None + if evt.sender == evt.client.mxid: return - elif msgtypes and event.content.msgtype not in msgtypes: + elif msgtypes and evt.content.msgtype not in msgtypes: return - match = regex.match(field(event)) + match = regex.match(field(evt)) if match: - await func(event, *list(match.groups())) + if self: + await func(self, evt, *list(match.groups())) + else: + await func(evt, *list(match.groups())) return replacement return decorator - - -class _Argument: - def __init__(self, name: str, required: bool, matches: Optional[str], - parser: Optional[Callable[[str], Any]]) -> None: - pass - - -def argument(name: str, *, required: bool = True, matches: Optional[str] = None, - parser: Optional[Callable[[str], Any]] = None) -> EventHandlerDecorator: - def decorator(func: EventHandler) -> EventHandler: - if not hasattr(func, "__mb_arguments__"): - func.__mb_arguments__ = [] - func.__mb_arguments__.append(_Argument(name, required, matches, parser)) - return func - - return decorator - - -def vararg(func: EventHandler) -> EventHandler: - func.__mb_vararg__ = True - return func diff --git a/maubot/handlers/event.py b/maubot/handlers/event.py index 7bec17e..d6bde8e 100644 --- a/maubot/handlers/event.py +++ b/maubot/handlers/event.py @@ -13,11 +13,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Callable, Union, NewType, Any, Tuple, Optional -import functools -import re +from typing import Callable, Union, NewType -from mautrix.types import EventType, Event, EventContent, MessageEvent, MessageEventContent +from mautrix.types import EventType from mautrix.client import EventHandler EventHandlerDecorator = NewType("EventHandlerDecorator", Callable[[EventHandler], EventHandler]) @@ -25,93 +23,12 @@ EventHandlerDecorator = NewType("EventHandlerDecorator", Callable[[EventHandler] def on(var: Union[EventType, EventHandler]) -> Union[EventHandlerDecorator, EventHandler]: def decorator(func: EventHandler) -> EventHandler: - @functools.wraps(func) - async def wrapper(event: Event) -> None: - pass - - wrapper.__mb_event_handler__ = True + func.__mb_event_handler__ = True if isinstance(var, EventType): - wrapper.__mb_event_type__ = var + func.__mb_event_type__ = var else: - wrapper.__mb_event_type__ = EventType.ALL + func.__mb_event_type__ = EventType.ALL - return wrapper - - return decorator if isinstance(var, EventType) else decorator(var) - - -class Field: - body: Callable[[MessageEventContent], str] = lambda content: content.body - msgtype: Callable[[MessageEventContent], str] = lambda content: content.msgtype - - -def _parse_key(key: str) -> Tuple[str, Optional[str]]: - if '.' not in key: - return key, None - key, next_key = key.split('.', 1) - if len(key) > 0 and key[0] == "[": - end_index = next_key.index("]") - key = key[1:] + "." + next_key[:end_index] - next_key = next_key[end_index + 2:] if len(next_key) > end_index + 1 else None - return key, next_key - - -def _recursive_get(data: EventContent, key: str) -> Any: - key, next_key = _parse_key(key) - if next_key is not None: - next_data = data.get(key, None) - if next_data is None: - return None - return _recursive_get(next_data, next_key) - return data.get(key, None) - - -def _find_content_field(content: EventContent, field: str) -> Any: - val = _recursive_get(content, field) - if not val and hasattr(content, "unrecognized_"): - val = _recursive_get(content.unrecognized_, field) - return val - - -def handle_own_events(func: EventHandler) -> EventHandler: - func.__mb_handle_own_events__ = True - - -def filter_content(field: Union[str, Callable[[EventContent], Any]], substr: str = None, - pattern: str = None, exact: bool = False): - if substr and pattern: - raise ValueError("You can only provide one of substr or pattern.") - elif not substr and not pattern: - raise ValueError("You must provide either substr or pattern.") - - if not callable(field): - field = functools.partial(_find_content_field, field=field) - - if substr: - def func(evt: MessageEvent) -> bool: - val = field(evt.content) - if val is None: - return False - elif substr in val: - return True - else: - pattern = re.compile(pattern) - - def func(evt: MessageEvent) -> bool: - val = field(evt.content) - if val is None: - return False - elif pattern.match(val): - return True - - return filter(func) - - -def filter(func: Callable[[MessageEvent], bool]) -> EventHandlerDecorator: - def decorator(func: EventHandler) -> EventHandler: - if not hasattr(func, "__mb_event_filters__"): - func.__mb_event_filters__ = [] - func.__mb_event_filters__.append(func) return func - return decorator + return decorator if isinstance(var, EventType) else decorator(var) diff --git a/maubot/plugin_base.py b/maubot/plugin_base.py index 2835b66..c720cc5 100644 --- a/maubot/plugin_base.py +++ b/maubot/plugin_base.py @@ -51,7 +51,7 @@ class Plugin(ABC): async def start(self) -> None: for key in dir(self): val = getattr(self, key) - if hasattr(val, "__mb_event_handler__"): + if hasattr(val, "__mb_event_handler__") and val.__mb_event_handler__: self._handlers_at_startup.append((val, val.__mb_event_type__)) self.client.add_event_handler(val.__mb_event_type__, val) From 0cf06f9f6bf3a3e89f73ce2f06b4529753b43c3c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 25 Dec 2018 00:37:02 +0200 Subject: [PATCH 4/5] Make new command handling system fully work --- maubot/handlers/command.py | 263 ++++++++++++++++++++++++++----------- 1 file changed, 185 insertions(+), 78 deletions(-) diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index 5e1b894..f850d12 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -13,7 +13,10 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any, List, Dict +from typing import (Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any, List, + Dict, Tuple) +from abc import ABC, abstractmethod +import asyncio import functools import re @@ -41,42 +44,65 @@ class CommandHandler: self.__mb_name__: str = None self.__mb_prefix__: str = None self.__mb_require_subcommand__: bool = True + self.__mb_arg_fallthrough__: bool = True self.__mb_event_handler__: bool = True self.__mb_event_type__: EventType = EventType.ROOM_MESSAGE self.__class_instance: Any = None - async def __call__(self, evt: MaubotMessageEvent, *, - _existing_args: Dict[str, Any] = None) -> Any: + async def __call__(self, evt: MaubotMessageEvent, *, _existing_args: Dict[str, Any] = None, + _remaining_val: str = None) -> Any: body = evt.content.body - if evt.sender == evt.client.mxid or not body.startswith(self.__mb_prefix__): + has_prefix = _remaining_val or body.startswith(self.__mb_prefix__) + if evt.sender == evt.client.mxid or not has_prefix: return call_args: Dict[str, Any] = {**_existing_args} if _existing_args else {} - remaining_val = body[len(self.__mb_prefix__) + 1:] - # TODO update remaining_val somehow + remaining_val = _remaining_val or body[len(self.__mb_prefix__) + 1:] + + if not self.__mb_arg_fallthrough__ and len(self.__mb_subcommands__) > 0: + ok, res = await self.__call_subcommand__(evt, call_args, remaining_val) + if ok: + return res + + ok, remaining_val = await self.__parse_args__(evt, call_args, remaining_val) + if not ok: + return + elif self.__mb_arg_fallthrough__ and len(self.__mb_subcommands__) > 0: + ok, res = await self.__call_subcommand__(evt, call_args, remaining_val) + if ok: + return res + elif self.__mb_require_subcommand__: + await evt.reply(self.__mb_full_help__) + return + + if self.__class_instance: + return await self.__mb_func__(self.__class_instance, evt, **call_args) + return await self.__mb_func__(evt, **call_args) + + async def __call_subcommand__(self, evt: MaubotMessageEvent, call_args: Dict[str, Any], + remaining_val: str) -> Tuple[bool, Any]: + remaining_val = remaining_val.strip() + split = remaining_val.split(" ") if len(remaining_val) > 0 else [] + try: + subcommand = self.__mb_subcommands__[split[0]] + return True, await subcommand(evt, _existing_args=call_args, + _remaining_val=" ".join(split[1:])) + except (KeyError, IndexError): + return False, None + + async def __parse_args__(self, evt: MaubotMessageEvent, call_args: Dict[str, Any], + remaining_val: str) -> Tuple[bool, str]: for arg in self.__mb_arguments__: try: - call_args[arg.name] = arg.match(remaining_val) + remaining_val, call_args[arg.name] = arg.match(remaining_val.strip()) if arg.required and not call_args[arg.name]: raise ValueError("Argument required") except ArgumentSyntaxError as e: await evt.reply(e.message + (f"\n{self.__mb_usage__}" if e.show_usage else "")) - return + return False, remaining_val except ValueError as e: await evt.reply(self.__mb_usage__) - return - - if len(self.__mb_subcommands__) > 0: - split = remaining_val.split(" ") if len(remaining_val) > 0 else [] - try: - subcommand = self.__mb_subcommands__[split[0]] - return await subcommand(evt, _existing_args=call_args) - except (KeyError, IndexError): - if self.__mb_require_subcommand__: - await evt.reply(self.__mb_full_help__) - return - return (await self.__mb_func__(self.__class_instance, evt, **call_args) - if self.__class_instance - else await self.__mb_func__(evt, **call_args)) + return False, remaining_val + return True, remaining_val def __get__(self, instance, instancetype): self.__class_instance = instance @@ -84,20 +110,45 @@ class CommandHandler: @property def __mb_full_help__(self) -> str: - basic = self.__mb_usage__ - usage = f"{basic} [...]\n\n" - usage += "\n".join(f"* {cmd.__mb_name__} {cmd.__mb_usage_args__} - {cmd.__mb_help__}" - for cmd in self.__mb_subcommands__.values()) + usage = self.__mb_usage_without_subcommands__ + "\n\n" + usage += "\n".join(cmd.__mb_usage_inline__ for cmd in self.__mb_subcommands__.values()) return usage @property def __mb_usage_args__(self) -> str: - return " ".join(f"<{arg.label}>" if arg.required else f"[{arg.label}]" - for arg in self.__mb_arguments__) + arg_usage = " ".join(f"<{arg.label}>" if arg.required else f"[{arg.label}]" + for arg in self.__mb_arguments__) + if self.__mb_subcommands__ and self.__mb_arg_fallthrough__: + arg_usage += " " + self.__mb_usage_subcommand__ + return arg_usage + + @property + def __mb_usage_subcommand__(self) -> str: + return f" [...]" + + @property + def __mb_usage_inline__(self) -> str: + if not self.__mb_arg_fallthrough__: + return (f"* {self.__mb_name__} {self.__mb_usage_args__} - {self.__mb_help__}\n" + f"* {self.__mb_name__} {self.__mb_usage_subcommand__}") + return f"* {self.__mb_name__} {self.__mb_usage_args__} - {self.__mb_help__}" + + @property + def __mb_subcommands_list__(self) -> str: + return f"**Subcommands:** {', '.join(self.__mb_subcommands__.keys())}" + + @property + def __mb_usage_without_subcommands__(self) -> str: + if not self.__mb_arg_fallthrough__: + return (f"**Usage:** {self.__mb_prefix__} {self.__mb_usage_args__}" + f" _OR_ {self.__mb_usage_subcommand__}") + return f"**Usage:** {self.__mb_prefix__} {self.__mb_usage_args__}" @property def __mb_usage__(self) -> str: - return f"**Usage:** {self.__mb_prefix__} {self.__mb_usage_args__}" + if len(self.__mb_subcommands__) > 0: + return f"{self.__mb_usage_without_subcommands__} \n{self.__mb_subcommands_list__}" + return self.__mb_usage_without_subcommands__ def subcommand(self, name: PrefixType = None, help: str = None ) -> CommandHandlerDecorator: @@ -114,6 +165,22 @@ class CommandHandler: return decorator +def new(name: PrefixType, *, help: str = None, event_type: EventType = EventType.ROOM_MESSAGE, + require_subcommand: bool = True, arg_fallthrough: bool = True) -> CommandHandlerDecorator: + def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: + if not isinstance(func, CommandHandler): + func = CommandHandler(func) + func.__mb_help__ = help + func.__mb_name__ = name or func.__name__ + func.__mb_require_subcommand__ = require_subcommand + func.__mb_arg_fallthrough__ = arg_fallthrough + func.__mb_prefix__ = f"!{func.__mb_name__}" + func.__mb_event_type__ = event_type + return func + + return decorator + + class ArgumentSyntaxError(ValueError): def __init__(self, message: str, show_usage: bool = True) -> None: super().__init__(message) @@ -121,36 +188,17 @@ class ArgumentSyntaxError(ValueError): self.show_usage = show_usage -class Argument: +class Argument(ABC): def __init__(self, name: str, label: str = None, *, required: bool = False, - matches: Optional[str] = None, parser: Optional[Callable[[str], Any]] = None, pass_raw: bool = False) -> None: self.name = name - self.required = required self.label = label or name + self.required = required + self.pass_raw = pass_raw - if not parser: - if matches: - regex = re.compile(matches) - - def parser(val: str) -> Optional[Sequence[str]]: - match = regex.match(val) - return match.groups() if match else None - else: - def parser(val: str) -> str: - return val - - if not pass_raw: - o_parser = parser - - def parser(val: str) -> Any: - val = val.strip().split(" ") - return o_parser(val[0]) - - self.parser = parser - - def match(self, val: str) -> Any: - return self.parser(val) + @abstractmethod + def match(self, val: str) -> Tuple[str, Any]: + pass def __call__(self, func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: if not isinstance(func, CommandHandler): @@ -159,49 +207,108 @@ class Argument: return func -def new(name: PrefixType, *, help: str = None, event_type: EventType = EventType.ROOM_MESSAGE, - require_subcommand: bool = True) -> CommandHandlerDecorator: - def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: - if not isinstance(func, CommandHandler): - func = CommandHandler(func) - func.__mb_help__ = help - func.__mb_name__ = name or func.__name__ - func.__mb_require_subcommand__ = require_subcommand - func.__mb_prefix__ = f"!{func.__mb_name__}" - func.__mb_event_type__ = event_type - return func +class RegexArgument(Argument): + def __init__(self, name: str, label: str = None, *, required: bool = False, + pass_raw: bool = False, matches: str = None) -> None: + super().__init__(name, label, required=required, pass_raw=pass_raw) + matches = f"^{matches}" if self.pass_raw else f"^{matches}$" + self.regex = re.compile(matches) - return decorator + def match(self, val: str) -> Tuple[str, Any]: + orig_val = val + if not self.pass_raw: + val = val.split(" ")[0] + match = self.regex.match(val) + if match: + return (orig_val[:match.pos] + orig_val[match.endpos:], + match.groups() or val[match.pos:match.endpos]) + return orig_val, None + + +class CustomArgument(Argument): + def __init__(self, name: str, label: str = None, *, required: bool = False, + pass_raw: bool = False, matcher: Callable[[str], Any]) -> None: + super().__init__(name, label, required=required, pass_raw=pass_raw) + self.matcher = matcher + + def match(self, val: str) -> Tuple[str, Any]: + if self.pass_raw: + return self.matcher(val) + orig_val = val + val = val.split(" ")[0] + res = self.matcher(val) + if res: + return orig_val[len(val):], res + return orig_val, None + + +class SimpleArgument(Argument): + def match(self, val: str) -> Tuple[str, Any]: + if self.pass_raw: + return "", val + res = val.split(" ")[0] + return val[len(res):], res def argument(name: str, label: str = None, *, required: bool = True, matches: Optional[str] = None, - parser: Optional[Callable[[str], Any]] = None) -> CommandHandlerDecorator: - return Argument(name, label, required=required, matches=matches, parser=parser) + parser: Optional[Callable[[str], Any]] = None, pass_raw: bool = False + ) -> CommandHandlerDecorator: + if matches: + return RegexArgument(name, label, required=required, matches=matches, pass_raw=pass_raw) + elif parser: + return CustomArgument(name, label, required=required, matcher=parser, pass_raw=pass_raw) + else: + return SimpleArgument(name, label, required=required, pass_raw=pass_raw) -def passive(regex: Union[str, Pattern], msgtypes: Sequence[MessageType] = (MessageType.TEXT,), - field: Callable[[MaubotMessageEvent], str] = lambda event: event.content.body, - event_type: EventType = EventType.ROOM_MESSAGE) -> PassiveCommandHandlerDecorator: +def passive(regex: Union[str, Pattern], *, msgtypes: Sequence[MessageType] = (MessageType.TEXT,), + field: Callable[[MaubotMessageEvent], str] = lambda evt: evt.content.body, + event_type: EventType = EventType.ROOM_MESSAGE, multiple: bool = False + ) -> PassiveCommandHandlerDecorator: if not isinstance(regex, Pattern): regex = re.compile(regex) def decorator(func: CommandHandlerFunc) -> CommandHandlerFunc: + combine = None + if hasattr(func, "__mb_passive_orig__"): + combine = func + func = func.__mb_passive_orig__ + @event.on(event_type) @functools.wraps(func) - async def replacement(self, evt: MaubotMessageEvent) -> None: - if isinstance(self, MaubotMessageEvent): + async def replacement(self, evt: MaubotMessageEvent = None) -> None: + if not evt and isinstance(self, MaubotMessageEvent): evt = self self = None if evt.sender == evt.client.mxid: return elif msgtypes and evt.content.msgtype not in msgtypes: return - match = regex.match(field(evt)) - if match: - if self: - await func(self, evt, *list(match.groups())) + data = field(evt) + if multiple: + val = [(data[match.pos:match.endpos], *match.groups()) + for match in regex.finditer(data)] + else: + match = regex.match(data) + if match: + val = (data[match.pos:match.endpos], *match.groups()) else: - await func(evt, *list(match.groups())) + val = None + if val: + if self: + await func(self, evt, val) + else: + await func(evt, val) + + if combine: + orig_replacement = replacement + + @event.on(event_type) + @functools.wraps(func) + async def replacement(self, evt: MaubotMessageEvent = None) -> None: + await asyncio.gather(combine(self, evt), orig_replacement(self, evt)) + + replacement.__mb_passive_orig__ = func return replacement From 88c2f9d4639fbcd72bf049d2225a04d97a11d9dd Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 26 Dec 2018 18:38:56 +0200 Subject: [PATCH 5/5] Add support for command aliases and dynamic command names --- maubot/handlers/command.py | 98 +++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 27 deletions(-) diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index f850d12..28f3c78 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -14,10 +14,11 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from typing import (Union, Callable, Sequence, Pattern, Awaitable, NewType, Optional, Any, List, - Dict, Tuple) + Dict, Tuple, Set) from abc import ABC, abstractmethod import asyncio import functools +import inspect import re from mautrix.types import MessageType, EventType @@ -26,6 +27,7 @@ from ..matrix import MaubotMessageEvent from . import event PrefixType = Optional[Union[str, Callable[[], str]]] +AliasesType = Union[List[str], Tuple[str, ...], Set[str], Callable[[str], bool]] CommandHandlerFunc = NewType("CommandHandlerFunc", Callable[[MaubotMessageEvent, Any], Awaitable[Any]]) CommandHandlerDecorator = NewType("CommandHandlerDecorator", @@ -35,28 +37,40 @@ PassiveCommandHandlerDecorator = NewType("PassiveCommandHandlerDecorator", Callable[[CommandHandlerFunc], CommandHandlerFunc]) +def _split_in_two(val: str, split_by: str) -> List[str]: + return val.split(split_by, 1) if split_by in val else [val, ""] + + class CommandHandler: def __init__(self, func: CommandHandlerFunc) -> None: self.__mb_func__: CommandHandlerFunc = func - self.__mb_subcommands__: Dict[str, CommandHandler] = {} + self.__mb_parent__: CommandHandler = None + self.__mb_subcommands__: List[CommandHandler] = [] self.__mb_arguments__: List[Argument] = [] self.__mb_help__: str = None - self.__mb_name__: str = None - self.__mb_prefix__: str = None + self.__mb_get_name__: Callable[[], str] = None + self.__mb_is_command_match__: Callable[[Any, str], bool] = self.__command_match_unset self.__mb_require_subcommand__: bool = True self.__mb_arg_fallthrough__: bool = True self.__mb_event_handler__: bool = True self.__mb_event_type__: EventType = EventType.ROOM_MESSAGE self.__class_instance: Any = None + @staticmethod + def __command_match_unset(self, val: str) -> str: + raise NotImplementedError("Hmm") + async def __call__(self, evt: MaubotMessageEvent, *, _existing_args: Dict[str, Any] = None, - _remaining_val: str = None) -> Any: - body = evt.content.body - has_prefix = _remaining_val or body.startswith(self.__mb_prefix__) - if evt.sender == evt.client.mxid or not has_prefix: + remaining_val: str = None) -> Any: + if evt.sender == evt.client.mxid: return + if remaining_val is None: + if not evt.content.body or evt.content.body[0] != "!": + return + command, remaining_val = _split_in_two(evt.content.body[1:], " ") + if not self.__mb_is_command_match__(self, command): + return call_args: Dict[str, Any] = {**_existing_args} if _existing_args else {} - remaining_val = _remaining_val or body[len(self.__mb_prefix__) + 1:] if not self.__mb_arg_fallthrough__ and len(self.__mb_subcommands__) > 0: ok, res = await self.__call_subcommand__(evt, call_args, remaining_val) @@ -80,14 +94,12 @@ class CommandHandler: async def __call_subcommand__(self, evt: MaubotMessageEvent, call_args: Dict[str, Any], remaining_val: str) -> Tuple[bool, Any]: - remaining_val = remaining_val.strip() - split = remaining_val.split(" ") if len(remaining_val) > 0 else [] - try: - subcommand = self.__mb_subcommands__[split[0]] - return True, await subcommand(evt, _existing_args=call_args, - _remaining_val=" ".join(split[1:])) - except (KeyError, IndexError): - return False, None + command, remaining_val = _split_in_two(remaining_val.strip(), " ") + for subcommand in self.__mb_subcommands__: + if subcommand.__mb_is_command_match__(subcommand.__class_instance, command): + return True, await subcommand(evt, _existing_args=call_args, + remaining_val=remaining_val) + return False, None async def __parse_args__(self, evt: MaubotMessageEvent, call_args: Dict[str, Any], remaining_val: str) -> Tuple[bool, str]: @@ -111,7 +123,7 @@ class CommandHandler: @property def __mb_full_help__(self) -> str: usage = self.__mb_usage_without_subcommands__ + "\n\n" - usage += "\n".join(cmd.__mb_usage_inline__ for cmd in self.__mb_subcommands__.values()) + usage += "\n".join(cmd.__mb_usage_inline__ for cmd in self.__mb_subcommands__) return usage @property @@ -126,6 +138,16 @@ class CommandHandler: def __mb_usage_subcommand__(self) -> str: return f" [...]" + @property + def __mb_name__(self) -> str: + return self.__mb_get_name__(self.__class_instance) + + @property + def __mb_prefix__(self) -> str: + if self.__mb_parent__: + return f"{self.__mb_parent__.__mb_prefix__} {self.__mb_name__}" + return f"!{self.__mb_name__}" + @property def __mb_usage_inline__(self) -> str: if not self.__mb_arg_fallthrough__: @@ -150,31 +172,53 @@ class CommandHandler: return f"{self.__mb_usage_without_subcommands__} \n{self.__mb_subcommands_list__}" return self.__mb_usage_without_subcommands__ - def subcommand(self, name: PrefixType = None, help: str = None + def subcommand(self, name: PrefixType = None, *, help: str = None, aliases: AliasesType = None, + required_subcommand: bool = True, arg_fallthrough: bool = True, ) -> CommandHandlerDecorator: def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: if not isinstance(func, CommandHandler): func = CommandHandler(func) - func.__mb_name__ = name or func.__name__ - func.__mb_prefix__ = f"{self.__mb_prefix__} {func.__mb_name__}" - func.__mb_help__ = help + new(name, help=help, aliases=aliases, require_subcommand=required_subcommand, + arg_fallthrough=arg_fallthrough)(func) + func.__mb_parent__ = self func.__mb_event_handler__ = False - self.__mb_subcommands__[func.__mb_name__] = func + self.__mb_subcommands__.append(func) return func return decorator -def new(name: PrefixType, *, help: str = None, event_type: EventType = EventType.ROOM_MESSAGE, - require_subcommand: bool = True, arg_fallthrough: bool = True) -> CommandHandlerDecorator: +def new(name: PrefixType = None, *, help: str = None, aliases: AliasesType = None, + event_type: EventType = EventType.ROOM_MESSAGE, require_subcommand: bool = True, + arg_fallthrough: bool = True) -> CommandHandlerDecorator: def decorator(func: Union[CommandHandler, CommandHandlerFunc]) -> CommandHandler: if not isinstance(func, CommandHandler): func = CommandHandler(func) func.__mb_help__ = help - func.__mb_name__ = name or func.__name__ + if name: + if callable(name): + if len(inspect.getfullargspec(name).args) == 0: + func.__mb_get_name__ = lambda self: name() + else: + func.__mb_get_name__ = name + else: + func.__mb_get_name__ = lambda self: name + else: + func.__mb_get_name__ = lambda self: func.__name__ + if callable(aliases): + if len(inspect.getfullargspec(aliases).args) == 1: + func.__mb_is_command_match__ = lambda self, val: aliases(val) + else: + func.__mb_is_command_match__ = aliases + elif isinstance(aliases, (list, set, tuple)): + func.__mb_is_command_match__ = lambda self, val: (val == func.__mb_name__ + or val in aliases) + else: + func.__mb_is_command_match__ = lambda self, val: val == func.__mb_name__ + # Decorators are executed last to first, so we reverse the argument list. + func.__mb_arguments__.reverse() func.__mb_require_subcommand__ = require_subcommand func.__mb_arg_fallthrough__ = arg_fallthrough - func.__mb_prefix__ = f"!{func.__mb_name__}" func.__mb_event_type__ = event_type return func