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

require '/afs/sipb/user/mkgray/bin/http.pl';

&load_db();
&http'httpd();

sub user_input {
	local($n, $text)=@_;
	($request, @headers) = split("\n", $text);
	($method, $file, $proto) = split(" ", $request);
	(@foo) = split('/', $file);

	$fcontents = '';
	if($foo[1] eq 'add'){
	    &addform;
	}
	elsif(substr($foo[1], 0, 3) eq 'add'){
	    &addnote(substr($foo[1], 4));
	    &mainpage;
	}
	elsif($foo[1] eq 'search'){
	    &searchform;
	    &mainpage;
	}
	elsif(substr($foo[1], 0, 6) eq 'search'){
	    &dosearch(substr($foo[1],7));
	    &mainpage;
	}
	else{
	    &mainpage;
	}
	&okheader($n, "text/html");
	&http'tell($fcontents, $n);
}

sub okheader {
    local($n, $type) = @_;

    &http'tell("HTTP/1.0 200 OK\n", $n);
    &http'tell("Content-type: ".$type."\n", $n);
    &http'tell("Content-length: ".length($fcontents)."\n", $n);
    &http'tell("\n", $n);
}

#
# Notes DB format includes
# ^notename  will automatically form a link to that note
# notes are separated by ^X's  with the notename separated from the rest by
# a ^W.
#
sub load_db {
    $notesdb = "/tmp/notesdb" unless $notesdb = $ENV{'NOTESDB'};

    $/ = "";
    open(NDB, $notesdb) || return;
    while(<NDB>){
	($notename, $note) = split("", $_);
	$notes{$notename} = $note;
    }
    @nnames = keys %notes;
    @nbodys = values %notes;
    $numnotes = $#nnames;
}


sub save_db {
    rename($notesdb, "/tmp/dbtmp");

    open(NDB, ">>$notesdb");
    for $note (@nnames){
	print(NDB "$note$notes{$note}");
    }
    close(NDB);
}

sub addform {
    $form = <<FormEnd;
<form>
Name: <input name="notename"><p>
Note: <p>
<textarea name="note" rows=17 cols=40></textarea>
<p><input type="submit">
</form>
FormEnd
    $fcontents = $form;
}
sub mainpage {
    $fcontents .= <<PageEnd;
<ul>
<li><a href="add">Add a note</a>
<li><a href="search">Search notes</a>
</ul>
PageEnd
}

sub addnote {
    local($addme) = @_;
    %form = &parseform($addme);
    push(@nnames, $form{'notename'});
    push(@nbodys, $form{'note'});
    $notes{$form{'notename'}} = $form{'note'};

    &present_note($form{'notename'});
    &save_db; 
}

sub searchform {
$fcontents = <<FormEnd
<form>
Search for <input name="searchfor">
</form>
FormEnd
}

sub dosearch {
    local($sfor) = @_;
    %form = &parseform($sfor);
    $sfor = $form{'searchfor'};
    for $name (@nnames){
	if($name =~ /$sfor/i || $notes{$name} =~ /$sfor/i){
	    &present_note($name);
	}
    }
}


sub present_note{
    local($noten) = @_;
    $noteb = $notes{$noten};
    $fcontents .= <<NoteEnd;
<h1>$noten</h1>
<pre>
$noteb
</pre>
NoteEnd
}


sub parseform {
    local($formthing) = @_;     # Expects something like:
                                # foo=wow%21&bar=hello&baz=blah
    local(%lookup);

    (@fields) = split('&', $formthing);
    for $f (@fields){

        ($name, $value) = split('=', $f);
            $value =~ y/\+/ /;
        $value =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig; # remove % escapes
        if(defined $lookup{$name}){
            $lookup{$name} .= "\n".$value;
            }
        else{
          $lookup{$name} = $value;
    }
    }

    %lookup;
}
