#! /bin/sh -e

#
# This extracts the body from email
#

PROGRAM=lr_processmail
if test -z "$LR_ID"
then
    LR_ID=`lr_tag || echo UNSET`
fi
tag="all all $LR_ID $PROGRAM"

if test $# -eq 0
then
    echo >&2 "$tag err please give 1 arg, indicating service"
    exit 1
fi

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

LR_SERVICE=$1; shift

tag="all $LR_SERVICE $LR_ID $PROGRAM"

if test -n "$TMPDIR"
then
    newfile=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_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

if test -z "$LR_GZIP"
then
    echo >&2 "$tag err gzip(1) isn't available"
    exit 1
elif ! test -x "$LR_GZIP"
then
    echo >&2 "$tag err $LR_GZIP isn't executable"
    exit 1
fi

if /usr/bin/perl -MMIME::Tools -e 'exit 0' 2>/dev/null
then
    :
else
    cat >&2 <<EOF
$tag err can't parse email: the needed MIME::Entity perl module isn't available
$tag err Please install the MIME::Tools perl modules from a local CPAN mirror.
$tag err Consult the Lire User's Manual for more informations.
EOF
    #' Fontification hack
    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 $newfile || true`

# we use lr_getbody_SUBMITTER _unquoted_.  This variable is constructed
# using MIME::Tools, so is reasonably trusted.  We should allow for
# embedded spaces, separating multiple addresses to sent the report to.
if test -z "$lr_getbody_OK"
then
    echo >&2 "$tag warning lr_getbody didn't completed successfully"
    if test -n "$lr_getbody_SUBMITTER"
    then
	echo >&2 "$tag err sending error message to $lr_getbody_SUBMITTER"
	errfile="$TMPDIR/$PROGRAM.$LR_ID.$$.errors"
	{ cat <<EOF
FATAL ERROR: We failed to parse your message.

There was an encoding problem with your message. Make sure that you
send your log file using a MIME compliant mail reader.

Contact the administrator of this responder for more help.

EOF
	} > $errfile
	lr_mail -s "[LogReport] Error in $LR_SERVICE report (was: $lr_getbody_SUBJECT)" text/plain $errfile $lr_getbody_SUBMITTER || echo >&2 "lr_mail failed"
	exit 1
    else
	echo >&2 "$tag err No submitter can be found. Keeping $newfile for debugging"
	exit 1
    fi
else
    echo >&2 "$tag info lr_getbody gave FILE '$lr_getbody_FILE'"
    echo >&2 "$tag info lr_getbody gave SUBJECT '$lr_getbody_SUBJECT'"
    echo >&2 "$tag info lr_getbody gave SUBMITTER '$lr_getbody_SUBMITTER'"
fi

bodyfile="$lr_getbody_FILE"
if test -z "$bodyfile"
then
    # Empty body
    bodyfile="$TMPDIR/$PROGRAM.$LR_ID.$$.body"
    touch $bodyfile
fi
subject="$lr_getbody_SUBJECT"
submitter="$lr_getbody_SUBMITTER"

output_flag=
if echo "$subject" | grep ^anon >/dev/null
then
    output_flag='-o xml'
    subject=`echo "$subject" | cut -d ' ' -f2-`
fi

# Sets LR_EXTID to an encoded form of the sender address
# Only keep alphanumeric characters, dot, hyphen, dash, plus and at
# Lowercase the email
LR_EXTID=`echo "$submitter" | tr -cd "a-zA-Z0-9._@+-" | tr A-Z a-z | sed -e 's/^\.\+//'`
export LR_EXTID

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

if test -n "$subject"
then
    lr_log2mail $output_flag -s "$subject" $LR_SERVICE $submitter < $bodyfile
else
    lr_log2mail $output_flag $LR_SERVICE $submitter < $bodyfile
fi

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

    # due to the way the archive is set up, we need to know this services'
    # superservice.  hrm...

    # we assume service is valid, since we're being called via an online
    # responder: these (should) have current mailbox names only
    LR_SUPERSERVICE=`lr_check_superservice $LR_SERVICE`

    ARCHIVEDIR=$LR_ARCHIVEDIR/email/raw/$LR_SUPERSERVICE/$LR_SERVICE/$LR_EXTID
    ARCHIVEFILE=$ARCHIVEDIR/$LR_ID.msg.gz

    echo >&2 "$tag notice storing compressed $newfile in $ARCHIVEFILE"
    test -d $ARCHIVEDIR || mkdir -p $ARCHIVEDIR
    $LR_GZIP --best -c $newfile > $ARCHIVEFILE
fi

if test -n "$LR_KEEP"
then
    echo >&2 "$tag notice keeping $newfile and $bodyfile on your request. remove manually."
else
    rm $newfile $bodyfile
fi

echo >&2 "$tag info stopped"

exit 0

POD=<<'EOPOD'


=pod

=head1 NAME

B<lr_processmail> - read an email message and invoke lr_log2mail(1)

=head1 SYNOPSIS

B<lr_processmail> I<service>

=head1 DESCRIPTION

B<lr_processmail> reads an email message from STDIN, parses the message
using lr_getbody(1) and invokes lr_log2mail(1) to process the extracted 
log file.

The environment variable LR_ID is used as an identifier, which shows up
in debug messages, and in names of temporary files, so that Lire jobs
can get tracked.  A Lire job currently is identified with one email
message or log file.

This script is invoked by lr_spool(1).

=head1 EXAMPLE

 $ lr_processmail combined < /var/spool/lire/done/985327208.21242.mailhost

=head1 SEE ALSO

lr_spool(1), documentation in the Lire User Manual

=head1 VERSION

$Id: lr_processmail.in,v 1.10 2002/08/18 12:58:51 vanbaal Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 2002 Stichting LogReport Foundation LogReport@LogReport.org

This file is part of Lire.
 
Lire 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.

=head1 AUTHOR

Joost van Baal <joostvb@logreport.org>, based on an idea by Edwin Groothuis

=cut

EOPOD


