#!/usr/bin/perl
use CGI qw(:standard);

print <<eND;
Content-type: text/html

eND

$dirlocation = "/home12c/sub007/sc13648-GPVH/";
chdir($dirlocation) || print "Couldn't change directory $!<BR>\n";

$time = time;

$cookieid = "cpid";

$cookie = $ENV{"HTTP_COOKIE"};
$cookie =~ m/$cookieid\=([^;]+)/;
$id = $1;
exit unless $id;

open(DATA,"cp/conversations/gcommlist.txt") || open(DATA,"cp/conversations/gcommlist.txt") || print "Problem with reading gcommlist: $!<BR>\n";
CHECKLOOP: while ($checkid = <DATA>) {
	$ci = substr($checkid,0,-1);
	if ($ci eq $id) {
		$fromname = <DATA>;
		$house = <DATA>;
		last CHECKLOOP;
	}
	else {
		$temp = <DATA>;
		$temp = <DATA>;
	}
}
close(DATA);
chomp($house);


$event = param("event");

$msgid = param("msgid");

if ($event eq "discard") {
	open(DATA,"cp/conversations/convs/$msgid.txt") || open(DATA,"cp/conversations/convs/$msgid.txt") || print "Couldn't write conv file: $!<BR>\n";
	@pids = <DATA>;
	close(DATA);

	$target = "$id\n";
	open(DATA,">cp/conversations/convs/$msgid.txt") || open(DATA,">cp/conversations/convs/$msgid.txt") || print "Couldn't write conv file: $!<BR>\n";
	foreach $pid (@pids) {
		print DATA $pid unless $pid eq $target;
	}
	close(DATA);
}

if ($event eq "discard" || $event eq "hide" || $event eq "selfarchive") {
	@lines = ();
	open(DATA,"cp/conversations/people/$id.txt") || open(DATA,"cp/conversations/people/$id.txt") || print "couldn't open: $!<BR>\n";
	while ($thisid = <DATA>) {
		if ($thisid =~ m/$msgid/o) {
			$temp01 = <DATA>;
			$temp02 = <DATA>;
			$temp03 = <DATA>;
			$temp04 = <DATA>;
		}
		else {
			$temp1 = <DATA>;
			$temp2 = <DATA>;
			$temp3 = <DATA>;
			$temp4 = <DATA>;
			push @lines, $thisid, $temp1, $temp2, $temp3, $temp4;
		}
	}
	close(DATA);

	open(DATA1,">cp/conversations/people/$id.txt") || open(DATA1,">cp/conversations/people/$id.txt") || print "couldn't open for writing: $!<BR>\n";

	foreach (@lines) {
		print DATA1;
	}
	close(DATA1);
}

if ($event eq "selfarchive") {
	open(DATA2,">>cp/conversations/selfarchive/$id.txt") || open(DATA2,">>cp/conversations/people/$_.txt") || print "Couldn't add to id file: $!<BR>\n";
	print DATA2 "$msgid\n$temp01$temp02$temp03$temp04";
	close(DATA2);
}

if ($event eq "library" || $event eq "bb" || $event eq "harch") {
	open(DATA,"cp/conversations/convs/$msgid.txt") || open(DATA,"cp/conversations/convs/$msgid.txt") || print "Couldn't write conv file: $!<BR>\n";
	@pids = <DATA>;
	close(DATA);
	open(DATA,">cp/conversations/convs/$msgid.txt") || open(DATA,">cp/conversations/convs/$msgid.txt") || print "Couldn't write conv file: $!<BR>\n";
	foreach $pid (@pids) {
		print DATA $pid unless $pid eq $event;
	}
	print DATA "$event\n";
	close(DATA);

#searching for the heading
	open(DATA,"cp/conversations/people/$id.txt") || open(DATA,"cp/conversations/people/$_.txt") || print "Couldn't open id file $_: $!<BR>\n";
	LOOKINGLOOP: while ($thisid = <DATA>) {
		if ($thisid =~ m/$msgid/o) {
			$temp = <DATA>;
			$currentheading = $temp;
			$temp2 = <DATA>;
			$currenttofrom = $temp2;
			$temp3 = <DATA>;
			$currentlast = $temp3;
			$temp4 = <DATA>;
			$currentlasttime = $temp4;
			last LOOKINGLOOP;
		}
		else {
			$temp1 = <DATA>;
			$temp2 = <DATA>;
			$temp3 = <DATA>;
			$temp4 = <DATA>;
		}
	}
	close(DATA);

#adding to library information or bb information
	open(DATA,">>cp/conversations/people/$event.txt") || open(DATA,">>cp/conversations/people/$event.txt") || print "Couldn't open $event file: $!<BR>\n";
	print DATA "$msgid\n$currentheading$currenttofrom$currentlast$currentlasttime";
	close(DATA);
}

if ($event eq "hidebyheading") {
	exit unless $msgheading;
	@lines = ();
	open(DATA,"cp/conversations/people/$id.txt") || open(DATA,"cp/conversations/people/$id.txt") || print "couldn't open: $!<BR>\n";
	while ($thisid = <DATA>) {
		$temp1 = <DATA>;
		if ($temp1 =~ m/^$msgheading$/o) {
			$temp02 = <DATA>;
			$temp03 = <DATA>;
			$temp04 = <DATA>;
		}
		else {
			$temp2 = <DATA>;
			$temp3 = <DATA>;
			$temp4 = <DATA>;
			push @lines, $thisid, $temp1, $temp2, $temp3, $temp4;
		}
	}
	close(DATA);

	open(DATA1,">cp/conversations/people/$id.txt") || open(DATA1,">cp/conversations/people/$id.txt") || print "couldn't open for writing: $!<BR>\n";

	foreach (@lines) {
		print DATA1;
	}
	close(DATA1);
}

print "<a href=/cgi-bin/rare2/mail.pl>return</a><BR>\n";

exit;