]> gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
mailshears.gemspec: bump version to 0.1.0
[mailshears.git] / bin / mailshears
1 #!/usr/bin/ruby -wU
2 #
3 # mailshears, to mangle your mail garden
4 #
5
6 # Load all of our lib/ code.
7 require 'mailshears'
8
9 # Define a usage string using the program name.
10 program_name = File.basename($PROGRAM_NAME)
11
12 # Defaults
13 mode_name = 'prune'
14 mode = :prune
15
16 # Before doing anything else, check for "-h" and "--help" in the args,
17 # because those should cause us to dump usage info and bail out.
18 if ARGV.include?('-h') or ARGV.include?('--help') then
19 puts "Usage: #{UserInterface.usage(program_name)}"
20 Kernel.exit(ExitCodes::SUCCESS)
21 end
22
23
24 # 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 # Determine the mode from its name.
34 if mode_name == 'rm' then
35 mode = :rm
36 elsif mode_name == 'mv' then
37 mode = :mv
38 end
39
40 # Since we removed the mode name (if it existed) from ARGV, what
41 # remains should be the required arguments. Figure out if we have the
42 # wrong number of arguments, and store the associated error message in
43 # args_error_message if necessary.
44 args_error_message = nil
45
46 if mode == :prune and ARGV.length() != 0 then
47 args_error_message = "ERROR: prune mode takes no additional arguments."
48 elsif mode == :rm and ARGV.length() < 1 then
49 args_error_message = "ERROR: rm mode takes two or more user arguments."
50 elsif mode == :mv and ARGV.length() != 2 then
51 args_error_message = "ERROR: mv mode takes exactly two user arguments."
52 end
53
54 # If we got the wrong number of arguments, we'll have an error message
55 # here. Report it and exit with a failure code.
56 if not args_error_message.nil? then
57 STDERR.puts args_error_message
58 puts "Usage: #{UserInterface.usage(program_name)}"
59 Kernel.exit(ExitCodes::BAD_COMMAND_LINE)
60 end
61
62
63 # Load each of the plugins that we'll need.
64 cfg = Configuration.new()
65
66 cfg.plugins.each do |plugin_file|
67 require "#{mode_name}/plugins/#{plugin_file}"
68 end
69
70 # And the runners.
71 require "#{mode_name}/#{mode_name}_runner"
72 require "#{mode_name}/#{mode_name}_dummy_runner"
73
74 # Now we figure out which plugin module to use based on our mode.
75 plugin_module = nil
76
77 if mode == :rm then
78 plugin_module = RmPlugin
79 elsif mode == :mv then
80 plugin_module = MvPlugin
81 else
82 # Safe, catch-all default
83 plugin_module = PrunePlugin
84 end
85
86 # Parse the remaining arguments as User/Domain objects. If we get some
87 # other argument that isn't one of those, it's an error.
88 parsed_args = []
89
90 ARGV.each do |arg|
91 begin
92 u = User.new(arg)
93 parsed_args << u
94 rescue InvalidUserError
95 begin
96 d = Domain.new(arg)
97 parsed_args << d
98 rescue InvalidDomainError
99 STDERR.puts "ERROR: invalid user/domain argument #{arg}"
100 Kernel.exit(ExitCodes::BAD_COMMAND_LINE)
101 end
102 end
103 end
104
105
106 # Buffer the output so that we can avoid printing the informational
107 # header when no plugins produce output.
108 require 'stringio'
109 output_buffer = StringIO.new()
110 $stdout = output_buffer
111
112 begin
113 plugin_module.run(cfg, *parsed_args)
114 ensure
115 # Now restore stdout, and print the header plus whatever the plugins
116 # produced (if they produced anything). If they didn't produce any
117 # output, then we avoid printing the header.
118 #
119 # This gets wrapped in an "ensure" block because otherwise, if
120 # plugin_module.run() crashes, the traceback will get stored in
121 # output_buffer and never get printed.
122 $stdout = STDOUT
123
124 if output_buffer.size > 0 then
125 puts UserInterface.make_header(program_name, plugin_module.to_s())
126 puts output_buffer.string()
127 end
128 end
129
130 # If we made it here without crashing, well that sounds pretty
131 # successful to me.
132 Kernel.exit(ExitCodes::SUCCESS)