# FILE: src-board-subs-5
# Board e-mailing features
#-------------------------------------------------------------------------------
# DISCUS VERSION 3.10 COPYRIGHT NOTICE
#
# Discus 3.10 is copyright (c) 2000 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

#---SEPARATOR---#
#REQ:send_email_message

sub email_configuration {
	if ($FORM{'net_SMTP'} == 0) {
		$mail_prog = $FORM{'mail_program'};
		if ($mail_prog =~ m|[;"'\[\(\)\|\\`\*<>!]|) {
			&error_message("Invalid Mail Program", "Mail program cannot contain metacharacters.  If you're using a Windows server, write your backslashes (\\) as forward slashes (/) instead.", 0, 1);
		} elsif (!-e $mail_prog) {
			&error_message("Invalid Mail Program", "Mail program does not exist!  The path that you entered for your mail program is not valid.", 0, 1);
		} elsif (!-x $mail_prog) {
			&error_message("Invalid Mail Program", "Mail program is not executable!", 0, 1);
		}
		$command_line = "| $mail_prog "; $input_stream = "";
		if ($FORM{'chuckaddress'} !~ m|^([\w\-\+\.]+)\@([\w\-\+\.]+)$| && $FORM{'1message'} == 1) {
			&error_message("Invalid E-mail Address", "The To: address is invalid");
		}
		if ($FORM{'commandline'}) {
			$FORM{'commandline'} =~ s/[^\w\-\$\/\>\<\&\s\.]//g;
			$FORM{'commandline'} =~ s/\.\.//g;
			$command_line .= $FORM{'commandline'};
		}
		if ($FORM{'toaddress'} == 2) {
			&error_message("Invalid To Address", "The \"To\" address must be specified either in the input stream or on the command line");
		} elsif ($FORM{'toaddress'} == 1) {
			$FORM{'toaddress_switch'} =~ s/[^\w\-]//g;
			$command_line .= " $FORM{'toaddress_switch'} \$TO" if ($FORM{'1message'} == 0 || $FORM{'1message'} == 2);
			$command_line .= " $FORM{'toaddress_switch'} $FORM{'chuckaddress'}" if $FORM{'1message'} == 1;
		} elsif ($FORM{'toaddress'} == 0) {
			$input_stream .= "$FORM{'toaddress_prefix'} \$TO\n" if ($FORM{'1message'} == 0 || $FORM{'1message'} == 2);
			$input_stream .= "$FORM{'toaddress_prefix'} $FORM{'chuckaddress'}\n" if $FORM{'1message'} == 1;
		}
		if ($FORM{'fromaddress'} == 1) {
			$FORM{'fromaddress_switch'} =~ s/[^\w\-]//g;
			$FROM = $FORM{'fromname'}; $FROM =~ s/[^\w\s\-\+\.\@]//g;
			$command_line .= " $FORM{'fromaddress_switch'} '$FROM'" if $FORM{'lineformat'} == 0;
			$command_line .= " '$FORM{'fromaddress_switch'}$FROM'" if $FORM{'lineformat'} == 1;
		} elsif ($FORM{'fromaddress'} == 0) {
			$FROM = "\"$FORM{'fromname'}\" <$FORM{'fromaddr'}>";
			$input_stream .= "$FORM{'fromaddress_prefix'} $FROM\n";
		}
		if ($FORM{'useraddress'} == 1) {
			$FORM{'useraddress_switch'} =~ s/[^\w\-]//g;
			$FROM = $FORM{'fromaddr'}; $FROM =~ s/[^\w\s\-\+\.\@<>]//g;
			$command_line .= " $FORM{'useraddress_switch'} '$FROM'" if $FORM{'lineformat'} == 0;
			$command_line .= " '$FORM{'useraddress_switch'}$FROM'" if $FORM{'lineformat'} == 1;
		} elsif ($FORM{'useraddress'} == 0) {
			$FROM = "$FORM{'fromaddr'}";
			$input_stream .= "$FORM{'useraddress_prefix'} $FROM\n";
		}
		if ($FORM{'subjectaddress'} == 1) {
			$FORM{'subjectaddress_switch'} =~ s/[^\w\-]//g;
			$command_line .= " $FORM{'subjectaddress_switch'} '\$SUBJECT'" if $FORM{'lineformat'} == 0;
			$command_line .= " '$FORM{'subjectaddress_switch'}\$SUBJECT'" if $FORM{'lineformat'} == 1;

		} elsif ($FORM{'subjectaddress'} == 0) {
			$input_stream .= "$FORM{'subjectaddress_prefix'} \$SUBJECT\n";
		}
		if ($FORM{'bccaddress'} == 1) {
			$FORM{'bccaddress_switch'} =~ s/[^\w\-]//g;
			$command_line .= " $FORM{'bccaddress_switch'} '\$BCC'" if ($FORM{'1message'} == 1 && $FORM{'lineformat'} == 0);
			$command_line .= " '$FORM{'bccaddress_switch'}\$BCC'" if ($FORM{'1message'} == 1 && $FORM{'lineformat'} == 1);

		} elsif ($FORM{'bccaddress'} == 0) {
			$input_stream .= "$FORM{'bccaddress_prefix'} \$BCC\n" if $FORM{'1message'} == 1;
		}
		if ($FORM{'replytoaddress'} == 1) {
			if ($FORM{'fromaddr'} =~ m|^([\w\-\+\.]+)\@([\w\-\+\.]+)$|) {
				$FORM{'replytoaddress_switch'} =~ s/[^\w\-]//g;
				$command_line .= " $FORM{'replytoaddress_switch'} '$FORM{fromaddr}'" if $FORM{'lineformat'} == 0;
				$command_line .= " '$FORM{'replytoaddress_switch'}$FORM{fromaddr}'" if $FORM{'lineformat'} == 1;
			}
		} elsif ($FORM{'replytoaddress'} == 0) {
			if ($FORM{'fromaddr'} =~ m|^([\w\-\+\.]+)\@([\w\-\+\.]+)$|) {
				$input_stream .= "$FORM{'replytoaddress_prefix'} $FORM{fromaddr}\n";
			}
		}
		if ($FORM{'commandlineend'} ne "") {
			$FORM{'commandlineend'} =~ s/[^\w\-\$\/\>\<\&\s\.]//g;
			$FORM{'commandlineend'} =~ s/\.\.//g;
			$command_line .= " $FORM{'commandlineend'}";
		}
	} elsif ($FORM{'net_SMTP'} == 1) {
		if ($FORM{'SMTP'} !~ m|^([\w\-\+\.]+)$|) {
			&error_message("Invalid SMTP server", "The SMTP server specified does not have a valid name.", 0, 1);
		}
	} elsif ($FORM{'net_SMTP'} == 2) {
		if ($FORM{'SMTP2'} !~ m|^([\w\-\+\.\:]+)$|) {
			&error_message("Invalid SMTP server", "The SMTP server specified [$FORM{'SMTP2'}]does not have a valid name.", 0, 1);
		}
	}
	if ($FORM{'submit'} ne "Test") {
		&lock("email_configuration", "$admin_dir/email.txt");
		open (EMAIL, ">$admin_dir/email.txt");
		print EMAIL "# E-mail configuration file for Discus\n";
		print EMAIL "#\n";
		print EMAIL "# command_line is what is used for the program's command\n";
		print EMAIL "# line to send the e-mail message.\n";
		print EMAIL "# \$TO           'To' address(es)\n";
		print EMAIL "# \$FROM         'From' address\n";
		print EMAIL "# \$SUBJECT      Subject line\n";
		print EMAIL "# \$BCC          BCC address(es)\n";
		print EMAIL "# \$TEMPFILE     Full path to temporary file (if used)\n";
		print EMAIL "#\n";
		print EMAIL "command_line=$command_line\n";
		print EMAIL "#\n";
		print EMAIL "# input_stream is what's put in the mail program's input\n";
		print EMAIL "# stream.  This is written in 'escaped' format so you will\n";
		print EMAIL "# need to 'escape' any changes you make as well.\n";
		print EMAIL "input_stream=", &escape($input_stream), "\n";
		print EMAIL "# The remaining information is saved for your use in the e-mail\n";
		print EMAIL "# configurator interface.\n";
		print EMAIL "mail_program=$mail_prog\n";
		print EMAIL "1message=$FORM{'1message'}\n";
		print EMAIL "chuckaddress=$FORM{'chuckaddress'}\n";
		print EMAIL "commandline=$FORM{'commandline'}\n";
		print EMAIL "commandlineend=$FORM{'commandlineend'}\n";
		print EMAIL "lineformat=$FORM{'lineformat'}\n";
		print EMAIL "fromname=$FORM{'fromname'}\n";
		print EMAIL "fromaddr=$FORM{'fromaddr'}\n";
		print EMAIL "subjectline=$FORM{'subjectline'}\n";
		print EMAIL "static=$FORM{'static'}\n";
		print EMAIL "toaddress=$FORM{'toaddress'}\n";
		print EMAIL "toaddress_prefix=$FORM{'toaddress_prefix'}\n";
		print EMAIL "toaddress_switch=$FORM{'toaddress_switch'}\n";
		print EMAIL "fromaddress=$FORM{'fromaddress'}\n";
		print EMAIL "fromaddress_prefix=$FORM{'fromaddress_prefix'}\n";
		print EMAIL "fromaddress_switch=$FORM{'fromaddress_switch'}\n";
		print EMAIL "subjectaddress=$FORM{'subjectaddress'}\n";
		print EMAIL "subjectaddress_prefix=$FORM{'subjectaddress_prefix'}\n";
		print EMAIL "subjectaddress_switch=$FORM{'subjectaddress_switch'}\n";
		print EMAIL "useraddress=$FORM{'useraddress'}\n";
		print EMAIL "useraddress_prefix=$FORM{'useraddress_prefix'}\n";
		print EMAIL "useraddress_switch=$FORM{'useraddress_switch'}\n";
		print EMAIL "bccaddress=$FORM{'bccaddress'}\n";
		print EMAIL "bccaddress_prefix=$FORM{'bccaddress_prefix'}\n";
		print EMAIL "bccaddress_switch=$FORM{'bccaddress_switch'}\n";
		print EMAIL "replytoaddress=$FORM{'replytoaddress'}\n";
		print EMAIL "replytoaddress_prefix=$FORM{'replytoaddress_prefix'}\n";
		print EMAIL "replytoaddress_switch=$FORM{'replytoaddress_switch'}\n";
		print EMAIL "sendtext=$FORM{'sendtext'}\n";
		print EMAIL "tempfile=$FORM{'tempfile'}\n";
		print EMAIL "SMTP=$FORM{'SMTP'}\n";
		print EMAIL "net_SMTP=$FORM{'net_SMTP'}\n";
		print EMAIL "SMTP2=$FORM{'SMTP2'}\n";
		print EMAIL "reply_email=$FORM{'reply_email'}\n";
		print EMAIL "command_email=$FORM{'command_email'}\n";
		print EMAIL "id_method=$FORM{'id_method'}\n";
		close (EMAIL);
		&unlock("email_configuration", "$admin_dir/email.txt");
	} elsif ($FORM{'submit'} eq "Test") {
		if ($FORM{'testemail'} !~ m|^([\w\-\+\.]+)\@([\w\-\+\.]+)$|) {
			&error_message("Test E-mail Error", "You did not give a valid address to which to send the mail.", 0, 1);
		} else {
			if ($FORM{'subjectline'} == 0) {
				$SUBJECT = "Options Manager: E-mail Configurator: Test of E-mail";
			} elsif ($FORM{'subjectline'} == 2) {
				$SUBJECT = "Test of E-mail";
			} else {
				$SUBJECT = $FORM{'static'};
				$SUBJECT =~ s/[^\w\s\-\+\.]//g;
			}
			foreach $key (keys(%FORM)) {
				$EMAIL_OPTION{$key} = $FORM{$key};
				$email{$key} = $FORM{$key};
			}
			$EMAIL_OPTION{'command_line'} = $command_line;
			$EMAIL_OPTION{'input_stream'} = $input_stream;
			$msg = "This is a test of e-mail notification.  If you received it and\n";
			$msg .= "all parameters are correct, then e-mail notification is properly\n";
			$msg .= "set up on your server!  Hit \"Save Settings\" to apply changes.\n\n";
			$msg .= "-" x 50;
			$msg .= "\n";
			if ($EMAIL_OPTION{'reply_email'} == 1) {
				$msg .= "This section is added to test e-mail reply.  To test e-mail\n";
				$msg .= "reply, reply to this message and quote all of the text of the\n";
				$msg .= "message.  Discus will reply with a confirmation if your message\n";
				$msg .= "has been accepted.\n\n";
				$c = 0;
				for ($i = 1; $i <= length($admin_dir); $i++) {
					$c += ord(substr($admin_dir, $i-1, 1));
				}
				$msg .= "******DO*NOT*MODIFY*THE*FOLLOWING*LINE******\n";
				$msg .= "<==============>00000<=>$c<==============>\n";
				$msg .= "********DO*NOT*MODIFY*THE*ABOVE*LINE********\n";
				$msg .= "<=====>$FORM{'testemail'}<=====>\n";
				$msg .= "\n";
				$msg .= "-" x 50 . "\n";
			}
			if ($FORM{'net_SMTP'} == 0) {
				$msg .= "This command line:  $command_line\n\n";
				$msg .= "This input stream follows:\n\n";
				$msg .= $input_stream;
			} elsif ($FORM{'net_SMTP'} == 1) {
				$msg .= "Message sent using Net::SMTP by SMTP host: $FORM{'SMTP'}\n";
			} elsif ($FORM{'net_SMTP'} == 2) {
				$msg .= "Message sent using Mail::Sendmail by SMTP host: $FORM{'SMTP2'}\n";
			}
			$msg .= "-" x 50;
			$msg .= "\n";
			$msg .=  "DON'T FORGET TO SAVE YOUR SETTINGS!\n\n";
			$msg .= "- This is the end of the test -\n";
			$send_code = "&send_email_message did not execute!";
			$send_code = &send_email_message($FORM{'testemail'}, $SUBJECT, $msg, "", 0, 0, 1, 1, $FORM{'debugnow'});
			&header;
			print "<HTML><HEAD><TITLE>E-mail Configurator Status</TITLE>\n";
			print "<BODY BGCOLOR=#ffffff TEXT=#000000 LINK=#0000FF VLINK=#0000FF>$fs\n";
			if ($send_code ne "0") {
				print "<FONT SIZE=3 COLOR=#ff0000><CENTER><B>E-mail Configurator -- Test Failed</B></CENTER></FONT>\n";
				print "<HR>The test message was not properly sent.<P>\n";
				if ($send_code ne "") {
					print "Explanation:  <B>$send_code</B><P>\n";
				} else {
					print "Your e-mail notification mail settings are not properly entered.<P>\n";
				}
				print "<P>In a small number of cases, the message will still actually go\n";
				print "through although your system reports this error.  Check your mailbox and\n";
				print "if you receive the mail message, go back and save these settings.\n";
				print "</FONT></BODY></HTML>\n";
				exit(0);
			}
			print "<FONT SIZE=3><CENTER><B>E-mail Configurator -- Message Sent</B></CENTER></FONT>\n";
			print "<HR>\n";
			print "A test message was sent to the address you provided (<B>$FORM{'testemail'}</B>).\n";
			print "Please check your e-mail to see if the test message was properly received.  If it was,\n";
			print "click the &quot;Save Settings&quot; button below.  If it did not go through, you\n";
			print "can &quot;Exit Without Saving&quot; using the button below.<P>\n";
			print "<HR SIZE=0><P>\n";
			print "Please allow 5-10 minutes for this e-mail message to go through.  If you do not\n";
			print "get the message after that time, there is probably something wrong with how you\n";
			print "set up your parameters.<P>\n";
			print "<HR SIZE=0><P>\n";
			if ($FORM{'reply_email'} != 0) {
				print "You should save your settings <B>before</B> replying to the e-mail message\n";
				print "if you are going to test e-mail reply.<P>\n";
			}
			print "<FORM ACTION=$cgiurl METHOD=POST>\n";
			print "<INPUT NAME=action TYPE=HIDDEN VALUE=email_save>\n";
			print "<INPUT NAME=username TYPE=HIDDEN VALUE=$superuser>\n";
			foreach $key (keys(%FORM)) {
				next if $key eq "submit";
				next if $key eq "action";
				next if $key eq "username";
				print "<INPUT TYPE=HIDDEN NAME=$key VALUE=\"$FORM{$key}\">\n";
			}
			print "<INPUT TYPE=SUBMIT VALUE=\"Save Settings\">\n";
			print "<INPUT TYPE=BUTTON VALUE=\"Exit Without Saving\" onClick=\"history.back()\">\n";
			print "</FORM>\n";
			print "</FONT></BODY></HTML>\n";
			exit(0);
		}
	}
}

#---SEPARATOR---#

sub send_email_message {
	my ($addresses, $subject_line, $text_out, $footer, $topic_number, $me_number, $flag, $debugging, $debugnow) = @_;
	my ($send_code);
	if ($flag == 0) {
		if ($GLOBAL_OPTIONS{'options_used'}) {
			if ($GLOBAL_OPTIONS{'capable'} eq "0") {
				return -1;
			}
			if ($GLOBAL_OPTIONS{'capable'} eq "" && $GLOBAL_OPTIONS{'email'} == 0) {
				return -2;
			}
		}
	}

	# Important note:  Each address in '$addresses' was checked in the calling
	# subroutine to meet the following pattern:
	#    m|^([\w\-\+\.]+)\@([\w\-\+\.]+)$|

	undef %email;

	# The following code checks for an "email.txt" file in your source directory
	# and if it exists, it uses it.  We use this in our demonstration service,
	# although it is unlikely that anyone else would need to use this.

	if ($flag == 0) {
		if (open (CFG, "$admin_dir/email.txt")) {
			@cfg = <CFG>;
			close (CFG);
		} else {
			return 1007;
		}
		foreach $line (@cfg) {
			if ($line =~ m|^(\w+)=(.*)|) {
				$o = $1; $t = $2; $t =~ s/\r//g;
				$t = &unescape($t) if $t !~ /\s/;
				$email{$o} = $t;
			}
		}
	} else {
		foreach $key (keys(%EMAIL_OPTION)) {
			$email{$key} = $EMAIL_OPTION{$key};
		}
	}

	$subject_line = &char_convert($subject_line, 1, 1, 0) if $command =~ m|\$SUBJECT|;
	$subject_line = &char_convert($subject_line, 1, 0, 0) if $command !~ m|\$SUBJECT|;
	$subject_line =~ s/\s+/ /g;
	$send_code = 0;

	if ($email{'net_SMTP'} == 0) {
		if ($email{'1message'} == 2) {
			if (!-w "$admin_dir/mailqueue") {
				$email{'1message'} = 1;
				&log_error("src-board-subs-5", "send_email_message", "Mail queue directory is not writable in sending message!");
			} else {
				$filename = join("", time, $$, $ENV{'REMOTE_ADDR'});
				$filename =~ s/\D//g;
				$fullpath = "$admin_dir/mailqueue/$filename.txt";
				$boundary = "---boundary: $filename---\n";
				while ($input =~ m|$boundary| || $addresses =~ m|$boundary|) {
					$filename .= "1";
					$boundary = "---boundary: $filename---\n";
				}
			}
		}
		$command = $email{'command_line'};
		$input = $email{'input_stream'};
		$command =~ s/\$SUBJECT/$subject_line/g;
		$input =~ s/\$SUBJECT/$subject_line/g;
		$command =~ s/\$REPLYTO/$reply_to/g;
		$input =~ s/\$REPLYTO/$reply_to/g;
		$input .= "X-Mailer: Discus $release_version";
		$input .= ".$free_revision" if !$pro;
		$input .= ".$pro_revision Pro" if $pro;
		$input .= " <$html_url>\n";
		if ($email{'1message'} == 2) {
			open (FILE, ">$fullpath") || &error_message("Mail Send Error", "Could not open mail queue for writing!");
			print FILE $boundary;
			@addresses = split(/,/, $addresses);
			foreach $line (@addresses) {
				($email, $user, $pass) = split(/\|\|/, $line);
				$line = $email;
				$line =~ s/\@\./\@/g;
				$command_temp = $command;
				$command_temp =~ s/\$TO/$email/g;
				print FILE "$command_temp\n";
				$input_temp = $input;
				$input_temp =~ s/\$TO/$email/g;
				print FILE "$input_temp\n";
				print FILE $text_out;
				$flag = 0;
				print FILE "$footer";
				print FILE "<==============>|$user|", crypt($pass, "cookie"), "|<==============>\n" if ($user ne "" && $email{'reply_email'});
				print FILE $boundary;
			}
			close (FILE);
			chmod (oct($perms0666), $fullpath);
			return 0;
		} elsif ($email{'1message'} == 1) {
			@addresses = split(/,/, $addresses);
			foreach $line (@addresses) {
				($email, $user, $pass) = split(/\|\|/, $line);
				$line = $email;
				$line =~ s/\@\./\@/g;
			}
			$addresses = join(",", @addresses);
			$command =~ s/\$BCC/$addresses/g;
			$input =~ s/\$BCC/$addresses/g;
			if ($email{'tempfile'} == 0) {
				open (MAIL, "$command") || return 1000;
				$send_code = 0;
			} else {
				$pid = $$; $pid =~ s/\D//g; $time = time;
				$tempfile = "$message_dir/$time-$pid.tmp";
				open (MAIL, ">$tempfile") || return 1001;
				$send_code = 0;
			}
			print MAIL $input;
			print MAIL "\n";
			print MAIL $text_out;
			print MAIL $footer if $footer;
			close (MAIL);
			return 0 if $email{'tempfile'} != 1;
			$command_temp = $command;
			$command_temp =~ s/^\|\s*//;
			if ($email{'tempfile'} == 1) {
				$mail_prog = $email{'mail_program'};
				if ($command_temp !~ m|\$TEMPFILE|) {
					$pr = $email{'mail_program'}; $pr2 = quotemeta($pr);
					if ($command_temp =~ m|$pr2|) {
						$stuff = $';
						$command_temp = $pr;
					} else {
						$stuff = "";
					}
					$cmdline = "$mail_prog $tempfile $stuff";
				} else {
					$pr = $email{'mail_program'}; $pr2 = quotemeta($pr);
					$cmdline = $command_temp;
					$cmdline =~ s/\$TEMPFILE/$tempfile/g;
				}
				$cmdline =~ s/^\|\s*//;
				if ($^O eq "MSWin32") {
					$mail_prog = $pr;
					$mail_prog =~ s%/%\\%g;
					$cmdline =~ s%/%\\%g;
					$cmdline =~ s/'/"/g;
					$cmdline =~ s/^\s*//;
					if ($mail_prog =~ m|(.*)\\|) {
						$cwd = $1;
					} else {
						$cwd = ".";
					}
					require "$admin_dir/source/src-board-subs-win32" || return 1002;
					$send_code = &Win32_send($mail_prog, $cmdline, $cwd, $debugnow);
				} else {
					if (system $cmdline) {
						&log_error("src-board-subs-5", "send_email_message", "Send failed for [$cmdstuff] [[$otherstuff]] $!");
						return "1008 (Command failed: $!)";
					}
					$send_code = 0;
				}
				unlink ($tempfile);
				return $send_code;
			}
		} else {
			$pid = $$; $pid =~ s/\D//g; $time = time;
			$tempfile = "$message_dir/$time-$pid.tmp";
			@addresses = split(/,/, $addresses);
			foreach $address (@addresses) {
				($email, $user, $pass) = split(/\|\|/, $address);
				$address = $email;
				$address =~ s/\@\./\@/g;
				$command_temp = $command;
				$input_temp = $input;
				$command_temp =~ s/\$TO/$address/g;
				$input_temp =~ s/\$TO/$address/g;
				if ($email{'tempfile'} == 0) {
					open (MAIL, $command_temp) || return 1025;
					$send_code = 0;
				} else {
					$command_temp =~ s%^\|\s*%%;
					open (MAIL, ">$tempfile") || return 1003;
				}
				print MAIL $input_temp;
				print MAIL "\n";
				print MAIL $text_out;
				print MAIL $footer if $footer;
				close (MAIL);
				if ($email{'tempfile'} == 1) {
					if ($command_temp !~ m|\$TEMPFILE|) {
						$pr = $email{'mail_program'}; $pr2 = quotemeta($pr);
						if ($command_temp =~ m|$pr2|) {
							$stuff = $';
							$command_temp = $pr;
						} else {
							$stuff = "";
						}
						$cmdline = "$pr $tempfile $stuff";
					} else {
						$pr = $email{'mail_program'}; $pr2 = quotemeta($pr);
						$cmdline = $command_temp;
						$cmdline =~ s/\$TEMPFILE/$tempfile/g;
					}
					$cmdline =~ s/^\|\s*//;
					if ($^O eq "MSWin32") {
						$mail_prog = $pr;
						$mail_prog =~ s%/%\\%g;
						$cmdline =~ s%/%\\%g;
						$cmdline =~ s/'/"/g;
						$cmdline =~ s/^\s*//;
						if ($mail_prog =~ m|(.*)\\|) {
							$cwd = $1;
						} else {
							$cwd = ".";
						}
						require "$admin_dir/source/src-board-subs-win32" || return 1004;
						$send_code = &Win32_send($mail_prog, $cmdline, $cwd, $debugnow);
					} else {
						if (system $cmdline) {
							&log_error("src-board-subs-5", "send_email_message", "Send failed for [$cmdstuff] [[$otherstuff]] $!");
							return "1005 (Command failed: $!)";
						}
						$send_code = 0;
					}
					unlink ($tempfile);
				}
			}
			return $send_code;
		}
		return 0;
	} elsif ($email{'net_SMTP'} == 1) {
		require "$admin_dir/source/src-board-subs-smtp" || return 1006;
		$data = $text_out;
		$data .= "\n";
		$data .= $footer if $footer;
		$data .= "\n";
		@addresses = split(/,/, $addresses);
		foreach $address (@addresses) {
			($email, $user, $pass) = split(/\|\|/, $address);
			$address = $email;
			$result_SMTP = &SMTP_send($address, "", $data, $debugging, $subject_line, $reply_to);
			if ($result_SMTP != 1) {
				&error_message("Could not send e-mail", "Net::SMTP module not properly installed on this system.");
			}
		}
		return 0;
	} elsif ($email{'net_SMTP'} == 2) {
		require "$admin_dir/source/src-board-subs-msm" || return 1006;
		$data = $text_out;
		$data .= "\n";
		$data .= $footer if $footer;
		$data .= "\n";
		@addresses = split(/,/, $addresses);
		undef @finals;
		foreach $address (@addresses) {
			($email, $user, $pass) = split(/\|\|/, $address);
			$address = $email;
			push (@finals, $email);
		}
		if (scalar(@finals) == 1) {
			$to = $finals[0]; $bcc = "";
		} else {
			$to = ""; $bcc = join(",", @finals);
		}
		$result_msm = &mail_sendmail_send($to, $bcc, $data, $debugging, $subject_line, $reply_to);
		if ($result_msm != 1) {
			&error_message("Could not send e-mail", "Mail::Sendmail module not properly installed on this system.");
		} else {
			return 0;
		}
	}
	return 0;
}


#---SEPARATOR---#
#REQ:send_email_message
#REQ:notification_message

sub email_notification {
	my ($topic, $page, $postby, $message_in, $username, $groupname, $postnum) = @_;
	undef @addresses; undef %addresses;
	open (PASSWD, "$admin_dir/passwd.txt"); @passwd = <PASSWD>; close (PASSWD);
	if ($GLOBAL_OPTIONS{send_mail_only_admins} != 1) {
		open (USERS, "$admin_dir/users.txt"); @users = <USERS>; close (USERS);
	} else {
		undef @users;
	}
	foreach $line (@passwd) {
		$line =~ s/\s*$//;
		push (@users, "$line:MODERATOR\n");
	}
	@user = grep(/^$username:/, @users);
	($uline) = grep(/:$groupname\s*$/, @user);
	if ($uline) {
		$line = $uline;
		$line =~ s/\s*$//;
		($user, $pass, $email, $full, $edit, $notify, $last, $group) = split(/:/, $line);
		$mod = 0;
		$mod = 1 if $group eq "MODERATOR";
		$addresses{$email} = 1;
		if ($email =~ m|^([\w\-\.\+]+)\@([\w\-\.\+]+)$|) {
			$t = $2;
			if ($t !~ m|^\.| && $t !~ m|\.$|) {
				@emt = split(/,/, $notify);
				if (grep(/^0$/, @emt)) {
					push (@addresses, "$email||$user||$pass||$mod");
					$addresses{$email} = 1;
				}
			}
		}
	}
	foreach $line (@users) {
		$line =~ s/\s*$//;
		($user, $pass, $email, $full, $edit, $notify, $last, $group) = split(/:/, $line);
		next if $addresses{$email};
		next if $email !~ m|^([\w\-\.\+]+)\@([\w\-\.\+]+)$|;
		$email =~ m|^([\w\-\.\+]+)\@([\w\-\.\+]+)$|;
		$t = $2;
		next if $t =~ m|^\.|;
		next if $t =~ m|\.$|;
		@emt = split(/,/, $notify);
		foreach $l (@emt) {
			if ($l =~ m|^(\d+)/|) {
				$l = $1;
			}
		}
		$mod = 0;
		$mod = 1 if $group eq "MODERATOR";
		if (grep(/^$topic_number$/, @emt)) {
			push (@addresses, "$email||$user||$pass||$mod");
			$addresses{$email} = 1;
		}
	}
	if (scalar(@addresses)) {
		$addresses = join(",", @addresses);
		open (CFG, "$admin_dir/email.txt"); @cfg = <CFG>; close (CFG);
		foreach $line (@cfg) {
			if ($line =~ m|^(\w+)=(.*)|) {
				$o = $1; $t = $2;
				$t = &unescape($t) if $t !~ /\s/;
				$email{$o} = $t;
			}
		}
		&extract("//$topic/$page.$ext");
		$titlestr = "$title: $topic_name";
		$emts = $topic_name;
		foreach $key (sort {$a <=> $b} keys(%level_number)) {
			$titlestr .= ": $level_name{$key}";
			$emts .= ": $level_name{$key}";
		}
		$emts = $me_name if $email{'subjectline'} == 2;
		$emts_result = &prepare_variable($emts);
		$titlestr_result = &prepare_variable($titlestr);
		if ($email{'subjectline'} != 1) {
			$subject_result = &prepare_variable($emts_result, 1) if $email{'subjectaddress'} != 0;
			$subject_result = &prepare_variable($emts_result) if $email{'subjectaddress'} == 0;
		} elsif ($email{'subjectline'} == 1) {
			$subject_result = &prepare_variable($email{'static'}, 1) if $email{'subjectaddress'} != 0;
			$subject_result = &prepare_variable($email{'static'}) if $email{'subjectaddress'} == 0;
		}
		$postby_result = &prepare_variable($postby);
		$message_result = $message_in;
		if ($email{'sendtext'} == 0) {
			$message_result = $L{EMAIL_NOTIFY_NOTEXT};
		}
		$pb = $L{POSTBY};
		($datetime) = &get_date_time("long");
		$pb =~ s/\%name/$postby_result/g;
		$pb =~ s/\%date/$datetime/g;
		$pb = &prepare_variable($pb);
		($text_out) = &notification_message($titlestr_result, $pb, $message_result, 0, $topic_number, $me_number, "", "", $postnum);
		&send_email_message($addresses, $subject_result, $text_out, "", $topic_number, $me_number);
	}
}

sub prepare_variable {
	my ($msg, $remove_metachar) = @_;
	$msg =~ s/<BR>/\n/g;
	$msg =~ s/<IMG SRC="[^"]*" ALT="([^"]*)">/$1/g;
	$msg =~ s/<LI>/\n\* /g;
	$msg =~ s/<\/UL>/\n\n/g;
	$msg =~ s/<[^>]*>//g;
	$msg =~ s/&#(\d+);/pack("c", $1)/ge;
	$msg =~ s/&gt;/>/g;
	$msg =~ s/&lt;/</g;
	$msg =~ s/&quot;/"/g;
	$msg =~ s/&amp;/&/g;
	if ($remove_metachar) {
		$msg =~ s/[^\w\s\-\.\+\:\,\/]//g;
	}
	return $msg;
}

#---SEPARATOR---#

sub notification_message {
	my ($navbar, $posted_by, $msg, $footer_flag, $topic, $page, $user, $pass, $index) = @_;
	my ($result); $result = "";

	# Navigation Bar Simulation

	$navbar = &char_convert($navbar, 1);

	$result = "-" x 60; $result .= "\n";
	$result .= &wordwrap($navbar, 60, 0);
	$result .= "-" x 60; $result .= "\n";
	$result .= "\n";

	# Posted by ... simulation

	$result .= &wordwrap($posted_by, 60, 0);
	$result .= "\n";

	# Actual text of message

	$imgtext = $L{MESSAGE_IMAGE};
	$msg =~ s/<BR>/\n/g;
	$msg =~ s/<IMG SRC="[^"]*" ALT="([^"]*)">/\[$imgtext $1 \]/gi;
	$msg =~ s%<IMG SRC="$html_url/clipart/([^"]+)\.(\w+)">%\[$imgtext $1 \]%gi;
	$msg =~ s/<LI>/\n\* /g;
	$msg =~ s/<\/UL>/\n\n/g;
	$msg =~ s/&nbsp;/ /g;
	$msg = &char_convert($msg, 1);
	$msg =~ s/\\\}/\}/g;
	$msg =~ s/\\\\/\\/g;
	$msg =~ s/\\\{/\{/g;

	# Quoted text

	while ($msg =~ m|<!-Quote-!>|) {
		$b = $`; $a = $';
		last if $a !~ m|<!-/Quote-!>|;
		$a =~ m|<!-/Quote-!>|; $b1 = $`; $a1 = $';
		$b1 =~ s/<P>/\n/g;
		$b1 =~ s/<[^>]*>//g;
		$msg = join("\n", $b, join("", "     ", "-" x 50, "\n", &wordwrap($b1, 55, 5, "", ""), "     ", "-" x 50, "\n"), $a1);
	}

	# Strip remaining HTML

	$msg =~ s/<P>/\n/g;
	$msg =~ s/<[^>]*>//g;

	$result .= &wordwrap($msg, 60, 0, "", "");
	$result .= "\n";

	# Add footer

	if (open (CFG, "$admin_dir/email.txt")) {
		@cfg = <CFG>;
		close (CFG);
		if (grep(/^reply_email=1/, @cfg)) {
			$reply_by_email = 1;
		} else {
			$reply_by_email = 0;
		}
	}

	if ($footer_flag == 0) {
		if ($reply_by_email == 1) {
			if ($GLOBAL_OPTIONS{'link_first'}) {
				if ($topic) {
					$result .= "$L{BPR_GOTOLINK}\n";
					$result .= "$script_url/show.$cgi_extension?$topic/$page\n\n";
				}
				$result .= $L{EMAIL_REPLY_FOOTER};
			} else {
				$result .= $L{EMAIL_REPLY_FOOTER};
				if ($topic) {
					$result .= "$L{BPR_GOTOLINK}\n";
					$result .= "$script_url/show.$cgi_extension?$topic/$page\n\n";
				}
			}
			$result .= "\n" x 5;
			$result .= "******DO*NOT*MODIFY*THE*FOLLOWING*LINE******\n";
			$result .= "<==============>$topic<=>$page<=>$index<==============>\n";
			$result .= "********DO*NOT*MODIFY*THE*ABOVE*LINE********\n";
		} else {
			$r = $L{BPR_STANDARD_FOOTER};
			$result .= $r;
			$result .= "\n\n";
			if ($topic) {
				$result .= "$L{BPR_GOTOLINK}\n";
				$result .= "$script_url/show.$cgi_extension?$topic/$page\n\n";
			}
		}
	}
	return $result;
}

sub wordwrap {
	my ($text_in, $line_length, $indented, $boxstart, $boxstop) = @_;
	my ($tout); $tout = "";
	$max = $line_length - $indented - length($boxstart) - length($boxstop);
	@frag = split(/\n/, $text_in);
	foreach $frag (@frag) {
		if ($frag eq "" || $frag !~ m|\S|) {
			if ($boxstart ne "") {
				$tout .= $boxstart;
			}
			if ($boxstop ne "") {
				$tout .= " " x ($line_length - length($boxstart) - length($boxstop));
				$tout .= $boxstop;
			}
			$tout .= "\n";
			next;
		}
		$frag =~ s/\s*$//;
		$frag .= " ";
		while ($frag ne "") {
			while ($frag =~ m|^(.{0,$max})(\s)|) {
				$o = $1; $t = $2; $frag = $';
				$tout .= join("", $boxstart, " " x ($indented - length($boxstart)), $o);
				if ($boxstop) {
					$sp = $line_length - length($o) - $indented - length($boxstart);
					$tout .= " " x ($sp - length($boxstop));
					$tout .= $boxstop;
				}
				$tout .= "\n";
			}
			while ($frag =~ m|^(.{$max,})(\s)|) {
				$o = $1; $t = $2; $frag = $';
				$tout .= $boxstart;
				$tout .= " " x ($indented - length($boxstart));
				$tout .= $o;
				$tout .= "\n";
			}
			last if $frag !~ m|\S|;
		}
	}
	return $tout;
}

#---SEPARATOR---#

sub email_configurator {
	&auto_configure_email;
	&lock("email_configurator", "$admin_dir/email.txt");
	open (EMAIL, "$admin_dir/email.txt"); @email = <EMAIL>; close (EMAIL);
	foreach $line (@email) {
		if ($line =~ m|^(\w+)=(.*)|) {
			$EMAIL_OPTION{$1} = $2;
		}
	}
	&unlock("email_configurator", "$admin_dir/email.txt");
	$override_flag = 0;
	&header;
	print "<HTML><HEAD><TITLE>E-mail Configurator</TITLE>\n";
	print '<BODY BGCOLOR="ffffff" TEXT="000000" LINK="0000FF" VLINK="0000FF" ';
	print "onLoad=\"window.defaultStatus = 'E-mail Configurator'\">$fs\n";
	print "<FONT SIZE=3><CENTER><B>E-mail Configurator</B></CENTER></FONT>\n";
	print "<HR>\n";
	print "<B><A HREF=\"$cgiurlm?cmd=options_mgr&username=$superuser\" onMouseOver=\"window.status = 'Return to Options Manager'; return true\">Options Manager</A>: E-mail Configurator</B><HR>\n";
	if ($override_flag) {
		print "<TABLE BORDER=7 WIDTH=100% BORDERCOLOR=#ff0000 BGCOLOR=#ffcccc><TR><TD>$fs\n";
		print "<FONT COLOR=#ff0000><B>Warning!</B>  The e-mail settings are being\n";
		print "obtained from the file <B>$admin_dir/source/email.txt</B>, which is\n";
		print "read-only.  If you wish to use this interface to configure e-mail\n";
		print "notification, move the <B>$admin_dir/source/email.txt</B> file\n";
		print "into your <B>$admin_dir</B> directory (and remove it from the\n";
		print "<B>$admin_dir/source</B> directory).</FONT></TD></TR></TABLE><P>\n";
	}
	if ($EMAIL_OPTION{'mail_program'} eq "" && $EMAIL_OPTION{'net_SMTP'} == 0) {
		print "<FONT COLOR=#ff4444><B>Warning!</B>  You have not yet configured\n";
		print "e-mail notification.  Where possible, suggestions based on common\n";
		print "implementations of mail programs have been specified below.  However,\n";
		print "you must save the settings for them to take effect.<P></FONT>\n";
	}
	print "This screen allows you to configure e-mail notification for your\n";
	print "entire board.  These parameters will be used to control all e-mail\n";
	print "messages originating from this board.<P>\n";
	if ($GLOBAL_OPTIONS{send_mail_only_admins} == 1) {
		print "<B><FONT COLOR=#ff0000>You have configured your board to send e-mail notification only to\n";
		print "board moderators.  This setting can be changed through the Options Manager.</FONT></B><P>\n";
	}
	$nsmtp = 0;
	eval 'use Net::SMTP;';
	$nsmtp = 1 if $@ eq "";
	$msm = 0;
	eval 'use Mail::Sendmail;';
	$msm = 1 if $@ eq "";
	print "<FORM ACTION=$cgiurl METHOD=POST>\n";
	print "<INPUT NAME=action TYPE=HIDDEN VALUE=email_save>\n";
	print "<INPUT NAME=username TYPE=HIDDEN VALUE=$superuser>\n";
	print "<TABLE BGCOLOR=#ffffcc BORDER=1 WIDTH=100%><TR><TD>$fs";
	print "<B>Mail Program Location</B>\n";
	print "<BLOCKQUOTE>This is the full path to the mail program on your server\n";
	print "(we recommend using <I>sendmail</I> if it is available).";
	if ($msm == 1 || $nsmtp == 1) {
		print "  Otherwise you can supply the network address of an SMTP server.<P>\n";
	} else {
		print "<P>\n";
	}
	@sp = ("/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", "/usr/local/sbin", "/usr/contrib/bin", "/usr/contrib/sbin", "/usr/lib", "/lib", "/etc", "/");
	$mail_prog = "";
	foreach $p (@sp) {
		if (-x "$p/sendmail") {
			print "<FONT COLOR=ff6600>It appears that <B>sendmail</B> on your system is located at <B>$p/sendmail</B>.  Unless you have a strong reason to do choose otherwise, you should choose this as the default mail program.</FONT><P>";
			$mail_prog = "$p/sendmail";
			last;
		}
	}
	if (!$mail_prog) {
		foreach $p (@sp) {
			if (-x "$p/mail") {
				print "<FONT COLOR=ff6600>It appears that <B>mail</B> on your system is located at <B>$p/mail</B>.  Unless you have a strong reason to do choose otherwise (for example, you know where <I>sendmail</I> is), you should choose this as the default mail program.</FONT><P>";
				$mail_prog = "$p/mail";
				last;
			}
		}
	}
	print "<TABLE><TR><TD>$fs";
	print "<INPUT TYPE=RADIO NAME=net_SMTP VALUE=0";
	print " CHECKED" if $EMAIL_OPTION{'net_SMTP'} == 0;
	print "> Mail Program:</FONT></TD><TD><INPUT SIZE=35 NAME=mail_program TYPE=TEXT VALUE='";
	print $EMAIL_OPTION{'mail_program'} if $EMAIL_OPTION{'mail_program'};
	$mail_prog = $EMAIL_OPTION{'mail_program'} if $EMAIL_OPTION{'mail_program'};
	print $mail_prog if !$EMAIL_OPTION{'mail_program'};
	print "'></TD></TR>\n";
	if ($nsmtp == 1) {
		print "<TR><TD>$fs";
		print "<INPUT TYPE=RADIO NAME=net_SMTP VALUE=1";
		print " CHECKED" if $EMAIL_OPTION{'net_SMTP'} == 1;
		print "> Net::SMTP server:";
		print "</TD><TD><INPUT TYPE=TEXT NAME=SMTP VALUE='$EMAIL_OPTION{'SMTP'}' SIZE=35></TD></TR>\n";
	}
	if ($msm == 1) {
		print "<TR><TD>$fs";
		print "<INPUT TYPE=RADIO NAME=net_SMTP VALUE=2";
		print " CHECKED" if $EMAIL_OPTION{'net_SMTP'} == 2;
		print "> Mail::Sendmail server:";
		print "</TD><TD><INPUT TYPE=TEXT NAME=SMTP2 VALUE='$EMAIL_OPTION{'SMTP2'}' SIZE=35></TD></TR>\n";
	}
	print "</TABLE></BLOCKQUOTE>\n";
	if ($EMAIL_OPTION{'net_SMTP'} == 0) {
		print "<B>Mailing Method</B>\n";
		print "<BLOCKQUOTE>If you have relatively few users on your board, a separate e-mail\n";
		print "message can be generated for each user.  However, if you have a large number\n";
		print "of users, it can bog down the server significantly, so you can just send one\n";
		print "message with all of the users in the BCC field.\n";
		print "<P>\n";
		print "<INPUT TYPE=RADIO NAME=1message VALUE=0";
		print " CHECKED" if $EMAIL_OPTION{'1message'} == 0;
		print ">  Send a separate message to each recipient<BR>\n";
		print "<INPUT TYPE=RADIO NAME=1message VALUE=1";
		print " CHECKED" if $EMAIL_OPTION{'1message'} == 1;
		print ">  Send one message with recipients listed in the BCC field<BR>\n";
		print "<BLOCKQUOTE>If you send one message with recipients in the BCC field, you\n";
		print "must provide a \"To:\" address for the message.  This must be a <I>valid</I>\n";
		print "address that <I>you</I> control -- this address will receive a copy of every\n";
		print "notification from your board.<P>\n";
		print "To Address: </FONT><INPUT TYPE=TEXT NAME=chuckaddress VALUE='";
		if ($EMAIL_OPTION{'chuckaddress'}) {
			print "$EMAIL_OPTION{'chuckaddress'}";
		} else {
			if ($contact =~ m|<A HREF="mailto:([^"]+)"|) {
				print $1;
			}
		}
		print "' SIZE=30></BLOCKQUOTE><P>\n";
		if (-d "$admin_dir/mailqueue" && -W "$admin_dir/mailqueue") {
			print "<INPUT TYPE=RADIO NAME=1message VALUE=2";
			print " CHECKED" if $EMAIL_OPTION{'1message'} == 2;
			print ">$fs Send messages using a cron job</FONT><BR>\n";
		}

		print "</BLOCKQUOTE>$fs\n";
	}
	print "<B>From Name and Address</B><BLOCKQUOTE>Generally, you can make the &quot;From:&quot;\n";
	print "name and address to be <I>your</I> forum name.  Note that forging someone else's name is\n";
	print "unethical and possibly illegal.<P>\n";
	print "From Name:</FONT>  <INPUT TYPE=TEXT NAME=fromname SIZE=30 VALUE='";
	if ($EMAIL_OPTION{'fromname'}) {
		print "$EMAIL_OPTION{'fromname'}";
	} else {
		print $titlej;
	}
	print "'><P>$fs\n";
	print "You can also specify this e-mail address, which will be used as both the From address\n";
	print "and the Reply To address.<P>\n";
	print "From Address:</FONT>  <INPUT TYPE=TEXT NAME=fromaddr SIZE=30 VALUE='";
	if ($EMAIL_OPTION{'fromaddr'}) {
		print "$EMAIL_OPTION{'fromaddr'}";
	} else {
		if ($contact =~ m|<A HREF="mailto:([^"]+)"|) {
			print $1;
		}
	}
	print "'></BLOCKQUOTE>$fs<P>\n";
	print "<B>Subject Line</B><BLOCKQUOTE>You can choose to have the subject line be\n";
	print "the navigation bar of the topic where the message was posted, or you can\n";
	print "use static text.<P>\n";
	print "<INPUT TYPE=RADIO NAME=subjectline VALUE=0";
	print " CHECKED" if $EMAIL_OPTION{'subjectline'} == 0;
	print ">  Use the navigation bar as the subject line<BR>\n";
	print "<INPUT TYPE=RADIO NAME=subjectline VALUE=2";
	print " CHECKED" if $EMAIL_OPTION{'subjectline'} == 2;
	print ">  Use the page title as the subject line<BR>\n";
	print "<INPUT TYPE=RADIO NAME=subjectline VALUE=1";
	print " CHECKED" if $EMAIL_OPTION{'subjectline'} == 1;
	print ">  Use the following static text as the subject line:<P>\n";
	print "<BLOCKQUOTE>Static Text:</FONT> <INPUT TYPE=TEXT NAME=static SIZE=30 VALUE='";
	if ($EMAIL_OPTION{'static'}) {
		print "$EMAIL_OPTION{'static'}";
	} else {
		print "E-mail Notification of Post";
	}
	print "'></BLOCKQUOTE></BLOCKQUOTE>$fs\n";
	print "<B>Notification Content</B><BLOCKQUOTE>You can choose whether or not the notifications that\n";
	print "are sent contain the text of the message posted.<P>\n";
	print "<INPUT TYPE=RADIO NAME=sendtext VALUE=1";
	print " CHECKED" if ($EMAIL_OPTION{'sendtext'} == 1 || $EMAIL_OPTION{'sendtext'} eq "");
	print "> Include full text of message posted<BR>\n";
	print "<INPUT TYPE=RADIO NAME=sendtext VALUE=0";
	print " CHECKED" if ($EMAIL_OPTION{'sendtext'} == 0 && $EMAIL_OPTION{'sendtext'} ne "");
	print "> DO NOT include full text of message posted<BR>\n";
	print "</BLOCKQUOTE><P>\n";
	if ($EMAIL_OPTION{'net_SMTP'} == 0) {
		print "<B>Technical Configuration of Mail Program</B>\n";
		print "<BLOCKQUOTE>Most mail programs work properly by accepting input on the input\n";
		print "stream.  However, some mail programs (notably many mail programs available for\n";
		print "Windows servers) require creation of a temporary file.\n";
		print "<P>\n";
		print "<INPUT TYPE=RADIO NAME=tempfile VALUE=0";
		print " CHECKED" if $EMAIL_OPTION{'tempfile'} == 0;
		print ">  Send input directly into the mail program (<U>no</U> temporary file)<BR>\n";
		print "<INPUT TYPE=RADIO NAME=tempfile VALUE=1";
		print " CHECKED" if $EMAIL_OPTION{'tempfile'} == 1;
		print ">  Send input into a temporary file\n";
		print "<P>Most <I>sendmail</I> programs behave similarly, and most <I>mail</I>\n";
		print "programs behave similarly.  However, if your mail program behaves differently, you\n";
		print "can specify that here.  Because <I>sendmail</I> is more universal in commands,\n";
		print "we recommend using <I>sendmail</I> if it is available.<P>\n";
		print "<U>Necessary Command Line Options</U>:  If your mail program requires that anything\n";
		print "be put in the command line, other than possibly the address, from address, BCC address,\n";
		print "or subject line, enter those arguments here.</FONT>\n";
		print "<BLOCKQUOTE><TABLE><TR><TD>$fs";
		print "Beginning of line options:</FONT></TD><TD><INPUT TYPE=TEXT NAME=commandline SIZE=25 VALUE='";
		if ($EMAIL_OPTION{'commandline'}) {
			print "$EMAIL_OPTION{'commandline'}";
		} else {
			print "-t" if $mail_prog =~ m|/sendmail|;
		}
		print "'></TD></TR><TR><TD>$fs";
		print "End of line options:</FONT></TD><TD><INPUT TYPE=TEXT NAME=commandlineend SIZE=25 VALUE='";
		if ($EMAIL_OPTION{'commandlineend'}) {
			print "$EMAIL_OPTION{'commandlineend'}";
		}
		print "'></TD></TR>";
		print "<TR><TD VALIGN=TOP>$fs", "Command line format:</FONT></TD><TD>$fs";
		print "<INPUT TYPE=RADIO NAME=lineformat VALUE=0 ";
		print "CHECKED" if $EMAIL_OPTION{'lineformat'} == 0;
		print "> Values with quotes<BR>\n";
		print "<INPUT TYPE=RADIO NAME=lineformat VALUE=1 ";
		print "CHECKED" if $EMAIL_OPTION{'lineformat'} == 1;
		print "> Entire arguments with quotes<BR>\n";
		print "</FONT></TD></TR>\n";
		print "</TABLE></BLOCKQUOTE>$fs<P>\n";
		foreach $wu ('to', 'from', 'user', 'bcc', 'subject', 'reply-to') {
			$wdisp = $wu;
			$w = $wu; $w =~ s/\s//g; $w =~ s/-//g;
			print "<B><U>\U$wdisp\E</U></B><P>\n";
			if ($wu eq "user") {
				print "<FONT SIZE=1>This option controls the \"from\" e-mail address on those systems where this must be passed separately (such as BLAT).  This is not generally used for <I>mail</I> or <I>sendmail</I>.</FONT><P>\n";
			} elsif ($wu eq "to") {
				print "<FONT SIZE=1>This option controls the \"to\" e-mail address and is used on all platforms.</FONT><P>\n";
			} elsif ($wu eq "from") {
				print "<FONT SIZE=1>This option controls the \"from\" name (and e-mail address if placed in the input stream) and is used on most platforms.</FONT><P>\n";
			} elsif ($wu eq "bcc") {
				print "<FONT SIZE=1>This option controls the \"blind carbon copy\" e-mail address, used for the \"one message\" option above, and is used on most platforms.</FONT><P>\n";
			} elsif ($wu eq "reply-to") {
				print "<FONT SIZE=1>This option controls the \"reply to\" e-mail address and is used on most platforms.</FONT><P>\n";
			} elsif ($wu eq "subject") {
				print "<FONT SIZE=1>This option controls the \"subject line\" and is used on all platforms.</FONT><P>\n";
			}
			print "<INPUT TYPE=RADIO NAME=$w", "address VALUE=0 ";
			if ($EMAIL_OPTION{$w . 'address'} ne "") {
				print "CHECKED" if $EMAIL_OPTION{$w . 'address'} == 0;
			} else {
				print "CHECKED" if ($mail_prog =~ m|/sendmail| && $wu ne "user");
			}
			print "></FONT> \n";
			print "<INPUT TYPE=TEXT NAME=$w", "address_prefix SIZE=10 VALUE='";
			if ($EMAIL_OPTION{$w . 'address_prefix'} ne "") {
				print $EMAIL_OPTION{$w . 'address_prefix'};
			} else {
				print "\U$wdisp\E:" if $mail_prog =~ m|/sendmail|;
			}
			print "'>$fs", " in the input stream<BR>\n";
			print "<INPUT TYPE=RADIO NAME=$w", "address VALUE=1 ";
			if ($EMAIL_OPTION{$w . 'address'} ne "") {
				print "CHECKED" if $EMAIL_OPTION{$w . 'address'} == 1;
			} else {
				print "CHECKED" if ($mail_prog =~ m|/mail|);
			}
			print "></FONT> <INPUT TYPE=TEXT NAME=$w", "address_switch SIZE=10 VALUE='";
			if ($EMAIL_OPTION{$w . 'address_switch'} ne "") {
				print $EMAIL_OPTION{$w . 'address_switch'};
			} else {
				print "-s" if ($mail_prog =~ m|/mail| && $w eq "subject");
				print "-b" if ($mail_prog =~ m|/mail| && $w eq "bcc");
				print "-F" if ($mail_prog =~ m|/mail| && $w eq "from");
			}
			print "'>$fs on the command line<BR>\n";
			print "<INPUT TYPE=RADIO NAME=$w", "address VALUE=2 ";
			if ($EMAIL_OPTION{$w . 'address'} ne "") {
				print "CHECKED" if $EMAIL_OPTION{$w . 'address'} == 2;
			} else {
				print "CHECKED" if $wu eq "user";
			}
			print "> Do not use this parameter";
			print "<P>\n";
		}
		print "</BLOCKQUOTE>\n";
	}
	print "<B>Reply by E-mail</B><BLOCKQUOTE>Properly set up mail servers can accept Discus postings\n";
	print "by e-mail to the address you have specified above.  For help in configuring your system\n";
	print "for e-mail reply, you must consult <A HREF=http://www.discusware.com/support/resources/reply TARGET=_blank>";
	print "DiscusWare's resources</A> regarding this feature.<P>\n";
	print "<INPUT TYPE=RADIO NAME=reply_email VALUE=0";
	print " CHECKED" if $EMAIL_OPTION{'reply_email'} == 0;
	print "> Feature disabled<BR>\n";
	print "<INPUT TYPE=RADIO NAME=reply_email VALUE=1";
	print " CHECKED" if $EMAIL_OPTION{'reply_email'} == 1;
	print "> Feature enabled</BLOCKQUOTE><P>\n";
	if ($pro) {
		print "<BLOCKQUOTE>Moderators can also enter administrative commands by e-mail using a special format.\n";
		print "Commands will be parsed according to the parameters you enter on the subsequent screen.\n";
		print "Consult <A HREF=http://www.discusware.com/support/resources/reply TARGET=_blank>DiscusWare's ";
		print "resources</A> regarding this feature.<P>\n";
		print "<INPUT TYPE=CHECKBOX NAME=command_email VALUE=1";
		print " CHECKED" if $EMAIL_OPTION{'command_email'};
		print "> Enable administration by e-mail (<A HREF=$cgiurl3?username=$superuser&action=adm_eml_config>";
		print "Configure</A>)<P></BLOCKQUOTE>\n";
	}
	print "<P><BLOCKQUOTE>When using reply by e-mail, if a user has not provided proper username and password data,\n";
	print "the program will identify the user based on the name and e-mail address in the &quot;From&quot; header.\n";
	print "Select the desired method for this identification:<P>\n";
	print "<INPUT TYPE=RADIO NAME=id_method VALUE=0";
	print " CHECKED" if $EMAIL_OPTION{'id_method'} == 0;
	print "> Use the 'From' name as the name and the 'From' e-mail as the e-mail address<BR>\n";
	print "<INPUT TYPE=RADIO NAME=id_method VALUE=1";
	print " CHECKED" if $EMAIL_OPTION{'id_method'} == 1;
	print "> Use the 'From' name as the name but do not enter the e-mail address<BR>\n";
	print "<INPUT TYPE=RADIO NAME=id_method VALUE=2";
	print " CHECKED" if $EMAIL_OPTION{'id_method'} == 2;
	print "> Enter the post as 'Anonymous' (permitted topics only)<BR>\n";
	print "<INPUT TYPE=RADIO NAME=id_method VALUE=3";
	print " CHECKED" if $EMAIL_OPTION{'id_method'} == 3;
	print "> Do not accept any posts from unauthenticated sources</BLOCKQUOTE><P>\n";
	print "<B>Test Parameters</B><BLOCKQUOTE>You can test the parameters you specified above\n";
	print "by sending a sample message to yourself.  Enter your e-mail address in the blank\n";
	print "below and then click the &quot;Test&quot; button.  If you get an error message of any kind, then\n";
	print "this is not correctly configured.<P>\n";
	print "Your Address:</FONT>  <INPUT TYPE=TEXT NAME=testemail SIZE=35 VALUE='";
	if ($contact =~ m|<A HREF="?mailto:([^">]+)"?|) {
		print $1;
	}
	print "'>$fs\n";
	if ($^O eq "MSWin32") {
		print "<BR><INPUT TYPE=checkbox VALUE=1 NAME=debugnow> Debug mail settings\n";
	}

	print "<P><INPUT TYPE=SUBMIT NAME=submit VALUE=Test>\n";

	print "</BLOCKQUOTE><P>\n";
	print "<B>Save Parameters</B><BLOCKQUOTE>When you know that your parameters above work properly,\n";
	print "you can save them to begin using them immediately.<P>\n";
	print "<INPUT TYPE=SUBMIT NAME=submit VALUE=\"Save Settings\"><P>";
	print "</TD></TR></TABLE></FORM></BODY></HTML>\n";
	exit(0);
}

sub auto_configure_email {
	# This automatically configures e-mail notification if the email.txt file does
	# not contain the necessary information.

	# If email.txt is good, we exit

	if (open(EMAIL, "$admin_dir/email.txt")) {
		@email = <EMAIL>;
		close (EMAIL);
		if (grep(/^command_line=/, @email)) {
			return 1;
		}
	}

	undef %EMT_params;

	# See if there is a send_mail.conf file

	if (open(SMC, "$admin_dir/send_mail.conf")) {
		@smc = <SMC>;
		close (SMC);
		undef @res;
		foreach $line (@smc) {
			$line =~ s/#(.*)//;
			$line =~ s/^\s+//;
			$line =~ s/\s+$//;
			next if $line !~ /\S/;
			next if $line =~ /^#/;
			$line =~ s/\s+/ /g;
			push (@res, $line);
		}
		foreach $line (@res) {
			$line =~ m|^(\w+)\s?=\s?(.*)|; ($varname, $param) = ($1, $2);
			$P{$varname} = $param;
		}
		if ($P{'program_path'}) {
			$EMT_params{'command_line'} = "| $P{'program_path'}";
			$EMT_params{'mail_program'} = $P{'program_path'};
			if ($P{'command_line'}) {
				$EMT_params{'command_line'} .= " $P{'command_line'}";
				$EMT_params{'commandline'} = $P{'command_line'};
			}
		}
		if ($P{'from'}) {
			if ($P{'from'} =~ m|([\w\-\+\.]+)\@([\w\-\+\.]+)|) {
				$EMT_params{'fromaddr'} = "$1\@$2";
				$EMT_params{'fromname'} = join("", $`, $');
				$EMT_params{'fromname'} =~ s/[^\w\s]//g;
			} else {
				$EMT_params{'fromname'} = $P{'from'};
				$EMT_params{'fromaddr'} = "";
			}
		} else {
			$EMT_params{'fromname'} = $titlej;
			$EMT_params{'fromaddr'} = "";
		}
		if ($P{'subject'}) {
			$EMT_params{'static'} = $P{'subject'};
		} else {
			$EMT_params{'static'} = "E-mail Notification of Post";
		}
		if ($P{'from_name'}) {
			if ($P{'from_name'} =~ m|<(.*)>|) {
				$EMT_params{'fromaddress'} = 0;
				$EMT_params{'fromaddress_prefix'} = $1;
				$EMT_params{'fromaddress_switch'} = "";
				$EMT_params{'input_stream'} .= "$1: \$FROM\n";
			} else {
				$EMT_params{'fromaddress'} = 1;
				$EMT_params{'fromaddress_prefix'} = "";
				$EMT_params{'fromaddress_switch'} = "-$P{'from_name'}";
				$EMT_params{'command_line'} .= " -$P{'from_name'} '\$FROM'";
			}
		}
		if ($P{'subj'}) {
			if ($P{'subj'} =~ m|<(.*)>|) {
				$EMT_params{'subjectaddress'} = 0;
				$EMT_params{'subjectaddress_prefix'} = $1;
				$EMT_params{'subjectaddress_switch'} = "";
				$EMT_params{'input_stream'} .= "$1: \$SUBJECT\n";
			} else {
				$EMT_params{'subjectaddress'} = 1;
				$EMT_params{'subjectaddress_prefix'} = "";
				$EMT_params{'subjectaddress_switch'} = "-$P{'subj'}";
				$EMT_params{'command_line'} .= " -$P{'subj'} '\$SUBJECT'";
			}
		}
		if ($P{'to'}) {
			if ($P{'to'} =~ m|<(.*)>|) {
				$EMT_params{'toaddress'} = 0;
				$EMT_params{'toaddress_prefix'} = $1;
				$EMT_params{'toaddress_switch'} = "";
				$EMT_params{'input_stream'} .= "$1: \$TO\n";
			} else {
				$EMT_params{'toaddress'} = 1;
				$EMT_params{'toaddress_prefix'} = "";
				$EMT_params{'toaddress_switch'} = "-$P{'to'}" if $P{'to'} ne "";
				$EMT_params{'command_line'} .= " -$P{'to'} '\$TO'" if $P{'to'} ne "";
				$EMT_params{'command_line'} .= " \$TO" if $P{'to'} eq "";
			}
		}
	}
	if ($EMT_params{'mail_program'} eq "") {
		if ($mailprog =~ m|/sendmail|) {
			$EMT_params{'command_line'} = "| $mailprog -t";
			$EMT_params{'mail_program'} = $mailprog;
			$EMT_params{'commandline'} = "-t";
			$EMT_params{'toaddress'} = 0;
			$EMT_params{'toaddress_prefix'} = "To:";
			$EMT_params{'toaddress_switch'} = "";
			$EMT_params{'fromaddress'} = 0;
			$EMT_params{'fromaddress_prefix'} = "From:";
			$EMT_params{'fromaddress_switch'} = "";
			$EMT_params{'subjectaddress'} = 0;
			$EMT_params{'subjectaddress_prefix'} = "Subject:";
			$EMT_params{'subjectaddress_switch'} = "";
			$EMT_params{'input_stream'} = "To: \$TO\nFrom: \$FROM\nSubject: \$SUBJECT\n";
		} elsif ($mailprog =~ m|/mail|) {
			$EMT_params{'command_line'} = "| $mailprog -s '\$SUBJECT' \$TO";
			$EMT_params{'mail_program'} = $mailprog;
			$EMT_params{'commandline'} = "";
			$EMT_params{'toaddress'} = 1;
			$EMT_params{'toaddress_prefix'} = "To:";
			$EMT_params{'toaddress_switch'} = "";
			$EMT_params{'fromaddress'} = 2;
			$EMT_params{'fromaddress_prefix'} = "";
			$EMT_params{'fromaddress_switch'} = "";
			$EMT_params{'subjectaddress'} = 1;
			$EMT_params{'subjectaddress_prefix'} = "";
			$EMT_params{'subjectaddress_switch'} = "-s";
		}
	}
	if ($EMT_params{'mail_program'} eq "") {
		@sp = ("/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", "/usr/local/sbin", "/usr/contrib/bin", "/usr/contrib/sbin", "/usr/lib", "/lib", "/etc", "/");
		foreach $p (@sp) {
			if (-x "$p/sendmail") {
				$EMT_params{'command_line'} = "| $p/sendmail -t";
				$EMT_params{'mail_program'} = "$p/sendmail";
				$EMT_params{'commandline'} = "-t";
				$EMT_params{'toaddress'} = 0;
				$EMT_params{'toaddress_prefix'} = "To:";
				$EMT_params{'toaddress_switch'} = "";
				$EMT_params{'fromaddress'} = 0;
				$EMT_params{'fromaddress_prefix'} = "From:";
				$EMT_params{'fromaddress_switch'} = "";
				$EMT_params{'subjectaddress'} = 0;
				$EMT_params{'subjectaddress_prefix'} = "Subject:";
				$EMT_params{'subjectaddress_switch'} = "";
				$EMT_params{'input_stream'} = "To: \$TO\nFrom: \$FROM\nSubject: \$SUBJECT\n";
				last;
			}
		}
	}
	if ($EMT_params{'mail_program'} eq "") {
		@sp = ("/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", "/usr/local/sbin", "/usr/contrib/bin", "/usr/contrib/sbin", "/usr/lib", "/lib", "/etc", "/");
		foreach $p (@sp) {
			if (-x "$p/mail") {
				$EMT_params{'command_line'} = "| $p/mail -s '\$SUBJECT' \$TO";
				$EMT_params{'mail_program'} = "$p/mail";
				$EMT_params{'commandline'} = "";
				$EMT_params{'toaddress'} = 1;
				$EMT_params{'toaddress_prefix'} = "TO:";
				$EMT_params{'toaddress_switch'} = "";
				$EMT_params{'fromaddress'} = 2;
				$EMT_params{'fromaddress_prefix'} = "";
				$EMT_params{'fromaddress_switch'} = "";
				$EMT_params{'subjectaddress'} = 1;
				$EMT_params{'subjectaddress_prefix'} = "";
				$EMT_params{'subjectaddress_switch'} = "-s";
			}
		}
	}
	$EMT_params{'1message'} = 0;
	$EMT_params{'sendtext'} = 1;
	$EMT_params{'tempfile'} = 0;
	$EMT_params{'subjectline'} = 0;
	if ($EMT_params{'fromaddr'} eq "") {
		if ($contact =~ m|<A HREF="mailto:([^"]+)"|) {
			$EMT_params{'fromaddr'} = $1;
		}
	}
	$EMT_params{'fromname'} = &JavaScript_prepare($title) if $EMT_params{'fromname'} eq "";
	if ($FORM{'fromaddr'} eq "") {
		$fromstring = "\"$EMT_params{'fromname'}\" <nobody\@localhost>";
	} else {
		$fromstring = "\"$EMT_params{'fromname'}\" <$EMT_params{'fromaddr'}>";
	}
	$EMT_params{'command_line'} =~ s/\$FROM/$fromstring/g;
	$EMT_params{'input_stream'} =~ s/\$FROM/$fromstring/g;
	$EMT_params{'static'} = "E-mail Notification of Post" if $EMT_params{'static'} eq "";
	$EMT_params{'input_stream'} = &escape($EMT_params{'input_stream'});
	&lock("auto_configure_email", "$admin_dir/email.txt");
	open (EMAIL, ">$admin_dir/email.txt");
	foreach $key (keys(%EMT_params)) {
		print EMAIL "$key=$EMT_params{$key}\n";
	}
	close (EMAIL);
	&unlock("auto_configure_email", "$admin_dir/email.txt");
}

# END - FILE IS CORRECTLY UPLOADED #
