Overhaul everything to get consistent error reports.
[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(src, dst)
13 # It's obviously an error if the source user does not exist.
14 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
15
16 # And it's an error if the destination user exists already.
17 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
18
19 # But is it an error if the target domain does not exist? That's a
20 # bit subtle... The domain may exist in the database, but if it
21 # has not received any mail yet, then its directory won't exist
22 # on-disk.
23 #
24 # There are two possible "oops" scenarios resulting from the fact
25 # that we may run either the Postfixadmin move first or the
26 # Dovecot move first. If we move the user in the database, we
27 # definitely want to move him on disk (that is, we should create
28 # the directory here). But if we move him on disk first, then we
29 # don't know if the database move will fail! We don't want to move
30 # his mail files if he won't get moved in the database.
31 #
32 # Faced with two equally-bad (but easy-to-fix) options, we do the
33 # simplest thing and fail if the destination domain directory
34 # doesn't exist. If nothing else, this is at least consistent.
35 if not self.domain_exists(dst.domain()) then
36 raise NonexistentDomainError.new(dst.domainpart())
37 end
38
39 # We may need to create the target domain directory, even if the
40 # domain exists in the database.
41 FileUtils.mkdir_p(self.get_domain_path(dst.domain()))
42
43 # The parent of dst_path exists because we just created it.The
44 # source path should exist too, because the "source user" does,
45 # and, well, how did we determine that?
46 src_path = self.get_user_path(src)
47 dst_path = self.get_user_path(dst)
48 FileUtils.mv(src_path, dst_path)
49 end
50
51 end