#! /bin/sh -e

PROGRAM=lr_processmail

# dereference sysconfdir's prefix dependency
prefix="/usr"
etcdir="/etc/lire"
. $etcdir/defaults

if test -z "$LR_ID"
then
    LR_ID=`lr_tag || echo UNSET`
fi

if test $# -eq 0
then
    lire_log "err please give 1 arg, indicating service"
    exit 1
fi

lire_log "info started with $@"

LR_SERVICE=$1; shift

newfile=`lr_tempfile $PROGRAM.$LR_SERVICE.$LR_ID.XXXXXX.mail`
lr_mark_for_cleanup $newfile

if test -z "$gzip_path"
then
    lire_log "err gzip(1) isn't available"
    exit 1
fi

if /usr/bin/perl -MMIME::Tools -e 'exit 0' 2>/dev/null
then
    :
else
    lire_log "err can't parse email: the needed MIME::Entity perl module isn't available"
    lire_log "err Please install the MIME::Tools perl modules from a local CPAN mirror."
    lire_log "err Consult the Lire User's Manual for more informations."
    exit 1
fi

cat >> $newfile

lire_log "info processing $newfile"
lire_log "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
    lire_log "warning lr_getbody didn't completed successfully"
    if test -n "$lr_getbody_SUBMITTER"
    then
	lire_log "err sending error message to $lr_getbody_SUBMITTER"
	errfile="`lr_tempfile $PROGRAM.$LR_ID.XXXXXX.errors`"
        lr_mark_for_cleanup $errfile
	{ 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
	lire_log "err No submitter can be found. Keeping $newfile for debugging"
	exit 1
    fi
else
    lire_log "info lr_getbody gave FILE '$lr_getbody_FILE'"
    lire_log "info lr_getbody gave SUBJECT '$lr_getbody_SUBJECT'"
    lire_log "info lr_getbody gave SUBMITTER '$lr_getbody_SUBMITTER'"
fi

bodyfile="$lr_getbody_FILE"
if test -z "$bodyfile"
then
    # Empty body
    bodyfile="`lr_tempfile $PROGRAM.$LR_ID.XXXXXX.body`"
fi
lr_mark_for_cleanup $bodyfile
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" | /usr/bin/perl -pe 'tr/a-zA-Z0-9\@._-//cd; tr/A-Z/a-z/; s/^\.+//;'`
export LR_EXTID

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_archive_dir/email/raw/$LR_SUPERSERVICE/$LR_SERVICE/$LR_EXTID
    ARCHIVEFILE=$ARCHIVEDIR/$LR_ID.msg.gz

    lire_log "notice storing compressed $newfile in $ARCHIVEFILE"
    test -d $ARCHIVEDIR || mkdir -p $ARCHIVEDIR
    $gzip_path --best -c $newfile > $ARCHIVEFILE
fi

lire_log "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.14 2004/01/21 07:23:00 wsourdeau Exp $

=head1 AUTHOR

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

=head1 COPYRIGHT

Copyright (C) 2000-2003 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.

=cut

EOPOD


