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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
=================
Content filtering
=================
Mailman can filter the content of messages posted to a mailing list by
stripping MIME subparts, and possibly reorganizing the MIME structure of a
message.
>>> mlist = create_list('test@example.com')
Several mailing list options control content filtering. First, the feature
must be enabled, then there are two options that control which MIME types get
filtered and which get passed. Finally, there is an option to control whether
``text/html`` parts will get converted to plain text. Let's set up some
defaults for these variables, then we'll explain them in more detail below.
>>> mlist.filter_content = True
>>> mlist.filter_types = []
>>> mlist.pass_types = []
>>> mlist.convert_html_to_plaintext = False
Filtering the outer content type
================================
A simple filtering setting will just search the content types of the messages
parts, discarding all parts with a matching MIME type. If the message's outer
content type matches the filter, the entire message will be discarded.
However, if we turn off content filtering altogether, then the handler
short-circuits.
::
>>> from mailman.interfaces.mime import FilterAction
>>> mlist.filter_types = ['image/jpeg']
>>> mlist.filter_action = FilterAction.discard
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: image/jpeg
... MIME-Version: 1.0
...
... xxxxx
... """)
>>> process = config.handlers['mime-delete'].process
>>> mlist.filter_content = False
>>> msgdata = {}
>>> process(mlist, msg, msgdata)
>>> print(msg.as_string())
From: aperson@example.com
Content-Type: image/jpeg
MIME-Version: 1.0
<BLANKLINE>
xxxxx
>>> msgdata
{}
Similarly, no content filtering is performed on digest messages, which are
crafted internally by Mailman.
>>> mlist.filter_content = True
>>> msgdata = {'isdigest': True}
>>> process(mlist, msg, msgdata)
>>> print(msg.as_string())
From: aperson@example.com
Content-Type: image/jpeg
MIME-Version: 1.0
<BLANKLINE>
xxxxx
>>> dump_msgdata(msgdata)
isdigest: True
Simple multipart filtering
==========================
If one of the subparts in a ``multipart`` message matches the filter type,
then just that subpart will be stripped. If that leaves just a single subpart,
the ``multipart`` will be replaced by the subpart.
::
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: multipart/mixed; boundary=BOUNDARY
... MIME-Version: 1.0
...
... --BOUNDARY
... Content-Type: image/jpeg
... MIME-Version: 1.0
...
... xxx
...
... --BOUNDARY
... Content-Type: image/gif
... MIME-Version: 1.0
...
... yyy
... --BOUNDARY--
... """)
>>> process(mlist, msg, {})
>>> print(msg.as_string())
From: aperson@example.com
MIME-Version: 1.0
Content-Type: image/gif
X-Content-Filtered-By: Mailman/MimeDel ...
<BLANKLINE>
yyy
Collapsing multipart/alternative messages
=========================================
When content filtering encounters a ``multipart/alternative`` part, and the
results of filtering leave only one of the subparts, then the
``multipart/alternative`` may be collapsed. For example, in the following
message, the outer content type is a ``multipart/mixed``. Inside this part is
just a single subpart that has a content type of ``multipart/alternative``.
This inner multipart has two subparts, a jpeg and a gif.
Content filtering will remove the jpeg part, leaving the
``multipart/alternative`` with only a single gif subpart. Because there's
only one subpart left, the MIME structure of the message will be reorganized,
removing the inner ``multipart/alternative`` so that the outer
``multipart/mixed`` has just a single gif subpart, and then the multipart is
recast as just the subpart.
>>> mlist.collapse_alternatives = True
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: multipart/mixed; boundary=BOUNDARY
... MIME-Version: 1.0
...
... --BOUNDARY
... Content-Type: multipart/alternative; boundary=BOUND2
... MIME-Version: 1.0
...
... --BOUND2
... Content-Type: image/jpeg
... MIME-Version: 1.0
...
... xxx
...
... --BOUND2
... Content-Type: image/gif
... MIME-Version: 1.0
...
... yyy
... --BOUND2--
...
... --BOUNDARY--
... """)
>>> process(mlist, msg, {})
>>> print(msg.as_string())
From: aperson@example.com
MIME-Version: 1.0
Content-Type: image/gif
X-Content-Filtered-By: Mailman/MimeDel ...
<BLANKLINE>
yyy
When the outer part is a ``multipart/alternative`` and filtering leaves this
outer part with just one subpart, the entire message is converted to the left
over part's content type. In other words, the left over inner part is
promoted to being the outer part.
::
>>> mlist.filter_types = ['image/jpeg', 'text/html']
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: multipart/alternative; boundary=AAA
...
... --AAA
... Content-Type: text/html
...
... <b>This is some html</b>
... --AAA
... Content-Type: text/plain
...
... This is plain text
... --AAA--
... """)
>>> process(mlist, msg, {})
>>> print(msg.as_string())
From: aperson@example.com
Content-Type: text/plain
X-Content-Filtered-By: Mailman/MimeDel ...
<BLANKLINE>
This is plain text
Clean up.
>>> mlist.filter_types = ['image/jpeg']
Conversion to plain text
========================
Some mailing lists prohibit HTML email, and in fact, such email can be a
phishing or spam vector. However, many mail readers will send HTML email by
default because users think it looks pretty. One approach to handling this
would be to filter out ``text/html`` parts and rely on
``multipart/alternative`` collapsing to leave just a plain text part. This
works because many mail readers that send HTML email actually send a plain
text part in the second subpart of such ``multipart/alternatives``.
While this is a good suggestion for plain text-only mailing lists, often a
mail reader will send only a ``text/html`` part with no plain text
alternative. in this case, the site administer can enable ``text/html`` to
``text/plain`` conversion by defining a conversion command. A list
administrator still needs to enable such conversion for their list though.
>>> mlist.convert_html_to_plaintext = True
By default, Mailman sends the message through lynx, but since this program is
not guaranteed to exist, we'll craft a simple, but stupid script to simulate
the conversion process. The script expects a single argument, which is the
name of the file containing the message payload to filter.
::
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: text/html
... MIME-Version: 1.0
...
... <html><head></head>
... <body></body></html>
... """)
>>> from mailman.handlers.tests.test_mimedel import dummy_script
>>> with dummy_script():
... process(mlist, msg, {})
>>> print(msg.as_string())
From: aperson@example.com
MIME-Version: 1.0
Content-Type: text/plain
X-Content-Filtered-By: Mailman/MimeDel ...
<BLANKLINE>
Converted text/html to text/plain
Filename: ...
<BLANKLINE>
Discarding empty parts
======================
Similarly, if after filtering a multipart section ends up empty, then the
entire multipart is discarded. For example, here's a message where an inner
``multipart/mixed`` contains two jpeg subparts. Both jpegs are filtered out,
so the entire inner ``multipart/mixed`` is discarded.
::
>>> msg = message_from_string("""\
... From: aperson@example.com
... Content-Type: multipart/mixed; boundary=AAA
...
... --AAA
... Content-Type: multipart/mixed; boundary=BBB
...
... --BBB
... Content-Type: image/jpeg
...
... xxx
... --BBB
... Content-Type: image/jpeg
...
... yyy
... --BBB---
... --AAA
... Content-Type: multipart/alternative; boundary=CCC
...
... --CCC
... Content-Type: text/html
...
... <h2>This is a header</h2>
...
... --CCC
... Content-Type: text/plain
...
... A different message
... --CCC--
... --AAA
... Content-Type: image/gif
...
... zzz
... --AAA
... Content-Type: image/gif
...
... aaa
... --AAA--
... """)
>>> with dummy_script():
... process(mlist, msg, {})
>>> print(msg.as_string())
From: aperson@example.com
Content-Type: multipart/mixed; boundary=AAA
X-Content-Filtered-By: Mailman/MimeDel ...
<BLANKLINE>
--AAA
MIME-Version: 1.0
Content-Type: text/plain
<BLANKLINE>
Converted text/html to text/plain
Filename: ...
<BLANKLINE>
--AAA
Content-Type: image/gif
<BLANKLINE>
zzz
--AAA
Content-Type: image/gif
<BLANKLINE>
aaa
--AAA--
<BLANKLINE>
Passing MIME types
==================
XXX Describe the pass_mime_types setting and how it interacts with
``filter_mime_types``.
|