#!/usr/bin/perl -w
# filter-path -- filter for newsx, adds HOST to Path: in header
# copyright 1999 by Richard L. Hawes under Perl's license
# free software, use at your own risk
# $Id: filter-path.pl,v 0.13 1999/06/03 01:31:08 news Exp $

# usage: `filter-path  news-site-name'
# edits all Path headers to contain the news-site-name
# pipes output to rnews

# latest version: http://www.dma.org/~rhawes/programs/filter-path


use strict;

# System constants
# pipe to this:
*::PIPE_TO	=\"rnews";
# set to incoming spool directory
*::INCOMING	=\"/var/spool/news/in.coming";
# temporary directory
*::TMPDIR	=\"$::INCOMING/tmp";
# set to rnews directory
*::RNEWS_DIR	=\"/usr/lib/news/rnews";
# set to desired file suffix
*::TMP_FILE	=\"$::TMPDIR/filter-path.$$";

# Mode constants
*::FILE_LINE1	=\1;
*::MSG_LINE1	=\2;
*::FIND_PATH	=\3;
*::FIND_BODY	=\4;
*::FIND_END	=\5;

($#ARGV == 0) or die("incorrect number of arguments");

*::HOST=\"$ARGV[0]!";
*::HOST_LEN=\length($::HOST);
*::NEW_PATH=\"Path: ${main::HOST}not-for-mail\n";
*::PATH_LEN=\length($::NEW_PATH);

sub do_special($) {
	my($command) = @_;

	open(TMP, ">$::TMP_FILE")
		or die("cannot open file $::TMP_FILE: $!");
	# delete first line
	print(TMP <INPUT>);
	close(TMP);


	# handle special case of cunbatch
	if($command =~ /^cunbatch$/o ) {
                unless($command=`which compress`) {
                        $command=`which gzip`
                        or die("neither compress nor gzip is in PATH");
                }
		chop($command);
		$command="$command -d";
	}
	else {
		$command="$::RNEWS_DIR/$command";
	}
	open(INPUT, "$command <$::TMP_FILE |")
		or die("cannot open pipe to $command: $!");
}

sub do_file() {
	my(
		$bytes,
		$fixed,
		$len,
		@message,
		$mode,
	);

	$mode = $::FILE_LINE1;
	READ_MESSAGE: while(<INPUT>) {
		$fixed=0;
		$len=0;
		@message=($_);
		if(/^#! rnews (\d+)$/o) {
			$bytes=$1;
		}
		elsif( $mode == $::FILE_LINE1 and /^#! (.+)$/o) {
			# special command
			do_special($1);
			$mode = $::MSG_LINE1;
			next;
		}
		else {
			die("bad file format: missing message prefix");
		}
		$mode = $::FIND_PATH;
		READ_HEADER: while(<INPUT>) {
			$len += length($_);
			( $len < $bytes )
				or die("bad message format: premature end");
			if( $mode == $::FIND_BODY ) {
				if(/^[ 	]*$/o) {
					$mode = $::FIND_END;
					last;
				}
			}
			elsif(/^[ 	]*$/o) {
				push(@message, $::NEW_PATH );
				$fixed=$::PATH_LEN;
				$mode = $::FIND_END;
				last;
			}
			elsif(/^Path: /o) {
				if(not /^Path: $::HOST/o) {
					s/^Path: /Path: $::HOST/o;
					$fixed=$::HOST_LEN;
				}
				$mode = $::FIND_BODY;
			}
			push(@message, $_);
		}
		( $mode == $::FIND_END )
			or warn("missing body"); 
		push(@message, $_);
		READ_BODY: while(<INPUT>) {
			$len += length($_);
			push(@message, $_);
			unless( $len < $bytes ) {
				( $len == $bytes )
					or die("bad message format: premature end");
				last;
			}
		}
		$mode = $::MSG_LINE1;
		if($fixed) {
			#print "$fixed,  $len\n";
			$fixed += $len;
			$message[0] = "#! rnews $fixed\n";
		}
		print(OUPUT @message)
			or die("cannot write to pipe: $!");
	}
}

my(
	$File,
);


open(OUPUT, "|$::PIPE_TO") or die("cannot pipe to $::PIPE_TO: $!");

#open(OUPUT, ">-") or die("$!");

chdir($::INCOMING) or die("cannot change directory to $::INCOMING");

READ_FILE: foreach $File ( `ls` ) {
	chop($File);
	-f $File or next;
	unless( open(INPUT, "<$File") ) {
		warn("cannot open $File: $!");
		next;
	}
	eval { do_file() };
	if( $@ ) {
		warn($@);
		last;
	}
	close(INPUT) or die("cannot close $File $!");
	system("rm -f $File") >> 8
		and warn("could not rm $File: $!");
}

close(OUPUT);

system("rm -f $::TMP_FILE") >> 8
	and die("could not rm $::TMP_FILE: $!");

