#!/usr/pkg/bin/perl
#
# $Id: ftninpost.pl,v 4.9 1998/09/23 19:23:14 mj Exp $
#
# Postprocessor for ftnin, feeds output of ftn2rfc to rnews and sendmail.
# Call via ftnin's -x option or run after ftn2rfc. Replaces old fidorun
# script.
#
 
require 5.000;

my $PROGRAM = "ftninpost";
 
use strict;
use vars qw($opt_v $opt_c);
use Getopt::Std;
use FileHandle;

##############################################################################
#
# $Id: config.pl,v 4.4 2001/01/04 20:03:42 mj Exp $
#
# Perl functions to read FIDOGATE config file,
# included by <INCLUDE config.pl> when running subst.pl
#

my %CONFIG;

# specials for DosDrive and Zone
my %CONFIG_dosdrive;
my %CONFIG_zone;



my %CONFIG_default =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"newslibdir", "/usr/pkg/inn/lib",
	"seq_ff", "%V/seq/ff",
	"packing", "%C/packing",
	"outpkt_mail", "%S/outpkt/mail",
	"newsspooldir", "/var/news/spool/articles",
	"routing", "%C/routing",
	"acl", "%C/acl",
	"tick_hold", "%B/tick",
	"uuinbound", "/var/spool/bt/uuin",
	"outpkt_news", "%S/outpkt/news",
	"configdir", "/usr/pkg/etc/fidogate",
	"toss_pack", "%S/toss/pack",
	"ftpinbound", "/var/spool/bt/ftpin",
	"toss_route", "%S/toss/route",
	"libdir", "/usr/pkg/lib/fidogate",
	"newsvardir", "/var/news",
	"seq_split", "%V/seq/split",
	"bindir", "/usr/pkg/lib/fidogate/bin",
	"lockdir", "/var/spool/fido/lock",
	"seq_mail", "%V/seq/mail",
	"seq_pkt", "%V/seq/pkt",
	"spooldir", "/var/spool/fido",
	"seq_pack", "%V/seq/pack",
	"aliases", "%C/aliases",
	"seq_toss", "%V/seq/toss",
	"logdir", "/var/log/fido",
	"seq_msgid", "%V/seq/msgid",
	"passwd", "%C/passwd",
	"inbound", "/var/spool/bt/in",
	"hosts", "%C/hosts",
	"history", "%V/history",
	"seq_tick", "%V/seq/tick",
	"areas", "%C/areas",
	"ifmaildir", "/usr/pkg/sbin",
	"toss_bad", "%S/toss/bad",
	"config_gate", "%C/fidogate.conf",
	"config_main", "%C/fidogate.conf",
	"newsetcdir", "/var/news/etc",
	"outrfc_news", "%S/outrfc/news",
	"logfile", "%G/log",
	"outpkt", "%S/outpkt",
	"pinbound", "/var/spool/bt/pin",
	"lock_history", "history",
	"config_ffx", "%C/fidogate.conf",
	"seq_news", "%V/seq/news",
	"vardir", "/var/spool/fido",
	"toss_toss", "%S/toss/toss",
	"btbasedir", "/var/spool/bt",
	"outrfc_mail", "%S/outrfc/mail",
	"charsetmap", "%L/charset.bin",
     );
my %CONFIG_abbrev =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"L", "libdir",
	"C", "configdir",
	"U", "uuinbound",
	"V", "vardir",
	"N", "bindir",
	"B", "btbasedir",
	"K", "lockdir",
	"G", "logdir",
	"P", "pinbound",
	"S", "spooldir",
	"I", "inbound",
     );



sub CONFIG_read {
    my($file) = @_;
    my($key, $arg);
    local *C;

    $file = CONFIG_expand($file);

    open(C,"$file") || die "config.pl: can't open config file $file\n";
    while(<C>) {
	chop;
	next if( /^\s*\#/ );	# comments
	next if( /^\s*$/  );	# empty
	s/\s*$//;		# remove trailing white space
	s/^\s*//;		# remove leading white space
	($key,$arg) = split(' ', $_, 2);
	$key =~ tr/A-Z/a-z/;
	if($key eq "include") {
	    CONFIG_read($arg);
	    next;
	}
	if($key eq "dosdrive") {
	    my ($d, $path) = split(' ', $arg);
	    $CONFIG_dosdrive{lc($d)} = $path;
	    next;
	}
	if($key eq "zone") {
	    my ($z, $rest) = split(' ', $arg, 2);
	    $CONFIG_zone{$z} = $rest;
	    next;
	}
	$CONFIG{$key} = $arg if(!$CONFIG{$key});
    }
    close(C);
}


sub CONFIG_get1 {
    my($key) = @_;
    my($ukey);

    $ukey = $key;
    $ukey =~ tr/a-z/A-Z/;
    return $ENV{"FIDOGATE_$ukey"} if($ENV{"FIDOGATE_$ukey"});

    return $CONFIG{$key} if($CONFIG{$key});
    return $CONFIG_default{$key};
}


sub CONFIG_get {
    my($key) = @_;
    my($ret);
    my($exp);

    $key =~ tr/A-Z/a-z/;
    return CONFIG_expand( CONFIG_get1($key) );
}


sub CONFIG_expand {
    my($v) = @_;
    my($exp);

    if($v =~ /^%([A-Z])/) {
	$exp = CONFIG_get1($CONFIG_abbrev{$1});
	$v =~ s/^%./$exp/;
    }

    return $v;
}


sub CONFIG_debug {    
    my($key);

    for $key (keys %CONFIG) {
	print "$key = $CONFIG{$key} -> ", CONFIG_get($key), "\n";
    }
}

##############################################################################

getopts('vc:');

# read config
my $CONFIG = $opt_c ? $opt_c : "%C/fidogate.conf";
CONFIG_read($CONFIG);

# options
my @options;
if($opt_c) {
    push @options, ("-c", $CONFIG);
}
if($opt_v) {
    push @options, "-v";
}


# get gate.conf parameters
my $SENDMAIL    = CONFIG_get("FTNInSendmail");
my $RNEWS       = CONFIG_get("FTNInRnews");
my $RECOMB      = CONFIG_get("FTNInRecombine");

my $OUTRFC_MAIL = CONFIG_get("OUTRFC_MAIL");
my $OUTRFC_NEWS = CONFIG_get("OUTRFC_NEWS");

if(! $SENDMAIL) {
    print STDERR "ftninpost:$CONFIG:FTNInSendmail not specified\n";
    exit 1;
}
if(! $RNEWS) {
    print STDERR "ftninpost:$CONFIG:FTNInRnews not specified\n";
    exit 1;
}

print
    "sendmail  = $SENDMAIL\n",
    "rnews     = $RNEWS\n",
    "recombine = $RECOMB\n",
    "mail      = $OUTRFC_MAIL\n",
    "news      = $OUTRFC_NEWS\n"
    if($opt_v);


# command lists
my @sendmail = split(' ', $SENDMAIL);
my @rnews    = split(' ', $RNEWS);
# remove -f%s option from sendmail command if present
# (compatibility with old configurations)
my $fidx = -1;
my $i;
for($i=0; $i<=$#sendmail; $i++) {
    $fidx = $i if($sendmail[$i] eq "-f%s");
}
if($fidx > -1) {
    splice(@sendmail, $fidx, 1);
    print "sendmail  = @sendmail\n" if($opt_v);
}



# ----- main -----------------------------------------------------------------
my $dir;
my $f;
my @files;

# do recombining of split messages
if($RECOMB) {
    my @cmd = (&CONFIG_expand($RECOMB));
    push(@cmd, @options);
    print "Running @cmd\n" if($opt_v);
    system @cmd;
}

# mail
$dir = $OUTRFC_MAIL;

opendir(DIR, "$dir") || die "ftninpost: can't open $dir\n";
@files = grep(/\.rfc$/, readdir(DIR));
closedir(DIR);

for $f (sort @files) {
    do_file(1, "$dir/$f");
}

# news
$dir = $OUTRFC_NEWS;

opendir(DIR, "$dir") || die "ftninpost: can't open $dir\n";
@files = grep(/\.rfc$/, readdir(DIR));
closedir(DIR);

for $f (sort @files) {
    do_file(0, "$dir/$f");
}



# ----- do_file() - process mail message or news batch -----------------------

sub do_file {
    my($mail, $file) = @_;
    my($ret, $bad, $from, @cmd);
    local(*SAVE);

    if($mail) {
	# Mail
	@cmd = @sendmail;
	$from = &get_sender($file);
	push(@cmd, "-f$from") if($from);
    }
    else {
	# News
	@cmd = @rnews;
    }

    print "CMD: @cmd" if($opt_v);

    # Save STDIN, open $file as new STDIN
    open(SAVE, "<&STDIN") || die "ftninpost: can't save STDIN\n";
    open(STDIN, "$file") || die "ftninpost: can't open STDIN with $file\n";

    # Run
    $ret = system(@cmd) >> 8;

    # Restore STDIN
    close(STDIN);
    open(STDIN, "<&SAVE") || die "ftninpost: can't restore STDIN\n";

    if($ret == 0) {
	print " - SUCCESS\n" if($opt_v);
	unlink($file) || die "ftninpost: can't unlink $file\n";
    }
    else {
	print " - ERROR\n" if($opt_v);
	$bad = $file;
	$bad =~ s/\.rfc$/.bad/;
	rename($file, $bad) || die "ftninpost: can't move $file -> $bad\n";
    }
}



# ----- get_sender() - get envelope sender for mail --------------------------

sub get_sender {
    my($file) = @_;
    local(*FILE);

    open(FILE, "$file") || die "ftninpost: can't open $file\n";
    $_ = <FILE>;
    close(FILE);

    if( /^From ([^ ]+) / ) {
	return $1;
    }
    else {
	return "";
    }
}
