scripted delete mailbox for cyradm

John Wade jwade at oakton.edu
Thu Feb 10 14:32:54 EST 2005


We use something like this,   Note that I hacked this out of a larger
script, that does some site specific things so there is no guarantee that
this actually runs or that it includes all the error checking you would
want, but it should give you the idea.   Also I don't think you need to
iterate through the sub mailboxes at this point to delete them, but we never
removed it from the script and it means you get a nice listing as it
goes.    I also won't claim this is beautiful perl.

Hope it helps,
John
--------------------------------------------
#!/usr/bin/perl
# cyrdel.pl: Script to Delete mailboxes
#

use Getopt::Long;
use File::Find;
use Cyrus::IMAP::Admin;

# IMAP Variables
my $mech='LOGIN';
my $server = "<servername>";
my $port='143';
$imapauthuser = '<cyrus admin user>';
my $imapauthpass = '<cyrus admin user password>';


$usage= "USAGE: ./cyrdl.pl [ -user <UID> | -file </path/to/<filename>> ]";
@ARGV == 2 or die "$usage\n";

## Open up a new session to imap server
$IMAPERROR='';
$imap=Cyrus::IMAP::Admin->new($server,$port);
$imap->addcallback({-trigger=>'EOF',
                    -callback=>\&_cb_eof,
                    -rock=>\$imap}) if defined $imap;

die "Unable to connect to IMAP server: $server" if !$imap;
my $err=$imap->authenticate(-user=>$imapauthuser,
                            -password=>$imapauthpass,
                            -mechanism=>$mech);

$IMAPERROR=$imap->error;
if ($IMAPERROR || !$err) {
   $IMAPERROR="Error Authenticating to $server as $imapauthuser";
   print"IMAP Error: $IMAPERROR";
   exit;
}

GetOptions("file=s@"=>\&prfiles, "user=s@"=>\&prusers);

sub prusers {
 %opt = @_;
        $username = $opt{user};
        print "Deleting mailboxes for $username\n";
        &deletemailbox($username);
}

sub prfiles {
        print "getting files\n";
        %opt = @_;
        open(P,$opt{file});
        while(<P>) {
             /(\w+)[\t ]*/;
             $uid = $1;
            &deletemailbox($uid);
        }
}

sub deletemailbox {
        ($mailbox) = @_;
        @mbxs=$imap->list("user.$mailbox*");
        my $IMAPERROR=$imap->error;
        if ( $IMAPERROR ) {
           print " Error occurred listing mailboxes: $IMAPERROR\n";
           exit;
        }
        if ( scalar(@mbxs) == 0 ) {
                print " Error: no mailboxes found for user $mailbox\n";
                exit;
        }
        # step through mailboxes
        foreach $mbox (reverse @mbxs) {

                # Give ourselve permissions to mailbox
                $result=$imap->setacl($mbox->[0],"$imapauthuser","c");
                $IMAPERROR=$imap->error;
                if ( $IMAPERROR ) {
                   print " Error occurred setting mailbox acls:
$IMAPERROR\n";
                   exit;
                }

                # Now delete mailbox
                $result=$imap->delete($mbox->[0]);
                $IMAPERROR=$imap->error;
                if ( $IMAPERROR ) {
                   print " Error occurred deleting $mbox->[0]:
$IMAPERROR\n";
                   exit;
                }
                else {
                        print " deleted $mbox->[0]\n";
                }
        }
}
--------------------------------------------



Aleksandar Milivojevic wrote:

> Craig White wrote:
> > Trying to script deleting a user's mailbox from system...
> >
> > /bin/su - cyrus -c '/usr/lib/cyrus-imapd/cyradm localhost \
> > --userrc=/var/lib/imap/.systemrc dm user.public'
>
> Try something like this instead:
>
> #! /usr/bin/perl -w
>
> use Cyrus::IMAP::Admin;
>
> $imap = Cyrus::IMAP::Admin->new("servername")
>     or die "Failed to connect";
> $imap->authenticate("-user" => "username",
>                      "-password" => "password",
>                      "-mechanism" => "LOGIN")
>     or die "Failed to authenticate";
> $imap->delete("user.mailbox")
>     or die "Failed to delete mailbox";
>
> See man pages for Cyrus::IMAP::Admin and Cyrus::IMAP for more details.
>
> --
> Aleksandar Milivojevic <amilivojevic at pbl.ca>    Pollard Banknote Limited
> Systems Administrator                           1499 Buffalo Place
> Tel: (204) 474-2323 ext 276                     Winnipeg, MB  R3T 1L7
> ---
> Cyrus Home Page: http://asg.web.cmu.edu/cyrus
> Cyrus Wiki/FAQ: http://cyruswiki.andrew.cmu.edu
> List Archives/Info: http://asg.web.cmu.edu/cyrus/mailing-list.html

---
Cyrus Home Page: http://asg.web.cmu.edu/cyrus
Cyrus Wiki/FAQ: http://cyruswiki.andrew.cmu.edu
List Archives/Info: http://asg.web.cmu.edu/cyrus/mailing-list.html




More information about the Info-cyrus mailing list