summaryrefslogtreecommitdiff
path: root/Mailman/docs/filtering.txt
blob: 67bef2f8e15cb868649fdb14cb63fa703a9162d4 (plain) (blame)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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.  It does this with the MimeDel handler module, although other
handlers can potentially do other kinds of finer level content filtering.

    >>> from Mailman.Handlers.MimeDel import process
    >>> from Mailman.configuration import config
    >>> mlist = config.db.list_manager.create(u'_xtest@example.com')
    >>> mlist.preferred_language = u'en'

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_mime_types = []
    >>> mlist.pass_mime_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.

    >>> mlist.filter_mime_types = ['image/jpeg']
    >>> # XXX Change this to an enum
    >>> mlist.filter_action = 0 # Discard
    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Content-Type: image/jpeg
    ... MIME-Version: 1.0
    ...
    ... xxxxx
    ... """)
    >>> process(mlist, msg, {})
    Traceback (most recent call last):
    ...
    DiscardMessage

However, if we turn off content filtering altogether, then the handler
short-circuits.

    >>> 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
    >>> 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.

    >>> 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
    Content-Type: multipart/mixed; boundary=BOUNDARY
    MIME-Version: 1.0
    X-Content-Filtered-By: Mailman/MimeDel ...
    <BLANKLINE>
    --BOUNDARY
    Content-Type: image/gif
    MIME-Version: 1.0
    <BLANKLINE>
    yyy
    --BOUNDARY--
    <BLANKLINE>


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.

    >>> 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
    Content-Type: multipart/mixed; boundary=BOUNDARY
    MIME-Version: 1.0
    X-Content-Filtered-By: Mailman/MimeDel ...
    <BLANKLINE>
    --BOUNDARY
    Content-Type: image/gif
    MIME-Version: 1.0
    <BLANKLINE>
    yyy
    --BOUNDARY--
    <BLANKLINE>

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_mime_types.append('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.

    >>> ignore = mlist.filter_mime_types.pop()


Conversion to plain text
------------------------

Many 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.

    >>> import os, sys
    >>> script_path = os.path.join(config.DATA_DIR, 'filter.py')
    >>> fp = open(script_path, 'w')
    >>> try:
    ...     print >> fp, """\
    ... import sys
    ... print 'Converted text/html to text/plain'
    ... print 'Filename:', sys.argv[1]
    ... """
    ... finally:
    ...     fp.close()
    >>> config.HTML_TO_PLAIN_TEXT_COMMAND = '%s %s %%(filename)s' % (
    ...     sys.executable, script_path)
    >>> msg = message_from_string("""\
    ... From: aperson@example.com
    ... Content-Type: text/html
    ... MIME-Version: 1.0
    ... 
    ... <html><head></head>
    ... <body></body></html>
    ... """)
    >>> 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--
    ... """)
    >>> 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.