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