#!/usr/local/bin/perl
###################################################################################
#      Mailomatic.pl               Version 1.6                                    #
#      Copyright 2001 Paul Ryan    mailomatic@mujik.com                           #
#      Created  15/02/2001              Last Modified                             #
#     This script got great inspiration from the NPH_MAILLIST script from         #
#     Marketrends(mrt@marketrends.net 1998),from the excellent "Page-o-matic"     #
#     script by Dave Palmer (dave@upstatepress.com 1997)			  #
#     Thanks also to Shawn Pitty at www.spylight.com for the help in debugging    #
#     a problem with malformed headers on BSD boxes. The code is a lot cleaner    #
#     thanks to his interest.							  #
#     This is script is freeware but you must leave in the link to my email       #
#     address. If you really want to remove it, you can pay me US$10.00 :)        #
#                                                                                 #
###################################################################################

# Set your Variables - there are a rather lot of these :( sorry) 

# Set the url path where you want to return to after using mailomatic
# Usually this will be the page where you put the input form 

$webform ="http://www.mydomain.com.au/signup.html";

# Set the name of your lists - this is used through out the script to print the name
# of your list (eg: print "you are subscribed to $listname") depending on what type of
# list selected (ie: HTML or TEXT)

$htmllistname = "the MY DOMAIN HTML List";
$textlistname = "the MY DOMAIN TEXT List";

# The url to your site. This is used in both links and for printing out the site name
# (eg: <a href:http://$website> $website</a>) so make sure it is the legitimate url to your homepage

$website="www.mydomain.com.au";

# Path to this script 

$cgiurl="http://www.mydomain.com.au/cgi-bin/mailomatic.pl";

# Database files. You will need to chmod to 777, so probably wise if you don't put 
# them in a http accessible directory. These files are never directly accessed
# by http - they are databases for the script - and 
#you will have to put in the 'base' path of these files.
# that is, "/u131/cust/home/public_html/mailomatic/text.txt"
#NOT "http://www.mydomain.com/mailomatic/text" or "../mailomatic/text.txt"  

$textfile = "/custs/home/s/username/protected/tlist.txt";
$htmlfile ="/custs/home/s/username/protected/hlist.txt";

# Your edit files. You will also need to chmod to 777

$textnotice = "/custs/home/s/username/protected/tnotice.txt";
$htmlnotice ="/custs/home/s/username/protected/hnotice.txt";

# name you want in the "From:" header of emails you send. When you get 'replies' to
# post outs, if you make the header different, you will know immediately which list the
# sender belongs to.

$textuname = "My Domain Text News";
$htmluname = "My Domain HTML News";

# The "return" email address,(the backslash (\) is needed to cancel the @ symbol as code.)
# Usually you will have the same reply address for both but if you want to have separate
# addresses for the two lists, here is where you can set it.

$textaddress = "list\@mydomain.com.au";
$htmladdress = "list\@mydomain.com.au";

# Set your admin password name. You should use something other than the default,
# "mom" to keep others from breaking in. There is only one password needed to gain access to
# all the admin functions (both lists), so try to make this a little difficult to guess.

$admin = "mom";
##############################################################################
# The path of some server apps this script uses. They need to be set for your server
# so if you don't know them, ask your systems administrator. If you have telnet access
# to your site, use the "whereis" command to get this info.

$mailprog="/usr/lib/sendmail";
$date_command = "/bin/date";

# Unix Sort Path.
$sort="/usr/bin/sort";

#An environment variable we need for the dynamic html
$server_software=$ENV{'SERVER_SOFTWARE'};

# Done!
###############################################################################
###############################################################################


###############################################################################
#The following code should not have to be touched for the script to run. Feel
#free to customise but make sure you know what you are doing
#############################START MAIN PROGRAM################################

#do a bit of housekeeping - this sets the format of the "date" field on dynamic
#pages and emails. 
$date = `$date_command +"%a, %b %d, %y"`; chop($date);

#do the input routine - supports both "GET" and "POST" Form methods. 

if ($ENV{'REQUEST_METHOD'} eq "GET") 
{
	$buffer = $ENV{'QUERY_STRING'};
}elsif ($ENV{'REQUEST_METHOD'} eq "POST") 
{
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Routine Perl, split the name-value pairs and de-webify. This instance was pinched
#from the pageomatic script by Dave Stewart but I am sure he pinched it from someone
#else before me ...:)
@input = split(/&/, $buffer);
foreach $input(@input) 
{
	($name, $value) = split(/=/, $input);
	$name =~ tr/+/ /;
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$FORM{$name} = $value;
}

#set up some input variables

$eaddress=$FORM{'emailaddress'};
$mstatus = $FORM{'status'};
$etype=$FORM{'emailtype'};

if( $etype eq "on"){
	$mailfile=$htmlfile;
	$notice=$htmlnotice;
	$uname=$htmluname;
	$address=$htmladdress;
	$listname=$htmllistname;
}
else{
	$mailfile=$textfile;
	$notice=$textnotice;
	$uname=$textuname;
	$address=$textaddress;
	$listname=$textlistname;
}
#

#Now we need to check for the various different ways to call mailomatic and
#redirect accordingly. 

#check for a user confirmation
if($mstatus eq "addmail"){&add_email;}

#Admin Add User
elsif($mstatus eq "momadd"){&mom_add;}
   
#A User self delete   
elsif($mstatus eq "delmail"){&del_mail;}

#Admin Delete User
elsif($mstatus eq "momdel"){&mom_del;} 

#Admin edit - new session screen
elsif($mstatus eq "momedit"){&mom_edit;}

#Send a mailout
elsif($mstatus eq "momsend"){&mom_send;}
 
#Write an Admin update to file
elsif($FORM{'body'}){&edit_notice;}


#No stand alone special functions, so now we check the address 
#- if there isn't one we throw a debug screen and exit

&no_input unless $FORM{'emailaddress'};

##Check if the email address is the admin password, 
#and make the admin panel accessable if so

if($FORM{'emailaddress'} eq $admin){
	&admin_function;
	}

## Otherwise, check the "email field to see that it is in legitimate email address format
#This does NOT check if the email address is valid, just that it is in the correct
# format (eg: something@something.something). 

elsif($FORM{'emailaddress'} !~ /\@/) { &bad_email();}
elsif($FORM{'emailaddress'} !~ /\./) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\ /) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\)/) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\(/) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\:/) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\//) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\\/) { &bad_email();}
elsif($FORM{'emailaddress'} =~ /\http:/) { &bad_email();}


#A check for the last admin function (admin add), This function inputs
#a new address directly into the system so we needed to check it was  
#in correct format first
 


#to get this far, we have an email address in correct format and without any .
#admin switches, so we send a 'please confirm' mail (we do not enter them 
#into the list, yet), tell them a mail is on the way and exit the script
else{

&send_check;
}

##DONE 
###################################END-MAIN####################################
###############################################################################
#################################START SUBS ###################################

######################User Sub Routines - Public Accessible####################
###############################################################################

##################### SUB send_check ######################
sub send_check{ 
    print "Server: $server_software\n";
    print "Content-type: text/html\n\n";

#we make a local copy of 'emailaddress'  - probably overkill but 
#makes sure we don't accidentally overwrite any globals. I do this in all the 
#subs - like I say, probably paranoia but...

my $ent0 = $FORM{'emailaddress'};


open (SCMA, $mailfile) || die "Cannot open $mailfile";
@wx = <SCMA>;
close (SCMA);
$bound=scalar(@wx);
$match=0;

for ($i=0;$i<=$bound;$i++) {
     $_=$wx[$i];
     s/\012//;
     $wx[$i]=$_;

  if ($ent0 eq $wx[$i]) {
     $match=1;
    }
}
if ( $match eq 1 ) {
     &exist; 
  }
  else {
open (MAIL, "|$mailprog $ent0") || die "Cannot open $mailprog";
   print MAIL "Reply-to: $uname <$address>\n";
   print MAIL "From: $uname <$address>\n";
   print MAIL "To: $ent0\n";
   print MAIL "Subject:Subscription Pending.\n";
   if ($etype eq "on"){
   	print MAIL "Content-type: text\/html\n\n";
   	print MAIL "<HTML><HEAD><TITLE>$listname Subscription.</TITLE></HEAD><BODY BGCOLOR=\"#0099CC\">\n";
   	print MAIL "<div align=right><b>--------------------------------------------------<FONT SIZE=-1>$listname, $date </FONT></b></div>\n";
   	print MAIL " <center>The address <H3>$ent0</H3><b>has been submitted for subscription to</b><H3>$listname.</H3>\n";
   	print MAIL "<hr noshade>\n";
   	print MAIL "To confirm your subscription, please click\n"; 
   	print MAIL "<form method=POST action=\"$cgiurl\">\n";
   	print MAIL "<input type=\"hidden\" name=\"status\" value=\"addmail\">\n";
   	print MAIL "<input name=\"emailaddress\" type=\"hidden\" value=\"$ent0\">\n"; 
	print MAIL "<input type=\"hidden\" name=\"emailtype\" value=\"$etype\">\n"; 
	print MAIL "<input name=\" \" type=\"submit\" value=\"CONFIRM\"></form>\n"; 
   	print MAIL "	<b>You have NOT yet been subscribed to $listname<b>,<BR> if this mail was sent in error or you no longer wish to subscribe, DO NOTHING !\n";
   	print MAIL " For further information, please visit us at <H3><a href=\"http://$website\">$website</a></H3><br>\n";
   	print MAIL "<br>\n";
   	print MAIL "<b>Or <a href=\"mailto:$address\">write </a>to us <center><b></b><br>\n";
   	print MAIL "<hr noshade>\n";
   	print MAIL "<B><FONT SIZE=-1><center><a href=\"mailto:mailomatic\@mujik.com\">sent with Mailomatic</a></center><FONT></B>\n";   
   	print MAIL "</center></BODY>\n";
   	print MAIL "</HTML>\n";
   	}else{
	print MAIL "------------------------------------------------------------------------------------------\n";
	print MAIL "$listname $date\n";
	print MAIL "------------------------------------------------------------------------------------------\n";
	print MAIL "\n";
	print MAIL "  The address $ent0 has been submitted for subscription to $listname\n";
	print MAIL "  To confirm your subscription, please visit\n";
	print MAIL "  $cgiurl?status=addmail&emailaddress=$ent0&emailtype=no\n"; 
	print MAIL "  To re-apply online or simply reply to this mail with Remove in the subject field.\n"; 
	print MAIL "  You have NOT been subscribed to $listname yet,\n"; 
	print MAIL "  If this mail was sent in error or you no longer wish to subscribe, DO NOTHING !\n";
	print MAIL "  For further information, check out our website at:\n";
	print MAIL "  http://$website\n";
	print MAIL "  Or write to us at mailto:$address\n";
	print MAIL "\n";
	print MAIL "-------------------------------------------------------------------------------------------\n";
	print MAIL "                  this mail posted with Mailomatic\n";
}
close (MAIL);
}
#and print a 'success' notification to the user's browser

print "<HTML>\n";
print "<HEAD><TITLE>$listname Pending Subscriptions.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H3><FONT COLOR =\"#FFFFFF\">Welcome to $listname.</FONT></H3>\n";
print "<hr noshade>\n";
print<<"ch_pass";
<center><B><H3>Congratulations! - $ent0 is pending subscription to $listname.</H3><BR>
<H4>We have sent a mail to $ent0 with details on how to confirm subscription</H4></center><br>
<P><B>We apologise if this causes any inconvienience but we do this to maintain our mailist integrity.
Please let us <a href=\"mailto:$address\">know</a> if you do not receive the confirmation mail 
or experience any other difficulty with $listname.
<hr noshade><center><a href= \"$webform\">Click here</a> to return to our site</center></B>
</BODY>
</HTML>
ch_pass
exit;
}
##END send_check

######################## Sub exist ########################
#Short & Sweet - this throws an error screen and then exits if a user tries  
#to input an address we already have in the list .

sub exist{

print "<HTML>\n";
print "<HEAD><TITLE>$listname Exists Error</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H3><FONT COLOR =\"#FFFFFF\"> $eaddress already exists in $listname.</FONT></H3>\n";
print "<hr noshade>\n";
print "<b><a href= \"$webform\">Return</a></b>\n";
exit;
}
##END exist noexist


##################### sub add_email #######################
#Like the man says, it adds an email to the list - in response to the
#confirm email we just sent. It also sends a final 'success' email to the
#user which also includes the specific 'remove' command for that address

sub add_email{
 
my $ent2=$FORM{'emailaddress'};
my $atype=$FORM{'emailtype'};
    
    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";

#open and check the email list for duplicate entries. We already checked before 
#sending the confirm email but there is a time lapse between the two. Just to
#make sure...
open (AEMA, $mailfile) || die "Cannot open $mailfile";
@wx = <AEMA>;
close (AEMA);
$bound=scalar(@wx);
$match=0;

for ($i=0;$i<=$bound;$i++) {
     $_=$wx[$i];
     s/\012//;
     $wx[$i]=$_;

  if ($ent2 eq $wx[$i]) {
     $match=1;
    }
}
if ( $match eq 1 ) {
     &exist; 
  }
  else {
#No duplicates exist, so we write the new address to file
open (PASS, ">>$mailfile") || die "Cannot open $mailfile";
  print PASS "$ent2\n";
  close (PASS);
  
#send a "Success" confirmation to the address just added
&send_confirm($ent2,$atype);

#and print a 'success' notification to the user's browser

print "<HTML>\n";
print "<HEAD><TITLE>$listname Subscription Success.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H3><FONT COLOR =\"#FFFFFF\">Added $ent2 to $listname.</FONT></H3>\n";
print "<hr noshade>\n";
print<<"ch_pass";
<center><B>Congratulations! - $ent2 is subscribed to $listname.<BR><center>
<P>We have sent a confirmation notice to that the above address was 
successfully logged to our database<br></center>
This mail also includes important details on how to cancel your subscription,if that becomes necessary.</B>
Please let us <a href=\"mailto:$address\">know</a> if you do not receive the
confirmation mail or experience any other difficulty with this mail program.
<p>
<hr noshade>
<a href= \"http://$website\">Click here</a> to visit our site</b>
</BODY>
</HTML>
ch_pass
}exit;
}
##END add_email

################### SUB del_mail ####################
#Again, the name is self-explanatory. The removal is in response to the
#"delete" string that was in the automatically generated email sent when 
#they first joined the list.
#all other deletions MUST be done through the admin panel.

sub del_mail{
my $ent3=$FORM{'emailaddress'};
my $dtype=$FORM{'emailtype'};
    
    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";
    open (RADD, $mailfile) || die "Cannot open $mailfile";
    @wx = <RADD>;
    close (RADD);
    $bound=scalar(@wx);
    $match=0;
    for ($i=0;$i<=$bound;$i++) {
    	$_=$wx[$i];
    	s/\012//;
    	$wx[$i]=$_;
    	if ($ent3 eq $wx[$i]) {
    		$match=1;
    	}
}   
if ( $match eq 0 ) {
	&noexist;
}else{
open (RID, $mailfile) || die "Cannot open $mailfile";
open (RCD,">jobx")|| die "Cannot open work file";    
while ($line = <RID>) {
	if ($line =~s/.*$ent3.*\n/$1/){
	}print RCD $line;
}
close (RCD);
close (RID);

open (FIL, "jobx");
open (RYD,">$mailfile");
@ln = <FIL>;
print RYD @ln;
         
close (RYD); 
close (FIL);
# cleanup work.
unlink ("jobx");

#send a "delete" confirmation to the deleted address
&send_delete($ent3,$dtype);
print "<HTML>\n";
print "<HEAD><TITLE>$listname Deletion Success.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H3>Removed $ent3 from $listname.</H3>\n";
print "<hr noshade>\n";
print "<center><H3><a href=\"http://$website\">$website</a></H3></center>\n";
print "<hr noshade>\n";
print "</BODY>\n";
print "</HTML>\n";
}
exit;
}

##END del_mail

###################### END USER SUBS ######################
###########################################################
################### Admin Sub Routines - Restricted Access ####################
###############################################################################

################### SUB admin_function ####################
##Brings up the main admin screen

sub admin_function{

    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";
 
#set up the sort variables
# The $sort function is used to order your lists in alphabetical order
# Most of this is from the NPH-MAILIST pl mentioned in the header. 
# Normal unix "sort" extentions will work here, if you are feeling creative :)
# In this case, the -f makes the sort case insensitive.
my $scommand="$sort -f $mailfile";
my @sorted_ple="";
my $lines="";
my $dex=0;

# Opens and reads the address  file.
open (FILE, $mailfile) || die "Cannot open $mailfile";
@ple = <FILE>;
close (FILE);

$dex=0;
open(SORT,"$scommand |")|| die "Cannot exec $scommand\n";
while ( $lines=<SORT>) 
                      { $sorted_ple[$dex]=$lines;
                        $dex++;}
close(SORT);
#                       
open (FILEX, ">$mailfile") || die "Cannot write to $mailfile";
print FILEX @sorted_ple;
close (FILEX);

#Now we start building the admin screen

print "<HTML>\n";
print "<HEAD><TITLE>$listname Administration.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H3><center><FONT COLOR =\"#FFFFFF\">$listname Administration.</FONT></center></H3>\n";

#see what list we want to edit - default is text
if($etype eq "on"){
&print_html;
print "<H3>Current HTML Address list</H3><b>\n";
}else{
&print_text;
print "<H3>Current Text Address list</H3><b>\n";
}
#Print out all the addresses in our list
open (FILEIN, $mailfile) || die "Cannot open $mailfile";
@pll = <FILEIN>;
close (FILEIN);

foreach $ix (0..$#ple) {
  print "$pll[$ix] <br>";
  ($i = 1+$ix);
  }

print "</b>\n";
print "<p><hr noshade><b>There are - <u>$i</u> - addresses in the list.<BR></b>\n";
print "<B>Please check the list for junk addresses before sending updates.</B>\n";
print "<hr noshade>\n";
print "</BODY>\n";
print "</HTML>\n";
exit;
}
#####END admin_function

###################### sub mom_add ######################
#This bit lets you add an address from the admin panel. Unlike the user
#routine, this writes direct to the database and allows you to switch 
#off the automatically generated 'confirmation' mail to the added address
sub mom_add{
my $ent1 = $FORM{'emailaddress'};
my $mtype=$FORM{'emailtype'};
my $mamail=$FORM{'addconfirm'};
if ($mtype eq "on"){$mailfile = $htmlfile;}
else{$mailfile = $textfile;}

    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";

#open and check the email list for duplicate entries. We already checked before 
#sending the confirm email but there is a time lapse between the two. Just to
#make sure...
open (MAMA, $mailfile) || die "Cannot open $mailfile";
@wx = <MAMA>;
close (MAMA);
$bound=scalar(@wx);
$match=0;

for ($i=0;$i<=$bound;$i++) {
     $_=$wx[$i];
     s/\012//;
     $wx[$i]=$_;

  if ($ent1 eq $wx[$i]) {
     $match=1;
    }
}
if ( $match eq 1 ) {
     &exist; 
  }
  else {
#No duplicates exist, so we write the new address to file
open (MAOK, ">>$mailfile") || die "Cannot open $mailfile";
  print MAOK "$ent1\n";
  close (MAOK);

  
#and print a 'success' notification to the user's browser

print "<HTML>\n";
print "<HEAD><TITLE>$listname Subscription Success.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<H4><FONT COLOR =\"#FFFFFF\">Added $ent1 to $listname.(Admin Mode)</FONT></H4>\n";
print "<hr noshade>\n";
#check if we want to send a "Success" confirmation - by default, we dont send
if($mamail eq "on"){
&send_confirm($ent1,$mtype);
print<<"ch_pass";
<center><H3>Success ! - $ent1 was subscribed to $listname<BR>(Admin Mode)</H3><BR>
<H3>A confirmation notice was sent to the above address.</H3></center><hr noshade>
<center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value=$mtype>
<input name="emailaddress" type="hidden" value=$admin>
<input name=" " type="submit" value="Return to Admin"><form></center>
<hr noshade><a href= \"$website\">To the Website</a></b>
</BODY>
</HTML>
ch_pass
exit;
}else{
print<<"ch_nomail";
<center><H3>Success ! - $ent1 was subscribed to $listname <BR>(Admin mode).</H3><BR>
<H3>NO confirmation notice was sent to the above address.</H3></center><br>
<B><p>The confirmation notice contains important details on how to cancel &listname 
subscriptions. so $ent1 now has no way of knowing the auto deletion \"string\" 
for this address<BR>
<p>To leave $listname will need to contact an administrator at $address for manual deletion.
If this is not what you intended, you will need to return to the Admin Screen, delete $ent1 
and re-add to the list with the "Mail" option selected.</B>
<center><form method=POST action=\"$cgiurl\">
<input type=\"hidden\" name=\"emailtype\" value=$mtype>
<input name=\"emailaddress\" type=\"hidden\" value=$admin>
<input name=\" \" type=\"submit\" value=\"Return to Admin\"></form></center>
<hr noshade><a href= \"$website\">To the Website</a></b>
</BODY>
</HTML>
ch_nomail
exit;
}
}
}
##END mom_add

#################### SUB mom_del #####################
#This allows the administrator to delete addresses

sub mom_del {
my $ent8=$FORM{'emailaddress'};
my $mdtype=$FORM{'emailtype'};
my $mdmail=$FORM{'delconfirm'};
#if ($mdtype eq "on"){$mailfile = $htmlfile;}
#else{$mailfile = $textfile;}

    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";
    open (MDMD, $mailfile) || die "Cannot open $mailfile";
    @wx = <MDMD>;
    close (MDMD);
    $bound=scalar(@wx);
    $match=0;
    for ($i=0;$i<=$bound;$i++) {
    	$_=$wx[$i];
    	s/\012//;
    	$wx[$i]=$_;
    	if ($ent8 eq $wx[$i]) {
    		$match=1;
    	}
}   
if ( $match eq 0 ) {
	&noexist;
}else{
open (RID, $mailfile) || die "Cannot open $mailfile";
open (RCD,">jobx")|| die "Cannot open work file";    
while ($line = <RID>) {
	if ($line =~s/.*$ent8.*\n/$1/){
	}print RCD $line;
}
close (RCD);
close (RID);

open (FIL, "jobx");
open (RYD,">$mailfile");
@ln = <FIL>;
print RYD @ln;
close (RYD); 
close (FIL);

# cleanup work.
unlink ("jobx");

#start writing the browser notification
print "<HTML>\n";
print "<HEAD><TITLE>$listname Deletion Success.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<b>Removed $ent8 from $listname (Admin Mode).</b>\n";

#check if we want to send a "deletion" notice
if($mdmail eq "on"){
&send_delete($ent8,$mdtype);
print<<"hide_admin";
<hr noshade><H3><center>$ent8 was deleted from $listname (Admin Mode)<BR>
<br>and a Deletion Notice was sent.</H3>
<center><form method=POST action=\"$cgiurl\">
<input type=\"hidden\" name=\"emailtype\" value=$mdtype>
<input name=\"emailaddress\" type=\"hidden\" value=$admin>
<input name=\" \" type=\"submit\" value=\"Return to Admin\"></form></center>
<hr noshade><a href= \"$website\">To the Website</a></b><hr noshade>
</BODY>
</HTML>
hide_admin
exit;
}else{
print<<"hide_tadmin";
<hr noshade><H3><center>$ent8 was deleted from $listname (Admin Mode)
<br>NO Deletion Notice was sent to $ent8.</H3><BR>
<H3>($ent8 will NOT BE AWARE they have been deleted)</H3><br>
<center><form method=POST action=\"$cgiurl\">
<input type=\"hidden\" name=\"emailtype\" value=$mdtype>
<input name=\"emailaddress\" type=\"hidden\" value=$admin>
<input name=\" \" type=\"submit\" value=\"Return to Admin\"></form></center>
<hr noshade><a href= \"$website\">To the Website</a></b><hr noshade>
</BODY>
</HTML>
hide_tadmin
}
}exit;
}

##END mom_del

####################### SUB noexist #######################
##Error screen
sub noexist{

print "<HTML>\n";
print "<HEAD><TITLE>$listname No User Error.</TITLE></HEAD>\n";
print "<BODY BGCOLOR=\"#99CCFF\">\n\n";
print "<CENTER><H3>Sorry, $eaddress does not exist in $listname.</H3>\n";
print "<hr noshade>\n";
print "<b>To subscribe, click";
print "<form method=POST action=\"$cgiurl\">\n";
print "<input type=\"hidden\" name=\"status\" value=\"addmail\">\n";
print "<input name=\"emailaddress\" type=\"hidden\" value=\"$eaddress\">\n"; 
print "<input type=\"hidden\" name=\"emailtype\" value=\"$etype\">\n"; 
print "<input name=\" \" type=\"submit\" value=\"Subscribe\"></form>\n"; 
print "</b><hr noshade></CENTER>\n";
print "</BODY>\n";
print "</HTML>\n";
exit;
}
##END noexist
###################### SUB mom_send #######################
##This controls the 'send' function of the administration panel and prints
#out a confirmation screen
 
sub mom_send{ 

#Call the email list engine sub-routine
&momengine($etype);

#Printout a html page and exit 
    
    print "Server: $server_software \n";
    print "Content-type: text/html\n\n";

print<<"mail_input";
<HTML><BODY BGCOLOR=\"#99CCFF\">
<HEAD><TITLE>$listname Post Is Sent.</TITLE></HEAD>
<center><H3><font color=\"#ffffff\">$listname Post is successfully underway!.</font>
<hr noshade>Use the back button on your browser or </H3><br> 
<form method=POST action=\"$cgiurl\">
<input type=\"hidden\" name=\"emailtype\" value=$etype>
<input name=\"emailaddress\" type=\"hidden\" value=$admin>
<input name=\" \" type=\"submit\" value=\"Return to Admin\"></form></center><br><hr noshade>
</BODY>
</HTML>
mail_input
exit;
}
##END mom_send

##################### SUB edit_notice #####################

sub edit_notice {
	
	print "Server: $server_software","\n";
	print "Content-type: text/html\n\n";
	print "<HTML>\n";
	print "<HEAD><TITLE>$listname Update Success.</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=\"#99CCFF\">\n";
@LINES = $FORM{'body'};
$SIZE=@LINES;
open (RED,">$notice") || die "Cannot open $notice";
for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   print RED $_;
}
close (RED);
if($etype eq "on"){
print "<H3><center><font color=\"#ffffff\">Mail-O-Matic HTML Mail Editor.</font></center></H3>\n";
}else{
print "<H3><center><font color=\"#ffffff\">Mail-O-Matic Text Mail Editor.</font></center></H3>\n";
}
print "<H3>Your Update was successful !</H3>\n";
print "<center><form method=POST action=\"$cgiurl\">\n";
print "<textarea name=\"body\" COLS=80 ROWS=20 wrap=physical>@LINES</textarea><br>\n";
print "<input type=\"hidden\" name=\"emailtype\" value=$etype>";
print "<INPUT name=\" \" type=\"submit\" value=\"Submit Change\">"; 
print "<INPUT name=\" \" type=\"reset\" value=\"Undo Changes\"></form>";
print "<form method=POST action=\"$cgiurl\">";
print "<input type=\"hidden\" name=\"emailaddress\" value=$admin>";
print "<input name=\"emailtype\" type=\"hidden\" value=&etype>";
print "<input name=\" \" type=\"submit\" value=\"Return to Admin\"></form></center>\n";
print "<hr noshade>\n";
print "</BODY>\n";
print "</HTML>\n";
exit;
}
###END edit_notice 

###################### SUB mom_edit #######################
#This is the screen you get the first time you go to edit in the
#admin panel
sub mom_edit {
	
	
	print "Server: $server_software","\n";
	print "Content-type: text/html\n\n";
	print "<HTML>\n";
	print "<HEAD><TITLE>$listname Mail Editor.</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=\"#99CCFF\">\n";
if($etype eq "on"){
	print "<H3><center><font color=\"#ffffff\">Mail-O-Matic HTML Mail Editor.</font></center></H3>\n";
}else{
print "<H3><center><font color=\"#ffffff\">Mail-O-Matic Text Mail Editor.</font></center></H3>\n";
}
print "<center><form method=POST action=\"$cgiurl\">\n";
print "<textarea name=\"body\" COLS=80 ROWS=20 wrap=physical>";
open (ROD, $notice) || die "Cannot open $notice";
@LINES = <ROD>;
$SIZE=@LINES;
for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   print "$_";
}
close (ROD);
print "</textarea>\n";
print "<p>\n";
print "<input type=\"hidden\" name=\"emailtype\" value=$etype>";
print "<INPUT name=\" \" type=\"submit\" value=\"Submit Change\"> 
<INPUT name=\"\" type=\"reset\" value=\"Undo Changes\">\n";
print "</form></center>\n";
print "<b><FONT SIZE=4>";
print "<center><form method=POST action=\"$cgiurl\">";
print "<input type=\"hidden\" name=\"emailtype\" value=$etype>";
print "<input name=\"emailaddress\" type=\"hidden\" value=$admin>";
print "<input name=\" \" type=\"submit\" value=\"Return to Admin\"><form></center>\n";
print "<hr noshade><a href= \"$website\">To the Website</a>";
print "</BODY>\n"; 
print "</HTML>\n";
exit; 
}
###END mom_edit

##################### SUB momengine #######################
#The 'send' engine for the list - sends all list mail

sub momengine{


open (MENOT, $notice);
@notis = <MENOT>;
close (MENOT);


open (MEMA, $mailfile) || die "Cannot open $mailfile";
@mailf = <MEMA>;
close (MEMA);

foreach $SIZE (@mailf) {
        $SIZE =~ s/\n//g;
open (MAIL, "|$mailprog $SIZE") || die "Cannot open $mailprog";
   print MAIL "Reply-to: $uname <$address>\n";
   print MAIL "From: $uname <$address>\n";
   print MAIL "To: $SIZE\n";
   print MAIL "Subject:$listname Update.\n";
   if ($etype eq "on"){
   print MAIL "Content-type: text\/html\n\n";
       }
   print MAIL "------------------------------------------------------\n";
   print MAIL "$date\n";
   print MAIL "@notis\n";
   print MAIL "------------------------------------------------------\n";

close (MAIL);
 }
}

##################### SUB print_text ######################
#Prints out the html for the admin (html) screen
sub print_text{

print<<"text_input";
<table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=4 WIDTH="100%" >
<tr VALIGN=CENTER><td VALIGN=TOP COLSPAN=\"2\">
<center><b><font size=+1>To remove an address from the list</font></b>
<br><form method=POST action=\"$cgiurl\">
<input type="hidden" name="status" value="momdel">
<input type="radio" name="delconfirm">Mail<option value="on">
<input type=text name="emailaddress" size=25><BR>
<input type="hidden" name="emailtype" value="no">
<input name="" type="reset" value="Clear">
<input name="" type="submit" value="DELETE USER"></form>
<FONT SIZE=-1><B>Check \"Mail\" if you want to send a delete notice</B></FONT><hr noshade></center></td>
<td COLSPAN="2">
<center><b><font size=+1>To add an address to the list</font></b>
<br><form method=POST action=\"$cgiurl\">
<input type="radio" name="addconfirm">Mail<option value="on">
<input type="hidden" name="status" value="momadd">
<input type=text name="emailaddress" size=25><BR>
<input type="hidden" name="emailtype" value="no">
<input name="" type="reset" value="Clear">
<input name="" type="submit" value="ADD USER">
</form><FONT SIZE=-1><B>Check \"Mail\" if you want to send a confirmation</B></FONT><hr noshade></center></td></tr>
<tr VALIGN=CENTER><td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="no">
<input type="hidden"  name="status" value="momedit">
<input name=" " type="submit" value="EDIT Update">
</form></center></td>
<td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="no">
<input name="status" type="hidden" value="momsend">
<input name=" " type="submit" value="SEND Update"></form></center></td>
<td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="on">
<input name="emailaddress" type="hidden" value=$admin>
<input name=" " type="submit" value="HTML Mode"></form></center></td>
<td><center><b><font size=+1><a href=\"http://$website\">Back to the site</a>
</font></b></center></td></tr></table><hr noshade>
text_input
}

##END print_text

##################### SUB print_html ######################
#Prints out the html for the admin (html) screen
sub print_html{

print<<"html_input";
<table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=4 WIDTH="100%" >
<tr VALIGN=CENTER><td VALIGN=TOP COLSPAN=\"2\">
<center><b><font size=+1>To remove an address from the list</font></b>
<br><form method=POST action=\"$cgiurl\">
<input type="hidden" name="status" value="momdel">
<input type="radio" name="delconfirm">Mail<option value="on">
<input type=text name="emailaddress" size=25><BR>
<input type="hidden" name="emailtype" value="on">
<input name="" type="reset" value="Clear">
<input name="" type="submit" value="DELETE USER"></form>
<FONT SIZE=-1><B>Check \"Mail\" if you want to send a delete notice</B></FONT><hr noshade></center></td>
<td COLSPAN="2">
<center><b><font size=+1>To add an address to the list</font></b>
<br><form method=POST action=\"$cgiurl\">
<input type="radio" name="addconfirm">Mail<option value="on">
<input type="hidden" name="status" value="momadd">
<input type=text name="emailaddress" size=25><BR>
<input type="hidden" name="emailtype" value="on">
<input name="" type="reset" value="Clear">
<input name="" type="submit" value="ADD USER">
</form><FONT SIZE=-1><B>Check \"Mail\" if you want to send a confirmation</B></FONT><hr noshade></center></td></tr>
<tr VALIGN=CENTER><td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="on">
<input type="hidden"  name="status" value="momedit">
<input name=" " type="submit" value="EDIT Update">
</form></center></td>
<td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="on">
<input name="status" type="hidden" value="momsend">
<input name=" " type="submit" value="SEND Update"></form></center></td>
<td VALIGN=CENTER><center><form method=POST action=\"$cgiurl\">
<input type="hidden" name="emailtype" value="no">
<input name="emailaddress" type="hidden" value=$admin>
<input name=" " type="submit" value="TEXT Mode"></form></center></td>
<td><center><b><font size=+1><a href=\"http://$website\">Back to the site</a>
</font></b></center></td></tr></table><hr noshade>
html_input
}

##END print_html

#################### Shared Sub Routines - Public Access ######################
###############################################################################

#################### SUB send_confirm #####################
#This is called when a new email address is first entered - the confirmation email
#which includes instuctions on how users can fully subscribe to the list
#This is always sent with user inputted new addresses but can be 'toggled' off 
#and on in administrator mode

sub send_confirm{ 

my ($ent7, $stype) = @_;


open (MAIL, "|$mailprog $ent7") || die "Cannot open $mailprog";
   print MAIL "Reply-to: $uname <$address>\n";
   print MAIL "From: $uname <$address>\n";
   print MAIL "To: $ent7\n";
   print MAIL "Subject:Subscription Confirmation.\n";
   if ($stype eq "on"){
   print MAIL "Content-type: text\/html\n\n";
   print MAIL "<HTML><HEAD><TITLE>$listname Confirmation.</TITLE></HEAD><BODY BGCOLOR=\"#0099CC\">\n";
   print MAIL "<div align=right><b>--------------------------------------------------<FONT SIZE=-1>$listname, $date </FONT></b></div>\n";
   print MAIL "<br>\n";
   print MAIL "<center>The address<H3><BR>$ent7<BR></H3> has been subscribed to <br><H3>$listname</H3>.\n"; 
   print MAIL "<hr noshade>\n";
   print MAIL "If you did not request this service or wish to cancel your subscription at any time,click<br>\n";
   print MAIL "<form method=POST action=\"$cgiurl\">\n";
   print MAIL "<input type=\"hidden\" name=\"status\" value=\"delmail\">\n";
   print MAIL "<input name=\"emailaddress\" type=\"hidden\" value=\"$ent7\">\n";
   print MAIL "<input type=\"hidden\" name=\"emailtype\" value=\"$stype\">\n";
   print MAIL "<input name=\" \" type=\"submit\" value=\"CANCEL\"></form>\n"; 
   print MAIL "Or visit our website at <H3><b><a href=\"http://$website\">$website</a></b></H3><br>\n";
   print MAIL "If you have any questions regarding the <b>$listname</b>, please <a href=\"mailto:$address\">write </a>to us - we really want to know ! <br>\n";
   print MAIL "<br>Thank you for your support.\n";
   print MAIL "from <b>$listname Staff</b></center>\n";
   print MAIL "<hr noshade>\n";
   print MAIL "<B><FONT SIZE=-1><center><a href=\"mailto:mailomatic\@mujik.com\">sent with Mailomatic</a></center><FONT></B>\n";
   print MAIL "</BODY>\n";
   print MAIL "</HTML>\n";
}else{
print MAIL "------------------------------------------------------------------------------------------\n";
print MAIL "$listname $date\n";
print MAIL "\n";
print MAIL "  The address $ent7 has been subscribed to $listname.\n";
print MAIL "  If you did not request this service or wish to cancel your subscription at any time, go to \n";
print MAIL "$cgiurl?status=delmail&emailaddress=$ent7&emailtype=no\n"; 
print MAIL "or simply reply to this mail with Remove in the subject field.\n";
print MAIL "\n";
print MAIL "  For further information, check out our website at\n";
print MAIL "http://$website or write to us at \n";
print MAIL "mailto:$address\n";
print MAIL "  Thanks for your support.\n";
print MAIL "			-from $listname Staff-\n";
print MAIL "------------------------------------------------------------------------------------------\n";
print MAIL "                           sent with Mailomatic\n";
}
close (MAIL);
}

##END send_confirm

#################### SUB send_delete ######################
# The above's twin - the 'confirm delete' email that lets a user know they have been
#unsubscribed. Can be toggled off and on in admin mode. 

sub send_delete{ 
my ($ent5, $sdtype) = @_;

#check the sort of mail we want to send - html or text
open (MAIL, "|$mailprog $ent5") || die "Cannot open $mailprog";
print MAIL "Reply-to: $uname <$address>\n";
print MAIL "From: $uname <$address>\n";
print MAIL "To: $ent5\n";
print MAIL "Subject:Deletion Notice.\n";
if ($sdtype eq "on"){
print MAIL "Content-type: text\/html\n\n";
print MAIL "<HTML><HEAD><TITLE>$listname Cancellation.</TITLE></HEAD><BODY BGCOLOR=\"#99CCFF\">\n";
print MAIL "<div align=right><b>--------------------------------------------------<FONT SIZE=-1>$listname, $date </FONT></b></div>\n";
print MAIL "<center>The address <H3>$ent5</H3> has been <b>deleted</b> from <H3>$listname.</H3>\n";
print MAIL "<hr noshade>\n";
print MAIL "	If you did not request deletion and wish to renew your subscription,click\n";
print MAIL "<form method=POST action=\"$cgiurl\">\n";
print MAIL "<input type=\"hidden\" name=\"status\" value=\"addmail\">\n";
print MAIL "<input type=\"hidden\" name=\"emailtype\" value=\"$sdtype\">\n";
print MAIL "<input name=\"emailaddress\" type=\"hidden\" value=\"$ent5\">\n";
print MAIL "<input name=\" \" type=\"submit\" value=\"RENEW\"></form>\n"; 
print MAIL "</b><br>\n";
print MAIL "	If you have had a problem with <b>$listname<b>, please <b><a href=\"mailto:$address\">write </a>to us\n";
print MAIL "<b>- we really want to know !</b><br>\n";
print MAIL "Thanks for your past support - please visit us again at <H3><b><a href=\"http://$website\">$website</a></b></H3><br>\n";
print MAIL "<br>\n";
print MAIL "<FONT SIZE=-1><b>- $listname Staff -</b></FONT></center>\n";
print MAIL "<hr noshade>\n";
print MAIL "<B><FONT SIZE=-2><center><a href=\"mailto:mailomatic\@mujik.com\">sent with Mailomatic</a></center><FONT></B>\n";   
print MAIL "</BODY>\n";
print MAIL "</HTML>\n";
}else{
print MAIL "-----------------------------------------------------------------------------------------\n";
print MAIL "$listname $date\n";
print MAIL "\n";
print MAIL "  The address $ent5 has been deleted from $listname\n";
print MAIL "  If you did not request deletion and wish to renew your subscription,\n"; 
print MAIL "please follow the following link  : \n";
print MAIL "$cgiurl?status=addmail&emailaddress=$ent5&emailtype=$sdtype\n";
print MAIL "to re-apply online, \n"; 
print MAIL "Or simply reply to this mail with Remove in the subject field.";
print MAIL "  If you have had any problems with $listname,"; 
print MAIL "please write to us at:";
print MAIL " mailto:$address - we really want to know !\n";
print MAIL "\n";
print MAIL "  Thanks for your past support - please visit us again soon\n";
print MAIL "		- at http://$website -\n";
print MAIL "------------------------------------------------------------------------------------------\n";
print MAIL "                           sent with Mailomatic\n";
}
close (MAIL);
}

#####END send_delete

###################### SUB set_type #######################
sub set_type{
my $stype=@_;
#We need to setup the lists according to the configuration details at the head
#of this script. Default is text mode. To change the user default to html, I would suggest
#setting it up in the input forms, not here. See the Mailomatic faq for more details

if( $stype eq "on"){
	$mailfile=$htmlfile;
	$notice=$htmlnotice;
	$uname=$htmluname;
	$address=$htmladdress;
	$listname=$htmllistname;
}
else{
	$mailfile=$textfile;
	$notice=$textnotice;
	$uname=$textuname;
	$address=$textaddress;
	$listname=$textlistname;
}

}
###################bad_email####################
sub bad_email{
    
    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";

print<<"bad_input";
<HTML>
<HEAD>
<TITLE>$listname Bad Email.</TITLE>
</HEAD>
<BODY BGCOLOR=\"#99CCFF\">
<H3>Sorry your input was not valid.</H3>
<hr noshade>
Use the back button on your browser or <a href=\"$webform\">
<B>click here </B></a>to re-enter your <B>email address</B><br>
Thank you for your support. 
<hr noshade>
</BODY>
</HTML>
bad_input
exit;
}

###################no_input####################

sub no_input {

    print "Server: $server_software","\n";
    print "Content-type: text/html\n\n";
    print<<"no_input";
<HTML>
<HEAD><TITLE>$listname No Input Notice</TITLE></HEAD>
<BODY BGCOLOR=\"#99CCFF\">
<center><H3>Your input was not recognized or left blank.</H3><br>
<hr noshade><BR><B>Please use your back button to re-enter your email address.</B><br>
Thank you for your support<br><hr noshade></center>
</BODY>
</HTML>
no_input
exit;
}

######END OF FILE######