ispman/lib mailbox_actions.lib, 1.1, 1.2 mailbox_actions_cyrus.lib, 1.2, 1.3

Joerg Delker <[email protected]> Tue, 21 Nov 2006 14:41:31 +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-serv21579/lib

Modified Files:
	mailbox_actions.lib mailbox_actions_cyrus.lib 
Log Message:
merged branch dev_1_3-cyrus_virtdomain2

Index: mailbox_actions.lib
===================================================================
RCS file: /cvsroot/ispman/ispman/lib/mailbox_actions.lib,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mailbox_actions.lib	7 Sep 2006 21:24:56 -0000	1.1
+++ mailbox_actions.lib	21 Nov 2006 14:41:29 -0000	1.2
@@ -1,5 +1,9 @@
 # Wrapper script for all mailbox actions
 # Depending on configured imapServerBackend, the appropiate library is loaded
+#
+# All methods take the primary mailbox address (LDAP: mailLocalAddress)
+# The particular mail backend libs need to derive their real mailbox name
+# from that
 
 use ISPMan;
 
@@ -11,16 +15,5 @@
 # load mailbox functions for this particular type
 require "mailbox_actions_$imapServerBackend.lib";
 
-# helper function to convert email into uid notation
-sub email2uid {
-    my $str = shift;
-    ( $uid, $domain ) = split( '@', $str );
-    if ($domain) {
-        $domain =~ s/\./_/g;
-        $str = join '_', ( $uid, $domain );
-    }
-    return $str;
-}
-
 1;
 

Index: mailbox_actions_cyrus.lib
===================================================================
RCS file: /cvsroot/ispman/ispman/lib/mailbox_actions_cyrus.lib,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- mailbox_actions_cyrus.lib	12 Sep 2006 13:04:34 -0000	1.2
+++ mailbox_actions_cyrus.lib	21 Nov 2006 14:41:29 -0000	1.3
@@ -1,11 +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;
 
-# imap connection handle
+# some convienence defaults
+my $cyrusVirtDomainFeature =
+  lc( $ispman->getConf("cyrusVirtDomainFeature") ) eq 'yes' ? 1 : 0;
+my $cyrusPrefix = $ispman->getConf('imapMailboxPrefix');
+my $cyrusSep    = substr( $cyrusPrefix, -1 );
+
+# static imap connection handle
 my $imap_conn;
 
-my $imap_mailbox_prefix = $ispman->getConf("imapMailboxPrefix");
+# 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 mail address
+sub mail2mailbox {
+    my $mail = shift;
+    my $box  = $mail;
+
+    $box =~ s/\.|@/_/g unless $cyrusVirtDomainFeature;
+
+    return $cyrusPrefix . $box;
+}
 
 sub _connect {
     my $server = shift;
@@ -20,26 +42,34 @@
 
 sub create_mailbox {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . shift;
+    my $mail = shift;
+    my $mailbox = mail2mailbox($mail);
 
     $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, $mail, $_ );
+    }
+
     return 0;
 }
 
 sub delete_mailbox {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . 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;
@@ -47,24 +77,77 @@
 
 sub create_folder {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . shift;
+    my $mailbox = mail2mailbox(shift);
     my $folder  = shift;
 
     $imap_conn or _connect($server);
-    if ( $imap_conn->create( join ".", ( $mailbox, $folder ) ) ) {
-        print $imap_conn->error."\n";
-        return 1;
+
+    if ( $imap_conn->list($mailbox) ) {
+
+        # mailbox exists. Lets continue
+        if ($cyrusVirtDomainFeature) {
+            ( $upath, $domain ) = split( '@', $mailbox );
+            my $tmpbox = $upath . $cyrusSep . $folder . '@' . $domain;
+            if ( $imap_conn->list($tmpbox) ) {
+
+                # 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";
+                    return 1;
+                }
+                print "Creating $folder on $mailbox\n";
+                if ( $imap_conn->create($tmpbox) ) {
+                    print $imap_conn->error . "\n";
+                    return 1;
+                }
+            }
+        }
+        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";
+                    return 1;
+                }
+                print "Creating $folder on $mailbox\n";
+                if ( $imap_conn->create( $mailbox . $cyrusSep . $folder ) ) {
+                    print $imap_conn->error . "\n";
+                    return 1;
+                }
+            }
+        }
     }
     return 0;
 }
 
 sub get_quota {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . 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;
@@ -72,12 +155,12 @@
 
 sub set_quota {
     my $server  = shift;
-    my $mailbox = $imap_mailbox_prefix . 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";
+    if ( $imap_conn->set_quota( $mailbox, $quota*1024 ) ) {
+        print $imap_conn->error . "\n";
         return 1;
     }
     return 0;


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV