Fix encoding issues
This commit is contained in:
@@ -95,7 +95,8 @@ class QuarantineMilter(Milter.Base):
|
|||||||
def set_configfiles(config_files):
|
def set_configfiles(config_files):
|
||||||
QuarantineMilter._config_files = config_files
|
QuarantineMilter._config_files = config_files
|
||||||
|
|
||||||
def connect(self, IPname, family, hostaddr):
|
def connect(self, hostname, family, hostaddr):
|
||||||
|
self.hostaddr = hostaddr
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
"accepted milter connection from {} port {}".format(
|
"accepted milter connection from {} port {}".format(
|
||||||
*hostaddr))
|
*hostaddr))
|
||||||
@@ -134,15 +135,33 @@ class QuarantineMilter(Milter.Base):
|
|||||||
"{}: received queue-id from MTA".format(self.queueid))
|
"{}: received queue-id from MTA".format(self.queueid))
|
||||||
self.recipients = list(self.recipients)
|
self.recipients = list(self.recipients)
|
||||||
self.headers = []
|
self.headers = []
|
||||||
|
self.logger.debug(
|
||||||
|
"{}: initializing memory buffer to save email data".format(
|
||||||
|
self.queueid))
|
||||||
|
# initialize memory buffer to save email data
|
||||||
|
self.fp = BytesIO()
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
@Milter.noreply
|
@Milter.noreply
|
||||||
def header(self, name, value):
|
def header(self, name, value):
|
||||||
self.headers.append((name, value))
|
try:
|
||||||
|
# write email header to memory buffer
|
||||||
|
self.fp.write("{}: {}\r\n".format(name, value).encode(
|
||||||
|
encoding="ascii", errors="surrogateescape"))
|
||||||
|
# keep copy of header without surrogates for later use
|
||||||
|
self.headers.append((
|
||||||
|
name.encode(errors="surrogateescape").decode(errors="replace"),
|
||||||
|
value.encode(errors="surrogateescape").decode(errors="replace")))
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.exception(
|
||||||
|
"an exception occured in header function: {}".format(e))
|
||||||
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
def eoh(self):
|
def eoh(self):
|
||||||
try:
|
try:
|
||||||
|
self.fp.write("\r\n".encode(encoding="ascii"))
|
||||||
self.whitelist_cache = whitelists.WhitelistCache()
|
self.whitelist_cache = whitelists.WhitelistCache()
|
||||||
|
|
||||||
# initialize dicts to set quaranines per recipient and keep matches
|
# initialize dicts to set quaranines per recipient and keep matches
|
||||||
@@ -238,26 +257,14 @@ class QuarantineMilter(Milter.Base):
|
|||||||
self.queueid))
|
self.queueid))
|
||||||
return Milter.ACCEPT
|
return Milter.ACCEPT
|
||||||
|
|
||||||
# check if the email body is needed
|
# check if the mail body is needed
|
||||||
keep_body = False
|
|
||||||
for recipient, quarantine in self.recipients_quarantines.items():
|
for recipient, quarantine in self.recipients_quarantines.items():
|
||||||
if quarantine["quarantine_obj"] or quarantine["notification_obj"]:
|
if quarantine["quarantine_obj"] or quarantine["notification_obj"]:
|
||||||
keep_body = True
|
# mail body is needed, continue processing
|
||||||
break
|
return Milter.CONTINUE
|
||||||
|
|
||||||
if keep_body:
|
|
||||||
self.logger.debug(
|
|
||||||
"{}: initializing memory buffer to save email data".format(
|
|
||||||
self.queueid))
|
|
||||||
# initialize memory buffer to save email data
|
|
||||||
self.fp = BytesIO()
|
|
||||||
# write email headers to memory buffer
|
|
||||||
for name, value in self.headers:
|
|
||||||
self.fp.write("{}: {}\n".format(name, value).encode())
|
|
||||||
self.fp.write("\n".encode())
|
|
||||||
else:
|
|
||||||
# quarantine and notification are disabled on all matching
|
# quarantine and notification are disabled on all matching
|
||||||
# quarantines, return configured action
|
# quarantines, just return configured action
|
||||||
quarantine = self._get_preferred_quarantine()
|
quarantine = self._get_preferred_quarantine()
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
"{}: {} matching quarantine is '{}', performing milter action {}".format(
|
"{}: {} matching quarantine is '{}', performing milter action {}".format(
|
||||||
@@ -269,8 +276,6 @@ class QuarantineMilter(Milter.Base):
|
|||||||
self.setreply("554", "5.7.0", quarantine["reject_reason"])
|
self.setreply("554", "5.7.0", quarantine["reject_reason"])
|
||||||
return quarantine["milter_action"]
|
return quarantine["milter_action"]
|
||||||
|
|
||||||
return Milter.CONTINUE
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.exception(
|
self.logger.exception(
|
||||||
"an exception occured in eoh function: {}".format(e))
|
"an exception occured in eoh function: {}".format(e))
|
||||||
@@ -372,6 +377,11 @@ class QuarantineMilter(Milter.Base):
|
|||||||
"an exception occured in eom function: {}".format(e))
|
"an exception occured in eom function: {}".format(e))
|
||||||
return Milter.TEMPFAIL
|
return Milter.TEMPFAIL
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.logger.debug(
|
||||||
|
"disconnect from {} port {}".format(
|
||||||
|
*self.hostaddr))
|
||||||
|
|
||||||
|
|
||||||
def generate_milter_config(configtest=False, config_files=[]):
|
def generate_milter_config(configtest=False, config_files=[]):
|
||||||
"Generate the configuration for QuarantineMilter class."
|
"Generate the configuration for QuarantineMilter class."
|
||||||
|
|||||||
Reference in New Issue
Block a user