#!/usr/bin/perl

# ifextrp [-e[alp]] [-s<speed>] [-wazoo|-bark] [-p|-l] <ftn_address> <req_file> <out_file> <rep_file>

$LogFile = "/var/log/ifmail/ifextrp.log";
#$LogFile = "ifextrp.log";
$DbmPath = "/files/filebase/index/files";
$ReportCols = 76;
$Koi2Alt = "/usr/local/bin/koi82alt";

#$UseGDBM_File = 1;
#use GDBM_File;

$FilesLimit{"a"} = 3;
$FilesLimit{"l"} = 5;
$FilesLimit{"p"} = 10;
$BytesLimit{"a"} = 1500000;
$BytesLimit{"l"} = 7000000;
$BytesLimit{"p"} = 10000000;
$Enable{"a"} = 1;
$Enable{"l"} = 1;
$Enable{"p"} = 1;

if( !open( LOG, ">>".$LogFile )) {
	print STDERR "Cannot open log file. $!\n";
	exit( 1 );
}

$arg = "";
for( $i = 0; $i <= $#ARGV; $i++ ) {
	$arg .= $ARGV[$i]." ";
	if( $ARGV[$i] =~ /^-e[alp]*$/i ) {
		$Enable{"a"} = 0;
		$Enable{"l"} = 0;
		$Enable{"p"} = 0;
		if( $ARGV[$i] =~ /a/i ) {
			$Enable{"a"} = 1;
		}
		if( $ARGV[$i] =~ /l/i ) {
			$Enable{"l"} = 1;
		}
		if( $ARGV[$i] =~ /p/i ) {
			$Enable{"p"} = 1;
		}
	} elsif( $ARGV[$i] =~ /^-s/i ) {
		$Speed = $ARGV[$i];
		$Speed =~ s/^-[sS]//;
		&logger( "Connection speed $Speed" );
	} elsif( $ARGV[$i] =~ /^-wazoo$/i ) {
		$method = "wazoo";
	} elsif( $ARGV[$i] =~ /^-bark$/i ) {
		$method = "bark";
	} elsif( $ARGV[$i] =~ /^-p$/i ) {
		$link = "p";
	} elsif( $ARGV[$i] =~ /^-l$/i ) {
		$link = "l";
	} elsif( !defined( $address )) {
		$address = $ARGV[$i];
	} elsif( !defined( $inpfile )) {
		$inpfile = $ARGV[$i];
	} elsif( !defined( $outfile )) {
		$outfile = $ARGV[$i];
	} elsif( !defined( $repfile )) {
		$repfile = $ARGV[$i];
	} else {
		&logger( "Error: too many arguments - $arg" );
		exit( 1 );
	}
}
if( !defined( $repfile )) {
		&logger( "Error: invalid arguments - $arg" );
		exit( 1 );
}
$method = "none" if( !defined( $method ));
$link = "a" if( !defined( $link ));

&logger( "Start: $method $link $address" );

$DbmPathTst = $DbmPath;
$DbmPathTst .= ".dir" if( $UseGDBM_File == 0 );
if( ! -f $DbmPathTst ) {
	&logger( "Cannot open $DbmPath. $!" );
	close( LOG );
	exit( 1 );
}
if( !dbmopen( Files, $DbmPath, 0664 )) {
	&logger( "Cannot open $DbmPath. $!" );
	close( LOG );
	exit( 1 );
}
	
if( !open( inpfile )) {
	&logger( "Cannot open input file $inpfile. $!" );
	dbmclose( Files );
	close( LOG );
	exit( 1 );
}
$fname = $outfile;
$outfile = ">".$outfile;
if( !open( outfile )) {
	&logger( "Cannot create output file $fname. $!" );
	dbmclose( Files );
	close( inpfile );
	close( LOG );
	exit( 1 );
}
$fname = $repfile;
$repfile = "| ".$Koi2Alt." >".$repfile;
if( !open( repfile )) {
	&logger( "Cannot create report file $fname. $!" );
	dbmclose( Files );
	close( inpfile );
	close( outfile );
	close( LOG );
	exit( 1 );
}

if(( $link eq "p" ) && ( $Enable{"p"} == 0 )) {
	$link = "l";
	print repfile "\nYour access level is limited to 'listed systems' (not 'protected') at this line/system.\n";
	&logger( "Change access level from 'p' to 'l'." );
}
if(( $link eq "l" ) && ( $Enable{"l"} == 0 )) {
	$link = "a";
	print repfile "\nYour access level is limited to 'unlisted systems' at this line/system.\n";
	&logger( "Change access level from 'l' to 'a'." );
}
if(( $link eq "a" ) && ( $Enable{"a"} == 0 )) {
	print repfile "\nYour haven't access to files at this line/system.\n";
	$link = "n";
	&logger( "Change access level from 'a' to 'n'." );
}
$TotalFiles = 0;
$TotalBytes = 0;
while( <inpfile> ) {
	chop;
	$_ =~ s/[ \t]+//g;
	next if( /^$/ );
	next if( /^\|/ );
	&ProcessFile( split( /\|/, $_, 3 ));
}

print repfile "\n\tYour limits: $FilesLimit{$link} files, $BytesLimit{$link} bytes\n\n";
print repfile "\tFREQ time access: 22:00-08:00 workday, CM weekend.\n\n";

close( repfile );
close( outfile );
close( inpfile );
dbmclose( Files );
close( LOG );
exit( 0 );

sub ProcessFile {
	local( $file ) = $_[0];
	local( $passwd ) = $_[1];
	local( $abc ) = $_[2];
	local( $path, $fstat, $size, $date, $desc, $rfile );

	$file =~ tr/[a-z]/[A-Z]/;
	$passwd =~ tr/[a-z]/[A-Z]/ if( $passwd );
	print repfile "> $file";
	if( $passwd ) {
		print repfile " !$passwd\n";
	} else {
		print repfile "\n";
	}
	if( defined( $Files{$file} )) {
		( $path, $fstat, $size, $date, $desc ) = split( /[ \t]+/, $Files{$file}, 5 );
		$rfile = substr( $path, rindex( $path, "/" ) + 1 );
		$rfile =~ tr/[a-z]/[A-Z]/;
		if( $fstat eq "-" ) {
			&logger( "Process: $file - access denied for all." );
			print repfile "Sorry, you do not have access to this file\n\n";
			return;
		} elsif(( $fstat eq "l" ) && ( $link eq "a" )) {
			&logger( "Process: $file - access denied for unlisted." );
			print repfile "Sorry, you do not have access to this file\n\n";
			return;
		} elsif(( $fstat eq "p" ) && ( $link ne "p" )) {
			&logger( "Process: $file - access denied for unprotected." );
			print repfile "Sorry, you do not have access to this file.\n\n";
			return;
		}
		if( $size =~ /off/ ) {
			&logger( "Process: $file - off line." );
			print repfile "This file is off line. Call SysOp.\n\n";
			return;
		}
		$TotalBytes += $size;
		if( $BytesLimit{$link} < $TotalBytes ) {
			&logger( "Process: $file - size limit overflow." );
			print repfile "Sorry, your size limit is overflow.\n\n";
			return;
		}
		$TotalFiles++;
		if( $FilesLimit{$link} < $TotalFiles ) {
			&logger( "Process: $file - files limit overflow." );
			print repfile "Sorry, your files limit is overflow.\n\n";
			return;
		}
		print outfile "$path $rfile\n";
		&logger( "Process: $file - $path" );
		&PrintReport( $rfile, $size, $desc );
		print repfile "\n";
	} else {
		&logger( "Process: $file - not found" );
		print repfile "This file is not found\n\n";
	}
	return;
}

sub strtime {
	local( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst );
	local( $tm, $month );
	$tm = time;
	( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( $tm );
	@months = ( Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec );
	return sprintf( "%3s %2d %02d:%02d:%02d", $months[$mon], $mday, $hour, $min, $sec );
}

sub logger {
	print LOG &strtime( "log" )." ifextrp[$$] ".$_[0]."\n";
	system "logger -t ifextrp[$$] $_[0]";
}

sub PrintReport {
	local( $file ) = $_[0];
	local( $size ) = $_[1];
	local( $desc ) = $_[2];
	local( @_desc, @adesc, $str, $l, $w, $s );

	@_desc = split( /[ \t]+/, $desc );
	$str = sprintf( "%-12s %7s", $file, $size );
	$l = length( $str ) + 1;
	$adesc[0] = "";
	for( $w = 0, $s = 0;; $w++ ) {
		if(((( length( $adesc[$s] ) + $l + length( $_desc[$w] )) < $ReportCols )) && ( $w <= $#_desc )) {
			$adesc[$s] .= " ".$_desc[$w];
		} else {
			if( $s == 0 ) {
				print repfile $str.$adesc[$s]."\n";
			} else {
				print repfile " " x $l, $adesc[$s]."\n";
			}
			$s++;
			$adesc[$s] = $_desc[$w];
			last if( $w > $#_desc );
		}
	}
}
