[Mailman-Developers] MimeDel.py: after filtering, munge text/plain's back together

Dan Mick dmick@utopia.West.Sun.COM
Wed, 15 May 2002 21:15:36 -0700 (PDT)


I've been playing with MimeDel (Content filtering), and have 
come to the conclusion that two things need to happen:

1) there needs to be a way to set "only allow text" or "only
allow text/plain" without enumerating each type you *don't* want,
and 

2) at the end of filtering, there needs to be a check to see if
all the remaining parts can profitably be recombined into
one part, at least for text/plain.

Here's a hack at doing #2 (so that, for instance, messages
with HTML alternatives end up being one big text/plain message,
even with headers and footers turned on).

#1 takes a new attribute and some GUI stuff, so I'll try to sell
that back to Barry directly.

Note: this diff is hand-hacked (as I have my partial #1 
solution in there too) so you may have to hand-apply this
if you want to play with it.

Comments welcome.




Index: MimeDel.py
===================================================================
RCS file: /cvsroot/mailman/mailman/Mailman/Handlers/MimeDel.py,v
retrieving revision 2.1
diff -u -r2.1 MimeDel.py
--- MimeDel.py  18 Apr 2002 20:46:53 -0000      2.1
+++ MimeDel.py  16 May 2002 04:13:32 -0000

@@ -91,12 +92,16 @@
     # Now perhaps convert all text/html to text/plain
     if mlist.convert_html_to_plaintext and mm_cfg.HTML_TO_PLAIN_TEXT_COMMAND:
         changedp += to_plaintext(msg)
+    # Now, scan through all parts, and if only text/plain left, fuse
+    # them all back together into one payload
+    collapse_textplain(msg)
+
     if changedp:
         msg['X-Content-Filtered-By'] = 'Mailman/MimeDel %s' % VERSION
 

@@ -138,6 +146,23 @@
             newpayload.append(subpart)
     msg.set_payload(newpayload)

+def collapse_textplain(msg):
+    if not msg.is_multipart():
+        return
+    for subpart in msg.get_payload():
+        if subpart.get_type() != 'text/plain':
+           return
+    # ok, all parts are text/plain; munge 'em together
+    newpayload = ""
+    for subpart in msg.get_payload():
+       if len(newpayload):
+           newpayload += '\n'
+        newpayload += subpart.get_payload()
+    if newpayload[-1] != '\n':
+        newpayload += '\n'
+    msg.set_payload(newpayload)
+    msg.set_type("text/plain")