2019-06-23 10:43:10 +00:00
|
|
|
# reminder - A maubot plugin that reacts to messages that match predefined rules.
|
2021-07-28 17:21:44 +00:00
|
|
|
# Copyright (C) 2021 Tulir Asokan
|
2019-06-23 10:43:10 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
2023-10-05 19:22:10 +00:00
|
|
|
from typing import Callable, Dict, List, NamedTuple, Optional
|
2019-06-23 10:43:10 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
class SimpleMatch(NamedTuple):
|
|
|
|
value: str
|
2019-06-23 10:43:10 +00:00
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def groups(self) -> List[str]:
|
|
|
|
return [self.value]
|
2021-07-21 12:09:29 +00:00
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def group(self, group: int) -> Optional[str]:
|
|
|
|
if group == 0:
|
|
|
|
return self.value
|
|
|
|
return None
|
|
|
|
|
|
|
|
def groupdict(self) -> Dict[str, str]:
|
2020-12-11 17:59:19 +00:00
|
|
|
return {}
|
|
|
|
|
2019-06-23 10:43:10 +00:00
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def matcher_equals(val: str, pattern: str) -> bool:
|
|
|
|
return val == pattern
|
|
|
|
|
|
|
|
|
|
|
|
def matcher_startswith(val: str, pattern: str) -> bool:
|
|
|
|
return val.startswith(pattern)
|
|
|
|
|
2019-06-23 10:43:10 +00:00
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def matcher_endswith(val: str, pattern: str) -> bool:
|
|
|
|
return val.endswith(pattern)
|
|
|
|
|
|
|
|
|
|
|
|
def matcher_contains(val: str, pattern: str) -> bool:
|
|
|
|
return pattern in val
|
|
|
|
|
|
|
|
|
|
|
|
SimpleMatcherFunc = Callable[[str, str], bool]
|
|
|
|
|
|
|
|
|
|
|
|
class SimplePattern:
|
|
|
|
matcher: SimpleMatcherFunc
|
|
|
|
pattern: str
|
2019-06-23 10:43:10 +00:00
|
|
|
ignorecase: bool
|
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def __init__(self, matcher: SimpleMatcherFunc, pattern: str, ignorecase: bool) -> None:
|
2019-06-23 10:43:10 +00:00
|
|
|
self.matcher = matcher
|
2021-07-28 17:21:44 +00:00
|
|
|
self.pattern = pattern
|
2019-06-23 10:43:10 +00:00
|
|
|
self.ignorecase = ignorecase
|
|
|
|
|
2021-07-28 17:21:44 +00:00
|
|
|
def search(self, val: str) -> SimpleMatch:
|
2019-06-23 10:43:10 +00:00
|
|
|
if self.ignorecase:
|
|
|
|
val = val.lower()
|
2021-07-28 17:21:44 +00:00
|
|
|
if self.matcher(val, self.pattern):
|
|
|
|
return SimpleMatch(self.pattern)
|
2019-06-23 10:43:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2023-10-05 19:22:10 +00:00
|
|
|
def compile(
|
|
|
|
pattern: str, flags: re.RegexFlag = re.RegexFlag(0), force_raw: bool = False
|
|
|
|
) -> Optional["SimplePattern"]:
|
2019-06-23 10:43:10 +00:00
|
|
|
ignorecase = flags == re.IGNORECASE
|
|
|
|
s_pattern = pattern.lower() if ignorecase else pattern
|
|
|
|
esc = ""
|
|
|
|
if not force_raw:
|
|
|
|
esc = re.escape(pattern)
|
|
|
|
first, last = pattern[0], pattern[-1]
|
2023-10-05 19:22:10 +00:00
|
|
|
if first == "^" and last == "$" and (force_raw or esc == f"\\^{pattern[1:-1]}\\$"):
|
2019-06-23 10:43:10 +00:00
|
|
|
s_pattern = s_pattern[1:-1]
|
2021-07-28 17:21:44 +00:00
|
|
|
func = matcher_equals
|
2023-10-05 19:22:10 +00:00
|
|
|
elif first == "^" and (force_raw or esc == f"\\^{pattern[1:]}"):
|
2019-06-23 10:43:10 +00:00
|
|
|
s_pattern = s_pattern[1:]
|
2021-07-28 17:21:44 +00:00
|
|
|
func = matcher_startswith
|
2023-10-05 19:22:10 +00:00
|
|
|
elif last == "$" and (force_raw or esc == f"{pattern[:-1]}\\$"):
|
2019-06-23 10:43:10 +00:00
|
|
|
s_pattern = s_pattern[:-1]
|
2021-07-28 17:21:44 +00:00
|
|
|
func = matcher_endswith
|
2019-06-23 10:43:10 +00:00
|
|
|
elif force_raw or esc == pattern:
|
2021-07-28 17:21:44 +00:00
|
|
|
func = matcher_contains
|
|
|
|
else:
|
|
|
|
# Not a simple pattern
|
|
|
|
return None
|
|
|
|
return SimplePattern(matcher=func, pattern=s_pattern, ignorecase=ignorecase)
|