#! /srvtools/http/bin/perl
#############################################################################
#  
#   File:          PostIt.pl
#
#   Author:        Jungmin Kim
#
#   Created:       03/05/95
#   Modified:      
#   Version: 
#
#   Description:   &MakeNewFile generates new message file with passed 
#                  arguments
#               
#############################################################################


#############################################################################
#
#   MakeNewFile
#
#   INPUT:  path name, file name, method(reply or post), title,
#           message, user information
#
#   OUTPUT: name of new message (file name)
#
#   FUNCTION: Generate a new message file and save the header of it to
#             index file. 
#
#############################################################################

require "GetDate.pl";
require "CheckPasswd.pl";
require "npac-lib";		# Some useful routines

sub MakeNewFile{
    local($dir, $file, $method, $user, $title, $message, 
	  $hname, $hname2, $subdir) = @_;
    local($uname, $uemail, $tmpuser, $cnt, @in, $i, $init);

    $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;

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

    $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;

    #### dummy header
    push(@in, ("0", &GetDate(0), $uname, "$title\n"));
    $cnt =0;

    $filename = "$webboardhome\/$subdir\/$dir\/$index";


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


    #### open index file to read headers of a conference

    # ******************************************* hardwired variable
    open(INFILE, "< $filename") || print "Cannot open $filename\: $!";

    while(<INFILE>) {
	# ********************************************* modified
	push(@in, split(/\005/, $_));
	# *****************************************************
	$cnt += 4;
    }
    close(INFILE);

    #### generate new file name
    $in[0] = "1";
    if ($method eq "newpost") {
	for($i=4; $i < $#in; $i += 4) {
	  $in[$i] =~ /^([^\.]+)/;
	  $in[0] = $1 + 1 if $in[0] <= $1;
        }
    }
    elsif($method eq "response") {
        $next = "1";
	$init = $1 if $file =~ /^(.+)\.([^\.]*)/;
	$init = $file if $file =~ /^[^\.]+$/;
	for($i = 0; $i <= $cnt; $i += 4) {
	    if ($in[$i] =~ /^$init\.([^\.]*)/) {
		$next = $1 + 1 if $next <= $1;
	    }
        }
	$in[0] = $init."\.".$next;
	if ($autoreply eq "yes") {
	    &SendMail("$webboardhome\/$subdir\/$check",$hname);
	}
    }
    else {
        $next = "1";
	for($i = 0; $i <= $cnt; $i += 4) {
	    if ($in[$i] =~ /^$file\.([^\.]*)/) {
		$next = $1 + 1 if $next <= $1;
	    }
        }
	$in[0] = $file."\.".$next;
	if ($autoreply eq "yes") {
	    &SendMail("$webboardhome\/$subdir\/$check",$hname2);
	}
    }

    ### write the modified headers to index file
    $i = 0;
    open(OUTFILE, "> $filename") || 
	print "Content-type\: text/html\n\n\n Cannot open $filename\: $!";
    while($i<=$cnt) {
	# ****************************************************** modified
	print OUTFILE "$in[$i++]\005$in[$i++]\005$in[$i++]\005$in[$i++]";
        # ***************************************************************
    }
    close(OUTFILE);

    #### write the message file

    $filename = "> $webboardhome\/$subdir\/$dir\/$in[0].html";
    open(OUTFILE,$filename) ||
	print "Content-type\: text/html\n\n\n Cannot open $filename\: $!";
    print  OUTFILE $message;
    close(OUTFILE);
   
   ### Confirm that the message has been added
   
   ### Display confirmation that message has been added
   $number = $in[0];
   ($tno, $junk) = split(/\./, $number);
   $url = "$action/$subdir\:read\:${dir}\:${tno}\:${user}#${number}"; 
   $forum = $conference{$dir};
   $confirm = &GetFile("HTML/Confirm.html", $webboardhome);

   # Put in the right parameters for the URL of the new message
   $confirm =~ s/\$url/$url/;
   $confirm =~ s/\$forum/$forum/;

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

    #### if autoreply is 'yes', send a mail 
    if($autoreply eq "yes") {
	$filename = "$webboardhome\/$subdir\/$dir\/letter";
	$filename = "mail $orguser < $filename";
	system($filename);
    }

    $in[0];
}
1;



########################################################################
# Send confirmation mail to the poster
########################################################################
sub SendMail{
    local($path, $name) = @_;
    local($command, $addr, $file);

    $addr = &GetAddress($name);
    $addr =~ s/\@/\\\@/;
    $file = "$path\/letter";
    $command = "mail $addr < $file";
    $command =~ s/</\</g;
#print "Content-type\: text/html\n\n\n $command <p>";
#$command = "mail jmkim\@nova.npac.syr.edu < /home/A6G/jmkim/public_html/cgi-bin/spring1/Test_version/abc/check/letter";
    system($command);
}
1;
