#! /bin/sh -e

#
# $Id: lr_processmail,v 1.26 2002/01/11 13:31:40 vanbaal 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.
#

#
# This extracts the body from email
#

PROGRAM=lr_processmail
id=${LR_ID:-UNSET}
tag="all all $id $PROGRAM"

if test $# -lt 2
then
        echo >&2 "$tag err please give at least 2 args, indicating superservice, service"
        exit 1
fi

echo >&2 "$tag info started with $@"

SUPERSERVICE=$1; shift
SERVICE=$1; shift

flags="$@"

tag="$SUPERSERVICE $SERVICE $id $PROGRAM"

if test -n "$TMPDIR"
then
    newfile=$TMPDIR/$PROGRAM.$SERVICE.$id.$$.mail
else
    echo >&2 "$tag err aborted, TMPDIR not set. did you source etc/defaults?"
    exit 1
fi

if touch $newfile 2>/dev/null
then
    :
else
    echo >&2 "$tag err aborted, cannot create $newfile"
    exit 1
fi

cat > $newfile

echo >&2 "$tag info processing $newfile"
echo >&2 "$tag info running lr_getbody"

# this sets lr_getbody_BODYFILE, lr_getbody_SUBJECTFILE
# and lr_getbody_SUBMITTER
eval `lr_getbody -i $id -f $newfile`

echo >&2 "$tag info lr_getbody gave BODYFILE '$lr_getbody_BODYFILE'"
echo >&2 "$tag info lr_getbody gave SUBJECTFILE '$lr_getbody_SUBJECTFILE'"
echo >&2 "$tag info lr_getbody gave SUBMITTER '$lr_getbody_SUBMITTER'"

bodyfile="$lr_getbody_BODYFILE"
subjectfile="$lr_getbody_SUBJECTFILE"
submitter="$lr_getbody_SUBMITTER"

if test -z "$bodyfile"
then
    echo >&2 "$tag err extracting body failed"
    exit 1
fi

echo >&2 "$tag info inflating body"
inflatedbody=`lr_inflate $bodyfile`
if test -z "$inflatedbody"
then
    echo >&2 "$tag err inflating body failed"
    exit 1
fi

# get evil stuff out of subjectfile
sed 's/[^-_.:a-zA-Z0-9 ]/_/g' $subjectfile > $TMPDIR/$PROGRAM.$$.tmp
head -1 $TMPDIR/$PROGRAM.$$.tmp > $subjectfile
rm $TMPDIR/$PROGRAM.$$.tmp

if grep ^anon $subjectfile >/dev/null
then
    anon='-a'
    subject=`cut -d ' ' -f2- $subjectfile`
else
    anon=''
    subject=`cat $subjectfile`
fi

if test -n "$subject"
then
    # submitter - email address of sender of logfile
    # extid - a string the submitter has given us to track his
    #   submission, e.g. hostname
    # flags - e.g. common

    LR_EXTID=`echo $subject | sed 's/ /_/g; s/\.\./__/g'`
else
    LR_EXTID=UNSET
fi

export LR_EXTID

if test -n "$ARCHIVE"
then
    if lr_db_store $id extid "$LR_EXTID"
    then
        :
    else
        echo >&2 "$tag crit cannot lr_db_store $id extid $LR_EXTID, exiting"
        exit 1
    fi
fi

if test -n "$subject"
then
    lr_log2mail $anon -s "$subject" $SUPERSERVICE $SERVICE "$submitter" \
      $flags < $inflatedbody
else
    lr_log2mail $anon $SUPERSERVICE $SERVICE "$submitter" \
      $flags < $inflatedbody
fi

if test -n "$ARCHIVE"
then
    # go store $newfile in the archive

    # first find out how the archived file should get named

    TMPFILE=$TMPDIR/$PROGRAM.$SERVICE.$id.$$.time_span
    if lr_db_fetch $id time_span > $TMPFILE
    then
        echo >&2 "$tag info lr_db_fetch $LR_ID timespan succeeded"
        LR_TIME=`cat $TMPFILE`
        rm $TMPFILE
    else
        echo >&2 "$tag crit lr_db_fetch $LR_ID timespan failed, exiting"                echo >&2 "$tag notice keeping $TMPFILE for debugging"
        exit 1
    fi

    ARCHIVEDIR=$LR_ARCHIVEDIR/email/raw/$SUPERSERVICE/$SERVICE/$LR_EXTID
    # might better be some other filename?  TODO 
    ARCHIVEFILE=$ARCHIVEDIR/$LR_TIME

    echo >&2 "$tag notice storing $newfile in $ARCHIVEFILE"
    test -d $ARCHIVEDIR || mkdir -p $ARCHIVEDIR
    mv $newfile $ARCHIVEFILE
elif test -n "$KEEP"
then
    echo >&2 "$tag notice keeping $newfile on your request. remove manually."
else
    rm $newfile
fi

echo >&2 "$tag info stopped"

