9cdfc36ead95a013f03cacadd5763c674ea521fc
[mailshears.git] / lib / mv / plugins / dovecot.rb
1 require 'fileutils'
2
3 require 'common/filesystem'
4 require 'common/dovecot_plugin'
5 require 'mv/mv_plugin'
6
7 class DovecotMv
8
9 include DovecotPlugin
10 include MvPlugin
11
12 def mv_user(user_from, user_to)
13 # Move the user's mail directory from one domain (that should
14 # exist) to another domain (that should exist).
15 #
16 # This is actually a little subtle, because none of the
17 # directories in question have to exist. If I create two new
18 # domains in postfixadmin and one mailbox but never deliver any
19 # mail, it should be possible to move the mailbox to the other
20 # domain. No directories will exist until mail is delivered,
21 # though.
22
23 # get_user_path() will fail if there's no mailbox to move. It will
24 # also fail on an invalid user -- oh well!
25 begin
26 from_path = self.get_user_path(user_from)
27 rescue NonexistentUserError
28 # Do nothing, there's no source mailbox...
29 return
30 end
31
32 # We may need to create the target domain directory, even if the
33 # domain exists in the database.
34 domain_to = user_to.split('@')[1]
35 domain_dir = self.get_domain_path(domain_to)
36 FileUtils.mkdir_p(domain_dir)
37
38 # The from_path exists or else we would have returned earlier. The
39 # parent of to_path exists because we just created it.
40 to_path = self.get_user_path(user_to, false)
41 FileUtils.mv(from_path, to_path)
42 end
43
44 end