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