#!/usr/bin/perl

#
# Interactive Story program written by Valerie Mates, valerie@cyberspace.org
# 1/15/98.
#
# This program is freeware, but, if you use it, you are required 
# to leave my URL in the page footer and send me an email message 
# (valerie@cyberspace.org) to let me know that you are using it.
#
# If you translate the program to another language, I would appreciate a
# copy to add to the collection of translated versions.  This is not
# required, but it is appreciated.
#
# The program is not permitted for use on pornographic sites.
#
# This program uses the ReadParse routine from Steve Brenner's cgi-lib program.
#

# Read our configuration.  Any customization for a particular site is
# stored in the config file.  No changes to this file (story.pl) should
# be required.
require("/srv/lojban/story/storyconfig");
use File::Temp qw/ tempfile tempdir /;
use Data::Dumper;



# Set our version number:
$version = "1.4";
$version_date = "July 9, 2001";

# Read any passed-in input, and put it into a hash named "in":
&ReadParse;

# Remove any illegal characters from the variable "next." Otherwise, people
# would be able to read any publicly readable file on the system.
$in{'next'} =~ s/[^0-9a-z]//g;

# Set a variable with the name of the next file to display:
$nextfile = "$story_dir/$in{'next'}.txt";

# Create a big messy number that we can occasionally stick into the URL.
# This is useful because it can be used to trigger a reload of a page with a 
# newly added chapter even without making the user press the reload button.
# That's especially helpful on framed websites, where many people won't
# know how to reload the story frame.  It's useful on other sites too.
$bignum = "$$" . time;

# Display headers and initial HTML:
print <<EOF;
Content-type: text/html




<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "DTD/xhtml1-strict.dtd" >
<html	xmlns	= "http://www.w3.org/1999/xhtml" lang="en">
<head>
    <title>lo lojbo kamjikca lisri</title>
EOF

open( HEADER, "</home/lojban-web/lojban-web/baseline/htdocs/header.affix.html");
while( <HEADER> )
{
    s/<!-- .* -->//;
    s/<.?@>//g;
    print $_;
};

if ($in{'next'} eq "") {

   print <<EOF;
<h1>lo lojbo kamjikca lisri</h1>

<p>
<strong>
    This page is an interactive story, written by its readers.
</strong>
</p>

<p>
As you read the story, each page tells part of the story.  Each page ends
with two choices for what to do next.  Click on one of the two links to read 
what happens next in the story.
</p>

<p>
When you reach the end of the part that the readers before you have 
written, you can enter the next page of the story.
</p>

<p>
<strong>
This story is in lojban.
</strong>
</p>

<p>
<strong>
lo vi lisri cu jai se bangu la lojban.
</strong>
</p>

<div class="centered">
<a href="story.pl?next=1&amp;chapter=0">Start reading</a>
</div>

EOF

} elsif ($in{'next'} eq "outline") {

   $chapter_count = 0;
   print "<h1>Story Outline:</h1>\n";
   print "<pre>\n";    # If you customize, I recommend leaving this tag in!
   outline_tree(1, 1);
   print "</pre>\n";
   print "<p><strong>$chapter_count chapters</strong></p>\n";

} elsif ($in{'next'} eq "about") {

   print <<EOF;

    <div class="centered">
    <cite>Interactive Story</cite> by 
    <a href="http://www.valeriemates.com/">Valerie Mates</a></cite>
    <br />
   This is version $version of the story program <br />
      ($version_date).<br /><br />
   This program is public domain (freeware).<br />
   View <a href="http://www.valeriemates.com/storysites.html">other stories</a>
   running this program.<br />
   <a href="http://www.valeriemates.com/story_download.html">Download</a> the 
   program (for Unix or Windows NT servers, 10K).<br />
   <br />
   About the <a href="$host_url">host</a> of this story.<br />
   </div>
EOF

} elsif ((-e $nextfile)  && ($in{'submit'} eq "")) {

   # The next page of the story exists (and hasn't snuck in
   # while we were writing a different new page).
   # Read it in and display it.

   if (!(open(STORY, "<$nextfile"))) {
      print "Could not open file $nextfile to read it!<br />\n";
   } else {
      # Read data from story file:
      $author_name = <STORY>;
      $author_email = <STORY>;
      $chapter_title = <STORY>;
      $next_text_1 = <STORY>;
      $next_text_2 = <STORY>;

      chomp($author_name);
      chomp($author_email);
      chomp($chapter_title);
      chomp($next_text_1);
      chomp($next_text_2);

      $chapter = 1 + $in{'chapter'};

      print "<h1>Chapter $chapter: $chapter_title</h1>\n";

      if (($author_name ne "") || ($author_email ne "")) {
         $author = "$author_name 
                    <a href=\"mailto:$author_email\">$author_email</a>";
      } else {
         $author= "an anonymous author";
      }
      print "<p><em>This page added by $author</em>.</p>\n";

      while (<STORY>) {
         print $_;
      }

      $next_num_1 = 2 * $in{'next'};
      $next_num_2 = $next_num_1 + 1;

      # Do some fancy formatting on the chapter numbers, so that they
      # don't print out in scientific notation.  This only becomes 
      # important when users enter chapters more than 50 levels deep. !!!
      $next_num_1 = format_chapter_number($next_num_1);
      $next_num_2 = format_chapter_number($next_num_2);

      print <<EOF;

<hr />

<p>
What happens next?
</p>

<p>
<a href="story.pl?next=$next_num_1&amp;chapter=$chapter"><img 
src="$image_url/pink_story_arrow.gif" width="15" height="16" alt=""
/></a>
<a href="story.pl?next=$next_num_1&amp;chapter=$chapter">$next_text_1</a>

<br />

<a href="story.pl?next=$next_num_2&amp;chapter=$chapter"><img 
src="$image_url/green_story_arrow.gif" width="15" height="16" alt=""
/></a>
<a href="story.pl?next=$next_num_2&amp;chapter=$chapter">$next_text_2</a>

</p>

EOF
   }


} elsif ($in{'submit'} eq "") {
   # The next page of the story doesn't exist.
   # Ask the user to enter it.

print <<EOF;
<h1>Your Turn</h1>

<p>
You have reached the end of the story action.
</p>

<p>
Now it's your turn to enter what happens next in the story.
</p>

<p>
<strong>
Please write your section in lojban only!  Thank you!
</strong>
</p>

<p>
<strong>
pe'usai .ei ko ciska le do lisri pagbu bau la lojban. po'o
</strong>
</p>

<blockquote>

<form action="story.pl" method="post">

<p>
<strong>Your name: </strong> (optional) <br />
<input type="text" name="author_name" size="50" maxlength="50" /> <br />

<strong>Your email address: </strong> (optional) <br />
<input type="text" name="author_email" size="50" maxlength="50" /> <br />

<br />

<strong>The next part of the story:</strong>
<br />
<textarea rows="30" cols="80" name="story_text" ></textarea>
<br />

<br />

<strong>The two things the reader can do next: </strong><br />
Option One: <input type="text" name="next_text_1" size="80"
maxlength="256" />
<br />
Option Two: <input type="text" name="next_text_2" size="80"
maxlength="256" />
<br />

<br />

<strong>A title for your chapter:</strong> <br />
<input type="text" name="chapter_title" size="80" maxlength="256" /> <br />

<input type="hidden" name="next" value="$in{'next'}">
<input type="hidden" name="chapter" value="$in{'chapter'}">

<br>
<input type="submit" name="submit" value="Enter it!">
</input>

</p>

</form>

</blockquote>

EOF

} else {
   # The user has entered a next page.
   # See if it's okay.  If it is, save it.

   # Get rid of freaking ^Ms.
   $in{story_text} =~ tr/\015//d;

   # Convert blank lines to </p><p> html in the story:
   $in{story_text} =~ s/^$/<\/p><p>/mg;

   # Add start and end tags.
   $in{story_text} = "<p>\n$in{story_text}\n</p>\n";

   # Don't let people create any old file:
   $prev_page_num = format_chapter_number(int($in{'next'} / 2));
   if (($in{'next'} ne "1") && (! -e "$story_dir/$prev_page_num.txt")) {
      print "<strong>Invalid chapter name!</strong><br />";

   # Check for any missing text:
   } elsif ($in{story_text} eq "") {
      print "<b>Please write the description of what happens now.</b><br />";
      print "Press your browser's 'Back' button to add it.";
   } elsif (($in{next_text_1} eq "") || ($in{next_text_2} eq "")) {
      print "<b>Please enter two options for what the user can do next.</b><br />";
      print "Press your browser's 'Back' button to add this.";
   } elsif ($in{chapter_title} eq "") {
      print "<b>Please enter a title for your chapter.</b><br />";
      print "Press your browser's 'Back' button to add it.";
   } else {

      # Make sure file is still available.
      if (-e $nextfile) {
         print <<EOF;

<p><strong>Ooops!  Someone else entered this section of the story 
while you were typing.  I can't enter your section</strong>.
</p>

<div class="centered">
<a href="story.pl?next=$in{'next'}&amp;chapter=$in{'chapter'}&amp;reload=$bignum">See it</a>
</div>


<p>After you select the
"See it" link, you may need to press the 'Reload' button on your browser 
to see what the other person 
entered.
<br />
</p>

EOF
      } else {

         # Save it!
         if (!open(STORY, ">$nextfile")) {
            print "Oh oh.  I couldn't open the file '$nextfile' to save your
                   section.  (Error: $!)  So I didn't save it.\n";
         } else {
            print STORY "$in{author_name}\n";
            print STORY "$in{author_email}\n";
            print STORY "$in{chapter_title}\n";
            print STORY "$in{next_text_1}\n";
            print STORY "$in{next_text_2}\n";
            print STORY "$in{story_text}\n";
            close(STORY);

            print <<EOF;
<p>
Your addition to the story has been saved.
</p>

<div class="centered">
<a href="story.pl?next=$in{'next'}&amp;chapter=$in{'chapter'}&amp;reload=$bignum">See it</a>
</div>

<p>
After you select the "See it" link, you may need to press the 'Reload'
button on your browser to see the chapter you wrote.
<br />
</p>

EOF

            # Send me an email message with the new section:
            if (($announce_to ne "") && ($sendmail ne "")) 
	    {
               if (open(MAIL, "| $sendmail -t")) 
	       {
		    $dir = tempdir( CLEANUP => 1 );
		    ($fh, $filename) = tempfile( DIR => $dir );
		    local $/=undef;

		    print $fh $in{story_text};

		    if( open(LYNX, "/usr/bin/lynx --force-html --dump $filename |") )
		    {
			local $/=undef;
			$formatted_text=<LYNX>;
			print MAIL <<EOF;
To: $announce_to
From: $announce_to
Subject: New Posting in The Lojbanic Interactive Story

See it at:

http://www.lojban.org/texts/original/lojban_story/story/story.pl?next=$in{'next'}&chapter=$in{'chapter'}

Author:   $in{author_name} ($in{author_email})
Chapter $in{chapter}:  $in{chapter_title}

Text:
$formatted_text

Options:
   $in{next_text_1}
   $in{next_text_2}
.

EOF
			close(MAIL);
		  }
               }
            }
         }
      }
   }
}

#print <<EOF;
#
#</td>
#</tr>
#<tr>
#<td valign=bottom>
#EOF

# Display the links at the bottom of the page to see the previous
# chapter and the outline.  I didn't include these links on the 
# first page because I want the reader to get at least a little way
# into the story in the normal way before being able to look at the
# outline.

if ($in{'next'} > 1) {
   $prev_page_num = format_chapter_number(int($in{'next'} / 2));
   $prev_chapter = $in{'chapter'} - 1;

   print <<EOF;
<p>
<br />
</p>

<hr />

<p>
<a href="story.pl?next=$prev_page_num&amp;chapter=$prev_chapter">
<img src="$image_url/story_back_arrow.gif" width="15" height="15" alt=""
/> </a>

Back to the 

<a href="story.pl?next=$prev_page_num&amp;chapter=$prev_chapter">previous
chapter</a>

</p>


<p>
<a href="story.pl?next=1&amp;chapter=0">
<img src="$image_url/story_restart_arrow.gif" width="31" height="16"
alt="" />
</a>

Back to 
<a href="story.pl?next=1&amp;chapter=0">the beginning</a> of the story.
</p>

<p>
View 
<a href="story.pl?next=outline">outline</a> of the chapters.
</p>

EOF

}

# Reminder: This program is free as long as you do not remove this
# link back to valeriemates.com:

print <<EOF;

<p>
<br />
</p>

<hr />

<p>
View info <a href="story.pl?next=about">about</a> this story
</p>

<p>
Story program written by <a href="http://www.valeriemates.com/">Valerie
Mates</a>
</p>

<p>
Please <a href="mailto:webmaster\@lojban.org">e-mail webmaster\@lojban.org</a>
with any comments or concerns.
</p>

</body>
</html>
EOF



# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# Returns TRUE if there was input, FALSE if there was no input 
# UNDEF may be used in the future to indicate some failure.

# Now that cgi scripts can be put in the normal file space, it is useful
# to combine both the form and the script in one place.  If no parameters
# are given (i.e., ReadParse returns FALSE), then a form could be output.

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.

sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);

  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }

  @in = split(/[&;]/,$in); 

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;

  }

  return scalar(@in); 
}


#
# The "outline" option recursively calls this function to print a chapter
# and see if it has any subchapters.
#
sub outline_tree {
   if (-e "$story_dir/$_[0].txt") {

      $auth = "";
      $title = "";

      if (open(CHAPTER, "$story_dir/$_[0].txt")) {
         $auth = <CHAPTER>;
         $junk = <CHAPTER>;
         $title = <CHAPTER>;
         close(CHAPTER);

         chomp($auth);
         chomp($title);

         if ($auth ne "") {
            $auth = "by $auth";
         }
      }

      #print "&nbsp;&nbsp;&nbsp;";
      print "&nbsp;|&nbsp;" x ($_[1] - 1);
      print "&nbsp;+-";
      $x = format_chapter_number($_[1] - 1);
      print "Chapter $_[1]: <a href=\"story.pl?next=$_[0]&amp;chapter=$x\">$title</a> $auth\n";

      outline_tree(2 * $_[0], $_[1] + 1);
      outline_tree((2 * $_[0]) + 1, $_[1] + 1);

      $chapter_count++
   }
}


#
# Do some fancy formatting on the chapter numbers, so that they
# don't print out in scientific notation.  This only becomes 
# necessary when users enter chapters more than 50 levels deep!!!
#
sub format_chapter_number {
   my $my_chapter_number = $_[0];   # Parameter is chapter number.
   return(sprintf("%.0f", $my_chapter_number));
}

