/[packages]/cauldron/cyrus-imapd/current/SOURCES/imapcreate.pl
ViewVC logotype

Contents of /cauldron/cyrus-imapd/current/SOURCES/imapcreate.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33418 - (show annotations) (download)
Sat Jan 22 23:52:57 2011 UTC (13 years, 2 months ago) by ennael
File MIME type: text/plain
File size: 4465 byte(s)
imported package cyrus-imapd
1 #!/usr/bin/perl -w
2 #
3 # imapcreate: create IMAP mailboxes with quotas
4 # Reads user names from standard input.
5 # launch without argument for a short help.
6 #
7 # originally found on http://cyrus-utils.sourceforge.net
8 # (could not find any copyright info, thought)
9 #
10 # enhanced by Clément "nodens" Hermann <clement.hermann@free.fr>
11 #
12 # 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.
13 #
14
15 use Getopt::Long;
16 use Cyrus::IMAP::Admin;
17 use strict;
18
19
20 my $debug;
21 my $user;
22 my $pass;
23 my $quota;
24 my @part;
25 my $useunixhierarchy;
26 my @mailboxes;
27 my $delete;
28 my $cyrus;
29
30 sub usage {
31 print <<EOU;
32 imapcreate - create IMAP mailboxes with quotas
33 usage:
34 imapcreate [-d] [-u user] [-p pass] [-m mailbox1[,mailbox2][,mailbox<n>]]
35 [-q quota] [-t partition:list] [-s] [-v] <server>
36
37 Options:
38 -t : the partition to use. Default to the \"default\" partition
39 -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
40 -m : a comma-separated mailbox list
41 -u : your cyrus admin user (usually cyrus or cyradm)
42 -p : your cyrus admin password (if not provided, it will be asked for)
43 -s : use the unix hierarchy separator (see imapd.conf(1))
44 -d : delete mailboxes instead of creating them
45 -v : run in debug mode, and print information on stdout
46
47 If no password is submitted with -p, we'll prompt for one.
48 if no mailbox name is specified with -m, read user names from standard input
49
50 examples:
51 imapcreate -u cyradm -m foo,bar,joe -q 50000 -t p1:p2 mail.testing.umanitoba.ca
52 cat list.txt | imapcreate -u cyradm -p 'cyruspass' -q 50M mail.testing.umanitoba.ca
53 EOU
54 exit 1;
55 }
56
57 # Create a mailbox... usage : &CreateMailBox(user,partition[,quota]).
58 # You have to be authentified already. We use "$cyrus" as the connection name.
59 # partition can be 'default'
60 sub CreateMailBox {
61 my $mbuser = $_[0];
62 my $mbpart = $_[1];
63 my $mbquota = $_[2];
64
65 print "Creating $mbuser on $mbpart\n" if $debug;
66 if ($mbpart eq 'default') {
67 $cyrus->createmailbox($mbuser);
68 }
69 else {
70 $cyrus->createmailbox($mbuser, $mbpart);
71 }
72 warn $cyrus->error if $cyrus->error;
73
74 # Set the quota
75 if ($mbquota) {
76 print "Setting quota for $mbuser to $mbquota\n" if $debug;
77 $cyrus->setquota($mbuser, 'STORAGE', $mbquota);
78 warn $cyrus->error if $cyrus->error;
79 }
80 }
81
82 # Delete a mailbox. Usage: $DeleteMailBox($user)
83 # Assuming we use $user as the admin.
84 sub DeleteMailBox {
85 my $mbuser = $_[0];
86 my $delacl = "c";
87
88 print "Deleting $mbuser\n" if $debug;
89 $cyrus->setaclmailbox($mbuser, $user, $delacl);
90 $cyrus->deletemailbox($mbuser);
91 warn $cyrus->error if $cyrus->error;
92 }
93
94 GetOptions("d|delete" => \$delete, "u|user=s" => \$user, "p|pass=s" => \$pass, "m|mailboxes=s" => \@mailboxes, "q|quota=s" => \$quota,
95 "t|part=s" => \@part, "s|UnixHierarchy" => \$useunixhierarchy, "v|verbose" => \$debug );
96 @part = split(/:/, join(':', @part));
97 push @part, 'default' unless @part;
98 my $pn = 0;
99 @mailboxes = split(/,/, join(',', @mailboxes));
100
101 my $server = shift(@ARGV) if (@ARGV);
102 usage unless $server;
103
104 # quotas formatting:
105 if ($quota) {
106 if ($quota =~ /^(\d+)([mk]?)$/i) {
107 my $numb = $1;
108 my $letter = $2;
109 if ($letter =~ /^m$/i) {
110 $quota = $numb * 1024;
111 print "debug: quota=$quota\n" if $debug;
112 } elsif ($letter =~ /^k$/i) {
113 $quota = $numb;
114 print "debug: quota=$quota\n" if $debug;
115 } else {
116 die "malformed quota: $quota (must be at least one digit eventually followed by m, M, k or K\n";
117 # $quota = $numb;
118 # print "debug: quota=$quota\n" if $debug;
119 }
120 } else {
121 die "malformed quota: $quota (must be at least one digit eventually followed by m, M, k or K\n";
122 }
123 }
124
125 # Authenticate
126 $cyrus = Cyrus::IMAP::Admin->new($server);
127 $cyrus->authenticate(-mechanism => 'login', -user => $user,
128 -password => $pass);
129 die $cyrus->error if $cyrus->error;
130
131 # if there isn't any mailbox defined yet, get them from standard input
132 if (! (defined $mailboxes[0])) {
133 # For all users
134 while (<>) {
135 chomp;
136 my $mbox = $_;
137 push @mailboxes, $mbox;
138 }
139 }
140
141 # create/delete mailboxes for each user
142 foreach my $mailbox (@mailboxes) {
143 if ($useunixhierarchy) {
144 $mailbox = 'user/' . $mailbox;
145 } else {
146 $mailbox = 'user.' . $mailbox;
147 }
148
149 if ($delete) {
150 &DeleteMailBox($mailbox)
151 } else {
152 # Select the partition
153 my $pt = $part[$pn];
154 $pn += 1;
155 $pn = 0 unless $pn < @part;
156 &CreateMailBox($mailbox,$pt,$quota)
157 }
158 }
159

  ViewVC Help
Powered by ViewVC 1.1.30