package DADA::App::Messages; 

use lib qw(./ ../  ../DADA ../DADA/perllib); 

use DADA::Config qw(!:DEFAULT); 
use DADA::App::Guts; 

require Exporter; 
@ISA = qw(Exporter); 

@EXPORT = qw(

send_confirmation_message
send_unsub_confirm_email

send_unsubscription_email
send_subscribed_message

send_owner_happenings


send_newest_archive


); 

use strict; 
use vars qw(@EXPORT); 


sub send_confirmation_message { 

	my %args = (
			-List         => undef, 
			-Email        => undef, 
			-Settings_obj => undef, 
			@_		
	);
	
	my $li    = $args{-Settings_obj}->get;
	my $email = $args{-Email}; 
	my $list  = $args{-List}; 

	my $Body =  $li->{confirmation_message}; 
		
    
    my $headers = {To              => '"'. escape_for_sending($li->{list_name}) .'" <'. $email .'>', , 
                   Subject         => $li->{list_name} . ' Mailing List Confirmation', 
                  };
    
    my $msg = undef; 

       $msg .= $_ . ': ' . $headers->{$_} . "\n"
        foreach(keys %$headers); 
       $msg .= "\n" . $Body; 
        
    require DADA::App::FormatMessages; 
    my $fm = DADA::App::FormatMessages->new(-List => $list); 
       $fm->use_header_info(1);
       $fm->use_email_templates(0); 
       
    my ($header_str, $body_str) = $fm->format_headers_and_body(-msg => $msg );
    
    require DADA::Mail::Send;  
    my $mh = DADA::Mail::Send->new($li); 

    $mh->send($mh->return_headers($header_str), 
              Body => $body_str,
             ); 
				 
}




sub send_unsub_confirm_email { 

	my %args = (
			-List         => undef, 
			-Email        => undef, 
			-Settings_obj => undef, 
			@_	
		); 
		
	my $email = $args{-Email}; 
	my $list  = $args{-List}; 
	
	if (! $args{-Settings_obj}){
		require DADA::MailingList::Settings; 
		$args{-Settings_obj} = DADA::MailingList::Settings->new(-LIst => $args{-List}); 
	}
	my $li = $args{-Settings_obj}->get; # yeah, I have no idea why this is being passed...

	my $headers = { 
					To              =>    $email, 
					Subject         =>    $li->{list_name} . ' Mailing List Confirmation', 
				  }; 
	my $msg = undef; 
	
	$msg .= $_ . ': ' . $headers->{$_} . "\n"
	   	foreach(keys %$headers); 
	$msg .= "\n" . $li->{unsub_confirmation_message};
	
	require DADA::App::FormatMessages; 
	my $fm = DADA::App::FormatMessages->new(-List => $list); 
	   $fm->use_header_info(1);
	   $fm->use_email_templates(0); 
	   
	my ($header_str, $body_str) = $fm->format_headers_and_body(-msg => $msg );
	
	require DADA::Mail::Send;  
	my $mh = DADA::Mail::Send->new($li); 

	$mh->send($mh->return_headers($header_str), 
		      Body => $body_str,
		     ); 
}




sub send_generic_email { 

	my %args = (
			-List         => undef, 
			-Email        => undef, 
			-Settings_obj => undef,
			-Subject      => '', 
			-Message      => '', 
			@_	
		); 
		
	my $email = $args{-Email}; 
	my $list  = $args{-List}; 
	
	if (! $args{-Settings_obj}){
		require DADA::MailingList::Settings; 
		$args{-Settings_obj} = DADA::MailingList::Settings->new(-List => $args{-List}); 
	}
	my $li = $args{-Settings_obj}->get; # yeah, I have no idea why this is being passed...

    $args{-Subject} =~ s/\[list_name\]/$li->{list_name}/g; # should be everything... but... meh.
    
	my $headers = { 
					To              =>    $email, 
					Subject         =>    $args{-Subject}, 
				  }; 

	my $msg = undef; 
	
	$msg .= $_ . ': ' . $headers->{$_} . "\n"
	   	foreach(keys %$headers); 
	$msg .= "\n" . $args{-Message};
	
	require  DADA::App::FormatMessages; 
	my $fm = DADA::App::FormatMessages->new(-List => $list); 
	   $fm->use_header_info(1);
	   $fm->use_email_templates(0); 
	   
	my ($header_str, $body_str) = $fm->format_headers_and_body(-msg => $msg );
	
	require DADA::Mail::Send;  
	my $mh = DADA::Mail::Send->new($li); 

	$mh->send(
	   $mh->return_headers($header_str), 
	   Body => $body_str,
    ); 

}




sub send_unsubscription_email{ 

	my %args = (-List      => undef,
				-Email     => undef,
				-List_Info => undef,
				@_);

	my $li    = $args{-List_Info};
	my $email = $args{-Email}; 
	my $list  = $args{-List}; 
	
	my $headers = { To              =>   $email, 
					Subject         =>   $li->{list_name} . ' Unsubscription', 
				  }; 
	
	my $msg = undef; 
	
	$msg .= $_ . ': ' . $headers->{$_} . "\n"
	   	foreach(keys %$headers); 
	$msg .= "\n" . $li->{unsubscribed_message};
	
	require DADA::App::FormatMessages;
	
	my $fm = DADA::App::FormatMessages->new(-List => $li->{list}); 
	   $fm->use_header_info(1);
	   $fm->use_email_templates(0); 
	   
	my ($header_str, $body_str) = $fm->format_headers_and_body( -msg => $msg );
	
	require DADA::Mail::Send;  
	my $mh = DADA::Mail::Send->new($li); 

	$mh->send($mh->return_headers($header_str), 
		      Body => $body_str,
		     ); 
		     
}



sub send_subscribed_message { 

	my %args = (
	-List         => undef, 
	-Email        => undef, 
	-Settings_obj => undef, 
	@_	
	); 

	my $email = $args{-Email}; 
	my $list  = $args{-List}; 
	
	if (! $args{-Settings_obj}){
		require DADA::MailingList::Settings; 
		$args{-Settings_obj} = DADA::MailingList::Settings->new(-LIst => $args{-List}); 
	}
	my $li = $args{-Settings_obj}->get; # yeah, I have no idea why this is being passed...
	

	my $headers = {
					To            => "\"New Subscriber\" <$email>", 
					Subject       => "Welcome to " . $li->{list_name}, 
				   };
				   
	my $msg  = undef; 
	   $msg .= $_ . ': ' . $headers->{$_} . "\n"
		foreach(keys %$headers); 
	   
	   $msg .= "\n" . $li->{subscribed_message};
		
	require DADA::App::FormatMessages; 
	
	my $fm = DADA::App::FormatMessages->new(-List => $list); 
	   $fm->use_header_info(1);
	   $fm->use_email_templates(0); 
	   
	my ($header_str, $body_str) = $fm->format_headers_and_body(-msg => $msg );
	
	require DADA::Mail::Send;  
	my $mh = DADA::Mail::Send->new($li); 

	$mh->send($mh->return_headers($header_str), 
			  Body => $body_str,
			 ); 
				 
}




sub send_owner_happenings { 

	my $list    = shift; 
	my $email   = shift; 
	my $status  = shift; 
	my $lh_obj  = shift || undef; # bad form..
	my $ls_obj  = shift || undef; # bad form..
	my $note    = shift || '';    # gah.

	my $send_it = 1; 
	
	my $ls; 
	if($ls_obj){ 
		$ls = $ls_obj; 	
	}else{ 
		require  DADA::MailingList::Settings; 
		   $ls = DADA::MailingList::Settings->new(-List => $list); 
	}
	
	my $li = $ls->get; 
	
	if($status eq "subscribed"){  
	   if(exists($li->{get_sub_notice})){ 
		 if($li->{get_sub_notice} == 0){         
			$send_it = 0; 
			}
		 }   
	   }elsif($status eq "unsubscribed"){  
		if(exists($li->{get_unsub_notice})){ 
		  if($li->{get_unsub_notice} == 0){ 
			$send_it = 0; 
		   }
		}   
	  } 
		
	if($send_it == 1){ 
	
		my $lh;		
		if($lh_obj){ 
			$lh = $lh_obj; 
		}else{ 
			$lh = DADA::MailingList::Subscribers->new(-List => $list); 
		}
		
							   
		my $num_subscribers = $lh->num_subscribers;   
		
		my $localtime = scalar(localtime());

my $Body = qq{

Hello,
This is a quick note to say that 
$email has $status on list: $li->{list_name} 

Server Time: $localtime
IP Logged:   $ENV{'REMOTE_ADDR'}
$note

There is now a total of: $num_subscribers subscribers.

-$DADA::Config::PROGRAM_NAME

};
		require DADA::Mail::Send;  
		my $mh = DADA::Mail::Send->new($li);
		my %mailing = ( 
		'Reply-To'     => $email, 
		To             =>  '"List Owner for: '. escape_for_sending($li->{list_name}) .'" <'. $li->{list_owner_email} .'>', 
		Subject        => "$status $email",
		Body           => $Body,
		);  
		
		$mh->send(%mailing); 	
	}
}




sub send_newest_archive { 

	my %args = (-List         => undef, 
				-Email        => undef, 
				-Settings_obj => undef, 
				@_); 

	die "no list!"         if ! $args{-List}; 
	die "no email!"        if ! $args{-Email}; 
	die "no settings obj!" if ! $args{-Settings_obj};
	
	my $ls = $args{-Settings_obj}; 
	my $li = $ls->get; 
	
	require DADA::MailingList::Archives; 
	my $ah = DADA::MailingList::Archives->new(-List => $li); 
	
	my @a = $ah->get_available_archives; 


    
    my $newest_entry = $ah->newest_entry; 

	if(defined($newest_entry) && ($newest_entry > 1)){ 
		
		my ($head, $body) = $ah->massage_msg_for_resending(-key => $newest_entry, '-split' => 1);
		
		require DADA::Mail::Send; 
		my $mh = DADA::Mail::Send->new($li);
		
		
		
		$mh->send(
				  $mh->return_headers($head), 
				  To => $args{-Email}, 
				  Body => $body, 
		); 	
		
	}
	return 1; 

}






1;


=pod

=head1 COPYRIGHT 

Copyright (c) 1999-2007 Justin Simoni All rights reserved. 

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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, 
Boston, MA  02111-1307, USA.

=cut 

