summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-11-20 15:09:51 +0100
committerAnton Khirnov <anton@khirnov.net>2021-11-20 15:15:36 +0100
commitd38607586467ab1cdc858756402de79cd5bf516b (patch)
treea5252c8289129f372d93bb02031172b1bb5c55d6
parent91b6b4f25e0fb8ccc9d96893f19499b62f73c78b (diff)
commands/thread: use named constants instead of string literals
This is safer against typos.
-rw-r--r--alot/commands/thread.py59
-rw-r--r--alot/mail/headers.py18
2 files changed, 49 insertions, 28 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py
index 205455f5..00267418 100644
--- a/alot/commands/thread.py
+++ b/alot/commands/thread.py
@@ -33,6 +33,7 @@ from ..helper import formataddr
from ..helper import split_commandstring
from ..mail.attachment import Attachment
from ..mail.envelope import Envelope
+from ..mail import headers as HDR
from ..settings.const import settings
from ..utils import argparse as cargparse
from ..utils.mailcap import MailcapHandler
@@ -154,7 +155,7 @@ class ReplyCommand(Command):
super().__init__(**kwargs)
def _reply_subject(self, message):
- subject = message.headers.get('Subject') or ''
+ subject = message.headers.get(HDR.SUBJECT) or ''
reply_subject_hook = settings.get_hook('reply_subject')
if reply_subject_hook:
subject = reply_subject_hook(subject)
@@ -197,7 +198,7 @@ class ReplyCommand(Command):
envelope = Envelope(bodytext = mailcontent, replied = message)
- envelope.add('Subject', self._reply_subject(message))
+ envelope.add(HDR.SUBJECT, self._reply_subject(message))
# set From-header and sending account
try:
@@ -205,17 +206,18 @@ class ReplyCommand(Command):
except AssertionError as e:
ui.notify(str(e), priority='error')
return
- envelope.add('From', from_header)
+ envelope.add(HDR.FROM, from_header)
envelope.account = account
# set To
- sender = message.headers.get('Reply-To') or message.headers.get('From') or ''
+ sender = (message.headers.get(HDR.REPLY_TO) or
+ message.headers.get(HDR.FROM) or '')
sender_address = parseaddr(sender)[1]
cc = []
# check if reply is to self sent message
if account.matches_address(sender_address):
- recipients = message.headers.get_all('To')
+ recipients = message.headers.get_all(HDR.TO)
emsg = 'Replying to own message, set recipients to: %s' \
% recipients
logging.debug(emsg)
@@ -225,26 +227,26 @@ class ReplyCommand(Command):
if self.groupreply:
# make sure that our own address is not included
# if the message was self-sent, then our address is not included
- MFT = message.headers.get_all('Mail-Followup-To')
+ MFT = message.headers.get_all(HDR.MAIL_FOLLOWUP_TO)
followupto = clear_my_address(account, MFT)
if followupto and settings.get('honor_followup_to'):
logging.debug('honor followup to: %s', ', '.join(followupto))
recipients = followupto
# since Mail-Followup-To was set, ignore the Cc header
else:
- if sender != message.headers.get('From'):
- recipients.append(message.headers.get('From'))
+ if sender != message.headers.get(HDR.FROM):
+ recipients.append(message.headers.get(HDR.FROM))
# append To addresses if not replying to self sent message
if not account.matches_address(sender_address):
cleared = clear_my_address(account,
- message.headers.get_all('To'))
+ message.headers.get_all(HDR.TO))
recipients.extend(cleared)
# copy cc for group-replies
- if 'Cc' in message.headers:
- cc = clear_my_address(account, message.headers.get_all('Cc'))
- envelope.add('Cc', ', '.join(cc))
+ if HDR.CC in message.headers:
+ cc = clear_my_address(account, message.headers.get_all(HDR.CC))
+ envelope.add(HDR.CC, ', '.join(cc))
to = ', '.join(ensure_unique_address(recipients))
logging.debug('reply to: %s', to)
@@ -254,20 +256,21 @@ class ReplyCommand(Command):
# Reply-To is standart reply target RFC 2822:, RFC 1036: 2.2.1
# X-BeenThere is needed by sourceforge ML also winehq
# X-Mailing-List is also standart and is used by git-send-mail
- to = message.headers.get('Reply-To') or message.headers.get('X-BeenThere') or \
- message.headers.get('X-Mailing-List')
+ to = (message.headers.get(HDR.REPLY_TO) or
+ message.headers.get(HDR.X_BEEN_THERE) or
+ message.headers.get(HDR.X_MAILING_LIST))
# Some mail server (gmail) will not resend you own mail, so you
# have to deal with the one in sent
if to is None:
- to = message.headers['To']
+ to = message.headers[HDR.TO]
logging.debug('mail list reply to: %s', to)
# Cleaning the 'To' in this case
- if envelope.get('To') is not None:
- envelope.__delitem__('To')
+ if envelope.get(HDR.TO) is not None:
+ envelope.__delitem__(HDR.TO)
# Finally setup the 'To' header
- envelope.add('To', to)
+ envelope.add(HDR.TO, to)
# if any of the recipients is a mailinglist that we are subscribed to,
# set Mail-Followup-To header so that duplicates are avoided
@@ -279,22 +282,22 @@ class ReplyCommand(Command):
if any(addr in lists for n, addr in getaddresses(allrecipients)):
followupto = ', '.join(allrecipients)
logging.debug('mail followup to: %s', followupto)
- envelope.add('Mail-Followup-To', followupto)
+ envelope.add(HDR.MAIL_FOLLOWUP_TO, followupto)
# set In-Reply-To header
- envelope.add('In-Reply-To', '<%s>' % message.id)
+ envelope.add(HDR.IN_REPLY_TO, '<%s>' % message.id)
# set References header
- old_references = message.headers.get('References')
+ old_references = message.headers.get(HDR.REFERENCES)
if old_references:
old_references = old_references.split()
references = old_references[-8:]
if len(old_references) > 8:
references = old_references[:1] + references
references.append('<%s>' % message.id)
- envelope.add('References', ' '.join(references))
+ envelope.add(HDR.REFERENCES, ' '.join(references))
else:
- envelope.add('References', '<%s>' % message.id)
+ envelope.add(HDR.REFERENCES, '<%s>' % message.id)
# continue to compose
await ui.apply_command(ComposeCommand(envelope=envelope,
@@ -360,7 +363,7 @@ class ForwardCommand(Command):
envelope.attach(a)
# copy subject
- subject = message.headers.get('Subject') or ''
+ subject = message.headers.get(HDR.SUBJECT) or ''
subject = 'Fwd: ' + subject
forward_subject_hook = settings.get_hook('forward_subject')
if forward_subject_hook:
@@ -369,7 +372,7 @@ class ForwardCommand(Command):
fsp = settings.get('forward_subject_prefix')
if not subject.startswith(('Fwd:', fsp)):
subject = fsp + subject
- envelope.add('Subject', subject)
+ envelope.add(HDR.SUBJECT, subject)
# set From-header and sending account
try:
@@ -377,7 +380,7 @@ class ForwardCommand(Command):
except AssertionError as e:
ui.notify(str(e), priority='error')
return
- envelope.add('From', from_header)
+ envelope.add(HDR.FROM, from_header)
envelope.account = account
# continue to compose
@@ -469,8 +472,8 @@ class EditNewCommand(Command):
envelope = Envelope(bodytext=mailcontent, tags=tags)
# copy selected headers
- to_copy = ['Subject', 'From', 'To', 'Cc', 'Bcc', 'In-Reply-To',
- 'References']
+ to_copy = [HDR.SUBJECT, HDR.FROM, HDR.TO, HDR.CC, HDR.BCC,
+ HDR.IN_REPLY_TO, HDR.REFERENCES]
for key in to_copy:
if key in self.message.headers:
value = self.message.headers[key][0]
diff --git a/alot/mail/headers.py b/alot/mail/headers.py
new file mode 100644
index 00000000..de301b99
--- /dev/null
+++ b/alot/mail/headers.py
@@ -0,0 +1,18 @@
+# This file is released under the GNU GPL, version 3 or a later revision.
+# For further details see the COPYING file
+
+FROM = 'From'
+TO = 'To'
+CC = 'Cc'
+BCC = 'Bcc'
+REPLY_TO = 'Reply-To'
+MAIL_REPLY_TO = 'Mail-Reply-To'
+MAIL_FOLLOWUP_TO = 'Mail-Followup-To'
+
+SUBJECT = 'Subject'
+
+IN_REPLY_TO = 'In-Reply-To'
+REFERENCES = 'references'
+
+X_BEEN_THERE = 'X-Been-There'
+X_MAILING_LIST = 'X-Mailing-List'