#!/usr/local/bin/perl
#############################################################################
# WWWV3 based on WWWBoard2.0 by Matt Wright
# Portions of code different from Matt's original WWWBoard script
# are Copyrighted by David S. Choi, 1997 and hyperlink KOREA Design
#
# This Code is being redistributed with written permission from Matt M. Wright.
#
# COPY RIGHT NOTICE -- applies to the portions of code written by David S. Choi
# Copyright 1997 David S. Choi All Rights Reserved.
#
# WWWV3 may be used and modified free of charge by anyone so long as      
# this copyright notice and the comments above remain intact.  By using this 
# code you agree to indemnify Matthew M. Wright, David S. Choi, and hyperlink
# KOREA Design from any liability that might arise from it's use.                                                   
#                                                                            
# Selling the code for this program without prior written consent is         
# a violation of Matt's and David's copyrights. Obtain permission from David and
# Matt before redistributing this software over the Internet or 
# in any other medium.  In all cases copyright and header must remain intact.
# 
# CGI Library script cgi-lib.pl was written by Selena Sol
#
# MODIFICATIONS BY DAVID S. CHOI
#
# 1) Replaced <li> tags with folder grahpics
# 2) Removed reply threads from main board
# 3) Instead of linked replies, actual reply messages are embedded in the
#    original message file
#
##############################################################################
# WWWBoard                      Version 2.0 ALPHA 2                          #
# Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
# Created 10/21/95              Last Modified 11/25/95                       #
# Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
#                                                                            #
# WWWBoard may be used and modified free of charge by anyone so long as      #
# this copyright notice and the comments above remain intact.  By using this #
# code you agree to indemnify Matthew M. Wright from any liability that      #  
# might arise from it's use.                                                 #  
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  In other words, please ask first before you try and  #
# make money off of my program.                                              #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium.  In all cases copyright and header must remain intact.#
##############################################################################
# Define Variables
#
#--------------------------CUT AND PASTE THIS SECTION

$basedir = "/raid/web/AllenWu/cs3423_01/wwwboard";
$baseurl = "http://nthucad.cs.nthu.edu.tw/AllenWu/cs3423_01/wwwboard";
$cgi_url = "/cgi-bin/cgiwrap/webmaster/cs342301_wwwboard.pl";

$mailprog = '/usr/lib/sendmail';
$khsue_email = 'khsue@nthucad.cs.nthu.edu.tw';
$alix_email = 'alix@nthucad.cs.nthu.edu.tw';

$my_name = '蘇慶峰';

$mesgdir = "messages";
$datafile = "data.txt";
$mesgfile = "wwwboard.htm";
$faqfile = "faq.html";

$ext = "htm";

$title = "301討論區";

#-------------------------------------------------------------
# Done
###########################################################################

###########################################################################
# Configure Options

$show_faq = 1;		# 1 - YES; 0 = NO
$allow_html = 1;	# 1 = YES; 0 = NO
$quote_text = 0;	# 1 = YES; 0 = NO
$subject_line = 0;	# 0 = Quote Subject Editable; 1 = Quote Subject 
			#   UnEditable; 2 = Don't Quote Subject, Editable.
$use_time = 1;		# 1 = YES; 0 = NO
$mailMe = 1;
# Done
###########################################################################

# Get data number is moved...check further down

# Get Form Information
&parse_form;

# Put items into nice variables
&get_variables;

# Open the new file and write information to it.
#---DSC---open new file if and only if it is not a reply
#---DSC---get number, but only increment if it is new.

&get_number;

if ($followup == 0){
   &new_file;
}


# Open the Main WWWBoard File to add link
&main_page;

# Now Add Thread to Individual Pages if
# the input message is a reply

if ($num_followups >= 1) {
   &thread_pages;
#   print "Location: $baseurl/$mesgdir/$mes_num\.$ext\n\n";  
   print "Location: $ENV{HTTP_REFERER}\n\n";
}else{
# Return the user HTML
#&return_html;
   $home = $baseurl."/wwwboard.htm";
   print "Location: $home\n\n";
}
# Increment Number-->write to data.txt
&increment_num;

if ($mailMe) {&mailMe;}

############################
# Get Data Number Subroutine
# If followup == 0, don't increment the number

sub get_number {
   open(NUMBER,"$basedir/$datafile");
   $num = <NUMBER>;
   close(NUMBER);
   if ($num == 99999)  {
      $num = "1";
   }
   else {
      if ($followup == 0) {
         $num++;
      }
   }
}


#######################
# Parse Form Subroutine

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;

      if ($allow_html != 1) {
         $value =~ s/<([^>]|\n)*>//g;
      }
      else {
         unless ($name eq 'body') {
	    $value =~ s/<([^>]|\n)*>//g;
         }
      }

      $FORM{$name} = $value;
   }

}

###############
# Get Variables

sub get_variables {

   if ($FORM{'followup'}) {
      $followup = "1";
      @followup_num = split(/,/,$FORM{'followup'});
      $num_followups = @followups = @followup_num;
      $last_message = pop(@followups);
      $origdate = "$FORM{'origdate'}";
      $origname = "$FORM{'origname'}";
      $origsubject = "$FORM{'origsubject'}";
   }
   else {
      $followup = "0";
   }

   if ($FORM{'name'}) {
      $name = "$FORM{'name'}";
      $name =~ s/"//g;
      $name =~ s/<//g;
      $name =~ s/>//g;
      $name =~ s/\&//g;
   }
   else {
      &error(no_name);
   }

   if ($FORM{'email'} =~ /.*\@.*\..*/) {
      $email = "$FORM{'email'}";
   }

   if ($FORM{'subject'}) {
      $subject = "$FORM{'subject'}";
      $subject =~ s/\&/\&amp\;/g;
      $subject =~ s/"/\&quot\;/g;
   }
   else {
      &error(no_subject);
   }

   if ($FORM{'url'} =~ /.*\:.*\..*/ && $FORM{'url_title'}) {
      $message_url = "$FORM{'url'}";
      $message_url_title = "$FORM{'url_title'}";
   }

   if ($FORM{'img'} =~ /.*tp:\/\/.*\..*/) {
      $message_img = "$FORM{'img'}";
   }

   if ($FORM{'body'}) {
      $body = "$FORM{'body'}";
      $body =~ s/\cM//g;
      $body =~ s/\n\n/<p>/g;
      $body =~ s/\n/<br>/g;

      $body =~ s/&lt;/</g; 
      $body =~ s/&gt;/>/g; 
      $body =~ s/&quot;/"/g;
   }
   else {
      &error(no_body);
   }

   if ($quote_text == 1) {
      $hidden_body = "$body";
      $hidden_body =~ s/</&lt;/g;
      $hidden_body =~ s/>/&gt;/g;
      $hidden_body =~ s/"/&quot;/g;
   }

   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   $year+=1900;    
   if ($sec < 10) {
      $sec = "0$sec";
   }
   if ($min < 10) {
      $min = "0$min";
   }
   if ($hour < 10) {
      $hour = "0$hour";
   }
   if ($mon < 10) {
      $mon = "0$mon";
   }
   if ($mday < 10) {
      $mday = "0$mday";
   }

   $month = ($mon + 1);

   @months = ("January","February","March","April","May","June","July","August","September","October","November","December");

   if ($use_time == 1) {
      $date = "$hour\:$min\:$sec $month/$mday/$year";
   }
   else {
      $date = "$month/$mday/$year";
   }
   chop($date) if ($date =~ /\n$/);

   $long_date = "$months[$mon] $mday, $year at $hour\:$min\:$sec";
}      



#####################
# New File Subroutine
#

sub new_file {

   open(NEWFILE,">$basedir/$mesgdir/$num\.$ext") || die $!;

   print NEWFILE "<html>\n";

   print NEWFILE "  <head>\n";
   print NEWFILE "      <title>網路討論區主題為 $subject</title>\n";
   print NEWFILE "  </head>\n";

   print NEWFILE "  <body bgcolor=#FFFFFF>\n";

# Banner and Icons 

   print NEWFILE "  <center>\n";
#   print NEWFILE "<img src=\"$baseurl/forum.jpg\" alt=\"網路討論區標題\" border=0 width=400 height=100>\n";
   print NEWFILE "<table border=0 cellpadding=2 cellspacing=0>\n";
   print NEWFILE "<tr>\n";
   print NEWFILE "<td><a href=\"#postfp\"><img src=\"$baseurl/reply.gif\" alt=\"回應訊息圖像\" border=0 width=64 height=32></td>\n";
   print NEWFILE "<td><a href=\"$baseurl/reboard.html\"><img src=\"$baseurl/home.gif\" alt=\"返回主頁圖像\" border=0 width=64 height=32></td>\n";

   if ($show_faq == 1) {
      print NEWFILE "<td><a href=\"$baseurl/faq.html\"><img src=\"$baseurl/faq.gif\" alt=\"FAQ 圖像\" border=0 width=64 height=32></a></td>\n";
   }

   print NEWFILE "</tr>\n";
   print NEWFILE "</table>\n";
   print NEWFILE "</center>\n";

   print NEWFILE "<HR>\n";

# Subject and the rest

   print NEWFILE "<center><h2><font color=#0080C0>主題: $subject</font></h2></center><p>\n";

   print NEWFILE "<center><table border=0 cellpadding=2 cellspacing=0 width=90%>\n";
   print NEWFILE "<tr><td valign=top>\n";
   print NEWFILE "<center><font color=#0080C0 size=3><B>原始訊息</B></font></center>\n";
   print NEWFILE "</td></tr>\n";

   print NEWFILE "<tr><td bgcolor=#EEB4B4 valign=top>\n";

   if ($email) {
       print NEWFILE "<font size=3><B>名稱: </B><a href=\"mailto:$email\">$name</a></font><BR>\n";
   }
   else {
       print NEWFILE "<font size=3><B>名稱: </B>$name</font><BR>\n";
   }

   print NEWFILE "<font size=3><B>日期: </B>$long_date</font><BR>\n";

   if ($message_url) {
        print NEWFILE "<font size=><B>網頁: </B><a href=\"$message_url\">$message_url_title</a><BR>\n";
   }

   print NEWFILE "<font size=3><B>主題: </B>$subject</font><BR>\n";
   print NEWFILE "</td></tr>\n";
   print NEWFILE "<tr><td bgcolor=#FFFFCC valign=top>\n";
   print NEWFILE "<font size=3><b>意見內容: </B></font><BLOCKQUOTE>$body</BLOCKQUOTE>\n";
   print NEWFILE "</td></tr></table></center><BR>\n";

# DSC: Attach image URL

   if ($message_img) {
       print NEWFILE "<center><img src=\"$message_img\"></center><BR>\n";
   }

   print NEWFILE "<ul><!--insert: 0-->\n";
   print NEWFILE "</ul><!--end: 0-->\n";

   print NEWFILE "<p><p><p><p>\n";


   print NEWFILE "    <center>\n";
   print NEWFILE "    <font color=#0080C0 size=3><strong>\n";

   print NEWFILE "    <a name=\"postfp\">回應現在的訊息:</a> \n";

   print NEWFILE "    </strong></font>\n";
   print NEWFILE "    </center>\n";
   print NEWFILE "<p>\n";

   print NEWFILE "<form method=POST action=\"$cgi_url\">\n";
   print NEWFILE "<input type=hidden name=\"followup\" value=\"";

   if ($followup == 1) {
      foreach $followup_num (@followup_num) {
         print NEWFILE "$followup_num,";
      }
   }
   print NEWFILE "$num\">\n";
   print NEWFILE "<input type=hidden name=\"origname\" value=\"$name\">\n";
   if ($email) {
      print NEWFILE "<input type=hidden name=\"origemail\" value=\"$email\">\n";
   }
   print NEWFILE "<input type=hidden name=\"origsubject\" value=\"$subject\">\n";
   print NEWFILE "<input type=hidden name=\"origdate\" value=\"$long_date\">\n";
   print NEWFILE "<pre>   名稱: <input type=text name=\"name\" size=50>\n";
   print NEWFILE " E-Mail: <input type=text name=\"email\" size=50>\n";
   if ($subject_line == 1) {
      if ($subject_line =~ /^/) {
         print NEWFILE "<input type=hidden name=\"subject\" value=\"$subject\">\n";
         print NEWFILE "   主題: <b>$subject</b><p>\n";
      }
      else {
         print NEWFILE "<input type=hidden name=\"subject\" value=\" $subject\">\n";
         print NEWFILE "   主題: <b> $subject</b><p>\n";
      }
   } 
   elsif ($subject_line == 2) {
      print NEWFILE "   主題: <input type=text name=\"subject\" size=50></pre>\n";
   }
   else {
      if ($subject =~ /^/) {
         print NEWFILE "   主題: <input type=text name=\"subject\"value=\"$subject\" size=50></pre>\n";
      }
      else {
         print NEWFILE "   主題: <input type=text name=\"subject\" value=\" $subject\" size=50></pre>\n";
      }
   }
   print NEWFILE "<pre>意見內容:\n";
   print NEWFILE "         <textarea name=\"body\" COLS=48 ROWS=6 wrap=physical>\n";
   if ($quote_text == 1) {
      @chunks_of_body = split(/\&lt\;p\&gt\;/,$hidden_body);
      foreach $chunk_of_body (@chunks_of_body) {
         @lines_of_body = split(/\&lt\;br\&gt\;/,$chunk_of_body);
         foreach $line_of_body (@lines_of_body) {
            print NEWFILE ": $line_of_body\n";
         }
         print NEWFILE "\n";
      }
   }
   print NEWFILE "</textarea></pre>\n";
   print NEWFILE "<p>\n";
   print NEWFILE "<div align=center><center><pre><input type=submit value=\"確定動作\"> <input type=reset></pre></center></div>\n";
   print NEWFILE "<pre>  Homepage URL (*可不寫): <input type=text name=\"url\" size=40>\n";
   print NEWFILE "Homepage Title (*可不寫): <input type=text name=\"url_title\" size=40>\n";
   print NEWFILE "     Image URL (*可不寫): <input type=text name=\"img\" size=40></pre>\n";

   print NEWFILE "</body></html>\n";
   close(NEWFILE);
}


###############################
# Main WWWBoard Page Subroutine

sub main_page {

# DSC: Read in Main Document

   open(MAIN,"$basedir/$mesgfile") || die $!;
   @main = <MAIN>;
   close(MAIN);

# DSC: Open Main Document and reconstruct
# DSC: Look for <!--begin--> tag and place new message here


   open(MAIN,">$basedir/$mesgfile") || die $!;

# DSC: Note major editing here to put stuff in table

# DSC if it's a new message...then add to the main page

   if ($followup == 0) {

      foreach $main_line (@main) {

# DSC: If $main_line finds begin marker, insert new message here

         if ($main_line =~ /<!--begin-->/) {
            print MAIN "<!--begin-->\n";
            print MAIN "<!--top: $num--><tr valign=top><td><img src=\"folder.gif\"> <font
size=3><a href=\"$mesgdir/$num\.$ext\"><b>$subject</b></a></font></td><td align=center><font
size=3>$name</font></td><td align=center><font size=3><i>$date</i></font></td>\n";
            print MAIN "<td align=center>\n";
            print MAIN "(<!--responses: $num-->0)</td></tr>\n";
         }

# DSC: If not then just write back the code

         else {
            print MAIN "$main_line";
         }

      }
   }

# DSC: If the new message is a Reply, then update the number of responses
# DSC: Note that we are not interested in <!--begin--> tag.

   else {
      foreach $main_line (@main) {
	 $work = 0;

      if ($main_line =~ /<!--top: (.*)--><tr valign=top><td>(.*)<font size=3><a href="$mesgdir\/\1\.$ext">(.*)<\/a><\/font><\/td><td align=center><font
size=3>(.*)<\/font><\/td><td align=center><font size=3><i>(.*)<\/i><\/font><\/td>/) {

            $mes_num = $1;
            $orig_subject = $3;
            $orig_author = $4;
            $orig_date = $5;

            foreach $followup_num (@followup_num) {
               if ($followup_num == $mes_num) {
                  print MAIN "<!--top: $mes_num--><tr valign=top><td><img src=\"openfd.gif\"> <font
size=3><a href=\"$mesgdir/$mes_num\.$ext\">$orig_subject</a></font></td><td align=center><font
size=3>$orig_author</font></td><td align=center><font size=3><i>$orig_date</i></font></td>\n";
               }
               else {
                  print MAIN "$main_line";
               }
            }
           
        }
  
       elsif ($main_line =~ /\(<!--responses: (.*)-->(.*)\)/) {

            $response_num = $1;
            $num_responses = $2;
            $num_responses++;
            foreach $followup_num (@followup_num) {
               if ($followup_num == $response_num) {
                  print MAIN "(<!--responses: $followup_num-->$num_responses)</td></tr>\n";
		      
		  $work = 1;
               }
            }
            if ($work != 1) {
               print MAIN "$main_line";
            }
        }
        else {
            print MAIN "$main_line";
        }
      }
   }

   close(MAIN);
}


############################################
# Add Followup Threading to Individual Pages
sub thread_pages {

   foreach $followup_num (@followup_num) {
      open(FOLLOWUP,"$basedir/$mesgdir/$followup_num\.$ext");
      @followup_lines = <FOLLOWUP>;
      close(FOLLOWUP);

      open(FOLLOWUP,">$basedir/$mesgdir/$followup_num\.$ext");
      foreach $followup_line (@followup_lines) {
         $work = 0;
         if ($followup_line =~ /<ul><!--insert: (.*)-->/) {
            $num_responses = $1;
            $num_responses++;

            print FOLLOWUP "<center><table border=0 cellpadding=2 cellspacing=0 width=90%>\n";
            print FOLLOWUP "<tr><td valign=top>\n";
            print FOLLOWUP "<center><font color=#0080C0 size=3><B>回應者 $num_responses</B></font></center>\n";
            print FOLLOWUP "</td></tr>\n";
            print FOLLOWUP "<tr><td bgcolor=#EEB4B4 valign=top>\n";

             if ($email) {
                print FOLLOWUP "<font size=3><B>名稱: </B><a href=\"mailto:$email\">$name</a></font><BR>\n";             }
             else {
                print FOLLOWUP "<font size=3><B>名稱: </B>$name</font><BR>\n";
             }

            print FOLLOWUP "<font size=3><B>日期: </B>$long_date</font><BR>\n";

            if ($message_url) {
               print FOLLOWUP "<font size=3><B>網頁: </B><a href=\"$message_url\">$message_url_title</a><BR>\n";
            }


            print FOLLOWUP "<font size=3><B>主題: </B>$subject</font><BR>\n";

            print FOLLOWUP "</td></tr>\n";
            print FOLLOWUP "<tr><td bgcolor=#FFFFCC valign=top>\n";
            print FOLLOWUP "<font size=3><b>Reply: </B></font><BLOCKQUOTE>$body</BLOCKQUOTE>\n";

            if ($message_img) {
                print FOLLOWUP "<center><img src=\"$message_img\"></center><BR>\n";
            }

            print FOLLOWUP "</td></tr></table></center><BR>\n";
            print FOLLOWUP "<ul><!--insert: $num_responses-->\n";
            print FOLLOWUP "</ul><!--end: $num_responses-->\n";

         }

         elsif ($followup_line =~ /<\/ul><!--end: (.*)-->/) {
	   
         }

         else {
            print FOLLOWUP "$followup_line";
         }
      }
      close(FOLLOWUP);
   }
}

sub return_html {
   print "Content-type: text/html\n\n";
   print "<html><head><title>訊息增加: $subject</title></head>\n";
   print  "  <body bgcolor=#FFFFFF>\n";

   print "    <center>\n";

#   print  "<img src=\"$baseurl/forum.jpg\" alt=\"網路討論區標題\" border=0 width=400 height=100>\n";
   print  "<p><font size=5 color=red>若回應文章後進入討論主題頁,
要按下Reload才能看到剛剛Reply的文章!!!</font></p>\n";
   print  "<table border=0 cellpadding=2 cellspacing=0>\n";
   print  "<tr>\n";
   print  "<td><a href=\"$baseurl/reboard.html\"><img src=\"$baseurl/home.gif\" alt=\"返回主頁圖像\" border=0 width=64 height=32>返回主頁</td>\n";
   print  "</tr>\n";
   print  "</table>\n";

   print  "<H2><font color=#0080C0>訊息增加: $subject</font></H2>\n";

   print  "</center>\n";
   print "<b>回應資料已經被加入訊息版面中:</b><p>\n";
   print "<b>名稱:</b> $name<br>\n";
   print "<b>E-Mail:</b> $email<br>\n";
   print "<b>主題:</b> $subject<br>\n";
   print "<b>意見內容:</b><p>\n";
   print "$body<p>\n";
   if ($message_url) {
      print "<b>連結:</b> <a href=\"$message_url\">$message_url_title</a><br>\n";
   }
   if ($message_img) {
      print "<b>圖像:</b> <img src=\"$message_img\"><br>\n";
   }
   print "<b>增加日期:</b> $date<p>\n";

   print "</body></html>\n";
}

sub increment_num {
   open(NUM,">$basedir/$datafile") || die $!;
   print NUM "$num";
   close(NUM);
}

sub error {
   $error = $_[0];

   print "Content-type: text/html\n\n";

   if ($error eq 'no_name') {
      print "<html><head><title>$title 錯誤: 無名稱</title></head>\n";

      print "<body>\n";
      print "<center>\n";
#      print "<img src=\"$baseurl/forum.jpg\" alt=\"Bulletin Board Banner\" border=0 width=400 height=100><P>\n";

      print "<h1>錯誤: 無名稱</h1></center>\n";
      print "你忘記填入這'名稱'資料欄. 現在你必須重新填寫 .  這必須要填寫的資料欄是：名稱，主題和訊息.<p>\n";
      &rest_of_form;
   }
   elsif ($error eq 'no_subject') {
      print "<html><head><title>$title 錯誤: 無主題</title></head>\n";

      print "<body>\n";
      print "<center>\n";
#      print "<img src=\"$baseurl/forum.jpg\" alt=\"Bulletin Board Banner\" border=0 width=400 height=100><P>\n";

      print "<h1>錯誤: 無主題</h1></center>\n";
      print "你忘記填入這'主題'資料欄. 現在你必須重新填寫 .  這必須要填寫的資料欄是：名稱，主題和訊息.<p>\n";

      &rest_of_form;
   }
   elsif ($error eq 'no_body') {
      print "<html><head><title>$title 錯誤: 無訊息內容</title></head>\n";

      print "<body>\n";
      print "<center>\n";
#      print "<img src=\"$baseurl/forum.jpg\" alt=\"Bulletin Board Banner\" border=0 width=400 height=100><P>\n";

      print "<h1>錯誤: 無訊息內容</h1></center>\n";
      print "你忘記填入這'訊息'資料欄. 現在你必須重新填寫 .  這必須要填寫的資料欄是：名稱，主題和訊息.<p>\n";
      &rest_of_form;
   }

   else {
      print "錯誤!  無法定義.\n";
   }
   exit;
}

sub rest_of_form {

   print "<form method=POST action=\"$cgi_url\">\n";

   if ($followup == 1) {
      print "<input type=hidden name=\"origsubject\" value=\"$FORM{'origsubject'}\">\n";
      print "<input type=hidden name=\"origname\" value=\"$FORM{'origname'}\">\n";
      print "<input type=hidden name=\"origemail\" value=\"$FORM{'origemail'}\">\n";
      print "<input type=hidden name=\"origdate\" value=\"$FORM{'origdate'}\">\n";
      print "<input type=hidden name=\"followup\" value=\"$FORM{'followup'}\">\n";
   }
   print "名稱: <input type=text name=\"name\" value=\"$FORM{'name'}\" size=50><br>\n";
   print "E-Mail: <input type=text name=\"email\" value=\"$FORM{'email'}\" size=50><p>\n";
   if ($subject_line == 1) {
      print "<input type=hidden name=\"subject\" value=\"$FORM{'subject'}\">\n";
      print "主題: <b>$FORM{'subject'}</b><p>\n";
   } 
   else {
      print "主題: <input type=text name=\"subject\" value=\"$FORM{'subject'}\" size=50><p>\n";
   }
   print "意見內容:<br>\n";
   print "<textarea COLS=50 ROWS=10 name=\"body\">\n";
   $FORM{'body'} =~ s/</&lt;/g;
   $FORM{'body'} =~ s/>/&gt;/g;
   $FORM{'body'} =~ s/"/&quot;/g;
   print "$FORM{'body'}\n";
   print "</textarea><p>\n";
   print "<input type=submit value=\"傳送訊息\"> <input type=reset>\n";
   print "Optional Link URL: <input type=text name=\"url\" value=\"$FORM{'url'}\" size=45><br>\n";
   print "Link Title: <input type=text name=\"url_title\" value=\"$FORM{'url_title'}\" size=50><br>\n";
   print "Optional Image URL: <input type=text name=\"img\" value=\"$FORM{'img'}\" size=45><p>\n";
   print "</form>\n";
   print "<br><hr size=7 width=75%>\n";
   if ($show_faq == 1) {
      print "<center>[ <a href=\"#followups\">回應</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ] [ <a href=\"$baseurl/$faqfile\">FAQ</a> ]</center>\n";
   }
   else {
      print "<center>[ <a href=\"#followups\">回應</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ]</center>\n";
   }
   print "</body></html>\n";
}
sub mailMe {
      open (MAIL, "|$mailprog -t");

      print MAIL "To: $khsue_email\n";
      print MAIL "From: cs342301作業系統討論區\n";
      print MAIL "Subject: OS討論區所問問題\n\n"; 
      print MAIL "發問者:$name\n";
      print MAIL "內容:$body";
      close (MAIL);

      open (MAIL, "|$mailprog -t");

      print MAIL "To: $alix_email\n";
      print MAIL "From: cs342301作業系統討論區\n";
      print MAIL "Subject: OS討論區所問問題\n\n"; 
      print MAIL "發問者:$name\n";
      print MAIL "內容:$body";
      close (MAIL);

}