#!/usr/bin/perl
#
# hate.pl
#
# The Hate File.  Nothing needs explanation.
#
# (c) 2004 Kyle Donaldson <gile@csh.rit.edu>
#

use Digest::MD5 qw(md5_hex);

my $root = "/~gile/hate/hate.cgi";  # the directory containing this...
my $base = $root;
my $pathdata = $ENV{PATH_INFO};
my $filebase = "/u11/gile/web/hate/files";

sub page_top {
	print "Content-Type: text/html\r\n\r\n";
	print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">
<html>
	<head>
		<title>The I Hate People File$addtitle</title>
		<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/~gile/hate/css.css\">
	</head>
	<body>
		<div id=\"container\">
			<div id=\"titlerow\" align=\"center\">
				<img src=\"/~gile/hate/ihp.jpg\" width=\"520\" height=\"160\" border=\"0\" alt=\"I HATE PEOPLE!\">
			</div>
			<div id=\"linkrow\">
				<a href=\"$root\">I Hate People</a> &middot;
				<a href=\"$root/submit\">Submit New Hate</a> &middot;
				<a href=\"$root/search\">Search Hate</a> &middot;
				<a href=\"$root/about\">About the Hate</a> &middot;
				<a href=\"/~gile/hate/files/ihatepeople.rss\">Syndicate</a>
			</div>
			<div id=\"maindata\">\n";
}

sub page_bottom {
	print "			</div>
			<div id=\"crrow\">
				Copyright &copy; 2004 Kyle Donaldson.  Don't Mess With His Rights.
			</div>
		</div>
	</body>
</html>\n";
}

sub almost_die {
	print "$_\n";
	page_bottom();
	die($_);
}

sub write_rss_data {
	my $curdate = `date '+%a, %d %b %Y %H:%M:%S %Z'`;
	open(COUNT, "$filebase/count") or almost_die("Drag.  No Hate File entries :(");
	my $count = <COUNT>;
	$count =~ s/\n//g;
	close(COUNT);
	$count = int($count);
	my $ocount = $count;
	open(RSS, ">$filebase/ihatepeople.rss") or almost_die("Cannot create RSS syndication file.  :-(");
	print RSS "<?xml version='1.0' encoding='utf-8' ?>
	<rss version='2.0'>
	<channel>
		<title>The I Hate People File</title>
		<link>http://csh.rit.edu/~gile/hate/</link>
		<description>These are the reasons I hate people.</description>
		<lastBuildDate>$curdate</lastBuildDate>
		<generator>I Hate People / gile</generator>\n";
	for ($count--; ($count > -1) && (($ocount - $count) < 20); $count--) {
		if (open(FILE, "$filebase/$count.text")) {
			my $subject = <FILE>;
			my $date = <FILE>;
			my $text = "";
			while (<FILE>) { $text .= $_; }
			close(FILE);
			print RSS "     <item>
	<guid isPermaLink='true'>http://csh.rit.edu/$base/entries/$count</guid>
	<pubDate>$date</pubDate>
	<title>$subject</title>
	<link>http://csh.rit.edu/$base/entries/$count</guid>
	<description><!CDATA[[$text]]></description>
	</item>\n";
		}
	}
	print RSS "</channel>\n</rss>\n";
	close(RSS);
}

sub decode_and_write_new {
	my $cgidata = "";
	my $subject = "";
	my $date = `date '+%a, %d %b %Y %H:%M:%S %Z'`;
	my $text = "<p>";
	my $passwerd = "";
	my $password = "dca4cfb0f86f7f0c2fc9acc69f08551c";
	while (<>) { $cgidata .= $_; }
	$cgidata =~ s/\n//g;
	my @pairs = split(/&/, $cgidata);
	foreach (@pairs) {
		@set = split(/=/);
		$set[1] =~ s/\+/ /g;
		$set[1] =~ s/%([0-9A-F][0-9A-F])/chr hex $1/ge;
		if ($set[0] eq "subject") { $subject = $set[1]; }
		if ($set[0] eq "text") { $text .= $set[1]; }
		if ($set[0] eq "passwerd") { $passwerd = $set[1]; }
	}
	if (md5_hex($passwerd) != $password) {
		almost_die("Bad password. KTHXDIENOWBYEBYE.\n");
	}
	$text =~ s/\n\n/<\/p><p>/g;
	$text =~ s/\n/<br>/g;
	$text .= "</p>";
	if (open(COUNT, "$filebase/count")) {
		$count = <COUNT>;
		$count =~ s/\n//g;
		close(COUNT);
	} else {
		$count = 0;
	}
	open(ENTRY, ">$filebase/$count.text") or almost_die("Cannot post new entries.  Sorry :(");
	print ENTRY "$subject\n";
	print ENTRY "$date\n";
	print ENTRY "$text\n";
	close(ENTRY);
	$count++;
	open(COUNT, ">$filebase/count") or almost_die("Cannot post new entries.  Sorry :(");
	print COUNT "$count\n";
	close(COUNT);
	write_rss_data();
}

sub decode_and_write_edit {
	my $id = shift;
	my $cgidata = "";
	my $text = "<p>";
	my $passwerd = "";
	my $password = "dca4cfb0f86f7f0c2fc9acc69f08551c";
	open(ENTRY, "$filebase/$id.text") or almost_die("Cannot edit because I cannot read.  :-(");
	my $subject = <ENTRY>;
	my $date = <ENTRY>;
	$date =~ s/\n//g;
	close(ENTRY);
	while (<>) { $cgidata .= $_; }
	$cgidata =~ s/\n//g;
	my @pairs = split(/&/, $cgidata);
	foreach (@pairs) {
		@set = split(/=/);
		$set[1] =~ s/\+/ /g;
		$set[1] =~ s/%([0-9A-F][0-9A-F])/chr hex $1/ge;
		if ($set[0] eq "subject") { $subject = $set[1]; }
		if ($set[0] eq "text") { $text .= $set[1]; }
		if ($set[0] eq "passwerd") { $passwerd = $set[1]; }
	}
	if (md5_hex($passwerd) != $password) {
		almost_die("Bad password. KTHXDIENOWBYEBYE.\n");
	}
	$text =~ s/\n\n/<\/p><p>/g;
	$text =~ s/\n/<br>/g;
	$text .= "</p>";
	open(ENTRY, ">$filebase/$id.text") or almost_die("Cannot post new entries.  Sorry :(");
	print ENTRY "$subject\n";
	print ENTRY "$date\n";
	print ENTRY "$text\n";
	close(ENTRY);
	$count++;
	open(COUNT, ">$filebase/count") or almost_die("Cannot post new entries.  Sorry :(");
	print COUNT "$count\n";
	close(COUNT);
	write_rss_data();
}

sub decode_and_write_reply {
	my $id = shift;
	my $cgidata = "";
	my $name = "";
	my $text = "";
	while (<>) { $cgidata .= $_; }
	$cgidata =~ s/\n//g;
	my @pairs = split(/&/, $cgidata);
	foreach (@pairs) {
		@set = split(/=/);
		$set[1] =~ s/\+/ /g;
		$set[1] =~ s/%([0-9A-F][0-9A-F])/chr hex $1/ge;
		if ($set[0] eq "name") { $name = $set[1]; }
		if ($set[0] eq "text") { $text = $set[1]; }
	}
	# haha, no html for you.  KTHXBYE.
	$text =~ s/&/&amp;/g;
	$text =~ s/</&lt;/g;
	$text =~ s/>/&gt;/g;
	$text =~ s/\n\n/<\/p><p>/g;
	$text =~ s/\n/<br>/g;
	$text = "<p>$text</p>";
	if (open(COUNT, "$filebase/$id.commentcount")) {
		$count = <COUNT>;
		$count =~ s/\n//g;
		close(COUNT);
	} else {
		$count = 0;
	}
	`mkdir -p $filebase/$id.comments`;
	open(COMMENT, ">$filebase/$id.comments/$count") or almost_die("Cannot post new comments.  Sorry :(");
	print COMMENT "$name\n";
	print COMMENT "$text\n";
	close(COMMENT);
	$count++;
	open(COUNT, ">$filebase/$id.commentcount") or almost_die("Cannot post new comments.  Sorry :(");
	print COUNT "$count\n";
	close(COUNT);
}

sub show_new_form {
	print "<div id=\"reply\">
		<form action=\"$base/postnew\" method=\"POST\">
		Password: <input type=\"password\" name=\"passwerd\" size=\"50\"><br>
		Subject: <input type=\"text\" name=\"subject\" size=\"50\"><br>
		<textarea name=\"text\" rows=\"20\" cols=\"45\"></textarea><br>
		<input type=\"submit\" name=\"submit\" value=\"Submit\">
		</form></div>\n";
}

sub show_entry {
	my $id = shift;
	open(ENTRY, "$filebase/$id.text") or almost_die("Error: Entry $id does not exist or is not available for viewing right now.");
	my $subject = <ENTRY>;
	my $date = <ENTRY>;
	my $text = "";
	while (<ENTRY>) { $text .= $_; }
	close(ENTRY);
	print "<div id=\"entry\">
		<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">
		<tr><td align=\"left\"><b>$subject</b></td>
		<td align=\"right\"><i>$date</i></td>
		</tr></table>
		$text<br>
		<a href=\"$base/entries/$id\">Link</a> &middot;
		<a href=\"$base/entries/$id\">Read Comments</a> &middot;
		<a href=\"$base/discuss/$id\">Share Your Thoughts</a>
		</div>\n";
}

sub show_reply_form {
	my $id = shift;
	show_entry($id);
	print "<div id=\"entry\">
		<form action=\"$base/reply/$id\" method=\"POST\">
		Name: <input type=\"text\" size=\"50\" name=\"name\"><br>
		<textarea rows=15 cols=45 name=\"text\"></textarea><br>
		<input type=\"submit\" name=\"submit\" value=\"Submit\">
		</form></div>\n";
}

sub show_entry_and_comments {
	my $id = shift;
	show_entry($id);
	open(COUNT, "$filebase/$id.commentcount") or almost_die("No comments available.");
	my $count = <COUNT>;
	$count =~ s/\n//g;
	close(COUNT);
	for ($i = 0; $i < $count; $i++) {
		if (open(COMMENT, "$filebase/$id.comments/$i")) {
			my $name = <COMMENT>;
			my $text = "";
			while (<COMMENT>) { $text .= $_; }
			print "<div id=\"reply\">\n";
			if ($name gt "") { print "Name: <i>$name</i><br>\n"; }
			print "$text</div>";
			close(COMMENT);
		}
	}
}

sub show_current {
	open(COUNT, "$filebase/count") or almost_die("Drag.  No Hate File entries :(");
	my $count = <COUNT>;
	$count =~ s/\n//g;
	$count = int($count);
	close(COUNT);
	my $ocount = $count;
	for ($count--; ($count > -1) && (($ocount - $count) < 20); $count--) {
		show_entry($count);
	}
}

sub show_edit_form {
	my $id = shift;
	open(ENTRY, "$filebase/$id.text") or almost_die("Cannot open entry $id for editing.  Drag.");
	my $subject = <ENTRY>;
	$subject =~ s/\n//g;
	my $date = <ENTRY>;
	my $text = "";
	while (<ENTRY>) { $text .= $_; }
	close(ENTRY);
	$text =~ s/<p>//g;
	$text =~ s/<\/p>/\n\n/g;
	$text =~ s/<br>/\n/g;
	print "<div id=\"reply\">
		<form action=\"$base/postedit/$id\" method=\"POST\">
		Password: <input type=\"password\" name=\"passwerd\" size=\"50\"><br>
		Subject: <input type=\"text\" name=\"subject\" size=\"50\" value=\"$subject\"><br>
		<textarea name=\"text\" rows=\"20\" cols=\"45\">$text</textarea><br>
		<input type=\"submit\" name=\"submit\" value=\"Submit\">
		</form></div>\n";
}

page_top();
if ($pathdata eq "/submit") {
	# send me new hate
	print "<div id=\"entry\"><p>This isn't some automated place that you can submit whatever you want and have it show up.  Here, I only allow things that inspire hate in <i>me</i>.  I allow submissions on the assumption that you understand that.</p><p>To submit, <a href=\"mailto:gile\@hatespeople.com?subject=[I Hate People]\">email gile@hatespeople.com</a> with whatever you think inspires my hate.  If it's good, it goes in the file.</p></div>";
} elsif ($pathdata eq "/search") {
	# search the archives
	print "No search yet :-(";
} elsif ($pathdata eq "/about") {
	# about us
	print "What do you need to know?  I hate people, and I'm going to show you why.";
} elsif ($pathdata eq "/new") {
	show_new_form();
} elsif ($pathdata eq "/postnew") {
	# auth and add new
	decode_and_write_new();
	print "<p>Whee!  A successful post!</p>\n";
} elsif ($pathdata =~ /\/entries\/[0-9]*/) {
	# display entry and comments
	$id = $pathdata;
	$id =~ s/\/entries\/([0-9]*)/$1/;
	show_entry_and_comments($id);
} elsif ($pathdata =~ /\/discuss\/[0-9]*/) {
	# add a reply to an entry
	$id = $pathdata;
	$id =~ s/\/discuss\/([0-9]*)/$1/;
	show_reply_form($id);
} elsif ($pathdata =~ /\/edit\/[0-9]*/) {
	$id = $pathdata;
	$id =~ s/\/edit\/([0-9]*)/$1/;
	show_edit_form($id);
} elsif ($pathdata =~ /\/postedit\/[0-9]*/) {
	$id = $pathdata;
	$id =~ s/\/postedit\/([0-9]*)/$1/;
	decode_and_write_edit($id);
} elsif ($pathdata =~ /\/reply\/[0-9]*/) {
	# post the reply
	$id = $pathdata;
	$id =~ s/\/reply\/([0-9]*)/$1/;
	decode_and_write_reply($id);
	print "<b>Reply posted.  Yay.</b>\n";
	show_entry_and_comments($id);
#} elsif ($pathdata eq "/kill") {
#	`rm -rf $filebase/*`
} else {
	# default listing
	show_current();
}
page_bottom();


