#!/usr/bin/perl -w
#
# List a user's quota in Cyrus
#

if ($#ARGV < 0) {
	print "Usage: $0 username\n";
	print "  Lists a user's quota in Cyrus\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 $username = $ARGV[0];

# Verify that mailbox exists
if (! $imap->list("user.$username")) {
	print "ERROR: Mailbox 'user.$username' does not exist\n";
	exit(-1);
}

# Fetch quota
if (@quota = $imap->get_quota("user.$username")) {
	$root = $quota[0];
	$usage = $quota[1];
	$limit = $quota[2];

	printf("%8s %d/%d (%.2f%%)\n", $root, $usage, $limit, 100 * $usage/$limit);
}
else {
	print "Error fetching quota: " . $imap->{'Error'} . "\n";
}

$imap->close;

