From d9bc00cd0e9f5cc4c0eb2e5ad2f09b0ec4f9856f Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Thu, 14 Jun 2018 13:38:38 +0200 Subject: [PATCH 1/3] MailMover: fix renames `:flags` need to be preserved when renaming files, but UIDs of the form `U=[0-9]+` should be removed. The current code fails to do that when a mail file has a UID but no flags. Change the code to nuke the non-flag part in any case. --- afew/MailMover.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/afew/MailMover.py b/afew/MailMover.py index d47d1aa..821c0f8 100644 --- a/afew/MailMover.py +++ b/afew/MailMover.py @@ -34,11 +34,16 @@ def __init__(self, max_age=0, rename = False, dry_run=False): def get_new_name(self, fname, destination): if self.rename: + parts = os.path.basename(fname).split(':') + if len(parts) > 1: + flagpart = ':' + parts[-1] + else: + flagpart = '' return os.path.join( destination, # construct a new filename, composed of a made-up ID and the flags part # of the original filename. - str(uuid.uuid1()) + ':' + os.path.basename(fname).split(':')[-1] + str(uuid.uuid1()) + flagpart ) else: return destination From b89d86ab78c1980c483b481ab287289fb4a7d09d Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Thu, 14 Jun 2018 14:19:15 +0200 Subject: [PATCH 2/3] MailMover: refactor get_new_name() Refactor so that the name generation becomes clearer, and before changing the dir generation. Change in behavour: returns a full path instead of a dir now in the rename==False case. --- afew/MailMover.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/afew/MailMover.py b/afew/MailMover.py index 821c0f8..2f278c3 100644 --- a/afew/MailMover.py +++ b/afew/MailMover.py @@ -33,20 +33,17 @@ def __init__(self, max_age=0, rename = False, dry_run=False): self.rename = rename def get_new_name(self, fname, destination): + basename = os.path.basename(fname) if self.rename: - parts = os.path.basename(fname).split(':') + parts = basename.split(':') if len(parts) > 1: flagpart = ':' + parts[-1] else: flagpart = '' - return os.path.join( - destination, # construct a new filename, composed of a made-up ID and the flags part # of the original filename. - str(uuid.uuid1()) + flagpart - ) - else: - return destination + basename = str(uuid.uuid1()) + flagpart + return os.path.join(destination, basename) def move(self, maildir, rules): ''' From d96c0accd4babe6a4bd0dd2b1bbe04ed2e369af0 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Thu, 14 Jun 2018 14:30:33 +0200 Subject: [PATCH 3/3] MailMover: preserve maildir subdir Currently, MailMover moves all mail to `/cur`. But everyone moving mail from `/new` to `/cur` is required to set up the maildir flags field in the name. Rather than setting up that field, preserve the maildir subdir part and let other clients change subdirs as necessary. After all, "moving" does not constitute "reading". --- afew/MailMover.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/afew/MailMover.py b/afew/MailMover.py index 2f278c3..9f75122 100644 --- a/afew/MailMover.py +++ b/afew/MailMover.py @@ -34,6 +34,7 @@ def __init__(self, max_age=0, rename = False, dry_run=False): def get_new_name(self, fname, destination): basename = os.path.basename(fname) + submaildir = os.path.split(os.path.split(fname)[0])[1] if self.rename: parts = basename.split(':') if len(parts) > 1: @@ -43,7 +44,7 @@ def get_new_name(self, fname, destination): # construct a new filename, composed of a made-up ID and the flags part # of the original filename. basename = str(uuid.uuid1()) + flagpart - return os.path.join(destination, basename) + return os.path.join(destination, submaildir, basename) def move(self, maildir, rules): ''' @@ -54,7 +55,7 @@ def move(self, maildir, rules): to_delete_fnames = [] moved = False for query in rules.keys(): - destination = '{}/{}/cur/'.format(self.db_path, rules[query]) + destination = '{}/{}/'.format(self.db_path, rules[query]) main_query = self.query.format( folder=maildir.replace("\"", "\\\""), subquery=query) logging.debug("query: {}".format(main_query))