From df1ddbf046a7d29b4ba960668b7f1f9a085cd68e Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Mon, 4 Oct 2021 21:04:30 +0200 Subject: [PATCH] convert meta files to new format when read --- pyquarantine/cli.py | 8 ++++---- pyquarantine/storage.py | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pyquarantine/cli.py b/pyquarantine/cli.py index 895f057..79e7fc0 100644 --- a/pyquarantine/cli.py +++ b/pyquarantine/cli.py @@ -142,10 +142,10 @@ def list_quarantine_emails(quarantines, args): for storage_id, metadata in emails.items(): row = emails[storage_id] row["storage_id"] = storage_id - row["date"] = time.strftime( + row["timestamp"] = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( - metadata["date"])) + metadata["timestamp"])) row["mailfrom"] = metadata["mailfrom"] row["recipient"] = metadata["recipients"].pop(0) if "subject" not in emails[storage_id]: @@ -156,7 +156,7 @@ def list_quarantine_emails(quarantines, args): if metadata["recipients"]: row = { "storage_id": "", - "date": "", + "timestamp": "", "mailfrom": "", "recipient": metadata["recipients"].pop(0), "subject": "" @@ -171,7 +171,7 @@ def list_quarantine_emails(quarantines, args): if not emails: logger.info(f"quarantine '{args.quarantine}' is empty") print_table( - [("Quarantine-ID", "storage_id"), ("Date", "date"), + [("Quarantine-ID", "storage_id"), ("When", "timestamp"), ("From", "mailfrom"), ("Recipient(s)", "recipient"), ("Subject", "subject")], rows diff --git a/pyquarantine/storage.py b/pyquarantine/storage.py index a17c4b1..e05ffc2 100644 --- a/pyquarantine/storage.py +++ b/pyquarantine/storage.py @@ -187,7 +187,6 @@ class FileMailStorage(BaseMailStorage): metadata = { "mailfrom": mailfrom, "recipients": recipients, - "date": timegm(gmtime()), "subject": subject, "timestamp": timegm(gmtime()), "queue_id": qid, @@ -210,7 +209,7 @@ class FileMailStorage(BaseMailStorage): # parsing of the message, catch all exceptions here try: subject = milter.msg["subject"] or "" - except Exception as e: + except Exception: subject = "" else: data = milter.msg.as_bytes @@ -242,6 +241,41 @@ class FileMailStorage(BaseMailStorage): raise RuntimeError( f"invalid metafile '{metafile}': {e}") + # convert metafile structure, this can be removed in the future + converted = False + if "subject" not in metadata: + try: + metadata["subject"] = metadata["headers"]["subject"] + except KeyError: + metadata["subject"] = "" + converted = True + if "timestamp" not in metadata: + try: + metadata["timestamp"] = metadata["date"] + except KeyError: + metadata["timestamp"] = 0 + converted = True + if "vars" not in metadata: + try: + metadata["vars"] = metadata["named_subgroups"] + except KeyError: + metadata["vars"] = {} + converted = True + if "headers" in metadata: + del metadata["headers"] + converted = True + if "date" in metadata: + del metadata["date"] + converted = True + if "named_subgroups" in metadata: + del metadata["named_subgroups"] + converted = True + if "subgroups" in metadata: + del metadata["subgroups"] + converted = True + if converted: + self._save_metafile(metafile, metadata) + return metadata def find(self, mailfrom=None, recipients=None, older_than=None): @@ -266,7 +300,8 @@ class FileMailStorage(BaseMailStorage): metafile[:-len(self._metadata_suffix)]) metadata = self.get_metadata(storage_id) if older_than is not None: - if timegm(gmtime()) - metadata["date"] < (older_than * 86400): + age = timegm(gmtime()) - metadata["timestamp"] + if age < (older_than * 86400): continue if mailfrom is not None: