]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
f7b1963cc8bde0a49cfc388fa29d963cf8e3db55
3 # mailshears, to prune unused mail directories.
5 # Mail users 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.
11 # This program compares the list of filesystem users with the ones
12 # in the database. It outputs any users that exist in the
13 # filesystem, but not the database.
16 # Define a usage string using the program name.
17 exe
= File
.basename($PROGRAM_NAME)
18 usage
= "#{exe} [prune | rm <target> | mv <src> <dst>]"
24 # Now, if a mode was supplied, it should be in ARGV[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()
33 if mode_name
== 'rm' then
35 elsif mode_name
== 'mv' then
39 # Need this before referencing ExitCodes.
42 # Since we removed both the executable name and the mode name (if it
43 # existed) from ARGV, what remains should be the required
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)
53 cfg
= Configuration
.new()
55 # Load each of the plugins that we'll need.
56 cfg
.plugins
.each
do |plugin_file
|
57 require "#{mode_name}/plugins/#{plugin_file}"
61 require "#{mode_name}/#{mode_name}_runner"
62 require "#{mode_name}/#{mode_name}_dummy_runner"
64 def make_header(plugin_name
)
65 # The header that we output before the list of domains/users.
66 # Just the path of this script, the current time, and the plugin name.
67 exe
= File
.basename($PROGRAM_NAME)
70 current_time
= Time
.now()
71 if current_time
.respond_to
?(:iso8601)
72 # Somehow this method is missing on some machines.
73 header +
= current_time
.iso8601
.to_s
75 # Fall back to whatever this looks like.
76 header +
= current_time
.to_s
79 header +
= ' (Plugin: ' + plugin_name +
")\n"
80 header +
= '-' * header
.size
# Underline the header.
88 dummy_runner_class
= nil
91 plugin_class
= RmPlugin
92 runner_class
= RmRunner
93 dummy_runner_class
= RmDummyRunner
94 elsif mode
== :mv then
95 plugin_class
= MvPlugin
96 runner_class
= MvRunner
97 dummy_runner_class
= MvDummyRunner
99 # Safe, catch-all default
100 plugin_class
= PrunePlugin
101 runner_class
= PruneRunner
102 dummy_runner_class
= PruneDummyRunner
106 # Buffer the output so that we can avoid printing the informational
107 # header when no plugins produce output.
109 output_buffer
= StringIO
.new()
110 $stdout = output_buffer
112 plugin_class
.includers
.each
do |plugin_class_includer
|
113 plugin
= plugin_class_includer
.new(cfg
)
115 if cfg
.i_mean_business
then
116 runner
= runner_class
.new()
118 runner
= dummy_runner_class
.new()
121 # The splat passes the correct (we hope) number of arguments to the
122 # appropriate runner. The Rm(Dummy)Runner have splats on their
123 # *target arguments as well, to turn ARGV back into an array.
124 runner
.run(plugin
, *ARGV)
127 # Restore stdout, and print the header plus whatever the plugins
128 # produced if they produced anything. If they didn't, we avoid
129 # printing the header.
131 if output_buffer
.size
> 0 then
132 puts
make_header(plugin_class
.to_s())
133 puts output_buffer
.string()