#!/usr/local/bin/perl

do "board_lib.cgi";

###########################################################################
# Get Form Information
print "Content-type: text/html\n\n";
print "<head><title>Mailing List for $title</title></head>\n";
print "<body><h1>Mailing List for $title</h1>\n";
print("<p>Users subscribed to this mailing list will receive an email copy\n");
print("of every new posting to $title.<p>\n");
print "<P>\n";

&parse_form;

$addrs=$FORM{'addrs'};

if ($addrs eq "") {
    &reset_form;
}
$tmpfile="$basedir/$mesgdir/$maili.$$.tmp";
$addrs=trim($addrs);
$_ = $addrs ;
if (/\s/ || !(/^..*\@..*\...*$/)) {
    print "\"",$addrs,"\": Illegally formed address<p>\n";
    &reset_form;
    }
elsif ($FORM{'add'}) {
    open OUT, ">$tmpfile" || endhtml("(2)not added: $!");
    if (-f $mailaddrsfile) {
	open INP, "<$mailaddrsfile" || endhtml("(1)not added:$!");
	$found=0;
	$count=0;
	while (<INP>) { 
	    $count=$count+1;
	    if (/^$addrs$/) {
	    $found=1;
	    }
	    print OUT;
	    }
	if ($found) {
	    close OUT;
	    close INP;
	    unlink("$tmpfile") || print("<p>(5)$!<p>");
	    endhtml("$addrs: already in mailing list");
	    }
	close INP || endhtml("(7)not added: $!");;
	}
    if ($count > $maxcount) {
 	endhtml("Not added: Too many subscribers!");
    }
    open PIP, "|$sendmail $addrs >/dev/null" || endhtml("(4)not added: $!");
    print PIP "From: $administrator ($title)\n";
    print PIP "To: $addrs\n";
    print PIP "Subject: Welcome to $title\n\n\n";
    print PIP "Your address ($addrs) has been added to $title\n";
    print PIP "At $indexurl.\n";
    print PIP "To delete your address from this list,\ngo to $mailingcgi\n";
    print PIP "Report misuses to $administrator";
    close PIP;
    if ($?==0) {
	# mail sent, add address
	print OUT "$addrs\n";
	close OUT || endhtml("(6)not added: $!");
	rename($tmpfile,$mailaddrsfile) || endhtml("(3)not added: $!");
	endhtml("$addrs: added.");
	}
    else {
	close OUT;
	endhtml("SENDMAIL to $addrs failed! Not added.");
	}
    }
elsif ($FORM{'del'}) {
    open OUT, ">$tmpfile" || endhtml("(2)not deleted: $!");
    open INP, "<$mailaddrsfile" || endhtml("(1)not deleted: $!");
    $found=0;
    while (<INP>) {
	if (/^$addrs$/) { $found=1;}
	else { print OUT; }
	}
    close OUT;
    close INP;
    if (!$found) {
	unlink($tmpfile) || print("<p>(5)$!<p>\n");
	endhtml("Not deleted: $addrs not found");
	}
    rename($tmpfile,$mailaddrsfile) || endhtml("(3)not deleted: $!");
    open PIP, "| $sendmail $addrs > /dev/null" || endhtml("(4)not deleted: $!");
    print PIP "From: $administrator ($title)\n";
    print PIP "To: $addrs\n";
    print PIP "Subject: Farewell from $title\n";
    print PIP "\n";
    print PIP "Your address ($addrs) has been \nremoved from $title\n";
    print PIP "at $indexurl.\n";
    print PIP "Report misuses to $administrator";
    close PIP;
    endhtml("$addrs deleted.\n");
    }
else {
   &reset_form;
}
##### end of main #####

sub reset_form {
    print("<br clear=all><hr><P>\n");
    print("<form action=\"$mailingcgi\" method=post>\n");
    print("Type an email address:\n");
    print("<input name=addrs size=50>\n");
    print("<p>\n");
    print("<input type=submit name=add value=\"Click here to add to mailing list\">\n");
    print("<p>\n");
    print("<input type=submit name=del value=\"Click here to delete from mailing list\">\n");
    print("<p>\n");
    endhtml("");
    }

sub endhtml {
    if ($_[0] ne "") {
	writelog("endhtml: $_[0]");
	}
    print("<p>\n");
    print("<b>",$_[0],"</b>");
    print("<p>\n");
    print("<a href=\"$indexurl\">\n");
    print("Click here to go back to $title </a>\n");
    print("</body>\n");
    exit(0);
}

