summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-05-07 20:50:12 (GMT)
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-05-07 20:50:12 (GMT)
commitaec02a3a38b88d01d12bda3469fecb3de20ac067 (patch)
treea4e2138f6c91c2e6c1e309d5b175f138812c9e1c
parent48e39bcece647e3f65ab44c7dce8bcb18b052346 (diff)
downloadperl-Kolab-aec02a3a38b88d01d12bda3469fecb3de20ac067.tar.gz
Create 2 new API calls:
Kolab::cyrusMurderCreateMailbox Kolab::cyrusMurderRenameMailbox
-rw-r--r--lib/Kolab.pm173
-rw-r--r--lib/Kolab/Cyrus.pm29
2 files changed, 195 insertions, 7 deletions
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@example.org' or 'info@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@example.org')
+
+ Kolab::cyrusMurderCreateMailbox('jdoe@example.org', 'cyrus01.example.org')
+
+ Kolab::cyrusMurderCreateMailbox('info@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@example.org' or 'info@company.com', and
+the new mailbox name, such as 'jsixpack', 'jsixpack@example.org' or
+'sales@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@example.org', 'jsixpack@example.org)
+
+ Kolab::cyrusMurderCreateMailbox('jdoe@example.org', 'cyrus01.example.org')
+
+ Kolab::cyrusMurderCreateMailbox('info@example.org', 'sales@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;