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

Dan Mick dmick@utopia.West.Sun.COM
Tue, 21 May 2002 12:43:04 -0700 (PDT)


I like this version for #2 much better; it handles any payload
type (at least theoretically) with the desirable text/plain as
a side effect.  I'm not happy about the \n hack but I don't
see a way around it.

Again, this is a manually-hacked diff, but now it's only one
block insertion, so should be easier to play with.  Comments
still welcome.

The syslog() is, of course, optional.  I left the nested if()'s for
ease of debugging.

@@ -91,12 +92,27 @@
     # 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)
+
+    # If we're left with only two parts, an empty body and one attachment,
+    # recast the message to one of just that part
+
+    if len(msg.get_payload()) == 2:
+        if msg.get_payload(0).get_payload() == '':
+            type = msg.get_payload(1).get_type()
+            syslog('post', "collapsing message %s type %s", 
+                msg['Message-ID'], type)
+            newpayload = msg.get_payload(1).get_payload()
+            if type == 'text/plain' and newpayload[-1] != '\n':
+                newpayload += '\n'
+            msg.set_payload(newpayload)
+            msg.set_type(type)
+
     if changedp:
         msg['X-Content-Filtered-By'] = 'Mailman/MimeDel %s' % VERSION


> 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")
>  
>  
> 
> 
> 
> _______________________________________________
> Mailman-Developers mailing list
> Mailman-Developers@python.org
> http://mail.python.org/mailman-21/listinfo/mailman-developers