+require 'fileutils'
+
require 'common/filesystem'
require 'common/dovecot_plugin'
require 'mv/mv_plugin'
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