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



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);
