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