Way too many changes to mention. The 'rm' mode works now.
[mailshears.git] / bin / mailshears
1 #!/usr/bin/ruby -wKU
2 #
3 # mailshears, to prune unused mail directories.
4 #
5 # Mail accounts for virtual hosts are stored in SQL, and managed by
6 # Postfixadmin. However, the physical directories are handled by
7 # Postfix/Dovecot and are left untouched by Postfixadmin. This is good
8 # for security, but comes at a cost: Postfixadmin can't remove a
9 # user's mail directory when his or her account is deleted.
10 #
11 # This program compares the list of filesystem accounts with the ones
12 # in the database. It outputs any accounts that exist in the
13 # filesystem, but not the database.
14 #
15
16 # Define a usage string using the program name.
17 exe = File.basename($PROGRAM_NAME)
18 usage = "#{exe} [prune | rm <target> | mv <src> <dst>]"
19
20 # Defaults
21 mode_name = 'prune'
22 mode = :prune
23
24 # Now, if a mode was supplied, it should be in ARGV[0].
25 if ARGV.length() > 0
26 mode_names = ['prune', 'rm', 'mv']
27 if mode_names.include?(ARGV.first().downcase()) then
28 # Peel the mode name off the head of the list.
29 mode_name = ARGV.shift()
30 end
31 end
32
33 if mode_name == 'rm' then
34 mode = :rm
35 elsif mode_name == 'mv' then
36 mode = :mv
37 end
38
39 # Need this before referencing ExitCodes.
40 require 'mailshears'
41
42 # Since we removed both the executable name and the mode name (if it
43 # existed) from ARGV, what remains should be the required
44 # arguments.
45 if (mode == :prune and (ARGV.length() != 0)) or
46 (mode == :rm and (ARGV.length() < 1)) or
47 (mode == :mv and (ARGV.length() != 2)) then
48 puts "ERROR: missing (or extra) command-line arguments."
49 puts "Usage: #{usage}"
50 Kernel.exit(ExitCodes::BAD_COMMAND_LINE)
51 end
52
53 cfg = Configuration.new()
54
55 # Load each of the plugins that we'll need.
56 cfg.plugins.each do |plugin_file|
57 require "#{mode_name}/plugins/#{plugin_file}"
58 end
59
60 # Always enabled, for now.
61 require "#{mode_name}/plugins/postfixadmin_db"
62
63 # And the runners.
64 require "#{mode_name}/#{mode_name}_runner"
65 require "#{mode_name}/#{mode_name}_dummy_runner"
66
67 def make_header(plugin_name)
68 # The header that we output before the list of domains/accounts.
69 # Just the path of this script, the current time, and the plugin name.
70 exe = File.basename($PROGRAM_NAME)
71 header = "#{exe}, "
72
73 current_time = Time.now()
74 if current_time.respond_to?(:iso8601)
75 # Somehow this method is missing on some machines.
76 header += current_time.iso8601.to_s
77 else
78 # Fall back to whatever this looks like.
79 header += current_time.to_s
80 end
81
82 header += ' (Plugin: ' + plugin_name + ")\n"
83 header += '-' * header.size # Underline the header.
84
85 return header
86 end
87
88
89 plugin_class = nil
90 runner_class = nil
91 dummy_runner_class = nil
92
93 if mode == :rm then
94 plugin_class = RmPlugin
95 runner_class = RmRunner
96 dummy_runner_class = RmDummyRunner
97 elsif mode == :mv then
98 plugin_class = MvPlugin
99 runner_class = MvRunner
100 dummy_runner_class = MvDummyRunner
101 else
102 # Safe, catch-all default
103 plugin_class = PrunePlugin
104 runner_class = PruneRunner
105 dummy_runner_class = PruneDummyRunner
106 end
107
108 puts make_header(plugin_class.to_s())
109
110 plugin_class.includers.each do |plugin_class_includer|
111 plugin = plugin_class_includer.new()
112
113 if cfg.i_mean_business then
114 runner = runner_class.new()
115 else
116 runner = dummy_runner_class.new()
117 end
118
119 # The splat passes the correct (we hope) number of arguments to the
120 # appropriate runner. The Rm(Dummy)Runner have splats on their
121 # *target arguments as well, to turn ARGV back into an array.
122 runner.run(plugin, *ARGV)
123
124 puts ""
125 end
126
127
128
129 Kernel.exit(0)
130
131
132 begin
133 # Get a list of domains from the Postfixadmin database.
134 db_domains = pgadb.get_domains_from_db()
135 rescue DatabaseError => e
136 puts "There was an error connecting to the database: #{e.to_s}"
137 Kernel.exit(ExitCodes::DATABASE_ERROR)
138 end
139
140 begin
141 # And the accounts.
142 db_accounts = pgadb.get_accounts_from_db()
143 rescue DatabaseError => e
144 puts "There was an error connecting to the database: #{e.to_s}"
145 Kernel.exit(ExitCodes::DATABASE_ERROR)
146 end
147
148
149 Plugin.includers.each do |plugin_class_includer|
150 plugin = plugin_class_includer.new()
151
152 begin
153 leftover_domains = plugin.get_leftover_domains(db_domains)
154 rescue StandardError => e
155 puts "There was an error retrieving domains from the filesystem: #{e.to_s}"
156 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
157 end
158
159 begin
160 leftover_accounts = plugin.get_leftover_accounts(db_accounts)
161 rescue StandardError => e
162 puts "There was an error retrieving accounts from the filesystem: #{e.to_s}"
163 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
164 end
165
166 if leftover_domains.size > 0 or leftover_accounts.size > 0
167 puts make_header(plugin_class.to_s())
168
169 leftover_domains.each do |domain|
170 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
171 end
172
173 leftover_accounts.each do |account|
174 puts "Found: #{account} (#{plugin.describe_account(account)})"
175 end
176
177 if cfg.i_mean_business
178 # We have to delete the accounts before the domain,
179 # otherwise they'd already be gone.
180 leftover_accounts.each do |account|
181 # Get the description before we delete the domain.
182 # This can still fail if the account's domain is gone.
183 account_description = plugin.describe_account(account)
184 plugin.delete_account(account)
185 puts "Removed: #{account} (#{account_description})"
186 end
187
188 leftover_domains.each do |domain|
189 # Get the description before we delete the domain.
190 domain_description = plugin.describe_domain(domain)
191 plugin.delete_domain(domain)
192 puts "Removed: #{domain} (#{domain_description})"
193 end
194 end
195
196 puts ""
197 end
198
199 end