#!/usr/local/bin/perl
#
#
# Create report for "problems" in FIDOGATE tosser log file:
#   - Unknown areas
#   - Insecure EchoMail
#   - Routed EchoMail
#   - EchoMail with circular path
#   - Messages from gateway
#

$NEWSGROUPS = "junk";
$SUBJECT    = "EchoMail problem report";


##############################################################################
#
#
# 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!!!##
	"pinbound", "/var/spool/fido/bt/pin",
	"tic_history", "%V/tic_hist",
	"seq_news", "%V/seq/news",
	"acl", "%C/acl",
	"outrfc_news", "%S/outrfc/news",
	"newsetcdir", "/usr/local/news/etc",
	"tick_hold", "%B/tick",
	"spyes", "%C/spyes",
	"seq_ff", "%V/seq/ff",
	"history", "%V/history",
	"seq_pack", "%V/seq/pack",
	"lockdir", "/var/run/fidogate",
	"charsetmap", "%L/charset.bin",
	"netmaildir", "/var/spool/fido/bt/netmail",
	"toss_route", "%S/toss/route",
	"ftnacl", "%C/ftnacl",
	"newsbindir", "/usr/local/news/bin",
	"toss_toss", "%S/toss/toss",
	"uuinbound", "/var/spool/fido/bt/uuin",
	"bindir", "/usr/local/bin",
	"seq_tick", "%V/seq/tick",
	"seq_toss", "%V/seq/toss",
	"outpkt_news", "%S/outpkt/news",
	"seq_msgid", "%V/seq/msgid",
	"configdir", "/usr/local/etc/fido/gate",
	"toss_pack", "%S/toss/pack",
	"btbasedir", "/var/spool/fido/bt",
	"hubroutedb", "%V/route",
	"lock_tic_hist", "tic_hist",
	"outrfc_mail", "%S/outrfc/mail",
	"newsvardir", "/usr/local/news/db",
	"newslibdir", "/usr/local/news",
	"seq_split", "%V/seq/split",
	"hosts", "%C/hosts",
	"logfile", "%G/log",
	"vardir", "/var/db/fidogate",
	"libexecdir", "/usr/local/libexec/fidogate",
	"toss_bad", "%S/toss/bad",
	"uplinks", "%C/uplinks",
	"newsspooldir", "/usr/local/news/spool/articles",
	"packing", "%C/packing",
	"spooldir", "/var/spool/fido/gate",
	"outpkt", "%S/outpkt",
	"passwd", "%C/passwd",
	"inbound", "/var/spool/fido/bt/in",
	"aliases", "%C/aliases",
	"ftpinbound", "/var/spool/fido/bt/ftpin",
	"inn_batchdir", "/usr/local/news/spool/outgoing",
	"dbc_history", "%V/bdc",
	"lock_history", "history",
	"routing", "%C/routing",
	"outpkt_mail", "%S/outpkt/mail",
	"seq_mail", "%V/seq/mail",
	"config_gate", "%C/fidogate.conf",
	"areas", "%C/areas",
	"seq_pkt", "%V/seq/pkt",
	"config_main", "%C/fidogate.conf",
	"lock_dbc", "dbc",
	"logdir", "/var/log/fido/gate",
     );
my %CONFIG_abbrev =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"G", "logdir",
	"S", "spooldir",
	"N", "bindir",
	"K", "lockdir",
	"P", "pinbound",
	"L", "libexecdir",
	"B", "btbasedir",
	"V", "vardir",
	"I", "inbound",
	"C", "configdir",
     );



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";
    }
}

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

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

my $INEWS       = CONFIG_get("INEWS");
my $SENDMAIL    = CONFIG_get("SENDMAIL");


require "getopts.pl";
&Getopts('g:s:nm:');

if($opt_g) {
    $NEWSGROUPS = $opt_g;
}
if($opt_s) {
    $SUBJECT    = $opt_s;
}
if($opt_n) {
    open(OUT, "|$INEWS") || die "logreport: can't open pipe to inews\n";
    select(OUT);
    $out_flag = 1;
}
if($opt_m) {
    open(OUT, "|$SENDMAIL $opt_m")
	|| die "logreport: can't open pipe to sendmail\n";
    select(OUT);
    $out_flag = 1;
}



while(<>) {
    # Dupe EchoMail
    if( /ftntoss dupe from (.*)\: (.+) MSGID/ ) {
	$key = "$1 $2";
	$dupes{$key}++;
    }

    # Unknown areas
    if( /ftntoss unknown area (.+) from ([0-9:\/.]+)$/ ) {
	$key = "$2 $1";
	$unknown{$key}++;
    }

    # Insecure EchoMail
    if( /ftntoss insecure echomail area (.+) from ([0-9:\/.]+)$/ ) {
	$key = "$2 $1";
	$insecure{$key}++;
    }

    # Routed EchoMail
    if( /ftntoss routed echomail area (.+) from ([0-9:\/.]+) to ([0-9:\/.]+)$/ ) {
	$key = "$2 $1 $3";
	$routed{$key}++;
    }

    # EchoMail with circular path
    if( /ftntoss circular path echomail area (.+) from ([0-9:\/.]+) to ([0-9:\/.]+)$/ ) {
	$key = "$2 $1 $3";
	$circular{$key}++;
    }

    # Messages from other FTN gateway
    if( /ftn2rfc skipping message from gateway.*origin=([0-9:\/.]+)$/ ) {
	$key = "$1";
	$gateway{$key}++;
    }
}

# Report
print "Newsgroups: $NEWSGROUPS\n" if($opt_n);
print "Subject: $SUBJECT\n";

print "\n";

if(scalar(%unknown)) {
    print "Unknown EchoMail areas:\n\n";
    print "    From                            ",
          "Area                            Msgs\n";
    print "    ----                            ",
          "----                            ----\n";
    
    for $k (sort keys(%unknown)) {
	($n,$a) = split(' ', $k);
    printf "    %-15.15s                 %-31.31s %d\n", $n, $a, $unknown{$k};
}

print "\n";
}

if(scalar(%dupes)) {
    print "Dupes EchoMail:\n\n";
    print "    From                            ",
          "Area                            Msgs\n";
    print "    ----                            ",
          "----                            ----\n";

    for $k (sort keys(%dupes)) {
	($n,$a) = split(' ', $k);
	printf "    %-15.15s                 %-31.31s %d\n",
	$n, $a, $dupes{$k};
    }

    print "\n";
}

if(scalar(%insecure)) {
    print "Insecure EchoMail:\n\n";
    print "    From                            ",
          "Area                            Msgs\n";
    print "    ----                            ",
          "----                            ----\n";

    for $k (sort keys(%insecure)) {
	($n,$a) = split(' ', $k);
	printf "    %-15.15s                 %-31.31s %d\n",
	$n, $a, $insecure{$k};
    }

    print "\n";
}

if(scalar(%routed)) {
    print "Routed EchoMail:\n\n";
    print "    From            To              ",
          "Area                            Msgs\n";
    print "    ----            --              ",
          "----                            ----\n";
    
    for $k (sort keys(%routed)) {
	($n,$a,$t) = split(' ', $k);
	printf "    %-15.15s %-15.15s %-31.31s %d\n", $n, $t, $a, $routed{$k};
    }
    
    print "\n";
}

if(scalar(%circular)) {
    print "EchoMail with circular ^APATH:\n\n";
    print "    From            To              ",
          "Area                            Msgs\n";
    print "    ----            --              ",
          "----                            ----\n";
    
    for $k (sort keys(%circular)) {
	($n,$a,$t) = split(' ', $k);
	printf "    %-15.15s %-15.15s %-31.31s %d\n", $n, $t, $a, $circular{$k};
    }
    
    print "\n";
}

if(scalar(%gateway)) {
    print "EchoMail messages from other FTN gateways ",
          "(at least looking like that):\n\n";
    print "    From                            ",
          "                                Msgs\n";
    print "    ----                            ",
          "                                ----\n";
    
    for $k (sort keys(%gateway)) {
	($n) = split(' ', $k);
	printf "    %-15.15s %-15.15s %-31.31s %d\n", $n, "", "", $gateway{$k};
    }
    
    print "\n";
}

if($out_flag) {
    close(OUT);
}
