1 # Copyright (C) 1998-2019 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Cook a message's headers."""
22 from email
.header
import Header
23 from email
.utils
import formataddr
, getaddresses
, parseaddr
24 from mailman
.core
.i18n
import _
25 from mailman
.interfaces
.handler
import IHandler
26 from mailman
.interfaces
.mailinglist
import Personalization
, ReplyToMunging
27 from mailman
.version
import VERSION
28 from public
import public
29 from zope
.interface
import implementer
32 log
= logging
.getLogger('mailman.error')
39 def uheader(mlist
, s
, header_name
=None, continuation_ws
='\t', maxlinelen
=None):
40 """Get the charset to encode the string in.
42 Then search if there is any non-ascii character is in the string. If
43 there is and the charset is us-ascii then we use iso-8859-1 instead. If
44 the string is ascii only we use 'us-ascii' if another charset is
47 If the header contains a newline, truncate it (see GL#273).
49 charset
= mlist
.preferred_language
.charset
51 s
= '{} [...]'.format(s
.split('\n')[0])
52 log
.warning('Header {} contains a newline, truncating it.'.format(
54 return Header(s
, charset
, maxlinelen
, header_name
, continuation_ws
)
57 def process(mlist
, msg
, msgdata
):
58 """Process the headers of the message."""
59 # Set the "X-Ack: no" header if noack flag is set.
60 if msgdata
.get('noack'):
63 # Because we're going to modify various important headers in the email
64 # message, we want to save some of the information in the msgdata
65 # dictionary for later. Specifically, the sender header will get waxed,
66 # but we need it for the Acknowledge module later.
67 msgdata
['original_sender'] = msg
.sender
68 # VirginRunner sets _fasttrack for internally crafted messages.
69 fasttrack
= msgdata
.get('_fasttrack')
70 # Add Precedence: and other useful headers. None of these are standard
71 # and finding information on some of them are fairly difficult. Some are
72 # just common practice, and we'll add more here as they become necessary.
73 # Good places to look are:
75 # http://www.dsv.su.se/~jpalme/ietf/jp-ietf-home.html
76 # http://www.faqs.org/rfcs/rfc2076.html
78 # None of these headers are added if they already exist. BAW: some
79 # consider the advertising of this a security breach. I.e. if there are
80 # known exploits in a particular version of Mailman and we know a site is
81 # using such an old version, they may be vulnerable. It's too easy to
82 # edit the code to add a configuration variable to handle this.
83 if 'x-mailman-version' not in msg
:
84 msg
['X-Mailman-Version'] = VERSION
85 # We set "Precedence: list" because this is the recommendation from the
86 # sendmail docs, the most authoritative source of this header's semantics.
87 if 'precedence' not in msg
:
88 msg
['Precedence'] = 'list'
89 # Reply-To: munging. Do not do this if the message is "fast tracked",
90 # meaning it is internally crafted and delivered to a specific user. BAW:
91 # Yuck, I really hate this feature but I've caved under the sheer pressure
92 # of the (very vocal) folks want it. OTOH, RFC 2822 allows Reply-To: to
93 # be a list of addresses, so instead of replacing the original, simply
94 # augment it. RFC 2822 allows max one Reply-To: header so collapse them
95 # if we're adding a value, otherwise don't touch it. (Should we collapse
98 # A convenience function, requires nested scopes. pair is (name, addr)
101 def add(pair
): # noqa: E306
102 lcaddr
= pair
[1].lower()
107 # List admin wants an explicit Reply-To: added
108 if (mlist
.reply_goes_to_list
is ReplyToMunging
.explicit_header
109 or mlist
.reply_goes_to_list
110 is ReplyToMunging
.explicit_header_only
):
111 add(parseaddr(mlist
.reply_to_address
))
112 # If we're not first stripping existing Reply-To: then we need to add
113 # the original Reply-To:'s to the list we're building up. In both
114 # cases we'll zap the existing field because RFC 2822 says max one is
116 if not mlist
.first_strip_reply_to
:
117 orig
= msg
.get_all('reply-to', [])
118 for pair
in getaddresses(orig
):
120 # Set Reply-To: header to point back to this list. Add this last
121 # because some folks think that some MUAs make it easier to delete
122 # addresses from the right than from the left.
123 if mlist
.reply_goes_to_list
is ReplyToMunging
.point_to_list
:
124 i18ndesc
= uheader(mlist
, mlist
.description
, 'Reply-To')
125 add((str(i18ndesc
), mlist
.posting_address
))
127 # Don't put Reply-To: back if there's nothing to add!
130 msg
['Reply-To'] = COMMASPACE
.join(
131 [formataddr(pair
) for pair
in new
])
132 # The To field normally contains the list posting address. However
133 # when messages are fully personalized, that header will get
134 # overwritten with the address of the recipient. We need to get the
135 # posting address in one of the recipient headers or they won't be
136 # able to reply back to the list. It's possible the posting address
137 # was munged into the Reply-To header, but if not, we'll add it to a
138 # Cc header. BAW: should we force it into a Reply-To header in the
140 # Also skip Cc if this is an anonymous list as list posting address
141 # is already in From and Reply-To in this case.
142 if (mlist
.personalize
is Personalization
.full
143 and mlist
.reply_goes_to_list
is not # noqa: W503
144 ReplyToMunging
.point_to_list
145 and not mlist
.anonymous_list
): # noqa: W503
146 # Watch out for existing Cc headers, merge, and remove dups. Note
147 # that RFC 2822 says only zero or one Cc header is allowed.
150 for pair
in getaddresses(msg
.get_all('cc', [])):
152 if (mlist
.reply_goes_to_list
is not
153 ReplyToMunging
.explicit_header_only
):
154 i18ndesc
= uheader(mlist
, mlist
.description
, 'Cc')
155 add((str(i18ndesc
), mlist
.posting_address
))
157 # Don't add an empty Cc:
159 msg
['Cc'] = COMMASPACE
.join([formataddr(pair
) for pair
in new
])
163 @implementer(IHandler
)
165 """Modify message headers."""
167 name
= 'cook-headers'
168 description
= _('Modify message headers.')
170 def process(self
, mlist
, msg
, msgdata
):
171 """See `IHandler`."""
172 process(mlist
, msg
, msgdata
)