#!/usr/pkg/bin/perl
#
# $Id: ffxrmail.pl,v 4.6 2000/11/18 17:57:56 mj Exp $
#
# sendmail frontend for processing ffx rmail commands
#

require 5.000;

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



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:f:');

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

# configuration
my $SENDMAIL = CONFIG_get("FFXRmailSendmail");
die "ffxrmail:$CONFIG:FFXRmailSendmail not defined\n" if(! $SENDMAIL);
$SENDMAIL =~ s/-p[^ ]*/-pFFX:$opt_f/ if($opt_f);
print "SENDMAIL: $SENDMAIL\n" if($opt_v);

# ignore SIGPIPE
$SIG{"PIPE"} = "IGNORE";

# read From_ line from <STDIN>
my $from;
my @cmd;
$_ = <STDIN>;
if( /^From ([^ ]*) / ) {
    $from = $1;
    $from = "news\@localhost" if($from eq "");
}
else {
    die "ffxrmail: no From_ line at start of message\n";
}
print "ENVELOPE: $from\n" if($opt_v);

# sendmail command
undef @cmd;
push(@cmd, split(' ', $SENDMAIL));
push(@cmd, "-f");
push(@cmd, $from);
push(@cmd, @ARGV);

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

# Safe pipe to sendmail
my $pid = open(PIPE, "|-");
if ($pid) {   # parent
  while (<STDIN>) {
    print PIPE $_;
  }
  close(PIPE);
  die "$PROGRAM: ERROR: close pipe to sendmail\n" if($?);
}
else {        # child
  exec (@cmd) || die "$PROGRAM: ERROR: can't exec $SENDMAIL program: $!\n";
  die "$PROGRAM: ERROR: impossible return from exec\n";
  # NOTREACHED
}


exit(0);
