#!/usr/npac/bin/perl
######################################################################
#  BEFORE TRYING TO EDIT THIS SCRIPT, READ THE README FILE
###################################################################### 
#
#     Dream Catchers CGI Scripts               Feel free to modify 
#     Post It!                                 this script to your 
#     Created by Seth Leonard                  needs, but please
#     for Dream Catchers Technologies, Inc.    keep this portion so
#                                              that I get credit.  
#     http://dreamcatchersweb.com/scripts      The same goes for 
#                                              distribution.
#
#     (c)1996/1997 Dream Catchers Technologies, Inc.,
#     All Rights Reserved
#
######################################################################
# ONLY EDIT THIS PART OF THE SCRIPT!!!!

$postpage = "http://www.foxsden.org/psf/f/cgi/postings.htm";
$postname = "My Post-It Page";
$postpath = "/npac/server/foxsden00/sroot/droot/psf/f/cgi/postings.htm";
$cgilocation = "http://www.foxsden.org/psf/f/cgi/post.cgi";
$mailprog = '/bin/mail';
$youmail = 'psf@npac.syr.edu';
$yourname = "Pam Fox";
$getdate = "/bin/date";

# DO NOT EDIT BELOW THIS LINE!!!!
#####################################################################

# Get Date and Time from Server
$date = `$getdate +"%B %d, %Y"`; chop($date);

# Receive info from Posting
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/<!--(.|\n)*-->//g;
	$value =~ s/<([^>]|\n)*>//g;
	$INPUT{$name} = $value;
}

# Give error message to left out categories
&no_name unless $INPUT{'name'};
&no_post unless $INPUT{'post'};

# Write info to Post Page
open (FILE,"$postpath") || die "Can't Open $postpath: $!\n";
@lines = <FILE>;
close(FILE);

# Open Link File to Output
open (POST,">$postpath") || die "Can't Open $postpath: $!\n";

foreach $line (@lines) {
	if ($line =~ /<!--begin-->/) { 
		print POST ("<!--begin-->\n");
		print POST ("<b>$INPUT{'name'}</b><br>\n");
		if ($INPUT{'email'}) {
			print POST ("<a href=\"mailto:$INPUT{'email'}\">$INPUT{'email'}</a><br>");
		}
		print POST ("$date<hr>\n");
		print POST ("$INPUT{'post'}<hr><br><br>\n");
	} else {
		print POST ("$line");
	}
}

close (POST);

# Send info to you

open (MAIL, "|$mailprog $youmail");

if ($INPUT{'email'} =~ /(.*)@(.*)/) {
	print MAIL "From: $INPUT{'name'} <$INPUT{'email'}>\n";
} else {
	print MAIL "From: $INPUT{'name'}\n";
}
print MAIL ("Subject: Posting\n\n");
print MAIL ("$INPUT{'name'} has posted something.\n\n");
print MAIL ("You can send response email to $INPUT{'email'}\n\n");
print MAIL ("Here is the new posting:\n\n");
print MAIL ("$INPUT{'name'}\n");
if ( $INPUT{'email'} ){
	print MAIL ("$INPUT{'email'}\n");
}
print MAIL ("$date\n");
print MAIL ("----------------------------------------------------------\n\n");
if ($INPUT{'post'}) {
	print MAIL ("$INPUT{'post'}");
}

close (MAIL);

# Send info to visitor
if ($INPUT{'email'}) {
	if ($INPUT{'email'} =~ /(.*)@(.*)/) {
		open (MAIL, "|-") || exec $mailprog,$INPUT{'email'} || die "Can't open $mailprog!\n";
		print MAIL ("From: $yourname <$youmail>\n");
		print MAIL ("Subject: Posting\n\n");
		print MAIL ("$INPUT{'name'},\n\n");
		print MAIL ("Thank you for making a posting!  You can see your posting at $postpage\n");
		print MAIL ("If you need to contact me, you can email me at $youmail.\n\n");
		print MAIL ("Thanks again!\n");
		close (MAIL);
	}
}

&follow_up;

# Error Messages

sub no_name {

print ("Content-type: text/html\n\n");
print ("<html><head><title>No Name</title></head>\n");
print ("<body><h1>Your Name appears to be blank</h1>\n");
print ("You didn't add your name so your entry was not posted.\n");
print ("Please add your name below.<br>\n");
print ("<form method=POST action=\"$cgilocation\">\n");
print ("Your Name:<input type=text name=\"name\" size=30><br>\n");
print ("<input type=hidden name=\"email\" value=\"$INPUT{'email'}\" size=40><br>\n");
print ("<input type=hidden name=\"post\" value=\"$INPUT{'post'}\">\n");
print ("<input type=submit value=\"Post Again\"><hr>\n");
print ("</body></html>\n");

exit;

}

sub no_post {

print ("Content-type: text/html\n\n");
print ("<html><head><title>No Posting</title></head>\n");
print ("<body><h1>Your Posting appears to be blank</h1>\n");
print ("You didn't post anything so your entry was not added.\n");
print ("Please add your posting below:<br>\n");
print ("<form method=POST action=\"$cgilocation\">\n");
print ("<textarea name=\"post\" COLS=60 ROWS=4></textarea>\n");
print ("<input type=hidden name=\"name\" value=\"$INPUT{'name'}\" size=30><br>\n");
print ("<input type=hidden name=\"email\" value=\"$INPUT{'email'}\" size=40><br>\n");
print ("<input type=submit value=\"Post Again\"><hr>\n");
print ("</body></html>\n");

exit;

}

# Print Follow Up HTML
sub follow_up {

print ("Content-Type: text/html\n\n");
print ("<html><head><title>Thank You</title></head>\n");
print ("<body><h1>Thank You For Posting!</h1>\n");
print ("Your entry has been posted.<hr>\n");
print ("Here is what you posted:<p>\n");
print ("<b>$INPUT{'name'}</b><br>\n");
if ($INPUT{'email'}) {
	print ("<a href=\"mailto:$INPUT{'email'}\">$INPUT{'email'}</a><br>\n");
}
print ("$date<hr>\n");
if ($INPUT{'post'}) {
	print ("$INPUT{'post'}<hr>\n");
}
print ("<a href=\"$postpage\">Back to $postname</a>. \n");         
print ("If you do not see your addition, hit RELOAD<br>\n");
print ("<b>Would you like a Post It! like this one? Then go <a href=\"http://dreamcatchersweb.com/scripts/\">Here</a></b></body></html>\n");

exit;

}
