# --------------------------------------------------------------------------
# Copyright (c) Ensim Corporation 2000, 2001
# ENSIM CORPORATION - ALL RIGHTS RESERVED
# --------------------------------------------------------------------------
# $Id: majordomo.pm,v 1.1.1.1 2008-03-01 00:21:36 aarefyev Exp $
# $Name: not supported by cvs2svn $
# --------------------------------------------------------------------------

package majordomo;

require '/usr/lib/opcenter/majordomo/mymajordomo-lib.pl';
require '/usr/lib/opcenter/sendmail/sendmail.pm';


use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/majordomo";
use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/cmdline_common";

use ERRORS;
use Carper;


sub getMyOS() 
{
  $out=$ENV{'OSTYPE'} || `uname`;
 if ($out =~ /linux/i) {
   $os = "Linux";
 } elsif ($out =~/SunOS/) {
   $os = "SunOS";
 }
 return $os;
}

# find(name, &array)
sub find
{
local($c, @rv);
foreach $c (@{$_[1]}) {
	if ($c->{'name'} eq $_[0]) {
		push(@rv, $c);
		}
	}
return @rv ? wantarray ? @rv : $rv[0]
           : wantarray ? () : undef;
}

# sendmail_call(function, [arg]*)
sub sendmail_call
  {
    local @args = @_[1 .. @_-1];   # CHECK THIS
    local @rv = eval "&sendmail::$_[0](\@args)";
    if ($@) { #&majordomo::mydbg($@); 
            }  
    return @rv;
  }



# find_value(name, &array)
sub find_value
{
local(@v);
@v = &find($_[0], $_[1]);
@v = grep { !$_->{'computed'} } @v;
if (!@v) { return undef; }
elsif (wantarray) { return map { $_->{'value'} } @v; }
else { return $v[0]->{'value'}; }
}


# perl_var_replace(string, &config)
# Replaces variables like $foo in a string with their value from
# the config file
sub perl_var_replace
{
local $str = $_[0];
local %donevar;
while($str =~ /\$([A-z0-9\_]+)/ && !$donevar{$1}) {
	$donevar{$1}++;
	local $val = &find_value($1, $_[1]);
	$str =~ s/\$([A-z0-9\_]+)/$val/;
	}
return $str;
}


# get_aliases_file()
# Returns the paths to the sendmail-style aliases files
sub get_aliases_file
{
return [ $config{'aliases_file'} ] if ($config{'aliases_file'});
#local $sm_conf = &foreign_call("sendmail", "get_sendmailcf");
#return &foreign_call("sendmail", "aliases_file", $sm_conf);
}



# Validate inputs
# iT takes in a dictionary of input to be validated and returns 
# another dictionary containing the error codes/string for each validation
# performed.
# The python method can simply do the validation by calling
# a method which does validation as done below. In case of error however,
# instead of printing it, the error is returned to the python-method
# which then apptly displays /pops it on the screen for the user.

sub validateInput()
{
local $in; 
local $err='';
#$in = $_[0];
%in = @_; # XXX (PR 16052)

# the err string maybe a sequence of error string separated by '#'
$in{'name'} =~ /^\s+$/ and
    ($cerr += $E_MISSINGLIST) and
	sprintf($err,"#%s","Invalid or missing list name.");
$in{'name'} = lc($in{'name'});
if (&get_list($in{'name'}, $conf)) {
    $cerr += $E_LISTEXISTS and $cerr += { list => $in{'name'} } ;
	sprintf($err,"#%s", "A mailing list called '$in{'name'}' already exists.");
	}
$aliases_files = &get_aliases_file();
#@aliases = &foreign_call("sendmail", "list_aliases", $aliases_files);
@aliases = &sendmail_call("list_aliases", $aliases_files);
foreach $a (@aliases) {
	if ($a->{'enabled'} && lc($a->{'name'}) eq lc($in{'name'})) {
        $cerr += $E_ALIASEXISTS and $cerr += { alias => $in{'name'} } ;
		sprintf($err, "#%s","A mail alias for '$in{'name'}' already exists.#");
		}
	}

if ($in{'owner'} =~ /^\s+$/) {
    ($cerr += $E_MISSINGOWNER) and 
	sprintf($err ,"#%s", "Invalid or missing owner's email address.#");
}
if ($in{'password'} =~ /^\s+$/) {
    ($cerr += $E_MISSINGADMINPASSWORD) and
	sprintf($err ,"#%s","Invalid or missing administration password.#");
}
if(($in{'moderator_def'} =~ /^\s+$/) or ($in{'moderator'} =~ /^\s+$/)) {
    ($cerr += $E_MISSINGMODERATOR) and 
	sprintf($err ,"#%s", "Invalid or missing moderator's email address.#");
}
$in{'info'} =~ s/\r//g;
$in{'footer'} =~ s/\r//g;

if ($err != '') {
  return $err;
}
}

# This takes in a string and returns a dictionary out of it.
sub stringToDict()
{
local %dict;
local $pair;
local $str = $_[0];
@pairs = split(/\&/, $str);
foreach $pair (@pairs) {
	local ($k, $v) = split(/=/, $pair, 2);
	$k =~ s/\+/ /g; $k =~ s/%(..)/pack("c",hex($1))/ge;
	$v =~ s/\+/ /g; $v =~ s/%(..)/pack("c",hex($1))/ge;
	$dict{$k} = defined($dict{$k}) ? $dict{$k}."\0".$v : $v;
	}
%dict;
}

sub stringToList()
{
  local @retval;
  local @list = split(/\&/, $_[0]);
  foreach $l (@list) {
	$l =~ s/\+/ /g; 
        $l =~ s/%(..)/pack("c",hex($1))/ge;
	push(@retval, $l);
      }
  return @retval;
} 



#only for debugging
# takes in a dictionary: mydump(%dict)
sub dbgDict()
  {
    local %dict = %{$_[0]};
    local $line = "{";
    foreach $f (keys(%dict)) {
      $line = $line . "$f=$dict{$f}, ";
    }
    chop $line;
    chop $line;
    return ($line . "}");
  }

sub mydbg()
{
  open(MYDEBUG, ">>/home/two/debug");
  print MYDEBUG "@_\n";
  close(MYDEBUG);
}

# list_lists(&config)
# Returns a list of mailing list names
sub list_lists
{
local ($l, @rv);
#local $ldir = &perl_var_replace(&find_value("listdir", $_[0]), $_[0]);
$myos = &getMyOS();
if ($myos eq "SunOS") {
$ldir = '/usr/local/majordomo/lists';
}
else {
$ldir = '/var/lib/majordomo/lists';
}

opendir(DIR, $ldir);
while($l = readdir(DIR)) {
	if ($l =~ /^(\S+)\.config$/) {
		push(@rv, $1);
		}
	}
closedir(DIR);
return @rv;
}

# get_list(name, &config)
# Returns the details of some list
sub get_list
{
#local $ldir = &perl_var_replace(&find_value("listdir", $_[1]), $_[1]);
$myos = &getMyOS();
if ($myos eq "SunOS") {
$ldir = '/usr/local/majordomo/lists';
}
else {
$ldir = '/var/lib/majordomo/lists';
}
local %list;
return undef if (!-r "$ldir/$_[0].config");
$list{'name'} = $_[0];
$list{'members'} = "$ldir/$_[0]";
$list{'config'} = "$ldir/$_[0].config";
$list{'info'} = "$ldir/$_[0].info";
$list{'intro'} = "$ldir/$_[0].intro";
return \%list;
}

# sendmail_call(function, [arg]*)
sub sendmail_call
  {
    local @args = @_[1 .. @_-1];   # CHECK THIS
    local @rv = eval "&sendmail::$_[0](\@args)";
    if ($@) { #&majordomo::mydbg($@); 
            }  
    return @rv;
  }


1;
return 1;

