#!@PERLBIN@
require 5.000;

# Set your INIT variable as it may be incorrect.
#
# FBB F_Filter
#
# Author:  Stewart Wilkinson G0LGS
#
$Vers="0.60";
$VDate="21/01/2020";
$VData="F_Filter for LinFBB (G0LGS V${Vers} ${VDate}).\nModernized by N1URO";

# Virus Scanner Command line (if available)
$SCAN="@SCANNER@ --delete --verbose";

#
# Nothing beyond here should need changing (unless its broken)
#

#$DEBUG=1;

# Where is FBB Setup File
$INIT="@FBBCONF@/fbb.conf";

# Read things we need from Setup File
if(! open(CONFIG, $INIT) ) {
   printf STDERR "Unable to open $INIT\n";
   exit 0;
}

while($line = <CONFIG>) {

   chomp($line);

   if( $line =~ /^\s*\#/) {
      next;

   }elsif( $line =~ /^\s*$/) {
      next;

   }else{
      $line =~ /^\s*(\S*)\s*\=\s*(.*)$/;
      $item = $1;
      $value = $2;

      if( $item =~ /callsign/ ){
         $value =~ /^(\w*)\.(.*)$/;
         $BBSCALL = $1;
         $BBSHR = $2;

      }elsif( $item =~ /data/ ) {
         $value =~ /^(\S*)$/;
         $INFSYS = $1 . "/inf.sys";

      }elsif( $item =~ /config/ ){
         $value =~ /^(\S*)$/;
         $CONF = $1;

      }elsif( $item =~ /name/ ){
         $value =~ /^(\S*)$/;
         $NAME = $1;

      }elsif( $item =~ /sysop/ ){
         $value =~ /^(\S*)$/;
         $SYSCALL = $1;

      }elsif( $item =~ /import/ ){
         $value =~ /^([^\.]\S*)\.(\S*)$/;
         $MAIL = "$1.$2";
         $LOCK = "$1.lck";
 
      }
   }
}

close(CONFIG);

# Our Config file.
$Config="${CONF}/f_filter.cfg";

# Wrong number of Parameters ?
if( $#ARGV < 3 ){
   printf STDERR "%s\n", ${VData};
   if( ! -f ${Config} ){
      printf STDERR "WARNING: The Config File %s required by f_filter is missing or unreadable.", ${Config};
      printf STDERR " Please correct the problem, as File Filtering will not work without it.\n";
   }
   exit 0;
}

# The Parameters
$Callssid=$ARGV[0];
$UNK=$ARGV[1];
$RecNo=$ARGV[2];
$DatName=$ARGV[3];

$Callssid =~ /(\S*)\-\d/;
$Callsign = $1;

# Some Default settings
$MAILSYSOP=1;

# Read Our Config file 

if(! open(CFG, "<${Config}") ) {
   if( ! -f "${CONF}/f_filter.warn" ){
      if( open(LOCK,"> ${LOCK}" ) ) {
         printf LOCK "$$";
         if( open(MAIL,">> ${MAIL}" ) ){
            printf MAIL "SP %s < FILTER\n", ${SYSCALL};
            printf MAIL "WARNING: F_Filter Config Error\n\n";
            printf MAIL "The Config File %s required by f_filter is missing or unreadable.\n", ${Config};
            printf MAIL "Please correct the problem, as File Filtering will not work without it.\n";
            printf MAIL "/EX\n";
            close(MAIL);
         }
         close(LOCK);
         unlink(${LOCK});
      }

      if( open(WRN,"> ${CONF}/f_filter.warn" ) ){
         print WRN "f_filter is not correctly configured !";
         close WRN;
      }
   }

   exit 0;
}

if( -f "${CONF}/f_filter.warn" ){
   unlink( "${CONF}/f_filter.warn" );
}


while(<CFG>) {

   chomp;

   if(/^\s*\;/) {
      next;

   }elsif(/^\s*$/) {
      next;

   }elsif(/^\s*\#\S*/){
      next;

   }elsif(/^\s*\!\s*MAIL\s*\=\s*(\S*)/){
      $MAILSYSOP = &IsEnabled($1);
      next;

   # Ignore unknown config lines.
   }elsif(/^\s*\!\S*/i) {
      next;

   }
}
close(CFG);

# Some information from INF.SYS

# Open inf.sys
if( open(INF, "< ${INFSYS}" ) ) {

   # Move to the appropriate Record
   seek(INF, ${RecNo} * 360, 0);

   # Read the Record
   if( read( INF, $Rec, 360 ) == 360 ) {

      ($call,$digis,$lastmes,$nbcon,$hcon,$lastyap,$flags,$on,
      $nbl,$lang,$newbanner,$download,$free,$thema,$Nom,$Name,
      $Addr,$City,$Phone,$Jphone,$Home,$QTH,$Pri,$Lc,$Pass,$Zip) = "";

      # Extract Data from Record.
      ($call,$digis,$lastmes,$nbcon,$hcon,$lastyap,$flags,$on,
      $nbl,$lang,$newbanner,$download,$free,$thema,$Nom,$Name,
      $Addr,$City,$Phone,$Jphone,$Home,$QTH,$Pri,$Lc,$Pass,$Zip) =
      unpack("A8 A64 l l l l s s c c l s A20 c A18 A13 A61 A31 A13 A13 A41 A7 A13 A7 A13 A9", $Rec);

      if( defined($DEBUG) ) {
         printf STDERR "call: $call\n";
         printf STDERR "digis: $digis\n";
         printf STDERR "lastmes: $lastmes\n";
         printf STDERR "nbcon: $nbcon\n";
         printf STDERR "hcon: $hcon\n";
         printf STDERR "lastyap: $lastyap\n";
         printf STDERR "flags: $flags\n";
         printf STDERR "on: $on\n";
         printf STDERR "nbl: $nbl\n";
         printf STDERR "lang: $lang\n";
         printf STDERR "newbanner: $newbanner\n";
         printf STDERR "download: $download\n";
         printf STDERR "free: $free\n";
         printf STDERR "thema: $thema\n";
         printf STDERR "Nom: $Nom\n";
         printf STDERR "Name: $Name\n";
         printf STDERR "Addr: $Addr\n";
         printf STDERR "City: $City\n";
         printf STDERR "Phone: $Phone\n";
         printf STDERR "Jphone: $Jphone\n";
         printf STDERR "Home: $Home\n";
         printf STDERR "QTH: $QTH\n";
         printf STDERR "Pri: $Pri\n";
         printf STDERR "Lc: $Lc\n";
         printf STDERR "Pass: $Pass\n";
         printf STDERR "Zip: $Zip\n";
      }

      close(INF);
   }

}else{
   printf STDERR "\nUnable to open ${INFSYS}\n\n";
}

if( defined(${Name}) ){
   printf STDOUT "\nThank you ${Name},\n\n";
}else{
   printf STDOUT "\nThank you ${Callsign},\n\n";
}

# Get Info about upload
if( open( DAT, "< ${DatName}" ) ) {

   while(<DAT>) {
      chomp;

      if(/^\s*\;/) {
         next;

      }elsif(/^\s*$/) {
         next;

      }elsif(/^\s*\#\S*/){
         next;

      }elsif(/^\s*TempName\s*\=\s*(\S*)/){
         $TempName = $1;
         next;

      }elsif(/^\s*FileName\s*\=\s*(\S*)/){
         $Filename = $1;
         next;

      }elsif(/^\s*Label\s*\=\s*(\S*)/){
         $Label = $1;
         next;

      # Ignore unknown lines.
      }elsif(/^\s*\!\S*/i) {
         next;

      }
   }

   close(DAT);

}else{

   printf STDERR "\nUnable to check your upload - Please inform Sysop\n\n";
   exit 0;
   
}


if( defined($SCAN) ){
   printf STDOUT "Please wait a moment ...\n";
   printf STDOUT "The system is checking your upload for Virus(es)...\n";

   if( ! open(SC, "${SCAN} ${TempName} |") ) {
      printf STDOUT "Unable to check ${Filename} for Virus(es)\n";
      $Err=1;
   }else{
      while( $L = <SC> ){
         chomp($L);
         push( @Res, $L);
      }
      close(SC);

      $Scan=$?;
      if( ${Scan} >= 128 ){
         printf STDOUT "An Error occured while Checking ${Filename} for Viruses.\n";
      }elsif( ${Scan} != 0 ){
         printf STDOUT "\nOne or more Virus(es) were detected in ${Filename} :\n";
         foreach $Res (@Res){
            printf STDOUT "${Res}\n";
         }
         printf STDOUT "The file has been deleted, Please Check your system for Virus(es)\n";
         printf STDOUT "and upload the file again another time.\n";
         unlink( ${Filename} );
      }else{
         printf STDOUT "\nThe Virus Check Completed OK - No Virus(es) were found.\n";
      }
   }
}

if( ( defined(${MAILSYSOP}) && ${MAILSYSOP} ) ) {

   if( open(LOCK,"> ${LOCK}" ) ) {

      printf LOCK "$$";

      if( open(MAIL,">> ${MAIL}" ) ){

         printf MAIL "#\nSP %s < FILTER\n", ${SYSCALL};
         printf MAIL "New File '${Filename}'\n\n";

         printf MAIL "A new file '${Filename}' was uploaded by : %s (%s)\n", ${Name}, ${Callsign};

         if( defined($Err) ){
            printf MAIL "\nNote:\tThe System was unable to Virus Check the upload.\n";
            printf MAIL "\tYou are advised to check the upload manually.\n";
         }

         if( defined($Scan) ){
            if( ${Scan} >= 128 ){
               printf STDOUT "An Error occured while Checking ${Filename} for Viruses\n";
               printf STDOUT "Virus Scanner Return Value = ${Scan}\n";
            }elsif( ${Scan} != 0 ){
               printf STDOUT "One or more Viruses were detected in the upload\n";
               printf STDOUT "Here is the report from the Virus Scanner:\n";
               foreach $Res (@Res){
                  printf STDOUT "${Res}\n";
               }
               printf STDOUT "Virus Scanner Return Value = ${Scan}\n";
               printf STDOUT "The file was deleted\n";
            }
         }

         printf MAIL "\n\n%s\n", ${VData};

         printf MAIL "/EX\n";
         close(MAIL);
      }

      close(LOCK);
      unlink(${LOCK});

      print STDOUT "\nThe Sysop has been advised of your Upload\n";
   }
}

print STDOUT "\n";

exit 0;

sub IsEnabled
{
   local($Ret) = 0;
   local($P) = $_[0];

   if( ($P =~ /^0/) || ($P =~ /^n\S*/i) || ($P =~ /^off/i) || ($P =~ /^di\S*/i) ){
         ${Ret}=0;

   }elsif( ($P =~ /^1/) || ($P =~ /^y\S*/i) || ($P =~ /^ok/i) || ($P =~/^on/i) || ($P =~ /^en\S*/i) ){
         ${Ret}=1;

   }

   return ${Ret};
}
