procmail
[Top] [All Lists]

Re: Filtering out unwanted mime attachments

1997-10-15 02:49:38
Mike A. Harris writes on 14 October 1997 at 18:48:13
On Mon, 13 Oct 1997, Michael Stone wrote:

Michael Stone writes on 12 October 1997 at 20:58:52
But how much better is it to recieve 
"<b>Yes</b>, I <i>want to</i> come to the party."
instead of
"Yes, I want to come to the party."

No argument there.  But having a recipe such as
   :0
   * ^Content-Type: multipart
   /dev/null
[...]
filter recipe that would DELETE ALL messages that hit my inbox
that contain HTML MIME attachments.

For the general case, processing MIME completely within procmail is
difficult for both technical reasons (nesting, encodings, etc.) and
semantics (I don't think MIME would consider the HTML portion of a
multipart/alternative message to be an "attachment" although you
probably do).

Anyway, here's a *very simple* way of reducing a multipart/alternative
message to just the first part more-or-less completely within procmail
(i.e., metamail isn't needed).  I only tried a few simple test
cases...it's enough to show that MIME processing in procmail isn't
easy.
   LINEBUF=100000
   :0B
   * $> ${LINEBUF}
   {
     # message too large
   }
   :0E
   * ^Mime-Version:
   * ^Content-Type:[    ]*multipart/alternative;[       ]*boundary="?\/[^"]+
   {
     boundary=$MATCH

     # reduce the body to just the first MIME part (plus a little extra
     # garbage)
     :0B
     * $.*^--${boundary}\/(.|$)+^--${boundary}$
     { part1=$MATCH }
     :0wfib
     | echo "$part1"

     # get the Content-Type: and clean up the body a bit
     :0B
     * $.*^Content-Type:[       ]*\/[^  ]+
     { content_type=$MATCH }
     :0B
     * $.*^Content-Type:[       ]*${content_type}$\/(.|$)+^--${boundary}
     { body=$MATCH }
     :0wfib
     | echo "$body"
     :0wfi
     | formail -I "Content-Length:" -I "Content-Type: $content_type"

     # I can't figure out how to do this w/o sed :-(
     :0wfib
     | sed "s/^--${boundary}$//"
   }

   Dan
------------------- message is author's opinion only ------------------
J. Daniel Smith <DanS(_at_)bristol(_dot_)com>        
http://www.bristol.com/~DanS
Bristol Technology B.V.                   +31 33 450 50 50, ...51 (FAX)
Amersfoort, The Netherlands               {info,jobs}(_at_)bristol(_dot_)com