#!/usr/local/bin/perl
#-*-Perl-*-
#
# $Id: autoreply,v 1.11 1997/09/24 17:45:08 pgreene Exp $
#
# Autoreply script for incoming news.answers mail.  This script is
# usually spawned from procmail.
# 
# The copy of this which actually gets executed should live on
# rtfm.mit.edu as /usr/local/news.answers/bin/autoreply, but the
# RCS-controlled source is $NADIR/autoreply.  I.e., don't modify the
# former directly, modify the latter and then install it onto local
# disk on rtfm.mit.edu.
# 
# This script uses /usr/local/news.answers/etc/autoreply-text, which
# similarly is RCS-controlled as $NADIR/autoreply-text.
# 
# Using files on local disk have the advantage that if AFS goes down,
# bounces won't get generated by the auto-replier.  Right now, bounces
# do get generated because file-message will fail, but file-message
# could conceivably be rewritten to queue rather than fail immediately.
#
# 09/24/97: pgreene: modified behavior so it also includes the first
# 20 lines of the incoming message's body, in a References: block, and
# so it relies upon the procmail-rc to perform the actual mailing
# (like $NADIR/mail-reply.pl)

# These should have been inherited, but we should still double-check
# to see if they are set:

$MODERATORS = $ENV{'MODERATORS'};
if ($MODERATORS eq "") {
  die("Environmental variable MODERATORS not set.\n");
}

$MH_PATH = $ENV{'MH_PATH'};
if ($MH_PATH eq "") {
  die("Environmental variable MH_PATH not set.\n");
}

$BINDIR = $ENV{'BINDIR'};
if ($BINDIR eq "") {
  die("Environmental variable BINDIR not set.\n");
}

# set this to "fool" mh about location of +inbox
$root = '/afs/sipb.mit.edu/project/periodic-postings/news.answers';
$ENV{'HOME'} = $root;

# global variables and constants
$scan_format = "%04(putnumf(year{date}))/%02(putnumf(mon{date}))/%02(putnumf(mday{date})) %40(putstrf(friendly{from}))";

$headers = "";
while (<STDIN>) {
    $headers .= "> " . $_;
    print;
    last unless /./;
}

# Slurp files whole
#undef $/;

open(REPLY_TEXT, "/usr/local/news.answers/etc/autoreply-text")
  || die ("Could not open file autoreply-text:\n$!\n");

$reply_text = "";
while (<REPLY_TEXT>) {
    $reply_text .= $_;
}

close(REPLY_TEXT);

# slurp them line-by-line again (CR = 13, octal 015)
#$/ = 015;

# Determine current data for variable substitution
@date_array = localtime(time);
# month, day and year, are the 4th, 3rd, and 5th entries of
# time_array, respectively
# month number is zero-offset
$date_array[4] += 1;
# year is modulo-2, sigh!
if ($date_array[5] >= 95) {
  $century = 19;
} else {
  $century = 20; 
}
# use sprintf to make date format consistent with date
# from mh scan command
$date = sprintf("%02d%02d/%02d/%02d",
   $century, $date_array[5], $date_array[4], $date_array[3]);
# Build an array of all files in time order
# (We only need the count here, but we need the message data
# later, may as well do it only once)
system("$MH_PATH/folder +inbox > /dev/null");
if ($?) {
    die("Error executing \"mh folder\" command: $!, died");
}
$msgs = `$MH_PATH/scan -format \"$scan_format\"`;
if ($?) {
    die("Error executing \"mh scan\" command: $!, died");
}
@msgs = split('\n', $msgs);
$count = $#msgs + 1;
# current message hasn't been saved yet
$count += 1;

# date of first submission in the queue is trickier, as there
# may be very old "placeholder" entries that will skew the data.
# Instead of finding the first mail message, find the first
# mail message not from one of the moderators (or root)

# if only 1 message (including this one) in the queue, the date
# of the first submission is today's date.
if ($count == 1) {
  $mdate = $date;
} else {
  # First build regular expression consisting of all moderator's
  # names (and root)
  @moderators = split(' ', $MODERATORS);
  $mod_regexp .= '^(';
  while (@moderators) {
    $moderator = shift(@moderators);
    $mod_regexp .= "$moderator";
    $mod_regexp .= '|';
  }
  # chop off last "|"
  chop($mod_regexp);
  $mod_regexp .= ')@';

  $msg = shift @msgs;
  ($msg_date, $msg_from) = split(' ', $msg); 

  while (($msg_from =~ /$mod_regexp/) && @msgs) {
    $msg = shift @msgs;
    ($msg_date, $msg_from) = split(' ', $msg); 
  }

  if($msg_from =~ /$mod_regexp/) {
    # all files in queue are from the moderators,
    # date of first submission is today's date
    $mdate = $date;
  } else {
    # $msg_date is the date of the first message in the incoming
    # queue not originating from one of the moderators
    $mdate = $msg_date;
  }
}

# Fill in variables in reply text with current data
$reply_text =~ s/%DATE%/$date/;
$reply_text =~ s/%COUNT%/$count/;
$reply_text =~ s/%MDATE%/$mdate/;

# Write out expanded reply text
print "$reply_text";

# write References: block, headers, and first 20 lines of message body

print "\nReference:\n\n  [begin reference inclusion]\n\n";

print $headers;

$count = 0;
while (<STDIN>) {
    print "> " . $_;
    $count++;
    if ($count == 20) {
	print "[remainder deleted for brevity]\n";
	last;
    }
}

print "\n  [end reference inclusion]\n";

