fix remove_batv and lazy-load config in CLI
This commit is contained in:
@@ -20,7 +20,7 @@ import logging.handlers
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from pyquarantine.config import get_milter_config
|
from pyquarantine.config import get_milter_config, ActionConfig
|
||||||
from pyquarantine.storage import Quarantine
|
from pyquarantine.storage import Quarantine
|
||||||
from pyquarantine import __version__ as version
|
from pyquarantine import __version__ as version
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ def _get_quarantine(quarantines, name, debug):
|
|||||||
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(quarantine, [], debug)
|
return Quarantine(ActionConfig(quarantine), [], debug)
|
||||||
|
|
||||||
|
|
||||||
def _get_notification(quarantines, name, debug):
|
def _get_notification(quarantines, name, debug):
|
||||||
@@ -105,7 +105,7 @@ def list_quarantines(quarantines, args):
|
|||||||
notification_type = "NONE"
|
notification_type = "NONE"
|
||||||
|
|
||||||
if "whitelist" in cfg:
|
if "whitelist" in cfg:
|
||||||
whitelist_type = cfg["whitelist"]["whitelist"]["type"]
|
whitelist_type = cfg["whitelist"]["type"]
|
||||||
else:
|
else:
|
||||||
whitelist_type = "NONE"
|
whitelist_type = "NONE"
|
||||||
|
|
||||||
@@ -567,12 +567,12 @@ def main():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
logger.debug("read milter configuration")
|
logger.debug("read milter configuration")
|
||||||
cfg = get_milter_config(args.config)
|
cfg = get_milter_config(args.config, raw=True)
|
||||||
if not cfg["rules"]:
|
if "rules" not in cfg or not cfg["rules"]:
|
||||||
raise RuntimeError("no rules configured")
|
raise RuntimeError("no rules configured")
|
||||||
|
|
||||||
for rule in cfg["rules"]:
|
for rule in cfg["rules"]:
|
||||||
if not rule["actions"]:
|
if "actions" not in rule or not rule["actions"]:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"{rule['name']}: no actions configured")
|
f"{rule['name']}: no actions configured")
|
||||||
except (RuntimeError, AssertionError) as e:
|
except (RuntimeError, AssertionError) as e:
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ class MilterConfig(BaseConfig):
|
|||||||
self["rules"] = rules
|
self["rules"] = rules
|
||||||
|
|
||||||
|
|
||||||
def get_milter_config(cfgfile):
|
def get_milter_config(cfgfile, raw=False):
|
||||||
try:
|
try:
|
||||||
with open(cfgfile, "r") as fh:
|
with open(cfgfile, "r") as fh:
|
||||||
# remove lines with leading # (comments), they
|
# remove lines with leading # (comments), they
|
||||||
@@ -360,5 +360,6 @@ 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}")
|
||||||
|
if raw:
|
||||||
|
return cfg
|
||||||
return MilterConfig(cfg)
|
return MilterConfig(cfg)
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class WhitelistBase:
|
|||||||
return "Base"
|
return "Base"
|
||||||
|
|
||||||
def remove_batv(self, addr):
|
def remove_batv(self, addr):
|
||||||
return self.batv_regex.sub(r"\g<LEFT_PART>", addr, count=1)
|
return self.batv_regex.sub(r"\g<LEFT_PART>@", addr, count=1)
|
||||||
|
|
||||||
def check(self, mailfrom, recipient):
|
def check(self, mailfrom, recipient):
|
||||||
"Check if mailfrom/recipient combination is whitelisted."
|
"Check if mailfrom/recipient combination is whitelisted."
|
||||||
|
|||||||
Reference in New Issue
Block a user