#!/afs/athena/contrib/perl/p

require '/afs/sipb/user/mkgray/bin/http.pl';
&http'httpd();

sub user_input {
	local($n, $text)=@_;
	$cmd = substr($text, index($text, ' ')+1);
	print("Command: $cmd...\n");
	if($cmd eq '/inc'){
		`inc`;
		&http'tell("<h1>Incing Mail</h1><a href=\"read\">Read Mail</a>", $n);
	}
	elsif($cmd =~ /\/read/){
		&http'tell("<dl>\n", $n);
		chdir();chdir('Mail/inbox');
		undef $/;
		while($file = <*>){
			open(MSG, $file);
			$msg{$file} = (<MSG>);
			close(MSG);
		}
		for $file (sort {$b <=> $a;} keys %msg){
			if($file =~ /^\d+$/){
			$msg{$file} =~ /Subject: (.+)/;
			$subj = $1;
			$msg{$file} =~ /From: (.+)/;
			&http'tell("<dt><a href = \"msg$file\">$file\t$1</a>\n<dd>$subj\n", $n);
#			print("Msg num $file\n");
			}
		}
#		print("Done\n");
		&http'tell("</ul>\n", $n);
	}
	elsif(substr($cmd, 0, 4) =~ /\/msg/){
		$num = substr($cmd, 4);
		$msg{$num} =~ s/Received: .+(\n[ \t]+.+)?//g;
		$msg{$num} =~ s/(To: .+)/<h2>\1<\/h2>/;
		$msg{$num} =~ s/(From: .+)/<h2>\1<\/h2>/;
		&http'tell('<pre>'.$msg{$num}.'<pre>', $n);
	}
	else{
		&http'tell("That file does not exist\n", $n);
	}
	&http'disconnect($n);
}
