#!/usr/local/bin/perl
#
# history database sanity checker
# David Barr <barr@math.psu.edu>
# version 1.1
# Throw away history entries with:
#   malformed lines (too long, contain nulls or special characters)
#
# INN Usage:
#   ctlinnd throttle 'fixing history'
#   ./fixhist <history >history.n
#   makehistory -r -s `wc -l <history` -f history.n
#   mv history.n history
#   mv history.n.dir history.dir
#   mv history.n.pag history.pag
#   ctlinnd go 'fixing history'
# any malformed entries will be output to stderr.



$MAXKEYLEN=255;
$now=time();

while (<>) {
	chop;
	($msgid,$dates,$arts,$xtra) = split('\t');
	if ($xtra) {
		&tossit();		# too many fields
		next;
	}
	unless ($msgid) {
		&tossit();		# too few fields (emtpy, usually)
		next;
	}
	if (length($msgid) >= $MAXKEYLEN) {
		&tossit();		# message-id too long
		next;
	}
	if ($msgid !~ /^<[^< ]*>/) {
		&tossit();		# malformed msg-ids
		next;
	}
	if (/[\000-\010\012-\037\177-\377]/) { # non control-chars except tab
		&tossit();		# illegal chars
		next;
	}
#	if ($dates) {
#		($date_a,$date_e,$date_p)= split('~');
#		$date_e=0 if ($date_e='-');
#		$date_p=0 if ($date_p='-');
#	}
	print "$_\n";
}
sub tossit {
	print STDERR "$_\n";
}
