procmail
[Top] [All Lists]

Re: Filtering out unwanted mime attachments

1997-10-16 08:54:03
Excerpts from: (16-Oct-97) Re: Filtering out unwanted mime attachments by J 
Daniel Smith
This seems to be difficult, if not impossible in procmail because
(as David pointed out) MATCH is greedy, thus the last $boundary
matches, not the first one.  Resorting "sed" seems to be the only
solution...unless there is some way to get procmail to parse/match one
line at a time.

This sounds like a job for ... recursive INCLUDERCs!

Put something like this in your .procmailrc:
#--------------------------------------------------------------------------
LINEBUF = 100000

:0B
* $ < ${LINEBUF}
* ^^\/(.*$)+
{
     BODYLINES = $MATCH

     INCLUDERC = parse-line-by-line.rc
}
:0E
{
     # message too large
}
#--------------------------------------------------------------------------

Then, in parse-line-by-line.rc file:

#--------------------------------------------------------------------------
:0
* BODYLINES ?? ^^(.*$)\/(.*$)+
{ REMAININGLINES = $MATCH }

:0E
{ REMAININGLINES }

:0
* BODYLINES ?? ^^\/.*$
{ THISLINE = $MATCH }

:0E
{ THISLINE }

:0
* THISLINE ?? parse.this.line
{
     # do whatever you want with this line
}

# Now recurse if there are any remaining lines.
:0
* REMAININGLINES ?? .
{
     BODYLINES = $REMAININGLINES

     INCLUDERC = $_
}
#--------------------------------------------------------------------------

You can even disassemble and then re-assemble the message using this generic
technique by doing something like

     NEWBODY = "$NEWBODY$THISLINE"

Stick in a global variable to act as a flag whenever THISLINE matches the
MIME multipart boundary and you're pretty much done.

The exact solution is left as an exercise for the reader.

Later,
Ed