#!/usr/bin/perl

$DB::profile = 0;

$MODE = 'highwind';

# load the filter and run get_config() and some other initialization code
require 'cleanfeed'; # XXX

$config_dir = './';
$config{active_file} = '/var/lib/news/active';
$config{debug_batch_directory} = './';

# set up our signal handlers.
$got_usr1 = 0;
$got_usr2 = 0;
$got_hup = 0;
$got_term = 0;
$SIG{USR1} = sub { $got_usr1 = 1 };	# use SIGUSR1 to write statfile
$SIG{USR2} = sub { $got_usr2 = 1 };	# use SIGUSR2 to dump EMP history
$SIG{HUP}  = sub { $got_hup  = 1 };	# use SIGHUP to reload configuration
$SIG{TERM} = sub { $got_term = 1 };	# catch SIGTERM so we can clean up

# Test for and create the PID file, if wanted.
pid_file() if defined $config{pid_file};

mainloop();
exit 0;

# main loop for standalone mode.
sub mainloop {
	my ($head);

	$| = 1; # Flush STDOUT

	open_logfile() if $config{logfile};

	my %known_headers = (
		'approved'				=> 'Approved',
		'content-base'			=> 'Content-Base',
		'content-disposition'	=> 'Content-Disposition',
		'content-type'			=> 'Content-Type',
		'control'				=> 'Control',
		'date'					=> 'Date',
		'distribution'			=> 'Distribution',
		'followup-to'			=> 'Followup-To',
		'from'					=> 'From',
		'lines'					=> 'Lines',
		'message-id'			=> 'Message-ID',
		'newsgroups'			=> 'Newsgroups',
		'nntp-posting-host'		=> 'NNTP-Posting-Host',
		'organization'			=> 'Organization',
		'path'					=> 'Path',
		'references'			=> 'References',
		'reply-to'				=> 'Reply-To',
		'sender'				=> 'Sender',
		'subject'				=> 'Subject',
		'supersedes'			=> 'Supersedes',
		'user-agent'			=> 'User-Agent',
		'x-trace'				=> 'X-Trace',
		'x-newsreader'			=> 'X-Newsreader',
		'x-newsposter'			=> 'X-Newsposter',
		'x-mailer'				=> 'X-Mailer',
		'x-poster'				=> 'X-Poster',
		'x-cancelled-by'		=> 'X-Cancelled-By',
		'x-canceled-by'			=> 'X-Canceled-By',
	);

	<STDIN>; # eat the first line

	while (not eof STDIN) {
		%hdr = ();
		$hdr{__BODY__} = '';

		# read in a line of the header and store it in a hash
		while (<STDIN>) {
			last if /^$/;
			next if not /^([^ ]+): (.*)$/;
			my ($header, $value) = ($1, $2);
			my $lcheader = lc $header;
			$header = $known_headers{$lcheader}
				if exists $known_headers{$lcheader};
			$hdr{$header} = $value;
		}
		while (<STDIN>) {
			last if /^From foo\@bar Thu/;
			$hdr{__BODY__} .= $_;
		}
		$bytes = length($hdr{__BODY__}) + 1000;

		$DB::profile = 1;
		my $ret = filter_art();	# the real work
		$DB::profile = 0;

		if ($ret) {
			print "REJ $ret\n"; # rejected
			log_entry() if $dolog;
		} else {
			print "ACC \n"; # accepted
			log_entry(1) if $dolog and $config{log_accepts};
		}

		%hdr = ();

		# reload our config if we caught SIGHUP
		re_configure() if $got_hup;
		# write stats file if we caught SIGUSR1
		if ($got_usr1) {
			writestats(1);
			$got_usr1 = 0;
		}
		# terminate cleanly if we caught SIGTERM
		last if $got_term;
		# dump EMP histories if we caught SIGUSR2
		if ($got_usr2) {
			dump_emp();
			$got_usr2 = 0;
		}

	} # stdin loop

	# Cleanup
	close_logfile()	if $dolog == 1;

	dump_emp() if $config{do_emp_dump};

	unlink $config{pid_file} if $config{pid_file};
}

# when running standalone, HUP brings us here.
# reload config, close logfiles and repoen if we still want them.
sub re_configure {
	$got_hup = 0;

	get_config();
	setup_stuff();

	# Close the logfile.
	close_logfile() if $dolog == 1;
	# If we still want logfiles, open again.
	open_logfile() if $config{logfile};
}

# Create a pid file. If it already exists, complain and die.
sub pid_file {
	return undef unless $config{pid_file};

	die "Cleanfeed already running (pid file)\n" if -e $config{pid_file};

	if (open(F, ">$config{pid_file}")) {
		print F $$;
		close F;
	} else {
		warn "cleanfeed can't create pid file: $!\n";
	}
}

print $bytes.$MODE; # lint food
