#!/usr/bin/perl -w
#
# Removes rights to a mailbox for a user
#

if ($#ARGV < 1) {
	print "Usage: $0 <mailbox> <username>\n";
	print "  Removes rights to <mailbox> for <username>\n";
	print "  <mailbox> should be in the form user.username\n";
	exit(-1);
}

use IMAP::Admin;

require "/private/admin/acct/email/prefs.pl";
require "/private/admin/acct/email/utils.pl";

my $imap = IMAP::Admin->new('Server' => $prefs{'cyrushost'},
			'Login' => $prefs{'cyrususer'},
			'Password' => $prefs{'cyruspass'},
			);

if (! $imap) {
	print "Error connecting to IMAP server.\n";
	exit;
}

my $mailbox = $ARGV[0];
my $username = $ARGV[1];


# Make sure that mailbox exists
if (! $imap->list($mailbox)) {
	print "Error: mailbox $mailbox not found.\n";
	exit;
}

# Get list of folders
my @folders = $imap->list($mailbox);
push @folders, $imap->list("$mailbox" . ".*");

# Walk through folders removing rights
foreach my $f (@folders) {
	print "Removing rights to $f for $username\n";
	$rv = $imap->delete_acl($f, $username);
	if ($rv != 0) {
		print "Error clearing acl on mailbox $f: " . $imap->{'Error'} . "\n";
		exit;
	}
}

$imap->close;

