lazy load quarantine objects in cli
This commit is contained in:
@@ -25,24 +25,24 @@ from pyquarantine.storage import Quarantine
|
|||||||
from pyquarantine import __version__ as version
|
from pyquarantine import __version__ as version
|
||||||
|
|
||||||
|
|
||||||
def _get_quarantine(quarantines, name):
|
def _get_quarantine(quarantines, name, debug):
|
||||||
try:
|
try:
|
||||||
quarantine = next((q for q in quarantines if q.name == name))
|
quarantine = next((q for q in quarantines if q["name"] == name))
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
raise RuntimeError(f"invalid quarantine '{name}'")
|
raise RuntimeError(f"invalid quarantine '{name}'")
|
||||||
return quarantine
|
return Quarantine(quarantine, [], debug)
|
||||||
|
|
||||||
|
|
||||||
def _get_notification(quarantines, name):
|
def _get_notification(quarantines, name, debug):
|
||||||
notification = _get_quarantine(quarantines, name).notification
|
notification = _get_quarantine(quarantines, name, debug).notification
|
||||||
if not notification:
|
if not notification:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"notification type is set to NONE")
|
"notification type is set to NONE")
|
||||||
return notification
|
return notification
|
||||||
|
|
||||||
|
|
||||||
def _get_whitelist(quarantines, name):
|
def _get_whitelist(quarantines, name, debug):
|
||||||
whitelist = _get_quarantine(quarantines, name).whitelist
|
whitelist = _get_quarantine(quarantines, name, debug).whitelist
|
||||||
if not whitelist:
|
if not whitelist:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"whitelist type is set to NONE")
|
"whitelist type is set to NONE")
|
||||||
@@ -96,24 +96,26 @@ def list_quarantines(quarantines, args):
|
|||||||
else:
|
else:
|
||||||
qlist = []
|
qlist = []
|
||||||
for q in quarantines:
|
for q in quarantines:
|
||||||
storage_type = q.storage.type
|
cfg = q["args"]
|
||||||
|
storage_type = cfg["store"]["type"]
|
||||||
|
|
||||||
if q.notification:
|
if "notify" in cfg:
|
||||||
notification_type = q.notification.type
|
notification_type = cfg["notify"]["type"]
|
||||||
else:
|
else:
|
||||||
notification_type = "NONE"
|
notification_type = "NONE"
|
||||||
|
|
||||||
if q.whitelist:
|
if "whitelist" in cfg:
|
||||||
whitelist_type = q.whitelist.type
|
whitelist_type = cfg["whitelist"]["whitelist"]["type"]
|
||||||
else:
|
else:
|
||||||
whitelist_type = "NONE"
|
whitelist_type = "NONE"
|
||||||
|
|
||||||
qlist.append({
|
qlist.append({
|
||||||
"name": q.name,
|
"name": q["name"],
|
||||||
"storage": storage_type,
|
"storage": storage_type,
|
||||||
"notification": notification_type,
|
"notification": notification_type,
|
||||||
"whitelist": whitelist_type,
|
"whitelist": whitelist_type,
|
||||||
"action": q.milter_action})
|
"action": q["args"]["milter_action"]})
|
||||||
|
|
||||||
print_table(
|
print_table(
|
||||||
[("Name", "name"),
|
[("Name", "name"),
|
||||||
("Storage", "storage"),
|
("Storage", "storage"),
|
||||||
@@ -126,7 +128,7 @@ def list_quarantines(quarantines, args):
|
|||||||
|
|
||||||
def list_quarantine_emails(quarantines, args):
|
def list_quarantine_emails(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
storage = _get_quarantine(quarantines, args.quarantine).storage
|
storage = _get_quarantine(quarantines, args.quarantine, args.debug).storage
|
||||||
|
|
||||||
# find emails and transform some metadata values to strings
|
# find emails and transform some metadata values to strings
|
||||||
rows = []
|
rows = []
|
||||||
@@ -173,7 +175,7 @@ def list_quarantine_emails(quarantines, args):
|
|||||||
|
|
||||||
def list_whitelist(quarantines, args):
|
def list_whitelist(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
whitelist = _get_whitelist(quarantines, args.quarantine)
|
whitelist = _get_whitelist(quarantines, args.quarantine, args.debug)
|
||||||
|
|
||||||
# find whitelist entries
|
# find whitelist entries
|
||||||
entries = whitelist.find(
|
entries = whitelist.find(
|
||||||
@@ -205,7 +207,7 @@ def list_whitelist(quarantines, args):
|
|||||||
|
|
||||||
def add_whitelist_entry(quarantines, args):
|
def add_whitelist_entry(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
whitelist = _get_whitelist(quarantines, args.quarantine)
|
whitelist = _get_whitelist(quarantines, args.quarantine, args.debug)
|
||||||
|
|
||||||
# check existing entries
|
# check existing entries
|
||||||
entries = whitelist.check(args.mailfrom, args.recipient, logger)
|
entries = whitelist.check(args.mailfrom, args.recipient, logger)
|
||||||
@@ -245,34 +247,34 @@ def add_whitelist_entry(quarantines, args):
|
|||||||
|
|
||||||
def delete_whitelist_entry(quarantines, args):
|
def delete_whitelist_entry(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
whitelist = _get_whitelist(quarantines, args.quarantine)
|
whitelist = _get_whitelist(quarantines, args.quarantine, args.debug)
|
||||||
whitelist.delete(args.whitelist_id)
|
whitelist.delete(args.whitelist_id)
|
||||||
logger.info("whitelist entry deleted successfully")
|
logger.info("whitelist entry deleted successfully")
|
||||||
|
|
||||||
|
|
||||||
def notify(quarantines, args):
|
def notify(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
quarantine = _get_quarantine(quarantines, args.quarantine)
|
quarantine = _get_quarantine(quarantines, args.quarantine, args.debug)
|
||||||
quarantine.notify(args.quarantine_id, args.recipient)
|
quarantine.notify(args.quarantine_id, args.recipient)
|
||||||
logger.info("notification sent successfully")
|
logger.info("notification sent successfully")
|
||||||
|
|
||||||
|
|
||||||
def release(quarantines, args):
|
def release(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
quarantine = _get_quarantine(quarantines, args.quarantine)
|
quarantine = _get_quarantine(quarantines, args.quarantine, args.debug)
|
||||||
quarantine.release(args.quarantine_id, args.recipient)
|
quarantine.release(args.quarantine_id, args.recipient)
|
||||||
logger.info("quarantined email released successfully")
|
logger.info("quarantined email released successfully")
|
||||||
|
|
||||||
|
|
||||||
def delete(quarantines, args):
|
def delete(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
storage = _get_quarantine(quarantines, args.quarantine).storage
|
storage = _get_quarantine(quarantines, args.quarantine, args.debug).storage
|
||||||
storage.delete(args.quarantine_id, args.recipient)
|
storage.delete(args.quarantine_id, args.recipient)
|
||||||
logger.info("quarantined email deleted successfully")
|
logger.info("quarantined email deleted successfully")
|
||||||
|
|
||||||
|
|
||||||
def get(quarantines, args):
|
def get(quarantines, args):
|
||||||
storage = _get_quarantine(quarantines, args.quarantine).storage
|
storage = _get_quarantine(quarantines, args.quarantine, args.debug).storage
|
||||||
_, msg = storage.get_mail(args.quarantine_id)
|
_, msg = storage.get_mail(args.quarantine_id)
|
||||||
print(msg.as_string())
|
print(msg.as_string())
|
||||||
|
|
||||||
@@ -581,8 +583,7 @@ def main():
|
|||||||
for rule in cfg["rules"]:
|
for rule in cfg["rules"]:
|
||||||
for action in rule["actions"]:
|
for action in rule["actions"]:
|
||||||
if action["type"] == "quarantine":
|
if action["type"] == "quarantine":
|
||||||
quarantines.append(
|
quarantines.append(action)
|
||||||
Quarantine(action, [], args.debug))
|
|
||||||
|
|
||||||
if args.syslog:
|
if args.syslog:
|
||||||
# setup syslog
|
# setup syslog
|
||||||
|
|||||||
@@ -360,5 +360,5 @@ def get_milter_config(cfgfile):
|
|||||||
cfg_text = [f"{n+1}: {l}" for n, l in enumerate(cfg.splitlines())]
|
cfg_text = [f"{n+1}: {l}" for n, l in enumerate(cfg.splitlines())]
|
||||||
msg = "\n".join(cfg_text)
|
msg = "\n".join(cfg_text)
|
||||||
raise RuntimeError(f"{e}\n{msg}")
|
raise RuntimeError(f"{e}\n{msg}")
|
||||||
|
|
||||||
return MilterConfig(cfg)
|
return MilterConfig(cfg)
|
||||||
|
|||||||
@@ -41,10 +41,6 @@ class BaseNotification:
|
|||||||
def __init__(self, pretend=False):
|
def __init__(self, pretend=False):
|
||||||
self.pretend = pretend
|
self.pretend = pretend
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
return "Base"
|
|
||||||
|
|
||||||
def execute(self, milter, logger):
|
def execute(self, milter, logger):
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -147,10 +143,6 @@ class EMailNotification(BaseNotification):
|
|||||||
|
|
||||||
self.parser_lib = parser_lib
|
self.parser_lib = parser_lib
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
return "E-Mail"
|
|
||||||
|
|
||||||
def get_email_body_soup(self, msg, logger):
|
def get_email_body_soup(self, msg, logger):
|
||||||
"Extract and decode email body and return it as BeautifulSoup object."
|
"Extract and decode email body and return it as BeautifulSoup object."
|
||||||
# try to find the body part
|
# try to find the body part
|
||||||
|
|||||||
@@ -47,10 +47,6 @@ class BaseMailStorage:
|
|||||||
self.metavar = metavar
|
self.metavar = metavar
|
||||||
self.pretend = False
|
self.pretend = False
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
return "Base"
|
|
||||||
|
|
||||||
def add(self, data, qid, mailfrom, recipients, subject, variables):
|
def add(self, data, qid, mailfrom, recipients, subject, variables):
|
||||||
"Add email to storage."
|
"Add email to storage."
|
||||||
return ("", "")
|
return ("", "")
|
||||||
@@ -107,10 +103,6 @@ class FileMailStorage(BaseMailStorage):
|
|||||||
cfg.append(f"original={self.original}")
|
cfg.append(f"original={self.original}")
|
||||||
return "FileMailStorage(" + ", ".join(cfg) + ")"
|
return "FileMailStorage(" + ", ".join(cfg) + ")"
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
return "File"
|
|
||||||
|
|
||||||
def get_storageid(self, qid):
|
def get_storageid(self, qid):
|
||||||
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||||
return f"{timestamp}_{qid}"
|
return f"{timestamp}_{qid}"
|
||||||
|
|||||||
@@ -141,10 +141,6 @@ class DatabaseWhitelist(WhitelistBase):
|
|||||||
cfg.append(f"{arg}={self.cfg[arg]}")
|
cfg.append(f"{arg}={self.cfg[arg]}")
|
||||||
return "DatabaseWhitelist(" + ", ".join(cfg) + ")"
|
return "DatabaseWhitelist(" + ", ".join(cfg) + ")"
|
||||||
|
|
||||||
@property
|
|
||||||
def type(self):
|
|
||||||
return "DB"
|
|
||||||
|
|
||||||
def _entry_to_dict(self, entry):
|
def _entry_to_dict(self, entry):
|
||||||
result = {}
|
result = {}
|
||||||
result[entry.id] = {
|
result[entry.id] = {
|
||||||
|
|||||||
Reference in New Issue
Block a user