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