diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2019-05-25 22:00:06 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2019-06-02 14:44:13 +0100 |
commit | 7a68d4d2cac3f12395696a6eaadce449fa336f52 (patch) | |
tree | 9ad9acbf0fa19381ab4a44e2ee759c25969c89fa | |
parent | a48547a145c6192da418cf52ad9e12e061b83ddc (diff) |
refactor ReplyCommand
This moves the static methods clear_my_address and ensure_unique_address
out of ReplyCommand and into alot.db.utils.
-rw-r--r-- | alot/commands/thread.py | 43 | ||||
-rw-r--r-- | alot/db/utils.py | 31 |
2 files changed, 38 insertions, 36 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 21d351e2..801e798b 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -29,6 +29,8 @@ from ..db.utils import decode_header from ..db.utils import formataddr from ..db.utils import extract_headers from ..db.utils import extract_body +from ..db.utils import clear_my_address +from ..db.utils import ensure_unique_address from ..db.envelope import Envelope from ..db.attachment import Attachment from ..db.errors import DatabaseROError @@ -204,7 +206,7 @@ class ReplyCommand(Command): # make sure that our own address is not included # if the message was self-sent, then our address is not included MFT = mail.get_all('Mail-Followup-To', []) - followupto = self.clear_my_address(account, MFT) + followupto = clear_my_address(account, MFT) if followupto and settings.get('honor_followup_to'): logging.debug('honor followup to: %s', ', '.join(followupto)) recipients = followupto @@ -215,17 +217,16 @@ class ReplyCommand(Command): # append To addresses if not replying to self sent message if not account.matches_address(sender_address): - cleared = self.clear_my_address( - account, mail.get_all('To', [])) + cleared = clear_my_address(account, + mail.get_all('To', [])) recipients.extend(cleared) # copy cc for group-replies if 'Cc' in mail: - cc = self.clear_my_address( - account, mail.get_all('Cc', [])) + cc = clear_my_address(account, mail.get_all('Cc', [])) envelope.add('Cc', decode_header(', '.join(cc))) - to = ', '.join(self.ensure_unique_address(recipients)) + to = ', '.join(ensure_unique_address(recipients)) logging.debug('reply to: %s', to) if self.listreply: @@ -280,36 +281,6 @@ class ReplyCommand(Command): spawn=self.force_spawn, encrypt=encrypt)) - @staticmethod - def clear_my_address(my_account, value): - """return recipient header without the addresses in my_account - - :param my_account: my account - :type my_account: :class:`Account` - :param value: a list of recipient or sender strings (with or without - real names as taken from email headers) - :type value: list(str) - :returns: a new, potentially shortend list - :rtype: list(str) - """ - new_value = [] - for name, address in getaddresses(value): - if not my_account.matches_address(address): - new_value.append(formataddr((name, str(address)))) - return new_value - - @staticmethod - def ensure_unique_address(recipients): - """ - clean up a list of name,address pairs so that - no address appears multiple times. - """ - res = dict() - for name, address in getaddresses(recipients): - res[address] = name - urecipients = [formataddr((n, str(a))) for a, n in res.items()] - return sorted(urecipients) - @registerCommand(MODE, 'forward', arguments=[ (['--attach'], {'action': 'store_true', 'help': 'attach original mail'}), diff --git a/alot/db/utils.py b/alot/db/utils.py index 63260b22..49d5b6d2 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -561,3 +561,34 @@ def is_subdir_of(subpath, superpath): # return true, if the common prefix of both is equal to directory # e.g. /a/b/c/d.rst and directory is /a/b, the common prefix is /a/b return os.path.commonprefix([subpath, superpath]) == superpath + + +def clear_my_address(my_account, value): + """return recipient header without the addresses in my_account + + :param my_account: my account + :type my_account: :class:`Account` + :param value: a list of recipient or sender strings (with or without + real names as taken from email headers) + :type value: list(str) + :returns: a new, potentially shortend list + :rtype: list(str) + """ + new_value = [] + for name, address in email.utils.getaddresses(value): + if not my_account.matches_address(address): + new_value.append(formataddr((name, address))) + return new_value + + +def ensure_unique_address(recipients): + """ + clean up a list of name,address pairs so that + no address appears multiple times. + """ + res = dict() + for name, address in email.utils.getaddresses(recipients): + res[address] = name + logging.debug(res) + urecipients = [formataddr((n, a)) for a, n in res.items()] + return sorted(urecipients) |