Split into many files
This commit is contained in:
parent
44ae0529be
commit
c5c2590a96
7 changed files with 395 additions and 300 deletions
61
reactbot/rule.py
Normal file
61
reactbot/rule.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
# reminder - A maubot plugin that reacts to messages that match predefined rules.
|
||||
# Copyright (C) 2019 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 <https://www.gnu.org/licenses/>.
|
||||
from typing import Optional, Match, Dict, List, Set, Union, Pattern
|
||||
|
||||
from attr import dataclass
|
||||
from jinja2 import Template as JinjaTemplate
|
||||
|
||||
from mautrix.types import RoomID, EventType
|
||||
|
||||
from maubot import MessageEvent
|
||||
|
||||
from .template import Template
|
||||
from .simplepattern import SimplePattern
|
||||
|
||||
RPattern = Union[Pattern, SimplePattern]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Rule:
|
||||
rooms: Set[RoomID]
|
||||
matches: List[RPattern]
|
||||
not_matches: List[RPattern]
|
||||
template: Template
|
||||
type: Optional[EventType]
|
||||
variables: Dict[str, JinjaTemplate]
|
||||
|
||||
def _check_not_match(self, body: str) -> bool:
|
||||
for pattern in self.not_matches:
|
||||
if pattern.search(body):
|
||||
return True
|
||||
return False
|
||||
|
||||
def match(self, evt: MessageEvent) -> Optional[Match]:
|
||||
if len(self.rooms) > 0 and evt.room_id not in self.rooms:
|
||||
return None
|
||||
for pattern in self.matches:
|
||||
match = pattern.search(evt.content.body)
|
||||
if match:
|
||||
if self._check_not_match(evt.content.body):
|
||||
return None
|
||||
return match
|
||||
return None
|
||||
|
||||
async def execute(self, evt: MessageEvent, match: Match) -> None:
|
||||
content = self.template.execute(evt=evt, rule_vars=self.variables,
|
||||
extra_vars={str(i): val for i, val in
|
||||
enumerate(match.groups())})
|
||||
await evt.client.send_message_event(evt.room_id, self.type or self.template.type, content)
|
Loading…
Add table
Add a link
Reference in a new issue