Skip to content

Commit

Permalink
Merge pull request #191 from aahnik/add-regex-support
Browse files Browse the repository at this point in the history
Add regex support for text filtering and replacement
  • Loading branch information
aahnik committed Jun 4, 2021
2 parents a5d852f + d07e570 commit 360963f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tgcf.config.yml
foo.py
bar.py
foo.bar
t.ipynb



Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <daw@aahnik.dev>"]
license = "MIT"
Expand Down
16 changes: 11 additions & 5 deletions tgcf/plugins/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -18,6 +19,7 @@ class FilesFilterList(BaseModel):

class TextFilter(FilterList):
case_sensitive: bool = False
regex: bool = False


class Filters(BaseModel):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tgcf/plugins/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
13 changes: 13 additions & 0 deletions tgcf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 360963f

Please sign in to comment.