21 May 2012

Cyrus IMAP: setting user quota


Setting Cyrus IMAP user quota


1. Using Cyrus::IMAP::Admin



#!/usr/bin/perl -w

use Data::Dumper;
use Cyrus::IMAP::Admin;   

# Create admin client object
my $client = Cyrus::IMAP::Admin->new('localhost');
if (!$client) {
    print "\n Error: $!";
    exit 1;
}
$client->setflags(Cyrus::IMAP::CONN_NONSYNCLITERAL);

# Connect to IMAP server
my %h = ("Mechanism" => "login",
        "User" => "cyrus",
        "Password" => "password");
$client->authenticate(%h) || die "error $@ \n";

my $username = 'user.' . $ARGV[0];
my $quota = $ARGV[1];


# Set cyrus IMAP quota for user
if (defined $quota && $quota ne "") {
    print "\n Setting Quota";
    if (! $client->setquota("$username", "STORAGE", "$quota")) {
        print "\n Unable to set quota : error: $@";
    };
} else {

    # To remove quota use following call
    print "\nRemoving quota";
    if (! $client->setquota("$username")) {
        print "\n Unable to set quota : error: $@";
    };
}

# Get the user quota
my %userQuota = $client->listquota("$username");
print "\n Qutota for user : $username: " . Dumper(@{$userQuota{STORAGE}}[1]);

exit(0);



2. Using IMAP::Admin


#! /usr/bin/perl -w

require IMAP::Admin;
use Data::Dumper;

my $user = $ARGV[0];

if (! defined $user || $user eq "") {
    print STDERR "please provide username ";
    exit 1;
}

# Connect to IMAP server
my $imap = IMAP::Admin->new('Server' => 'localhost',
                           'Login' => 'cyrus',
                           'Password' => 'password',
                           'Port' => 143, # (143 is default),
                           'Separator' => ".", # default is a period
                        );

if (! $imap) {
    print STDERR "ERROR: while connecting to imap server: $@";
    exit 1;
}


# Setting cyrus IMAP quota for user
my $err = $imap->set_quota("user.$user", 10000);
if ($err ) {
    print "\n STDERR: Unable to set quota: $err";
    exit 1;
};

# Get the quota for the user
my @quota = $imap->get_quota("user.$user");
print "\n Quota for user : $user ". Dumper(@quota);

exit 0;


No comments:

Post a Comment