diff --git a/.gitignore b/.gitignore index 6fb26929..259e1f8b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ tgcf.config.yml foo.py bar.py foo.bar +t.ipynb diff --git a/pyproject.toml b/pyproject.toml index 4b38462e..2eae764e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tgcf" -version = "0.2.4" +version = "0.2.5" description = "The ultimate tool to automate custom telegram message forwarding." authors = ["aahnik "] license = "MIT" diff --git a/tgcf/plugins/filter.py b/tgcf/plugins/filter.py index baadcf75..1bf70b5e 100644 --- a/tgcf/plugins/filter.py +++ b/tgcf/plugins/filter.py @@ -4,6 +4,7 @@ from pydantic import BaseModel # pylint: disable=no-name-in-module from tgcf.plugins import FileType, TgcfMessage, TgcfPlugin +from tgcf.utils import match class FilterList(BaseModel): @@ -18,6 +19,7 @@ class FilesFilterList(BaseModel): class TextFilter(FilterList): case_sensitive: bool = False + regex: bool = False class Filters(BaseModel): @@ -58,14 +60,18 @@ def text_safe(self, tm: TgcfMessage) -> bool: if not text and flist.whitelist == []: return True + # first check if any blacklisted pattern is present for forbidden in flist.blacklist: - if forbidden in text: - return False + if match(forbidden, text, self.filters.text.regex): + return False # when a forbidden pattern is found + if not flist.whitelist: - return True + return True # if no whitelist is present + + # if whitelist is present for allowed in flist.whitelist: - if allowed in text: - return True + if match(allowed, text, self.filters.text.regex): + return True # only when atleast one whitelisted pattern is found def users_safe(self, tm: TgcfMessage) -> bool: flist = self.filters.users diff --git a/tgcf/plugins/replace.py b/tgcf/plugins/replace.py index 71bee1c5..b6e9fa66 100644 --- a/tgcf/plugins/replace.py +++ b/tgcf/plugins/replace.py @@ -4,10 +4,12 @@ from pydantic import BaseModel # pylint: disable=no-name-in-module from tgcf.plugins import TgcfMessage, TgcfPlugin +from tgcf.utils import replace class Replace(BaseModel): text: Dict[str, str] = {} + regex: bool = False class TgcfReplace(TgcfPlugin): @@ -22,6 +24,6 @@ def modify(self, tm: TgcfMessage) -> TgcfMessage: if not msg_text: return tm for original, new in self.replace.text.items(): - msg_text = msg_text.replace(original, new) + msg_text = replace(original, new, msg_text, self.replace.regex) tm.text = msg_text return tm diff --git a/tgcf/utils.py b/tgcf/utils.py index 8794cb72..b7a5ae55 100644 --- a/tgcf/utils.py +++ b/tgcf/utils.py @@ -56,3 +56,16 @@ def safe_name(string: str) -> str: Certain characters in the file name can cause potential problems in rare scenarios. """ return re.sub(pattern=r"[-!@#$%^&*()\s]", repl="_", string=string) + + +def match(pattern: str, string: str, regex: bool) -> bool: + if regex: + return bool(re.match(pattern, string)) + return pattern in string + + +def replace(pattern: str, new: str, string: str, regex: bool) -> str: + if regex: + return re.sub(pattern, new, string) + else: + return string.replace(pattern, new)