From: Michael Orlitzky Date: Fri, 30 Oct 2015 02:53:26 +0000 (-0400) Subject: Get DovecotMv just barely working. X-Git-Tag: 0.0.1~45 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=6211ed741ea97719a96da6d6db8aed39f4fd05e9;p=mailshears.git Get DovecotMv just barely working. --- 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