#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2000-2006 Gormand Pty Ltd
# copyright (C) 2002,2006 Mitel Networks Corporation
# 
# 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
# 
#----------------------------------------------------------------------
package esmith;

use strict;
use Errno;
use esmith::ConfigDB;
use esmith::AccountsDB;
use esmith::util;

use File::Find;
use User::pwent;

use Mail::Ezmlm;

my $conf = esmith::ConfigDB->open;
my $accounts = esmith::AccountsDB->open;

my $event = $ARGV [0];
die "event argument missing." unless defined ($event);

my $listName = $ARGV[1];
die "listName argument missing." unless defined ($listName);

# We really want to do all ezmlm functions as the user ezmlm - saves
# lots of permissions fixups later on

unless (defined getpwnam("ezmlm"))
{
    system(qw(/usr/sbin/useradd -c EZMLM 
		-u 80 
		-d /home/e-smith/files/ezmlm
		-s /bin/false ezmlm)) == 0
	    or warn("Could not create user 'ezmlm'\n");

    $accounts->new_record("ezmlm", { type => "system", Uid => "80", Gid => "80" } )
	unless ($accounts->get("ezmlm"));
}

my $pw = getpwnam("ezmlm") or die "User ezmlm not found";
my $ownerHome = $pw->dir;

# XXX post-upgrade/init-accounts does chown root:root /home/e-smith/files/* 
# XXX and ezmlm needs to be able to write to its home directory
# XXX The permissions should be in the RPM spec file - just fix it for now
esmith::util::chownFile("ezmlm", "ezmlm",  $ownerHome);

$> = $pw->uid;
$) = $pw->gid;

my $list = $accounts->get($listName) or die "$listName not found in accounts db";
die "$listName is not a mailing list account." 
	unless ($list->prop('type') eq "mailinglist");

my $listHost = $list->prop("Domain");

my $listHomeBase = "${ownerHome}/lists";

mkdir $listHomeBase, 0700 unless (-d $listHomeBase);

my $listHome = "${listHomeBase}/${listName}";

my $dotQmail = ".qmail-${listName}";

my $listAlias = "${ownerHome}/${dotQmail}";

if ( $event eq "mailinglist-create" )
{
    my $ezmlm = new Mail::Ezmlm;
    
    my $thislist = $ezmlm->make(
		-dir      => $listHome,
                -qmail    => $listAlias,
                -name     => $listName,
                -host     => $listHost,
                -user     => '',
                -switches => 'aBdfgHiJklMnOpQrStuWx',
		);

    die "List creation failed\n" unless $thislist;

    my $listOwner = $list->prop("Owner");

    $ezmlm->update("5 $listOwner");
}
elsif ( $event eq "mailinglist-delete" )
{
  # Remove the .qmail files for this list
  find(\&deleteWanted, $ownerHome);

  # And the list management directory
  system( '/bin/rm', '-rf', $listHome ) ==0 or 
	die "Couldn't remove list directory $listHome";
}
else
{
    die "Couldn't work out function to call for $0";
}

#----------------------------------------------------------------------
# Called by File::Find
#----------------------------------------------------------------------
sub deleteWanted
{
    return unless 
        /^$dotQmail$/ ||
        /^$dotQmail-accept-default$/ ||
        /^$dotQmail-default$/ ||
        /^$dotQmail-digest-owner$/ ||
        /^$dotQmail-digest-return-default$/ ||
        /^$dotQmail-owner$/ ||
        /^$dotQmail-reject-default$/ ||
        /^$dotQmail-return-default$/;

    unlink($_) || die "Couldn't unlink($_)";
}

