#! /srvtools/http/bin/perl
#############################################################################
#  
#   File:         Conference.pl
#
#   Author:       Jungmin Kim
#
#   Created:      03/30/95
#   Modified: 
#   Version: 
#
#   Description:  Subroutines of Change.pl to modify conference file
#
#############################################################################

require "GetDate.pl";
require "Address.pl";

#############################################################################
#
#  AddConference
# 
#  INPUT:    user information (administrator)
#  OUTPUT:   HTML text
#  FUNCTION: create fill-out forms to get a new user name
#
#############################################################################

sub AddConference{
    local($newconf, $newdir, $newurl, $description, $user, $subdir) = @_;

# FIX Check the password

   if ($subdir) {
      $conftype = "Forum";
   }
   else {
      $conftype = "WebBoard";
   }

###############
    print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Add a New $conftype </TITLE>
</HEAD>
<BODY BGCOLOR="#55CCCC">
<H2> Add a New $conftype </H2>
<FORM METHOD="POST" ACTION="$change/$subdir:saveconf:$user">
<B>Type in a $conftype name to be added: </B><BR>
<INPUT SIZE=60 NAME="newconf" VALUE="$newconf">
<P>
<B>Type in a Unix directory name for the $conftype: </B><BR>
<INPUT SIZE=60 NAME="newdir" VALUE="$newdir">
<P>
<B>Type in a URL relevant to the $conftype name (optional): </B><BR>
<INPUT SIZE=80 NAME="newurl" VALUE="$newurl">
<P>
<B>Type in a short description of the $conftype (optional): </B> <BR>
<TEXTAREA NAME="description" ROWS=5 COLS=80>${description}</TEXTAREA>
<P>
EOF
###############

   if (($subdir) && (! $newconf)) {  # Option to add first message
###############
    print <<EOF;
<HR>
<FONT SIZE="+1"><B>Add the first topic to the Forum (optional)</B></FONT>
<P>
<B>Enter the subject title for the initial topic: </B><BR>
<INPUT SIZE=72 NAME="title" VALUE="$title">
<P>
<B>Enter the message for the initial topic: </B><BR>
<TEXTAREA NAME="message" ROWS=5 COLS=72>${message}</TEXTAREA><P>
<HR>
EOF
###############
   }

###############
    print <<EOF;
<P>
<INPUT TYPE="submit" VALUE="SUBMIT">
<B>Click on this button to submit the new $conftype</B><P>
or <A HREF="$action/$subdir:select:::${user}">
<B>RETURN to the list of ${conftype}s</B>
</A>
</FORM>
EOF
###############

# Print the navigation bar and address
&Address();

}
1;

#############################################################################
#
#  SaveConference
# 
#  INPUT:    conference name, directory path
#  OUTPUT:   HTML text
#  FUNCTION: save the new conference into password file
#
#############################################################################

# FIX Check the password

sub SaveConference{
    local($confname, $dirname, $url, $description, $title, $message, 
          $subdir, $user) = @_;
    local($filename, $date, $conftitle, $subcheckdir, $subcheck, $subconf,
	  $pname, $pemail, $tmpuser, $file, $line);

    # FIX Display an error message
    if ($confname eq "" || $dirname eq "") {return;}

    # Send this to a subroutine
    # FIX - url should not be broken now
    $url =~ s/&gt;/\>/g;
    $url =~ s/&lt;/\</g;
    $url =~ s/&quot;/\"/g;    
    $url =~ s/&amp;/&/g;
    $description =~ s/&gt;/\>/g;
    $description =~ s/&lt;/\</g;
    $description =~ s/&quot;/\"/g;    
    $description =~ s/&amp;/&/g;
    $description =~ s/(\r\n\r\n|\n\n|\r\r)/\<P\>\n/g;
    $title =~ s/&gt;/\>/g;
    $title =~ s/&lt;/\</g;
    $title =~ s/&quot;/\"/g;    
    $title =~ s/&amp;/&/g;
    $title =~ s/(\r\n\r\n|\n\n|\r\r)/\<P\>\n/g;
    $message =~ s/&gt;/\>/g;
    $message =~ s/&lt;/\</g;
    $message =~ s/&quot;/\"/g;    
    $message =~ s/&amp;/&/g;
    $message =~ s/(\r\n\r\n|\n\n|\r\r)/\<P\>\n/g;

    # split $user into name and email
    $tmpuser = $user;
    $tmpuser =~ s/\+/ /g;
    ($pname, $pemail) = split(/\:/, $tmpuser);

# print "Content-type\: text/html\n\n\n";
# print "confname is $confname <BR>";
# print "dirname is $dirname <BR>";
# print "url is $url <BR>";
# print "description is $description <BR>";
# print "subdir is $subdir <BR>";
# print "user is $user <P>";

    #### make a backup file
    system("cp $fconffile $fconffile.bak");

# print "Made backup <P>";

    #### add a new conference
    if (! open(OUTFILE, ">>$fconffile") ) {
	print "Content-type\: text/html\n\n\n Cannot open $fconffile\: $!";
	return;
    }
    print OUTFILE "$confname\005$dirname\n";
    close(OUTFILE);
    # chmod 0400, $fconffile;

# print "Updated fconffile $fconffile <BR>";

    #### make new directory
    if ($subdir) {
      $dirpath = "$webboardhome\/$subdir\/$dirname";
    }
    else {
      $dirpath = "$webboardhome\/$dirname";
    }
    if (! mkdir($dirpath, 0755) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot create directory $dirpath\: $!<P>"; 
	    return;
    }
    # chmod 0755, $dirpath;

# print "Made new directory $dirpath <BR>";

    #### Check to see if this is a leaf, i.e. a forum with messages

    #### Set up new check directory if it is not a leaf
    # FIX this needs fixing - note cannot create an fconffile in a leaf 
    # FIX Check whether files already exist and need to be overwritten,
    # and for an edit
    if ($subdir eq "") {
        ### make check directory
	$subcheckdir = "$dirpath\/$check";
	$subcheck = "$dirpath\/$checkfile";
        $subconf = "$dirpath\/$conffile";

	if( !mkdir($subcheckdir, 0755) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot create directory $subcheckdir\: $!<P>"; 
	    return;
	}
        # chmod 0755, $subcheckdir;

	### make passwd file (initially will just have administrator)
	# $line = &GetPasswd($pname);
	$line = &GetPasswd("", $pname);
	$file = "> $subcheck";
	if (! open(OUTFILE, $file) ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $subcheck\: $!<P>";
	  return;
	}
	print OUTFILE "$line";
	close(OUTFILE);

	### generate conference file
	if (! open(OUTFILE, ">$subconf") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $subconf\: $!<P>";
	  return;
	}
	close(OUTFILE);

	### generate title file
	if ($url) {
	  $conftitle = "<A HREF=\"$url\">$confname</A>";
	}
	else {
	  $conftitle = $confname;
	}
	$filename = "$dirpath\/$check\/title.html";
	if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
	}
	print  OUTFILE "<CENTER><H1> $header </H1></CENTER>\n";
	print  OUTFILE "<HR>\n";
	print  OUTFILE $description;
	print  OUTFILE "<HR>\n";
	close(OUTFILE);

        # FIX - Add the HTML files for this WebBoard
	&WriteHTMLFiles($dirpath, $confname, $conftitle, $description);
    }
    else { 
    #### generate starting message (first topic) for the new forum
	$filename = "$dirpath\/1.html";
	open(OUTFILE, ">$filename") ||
	    print "Content-type\: text/html\n\n\n Cannot open $filename\: $!";
	print  OUTFILE $message;
	close(OUTFILE);

	$date = &GetDate(0);
	$findex = "$dirpath\/$index";
	open(OUTFILE,">$findex") ||
	    print "Content-type\: text/html\n\n\n Cannot open $findex\: $!";
	print OUTFILE "1\005$date\005$pname\005$title\n";
	close(OUTFILE);
    }
  
    # Print confirmation of addition
    # &AddConfirm($subdir, $user);
  
    if ($subdir eq "") { 
       # Go straight to the new WebBoard to add some Forums 
       &SelectBoard($user, $dirname);
    }
    else { 
       # Go straight back to the list of forums 
       &SelectBoard($user, $subdir);
    }
}
1;

#############################################################################
#
#  Edit Conference
# 
#  INPUT:    conference name, directory path
#  OUTPUT:   HTML text
#  FUNCTION: save the new conference into password file
#
#############################################################################

# FIX Check the password

sub EditConference{
    local($newconfname, $newdirname, $newurl, $newdescription, $subdir, $user) = @_;
    local($confname, $dirname, $url, $description);
    local($filename, $date, $conftitle, $subcheckdir, $subcheck, $subconf,
	  $pname, $pemail, $tmpuser, $file, $line);

    # FIX Display an error message
    if ($confname eq "" || $dirname eq "") {return;}

    # Send this to a subroutine
    # FIX - url should not be broken now
    $url =~ s/&gt;/\>/g;
    $url =~ s/&lt;/\</g;
    $url =~ s/&quot;/\"/g;    
    $url =~ s/&amp;/&/g;
    $description =~ s/&gt;/\>/g;
    $description =~ s/&lt;/\</g;
    $description =~ s/&quot;/\"/g;    
    $description =~ s/&amp;/&/g;
    $description =~ s/(\r\n\r\n|\n\n|\r\r)/\<P\>\n/g;

    # split $user into name and email
    $tmpuser = $user;
    $tmpuser =~ s/\+/ /g;
    ($pname, $pemail) = split(/\:/, $tmpuser);

# print "Content-type\: text/html\n\n\n";
# print "confname is $confname <BR>";
# print "dirname is $dirname <BR>";
# print "url is $url <BR>";
# print "description is $description <BR>";
# print "subdir is $subdir <BR>";
# print "user is $user <P>";

    #### make a backup file
    system("cp $fconffile $fconffile.bak");

# print "Made backup <P>";

    #### add a new conference
    if (! open(OUTFILE, ">>$fconffile") ) {
	print "Content-type\: text/html\n\n\n Cannot open $fconffile\: $!";
	return;
    }
    print OUTFILE "$confname\005$dirname\n";
    close(OUTFILE);
    # chmod 0400, $fconffile;

# print "Updated fconffile $fconffile <BR>";

    #### make new directory
    if ($subdir) {
      $dirpath = "$webboardhome\/$subdir\/$dirname";
    }
    else {
      $dirpath = "$webboardhome\/$dirname";
    }
    if (! mkdir($dirpath, 0755) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot create directory $dirpath\: $!<P>"; 
	    return;
    }
    # chmod 0755, $dirpath;

# print "Made new directory $dirpath <BR>";

    #### Check to see if this is a leaf, i.e. a forum with messages

    #### Set up new check directory if it is not a leaf
    # FIX this needs fixing - note cannot create an fconffile in a leaf 
    # FIX Check whether files already exist and need to be overwritten,
    # and for an edit
    if ($subdir eq "") {
        ### make check directory
	$subcheckdir = "$dirpath\/$check";
	$subcheck = "$dirpath\/$checkfile";
        $subconf = "$dirpath\/$conffile";

	if( !mkdir($subcheckdir, 0755) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot create directory $subcheckdir\: $!<P>"; 
	    return;
	}
        # chmod 0755, $subcheckdir;

	### make passwd file (initially will just have administrator)
	# $line = &GetPasswd($pname);
	$line = &GetPasswd("", $pname);
	$file = "> $subcheck";
	if (! open(OUTFILE, $file) ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $subcheck\: $!<P>";
	  return;
	}
	print OUTFILE "$line";
	close(OUTFILE);

	### generate conference file
	if (! open(OUTFILE, ">$subconf") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $subconf\: $!<P>";
	  return;
	}
	close(OUTFILE);

	### generate title file
	if ($url) {
	  $conftitle = "<A HREF=\"$url\">$confname</A>";
	}
	else {
	  $conftitle = $confname;
	}
	$filename = "$dirpath\/$check\/title.html";
	if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
	}
	print  OUTFILE "<CENTER><H1> $header </H1></CENTER>\n";
	print  OUTFILE "<HR>\n";
	print  OUTFILE $description;
	print  OUTFILE "<HR>\n";
	close(OUTFILE);

        # FIX - Add the HTML files for this WebBoard
	&WriteHTMLFiles($dirpath, $confname, $conftitle, $description);
    }
    else { 
    #### generate starting message (first topic) for the new forum
	$filename = "$dirpath\/1.html";
	open(OUTFILE, ">$filename") ||
	    print "Content-type\: text/html\n\n\n Cannot open $filename\: $!";
	print  OUTFILE $message;
	close(OUTFILE);

	$date = &GetDate(0);
	$findex = "$dirpath\/$index";
	open(OUTFILE,">$findex") ||
	    print "Content-type\: text/html\n\n\n Cannot open $findex\: $!";
	print OUTFILE "1\005$date\005$pname\005$title\n";
	close(OUTFILE);
    }
  
    # Print confirmation of addition
    # &AddConfirm($subdir, $user);
  
    if ($subdir eq "") { 
       # Go straight to the new WebBoard to add some Forums 
       &SelectBoard($user, $dirname);
    }
    else { 
       # Go straight back to the list of forums 
       &SelectBoard($user, $subdir);
    }
}
1;


#############################################################################
#
#  AddConfirm
# 
#  INPUT:    Confirm the addition
#  OUTPUT:   HTML text
#  FUNCTION: create fill-out forms to delete a user name
#
#############################################################################

sub AddConfirm{
    local($subdir, $user) = @_;
   
   if ($subdir) {
      $conftype = "Forum";
   }
   else {
      $conftype = "WebBoard";
   }

################ Print Confirmation
    print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Added New $conftype </TITLE>
</HEAD>
<BODY BGCOLOR="#55CCCC">
<H2> Added a New $conftype : $confname </H2>
<A HREF="$action/$subdir:select:::${user}">
<B>RETURN to the index of ${conftype}s</B></A>
EOF
###############

# Print the navigation bar and address
&Address();

}
1;

   
#############################################################################
#
#  DeleteConference
# 
#  INPUT:    user information (administrator)
#  OUTPUT:   HTML text
#  FUNCTION: create fill-out forms to delete a user name
#
#############################################################################

sub DeleteConference{
   local($user, $subdir) = @_;
   local($key);

# FIX Check the password
    
   if ($subdir) {
      $conftype = "Forum";
   }
   else {
      $conftype = "WebBoard";
   }

################
    print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Delete a $conftype </TITLE>
</HEAD>
<BODY BGCOLOR="#55CCCC">
<H2> Delete a $conftype</H2>
<BR>
<FORM METHOD="POST" ACTION="$change/$subdir:removeconf:$user">
<B>Click to select the $conftype name to be deleted: </B><BR> 
<SELECT NAME="delconf">
EOF
################

   ## Print out a list of forums or WebBoards to choose
   $cnt = 0;
   foreach $key (sort keys %conference) {
      if ($key eq "") { next;}
      if ($cnt == 0) {
	print "<OPTION SELECTED> $conference{$key}\n";
      }
      else {
        print "<OPTION> $conference{$key}\n";
      }
      $cnt++;
   }

################
    print <<EOF;
</SELECT>
<P>
<INPUT TYPE="submit" VALUE="SUBMIT">
<B>Click this button to submit the delete request</B><P> 
or <A HREF="$action/$subdir:select:::${user}">
<B>RETURN to the list of ${conftype}s</B>
</A>
</FORM>
EOF
################

# Print the navigation bar and address
&Address();

}
1;

#############################################################################
#
#  ConfirmDelete
# 
#  INPUT:    conference name, user, directory
#  OUTPUT:   HTML text
#  FUNCTION: confirm remove conference name from conference file
#
#  TBD: Remove conference directory also.
#
#############################################################################

# FIX

sub ConfirmDelete{
    local($confname, $user, $subdir) = @_;
    local(@confs, $i);
    @names = ();
   
# Print confirmation
################ 
    print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Confirm Deletion of $conftype </TITLE>
</HEAD>
<BODY BGCOLOR="#55CCCC">
<H2> Confirm Deletion of $conftype : $confname</H2>
<FORM METHOD="POST" ACTION="$change/$subdir:removeconf:$user">
<H3> Do you really want to delete this $conftype ? <BR>
<INPUT TYPE="radio" NAME="confirm" VALUE="YES" CHECKED><BR>
<INPUT TYPE="radio" NAME="confirm" VALUE="NO"><BR>
</H3>
</FORM>
EOF
###############

# Print the navigation bar and address
&Address();

}
1;

#############################################################################
#
#  RemoveConf
# 
#  INPUT:    conference name
#  OUTPUT:   HTML text
#  FUNCTION: remove conference name from conference file
#
#  TBD: Remove conference directory also.
#
#############################################################################

# FIX

sub RemoveConf{
    local($confname, $user, $subdir, $confirm) = @_;
    local(@confs, $i);
    @names = ();
   
   if ($subdir) {
      $conftype = "Forum";
   }
   else {
      $conftype = "WebBoard";
   }

if ($confirm eq "YES") {
   
    # Backup the index file
    system("cp $fconffile $fconffile.bak");
   
    ## Remove the conference name from the index file
    open(INFILE, "< $fconffile") ||
	print "Content-type\: text/html\n\n\n Cannot open $fconffile\: $!";
    while(<INFILE>) {
        # Match random spaces or newlines
	if(/^$confname\s*\005/) { next; }
	push(@confs, $_);
    }
    close(INFILE);

    open(OUTFILE, "> $fconffile") ||
	print "Content-type\: text/html\n\n\n Cannot open $fconffile\: $!";
    foreach $i (0 .. $#confs) {
	print OUTFILE $confs[$i];
    }
    close(OUTFILE);
    # chmod 0400, $fconffile;
   
    ## FIX - Move the directory 
    $confdir = "$webboardhome/$conference{$confname}";
    # system("mv $confdir $confdir.DEL");

    # Go to the updated list of forums 
    &SelectBoard($user, $subdir);
} 
else {  # Don't delete it

# Print confirmation
################ 
    print <<EOF;
Content-type: text/html

<HTML>
<HEAD>
<TITLE> $conftype Not Deleted </TITLE>
</HEAD>
<BODY BGCOLOR="#55CCCC">
<H2> Did Not Delete the $conftype : $confname </H2>
<A HREF="$action/$subdir:select:::${user}">
<B>RETURN to the list of ${conftype}s</B></A>
EOF
###############

# Print the navigation bar and address
&Address();
}

}
1;


#############################################################################
#
#  WriteHTMLFiles
# 
#  INPUT:    conference directory ($subdir), name, url
#  OUTPUT:   HTML files in $subdir//HTML directory
#
#############################################################################
sub WriteHTMLFiles{
    local($dirpath, $confname, $conftitle, $description) = @_;

    # Make the directory
    $HTMLdir = "$dirpath\/HTML";
    if (! mkdir($HTMLdir, 0755) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot create directory $HTMLdir\: $!<P>"; 
	    return;
    }

    #### Make the Entry page files

    # Entry page header
    $filename = "$HTMLdir\/EntryHeader.html";
    if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
    }
################ 
    print OUTFILE <<EOF;
<HTML>
<HEAD>
<TITLE>
$confname
</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">

<CENTER>
<H2>Welcome to the WebBoard<SUP><FONT SIZE="-2">TM</FONT></SUP> for</H2>
<H1>
$conftitle
</H1>
</CENTER>
<HR>
$description
<HR>
<P>
<B>
<FONT SIZE="+1">
Please sign in to read the WebBoard Forums: 
</B>
</FONT>

EOF
################ 
    close(OUTFILE);

    # Entry page footer
    $filename = "$HTMLdir\/EntryFooter.html";
    if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
    }
################ 
    print OUTFILE <<EOF;
EOF
################ 
    close(OUTFILE);


    #### Make the Select page files

    # Select page header
    $filename = "$HTMLdir\/SelectHeader.html";
    if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
    }
################ 
    print OUTFILE <<EOF;
<HTML>
<HEAD>
<TITLE>
WebBoard Forums for 
$confname
</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<H1>
WebBoard Forums for 
<BR>
$conftitle
</H1>
</CENTER>
<HR SIZE=1 NOSHADE>
<H3>
Choose a Forum from the list below: 
</H3>
<H3>
EOF
################ 
    close(OUTFILE);

    # Select page footer
    $filename = "$HTMLdir\/SelectFooter.html";
    if (! open(OUTFILE, ">$filename") ) {
	  print "Content-type\: text/html\n\n\n";
	  print "Cannot open $filename\: $!<P>";
	  return;
    }
################ 
    print OUTFILE <<EOF;
</H3>
EOF
################ 
    close(OUTFILE);

}
1;

