From 1abed39ff3c8d6d9a92e98ea4e2c14a900df983b Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Tue, 24 Nov 2020 17:20:40 -0500 Subject: Add --cc-trailers option to b4 am By request, add ability to copy all addresses from the email's "Cc" header into Cc: trailers, unless they are already mentioned in some other trailer. Requested-by: Arnaldo Carvalho de Melo Signed-off-by: Konstantin Ryabitsev --- b4/__init__.py | 29 ++++++++++++++++++++++++----- b4/command.py | 2 ++ b4/mbox.py | 3 ++- man/b4.5 | 3 +++ man/b4.5.rst | 2 ++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/b4/__init__.py b/b4/__init__.py index 4da197b..86991e3 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -483,7 +483,7 @@ class LoreSeries: return slug def save_am_mbox(self, mbx, noaddtrailers=False, covertrailers=False, trailer_order=None, addmysob=False, - addlink=False, linkmask=None, cherrypick=None): + addlink=False, linkmask=None, cherrypick=None, copyccs=False): usercfg = get_user_config() config = get_main_config() @@ -559,7 +559,7 @@ class LoreSeries: add_trailers = True if noaddtrailers: add_trailers = False - msg = lmsg.get_am_message(add_trailers=add_trailers, trailer_order=trailer_order) + msg = lmsg.get_am_message(add_trailers=add_trailers, trailer_order=trailer_order, copyccs=copyccs) # Pass a policy that avoids most legacy encoding horrors mbx.add(msg.as_bytes(policy=emlpolicy)) else: @@ -1299,10 +1299,29 @@ class LoreMessage: return githeaders, message, trailers, basement, signature - def fix_trailers(self, trailer_order=None): + def fix_trailers(self, trailer_order=None, copyccs=False): bheaders, message, btrailers, basement, signature = LoreMessage.get_body_parts(self.body) # Now we add mix-in trailers trailers = btrailers + self.followup_trailers + + if copyccs: + allccs = email.utils.getaddresses([str(x) for x in self.msg.get_all('cc', [])]) + # Sort by domain name, then local + allccs.sort(key=lambda x: x[1].find('@') > 0 and x[1].split('@')[1] + x[1].split('@')[0] or x[1]) + for pair in allccs: + found = False + for ftr in trailers: + if ftr[1].lower().find(pair[1].lower()) >= 0: + # already present + found = True + break + + if not found: + if len(pair[0]): + trailers.append(('Cc', f'{pair[0]} <{pair[1]}>', None)) # noqa + else: + trailers.append(('Cc', pair[1], None)) # noqa + fixtrailers = list() if trailer_order is None: trailer_order = DEFAULT_TRAILER_ORDER @@ -1347,9 +1366,9 @@ class LoreMessage: self.body += signature self.body += '\n' - def get_am_message(self, add_trailers=True, trailer_order=None): + def get_am_message(self, add_trailers=True, trailer_order=None, copyccs=False): if add_trailers: - self.fix_trailers(trailer_order=trailer_order) + self.fix_trailers(trailer_order=trailer_order, copyccs=copyccs) am_body = self.body am_msg = email.message.EmailMessage() am_msg.set_payload(am_body.encode('utf-8')) diff --git a/b4/command.py b/b4/command.py index ee865b9..1105a05 100644 --- a/b4/command.py +++ b/b4/command.py @@ -112,6 +112,8 @@ def cmd(): sp_am.add_argument('-3', '--prep-3way', dest='threeway', action='store_true', default=False, help='Prepare for a 3-way merge ' '(tries to ensure that all index blobs exist by making a fake commit range)') + sp_am.add_argument('--cc-trailers', dest='copyccs', action='store_true', default=False, + help='Copy all Cc\'d addresses into Cc: trailers') sp_am.add_argument('--no-cover', dest='nocover', action='store_true', default=False, help='Do not save the cover letter (on by default when using -o -)') sp_am.set_defaults(func=cmd_am) diff --git a/b4/mbox.py b/b4/mbox.py index 86825f2..e808c58 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -114,7 +114,8 @@ def mbox_to_am(mboxfile, cmdargs): am_mbx = lser.save_am_mbox(mbx, noaddtrailers=cmdargs.noaddtrailers, covertrailers=covertrailers, trailer_order=config['trailer-order'], addmysob=cmdargs.addmysob, addlink=cmdargs.addlink, - linkmask=config['linkmask'], cherrypick=cherrypick) + linkmask=config['linkmask'], cherrypick=cherrypick, + copyccs=cmdargs.copyccs) except KeyError: sys.exit(1) diff --git a/man/b4.5 b/man/b4.5 index 8da4ef4..350c363 100644 --- a/man/b4.5 +++ b/man/b4.5 @@ -169,6 +169,9 @@ Try to guess the base of the series (if not specified) .B \-3\fP,\fB \-\-prep\-3way Prepare for a 3\-way merge (tries to ensure that all index blobs exist by making a fake commit range) .TP +.B \-\-cc\-trailers +Copy all Cc\(aqd addresses into Cc: trailers, if not already present +.TP .B \-\-no\-cover Do not save the cover letter (on by default when using \-o \-) .UNINDENT diff --git a/man/b4.5.rst b/man/b4.5.rst index b8f8f8b..d1aa251 100644 --- a/man/b4.5.rst +++ b/man/b4.5.rst @@ -104,6 +104,8 @@ optional arguments: Try to guess the base of the series (if not specified) -3, --prep-3way Prepare for a 3-way merge (tries to ensure that all index blobs exist by making a fake commit range) + --cc-trailers + Copy all Cc'd addresses into Cc: trailers, if not already present --no-cover Do not save the cover letter (on by default when using -o -) -- cgit v1.2.3