improve performance and logging
This commit is contained in:
@@ -115,19 +115,19 @@ class QuarantineMilter(Milter.Base):
|
|||||||
# serialize the message object so it updates its internal strucure
|
# serialize the message object so it updates its internal strucure
|
||||||
self.msg_as_bytes()
|
self.msg_as_bytes()
|
||||||
|
|
||||||
old_headers = [(f, f.lower(), v) for f, v in old_headers]
|
headers = set(self.msg.items())
|
||||||
headers = [(f, f.lower(), v) for f, v in self.msg.items()]
|
to_remove = list(set(old_headers) - headers)
|
||||||
|
to_add = list(headers - set(old_headers))
|
||||||
|
|
||||||
idx = defaultdict(int)
|
idx = defaultdict(int)
|
||||||
for field, field_lower, value in old_headers:
|
for field, value in old_headers:
|
||||||
idx[field_lower] += 1
|
|
||||||
if (field, field_lower, value) not in headers:
|
|
||||||
self.chgheader(field, "", idx=idx[field_lower])
|
|
||||||
idx[field_lower] -= 1
|
|
||||||
|
|
||||||
for field, value in self.msg.items():
|
|
||||||
field_lower = field.lower()
|
field_lower = field.lower()
|
||||||
if (field, field_lower, value) not in old_headers:
|
if (field, value) in to_remove:
|
||||||
|
self.chgheader(field, "", idx=idx[field_lower] + 1)
|
||||||
|
continue
|
||||||
|
idx[field_lower] += 1
|
||||||
|
|
||||||
|
for field, value in to_add:
|
||||||
self.addheader(field, value)
|
self.addheader(field, value)
|
||||||
|
|
||||||
def replacebody(self):
|
def replacebody(self):
|
||||||
|
|||||||
@@ -133,7 +133,6 @@ def list_quarantines(quarantines, args):
|
|||||||
|
|
||||||
|
|
||||||
def list_quarantine_emails(quarantines, args):
|
def list_quarantine_emails(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
storage = _get_quarantine(quarantines, args.quarantine, args.debug).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
|
||||||
@@ -170,7 +169,9 @@ def list_quarantine_emails(quarantines, args):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if not emails:
|
if not emails:
|
||||||
logger.info(f"quarantine '{args.quarantine}' is empty")
|
print(f"quarantine '{args.quarantine}' is empty")
|
||||||
|
return
|
||||||
|
|
||||||
print_table(
|
print_table(
|
||||||
[("Quarantine-ID", "storage_id"), ("When", "timestamp"),
|
[("Quarantine-ID", "storage_id"), ("When", "timestamp"),
|
||||||
("From", "mailfrom"), ("Recipient(s)", "recipient"),
|
("From", "mailfrom"), ("Recipient(s)", "recipient"),
|
||||||
@@ -180,7 +181,6 @@ def list_quarantine_emails(quarantines, args):
|
|||||||
|
|
||||||
|
|
||||||
def list_whitelist(quarantines, args):
|
def list_whitelist(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
whitelist = _get_whitelist(quarantines, args.quarantine, args.debug)
|
whitelist = _get_whitelist(quarantines, args.quarantine, args.debug)
|
||||||
|
|
||||||
# find whitelist entries
|
# find whitelist entries
|
||||||
@@ -189,8 +189,7 @@ def list_whitelist(quarantines, args):
|
|||||||
recipients=args.recipients,
|
recipients=args.recipients,
|
||||||
older_than=args.older_than)
|
older_than=args.older_than)
|
||||||
if not entries:
|
if not entries:
|
||||||
logger.info(
|
print(f"whitelist of quarantine '{args.quarantine}' is empty")
|
||||||
f"whitelist of quarantine '{args.quarantine}' is empty")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# transform some values to strings
|
# transform some values to strings
|
||||||
@@ -268,8 +267,10 @@ def notify(quarantines, args):
|
|||||||
def release(quarantines, args):
|
def release(quarantines, args):
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
quarantine = _get_quarantine(quarantines, args.quarantine, args.debug)
|
quarantine = _get_quarantine(quarantines, args.quarantine, args.debug)
|
||||||
quarantine.release(args.quarantine_id, args.recipient)
|
rcpts = quarantine.release(args.quarantine_id, args.recipient)
|
||||||
logger.info("quarantined email released successfully")
|
logger.info(
|
||||||
|
f"quarantined email {args.quarantine_id} released successfully "
|
||||||
|
f"for recipients {rcpts}")
|
||||||
|
|
||||||
|
|
||||||
def delete(quarantines, args):
|
def delete(quarantines, args):
|
||||||
|
|||||||
@@ -533,6 +533,8 @@ class Quarantine:
|
|||||||
f"error while sending email to '{recipient}': {e}")
|
f"error while sending email to '{recipient}': {e}")
|
||||||
self.storage.delete(storage_id, recipient)
|
self.storage.delete(storage_id, recipient)
|
||||||
|
|
||||||
|
return recipients
|
||||||
|
|
||||||
def execute(self, milter):
|
def execute(self, milter):
|
||||||
logger = CustomLogger(
|
logger = CustomLogger(
|
||||||
self.logger, {"name": self.cfg["name"], "qid": milter.qid})
|
self.logger, {"name": self.cfg["name"], "qid": milter.qid})
|
||||||
|
|||||||
Reference in New Issue
Block a user