Document everything with YARD and fix some bugs along the way.
[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
8 # Handle moving (renaming) Dovecot users on the filesystem.
9 #
10 class DovecotMv
11
12 include DovecotPlugin
13 include MvPlugin
14
15
16 # Move the Dovecot user *src* to *dst*. This relocates the user's
17 # directory within the Dovecot mailstore (on the filesystem).
18 #
19 # This fails if the source user does not exist, or if the
20 # destination user already exists before the move.
21 #
22 # But is it an error if the target domain does not exist? That's a
23 # bit subtle... The domain may exist in the database, but if it
24 # has not received any mail yet, then its directory won't exist
25 # on-disk.
26 #
27 # There are two possible "oops" scenarios resulting from the fact
28 # that we may run either the Postfixadmin move first or the
29 # Dovecot move first. If we move the user in the database, we
30 # definitely want to move him on disk (that is, we should create
31 # the directory here). But if we move him on disk first, then we
32 # don't know if the database move will fail! We don't want to move
33 # his mail files if he won't get moved in the database.
34 #
35 # Faced with two equally-bad (but easy-to-fix) options, we do the
36 # simplest thing and fail if the destination domain directory
37 # doesn't exist. If nothing else, this is at least consistent.
38 #
39 # @param src [User] the source user to be moved.
40 #
41 # @param dst [User] the destination user being moved to.
42 #
43 def mv_user(src, dst)
44 raise NonexistentUserError.new(src.to_s()) if not user_exists(src)
45 raise UserAlreadyExistsError.new(dst.to_s()) if user_exists(dst)
46
47 # See the docstring...
48 if not self.domain_exists(dst.domain()) then
49 raise NonexistentDomainError.new(dst.domainpart())
50 end
51
52 # We may need to create the target domain directory, even if the
53 # domain exists in the database.
54 FileUtils.mkdir_p(self.get_domain_path(dst.domain()))
55
56 # The parent of dst_path exists because we just created it.The
57 # source path should exist too, because the "source user" does,
58 # and, well, how did we determine that?
59 src_path = self.get_user_path(src)
60 dst_path = self.get_user_path(dst)
61 FileUtils.mv(src_path, dst_path)
62 end
63
64 end