#!/usr/npac/bin/perl

# this script lets users add themselves to a mailing list
# for Rob's Mass Mail

# please see readme.txt for installation instructions and copyright 
# information

# these should be the same as $maildir, $mailprog in massmail.cgi
$maildir = "/tmp_mnt/npac/server/foxsden00/sroot/droot/psf/FFE/m";
$mailprog = '/usr/lib/sendmail';

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

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

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

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;
}

$what = $FORM{'what'};

if ($what eq 'add') {

	$list = $FORM{'list'};
	$name = $FORM{'name'};
	$email = $FORM{'email'};	

	open(FILE, "$maildir/$list\.txt") || die "Can't find data file.\n";

	@info = <FILE>;
	close(FILE);
	$num = @info;

	for ($a = 0; $a < $num; $a++) {

		($recipient,$n,$nochop) = split(/``/,$info[$a]);
		if ($recipient eq $email) {
			$err = 1;
			&error1;
		}

	}

	open(FILE, ">>$maildir/$list\.txt") || die "Can't find data file.\n";
	print FILE "$email``$name``x\n";
	close(FILE);

	open (MAIL, "|$mailprog $email") || die "Can't open $mailprog!\n";

	print MAIL "Subject: Successful Addition to Mailing List\n";

	print MAIL "You have been successfully added to the mailing list through ";
	print MAIL "the form at $ENV{'HTTP_REFERER'}.  If you ";
	print MAIL "would like to remove yourself from the list, ";
	print MAIL "visit $ENV{'HTTP_REFERER'}.\n";
	
	close(MAIL);

	print "<html><head><title>User Added</title></head>\n";
	print "<body bgcolor=white link=blue vlink=blue text=black>\n\n";
	print "<!--\n";
	print "Rob's Mass Mail v2.02 Copyright 1998 by Rob Eisler\n";
	print "eislerr\@tdi.uregina.ca\n";
	print "http://tdi.uregina.ca/~eislerr\n";
	print "-->\n\n";
	print "<font face=arial>\n";
	print "<center>\n";
	print "<font size=5>You have been added to the mailing list. You should receive a confirmation e-mail shortly.</font><p>\n";
	print "<big><b>\&#183;</big></b> <a href=\"$ENV{'HTTP_REFERER'}\">Back</a> <big><b>\&#183;</b></big>\n";
	print "</center></font></body></html>\n";
	exit;

} elsif ($what eq 'remove') {

	$list = $FORM{'list'};
	$email = $FORM{'email'};	

	$found = 0;

	open(FILE, "$maildir/$list\.txt") || die "Can't find data file.\n";

	@info = <FILE>;
	close(FILE);
	$num = @info;

	open(FILE, ">$maildir/$list\.txt") || die "Can't find data file.\n";

	for ($a = 0; $a < $num; $a++) {

		($recipient,$name,$nochop) = split(/``/,$info[$a]);
		if ($recipient eq $email) {
			$found = 1;
		} else {
			print FILE "$recipient``$name``x\n";
		}

	}

	close(FILE);

	if ($found == 0) {
		$err = 3;
		&error1;
	}

	open (MAIL, "|$mailprog $email") || die "Can't open $mailprog!\n";

	print MAIL "Subject: Successful Removal From Mailing List\n";

	print MAIL "You have been successfully removed from the mailing list ";
	print MAIL "through the form at $ENV{'HTTP_REFERER'}.  If you ";
	print MAIL "would like to re-add yourself to the list, ";
	print MAIL "visit $ENV{'HTTP_REFERER'}.\n";
	print MAIL "This is the last e-mail you will receive from this mailing list.\n";
	
	close(MAIL);

	print "<html><head><title>User Removed</title></head>\n";
	print "<body bgcolor=white link=blue vlink=blue text=black>\n\n";
	print "<!--\n";
	print "Rob's Mass Mail v2.02 Copyright 1998 by Rob Eisler\n";
	print "eislerr\@tdi.uregina.ca\n";
	print "http://tdi.uregina.ca/~eislerr\n";
	print "-->\n\n";
	print "<font face=arial>\n";
	print "<center>\n";
	print "<font size=5>You have been removed from the mailing list. You should receive a confirmation e-mail shortly.  This is the last e-mail you will receive from this list.</font><p>\n";
	print "<big><b>\&#183;</big></b> <a href=\"$ENV{'HTTP_REFERER'}\">Back</a> <big><b>\&#183;</b></big>\n";
	print "</center></font></body></html>\n";
	exit;

} else {
	$err = 2;
	&error1;
}

sub error1 {

	print "<html><head><title>Error</title></head>\n";
	print "<body bgcolor=white link=blue vlink=blue text=black>\n\n";
	print "<!--\n";
	print "Rob's Mass Mail v2.02 Copyright 1998 by Rob Eisler\n";
	print "eislerr\@tdi.uregina.ca\n";
	print "http://tdi.uregina.ca/~eislerr\n";
	print "-->\n\n";
	print "<font face=arial>\n";
	print "<center>\n";
	print "<font size=5>Error</font><p>\n";
	if ($err == 1) {
		print "$email is already on the list.<p>\n";
	} elsif ($err == 2) {
		print "This script has been called incorrectly.<p>\n";
	} elsif ($err == 3) {
		print "Could not find $email in this list<p>\n";
	} else {
		print "Error Unknown<p>\n";
	}
	print "<big><b>\&#183;</big></b> <a href=\"$ENV{'HTTP_REFERER'}\">Back</a> <big><b>\&#183;</b></big>\n";
	print "</center></font></body></html>\n";
	exit;

}
                                                                                                                                                                                                                                                                            
