[Pkg-Cyrus-imapd-Debian-devel] [SVN] r99 - trunk/cyrus-imapd-2.2.12/debian/examples

pkg-cyrus-imapd-debian-devel@lists.alioth.debian.org pkg-cyrus-imapd-debian-devel@lists.alioth.debian.org
Fri, 15 Apr 2005 22:48:57 +0200


Author: sven
Date: 2005-04-15 22:48:56 +0200 (Fri, 15 Apr 2005)
New Revision: 99

Added:
   trunk/cyrus-imapd-2.2.12/debian/examples/README.imapcreate.pl
   trunk/cyrus-imapd-2.2.12/debian/examples/imapcreate.pl
Log:
Add imapcreate.pl as an example of how to use the Perl bindings

Added: trunk/cyrus-imapd-2.2.12/debian/examples/README.imapcreate.pl
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/cyrus-imapd-2.2.12/debian/examples/README.imapcreate.pl	2005-04=
-15 20:47:55 UTC (rev 98)
+++ trunk/cyrus-imapd-2.2.12/debian/examples/README.imapcreate.pl	2005-04=
-15 20:48:56 UTC (rev 99)
@@ -0,0 +1,23 @@
+Launching the script without any parameters will show a short help. It
+should be pretty self-explanatory.
+
+Here are some examples on how the script could be invoked:
+
+- this will read the file list.txt, and create the mailboxes listed in i=
t
+  with a 50MB quota:
+
+  cat list.txt | ./imapcreate.pl -u cyradm -p 'cyrpass' -q 50M mail.exam=
ple.com
+
+- this will create a mailbox for john, using the Unix Hierarchy separato=
r,
+  no quota, in verbose mode:
+
+  ./imapcreate.pl -u cyradm -m john -s -v mail.example.com
+
+  the output would look like this:
+
+  Creating user.john on default
+
+This script is far from being perfect, but it works great for me. feel f=
ree
+to e-mail me about it, to report bugs, to send patches etc:
+
+clement.hermann@free.fr

Added: trunk/cyrus-imapd-2.2.12/debian/examples/imapcreate.pl
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/cyrus-imapd-2.2.12/debian/examples/imapcreate.pl	2005-04-15 20:=
47:55 UTC (rev 98)
+++ trunk/cyrus-imapd-2.2.12/debian/examples/imapcreate.pl	2005-04-15 20:=
48:56 UTC (rev 99)
@@ -0,0 +1,167 @@
+#!/usr/bin/perl -w
+#=20
+# imapcreate: create IMAP mailboxes with quotas
+#			 Reads user names from standard input.
+# originally found on http://cyrus-utils.sourceforge.net
+# =A9 2001 Garry Mills =20
+#=20
+# enhanced by Cl=E9ment "nodens" Hermann <clement.hermann@free.fr>
+#
+# 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.
+# last modification : 2004/11/23
+# Changes :=20
+# 2005/03/31	- Finally found out the original author's name.
+# 2004/11/23	- removed LOGIN as a default mech, now use cyrus' default
+#		- Added --auth option to specify mech
+#
+# TODO : fix STDIN collision when reading password AND mailboxes name fr=
om STDIN
+#=20
+use Getopt::Long;
+use Cyrus::IMAP::Admin;
+use strict;
+
+# CLI options
+my ($debug,$user,$pass,$quota,@part,$useunixhierarchy,@mailboxes,$delete=
,$cyrus,$authmech);
+
+sub usage {
+  print "imapcreate - create IMAP mailboxes with quotas\n";
+  print "  usage:\n";
+  print "	imapcreate [-d] [-u user] [--auth mechanism] [-p pass] [-m mai=
lbox1[,mailbox2][,mailbox<n>]] [-q quota] [-t partition:list]\n";
+  print "	[-s] [-v] <server>\n";
+  print "\n";
+  print "if -s is set, we'll use the unix hierarchy separator (see imapd=
.conf(1))\n";
+  print "if -d is set, we'll delete mailboxes instead of creating them\n=
";
+  print "You can use M or ,m to specify quotas. e.g. 10M. By default,\n"=
;
+  print "the quota is expressed in Kbytes.\n";
+  print "If no password is submitted with -p, we'll prompt for one.\n";
+  print "if no mailbox name is specified with -m, read user names from s=
tandard input\n";
+  print "if -v is set, we'll run in debug mode, and print information on=
 stdout\n";
+  print "\n";
+  print "The default mechanism is used for authentication. If you need a=
nother\nmechanism, (try LOGIN), use --auth <mechanism> option\n";
+  print "\n";
+  print "  example: \n";
+  print "	imapcreate -u cyradm -m foo,bar,joe -q 50000 -t p1:p2 mail.tes=
ting.umanitoba.ca\n";
+  print "\n";
+  exit 0;
+}
+
+# Create a mailbox... usage : &CreateMailBox(user,partition[,quota]).
+# You have to be authentified already. We use "$cyrus" as the connection=
 name.
+# partition can be 'default'
+sub CreateMailBox {
+	my $mbuser =3D $_[0];
+	my $mbpart =3D $_[1];
+	my $mbquota =3D $_[2];
+=09
+	print "Creating $mbuser on $mbpart\n" if $debug;
+	if ($mbpart eq 'default') {
+	$cyrus->createmailbox($mbuser);
+	}
+	else {
+	$cyrus->createmailbox($mbuser, $mbpart);
+	}
+	warn $cyrus->error if $cyrus->error;
+=09
+	# Set the quota
+	if ($mbquota) {
+	print "Setting quota for $mbuser to $mbquota\n" if $debug;
+	$cyrus->setquota($mbuser, 'STORAGE', $mbquota);
+	warn $cyrus->error if $cyrus->error;
+	}
+}
+
+# Delete a mailbox. Usage: $DeleteMailBox($user)
+# Assuming we use $user as the admin.
+sub DeleteMailBox {
+	my $mbuser =3D $_[0];
+	my $delacl =3D "c";
+=09
+	print "Deleting $mbuser\n" if $debug;
+	$cyrus->setaclmailbox($mbuser, $user, $delacl);
+	$cyrus->deletemailbox($mbuser);
+	warn $cyrus->error if $cyrus->error;
+}
+
+GetOptions( "d|delete" =3D> \$delete,=20
+	    "u|user=3Ds" =3D> \$user,=20
+	    "auth=3Ds" =3D> \$authmech,=20
+	    "p|pass=3Ds" =3D> \$pass,=20
+	    "m|mailboxes=3Ds" =3D> \@mailboxes,=20
+	    "q|quota=3Ds" =3D> \$quota,
+	    "s|UnixHierarchy" =3D> \$useunixhierarchy,=20
+	    "t|part=3Ds" =3D> \@part,=20
+	    "v|verbose" =3D> \$debug );
+
+@part =3D split(/:/, join(':', @part));
+push @part, 'default' unless @part;
+my $pn =3D 0;
+@mailboxes =3D split(/,/, join(',', @mailboxes));
+
+my $server =3D shift(@ARGV) if (@ARGV);
+usage unless $server;
+
+# quotas formatting:
+if ($quota) {
+	if ($quota =3D~ /^(\d+)([mk]?)$/i) {
+		my $numb =3D $1;
+		my $letter =3D $2;
+		if ($letter =3D~ /^m$/i) {
+			$quota =3D $numb * 1024;
+			print "debug: quota=3D$quota\n" if $debug;
+		} elsif ($letter =3D~ /^k$/i) {
+			$quota =3D $numb;
+			print "debug: quota=3D$quota\n" if $debug;
+		} else {
+			die "malformed quota: $quota (must be at least one digit eventually f=
ollowed by m, M, k or K\n";
+#			$quota =3D $numb;
+#			print "debug: quota=3D$quota\n" if $debug;
+		}
+	} else {
+		die "malformed quota: $quota (must be at least one digit eventually fo=
llowed by m, M, k or K\n";
+	}
+}
+
+# Authenticate
+$cyrus =3D Cyrus::IMAP::Admin->new($server);
+
+if ($authmech) {
+	$cyrus->authenticate(-mechanism =3D> $authmech,=20
+			-user =3D> $user,
+	 		-password =3D> $pass);
+} else {
+	$cyrus->authenticate(
+			-user =3D> $user,
+	 		-password =3D> $pass);
+}=09
+die $cyrus->error if $cyrus->error;
+
+# if there isn't any mailbox defined yet, get them from standard input
+if (! (defined $mailboxes[0])) {=20
+	# For all users
+	while (<>) {
+		chomp;
+		my $mbox =3D $_;
+		push @mailboxes, $mbox;
+	}
+}
+
+# create/delete mailboxes for each user
+foreach my $mailbox (@mailboxes) {
+	if ($useunixhierarchy) {
+	$mailbox =3D 'user/' . $mailbox;
+	} else {
+	$mailbox =3D 'user.' . $mailbox;
+	}
+
+	if ($delete) {
+		&DeleteMailBox($mailbox)
+	} else {
+		# Select the partition
+		my $pt =3D $part[$pn];
+		$pn +=3D 1;
+		$pn =3D 0 unless $pn < @part;
+		&CreateMailBox($mailbox,$pt,$quota)
+	}
+}
+