#! /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";

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

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

    print "Content-type\: text/html\n\n";
    print "\n";
    print "<TITLE> ADD A NEW FORUM </TITLE>\n";
    print "<H1> Add a New Forum</H1>\n";
    print "<IMG SRC=\"$gif/pointer.xbm\" ALIGN=\"left\" ALT=\"\">";
    print "<A HREF=\"$action/$subdir\:select\:\:\:${user}\">\n";
    print "<B>RETURN to the index of forums</B>\n";
    print "</A>\n";

    print "<HR>\n";
    print "<FORM METHOD=\"POST\" ACTION=\"$change/$subdir\:preview\:$user\">\n";

    print "<B>Type in a forum name to be added\: </B><BR>\n";
    print "<INPUT SIZE=56 NAME=\"newconf\" VALUE=\"$newconf\"><P>\n";
    print "<B>Type in a directory name for the forum\: </B><BR>\n";
    print "<INPUT SIZE=56 NAME=\"newdir\" VALUE=\"$newdir\"><P>\n";

    if ($subdir ne "") {
	print "<B>Type in a subject title for a initial message\: </B><BR>\n";
	print "<TEXTAREA NAME=\"title\" ROWS=2 COLS=55>${title}</TEXTAREA><P>\n";
	print "<B>Type in the initial message\: </B><BR>\n";
	print "<TEXTAREA NAME=\"message\" ROWS=20 COLS=55>${message}</TEXTAREA><P>\n";
    }
    else {
	print "<B>Type in a new forum title\: </B><BR>\n";
	print "<TEXTAREA NAME=\"title\" ROWS=4 COLS=55>${title}</TEXTAREA><P>\n";
    }

    print "<INPUT TYPE=\"submit\" VALUE=\"SUBMIT\">\n";
    print "<B>Press this button to preview and submit the forum.</B><P>";
    print "</FORM>\n";
    print "<HR>\n";
    Address();
    
}
1;

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

sub SaveConference{
    local($conf, $dirpath, $title, $message, $subdir, $user) = @_;
    local($filename, $date, $subcheckdir, $subcheck, $subconf,
	  $pname, $pemail, $tmpuser, $file, $line);

    if ($conf eq "" || $dirpath eq "") {return;}

    $title =~ s/&gt;/\>/g;
    $title =~ s/&lt;/\</g;
    $title =~ s/&quot;/\"/g;    
    $title =~ s/&amp;/&/g;
    $message =~ s/&gt;/\>/g;
    $message =~ s/&lt;/\</g;
    $message =~ s/&quot;/\"/g;    
    $message =~ s/&amp;/&/g;
    $title =~ s/(\r\n\r\n|\n\n|\r\r)/\<P\>/g;
    $title =~ s/(\r\n|\n|\r|\s+)/ /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);

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

    #### add a new conference
    open(OUTFILE, ">>$conffile") ||
	print "Content-type\: text/html\n\n\n Cannot open $conffile\: $!";
    print OUTFILE "$conf\005$dirpath\n";
    close(OUTFILE);
    # chmod 0400, $conffile;

    #### make new directory
    $dirpath = "$webboardhome\/$subdir\/$dirpath";
    mkdir($dirpath, 0755) ||
	print "Content-type\: text/html\n\n\n Cannot open $dirpath\: $!";	
    chmod 0755, $dirpath;

    #### generate starting message to the new directory
    if ($subdir ne "") {
	$filename = "$dirpath\/0.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 "0\005$date\005$pname\005$title\n";
	close(OUTFILE);
    }
    else {
        ### make check directory
	$subcheckdir = "$dirpath\/$check";
	$subcheck = "$dirpath\/$checkfile";
        $subconf = "$dirpath\/$conffile";

	if( !mkdir($subcheckdir, 0700) ) {
	    print "Content-type\: text/html\n\n\n";
	    print "Cannot open $subcheckdir: $!<P>"; 
	    return;
	}

	### make passwd file (initially will just have administrator)
	# $line = &GetPasswd($pname);
	$line = &GetPasswd("", $pname);
	$file = "> $subcheck";
	open(OUTFILE, $file);
	print OUTFILE "$line";
	close(OUTFILE);

	### generate conference file
	$file = "> $subconf";
	open(OUTFILE, $file);
	close(OUTFILE);

	### generate title file
	$filename = "$dirpath\/$check\/title.html";
	open(OUTFILE, ">$filename") ||
	    print "Content-type\: text/html\n\n\n Cannot open $filename\: $!";
	print  OUTFILE $title;
	close(OUTFILE);


    }
}
1;

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

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


    print "Content-type\: text/html\n\n";
    print "\n";
    print "<TITLE> DELETE A FORUM </TITLE>\n";
    print "<H1> Delete a Forum</H1>\n";
    print "<IMG SRC=\"$gif/pointer.xbm\" ALIGN=\"left\" ALT=\"\">";
    print "<A HREF=\"$action/$subdir\:select\:\:\:${user}\">\n";
    print "<B>RETURN to the index of forums</B>\n";
    print "</A>\n";

    print "<HR>\n";
    print "<FORM METHOD=\"POST\" ACTION=\"$change/$subdir\:removeconf\:$user\">\n";
    print "Enter the <B>Forum name</B> to be deleted\: <BR> \n";
    print "<INPUT SIZE=50 NAME=\"delconf\" VALUE=\"\"><P>\n";
    print "Press the submit button to submit\: ";
    print "<INPUT TYPE=\"submit\" VALUE=\"SUBMIT\">\n";
    print "</FORM>\n";
    print "<HR>\n";
    Address();
}
1;

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

sub RemoveConf{
    local($conf) = @_;
    local(@confs, $i);
    @names = ();
    system("cp $conffile $conffile.bak");
    open(INFILE, "< $conffile") ||
	print "Content-type\: text/html\n\n\n Cannot open $conffile\: $!";
    while(<INFILE>) {
	if(/^$conf\005/) { next; }
	push(@confs, $_);
    }
    close(INFILE);

    open(OUTFILE, "> $conffile") ||
	print "Content-type\: text/html\n\n\n Cannot open $conffile\: $!";
    foreach $i (0 .. $#confs) {
	print OUTFILE $confs[$i];
    }
    close(OUTFILE);
    # chmod 0400, $conffile;
}
1;

