#!/afs/athena/contrib/perl/perl

#$opt_D++;

push(@INC, "$ENV{'HOME'}/lib/perl", "/afs/gza.com/user/jik/lib/perl");

require 'nntp.pl';

while (($_ = $ARGV[0]) && /^-/) {
    shift;
    if (/^-reason$/) {
	($reason = shift @ARGV) || die "Missing argument to -reason";
    } elsif (/^-read_ids/) {
	$read_ids++;
    } elsif (/^-D$/) {
	$opt_D++;
    } elsif (/^-spam$/) {
	$spam++;
    } elsif (/^--$/) {
	last;
    } else {
	die "Unknown option $_";
    }
}
    
@months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
	   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
@days = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;

$date = sprintf("%s, %d %s %d %02d:%02d:%02d GMT", $days[$wday], $mday,
		$months[$mon], $year + 1900, $hour, $min, $sec);

&news_open($ENV{'NNTPSERVER'} || "a");

while ($id = &next_id) {

    if (! &news_xhdr_init("newsgroups", $id)) {
	warn "$id: error xhdr newsgroups";
	next;
    }
    ($id, $newsgroups) = &news_xhdr_next();
    if (! $newsgroups) {
	warn "$id: no newsgroups";
	next;
    } else {
	&news_xhdr_next();
    }

    if (! &news_xhdr_init("from", $id)) {
	warn "$id: error xhdr from";
	next;
    }
    ($foo, $from) = &news_xhdr_next();
    if (! $from) {
	warn "$id: no from";
	next;
    } else {
	&news_xhdr_next();
    }

    if (! &news_xhdr_init("distribution", $id)) {
	warn "$id: error xhdr distribution";
	next;
    }
    (($foo, $distribution) = &news_xhdr_next());
    if ($distribution) {
	&news_xhdr_next();
    }

    if (! &news_xhdr_init("sender", $id)) {
	warn "$id: error xhdr sender";
	next;
    }
    (($foo, $sender) = &news_xhdr_next());
    if ($sender) {
	&news_xhdr_next();
    }

#    $new_id = sprintf("<$ENV{'USER'}.cancel.%d%02d%02d%02d%02d%02d.%d.%d@security.ov.com>",
#		      $year, $mon + 1, $mday, $hour, $min, $sec, $$,
#		      $cancel_num++);
    $new_id = $id;
    $new_id =~ s/^(.)/$1cancel./;

    $message = "";

    if ($distribution) {
	$message .= "Distribution: $distribution\n";
    }

    if ($sender) {
	$message .= "Sender: $sender\n";
    }

    if ($spam) {
	$message .= "Path: cyberspam!usenet\n";
    }
    else {
	$message .= "Path: senator-bedfellow.mit.edu!$ENV{'USER'}\n";
    }

    if (length("Newsgroups: $newsgroups") > 510) {
	@bad_newsgroups = ();
	@newsgroups = split(/,/, $newsgroups);
	$newsgroups = "Newsgroups: $newsgroups[0]";
	shift @newsgroups;
	while (@newsgroups && 
	       (length($newsgroups) + length($newsgroups[0]) + 1 < 510)) {
	    if (! &news_select_ng($newsgroups[0])) {
		push(@bad_newsgroups, shift @newsgroups);
	    }
	    else {
		$newsgroups .= ",$newsgroups[0]";
		shift @newsgroups;
	    }
	}
	while (@bad_newsgroups && 
	       (length($newsgroups) + length($bad_newsgroups[0]) + 1 < 510)) {
	    $newsgroups .= ",$bad_newsgroups[0]";
	    shift @bad_newsgroups;
	}
	if (@newsgroups || @bad_newsgroups) {
	    warn "Truncating Newsgroups line of cancel message for $id.\n";
	}
    }
    else {
	$newsgroups = "Newsgroups: $newsgroups";
    }

    $message .= <<EOF;
Date: $date
From: $from
Message-ID: $new_id
Approved: marthag@mit.edu (Martha H. Greenberg)
$newsgroups
Subject: cmsg cancel $id
Control: cancel $id

$reason
EOF

    if (! &news_post($message)) {
	warn "news_post failed: $nntp'reason\n";
	next;
    }
    print "$id cancelled.\n";
}

&news_close();

sub next_id {
    local($id);

    if ($read_ids) {
	if ($id = <>) {
	    chop $id;
	    $id;
	} else {
	    undef;
	}
    } else {
	shift @ARGV;
    }
}
	
	
