#!/usr/bin/perl
# From: Jonathan Kamens <jik@annex-1-slip-jik.cam.ov.COM>
#
# My script wasn't generated by my news reader, it was generated by  a
# Perl  script  that  I piped it into from my news reader.  Here's the
# script.  Feel free to adopt it for your own needs :-):

# The value of mailer should be whatever command is needed to process
# a file that contains a To: line and send the file to the recipient.

$mailer = 'smail -oi -t';
#mailer = '/usr/lib/sendmail -oi -t';

$offtopic_text = <<EOF;
This article should not have been posted in %NEWSGROUP%.

Reference:

EOF

$ad_text = <<EOF;
This commercial advertisement should not have been posted in the
non-commercial newsgroup %NEWSGROUP%.

Reference:

EOF

$multipost_text = <<EOF;
You posted the article below separately in at least two different
newsgroups.

Quoting from "A Primer on How to Work With the Usenet Community",
which is posted periodically in news.announce.newusers:

                     Only Post a Message Once

  Avoid posting messages to more than one newsgroup unless you are sure
  it is appropriate.  If you do post to multiple newsgroups, do not
  post to each group separately.  Instead, specify all the groups on a
  single copy of the message.  This reduces network overhead and lets
  people who subscribe to more than one of those groups see the message
  once instead of having to wade through each copy.

  If you have not read this posting, I suggest you do so, because all
USENET posters are expected to be familiar with the introductory
postings in news.announce.newusers and to abide by the guidelines they
set down.  The posting is available as follows:

Subject: A Primer on How to Work With the Usenet Community
Newsgroups: news.announce.newusers,news.answers

Available in the indicated USENET newsgroup(s), or via anonymous ftp from
rtfm.mit.edu (18.181.0.24) in the file:

/pub/usenet/news.answers/usenet/primer/part1

Also available from mail-server@rtfm.mit.edu by sending a mail message
containing:

send usenet/news.answers/usenet/primer/part1

Send a message containing "help" to get general information about the
mail server.

Reference:

EOF

$test_text = <<EOF;
  The test posting below should not have been posted in %NEWSGROUP%.

  Quoting from "A Primer on How to Work With the Usenet Community," which is
posted periodically in news.announce.newusers:

  If you want to try a test of something, do not use a world-wide newsgroup!
  Messages in misc.misc that say "This is a test" are likely to cause
  large numbers of caustic messages to flow into your mailbox.  There are
  newsgroups that are local to your computer or area that should be used.
  Your system administrator can tell you what they are.  

Consider this one such "caustic message."  Test messages should be posted in
test newsgroups, such as misc.test.

  If you have not read the Primer posting, I urge you to do so now.
You can get a copy of it as follows:

Subject: A Primer on How to Work With the Usenet Community
Newsgroups: news.announce.newusers,news.answers

Available in the indicated USENET newsgroup(s), or via anonymous ftp from
rtfm.mit.edu (18.181.0.24) in the file:

/pub/usenet/news.answers/usenet/primer/part1

Also available from mail-server@rtfm.mit.edu by sending a mail message
containing:

send usenet/news.answers/usenet/primer/part1

Send a message containing "help" to get general information about the
mail server.

Reference:

EOF

$cp_text = <<EOF;
This article should not have been posted in %NEWSGROUP%.

Your article probably appeared there because you followed up on a
previous article in another newsgroup, and that article was
inappropriate cross-posted to %NEWSGROUP% (possibly along with other
inappropriate newsgroups).  Your followup therefore went to the same
inappropriate newsgroups in addition to the newsgroup in which you
thought you were posting it.

You might ask why the article to which you were responding was posted
to inappropriate newsgroups.  The answer to this is sad but true --
there are unfortunately some people on the Usenet who think it's
amusing to post an article that is likely to generate many responses
to a large number of off-topic newsgroups, because they find it
amusing to annoy the readers of those other newsgroups.

Therefore, it is important for you to check the list of newsgroups to
which you are posting when you post a followup, and to remove from
that list any newsgroups which might be inappropriate (also, note that
if you don't know exactly what a newsgroup's about, you shouldn't be
posting it -- it's better to err on the side of posting in fewer
newsgroups than on the side of posting in too many).

Thank you for your time.

Reference:

EOF

($whoami = $0) =~ s,.*/,,;

$usage = <<EOF;
Usage: $whoami -ng newsgroup [ -ngdir | -dir dir ]
       [ -flame filename | -offtopic | -ad | -mp | -cp | -test ]
       [ article-filename ]
       \"%NEWSGROUP%\" will be replaced with the newsgroup name in the flame text.
EOF

while (($_ = $ARGV[0]) && /^-/) {
    shift;
    if (/^-ng$/) {
        ($newsgroup = shift) || 
            die "$whoami: Missing argument to \"$_\".\n$usage\n";
    } elsif (/^-flame$/) {
        ($flame = shift) || die "$whoami: Missing argument to \"$_\".\n$usage\n";
        $flame_spec++;
    } elsif (/^-pm$/) {
        $postmaster++;
    } elsif (/^-ngdir$/) {
        $ngdir++;
    } elsif (/^-dir$/) {
        ($dir = shift) || die "$whoami: Missing argument to \"$_\".\n$usage\n";
    } elsif (/^-offtopic$/) {
        $flametext = $offtopic_text;
        $logflame = "off-topic";
        $flame_spec++;
    } elsif (/^-ad$/) {
        $flametext = $ad_text;
        $logflame = "ad";
        $flame_spec++;
    } elsif (/^-mp$/) {
        $flametext = $multipost_text;
        $logflame = "multipost";
        $flame_spec++;
    } elsif (/^-cp$/) {
        $flametext = $cp_text;
        $logflame = "badly cross-posted thread";
        $flame_spec++;
    } elsif (/^-test$/) {
        $flametext = $test_text;
        $logflame = "test";
        $flame_spec++;
    } elsif (/^-addr$/) {
        ($addr = shift) || die "$whoami: Missing argument to \"$_\".\n$usage\n";
        push(@addrs, $addr);
    } elsif (/^--$/) {
        last;
    } else {
        die "$whoami: Unknown option \"$_\".\n$usage\n";
    }
}

die "$whoami: must specify newsgroup with \"-ng\".\n$usage\n"
    if (! $newsgroup);

die "$whoami: Specify at most one of:
        \"-flame\", \"-offtopic\", \"-ad\", \"-mp\", \"-cp\", or \"-test\".\n$usage\n" 
    if ($flame_spec > 1);

die "$whoami: Can't specify both \"-dir\" and \"-ngdir\".\n$usage\n"
    if ($dir && $ngdir);

if (@ARGV > 1) {
    die "$whoami: Too many arguments.\n$usage\n";
}

($slashgroup = $newsgroup) =~ s,\.,/,g;

if ($ngdir) {
    $dir = "$ENV{'HOME'}/News/$slashgroup";
} elsif (! $dir) {
    $dir = "$ENV{'HOME'}/News";
}

if (! $flametext) {
    if ($flame) {
        $logflame = $flame;
        $flame = "$dir/$flame" if ($flame !~ m,^/,);
    } else {
        $flame = "$dir/flame" if (! $flame);
    }
}
$log = "$dir/flame-list";
$filters = "$dir/filtered";

push(@INC,
     '/usr/local/FAQ_server',
     "$ENV{'HOME'}/lib/perl",
     "$ENV{'HOME'}/src/FAQ_server");

require 'header.pl';

open(FILTERS, $filters) || die;

while (<FILTERS>) {
    chop;
    push(@filters, $_);
}

close(FILTERS);

$addr = $date = $id = $subject = undef;

($next_line, %headers) = &header'parse(undef, "main'STDIN");

$addr = (&header'field_value('reply-to', %headers) ||
         &header'field_value('from', %headers));
$date = &header'field_value('date', %headers);
$id = &header'field_value('message-id', %headers);
$subject = &header'field_value('subject', %headers);

if (! ($addr && $date && $id && $subject)) {
    warn "Missing header field in input.\n";
    exit;
}

foreach $filter (@filters) {
    if ($addr =~ /$filter/) {
        print "FILTER ", &logstr, "\n";
        open(LOG, ">>$log") || die;
        print LOG "FILTER ", &logstr, "\n";
        close(LOG);
        exit;
    }
}

if ($subject !~ /^re:/i) {
    $subject = "Re: $subject";
}

open(MAIL, "|$mailer") || die "Opening mail: $!.\n";

print MAIL "To: $addr\n" || die;

if ($postmaster) {
    ($pmaddr = $addr) =~ s/^[^@]*@/postmaster@/;
    unshift(@addrs, $pmaddr);
}

for (@addrs) {
    print MAIL "Cc: $_\n" || die;
}

print MAIL "Subject: $subject
In-reply-to: Your message of $date.
        $id\n\n";

if ($flametext) {
    $flametext =~ s/%NEWSGROUP%/$newsgroup/g;
    print MAIL $flametext || die;
} else {
    open(FLAME, $flame) || die;

    while (<FLAME>) {
        $lastline = $_;
        s/%NEWSGROUP%/$newsgroup/g;
        print MAIL $_ || die;
    }

    close(FLAME);

    if ($lastline !~ /\n/) {
        print MAIL "\n" || die;
    }

    if ($lastline ne "\n") {
        print MAIL "\n" || die;
    }
}

print MAIL "$header'text_read$next_line" || die;

while (<>) {
    print MAIL $_ || die;
}

close(MAIL);

warn("Sendmail exit status ", $? >> 8, ".\n") if ($?);

open(LOG, ">>$log") || die;
print LOG &logstr, "\n";
close(LOG);
print &logstr, "\n";

sub logstr {
    local($str) = "$id $date ";

    if ($logflame) {
        $str .= "(flame: $logflame) ";
    }

    if (! $ngdir) {
        $str .= "(ng: $newsgroup) ";
    }

    $str .= $addr;

    $str;
}
