Last active
November 21, 2022 21:45
-
-
Save apeyroux/bd4dbeca3b6dfa9fee7aae945811be78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:[email protected]") | |
if action and "run" in dir(action): | |
action.run() | |
elif action: | |
action() | |
print("====== AUTRE EXEMPLE ======") | |
actionOk = Or(Reject("[ACTION] YES"), Filter("^From:.*$"), Filter(".*@.*"))("From:[email protected]") | |
if actionOk: | |
print("Action OK") | |
actionOk.run() | |
actionKo = And(Drop(), Filter("^From:.*$"), Filter("\d"))("From:[email protected]") | |
if actionKo: | |
print("Action KO") | |
actionKo.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ce qui donne: