package DADA::MailingList::Settings; 
use strict;
use lib qw(./ ../ ../../ ../../DADA ../perllib); 

use Carp qw(croak carp); 

my $type; 
use DADA::Config qw(!:DEFAULT); 	
BEGIN { 
	$type = $DADA::Config::SETTINGS_DB_TYPE;
	if($type =~ m/sql/i){ 
	 	if ($DADA::Config::SQL_PARAMS{dbtype} eq 'mysql'){ 
			$type = 'MySQL';
		}
		elsif ($DADA::Config::SQL_PARAMS{dbtype} eq 'Pg'){ 		
				$type = 'PostgreSQL';
		}
		elsif ($DADA::Config::SQL_PARAMS{dbtype} eq 'SQLite'){ 
			$type = 'SQLite';
		}
	}
}
use base "DADA::MailingList::Settings::$type";


sub _init  { 
    my $self   = shift; 
	my ($args) = @_; 

	
    if($args->{-new_list} == 1){ 
	
		$self->{name} = $args->{-list};
		# $self->{local_li} = $self->get;
		# warn 'in init: $args->{-list} ' . $args->{-list}; 	
	}else{ 
		
		if($self->_list_name_check($args->{-list}) == 0) { 
    		croak('BAD List name "' . $args->{-list} . '" ' . $!);
		}
		else { 
			#$self->{local_li} = $self->get; 
		}		
		
	}

}




sub get { 
	my $self = shift; 
	my %args = (
	    -Format => "raw", 
	    -dotted => 0, 
	    @_
	); 

	$self->_raw_db_hash;
	
	my $ls                   = $self->{RAW_DB_HASH}; 
	
	$ls = $self->post_process_get($ls, {%args});
	
	if($args{-dotted} == 1){ 
        my $new_ls = {}; 
        while (my ($k, $v) = each(%$ls)){
            $new_ls->{'list_settings.' . $k} = $v; 
        }
        # Will $ls undef every ref to it to?!
        #undef($ls); 
        
        return $new_ls; 

	}
	else { 
			
	    return $ls; 
    }
}




sub post_process_get { 

	my $self    = shift; 
	my $ls      = shift; 
	my $args    = shift; 
		
	carp "$DADA::Config::PROGRAM_NAME $DADA::Config::VER warning! List " . $self->{function} . " db empty!  List setting DB Possibly corrupted!"
		unless keys %$ls; 
		
	carp "$DADA::Config::PROGRAM_NAME $DADA::Config::VER warning! no listshortname saved in list " . $self->{function} . " db! List " . $self->{function} . " DB Possibly corrupted!" 
		if ! $ls->{list};
	
	carp "listshortname in db, '" . $self->{name} . "' does not match saved list shortname: '" . $ls->{list} . "'"
		if $self->{name} ne $ls->{list};
		

	if($args->{-Format} ne 'unmunged'){ 
	   $ls->{charset_value}  = $self->_munge_charset(        $ls);
	   $ls                   = $self->_munge_for_deprecated($ls); 	
	
        
       if(!exists($ls->{list_info})){ 
            $ls->{list_info} =  $ls->{info};
       }



	   # sasl_smtp_password
	   # pop3_password
	    
	   # If we don't need to load, DADA::Security::Password, let's not. 
	   
	   my $d_password_check = 0; 
	   foreach ('sasl_smtp_password', 'pop3_password', 'discussion_pop_password'){ 
	        if(exists($DADA::Config::LIST_SETUP_DEFAULTS{$_}) || exists($DADA::Config::LIST_SETUP_OVERRIDES {$_})){ 
	            $d_password_check = 1; 
	            require DADA::Security::Password; 
	            last; 
	        }
	   }
	   
	   foreach ('sasl_smtp_password', 'pop3_password', 'discussion_pop_password'){ 
	        
	        if($DADA::Config::LIST_SETUP_OVERRIDES{$_}){ 
	         
	             $self->{orig}->{LIST_SETUP_OVERRIDES}->{$_} = $DADA::Config::LIST_SETUP_OVERRIDES {$_}; 
	             $DADA::Config::LIST_SETUP_OVERRIDES {$_} = DADA::Security::Password::cipher_encrypt($ls->{cipher_key},$DADA::Config::LIST_SETUP_OVERRIDES {$_}); 
	             next;	            
	        }
	        
	        if($DADA::Config::LIST_SETUP_DEFAULTS{$_}){ 
	            if(!$ls->{$_}){ 
	                $self->{orig}->{LIST_SETUP_DEFAULTS}->{$_} = $DADA::Config::LIST_SETUP_DEFAULTS {$_}; 
	                $DADA::Config::LIST_SETUP_DEFAULTS{$_} = DADA::Security::Password::cipher_encrypt($ls->{cipher_key},$DADA::Config::LIST_SETUP_DEFAULTS {$_}); 
	            }
	        }
	   }
	
		foreach(keys %$ls){ 
		    if (exists($ls->{$_})){ 
	    
		        if(!defined($ls->{$_})){ 
	                    
	                delete($ls->{$_}); 
	            }
		    }
		}
	
		foreach(keys %DADA::Config::LIST_SETUP_DEFAULTS){
	
			if(! exists($ls->{$_}) || length($ls->{$_}) == 0){ 
				$ls->{$_} = $DADA::Config::LIST_SETUP_DEFAULTS{$_};
			}
		}

	    $DADA::Config::SUBSCRIPTION_QUOTA ||= undef; 

	    if(
	        $DADA::Config::SUBSCRIPTION_QUOTA       && 
	        $ls->{subscription_quota}               && 
	       ($ls->{subscription_quota}                > $DADA::Config::SUBSCRIPTION_QUOTA)
	      )
	    { 
	        $ls->{subscription_quota} = $DADA::Config::SUBSCRIPTION_QUOTA; 
	    }
      
	    foreach ('sasl_smtp_password', 'pop3_password', 'discussion_pop_password'){ 
	        if($DADA::Config::LIST_SETUP_OVERRIDES {$_}){ 
	            $DADA::Config::LIST_SETUP_OVERRIDES {$_} = $self->{orig}->{LIST_SETUP_OVERRIDES}->{$_};
	        }
        
	        if($DADA::Config::LIST_SETUP_DEFAULTS {$_}){ 
	            $DADA::Config::LIST_SETUP_DEFAULTS {$_} = $self->{orig}->{LIST_SETUP_DEFAULTS}->{$_};
	        }
	    }
	}


# And then, there's this: 
# DEV: Strange, that it's been left out? Did it get removed? 
foreach(keys %DADA::Config::LIST_SETUP_OVERRIDES){
	next if $_ eq 'sasl_smtp_password';
	next if $_ eq 'pop3_password';
	next if $_ eq 'discussion_pop_password';
	$ls->{$_} = $DADA::Config::LIST_SETUP_OVERRIDES{$_};

}





# This just causes all sorts of warnings. Uggh. 
no warnings;	
	if(!exists($ls->{admin_email})){ 
		$ls->{admin_email} = $ls->{list_owner_email}; 
	}elsif($ls->{admin_email} eq undef){ 
		$ls->{admin_email} = $ls->{list_owner_email}; 		
	}
use warnings; 

	return $ls; 

}



sub params { 
	
	my $self = shift; 
	
	if(keys %{$self->{local_li}}){ 
		#... 
	}
	else { 
		$self->{local_li} = $self->get; 
	}
	
	return $self->{local_li};
	
}



sub param { 
	
	my $self  = shift; 
	my $name  = shift  || undef; 
	my $value = shift;
	
	if(!defined($name)){ 
		croak "You MUST pass a name as the first argument!"; 
	}
	
	if(keys %{$self->{local_li}}){ 
		#... 
	}
	else { 
		$self->{local_li} = $self->get; 
	}
	
	if(defined($value)){ 
		
		if(!exists($DADA::Config::LIST_SETUP_DEFAULTS{$name})){ 
			croak "Cannot call param() on unknown setting."; 
		}
		else { 
				
			$self->save({$name => $value});
			$self->{local_li} = {};
			return $value; # or... what should I return?
		}
	}
	else { 
	
		return $self->{local_li}->{$name};
		
	}
	
	
}




sub _existence_check { 

    my $self = shift; 
    my $li   = shift; 
    
    foreach(keys %$li){ 
        #next if $_ eq 'list';
        
        if(!exists($DADA::Config::LIST_SETUP_DEFAULTS{$_})){ 
        
            croak("Attempt to save a unregistered setting - $_"); 
        
        }
           
    }



}





sub _format_settings { 
	# I don't like this at all. 
	
	my ($self, $ls) = @_; 
	  for($ls->{subscribed_message},
		  $ls->{unsubscribed_message},
		  $ls->{confirmation_message},
		  $ls->{mailing_list_message},
		  $ls->{not_allowed_to_post_message},
		  $ls->{html_confirmation_message},
		  $ls->{html_unsub_confirmation_message},
		  $ls->{html_subscribed_message},
		  $ls->{html_unsubscribed_message},
		  $ls->{unsub_confirmation_message}
		  ){ 
			$_ =~ s/\[list\]/$ls->{list}/g;
			$_ =~ s/\[list_name\]/$ls->{list_name}/g;
			$_ =~ s/\[list_info\]/$ls->{info}/g;
			$_ =~ s/\[list_owner_email\]/$ls->{list_owner_email}/g;
			$_ =~ s/\[list_admin_email\]/$ls->{admin_email}/g; 
			$_ =~ s/\[privacy_policy\]/$ls->{privacy_policy}/g; 
			$_ =~ s/\[list_privacy_policy\]/$ls->{privacy_policy}/g;			
			$_ =~ s/\[physical_address\]/$ls->{physical_address}/g;
			$_ =~ s/\[program_url\]/$DADA::Config::PROGRAM_URL/g; 
		}
	return $ls;
}






sub _munge_charset { 
	my ($self, $li) = @_;
	
	
	if(!exists($li->{charset})){ 
	   $li->{charset} =  $DADA::Config::LIST_SETUP_DEFAULTS{charset};
	    
	}
	
	my $charset_info = $li->{charset};
	my @labeled_charsets = split(/\t/, $charset_info);	
	return $labeled_charsets[$#labeled_charsets];      

}



sub _munge_for_deprecated { 
	
	my ($self, $li) = @_; 
	$li->{list_owner_email} ||= $li->{mojo_email};
#    $li->{admin_email}      ||= $li->{list_owner_email}; 
  
    $li->{privacy_policy}   ||= $li->{private_policy};
  
	#we're talkin' way back here..
	
	if(!exists($li->{list_name})){ 
		$li->{list_name} = $li->{list}; 
		$li->{list_name} =~ s/_/ /g;
	}
	
	return $li; 
}




sub _trim { 
	my ($self, $s) = @_;
	return DADA::App::Guts::strip($s);
}





1; 


=pod

=head1 COPYRIGHT 

Copyright (c) 1999-2008 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 
