add condition on variables

This commit is contained in:
2021-09-19 18:47:02 +02:00
parent b91460b629
commit 46a7103900
2 changed files with 21 additions and 13 deletions

View File

@@ -41,7 +41,6 @@ class ActionConfig(BaseConfig):
self["pretend"] = rule_cfg["pretend"] self["pretend"] = rule_cfg["pretend"]
self["conditions"] = None self["conditions"] = None
self["type"] = ""
if "pretend" in cfg: if "pretend" in cfg:
pretend = cfg["pretend"] pretend = cfg["pretend"]
@@ -53,13 +52,12 @@ class ActionConfig(BaseConfig):
f"{self['name']}: mandatory parameter 'type' not found" f"{self['name']}: mandatory parameter 'type' not found"
assert isinstance(cfg["type"], str), \ assert isinstance(cfg["type"], str), \
f"{self['name']}: type: invalid value, should be string" f"{self['name']}: type: invalid value, should be string"
self["type"] = cfg["type"]
if self["type"] == "add_header": if cfg["type"] == "add_header":
self["class"] = modify.AddHeader self["class"] = modify.AddHeader
self["headersonly"] = True self["headersonly"] = True
self.add_string_arg(cfg, ["field", "value"]) self.add_string_arg(cfg, ["field", "value"])
elif self["type"] == "mod_header": elif cfg["type"] == "mod_header":
self["class"] = modify.ModHeader self["class"] = modify.ModHeader
self["headersonly"] = True self["headersonly"] = True
args = ["field", "value"] args = ["field", "value"]
@@ -67,7 +65,7 @@ class ActionConfig(BaseConfig):
args.append("search") args.append("search")
self.add_string_arg(cfg, args) self.add_string_arg(cfg, args)
elif self["type"] == "del_header": elif cfg["type"] == "del_header":
self["class"] = modify.DelHeader self["class"] = modify.DelHeader
self["headersonly"] = True self["headersonly"] = True
args = ["field"] args = ["field"]
@@ -75,7 +73,7 @@ class ActionConfig(BaseConfig):
args.append("value") args.append("value")
self.add_string_arg(cfg, args) self.add_string_arg(cfg, args)
elif self["type"] == "add_disclaimer": elif cfg["type"] == "add_disclaimer":
self["class"] = modify.AddDisclaimer self["class"] = modify.AddDisclaimer
self["headersonly"] = False self["headersonly"] = False
if "error_policy" not in cfg: if "error_policy" not in cfg:
@@ -93,17 +91,17 @@ class ActionConfig(BaseConfig):
f"{self['name']}: error_policy: invalid value, " \ f"{self['name']}: error_policy: invalid value, " \
f"should be 'wrap', 'ignore' or 'reject'" f"should be 'wrap', 'ignore' or 'reject'"
elif self["type"] == "rewrite_links": elif cfg["type"] == "rewrite_links":
self["class"] = modify.RewriteLinks self["class"] = modify.RewriteLinks
self["headersonly"] = False self["headersonly"] = False
self.add_string_arg(cfg, "repl") self.add_string_arg(cfg, "repl")
elif self["type"] == "store": elif cfg["type"] == "store":
self["headersonly"] = False self["headersonly"] = False
assert "storage_type" in cfg, \ assert "storage_type" in cfg, \
f"{self['name']}: mandatory parameter 'storage_type' not found" f"{self['name']}: mandatory parameter 'storage_type' not found"
assert isinstance(cfg["type"], str), \ assert isinstance(cfg["storage_type"], str), \
f"{self['name']}: storage_type: invalid value, " \ f"{self['name']}: storage_type: invalid value, " \
f"should be string" f"should be string"
self["storage_type"] = cfg["storage_type"] self["storage_type"] = cfg["storage_type"]
@@ -132,7 +130,7 @@ class ActionConfig(BaseConfig):
raise RuntimeError( raise RuntimeError(
f"{self['name']}: storage_type: invalid storage type") f"{self['name']}: storage_type: invalid storage type")
elif self["type"] == "notify": elif cfg["type"] == "notify":
self["headersonly"] = False self["headersonly"] = False
self["class"] = notify.EMailNotification self["class"] = notify.EMailNotification
@@ -166,7 +164,7 @@ class ActionConfig(BaseConfig):
self.logger.debug(f"{self['name']}: pretend={self['pretend']}, " self.logger.debug(f"{self['name']}: pretend={self['pretend']}, "
f"loglevel={self['loglevel']}, " f"loglevel={self['loglevel']}, "
f"type={self['type']}, " f"type={cfg['type']}, "
f"args={self['args']}") f"args={self['args']}")

View File

@@ -49,6 +49,9 @@ class ConditionsConfig(BaseConfig):
if "header" in cfg: if "header" in cfg:
self.add_string_arg(cfg, "header") self.add_string_arg(cfg, "header")
if "var" in cfg:
self.add_string_arg(cfg, "var")
if "metavar" in cfg: if "metavar" in cfg:
self.add_string_arg(cfg, "metavar") self.add_string_arg(cfg, "metavar")
@@ -64,7 +67,8 @@ class Conditions:
self._local_addrs = milter_cfg["local_addrs"] self._local_addrs = milter_cfg["local_addrs"]
self._name = cfg["name"] self._name = cfg["name"]
for arg in ("local", "hosts", "envfrom", "envto", "header", "metavar"): for arg in ("local", "hosts", "envfrom", "envto", "header", "metavar",
"var"):
value = cfg["args"][arg] if arg in cfg["args"] else None value = cfg["args"][arg] if arg in cfg["args"] else None
setattr(self, arg, value) setattr(self, arg, value)
if value is None: if value is None:
@@ -176,8 +180,10 @@ class Conditions:
f"condition header matches for " f"condition header matches for "
f"header: {header}") f"header: {header}")
if self.metavar is not None: if self.metavar is not None:
named_subgroups = match.groupdict(default="") named_subgroups = match.groupdict(default=None)
for group, value in named_subgroups.items(): for group, value in named_subgroups.items():
if value is None:
continue
name = f"{self.metavar}_{group}" name = f"{self.metavar}_{group}"
milter.msginfo["vars"][name] = value milter.msginfo["vars"][name] = value
break break
@@ -188,4 +194,8 @@ class Conditions:
"condition header does not match") "condition header does not match")
return False return False
if self.var is not None:
if self.var not in milter.msginfo["vars"]:
return False
return True return True