]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/mv/plugins/dovecot.rb
Get DovecotMv just barely working.
[mailshears.git] / lib / mv / plugins / dovecot.rb
index 0eb688861442aba8e07e8cad640a92ce7ec30abc..9cdfc36ead95a013f03cacadd5763c674ea521fc 100644 (file)
@@ -1,23 +1,43 @@
+require 'fileutils'
+
 require 'common/filesystem'
-require 'common/mailstore'
 require 'common/dovecot_plugin'
 require 'mv/mv_plugin'
 
-class DovecotMv < Mailstore
+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_account(from, to)
-    from_path = self.get_account_path(from)
-    to_path = self.get_account_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