procmail
[Top] [All Lists]

Re: Decoding MIME Update

2000-04-06 04:26:34
Hello Martin,

        Glad to hear that you have tried it.  Yes I have made some
improvements.  It did not handle case nor muliple ISO-8859-1 fileds in
Subject and From headers.  I even allow for ISO8859-1, since I get a
lot of those, broken - I suppose, headers.

        Does anyone know if back-quoted scripts need to be waited
upon, or is it handled internally by procmail.

        The only other problem is non-standard MIME-encodings, where
there is only ONE multipart section listed as text, but it itself is
broken down.  Mimencode barfs on this.  I have not fixed that.

        Suggestions are welcome.

        Lastest source attached at end.

        --Ralph

# @(#) pm-jamime-decode.rc -- decode MIME body contents; quoted-printable, 
base64
# @(#) $Id: pm-jamime-decode.rc,v 1.2 1998/03/30 09:49:09 jaalto Exp $
#
#   File id
#
#       .Copyright (C)  1998 Jari Aalto
#       .$Maintainer:   Jari Aalto <jari(_dot_)aalto(_at_)poboxes(_dot_)com> $
#       .$Created:      1998-01 $
#       .$Keywords:     procmail recipe $
#
#       This code is free software in terms of GNU Gen. pub. Lic. v2 or later
#       You can get newest version by sending email to maintainer with
#       subject "send <FILENAME>"
#
#   Documentation
#
#       The original father of the decoding scheme used here was
#       presented by Peter Galbraith 
<galbraith(_at_)mixing(_dot_)qc(_dot_)dfo(_dot_)ca> in
#       procmail mailing list somewhere at the end of 1997.
#
#       This includerc supposes that the header has MIME header
#       Content-Type: text/plain and performs qp or base64 decoding
#       on the whole message. Note, that if you receive messages that
#       have many mime attachements, then this recipe is not suitable
#       for it.
#
#       Procmail is not designed to handle mime attachements and this
#       recipe only applies to whole _body_.
#
#   Required settings
#
#       PMSRC must point to source direcry of procmail code. This subroutine
#       will include
#
#       o   pm-javar.rc
#
#   Call arguments (variables to set before calling)
#
#       (none)
#
#   Return values
#
#       (none)
#
#   Examples
#
#       Instead of testing the existense of text/plain in the body,
#       you can force decoding by settings JA_MIME_DECODE_REGEXP to
#       ".*".
#
#           RC_MIME_DECODE = $PMSRC/pm-jamime-decode.rc
#
#           :0
#           * condition
#           { JA_MIME_DECODE_REGEXP = ".*" }
#
#           INCLUDERC = $RC_MIME_DECODE     # call subroutine.
#
#
#   Change Log 
# 00/03/24 - RpS: Adapted from Jari Alto's pm-jamime-decode, handles
# encoded Subject and From headers, as well as multipart with only ONE
# part.
# 00/03/30 - RpS: Allowed for multiple =?iso-8859-[1-9]?Q?.*?=
# fields.  Also eliminated QP embedded CRs and LFs in Subject and From
# headers.

# .................................................... &initialising ...

dummy = "init: pm-jamime-decode.rc Reading variable definitions if needed"

SHELL=/bin/sh

# :0
# * !  WSPC ?? ( )
# { INCLUDERC = $PMSRC/pm-javar.rc }

:0
* !  MIME_VER ?? [0-9]
{
    INCLUDERC = $PMSRC/pm-jamime.rc
}

# .......................................................... &public ...
# User configurable sections

JA_MIME_DECODE_REGEXP = ${JA_MIME_DECODE_REGEXP:-\
"^Content-Type: *text/(plain|enriched)"}

# ........................................................... &do-it ...
#   Run conversion if it was quoted printable.
#   Also reflect correct MIME header

dummy = "pm-jamime-decode.rc: handle quoted printable"

:0 D
* ^Subject:\/.*=\?iso-?8859.*\?=.*
{
        SUBJECT = $MATCH
        clean_Subject = `echo "$SUBJECT" |                            \
                sed -e 's/=?iso-\{0,1\}8859-[1-9]?Q?\([^?]*\)?=/\1/g' \
                        -e 's/=0D//g' -e 's/=0A//g' |                 \
                $MIME_BIN_QP `
        SUBJECT_DO_IT = "yes"
}

:0 ED
* ^Subject:\/.*=\?ISO-?8859.*\?=.*
{
        SUBJECT = $MATCH
        clean_Subject = `echo "$SUBJECT" |                            \
                sed -e 's/=?ISO-\{0,1\}8859-[1-9]?Q?\([^?]*\)?=/\1/g' \
                        -e 's/=0D//g' -e 's/=0A//g' |                 \
                $MIME_BIN_QP `
        SUBJECT_DO_IT = "yes"
}

:0 D
* ^From:\/.*=\?iso-?8859.*\?=.*
{
        FROM = $MATCH
        clean_From = `echo "$FROM" |                                  \
                sed -e 's/=?iso-\{0,1\}8859-[1-9]?Q?\([^?]*\)?=/\1/g' \
                        -e 's/=0D//g' -e 's/=0A//g' |                 \
                $MIME_BIN_QP `
        FROM_DO_IT = "yes"
}

:0 ED
* ^From:\/.*=\?ISO-?8859.*\?=.*
{
        FROM = $MATCH
        clean_From = `echo "$FROM" |                                  \
                sed -e 's/=?ISO-\{0,1\}8859-[1-9]?Q?\([^?]*\)?=/\1/g' \
                        -e 's/=0D//g' -e 's/=0A//g' |                 \
                $MIME_BIN_QP `
        FROM_DO_IT = "yes"
}

#   Prevent calling sh -c here. This speeds up procmail
#
jaMimeDecodeShellmetas = $SHELLMETAS
SHELLMETAS

:0 fhw
* SUBJECT_DO_IT ?? ^^yes^^
* FROM_DO_IT ?? ^^yes^^
| $FORMAIL                                                         \
        -i "Subject:$clean_Subject"                                \
        -i "From:$clean_From"                                      \
        -I "X-Mime-Header-Decoded: $MIME_TYPE"

:0E fhw
* SUBJECT_DO_IT ?? ^^yes^^
| $FORMAIL                                                         \
        -i "Subject:$clean_Subject"                               \
        -I "X-Mime-Header-Decoded: $MIME_TYPE"

:0E fhw
* FROM_DO_IT ?? ^^yes^^
| $FORMAIL                                                          \
        -i "From:$clean_From"                                      \
        -I "X-Mime-Header-Decoded: $MIME_TYPE"


:0 fbw i
* MIME_BOUNDARY_COUNT ?? ^^2^^
| $AWK                                                              \
  ' BEGIN {cont=0} {                                                \
        if (cont > 0) {cont=0; next}                                \
        if (match($0, BOUNDARY) > 0 ) { next }                      \
        if ( match($0, "^Content-") > 0 ) {                         \
                if (match($0, ";$") > 0) {cont=1}                   \
                next}                                               \
        print;                                                      \
    }                                                               \
  ' BOUNDARY="$MIME_BOUNDARY"                                       \

:0 afbw
| $MIME_BIN_QP

:0 afhw
| $FORMAIL                                                          \
        -I "Content-Type: text/plain"                               \
        -I "Mime-Version: $MIME_VER"                                \
        -I "X-Mime-Type-Decoded: $MIME_TYPE"

:0
*$ $JA_MIME_DECODE_REGEXP
{
    :0 fbw
    * ^Content-Transfer-Encoding: *quoted-printable
    | $MIME_BIN_QP

        :0 Afhw
        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"

    :0 fbw
    * ^Content-Transfer-Encoding: *base64
    | $MIME_BIN_64

        :0 Afhw
        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"
}

# $MIME_BIN_QP does not handle separate MIME sections

#:0 EB
#*$ $JA_MIME_DECODE_REGEXP
#{
#    :0 fbw
#    * MIME_B_QP ?? yes
#   * B ?? ^Content-Transfer-Encoding: *quoted-printable
#    | sed -e "'""\\|$JA_MIME_DECODE_REGEXP|,\\|$MIME_BOUNDARY|p""'"

#       :0 Afbw
#       | $MIME_BIN_QP

#        :0 Afbw
#        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"

#    :0 fbw
#    * B ?? ^Content-Transfer-Encoding: *base64
#    | $MIME_BIN_64

#        :0 Afbw
#        | $FORMAIL -I "Content-Transfer-Encoding: 8bit"
#}

SHELLMETAS  = $jaMimeDecodeShellmetas
dummy       = "subroutine: pm-jamime-decode.rc end."

#:0:
#test

# End of file pm-jamime-decode.rc
<Prev in Thread] Current Thread [Next in Thread>