From 9b14a9f95ccaffc1eac08d184cea5d7a6804c4fc Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 20 Nov 2021 16:42:54 +0100 Subject: commands/thread: refactor selecting reply recipients --- alot/commands/thread.py | 82 ++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 400d4774..09b18a8b 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -197,41 +197,7 @@ class ReplyCommand(Command): logging.debug('mail followup to: %s', followupto) return followupto - async def apply(self, ui): - message = ui.current_buffer.get_selected_message() - - # set body text - name, address = message.get_author() - timestamp = message.date - qf = settings.get_hook('reply_prefix') - if qf: - mail = email.message_from_bytes(message.as_bytes(), - policy = email.policy.SMTP) - quotestring = qf(name, address, timestamp, - message=mail, ui=ui, dbm=ui.dbman) - else: - quotestring = 'Quoting %s (%s)\n' % (name or address, timestamp) - mailcontent = quotestring - quotehook = settings.get_hook('text_quote') - if quotehook: - mailcontent += quotehook(message.get_body_text()) - else: - quote_prefix = settings.get('quote_prefix') - for line in message.get_body_text().splitlines(): - mailcontent += quote_prefix + line + '\n' - - headers = {} - - headers[HDR.SUBJECT] = self._reply_subject(message) - - # set From-header and sending account - try: - from_header, account = determine_sender(message.headers, 'reply') - except AssertionError as e: - ui.notify(str(e), priority='error') - return - headers[HDR.FROM] = from_header - + def _determine_recipients(self, message, account): # set To sender = (message.headers.get(HDR.REPLY_TO) or message.headers.get(HDR.FROM) or '') @@ -269,9 +235,9 @@ class ReplyCommand(Command): # copy cc for group-replies if HDR.CC in message.headers: cc = clear_my_address(account, message.headers.get_all(HDR.CC)) - headers[HDR.CC] = ', '.join(cc) to = ', '.join(ensure_unique_address(recipients)) + cc = ', '.join(cc) logging.debug('reply to: %s', to) if self._list_reply(message): @@ -289,8 +255,48 @@ class ReplyCommand(Command): to = message.headers[HDR.TO] logging.debug('mail list reply to: %s', to) - # Finally setup the 'To' header + return to, cc + + async def apply(self, ui): + message = ui.current_buffer.get_selected_message() + + # set body text + name, address = message.get_author() + timestamp = message.date + qf = settings.get_hook('reply_prefix') + if qf: + mail = email.message_from_bytes(message.as_bytes(), + policy = email.policy.SMTP) + quotestring = qf(name, address, timestamp, + message=mail, ui=ui, dbm=ui.dbman) + else: + quotestring = 'Quoting %s (%s)\n' % (name or address, timestamp) + mailcontent = quotestring + quotehook = settings.get_hook('text_quote') + if quotehook: + mailcontent += quotehook(message.get_body_text()) + else: + quote_prefix = settings.get('quote_prefix') + for line in message.get_body_text().splitlines(): + mailcontent += quote_prefix + line + '\n' + + headers = {} + + headers[HDR.SUBJECT] = self._reply_subject(message) + + # set From-header and sending account + try: + from_header, account = determine_sender(message.headers, 'reply') + except AssertionError as e: + ui.notify(str(e), priority='error') + return + headers[HDR.FROM] = from_header + + # set the recipients + to, cc = self._determine_recipients(message, account) headers[HDR.TO] = to + if cc: + headers[HDR.CC] = cc # set In-Reply-To + References headers headers[HDR.IN_REPLY_TO] = '<%s>' % message.id @@ -299,7 +305,7 @@ class ReplyCommand(Command): # if any of the recipients is a mailinglist that we are subscribed to, # set Mail-Followup-To header so that duplicates are avoided # to and cc are already cleared of our own address - mft = self._mail_followup_to([to] + cc) + mft = self._mail_followup_to([to, cc]) if mft: headers[HDR.MAIL_FOLLOWUP_TO] = mft -- cgit v1.2.3