#!/usr/bin/perl -w -I/usr/sausalito/perl -I. -I/usr/sausalito/handlers/base/maillist
# $Id: listmod_import,v 1.5 2001/06/20 23:11:42 mpashniak Exp $
#
# listmod_members depends on:
#     	      	update

use MailList; # should be a local file
use Sauce::Util;
use CCE;
my $cce = new CCE;
$cce->connectfd();

my $oid = $cce->event_oid();
my $obj = $cce->event_object();

# import:
import_members($cce, $oid, $obj);

$cce->bye('SUCCESS');
exit(0);

sub import_members
{
  my $cce = shift;
  my $oid = shift;
  my $obj = shift;
  my $changes = {};
  
  # extract information about the list...
  my $list = $obj->{name};
  # sorry about the ugly perl code here, it's efficient:
  my @local_recips = ();
  my @remote_recips = ();
  my $listfile = MailList::get_listfile($obj);

  # lock the members list:
  Sauce::Util::lockfile($listfile);

  my $hn = `/bin/hostname`; chomp($hn);

  my $fh = new FileHandle("<$listfile");
  if ($fh) {
    my @data = <$fh>;
    $fh->close();
    Sauce::Util::unlockfile($listfile);
    
    foreach $_ (@data) {
      chomp($_);
      next if (m/^\s*nobody\s*$/); # who's that?  oh, nobody.
      next if (m/^\s*\w+_alias\s*$/); # just a group.  ignore.
      s/\@$hn\s*$//;
      if (m/\@/) {
      	# is a remote recipient:
	push (@remote_recips, $_);
      } else {
      	push (@local_recips, $_);
      }
    }
    
    $changes->{remote_recips} = $cce->array_to_scalar(sort @remote_recips);
    $changes->{local_recips} = $cce->array_to_scalar(sort @local_recips);

  } else {
    Sauce::Util::unlockfile($listfile);
  }

  $cce->set($oid, "", $changes);
}

