ma acc --del should work foerste
authorMatthias Förste foerste@schlittermann.de
Thu, 24 Nov 2011 14:37:23 +0100
branchfoerste
changeset 42 b90dc89e8c66
parent 41 a0ed03bc2e1d
child 43 231984cb81a7
ma acc --del should work
account.pm
--- a/account.pm	Thu Nov 24 11:57:31 2011 +0100
+++ b/account.pm	Thu Nov 24 14:37:23 2011 +0100
@@ -5,6 +5,7 @@
 
 use strict;
 use warnings;
+use File::Path qw(remove_tree);
 use Net::LDAP;
 use Net::LDAP::Constant qw(LDAP_ALREADY_EXISTS LDAP_NO_SUCH_OBJECT LDAP_TYPE_OR_VALUE_EXISTS);
 use Net::LDAP::Entry;
@@ -25,6 +26,7 @@
 sub _mkpw($);
 sub uniq(@);
 sub verbose(@);
+sub _mbox($);
 
 sub OU_ACCOUNTS();
 sub OU_ALIASES();
@@ -93,14 +95,7 @@
     my $mailGroup = [split /,/, $Cf->group || ""];
     my $mailForwardingAddress = [split /,/, $Cf->forward || ""];
     my $pw = _mkpw($Cf->password || "{pwgen}");
-    # assuming usernames of the form localpart@domain
-    $user =~ /@(.+)$/;
-    my $escapes;
-    die "Invalid username '$user'" unless $escapes->{'%u'} = $user
-        and $escapes->{'%1'} = substr $user, 0, 1
-        and $escapes->{'%d'} = $1;
-    my $mbox = $Cf->imap_mail_location;
-    $mbox =~ s/$_/$escapes->{$_}/ for keys %{$escapes};
+    my $mbox = _mbox($user);
 
     if ($mailPrimaryAddress !~ /@/) {
 	$mailPrimaryAddress .= "@" . $Cf->default_domain;
@@ -161,20 +156,35 @@
 	die $r->error if $r->code;
 
 	verbose('ok');
-	verbose(" Password: $pw") if not $Cf->password or $Cf->password eq "{pwgen}";
+        verbose(" Password: $pw") if not $Cf->password or $Cf->password eq "{pwgen}";
     }
 
     if($Cf->mbox) {
+
 	verbose("\n\t$mbox...");
 
-	if (-d $mbox) { verbose('exists') }
-	else {
+	if (-d $mbox) {
+
+            verbose('exists')
+
+        } elsif($imap->capability->{acl}) {
 
             $imap->login($user, $pw) or die $@;
-            # 'list' seems to trigger acl file (and thus the maildir) creation with dovecot
-            $imap->list('', '*') or die $@;
+            # wenn wir acl verwenden,
+            #  * dann triggert 'list' acl file (und damit maildir) erzeugung
+            #    bei dovecot
+            #  * müssen wir dem master nutzer ausdrücklich rechte gewähren
+            #    (lra: sicht-, les- und administrierbar)
+            my $f = $imap->list('', '*') or die $@;
+            $imap->setacl($f->[0]->[2], $Cf->imap_admin, 'lra') or die $@;
             verbose('ok');
-	}
+
+        } else {
+
+            verbose('will be created automatically on first email delivery');
+
+        }
+
 
     }
 
@@ -360,12 +370,14 @@
 	verbose("\n");
 	
 	if ($Cf->mbox) {
-            verbose("\tdeleting mbox for $user...");
-            my $m = user_dovecot( 'LOGIN' )->{'home'};
-            if (not (defined $m and $m)) { verbose("can't determine mbox location - not deleting it"); }
-            elsif (not unlink $m) { verbose("Can't unlink $m: $!"); }
-            else { verbose('ok'); }
-	}
+            my $m = _mbox($user);
+            if (not (defined $m and $m)) {
+                verbose("can't determine mbox location - not deleting it");
+            } else {
+                verbose("\tdeleting $m...");
+                verbose((remove_tree $m) ? 'ok' : " Can't remove '$m': $!");
+            }
+        }
 
 	verbose("\n");
 
@@ -402,6 +414,13 @@
 	#}
 
         $imap->login("$uid*" . $Cf->imap_admin, $imap_password ) or die $@;
+        my $m = $imap->list('', '*') or die $@;
+        use Data::Dumper;
+verbose Dumper $m;
+        my $q = $imap->getquotaroot($m->[0]->[2]) or die $@;
+verbose Dumper $q;
+        $q = $imap->getquota($m->[0]->[2]) or die $@;
+verbose Dumper $q;
 
 	MBOX: {
 	    if (!$imap->list()) {
@@ -453,5 +472,25 @@
     
 } }
 
+sub _mbox($) {
+
+    my ($user) = @_;
+
+    my ($localpart, $domain, $escapes);
+
+    # assuming usernames of the form localpart@domain
+    $user =~ /(.+)@(.+)$/;
+    ($localpart, $domain) = ($1, $2);
+
+    die "Invalid username '$user'" unless $escapes->{'%u'} = $localpart
+        and $escapes->{'%1'} = substr $localpart, 0, 1
+        and $escapes->{'%d'} = $domain;
+    my $mbox = $Cf->imap_mail_location;
+    $mbox =~ s/$_/$escapes->{$_}/ for keys %{$escapes};
+
+    return $mbox;
+
+}
+
 1;
 # vim:sts=4 sw=4 aw ai sm nohlsearch: