improve exception handling
This commit is contained in:
@@ -154,41 +154,62 @@ class ModifyMilter(Milter.Base):
|
|||||||
self.rules = ModifyMilter._rules.copy()
|
self.rules = ModifyMilter._rules.copy()
|
||||||
|
|
||||||
def connect(self, IPname, family, hostaddr):
|
def connect(self, IPname, family, hostaddr):
|
||||||
self.logger.debug(
|
try:
|
||||||
f"accepted milter connection from {hostaddr[0]} "
|
if hostaddr is None:
|
||||||
f"port {hostaddr[1]}")
|
self.logger.error("unable to proceed, host address is None")
|
||||||
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
# remove rules which ignore this host
|
|
||||||
for rule in self.rules.copy():
|
|
||||||
if rule.ignores(host=hostaddr[0]):
|
|
||||||
self.rules.remove(rule)
|
|
||||||
|
|
||||||
if not self.rules:
|
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"host {hostaddr[0]} is ignored by all rules, "
|
f"accepted milter connection from {hostaddr[0]} "
|
||||||
f"skip further processing")
|
f"port {hostaddr[1]}")
|
||||||
return Milter.ACCEPT
|
|
||||||
|
# remove rules which ignore this host
|
||||||
|
for rule in self.rules.copy():
|
||||||
|
if rule.ignores(host=hostaddr[0]):
|
||||||
|
self.rules.remove(rule)
|
||||||
|
|
||||||
|
if not self.rules:
|
||||||
|
self.logger.debug(
|
||||||
|
f"host {hostaddr[0]} is ignored by all rules, "
|
||||||
|
f"skip further processing")
|
||||||
|
return Milter.ACCEPT
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.exception(
|
||||||
|
f"an exception occured in connect function: {e}")
|
||||||
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
def envfrom(self, mailfrom, *str):
|
def envfrom(self, mailfrom, *str):
|
||||||
mailfrom = "@".join(parse_addr(mailfrom)).lower()
|
try:
|
||||||
for rule in self.rules.copy():
|
mailfrom = "@".join(parse_addr(mailfrom)).lower()
|
||||||
if rule.ignores(envfrom=mailfrom):
|
for rule in self.rules.copy():
|
||||||
self.rules.remove(rule)
|
if rule.ignores(envfrom=mailfrom):
|
||||||
|
self.rules.remove(rule)
|
||||||
|
|
||||||
if not self.rules:
|
if not self.rules:
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"envelope-from address {mailfrom} is ignored by all rules, "
|
f"envelope-from address {mailfrom} is ignored by "
|
||||||
f"skip further processing")
|
f"all rules, skip further processing")
|
||||||
return Milter.ACCEPT
|
return Milter.ACCEPT
|
||||||
|
|
||||||
|
self.recipients = set()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.exception(
|
||||||
|
f"an exception occured in envfrom function: {e}")
|
||||||
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
self.recipients = set()
|
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
@Milter.noreply
|
@Milter.noreply
|
||||||
def envrcpt(self, to, *str):
|
def envrcpt(self, to, *str):
|
||||||
self.recipients.add("@".join(parse_addr(to)).lower())
|
try:
|
||||||
|
self.recipients.add("@".join(parse_addr(to)).lower())
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.exception(
|
||||||
|
f"an exception occured in envrcpt function: {e}")
|
||||||
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
def data(self):
|
def data(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user