#!/home/rebelsky/perl/bin/perl
#This script gets the data from an annotation file and displays it.
#(POD documentation at end)

#################
# Script Begins #
#################

#import modules
use AFile;

sub main 
  {
    #read arguments
    my $name=shift;		# ID of the user for this request (String)
    my $user_info=shift;	# A string with info. on the user (String)
    my $client=shift;		# Client connection (HTTP::Daemon::ClientConn)
    my $client_request=shift;	# Request from the client (HTTP::Request)

    
    #variables
    my $query;                  #the original query string
    my $file;                   #the annotation file address
    my $url;                    #the url of the annotated page
    my $email;                  #the email address of the user
    my $groups;                 #the groups that the user is a member of
    my $lasttime;               #lasttime user viewed this page
    
    # parse the string of user information
    $user_info =~ m/<EMAIL>(.*)<\/EMAIL>/;
    $email = $1;
    $user_info =~ m/<GROUPLIST>(.*)<\/GROUPLIST>/;
    $groups = $1;
    $query = $client_request->url;
    $query =~ /\?(.*)/i;
    $query = $1;
    
    #split the query into parts
    $query =~ /(.*)\&(.*)\&(.*)/i;
    $file = $1;
    $url = $2;
    $lasttime = $3;
    
    #process the annotation file using AFile->readFile
    &readFile(*fields,$file);
    
    #display the annotation
    &showAnnotation(*fields,$url,$name,$email,$groups,$lasttime,$client);
    
  }

###########################################################################
# showAnnotation #
###########################################################################
# This subroutine simply displays an annotation on a web page.
# ARGUMENTS: A typeglob that is a reference to a hash containing the info
# for the annotation obtained by reading the annotation file with
# AFile->readFile, the url of the page that was annotated in string form,
# the user's name in string form and the user's email address in string
# form.
# RETURNS: Returns 1 to say that it ran correctly.
###########################################################################
sub showAnnotation 
  {
    #read in arguments
    local(*data) = shift;       #hash in which annotation info is stored
    my $url = shift;            #the url for the annotated page
    my $name = shift;           #the user viewing the annotation
    my $email = shift;          #the email address of the user
    my $groups = shift;         #the groups that the user is a member of
    my $lasttime = shift;       #lasttime user viewed this page
    my $client = shift;         #the client object
    #other variables
    my $date;                   #the date of the annotation
    my $mon;                    #the month in $date
    my $day;                    #the day in $date
    my $year;                   #the year in $date
    my $protection;             #the protection setting in string form
    my $num;                    #number of commas replaced in group list
    my $response;               #the response to send the client
    my $response_object;        #the client response object

    #split date into month, day, year
    $date = $data{'date'};
    $mon = substr($date,0,2);
    $day = substr($date,2,2);
    $year = substr($date,4,2);
    
    #Turn the protection setting into a readable string.
    $protection = $data{'protection'};
    $protection =~ s/(.*),/$1,and /i;
    $num = ($protection =~ s/,/, /ig);
    
    #if exactly one comma existed in the protection line, we can remove 
    #that comma.
    if ($num == 1)
      {
        $protection =~ s/,//i;
      }                         #if ($num == 1)
    
    #if the author of the annotation is the same as the current user, they 
    #can delete the annotation thus, the delete button should show up.
    if (($data{'author'} eq $name) || ($name eq "sysadmin"))
      {
        $delete = "<br>\n<input type=\"button\" name=\"delete\" value=\"Delete\" onClick=parent.document.location=\"http://www.math.grin.edu/~rebelsky/Blazers/Annotations/Summer1999/delete.cgi?$data{'file'}\&$url\&$name\&$email\&$groups\&$lasttime\">";
      }                         #if ($data{'author'} eq $name)
    
    #display annotation
    $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<script language=\"javascript\">
function newLink(url) 
 {
   parent.opener.window.document.location=url
   parent.opener.focus()
 }
<\/script>
<title>$data{'title'}<\/title>
<\/head>
<body bgcolor=\"white\">
<h1>$data{'title'}<\/h1>
This annotation was made by <a href=\"mailto:$data{'email'}\">$data{'author'}<\/a> on $mon\/$day\/$year at $data{'time'}.
<hr>
<p>
$data{'annotation'}
<\/p>
<br>
<form method=\"post\" name=\"hidden\">
<input type=\"hidden\" name=\"url\" value=\"$url\">
<input type=\"button\" name=\"reply\"  value=\"Reply\" onClick=parent.document.location=\"http://www.math.grin.edu/~rebelsky/Blazers/Annotations/Summer1999/load_add.cgi?reply\&$url\&$data{'file'}\&$name\&$email\&$groups\&$lasttime\">
<input type=\"button\" name=\"close\" value=\"Close\" onClick=parent.close('Annotations')>
<br>
$delete
<\/form>
<br>
<small>
This annotation is for $protection viewing.
<\/small>
<br>
<\/body>
<\/html>
EOF

    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);
    
    #return the true value to say that it ran correctly
    return 1;
  }                             #sub showAnnotation

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Script

top.cgi

=item Author

Rachel Heck

=item Description

This script gets the data from an annotation file and displays it.

=back

=head1 Subroutines

=over 4

=item showAnnotation

This subroutine simply displays an annotation on a web page.

=back

=head1 History

=over 4

=item [9 July 1999]

The script and the subroutine showAnnotation work correctly and are fully 
commented.

=item [12 July 1999]

The annotation now displays the protection setting.  The program also gets 
and  passes the groups that the user is a member of.

=item [13 July 1999]

The protection line has been made easier to understand.

=item [14 July 1999]

The delete button will only show up on the annotation if the author is the 
one viewing it.

=item [16 July 1999]

Pop-up annotation windows will not use plug-ins in the Ravel server 
anymore.  The package containing readFile is now called AFile instead of 
Annotation.

=item [23 July 1999]

Added Javascript code to the header so that links in annotations will load 
pages in the main browser window.

=item [26 July 1999]

Added a space after the word and in the protection line.  This was an 
error.

=item [27 July 1999]

Changed one more small English problem.  If there are only two groups, the 
comma is taken out of the protection line.  I am now passing around the 
last time that the user viewed the main web page.

=back

=cut

#return the true value to show that the package loaded correctly
return 1;

