#!/usr/athena/bin/perl
#-*-Perl-*-
#
# $Id: autochecker,v 1.8 2005/05/03 15:50:00 arolfe Exp $
#
# Autochecker script for incoming news.answers mail.  
# 
# The copy of this which actually gets executed should live on
# penguin-lust.mit.edu as /rtfm/bin/autochecker, but the
# RCS-controlled source is $NADIR/autochecker.  I.e., don't modify the
# former directly, modify the latter and then install it onto local
# disk.
# 
# This script uses /rtfm/faq_archiver/save-faq, which
# is similarly RCS-controlled; it lives in $PPDIR/src/save-faq.
# 
# Using files on local disk have the advantage that if AFS goes down,
# bounces won't get generated by the auto-checker.  Right now, bounces
# do get generated because file-message will fail, but file-message
# could conceivably be rewritten to queue rather than fail immediately.

$SENDMAIL="/usr/lib/sendmail";
$BINDIR="/bin";
$PROCMAILDIR="/rtfm/bin";
$ERROR_ADDR = "news-answers-request@mit.edu";
$ARCHIVE =  "archive";
$ARCHIVESPAM =  "${ARCHIVE}-spam";

#$SAVEFAQ = "/afs/sipb.mit.edu/project/periodic-postings/src/save-faq";
# use the local file unless debugging save-faq
$SAVEFAQ = "/rtfm/faq_archiver/save-faq";
$SAVEFAQARGS = "-submitted -other_moderated -trim_mail_headers -quiet";
$SAVEFAQFAIL = 255;
# in a system call, perl returns exit status as given by wait() 
$SYSERRMULT = 256;

$warn = "";

# Slurp files whole
undef $/;

# read file in from standard input
$file = <STDIN>;

# write it to a temporary file
open (TMPFILE, "> /tmp/autocheck-in.$$")
    || &die ($warn,"Could not open /tmp/autocheck-in.$$ for writing:\n$!\n");
print (TMPFILE "$file");
close(TMPFILE);

# send it to the archive
$error = system("$PROCMAILDIR/file-message.pl -ham $ARCHIVE -spam $ARCHIVESPAM -home /var/mailspool/news-answers-submit -keytab /etc/keytab-discuss-pl -principal discuss/penguin-lust.mit.edu < /tmp/autocheck-in.$$");
$warn .= "Could not run file-message to save to archive:\n$!\n" if $error;

# run save-faq
$error = system("$SAVEFAQ $SAVEFAQARGS < /tmp/autocheck-in.$$ > /tmp/autocheck-out.$$");

if ($error && ($error != $SAVEFAQFAIL * $SYSERRMULT)) {
    &die ($warn,"Could not execute $SAVEFAQ $SAVEEFAQARGS:\n$!\n");
}

# Open pipe to sendmail process
open(MAILPIPE, "| $SENDMAIL -t -oi -fauto-replier-daemon@penguin-lust.mit.edu")
  || &die ($warn,"Could not execute $SENDMAIL for reply:\n$!\n");

# Generate reply mail headers
open(FORMAIL, "$BINDIR/cat /tmp/autocheck-in.$$ | $PROCMAILDIR/formail -rt -A 'From: auto-replier-daemon@penguin-lust.mit.edu (do not reply to this address)' -A 'Comment: send comments to news-answers-request@MIT.EDU' -A 'Precedence: junk' -A 'Bcc: pgreene@mit.edu' |")
  || &die ($warn,"Could not execute $PROCMAILDIR/formail for reply:\n$!\n");

# Write out reply mail headers
while ($data = <FORMAIL>) {
  print(MAILPIPE "$data");
}

close(FORMAIL);

open(REPLY_TEXT, "/tmp/autocheck-out.$$")
  || &die ($warn,"Could not open file autocheck-out.$$ for reading:\n$!\n");

# write out reply from save-faq
while ($data = <REPLY_TEXT>) {
    print (MAILPIPE "$data");
}
close (REPLY_TEXT);

# Close pipe to sendmail process (and send mail)
close(MAILPIPE);

if (! $error) {
# Open pipe to sendmail process
    open(MAILPIPE, "| $SENDMAIL -t -oi -fauto-replier-daemon@penguin-lust.mit.edu")
	|| &die ("Could not execute $SENDMAIL for queueing:\n$!\n");

# Generate forwarded mail
    open(FORMAIL, "$BINDIR/cat /tmp/autocheck-in.$$ | $PROCMAILDIR/formail -i 'Subject: [checked]' -i 'To: news-answers-request@mit.edu' -i 'Cc' -i 'Bcc' |")
	|| &die ("Could not execute $FORMAIL for queueing:\n$!\n");

# Write out forwarded mail headers and file
    while ($data = <FORMAIL>) {
	print(MAILPIPE "$data");
    }

    close(FORMAIL);

# Close pipe to sendmail process (and send mail)
    close(MAILPIPE);
}

# Clean up /tmp
$file_count = unlink("/tmp/autocheck-in.$$");
if ($file_count == 0) {
  $warn .= "Deletion of temporary file /tmp/autocheck-in.$$ failed.\n";
}

$file_count = unlink("/tmp/autocheck-out.$$");

if ($file_count == 0) {
  $warn .= "Deletion of temporary file /tmp/autocheck-out.$$ failed.\n";
}

if ($warn) {
    &email_warning($warn);
}

sub die {
    local ($warning,$error) = @_;

# also report any warnings that have accumulated so far
    &email_warning($warning . $error);
    die "$0: $warning $error\n";
}

sub email_warning {
    local ($error) = @_;

    open(MAILPIPE, "|$BINDIR/mail $ERROR_ADDR") || die "Running /bin/mail: $!\n";

    print MAILPIPE "Subject: Error report from $0\nTo: $ERROR_ADDR\n\n";
    print MAILPIPE "Error report from $0\n\n$error\n";

    close(MAILPIPE);
}
