#! /usr/bin/perl
#
# This is a utility script to manipulate Fidonet-related issues
# using Eugene Crosser's ifmail package
#
# Version 0.1
#
# (C) Michael Bravo and The Communication Tube, 1994
#
# You can do whatever you want with this script. I take no responsibility
# whatsoever in anything related to this script. If you make some useful
# additions to this, please think of sending them to me so I could partake
# of your wisdom.
#
# This script was written to help attaching and requesting files from the
# commandline, much like you do with Squish under DOS or OS/2. It is certainly
# not perfect - I used it as an exercise in Perl. It also probably lacks
# some other useful features, like ability to specify trunc/sent or kill/sent
# attributes etc etc. If you really want this or some other features 
# implemented, write me at mbravo@tctube.spb.su or mbravo@octopus.spb.su
# and I will try to do what I can.
#
# Note - files don't get copied to any spool dir, so if you move attached
# files somewhere, they won't get sent.
#
# This script is supposed to read ifmail's config to determine where outbound
# directory and logfile are. The only two parameters to modify in most cases
# are below.

### modified by P. Saratxaga on 28 Jun 1996
# when _requesting_ it musn't append $PWD to the file name. So I move that
# code to execute only if it is send mode.


$cfgfile="/etc/ifmail/config";     # where the config is
$ifowner="fnet";                           # who is the owner of the ifmail

if ( getpwuid($<) ne $ifowner ) { print "You must be owner of ifmail\n"; exit 1; }

if ( (@ARGV < 3) || $ARGV[0] eq "-?" || $ARGV[0] eq "-h" ) {
	&usage;
} 

$ARGV[0] =~ tr/A-Z/a-z/;
$ARGV[3] =~ tr/A-Z/a-z/;

&parsecfg;

if ( $logfile ne "" ) { 
	open(LOG, ">>".$logfile) || die "Can't open logfile";
}

if ($ARGV[3] eq "" || $ARGV[3] eq "normal") {
	$flavour = 'f';
} elsif ($ARGV[3] eq "crash") {
	$flavour = 'c';
} elsif ($ARGV[3] eq "hold") {
	$flavour = 'h';
} else {
	print "Unknown flavour, assuming normal\n";
	$flavour = 'f';
}

if ($ARGV[0] eq "send") {
	if (substr($ARGV[1], 0, 1) ne "/") {
		$cwd=`pwd`;
		chop $cwd;
		$ARGV[1] = $cwd."/".$ARGV[1];
	}
	&attach($ARGV[1], $ARGV[2]);
} elsif ($ARGV[0] eq "get") {
	&request($ARGV[1], $ARGV[2]);
} else {
	print "Unknown command, try ifman -h\n";
	exit 1;
}

close(LOG);

exit 0;

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

sub attach {
	local($fspec, $address) = @_;

	$floname = &resolve($address);

	open(FLO, ">>".$outbound."/".$floname) || die "Can't open flo-file $outbound/$floname";
	open(FIND, "find $fspec -print |") || die "Can't generate list of files"; 

	if ( eof(FIND) ) {
		print "No matching files, nothing to send\n";
		exit 1;
	}

	while (<FIND>) {

		chop;
		$datestamp = `date \"+%D %T\"`;
		chop $datestamp;
		printf LOG "%s %s %s\n", $datestamp, $$, "ifman: sending $_ to $address";
		printf FLO "%s\n", $_;	
	}

	close(FLO);
	close(FIND);
}

sub request {
	local($fspec, $address) = @_;

	$reqname = &resolve($address);

	$reqname =~ s/\.[fch]lo/\.req/;
	
	open(REQ, ">>".$outbound."/".$reqname) || die "Can't open req-file";

	$datestamp = `date \"+%D %T\"`;
	chop $datestamp;
	printf LOG "%s %s %s\n", $datestamp, $$, "ifman: requesting $fspec from $address";
	printf REQ "%s\n", $fspec;	

	close(REQ);
}

sub resolve {
	local($addr) = @_;

	if ( index($addr, ":") >=0 ) {
		print "I cannot resolve addresses with zones!\n";
		exit 1;
	} elsif ( index($addr, "/") == -1 ) {
		print "Not a valid address!\n";
		exit 1;
	}

	($net, $node, $point) = split(/\/|\./, $addr); 

	if ( defined $point ) {
		$pointdir = sprintf("%04x%04x.pnt", $net, $node);
		if ( ! -e $outbound."/".$pointdir ) {
			mkdir ($outbound."/".$pointdir, 0755) || die "Can't create point directory";
		}
		$flo = sprintf("0000%04x.%01slo", $point, $flavour);
		return $pointdir."/".$flo;
	} else {
		$flo = sprintf("%04x%04x.%01slo", $net, $node, $flavour);
		return $flo;
	}
}

sub usage {
	print "ifmail manager script\n";
	print "usage: ifman <cmd> <filespec> <address> [flavour]\n";
	print "  commands: send, get\n";
	print "  flavours: normal, crash, hold. Default is normal.\n";
	print "Only 2d addresses with points are supported - no zones!\n";
	exit 1;
}

sub parsecfg {
	open(CFG, $cfgfile) || die "Can't open ifmail config file";

	while (<CFG>) {
		chop;
		if (/^#/) { next; }
		if (/^outbound\s+(\S+)/) { $outbound = $1; }
		if (/^logfile\s+(\S+)/) { $logfile = $1; }
	}
	
	close(CFG);
}
