#!/usr/bin/perl # #Created by Vincent Li vincent@brc.ubc.ca on 2004-12-22 to only add mailbox on #Cyrus POP3 v2.2.10-Invoca-RPM-2.2.10-3.fc3 server on Fedora Core 3 use strict; use warnings; use Getopt::Long; use Term::ReadKey; use Cyrus::IMAP::Admin; ###### My parameters section ########### my $mechanism = "login"; my ( $cyrus_server, $cyrus_user, $cyrus_pass, $userfile, $add, $del, $help, ); ######## Main program section ########### get_options(); $cyrus_pass || do { print "What's the password for cyrus admin user:"; ReadMode 2; $cyrus_pass = <>; chop $cyrus_pass; printf "\n"; ReadMode 0; }; $cyrus_server = $cyrus_server || 'localhost'; $cyrus_user = $cyrus_user || 'cyrus'; my $cyrus = login_imap ($cyrus_server, $cyrus_user, $cyrus_pass); my $FH; open ($FH, "<$userfile") or die "Could not open $userfile:$!"; while (<$FH>) { create_mailbox("$_") if defined($add); delete_mailbox("$_") if defined($del); } close ($FH); #### Sub function section ########## sub get_options { my $numopt = scalar(@ARGV); my $opt_ret = GetOptions( "cyrus_server=s" => \$cyrus_server, "cyrus_user=s" => \$cyrus_user, "cyrus_pass=s" => \$cyrus_pass, "userfile=s" => \$userfile, "add!" => \$add, "del!" => \$del, "help" => \$help, ); # exit with --help option or no option at all usage() and exit if ($help or ! $numopt) ; } sub login_imap { my ($host, $user, $password) = @_; my $cyrus = Cyrus::IMAP::Admin->new($host); $cyrus->authenticate($mechanism,'imap','',$cyrus_user,'0','10000',$cyrus_pass) or die "Error login : [$host] with user [$user] : $@"; return($cyrus); } sub create_mailbox { my $user = shift; my $mailbox = 'user.' . "$user"; ##must add chomp to remove newline,otherwise, program will complain invalid mailbox name### chomp($mailbox); print "Creating mailbox: $mailbox \n"; $cyrus->create($mailbox); if ($cyrus->error) { print STDERR "Error: ", $mailbox," ", $cyrus->error, "\n"; } else { print "Created Mailbox: $mailbox \n"; } print "\n"; } sub delete_mailbox { my $user = shift; my $mailbox = 'user.' . "$user"; chomp($mailbox); $cyrus->setacl($mailbox, $cyrus_user => "all"); if ($cyrus->error) { print STDERR "Error: ", $mailbox," ", $cyrus->error, "\n"; } else { print "Gave ACL ", "all", " to $mailbox for $cyrus_user \n"; } print "Deleting mailbox: $mailbox\n"; $cyrus->delete($mailbox); if ($cyrus->error) { print STDERR "Delete Error: ", $mailbox," ", $cyrus->error, "\n"; } else { print "Deleted Mailbox: $mailbox \n"; } print "\n"; } sub usage { print < : imap server. --cyrus_user : cyrus admin user to login. --cyrus_pass : password for the cyrus admin user. --userfile : imap user file list. --add if defined add, then add mailbox. --del if defined del, then del mailbox. --help : print this. EOF }