Skip to content

Instantly share code, notes, and snippets.

@apeyroux
Last active November 21, 2022 21:45
Show Gist options
  • Save apeyroux/bd4dbeca3b6dfa9fee7aae945811be78 to your computer and use it in GitHub Desktop.
Save apeyroux/bd4dbeca3b6dfa9fee7aae945811be78 to your computer and use it in GitHub Desktop.
from typing import Union
import re
class Reject(object):
def __init__(self, reason: str):
self.reason = reason
def run(self):
print("[REJECT] Mail reject: {}".format(self.reason))
class Drop(object):
def run(self):
print("[DROP] Mail dropped")
class Filter:
def __init__(self, pattern: str):
self.pattern = pattern
def __call__(self, string: str) -> bool:
result = False
if re.match(self.pattern, string):
result = True
print("[DEBUG_FILTER] match {} with {} == {}".format(self.pattern, string, result))
return result
class Or:
def __init__(self, action: Union[Reject, Drop], *args):
self.args = args
self.action = action
def __call__(self, action: Union[Reject, Drop], *args) -> Union[Reject, Drop, None]:
if any(arg(action, *args) for arg in self.args):
return self.action
else:
return None
class And:
def __init__(self, action: Union[Reject, Drop], *args):
self.args = args
self.action = action
def __call__(self, action: Union[Reject, Drop], *args) -> Union[Reject, Drop, None]:
if all(arg(action, *args) for arg in self.args):
return self.action
else:
return None
if __name__ == "__main__":
#
# Liste de filtres
#
# And : tous les filtres doivent être validés
# Or : au moins un filtre doit être validé
#
# Or ou And prend en paramètre une fonction et une liste de filtres
#
filters = [
# Marche
And(Reject("[ACTION] OK car 2x From (AND) !"), Filter(r"^From:.*$"), Filter(r"^From:.*$")),
# Ne marche pas
And(Reject("[ACTION] Ne devrait pas passer ici. 1 bad dans un and"), Filter(r"\d+"), Filter(r"\w"), Filter(r".*@.*"), Filter(r".*@.*")),
# Marche
Or(lambda: print("[ACTION] Oups une lambda pour montrer qu'une action est n'importe quoi !"), Filter(r"^From:.*$"), Filter(r".*@.*")),
Or(Drop(), Filter(r"^From:.*$"), Filter(r"^From:.*$")),
Or(Reject("[ACTION] Ok all bad"), Filter(r"\d+"), Filter(r"\d+")),
]
for filter in filters:
action = filter("From:alex@px.io")
if action and "run" in dir(action):
action.run()
elif action:
action()
print("====== AUTRE EXEMPLE ======")
actionOk = Or(Reject("[ACTION] YES"), Filter("^From:.*$"), Filter(".*@.*"))("From:alex@px.io")
if actionOk:
print("Action OK")
actionOk.run()
actionKo = And(Drop(), Filter("^From:.*$"), Filter("\d"))("From:alex@px.io")
if actionKo:
print("Action KO")
actionKo.run()
@apeyroux
Copy link
Author

apeyroux commented Nov 21, 2022

Ce qui donne:

[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
[REJECT] Mail reject: [ACTION] OK car 2x From (AND) !
[DEBUG_FILTER] match \d+ with From:alex@px.io == False
[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
[ACTION] Oups une lambda pour montrer qu'une action est n'importe quoi !
[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
[DROP] Mail dropped
[DEBUG_FILTER] match \d+ with From:alex@px.io == False
[DEBUG_FILTER] match \d+ with From:alex@px.io == False
====== AUTRE EXEMPLE ======
[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
Action OK
[REJECT] Mail reject: [ACTION] YES
[DEBUG_FILTER] match ^From:.*$ with From:alex@px.io == True
[DEBUG_FILTER] match \d with From:alex@px.io == False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment