package ISPMan;
use AutoLoader 'AUTOLOAD';
use strict;
use Text::Template;
use ISPMan::IMAP;
use ISPMan::LDAP;
use ISPMan::Utils;
use ISPMan::Config;
use ISPMan::UserMan;
use ISPMan::DNSMan;
use ISPMan::ApacheMan;
use CGI::Carp "fatalsToBrowser"; 


use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $ldap $imap $Config);

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw();
$VERSION = '0.01';






sub new {
   my $proto = shift;
   my $class = ref($proto) || $proto;   
   my $self;
   
   
   my $confDir=shift || "/usr/pkg/etc/ispman";
   $Config=ISPMan::Config->new($confDir);
   $self->{'Config'}=$Config;

   
   
   $self->{'imap'}=ISPMan::IMAP->new();
   $self->{'ldap'}=ISPMan::LDAP->new($Config->{'ldapBaseDN'}, $Config->{'ldapHost'}, $Config->{'ldapRootDN'},$Config->{'ldapRootPass'});
   if (! $self->{'ldap'}->connected()){
         $self->{'ldap'}->connect();
   }
   

   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); 
   $year+=1900;
   $mon++;
   $mon=($mon<10)?"0$mon":$mon;

   $self->{'year'}=$year;
   $self->{'mon'}=$mon;
   $self->{'mday'}=$mday;

   return bless $self, $class;
}


sub getConf {
   my $self=shift;
   my $key=shift;
   return $self->{'Config'}{$key};
}


sub newDomain  {
   my $self=shift;
   my $r=shift;
	$self->{'ldap'}->addDomain($r, $self->{'Config'});
   print $self->refreshSignal();
}





sub deleteDomain {
   my $self=shift;
   my $r=shift;
   

   my $domain=$r->param("domain");

   #Lets be a BOFH and Delete All users MuhhahhhAAAAhaahaa
   my $users=$self->getUsers("ou=users, domain=$domain, " . $self->{'Config'}{'ldapBaseDN'});
   for (keys %$users) {
      my $dn=explodeDN($users->{$_});
      
      print "$users->{$_}<br>\n";
      my ($maildrophost)=$self->getEntry($users->{$_}, ["maildrophost"])->get_value("maildrophost");
      
      
      if ($maildrophost) {
            $self->{'imap'}->delete($dn->{'uid'}, $maildrophost);
      }
      $self->{'ldap'}->DeleteUser($users->{$_});
   }

   # Now Delete the rest of the tree.
   $self->{'ldap'}->delTree("domain=$domain, " . $self->{'Config'}{'ldapBaseDN'});

   print $self->refreshSignal();
}



sub explodeDN{
   my $dn=shift;
   my @attr=split(/\s*,\s*/ , CGI::unescape($dn));
   my $exploded;

   for (@attr){
      my ($var, $val)=split(/\s*=\s*/, $_);
      $exploded->{$var}=$val;
   }
   return $exploded;
}






sub getEntry {
   my $self=shift;
   return $self->{'ldap'}->getEntry(@_);
}



sub getEntries {
   my $self=shift;
   return $self->{'ldap'}->getEntries(@_);
}











sub getDomains{
   my $self=shift;
   my $domains=$self->{'ldap'}->getEntriesAsHashRef($Config->{'ldapBaseDN'}, "objectclass=dnsdomain", ["domain"]);
   my $dom;
   for (keys %$domains) {
      $dom->{$domains->{$_}{'domain'}}=$_;
   }
   return $dom;
}


sub getRecords {
   my $self=shift;
   my $dn=shift;
   my $type=shift;
   $dn="cn=$type, ou=dnsdata, $dn";
   my $entry=$self->getEntry($dn);
   if ($entry) {
      my @records=$entry->get_value("record");
      return \@records;
   }
}

1;
__END__
# Below is the stub of documentation for your module. You better edit it!

=head1 NAME

ISPMan - Perl module for ISPMan webAdministrator and command line utils

=head1 SYNOPSIS

  use ISPMan;
  something useful here.

=head1 DESCRIPTION

ISPMan.pm holds miscellneous functions for setting/getting informations to/from the LDAP server.
It also creates users and mail boxes.

ISPMan reads its configuration and templates from directory /usr/pkg/etc/ispman/
Make sure it is there and is protected




Simple example to fetch all domains from the ldap server.

   use ISPMan;
   my $ispman=ISPMan->new();
   my $domains=$ispman->getDomains;
   print join "\n", keys %$domains;

This will print all the domains that are in your LDAP tree.


Another example to list users from a domain

   use ISPMan;
   my $ispman=ISPMan->new();
   my $users=$ispman->getUsers("domain=sourceforge.net, o=ispman");
   print join "\n", keys %$users;


Sorry for not a lot of documentation, still cleaning the code.

=head1 AUTHOR

Atif Ghaffar, atif@developer.ch

=head1 HOMEPAGE

http://ispman.sourceforge.net (source for latest module and more documentation)



=head1 SEE ALSO

perl(1) 
IMAP::Admin(3pm) 
Text::Template(3pm) 
Net::LDAP(3pm)
Net::LDAP::Entry(3pm) 
Net::LDAP::Util(3pm).

=cut
