From 6211ed741ea97719a96da6d6db8aed39f4fd05e9 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 29 Oct 2015 22:53:26 -0400 Subject: [PATCH] Get DovecotMv just barely working. --- lib/mv/plugins/dovecot.rb | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/mv/plugins/dovecot.rb b/lib/mv/plugins/dovecot.rb index b1dc598..9cdfc36 100644 --- a/lib/mv/plugins/dovecot.rb +++ b/lib/mv/plugins/dovecot.rb @@ -1,3 +1,5 @@ +require 'fileutils' + require 'common/filesystem' require 'common/dovecot_plugin' require 'mv/mv_plugin' @@ -7,16 +9,35 @@ class DovecotMv include DovecotPlugin include MvPlugin + def mv_user(user_from, user_to) + # Move the user's mail directory from one domain (that should + # exist) to another domain (that should exist). + # + # This is actually a little subtle, because none of the + # directories in question have to exist. If I create two new + # domains in postfixadmin and one mailbox but never deliver any + # mail, it should be possible to move the mailbox to the other + # domain. No directories will exist until mail is delivered, + # though. - def mv_domain(from, to) - from_path = self.get_domain_path(from) - to_path = self.get_domain_path(to) - FileUtils.mv(from_path, to_path) - end + # get_user_path() will fail if there's no mailbox to move. It will + # also fail on an invalid user -- oh well! + begin + from_path = self.get_user_path(user_from) + rescue NonexistentUserError + # Do nothing, there's no source mailbox... + return + end + + # We may need to create the target domain directory, even if the + # domain exists in the database. + domain_to = user_to.split('@')[1] + domain_dir = self.get_domain_path(domain_to) + FileUtils.mkdir_p(domain_dir) - def mv_user(from, to) - from_path = self.get_user_path(from) - to_path = self.get_user_path(to) + # The from_path exists or else we would have returned earlier. The + # parent of to_path exists because we just created it. + to_path = self.get_user_path(user_to, false) FileUtils.mv(from_path, to_path) end -- 2.43.2