migrateAdmin.patch

Davide Principi, 02/27/2013 04:40 PM

Download (2.13 KB)

View differences:

root/etc/e-smith/events/actions/nethserver-mail-migrate-mailboxes
26 26
use User::pwent;
27 27
use esmith::AccountsDB;
28 28
use File::Path;
29
use File::Temp;
29 30

  
30 31
my $event = shift;
31 32
my $sourceDir = shift;
......
33 34

  
34 35
my $accountsDb = esmith::AccountsDB->open() or die("Could not open destination account db\n");
35 36

  
37

  
38
#
39
# Migrate admin's mailbox
40
#
41
if( ! migrateAdminMailbox($sourceDir)) {
42
    $errors ++;
43
}
44

  
45
#
46
# Migrate user mailboxes
47
#
36 48
foreach my $accountRecord ($accountsDb->users()) {
37 49

  
38 50
    my $userName = $accountRecord->key;
......
81 93
# action ends:
82 94
exit(0);
83 95

  
96

  
97
#
98
#
99
#
100
sub migrateAdminMailbox($)
101
{
102
    my $srcMaildir = $_[0] . '/home/e-smith/Maildir';
103

  
104
    # "By default the directory is deleted when the object goes out of scope":
105
    my $tmpMaildir = File::Temp->newdir('/var/tmp/migrateAdminMailbox.XXXXXXXXX');
106

  
107
    if( ! -d $srcMaildir) {
108
	warn "[WARNING] admin's mailbox not found: skipped\n";
109
	return 1; #  SUCCESS
110
    }
111

  
112
    if( ! migrateDir($srcMaildir, $tmpMaildir)) {
113
	return 0;
114
    }
115

  
116
    system(qw(chown -R vmail.vmail), $tmpMaildir);
117
    if($? != 0) {
118
	warn "[ERROR] failed to change ownership of temporary maildir\n";
119
	return 0;
120
    }
121

  
122
    system(qw(/usr/bin/doveadm import -u admin), 'maildir:' . $tmpMaildir, 'import-' . time(), 'ALL');
123
    if($? != 0) {
124
	warn "[ERROR] error while importing admin Maildir: $!\n";
125
	return 0;
126
    }
127

  
128
    if( ! open(PH, '-|', '/usr/bin/doveadm mailbox list -u admin')) {
129
	warn "[ERROR] could not list admin's mailbox\n";
130
	return 0;
131
    }
132

  
133
    while( ! eof(PH)) {
134
	my $mailboxName = <PH>;
135
	chomp($mailboxName);
136
	system(qw(/usr/bin/doveadm mailbox subscribe -u admin), $mailboxName);
137
	if($? != 0) {
138
	    warn "[WARNING] could not subscribe mailbox $mailboxName\n";
139
	}
140
    }
141

  
142
    close(PH);
143
    
144
    return 1; # SUCCESS
145
}