ispman/lib mailbox_actions_cyrus.lib,1.2.2.8,1.2.2.9

Joerg Delker <[email protected]> Tue, 24 Oct 2006 21:01:47 +0000
Newsgroups gmane.comp.isp.ispman.cvs
Message-ID <[email protected]>
Update of /cvsroot/ispman/ispman/lib
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv17299/lib

Modified Files:
      Tag: dev_1_3-cyrus_virtdomain2
	mailbox_actions_cyrus.lib 
Log Message:
adjusted to new confVars
modified some logic

Index: mailbox_actions_cyrus.lib
===================================================================
RCS file: /cvsroot/ispman/ispman/lib/mailbox_actions_cyrus.lib,v
retrieving revision 1.2.2.8
retrieving revision 1.2.2.9
diff -u -d -r1.2.2.8 -r1.2.2.9
--- mailbox_actions_cyrus.lib	8 Oct 2006 23:49:47 -0000	1.2.2.8
+++ mailbox_actions_cyrus.lib	24 Oct 2006 21:01:45 -0000	1.2.2.9
@@ -1,39 +1,33 @@
 # All mailbox actions necessary for Cyrus IMAP Server
+#
+# All methods take the primary mailbox address as arguments (mailLocalAddress)
+# Depending on ISPMan configuration the correct mailbox name for cyrus is
+# derived from that
 
 use IMAP::Admin;
 
-my $allowFullEmailLogins = 0;
-my $cyrusUnixHierarchySep = 0;
+# some convienence defaults
+my $cyrusVirtDomainFeature =
+  lc( $ispman->getConf("cyrusVirtDomainFeature") ) eq 'yes' ? 1 : 0;
+my $cyrusPrefix = $ispman->getConf('imapMailboxPrefix');
+my $cyrusSep    = substr( $cyrusPrefix, -1 );
 
-# Define a TRUE/FALSE value to decrease the LDAP calls and
-# to make logic easier
-if ( lc( $ispman->getConf("allowFullEmailLogins") ) eq 'yes' ) {
-    $allowFullEmailLogins = 1;
-}
+# static imap connection handle
+my $imap_conn;
 
-if ( lc( $ispman->getConf('cyrusUnixHierarchySep') ) eq 'yes' ) {
-    $cyrusUnixHierarchySep = 1;
-}
-    
+# sanity checks
+die "Fatal: invalid cyrus hierarchy separator \"$cyrusSep\""
+  unless ( $cyrusSep eq '.' || $cyrusSep eq '/' );
 
-# Helper funtion that get's the correct mailbox name from the user's UID
-sub get_correct_mailbox {
-    my $_uid = shift;
-    if ( $allowFullEmailLogins ) {
-        $_uid = $ispman->getUserInfo($_uid)->{'mailLocalAddress'};
-    }
-    return $_uid;
-}
+# Helper funtion that get's the correct mailbox name from the mail address
+sub mail2mailbox {
+    my $mail = shift;
+    my $box  = $mail;
 
-# imap connection handle
-my $imap_conn;
+    $box =~ s/\.|@/_/g unless $allowFullEmailLogins;
 
-my $imap_mailbox_prefix = $ispman->getConf('imapMailboxPrefix');
-my $sep = '.';
-if ( $cyrusUnixHierarchySep ) {
-    $sep = '/';
+    return $cyrusPrefix . $box;
 }
-$imap_mailbox_prefix .= $sep;
 
 sub _connect {
     my $server = shift;
@@ -48,26 +42,33 @@
 
 sub create_mailbox {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . get_correct_mailbox(shift);
+    my $mailbox = mail2mailbox(shift);
 
     $imap_conn or _connect($server);
 
     if ( $imap_conn->create($mailbox) ) {
-        print $imap_conn->error."\n";
+        print $imap_conn->error . "\n";
         return 1;
     }
+
+    # create any defaults folders
+    my @folders = split( ' ', $ispman->getConf('defaultMailboxFolders') );
+    for (@folders) {
+        create_folder( $server, $mailbox, $_ );
+    }
+
     return 0;
 }
 
 sub delete_mailbox {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . get_correct_mailbox(shift);
+    my $mailbox = mail2mailbox(shift);
 
     $imap_conn or _connect($server);
     $imap_conn->set_acl( $mailbox, $ispman->getConf("imapAdminUsername"),
         "lrswipcda" );
     if ( $imap_conn->delete($mailbox) ) {
-        print $imap_conn->error."\n";
+        print $imap_conn->error . "\n";
         return 1;
     }
     return 0;
@@ -75,46 +76,62 @@
 
 sub create_folder {
     my $server  = shift;
-    my $addr = get_correct_mailbox(shift);
-    my $mailbox = $imap_mailbox_prefix . $addr;
+    my $mailbox = mail2mailbox(shift);
     my $folder  = shift;
 
     $imap_conn or _connect($server);
 
     if ( $imap_conn->list($mailbox) ) {
-    	# mailbox exists. Lets continue
-        if ( $allowFullEmailLogins ) {
-            ( $addr_start, $addr_end ) = split( '@', $addr );
-            if ( $imap_conn->list( join '',
-                    ( $imap_mailbox_prefix, $addr_start, $sep, $folder, '@', $addr_end)) ) {
+
+        # mailbox exists. Lets continue
+        if ($allowFullEmailLogins) {
+            ( $upath, $domain ) = split( '@', $mailbox );
+            my $tmpbox = $upath . $cyrusSep . $folder . '@' . $domain;
+            if ( $imap_conn->list($tmpbox) ) {
+
                 # Already Exists
-                print "$folder on $addr_start\@$addr_end already exists. Skipping\n";
+                print "$folder on $mailbox already exists. Skipping\n";
                 return 2;
-            } else {
-                if ( $imap_conn->set_acl( $mailbox, $ispman->getConf("imapAdminUsername"), "lrswipdca" ) ) {
-                    print $imap_conn->error."\n";
+            }
+            else {
+                if (
+                    $imap_conn->set_acl(
+                        $mailbox, $ispman->getConf("imapAdminUsername"),
+                        "lrswipdca"
+                    )
+                  )
+                {
+                    print $imap_conn->error . "\n";
                     return 1;
                 }
-                print "Creating $folder on $addr_start\@$addr_end\n";
-                my $tmbox = join '', ( $imap_mailbox_prefix, $addr_start, $sep, $folder, '@', $addr_end);
-                if ( $imap_conn->create( $tmbox ) ) {
-                    print $imap_conn->error."\n";
+                print "Creating $folder on $mailbox\n";
+                if ( $imap_conn->create($tmpbox) ) {
+                    print $imap_conn->error . "\n";
                     return 1;
                 }
             }
-        } else {
-            if ( $imap_conn->list( join '.', ( $mailbox, $folder ) ) ) {
+        }
+        else {
+            if ( $imap_conn->list( $mailbox . $cyrusSep . $folder ) ) {
+
                 # Already Exists
                 print "$folder on $mailbox already exists. Skipping\n";
                 return 2;
-            } else {
-                if ( $imap_conn->set_acl( $mailbox, $ispman->getConf("imapAdminUsername"), "lrswipdca" ) ) {
-                    print $imap_conn->error."\n";
+            }
+            else {
+                if (
+                    $imap_conn->set_acl(
+                        $mailbox, $ispman->getConf("imapAdminUsername"),
+                        "lrswipdca"
+                    )
+                  )
+                {
+                    print $imap_conn->error . "\n";
                     return 1;
                 }
                 print "Creating $folder on $mailbox\n";
-                if ( $imap_conn->create( join '.', ( $mailbox, $folder ) ) ) {
-                    print $imap_conn->error."\n";
+                if ( $imap_conn->create( $mailbox . $cyrusSep . $folder ) ) {
+                    print $imap_conn->error . "\n";
                     return 1;
                 }
             }
@@ -125,11 +142,11 @@
 
 sub get_quota {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . get_correct_mailbox(shift);
+    my $mailbox = mail2mailbox(shift);
 
     $imap_conn or _connect($server);
     if ( $imap_conn->get_quota($mailbox) ) {
-        print $imap_conn->error."\n";
+        print $imap_conn->error . "\n";
         return 1;
     }
     return 0;
@@ -137,12 +154,12 @@
 
 sub set_quota {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . get_correct_mailbox(shift);
+    my $mailbox = mail2mailbox(shift);
     my $quota   = shift;
 
     $imap_conn or _connect($server);
     if ( $imap_conn->set_quota( $mailbox, $quota ) ) {
-        print $imap_conn->error."\n";
+        print $imap_conn->error . "\n";
         return 1;
     }
     return 0;


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642