d0d307
#!/usr/bin/perl -w
d0d307
# 
d0d307
# imapcreate: create IMAP mailboxes with quotas
d0d307
#			 Reads user names from standard input.
d0d307
# launch without argument for a short help.
d0d307
#
d0d307
# originally found on http://cyrus-utils.sourceforge.net
d0d307
# (could not find any copyright info, thought)
d0d307
# 
d0d307
# enhanced by Clément "nodens" Hermann <clement.hermann@free.fr>
d0d307
#
d0d307
# I'd like to consider this as GPL'd (cf www.gnu.org), but won't add any copyright without the original author's consent.
d0d307
# 
d0d307
d0d307
use Getopt::Long;
d0d307
use Cyrus::IMAP::Admin;
d0d307
use strict;
d0d307
d0d307
d0d307
my $debug;
d0d307
my $user;
d0d307
my $pass;
d0d307
my $quota;
d0d307
my @part;
d0d307
my $useunixhierarchy;
d0d307
my @mailboxes;
d0d307
my $delete;
d0d307
my $cyrus;
d0d307
d0d307
sub usage {
d0d307
  print <
d0d307
imapcreate - create IMAP mailboxes with quotas
d0d307
  usage:
d0d307
	imapcreate [-d] [-u user] [-p pass] [-m mailbox1[,mailbox2][,mailbox<n>]] 
d0d307
	[-q quota] [-t partition:list] [-s] [-v] <server>
d0d307
d0d307
Options:
d0d307
   -t : the partition to use. Default to the \"default\" partition
d0d307
   -q ; the quota, if a quota is needed. It is normally in KiloBytes, but you can use m,M,g or G suffix to use MB or GB instead, e.g 10k, 2048M or 100g
d0d307
   -m : a comma-separated mailbox list
d0d307
   -u : your cyrus admin user (usually cyrus or cyradm)
d0d307
   -p : your cyrus admin password (if not provided, it will be asked for)
d0d307
   -s : use the unix hierarchy separator (see imapd.conf(1))
d0d307
   -d : delete mailboxes instead of creating them
d0d307
   -v : run in debug mode, and print information on stdout
d0d307
d0d307
If no password is submitted with -p, we'll prompt for one.
d0d307
if no mailbox name is specified with -m, read user names from standard input
d0d307
d0d307
  examples: 
d0d307
	imapcreate -u cyradm -m foo,bar,joe -q 50000 -t p1:p2 mail.testing.umanitoba.ca
d0d307
	cat list.txt | imapcreate -u cyradm -p 'cyruspass' -q 50M mail.testing.umanitoba.ca
d0d307
EOU
d0d307
  exit 1;
d0d307
}
d0d307
d0d307
# Create a mailbox... usage : &CreateMailBox(user,partition[,quota]).
d0d307
# You have to be authentified already. We use "$cyrus" as the connection name.
d0d307
# partition can be 'default'
d0d307
sub CreateMailBox {
d0d307
	my $mbuser = $_[0];
d0d307
	my $mbpart = $_[1];
d0d307
	my $mbquota = $_[2];
d0d307
	
d0d307
	print "Creating $mbuser on $mbpart\n" if $debug;
d0d307
	if ($mbpart eq 'default') {
d0d307
	$cyrus->createmailbox($mbuser);
d0d307
	}
d0d307
	else {
d0d307
	$cyrus->createmailbox($mbuser, $mbpart);
d0d307
	}
d0d307
	warn $cyrus->error if $cyrus->error;
d0d307
	
d0d307
	# Set the quota
d0d307
	if ($mbquota) {
d0d307
	print "Setting quota for $mbuser to $mbquota\n" if $debug;
d0d307
	$cyrus->setquota($mbuser, 'STORAGE', $mbquota);
d0d307
	warn $cyrus->error if $cyrus->error;
d0d307
	}
d0d307
}
d0d307
d0d307
# Delete a mailbox. Usage: $DeleteMailBox($user)
d0d307
# Assuming we use $user as the admin.
d0d307
sub DeleteMailBox {
d0d307
	my $mbuser = $_[0];
d0d307
	my $delacl = "c";
d0d307
	
d0d307
	print "Deleting $mbuser\n" if $debug;
d0d307
	$cyrus->setaclmailbox($mbuser, $user, $delacl);
d0d307
	$cyrus->deletemailbox($mbuser);
d0d307
	warn $cyrus->error if $cyrus->error;
d0d307
}
d0d307
d0d307
GetOptions("d|delete" => \$delete, "u|user=s" => \$user, "p|pass=s" => \$pass, "m|mailboxes=s" => \@mailboxes, "q|quota=s" => \$quota,
d0d307
   "t|part=s" => \@part, "s|UnixHierarchy" => \$useunixhierarchy, "v|verbose" => \$debug );
d0d307
@part = split(/:/, join(':', @part));
d0d307
push @part, 'default' unless @part;
d0d307
my $pn = 0;
d0d307
@mailboxes = split(/,/, join(',', @mailboxes));
d0d307
d0d307
my $server = shift(@ARGV) if (@ARGV);
d0d307
usage unless $server;
d0d307
d0d307
# quotas formatting:
d0d307
if ($quota) {
d0d307
	if ($quota =~ /^(\d+)([mk]?)$/i) {
d0d307
		my $numb = $1;
d0d307
		my $letter = $2;
d0d307
		if ($letter =~ /^m$/i) {
d0d307
			$quota = $numb * 1024;
d0d307
			print "debug: quota=$quota\n" if $debug;
d0d307
		} elsif ($letter =~ /^k$/i) {
d0d307
			$quota = $numb;
d0d307
			print "debug: quota=$quota\n" if $debug;
d0d307
		} else {
d0d307
			die "malformed quota: $quota (must be at least one digit eventually followed by m, M, k or K\n";
d0d307
#			$quota = $numb;
d0d307
#			print "debug: quota=$quota\n" if $debug;
d0d307
		}
d0d307
	} else {
d0d307
		die "malformed quota: $quota (must be at least one digit eventually followed by m, M, k or K\n";
d0d307
	}
d0d307
}
d0d307
d0d307
# Authenticate
d0d307
$cyrus = Cyrus::IMAP::Admin->new($server);
d0d307
$cyrus->authenticate(-mechanism => 'login', -user => $user,
d0d307
	 -password => $pass);
d0d307
die $cyrus->error if $cyrus->error;
d0d307
d0d307
# if there isn't any mailbox defined yet, get them from standard input
d0d307
if (! (defined $mailboxes[0])) { 
d0d307
	# For all users
d0d307
	while (<>) {
d0d307
		chomp;
d0d307
		my $mbox = $_;
d0d307
		push @mailboxes, $mbox;
d0d307
	}
d0d307
}
d0d307
d0d307
# create/delete mailboxes for each user
d0d307
foreach my $mailbox (@mailboxes) {
d0d307
	if ($useunixhierarchy) {
d0d307
	$mailbox = 'user/' . $mailbox;
d0d307
	} else {
d0d307
	$mailbox = 'user.' . $mailbox;
d0d307
	}
d0d307
d0d307
	if ($delete) {
d0d307
		&DeleteMailBox($mailbox)
d0d307
	} else {
d0d307
		# Select the partition
d0d307
		my $pt = $part[$pn];
d0d307
		$pn += 1;
d0d307
		$pn = 0 unless $pn < @part;
d0d307
		&CreateMailBox($mailbox,$pt,$quota)
d0d307
	}
d0d307
}
d0d307