require "cgi-lib.pl";
#
$basedir = '/usr/local/archives/public/html/users/gcf/homepage/FFE/';
$dbfile = $basedir.'membersboard.htm';
$mailprog = '/usr/lib/sendmail';
$statusofread = &ReadParse (*formdata);
#
$name='';
$email='';
$subject='';
$message='';
#
unless ($statusofread)  {
    &therewaserror ("**ALERT**ALERT**THERE IS A GLITCH**FBI HAVE BEEN ALERTED**");
    exit;
}
#
$name=$formdata{'name'};
$email=$formdata('email'};
$subject=$formdata('subject'};
$message=$formdata('message'};
#
&therewaserror("Please enter all fields")   unless($name);
&therewaserror("Please enter all fields")   unless($subject);
&therewaserror("Please enter all fields")   unless($message);
$count = 0;
while (!&LockDatabase($dbfile)) { # Sleep for a second
    sleep(1);
++$count;
&therewaserror("Server out to lunch -- please email -- lock stuck")
if($count > 20);
}
#
open(MESSFILE,"<$dbfile") || &therewaserror("Server Error Cant Open Mesage File
for Read -- please email");
@messageb = <MESSFILE> ;
close(MESSFILE);
open(MESSFILE,">$dbfile") || &therewaserror("Server Error Cant Open Message
File for Rewrite -- please email");
#
foreach $line (@messageb)    {

#   Insert Line
    if( $line =~ m%^<!--MESSAGES-->% )  {
print MESSFILE <<EOF;
Message from: <a href="mailto:$email">$name</a><br>
Subject: $subject <br>
Message: <br>
$message <br>
<hr>
EOF
}
    print MESSFILE $line;
}
close(MESSFILE);

#
&UnlockDatabase($dbfile);
#
#   End Up
print "Location: http://www.npac.syr.edu/users/gcf/homepage/FFE/membersboard.htm\n\n";
exit;
#
sub LockDatabase {
# PURPOSE: Attempt to "lock" the database by creating a lock file.
# INPUTS:  Database file name
# OUTPUTS: A boolean telling if the lock was successful

    my ($dbfile) = @_;

    if (-e "$dbfile.lock") {  # DB is already locked
        return 0;
    }
    else {
        system("touch $dbfile.lock");  # Create the lock file
        return 1;
    }
}

sub UnlockDatabase {
# PURPOSE: Attempt to "unlock" the database by deleting the lock file.
# INPUTS:  Database file name
# OUTPUTS: None (but should probably check that the unlock was successful)

    my ($dbfile) = @_;
    unless( (unlink("$dbfile.lock")) )  {
        open (MAIL, "|$mailprog gcf") || (&therewaserror("Server Error Can't Mail -- Please email us")) ;
        print MAIL <<EOF;
From:gcf
To:gcf
Subject:Error in unlink
------------------------------------------------------

Please check messageb.pl
EOF
        close(MAIL);
    }
}
#
sub therewaserror   {

    my($mess) = @_;
#
    print <<EOF;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Error in Message Board Proccessing</TITLE>
</HEAD>
<BODY>
<P>
<h2>Error in Processing Message Board: $mess</H2>
Please correct or if it's our fault
<A HREF="mailto:psf\@npac.syr.edu?subject=Message Board Error $mess"
onMouseOver="window.status=' Sadness!.';return true">Mail Complaint</A>
<hr>
<CENTER><TT><FONT SIZE=5>FFE Message Board</FONT></TT></CENTER>
<P>
<CENTER><TT><FONT SIZE=5>This was made by Pam Fox, a kid!</FONT></TT></CENTER>
<P>
<CENTER><TT><FONT SIZE=5>You must enter input in ALL FIELDS!</FONT></TT></CENTER>
<FORM ACTION="http://boss.npac.syr.edu:8080/cgi-bin/foxmaniac/messageb.pl" METHOD="POST"
ENCTYPE="application/x-www-form-urlencoded">
<P>
<CENTER><TT>Name:</TT><INPUT NAME="name" VALUE="$name" MAXLENGTH="40" SIZE=25></CENTER>
<P>
<CENTER><TT>Email: </TT><INPUT NAME="email" VALUE="$email" MAXLENGTH="40" SIZE=25></CENTER>
<P>
<CENTER><TT>Subject: </TT><INPUT NAME="subject" VALUE="$subject" MAXLENGTH="40" SIZE=25></CENTER>
<P>
<CENTER><TT>Message:</TT></CENTER>
<P>
<CENTER>
<TEXTAREA NAME="message" VALUE="$message ROWS=1 COLS=25>
</TEXTAREA>
</CENTER>
<P>
<CENTER> <INPUT TYPE=SUBMIT VALUE="Post The Message" NAME="submit"></CENTER>
</FORM>

</BODY>
</HTML>

EOF
    exit 1;
}

