]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mv/mv_runner.rb
fe44e5c244d3fe4cff66f140bb4d684ca903364a
[mailshears.git] / lib / mv / mv_runner.rb
1 require 'common/domain'
2 require 'common/errors'
3 require 'common/runner'
4
5 # Perform the moving (renaming) of users/domains using {MvPlugin}s.
6 #
7 class MvRunner
8 include Runner
9
10 # Run *plugin* to move the user *src* to *dst*. The method
11 # signature includes the unused *cfg* for consistency with the
12 # runners that do need a {Configuration}.
13 #
14 # @param cfg [Configuration] unused.
15 #
16 # @param plugin [Class] plugin class that will perform the move.
17 #
18 # @param src [User] the source user to be moved.
19 #
20 # @param dst [User] the destination user being moved to.
21 #
22 def run(cfg, plugin, src, dst)
23
24 if src.is_a?(Domain) or dst.is_a?(Domain) then
25 msg = 'only users can be moved'
26 raise NotImplementedError.new(msg)
27 end
28
29 begin
30 src_description = plugin.describe(src)
31 plugin.mv_user(src, dst)
32 dst_description = plugin.describe(dst)
33
34 msg = "Moved user #{src} "
35
36 # Only append the extra description if it's useful.
37 if not src_description.nil? and
38 not src_description.empty? and
39 not src_description == src.to_s() then
40 msg += "(#{src_description}) "
41 end
42
43 msg += "to #{dst}"
44
45 # Only append the extra description if it's useful.
46 if not dst_description.nil? and
47 not dst_description.empty? and
48 not dst_description == dst.to_s() then
49 msg += " (#{dst_description})"
50 end
51
52 msg += "."
53 report(plugin, msg)
54
55 rescue NonexistentUserError => e
56 # This means that the SOURCE user didn't exist, since a
57 # nonexistent destination user is perfectly expected.
58 report(plugin, "Source user #{src.to_s()} not found.")
59 rescue NonexistentDomainError => e
60 # This could mean that the source domain doesn't exist, but in
61 # that case, we just report that the source user doesn't
62 # exist. So a nonexistent domain refers to a nonexistent
63 # DESTINATION domain.
64 report(plugin, "Destination domain #{dst.domainpart()} not found.")
65 rescue UserAlreadyExistsError => e
66 report(plugin, "Destination user #{dst.to_s()} already exists.")
67 end
68 end
69
70 end