#-*-perl-*-
#
# Command Handler

#
# Add Project adds a project
#
sub addproject {
}

sub removepost {
    return if (!$allowremove);

    # Open the incoming mail and the outgoing mail port
    open (ORIG, "<$hthome/$$.txt");
    while (<ORIG>) {
	tr/[a-z]/[A-Z]/;
	last if (/^\s*COMMAND\s+REMOVEPOST/);
    }
    # OK, now we have to go as many blank lines as
    while (<ORIG>) {
	if (! (m/^\s*$/) ) {
	    $infile = $_;
	    chop $infile;
	    $infile =~ s/^[ >]*//;
	    last;
	}
    }

    if ($infile =~ m:/:) {
	close ORIG;
	&oldremovepost;
    }

    ($remvproject, $remvdir) = split(' ',$infile);

    open (OUTGOING, "| sendmail $MainHeader{'email'}");
    print OUTGOING "From: $originator \(CoCoBoard Command Parser\)\n\n";
    print OUTGOING "Output from your Removal Request\n";
    print OUTGOING "--------------------------------\n";
    # Scan until we get to the COMMAND line

    close ORIG;
    $sesame = open (KILLME,"$hthome/$remvproject/$remvdir/index.html");

    if (!$sesame) {
	print OUTGOING "ERROR!!!\n";
	print OUTGOING "Could not open $hthome/$remvproject/$remvdir/index.html\n";
	print OUTGOING "Please be sure you specified the correct name for\n";
	print OUTGOING "Removal.\n\n$outgoingsig\n";
	close OUTGOING;
	&gohome;
    } else {
	print OUTGOING "Successfully opened $infile\n";
    }

    while (<KILLME>) {
	if (m/^\<b\>From\:\<\/b\>/) {
	    $FromLine = $'; #'
	    $FromLine = m/\b(\S+\@\S+)\b/;
	    $killedemail = $1;
	}
	last if (/\<hr\>/);
    }
    print "$MainHeader{'email'}\n";
    if (!($killedemail =~ $MainHeader{'email'})) {
	print OUTGOING "\n\nAUTHENTICATION ERROR!!\n";
	print OUTGOING "Your Email address, $MainHeader{'email'} does not\n";
	print OUTGOING "match the email address of the poster, $killedemail\n";
	print OUTGOING "Therefore, we will not allow you to remove this\n";
	print OUTGOING "posting.  If you have questions about this,\n";
	print OUTGOING "please contact $maintainer\n";
	&gohome;
    }
    # Now, find the project and test it for validity.
    $infile =~ m/^\s*(\S+)\s+(\S+)/;
    $theproject = $1;
    $themessfolder = $2;
    $isaproject = 0;

    foreach $VALIDPROJ (keys %projects) {
	$isaproject = 1 if ($theproject =~ /\b$VALIDPROJ\b/);
    }
    if (!$isaproject) {
	print OUTGOING "INTERNAL ERROR $theproject is not a project\n";
	print OUTGOING "Please contact $maintainer\n";
	close OUTGOING;
	exit(0);
    }

    if ($themessfolder) {
	# Remove the appropriate directory
	system "/bin/rm -rf $hthome/$theproject/$themessfolder/";
    }

    # Open the index and strip out any references to this article
    open (IND, "< $hthome/$theproject/index.html" ) || die "no index $hthome/$theproject/index.html";

    
    open (NEWIN, "> $hthome/tmp.new" ) || die "no output";
    while (<IND>) {
	print NEWIN if (!(m/\<a href=\"$themessfolder\/index.html\"\>/));  #"
    }
    close IND;
    close NEWIN;
    unlink("$hthome/$theproject/index.html");
    system "mv $hthome/tmp.new $hthome/$theproject/index.html";
    # Clean up
    system("chmod go+r $hthome/$theproject/index.html");

    print OUTGOING "\n$infile\nSuccesfully Removed\n";
    close OUTGOING;
}


sub oldremovepost {
    # Open the incoming mail and the outgoing mail port
    open (ORIG, "<$hthome/$$.txt");
    open (OUTGOING, "| sendmail $MainHeader{'email'}");
    print OUTGOING "From: $originator \(Projects Command Parser\)\n\n";
    print OUTGOING "Output from your Removal Request\n";
    print OUTGOING "--------------------------------\n";
    # Scan until we get to the COMMAND line
    while (<ORIG>) {
	tr/[a-z]/[A-Z]/;
	last if (/^\s*COMMAND\s+REMOVEPOST/);
    }
    # OK, now we have to go as many blank lines as
    while (<ORIG>) {
	if (! (m/^\s*$/) ) {
	    $infile = $_;
	    chop $infile;
	    $infile =~ s/^[ >]*//;
	    last;
	}
    }
    print "Trying to remove $hthome/$infile\n";
    close ORIG;
    $sesame = open (KILLME,"$hthome/$infile");

    if (!$sesame) {
	print OUTGOING "ERROR!!!\n";
	print OUTGOING "Could not open $hthome/$infile\n";
	print OUTGOING "Please be sure you specified the correct name for\n";
	print OUTGOING "Removal.\n\n$outgoingsig\n";
	close OUTGOING;
	&gohome;
    } else {
	print OUTGOING "Successfully opened $infile\n";
    }
    while (<KILLME>) {
	if (m/^\<b\>From\:\<\/b\>/) {
	    $FromLine = $'; #'
	    $FromLine = m/\b(\S+\@\S+)\b/;
	    $killedemail = $1;
	    print "ORIGINATED BY $killedemail\n";
	}
	last if (/\<hr\>/);
    }
    print "$MainHeader{'email'}\n";
    if (!($killedemail =~ $MainHeader{'email'})) {
	print OUTGOING "\n\nAUTHENTICATION ERROR!!\n";
	print OUTGOING "Your Email address, $MainHeader{'email'} does not\n";
	print OUTGOING "match the email address of the poster, $killedemail\n";
	print OUTGOING "Therefore, we will not allow you to remove this\n";
	print OUTGOING "posting.  If you have questions about this,\n";
	print OUTGOING "please contact $maintainer\n";
	&gohome;
    }
    # Now, find the project and test it for validity.
    $infile =~ m/^\s*(\S+)\/(\S+)/;
    $theproject = $1;
    $thefname = $2;
    $isaproject = 0;

    foreach $VALIDPROJ (keys %projects) {
	$isaproject = 1 if ($theproject =~ /\b$VALIDPROJ\b/);
    }
    if (!$isaproject) {
	print OUTGOING "INTERNAL ERROR $theproject is not a project\n";
	print OUTGOING "Please contact $maintainer\n";
    }

    print "Setting Project to $1\n";

    # Continue going through killme and strip out all the references
    # which are local into an array unlinkthese

    @unlinkthese = ("$hthome/$infile", "$hthome/$infile.removal.html");
    while (<KILLME>) {
	last if (/\<address\>/);
	if (m/\<a href=\"(\S+)\">/) { #"
	    print "LINK1! $1\n";
	    @unlinkthese = (@unlinkthese, "$hthome/$theproject/$1");
	}
	if (m/\<img src=\"(\S+)\"/) { #"
	    print "LINK2! $1\n";
	    @unlinkthese = (@unlinkthese, "$hthome/$theproject/$1");
	}
    }
    foreach $KILLTHIS (@unlinkthese) {
	print OUTGOING "Removing: $KILLTHIS\n";
	unlink ($KILLTHIS) if (-e $KILLTHIS);
    }

    # Open the index and strip out any references to this article
    open (IND, "< $hthome/$theproject/index.html" ) || die "no index $hthome/$theproject/index.html";

    
    open (NEWIN, "> $hthome/tmp.new" ) || die "no output";
    while (<IND>) {
	print NEWIN if (!(m/\<a href=\"$thefname\"\>/));  #"
    }
    close IND;
    close NEWIN;
    unlink("$hthome/$theproject/index.html");
    system "mv $hthome/tmp.new $hthome/$theproject/index.html";
    # Clean up
    system("chmod go+r $hthome/$theproject/*");
    print OUTGOING "\n$infile\nSuccesfully Removed\n";
    close OUTGOING;
}

1;
