2 commits - lib/Kolab lib/Kolab.pm

Jeroen van Meeuwen vanmeeuwen at kolabsys.com
Sat May 7 22:51:44 CEST 2011


 lib/Kolab.pm       |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 lib/Kolab/Cyrus.pm |   29 +++++++-
 2 files changed, 196 insertions(+), 8 deletions(-)

New commits:
commit aec02a3a38b88d01d12bda3469fecb3de20ac067
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Sat May 7 16:50:12 2011 -0400

    Create 2 new API calls:
    
      Kolab::cyrusMurderCreateMailbox
      Kolab::cyrusMurderRenameMailbox

diff --git a/lib/Kolab.pm b/lib/Kolab.pm
index 05f6b38..d9c1384 100644
--- a/lib/Kolab.pm
+++ b/lib/Kolab.pm
@@ -31,7 +31,7 @@ use Sys::Syslog;
 use URI;
 use Net::LDAP;
 use Kolab::Util;
-#use Kolab::LDAP;
+
 use vars qw(%config $reloadOk);
 
 require Exporter;
@@ -80,8 +80,19 @@ sub KOLAB_WARN()        { 2 }
 sub KOLAB_INFO()        { 3 }
 sub KOLAB_DEBUG()       { 4 }
 
-sub reloadConfig
-{
+################################################################################
+sub reloadConfig {
+################################################################################
+=pod
+
+=head1 Configuration Reload
+
+=head2 reloadConfig
+
+This function reloads the configuration for the Kolab perl library.
+
+=cut
+
     my $kolab_globals = shift;
     my $globals_only = shift || 0;
 
@@ -449,6 +460,162 @@ sub log
     print STDERR "$text\n" if $config{'debug'};
 }
 
+################################################################################
+sub cyrusMurderCreateMailbox {
+################################################################################
+=pod
+
+=head1 Create a Mailbox in a Cyrus Murder
+
+=head2 cyrusMurderCreateMailbox
+
+This function creates a mailbox in a Cyrus IMAP Murder. It expects to be passed
+a mailbox name, such as 'jdoe', 'jdoe at example.org' or 'info at company.com'.
+
+A second argument, the mailHost, may be specified. The mailHost represents a
+Cyrus IMAP Murder backend server on which the mailbox is to be created. If no
+mailHost is specified, the mailbox is created on the backend server with the
+most free space available.
+
+A third argument may be specified to indicate the folder type, 'user' or
+'shared'. If the folder type is not specified, 'user' is used.
+
+The function connects to the configured 'connect_addr', which should be set to
+a (functional) Cyrus IMAP frontend server using /etc/kolab/kolab.conf.
+
+It creates the administrative connection using the 'cyrus_admin' and
+'cyrus_admin_pw' settings from /etc/kolab/kolab.conf, and issues the create
+command, after which it retrieves the /vendor/cmu/cyrus-imapd/server annotation
+to determine the server on which the mailbox has been created.
+
+The return value is the fully qualified domain name for the mailbox server.
+
+Example usage:
+
+  Kolab::cyrusMurderCreateMailbox('jdoe at example.org')
+
+  Kolab::cyrusMurderCreateMailbox('jdoe at example.org', 'cyrus01.example.org')
+
+  Kolab::cyrusMurderCreateMailbox('info at example.org', 'cyrus04.example.org',
+        'shared'
+    )
+
+=cut
+
+    my $location = '';
+
+    my $mailboxName = shift;
+    my $mailHost = shift || 0;
+    my $mailboxType = shift || 'user';
+
+    my $connectAddress = (
+            $mailHost ? $mailHost :
+            ( $Kolab::config{'connect_addr'} ?
+                    $Kolab::config{'connect_addr'} : 'localhost'
+            )
+        );
+
+    my $cyrus = Kolab::Cyrus::create($connectAddress);
+
+    # Make whether the mailbox we're about to create is a shared or a user
+    # folder a boolean.
+    my $sharedFolder = ( $mailboxType eq 'user' ? 0 : 1 );
+
+    Kolab::Cyrus::createMailbox( $cyrus, $mailboxName, $sharedFolder);
+
+    my $mailboxPath = Kolab::Cyrus::createUid($mailboxName, $sharedFolder);
+
+    use Mail::IMAPClient;
+
+    my $imap = Mail::IMAPClient->new(
+            Server => $connectAddress,
+            User => $Kolab::config{'cyrus_admin'},
+            Password => $Kolab::config{'cyrus_admin_pw'},
+        );
+
+    if (! $imap) {
+        die("Cannot connect to mail server '$connectAddress' to locate mailbox - $!");
+    }
+
+    my @results = $imap->tag_and_run(qq/GETANNOTATION $mailboxPath "*" "value.shared"/);
+
+    $imap->logout;
+
+    foreach my $r (@results) {
+        $r =~ s/\r//g;
+        $r =~ s/\n//g;
+        if ($r =~ /\/vendor\/cmu\/cyrus-imapd\/server" \("value.shared" "(.*)"\)$/) {
+            $location = $1;
+        }
+    }
+
+    if ($location eq '') {
+        die("Cannot locate mailbox '$mailboxPath'.\n");
+    }
+
+    return $location;
+}
+
+################################################################################
+sub cyrusMurderRenameMailbox {
+################################################################################
+=pod
+
+=head1 Rename a Mailbox in a Cyrus Murder
+
+=head2 cyrusMurderRenameMailbox
+
+This function renames a mailbox in a Cyrus IMAP Murder. It expects to be passed
+a mailbox name, such as 'jdoe', 'jdoe at example.org' or 'info at company.com', and
+the new mailbox name, such as 'jsixpack', 'jsixpack at example.org' or
+'sales at company.com'.
+
+A third argument may be specified to indicate the folder is a 'shared' folder.
+If the folder type is not specified, 'user' is used as the prefix.
+
+The function connects to the configured 'connect_addr', which should be set to
+a (functional) Cyrus IMAP frontend server using /etc/kolab/kolab.conf.
+
+It creates the administrative connection using the 'cyrus_admin' and
+'cyrus_admin_pw' settings from /etc/kolab/kolab.conf, and issues the rename
+command.
+
+Example usage:
+
+  Kolab::cyrusMurderRenameMailbox('jdoe at example.org', 'jsixpack at example.org)
+
+  Kolab::cyrusMurderCreateMailbox('jdoe at example.org', 'cyrus01.example.org')
+
+  Kolab::cyrusMurderCreateMailbox('info at example.org', 'sales at example.org',
+        'shared'
+    )
+
+=cut
+
+    my $location = '';
+
+    my $mailboxName = shift;
+    my $newMailboxName = shift;
+    my $mailboxType = shift || 'user';
+
+    my $connectAddress = ( $Kolab::config{'connect_addr'} ?
+            $Kolab::config{'connect_addr'} : 'localhost'
+        );
+
+    my $cyrus = Kolab::Cyrus::create($connectAddress);
+
+    # Make whether the mailbox we're about to create is a shared or a user
+    # folder a boolean.
+    my $sharedFolder = ( $mailboxType eq 'user' ? 0 : 1 );
+
+    Kolab::Cyrus::renameMailbox(
+            $cyrus,
+            $mailboxName,
+            $newMailboxName,
+            $sharedFolder
+        );
+}
+
 1;
 __END__
 =head1 NAME
diff --git a/lib/Kolab/Cyrus.pm b/lib/Kolab/Cyrus.pm
index f4462af..17b1adf 100644
--- a/lib/Kolab/Cyrus.pm
+++ b/lib/Kolab/Cyrus.pm
@@ -42,6 +42,7 @@ our %EXPORT_TAGS = (
         &createMailbox
         &createCalendar
         &deleteMailbox
+        &renameMailbox
         &setQuota
         &setACL
     ) ]
@@ -57,12 +58,15 @@ our $VERSION = '0.9';
 
 sub create
 {
-    Kolab::log('Y', 'Connecting to local Cyrus admin interface');
 
-    my $cyrus = Cyrus::IMAP::Admin->new($Kolab::config{'connect_addr'});
+    my $connectAddress = shift || $Kolab::config{'connect_addr'};
+
+    Kolab::log('Y', 'Connecting to Cyrus at ' . $connectAddress);
+
+    my $cyrus = Cyrus::IMAP::Admin->new($connectAddress);
 
     if (!$cyrus) {
-        Kolab::log('Y', 'Unable to connect to local Cyrus admin interface', KOLAB_ERROR);
+        Kolab::log('Y', 'Unable to connect to Cyrus admin interface', KOLAB_ERROR);
         return 0;
     }
 
@@ -101,13 +105,14 @@ sub createMailbox
 
     my $cyruid = &createUid($uid, $sf);
     my $mailbox = ($cyrus->list($cyruid))[0];
+
     if ($uid && ($uid ne $Kolab::config{'cyrus_admin'}) && ($uid ne "freebusy") && ($uid ne "nobody") && !defined($mailbox)) {
         Kolab::log('Y', "Creating mailbox `$cyruid' on ".($partition?"partition `$partition'":"default partition"));
         if (!$cyrus->create($cyruid, $partition)) {
             Kolab::log('Y', "Unable to create mailbox `$cyruid', Error = `" . $cyrus->error . "'", KOLAB_WARN);
         }
     } else {
-        Kolab::log('Y', "Skipping mailbox creation for $uid (curuid='$cyruid', mailbox='".join(',',@{$mailbox})."'", KOLAB_DEBUG);
+        Kolab::log('Y', "Skipping mailbox creation for $uid (cyruid='$cyruid', mailbox='".join(',',@{$mailbox})."'", KOLAB_DEBUG);
     }
 }
 
@@ -189,6 +194,22 @@ sub deleteMailbox
     }
 }
 
+sub renameMailbox
+{
+    my $cyrus = shift;
+    my $mailboxName = shift;
+    my $newMailboxName = shift;
+    my $sharedFolder = shift || 0;
+    my $mailboxPath = &createUid($mailboxName, $sharedFolder);
+    my $newMailboxPath = &createUid($newMailboxName, $sharedFolder);
+
+    Kolab::log('Y', "Renaming mailbox from `$mailboxPath' to `$newMailboxPath'");
+
+    if (!$cyrus->renamemailbox($mailboxPath, $newMailboxPath)) {
+        Kolab::log('Y', "Unable to rename mailbox `$mailboxPath' to `$newMailboxPath', Error = `" . $cyrus->error . "'", KOLAB_WARN);
+    }
+}
+
 sub setACL
 {
     my $cyrus = shift;


commit 48e39bcece647e3f65ab44c7dce8bcb18b052346
Author: Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen at kolabsys.com>
Date:   Wed May 4 12:19:10 2011 -0400

    Increment version number beyond 2.3.z

diff --git a/lib/Kolab.pm b/lib/Kolab.pm
index dd814f0..05f6b38 100644
--- a/lib/Kolab.pm
+++ b/lib/Kolab.pm
@@ -59,7 +59,7 @@ our @EXPORT = qw(
 );
 
 # The Kolab version number for the perl-kolab package
-our $KOLAB_BASE_VERSION = "2.3.1";
+our $KOLAB_BASE_VERSION = "2.4";
 
 # Are current releases cvs based or is this a real release?
 my $KOLAB_GIT = 1;





More information about the commits mailing list