#! /bin/sh -e

#
# $Id: lr_getbody,v 1.55 2001/10/27 23:42:20 flacoste Exp $
#
# Copyright (C) 2000-2001 Stichting LogReport Foundation LogReport@LogReport.org
# 
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
# 
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program (see COPYING); if not, check with
#     http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

#
# lr_getbody - get body from mail on stdin to file. echo filename, a space,
#      and sender address to stdout. sender address gets extracted from
#      message headers
#

PROGRAM=lr_getbody

identifier="none"
file=
while getopts :f:i: a
do
    case $a in
        f) file=$OPTARG
            ;;
        i) identifier=$OPTARG
            ;;
        *) echo >&2 "$tag err usage $PROGRAM -f file -i identifier"
           exit 1
            ;;
    esac
done

tag="all all $identifier $PROGRAM"

echo >&2 "$tag info started with '$*'"

if test "$identifier" = "none"
then
    echo >&2 "$tag err aborted, no identifier found (use -i)"
    exit 1
fi

if test ! -f "$file"
then
    echo >&2 "$tag err aborted, no readable inputfile specified (use -f)"
    exit 1
fi

if test -z "$TMPDIR"
then
    echo >&2 "$tag err TMPDIR not set. did you source etc/defaults?"
    exit 1
fi

if test ! -d "$TMPDIR"
then
    if test -f "$TMPDIR"
    then
        echo >&2 "$tag err $TMPDIR is a file, it should be a dir, aborting"
        exit 1
    else
        echo >&2 "$tag info creating $TMPDIR"
        mkdir $TMPDIR
    fi
fi

#
# we try to implement RFC 822, 4.4.4 policy
#

echo >&2 "$tag info extracting sender, running lr_smtpfield"

REPLYTO=`lr_smtpfield Reply-To < $file || true`
FROM=`lr_smtpfield From < $file || true`
SENDER=`lr_smtpfield Sender < $file || true`

SUBJECTFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.subject
if lr_smtpfield Subject < $file > $SUBJECTFILE
then
    echo >&2 "$tag info subjectfile $SUBJECTFILE"
else
    echo >&2 "$tag err cannot run lr_smtpfield Subject < $file > $SUBJECTFILE"
    echo >&2 "$tag notice keeping $SUBJECTFILE for debug"
    exit 1
fi

if [ ! -z "$REPLYTO" ]; then
    SUBMITTER="$REPLYTO"
elif [ ! -z "$FROM" ]; then
    SUBMITTER="$FROM"
elif [ ! -z "$SENDER" ]; then
    SUBMITTER="$SENDER"
else
    echo >&2 "$tag err sender unknown"
    exit 1
fi

# strip leading < and trailing >
# BEWARE! there exist unix vendors who still ship a 
# /bin/sh which
# does not know ${a%.*}. Sun is one of those.
#

# this removes embedded newlines, and leadind and trailing whitespace
# quote single quote
SUBMITTER=`echo $SUBMITTER | sed -e "s|'|'\"'\"'|g"`
echo >&2 "$tag info submitter $SUBMITTER"

# make it possible to eval what we're gonna print to stdout.
# SUBMITTER=`echo $SUBMITTER | sed 's/(/\\\(/g'`
# SUBMITTER=`echo $SUBMITTER | sed 's/)/\\\)/g'`

# Extract the body from the mail message.
# Use munpack if it is a MIME message.
# BUT : We don't use munpack if it is a single part text message
# since munpack doesn't work with single part text message.
CONTENT_TYPE=`lr_smtpfield Content-Type < $file || true`
echo >&2 "$tag info content-type $CONTENT_TYPE"
ENCODING=`lr_smtpfield Content-Transfer-Encoding < $file | grep -i "quoted" || true`
echo >&2 "$tag info content-transfer-encoding $ENCODING"
IS_TEXT=`echo $CONTENT_TYPE | grep -i "text" || true`
if test -n "$ENCODING" || test -n "$CONTENT_TYPE" -a -z "$IS_TEXT" 
then
    echo >&2 "$tag info $file is an encoded MIME message"
    cd $TMPDIR
    echo >&2 "$tag info munpacking $file"
    # FIXME: When there are multiple parts, we only take the 
    # last one. This seems logical, but may not always work.
    # munpack is from mpack ( ftp://ftp.andrew.cmu.edu/pub/mpack/ )
    munpack -t -q $file | 
        while read a b
        do
            extension=`echo $a | sed -e 's/.*\.//'`
            BODYFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.$extension
            mv $TMPDIR/$a $BODYFILE
            echo >&2 "$tag info bodyfile $BODYFILE"
            echo "${PROGRAM}_BODYFILE='$BODYFILE'"
            echo "${PROGRAM}_SUBJECTFILE='$SUBJECTFILE'"
            echo "${PROGRAM}_SUBMITTER='$SUBMITTER'"

            base=`basename $a .$extension`
            rm -f $TMPDIR/$base.desc
        done
else
    echo >&2 "$tag info $file is plain message"
    BODYFILE=$TMPDIR/${PROGRAM}_${identifier}_$$.txt
    body="no"
    cat $file |
        while read line
        do
            if [ -z "$line" ]
            then
                body="yes"
            elif [ "$body" = "yes" ]
            then
                echo $line >> $BODYFILE
            fi
        done

    # just in case body was empty: still create the file
    touch $BODYFILE

    echo "${PROGRAM}_BODYFILE='$BODYFILE'"
    echo "${PROGRAM}_SUBJECTFILE='$SUBJECTFILE'"
    echo "${PROGRAM}_SUBMITTER='$SUBMITTER'"
    echo >&2 "$tag info bodyfile $BODYFILE"
fi

echo >&2 "$tag info stopped"

