#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#this is the don philippe email tools package

package donemail;
########################################################################################################
#tries to find sendmail; returns it.
sub find_sendmail{
	my @possibilities = ('/usr/bin/sendmail','/usr/sbin/sendmail','/usr/lib/sendmail','/usr/local/lib/sendmail','/bin/sendmail','/sbin/sendmail');
	foreach my $possibility (@possibilities){
		if(-x $possibility){
			return $possibility;
		};
	};
	return "";
};

########################################################################################################
#sends email. $_[0] must contain sendmail including the path, $_[1] must contain 
#a valid e-mail that includes header and footer, as given to sendmail on the console.

sub send_mail{
	my $mailprog = $_[0];
	open(SMAIL, "|$mailprog -t");
	my $mailtxt = add_header_line($_[1],"X-MAILER: Don Philippe Libraries by Philipp Kindt, www.Pagenews.dd.vu");	
	
	#insert carriage returns if missing!
	$mailtxt =~ s/([^\r]){1}\n/$1\r\n/g;	
	print SMAIL $mailtxt;
	if(!(close(SMAIL))){
		&donstdlib::error("donextra_email_invalid_sendmail_path",$_[0]);
	};
	open(S,"> mail.txt");
	print S $mailtxt;
	close(S);
};

########################################################################################################
#adds line $_[1] to the header of email text $_[0], returns new email.
#line must NOT contain a newline at the end.
#disactivated at the moment.
sub add_header_line{
	my $txt = $_[0];
	(my $header, my $footer) = split(/(\r)?\n(\r)?\n/,$txt,1);
		$header .= "\n".$_[1];
		return $_[0];
		

};

########################################################################################################
#checks if adress $_[0] is valid. Error message if not.
sub check_address{
	if($_[0] !~ /\@/){
		donstdlib::error("donnewsletter_invalidaddress",$_[0]);
	};
	if($_[0] !~ /\./){
		donstdlib::error("donnewsletter_invalidaddress",$_[0]);
	};
	
};

########################################################################################################
#sends mail template $_[0];
#$_[1] must be the path to sendmail if $_[2] contains a reference on an array of adresses,
#the template gets a new handler [DONEMAIL_ADDRESSES] that contains a list
# of adresses separated by ", " each, so it is suitable for fiting in a
#TO-field, for example.
#if you just want to send the mail to ONE SINGLE RECEIPIENT,
#put his maildata block as a reference in $_[3]
#you can also use $_[3] to add more [DONEMAIL_*] handlers
#(also for more than one receipients[ by just gving a refernce on a hash
#may contain a task specification ("user","internal") $_[4]
#you will have all mail data in the template by using the handlers [DONEMAIL_*] where * can be
# 'address' for example

sub send_mail_template{
	my $path;	
	if($_[4] eq ""){
		$path = "user"
	}else{
		$path = $_[4];	
	}
	my $parser = donparser->new(dntemplatemanager::determine_path($path));
	$parser->set_option_manually("line_by_line","yes");
	$parser->set_option_manually("remove_silly_lines","yes");

	$parser->open($_[0]);
	my $addrlist;
	if(@{$_[2]} > 0){
		my $addrlist;
		foreach my $addr (@{$_[2]}){
			$addrlist .= $addr.", ";
		};
		#cut away last appendix
		chop($addrlist);
		chop($addrlist);
		$parser->add_handler("DONEMAIL_ADDRESSES",$addrlist);
	};
	if(ref($_[3])){
		foreach my $key (keys(%{$_[3]})){
			$parser->add_handler("DONEMAIL_$key",$_[3]->{$key});
		};
	};
	$parser->parse();
	send_mail($_[1],$parser->get_text());
}
1;

