This documentation is for Dovecot v2.x, see wiki1 for v1.x documentation.

My mundane mass folder reindexing script

# email me if you have questions gmillerd@qualhost.com
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use DBI;
# http://search.cpan.org/~markov/Mail-IMAPClient/
use Mail::IMAPClient;
my $args = {
    'folders'   => 'sub', # 'sub' or 'all'
    'autounsub' => 1, # unsubscribe to missing folders
    'msgcount'  => 1, # hit each folder of a message count
    'imaphost'  => 'localhost',
    'imapport'  => '143', # point to server, not proxy we will not be returning
    'imapauth'  => 'CRAM-MD5', # use required method CRAM-MD5
    # must change dsn/user/pass to work for your query
    'sqldsn'    => 'DBI:mysql:database=isp;mysql_socket=/var/run/mysqld/mysqld.sock',
    'sqluser'   => 'mail',
    'sqlpass'   => 'mail',
    # must return user and pass
    #'sqlquery'  => 'SELECT email as user, clear as pass FROM isp.v_exim', # this method for know passwords
    #'sqlquery'  => 'SELECT user, 'secret' AS pass FROM isp.v_exim', #this method for unknown passwords
};
GetOptions(
    'folders=s'  => \$args->{folders},
    'autounsub'  => \$args->{autounsub},
    'msgcount'   => \$args->{msgcount},
    'imaphost=s' => \$args->{imaphost},
    'imapport=s' => \$args->{imapport},
    'imapauth=s' => \$args->{imapauth},
   );
my %acct;
## use handle and user/pass
my $dbh = DBI->connect($args->{sqldsn},
                       $args->{sqluser},
                       $args->{sqlpass},
                       { RaiseError => 1 }) || die($!);
{
    my $sth = $dbh->prepare($args->{sqlquery});
    $sth->execute();
    while (my $h = $sth->fetchrow_hashref())
    {
        $acct{ $h->{user} } = $h->{pass};
    }
}
## cycle through users folders via imap
while (my ($user, $pass) = (each %acct))
{
    my $imap = Mail::IMAPClient->new(
        Server        => $args->{imaphost},
        Port          => $args->{imapport},
        Authmechanism => $args->{imapauth} || 'LOGIN',
        User          => $user,
        Password      => $pass,
        Uid           => 1,
        Debug         => 0) || die($@);
    printf("[%s]\n", $user);
    my @folders=();
    if($args->{folders} eq 'all') {
        @folders = $imap->folders;
    }
    else {
        @folders = $imap->subscribed;
    }

    for my $folder (@folders)
    {
        unless($imap->exists($folder))
        {
            printf("\t%s (NOTEXISTS)\n",
                   $folder,
                  );
            if($args->{autounsub})
            {
                # bah, lack of unsubscribe method wtf ... open a ticket
                # http://rt.cpan.org/Public/Dist/Display.html?Name=Mail-IMAPClient
            }
            next;
        }
        my $msgcount='?';
        if(defined $args->{msgcount})
        {
            $msgcount = $imap->message_count($folder);
        }
        printf("\t%s (%s)\n",
               $folder,
               $msgcount,
              );
    }
}

GabrielMillerd (last edited 2009-03-15 22:35:12 by localhost)