~barry/mailman/events-and-web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
===============
Subject munging
===============

Messages that flow through the global pipeline get their headers *cooked*,
which basically means that their headers go through several mostly unrelated
transformations.  Some headers get added, others get changed.  Some of these
changes depend on mailing list settings and others depend on how the message
is getting sent through the system.  We'll take things one-by-one.

    >>> mlist = create_list('test@example.com')


Inserting a prefix
==================

Another thing header cooking does is *munge* the ``Subject`` header by
inserting the subject prefix for the list at the front.  If there's no subject
header in the original message, Mailman uses a canned default.  In order to do
subject munging, a mailing list must have a preferred language.
::

    >>> mlist.subject_prefix = '[XTest] '
    >>> mlist.preferred_language = 'en'
    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ...
    ... A message of great import.
    ... """)
    >>> msgdata = {}

    >>> from mailman.handlers.cook_headers import process
    >>> process(mlist, msg, msgdata)

The original subject header is stored in the message metadata.

    >>> msgdata['original_subject']
    u''
    >>> print msg['subject']
    [XTest] (no subject)

If the original message had a ``Subject`` header, then the prefix is inserted
at the beginning of the header's value.

    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Subject: Something important
    ...
    ... A message of great import.
    ... """)
    >>> msgdata = {}
    >>> process(mlist, msg, msgdata)
    >>> print msgdata['original_subject']
    Something important
    >>> print msg['subject']
    [XTest] Something important

``Subject`` headers are not munged for digest messages.
    
    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Subject: Something important
    ...
    ... A message of great import.
    ... """)
    >>> process(mlist, msg, dict(isdigest=True))
    >>> print msg['subject']
    Something important

Nor are they munged for *fast tracked* messages, which are generally defined
as messages that Mailman crafts internally.

    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Subject: Something important
    ...
    ... A message of great import.
    ... """)
    >>> process(mlist, msg, dict(_fasttrack=True))
    >>> print msg['subject']
    Something important

If a ``Subject`` header already has a prefix, usually following a ``Re:``
marker, another one will not be added but the prefix will be moved to the
front of the header text.

    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Subject: Re: [XTest] Something important
    ...
    ... A message of great import.
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest] Re: Something important

If the ``Subject`` header has a prefix at the front of the header text, that's
where it will stay.  This is called *new style* prefixing and is the only
option available in Mailman 3.

    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Subject: [XTest] Re: Something important
    ...
    ... A message of great import.
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest] Re: Something important


Internationalized headers
=========================

Internationalization adds some interesting twists to the handling of subject
prefixes.  Part of what makes this interesting is the encoding of i18n headers
using RFC 2047, and lists whose preferred language is in a different character
set than the encoded header.

    >>> msg = message_from_string("""\
    ... Subject: =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest] =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    >>> unicode(msg['subject'])
    u'[XTest] \u30e1\u30fc\u30eb\u30de\u30f3'


Prefix numbers
==============

Subject prefixes support a placeholder for the numeric post id.  Every time a
message is posted to the mailing list, a *post id* gets incremented.  This is
a purely sequential integer that increases monotonically.  By added a ``%d``
placeholder to the subject prefix, this post id can be included in the prefix.

    >>> mlist.subject_prefix = '[XTest %d] '
    >>> mlist.post_id = 456
    >>> msg = message_from_string("""\
    ... Subject: Something important
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] Something important

This works even when the message is a reply, except that in this case, the
numeric post id in the generated subject prefix is updated with the new post
id.

    >>> msg = message_from_string("""\
    ... Subject: [XTest 123] Re: Something important
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] Re: Something important

If the ``Subject`` header had old style prefixing, the prefix is moved to the
front of the header text.

    >>> msg = message_from_string("""\
    ... Subject: Re: [XTest 123] Something important
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] Re: Something important


And of course, the proper thing is done when posting id numbers are included
in the subject prefix, and the subject is encoded non-ASCII.

    >>> msg = message_from_string("""\
    ... Subject: =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    >>> unicode(msg['subject'])
    u'[XTest 456] \u30e1\u30fc\u30eb\u30de\u30f3'

Even more fun is when the internationalized ``Subject`` header already has a
prefix, possibly with a different posting number.

    >>> msg = message_from_string("""\
    ... Subject: [XTest 123] Re: =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] Re: =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=

..
 # XXX This requires Python email patch #1681333 to succeed.
 #    >>> unicode(msg['subject'])
 #    u'[XTest 456] Re: \u30e1\u30fc\u30eb\u30de\u30f3'

As before, old style subject prefixes are re-ordered.

    >>> msg = message_from_string("""\
    ... Subject: Re: [XTest 123] =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest 456] Re:
      =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=

..
 # XXX This requires Python email patch #1681333 to succeed.
 #    >>> unicode(msg['subject'])
 #    u'[XTest 456] Re: \u30e1\u30fc\u30eb\u30de\u30f3'


In this test case, we get an extra space between the prefix and the original
subject.  It's because the original is *crooked*.  Note that a ``Subject``
starting with '\n ' is generated by some version of Eudora Japanese edition.

    >>> mlist.subject_prefix = '[XTest] '
    >>> msg = message_from_string("""\
    ... Subject:
    ...  Important message
    ...
    ... """)
    >>> process(mlist, msg, {})
    >>> print msg['subject']
    [XTest]  Important message

And again, with an RFC 2047 encoded header.

    >>> msg = message_from_string("""\
    ... Subject:
    ...  =?iso-2022-jp?b?GyRCJWEhPCVrJV4lcxsoQg==?=
    ...
    ... """)
    >>> process(mlist, msg, {})

..
 # XXX This one does not appear to work the same way as
 # test_subject_munging_prefix_crooked() in the old Python-based tests.  I need
 # to get Tokio to look at this.
 #    >>> print msg['subject']
 #    [XTest] =?iso-2022-jp?b?IBskQiVhITwlayVeJXMbKEI=?=