package IsMailMgr; use strict; use vars qw(@ISA $VERSION @EXPORT); #use diagnostics; #use Apache::Debug('level'=>7); #no strict 'subs'; #no strict 'vars'; use IsMailDb; $VERSION = "1.00"; @ISA = qw(); =head1 NAME IsMailMgr - Simple ISMail configuration manager =head1 SYNOPSIS # Create new IsMailMgr object $ismgr = new IsMailMgr(); # Add a CSV list of users $ismgr->AddList(fname) # Remove a CSV list of users $ismgr->RemoveList(fname) =head1 DESCRIPTION =head2 Methods =item new Creates a new IsMailMgr object. =item AddList(fname) Add a CSV list of users. Initial line of CSV file may specify the ordering of CSV items with keywords as: 'domain', 'description', 'name', 'password' If not present, the default order is: domain,description,name,password Example 1: domain,name,password,description dom.com,john,johnpwd,"John Doe" Example 2: name,password,description,domain john,johnpwd,"John Doe",dom.com Example 3 (no CSV item specification -- default order): dom.com,john,johnpwd,"John Doe" =item RemoveList(fname) Remove a CSV list of users: Initial line of CSV file may specify the ordering of CSV items with keywords as: 'domain', 'description', 'name', 'password' If not present, the default order is: domain,description,name,password Example 1: domain,name,password,description dom.com,john,johnpwd,"John Doe" Example 2: name,password,description,domain john,johnpwd,"John Doe",dom.com Example 3 (no CSV item specification -- default order): dom.com,john,johnpwd,"John Doe" =item AddUser(domain,desc,name,pwd) Add a user to a domain. =item RemoveUser(domain,name) Remove a user from a domain. =cut my ($ismgr, $ism); sub new { my $obref = {}; my $class = shift; $ism = new IsMailDb; return 0 if !defined($ism); bless $obref, $class; return $obref; } sub AddUser { return 0 if scalar(@_) < 5; my $ret; my $self = shift; my $domain = shift; my $udesc = shift; my $uname = shift; my $pwd = shift; # Add user $ret = $ism->AddUserToDomain($domain, $uname, 1, $udesc, 0); # Add/Change password $ret = $ism->SetUserInfo($domain, $uname, 'Password', $pwd) if $ret; return $ret; } sub RemoveUser { return 0 if scalar(@_) < 3; my $ret; my $self = shift; my $domain = shift; my $uname = shift; # Remove user $ret = $ism->RemoveUserFromDomain($domain, $uname); return $ret; } sub DoList { return 0 if scalar(@_) < 3; my ($self, $fname, $mod_type, $domain, $desc, $name, $pwd); my (%order, @hdr, $i, $found); my (@val); $self = shift; $fname = shift; $mod_type = shift; $found = 0; # Check (optional) header line for argument order specification open(OLD, "< $fname") or die "Can't open $fname: $!"; $_ = ; if (($hdr[0],$hdr[1],$hdr[2],$hdr[3]) = $_ =~ /^(.*),(.*),(.*),(.*)/) { for ($i = 0; $i < 4; $i++) { $order{'domain'} = $i if ($hdr[$i] =~ /domain/i); $order{'desc'} = $i if ($hdr[$i] =~ /desc/i); $order{'name'} = $i if ($hdr[$i] =~ /name/i); $order{'pwd'} = $i if ($hdr[$i] =~ /pass/i); } $found = 1 if (exists($order{'domain'}) and exists($order{'desc'}) and exists($order{'name'}) and exists($order{'pwd'})); } # If no header line, use default argument order if (!$found) { close(OLD); open(OLD, "< $fname") or die "Can't open $fname: $!"; $order{'domain'} = 0; $order{'desc'} = 1; $order{'name'} = 2; $order{'pwd'} = 3; } while () { chomp($_); if (($val[0],$val[1],$val[2],$val[3]) = ($_ =~ /^(.*),(.*),(.*),(.*)/)) { $domain = $val[$order{'domain'}]; $desc = $val[$order{'desc'}]; $name = $val[$order{'name'}]; $pwd = $val[$order{'pwd'}]; $desc =~ s/\"//g; $self->AddUser($domain, $desc, $name, $pwd) if ($mod_type); $self->RemoveUser($domain, $name) if (!$mod_type); } else { print "Bad CSV format (skipping): $_", "\n"; } } close(OLD); return 1; } sub AddList { return DoList(@_, 1); } sub RemoveList { return DoList(@_, 0); } 1;