]> gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
Print the plugin name on the same line as the executable, time.
[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 require 'mailshears'
17
18 cfg = Configuration.new()
19 pgadb = PostfixadminDb.new(cfg.dbhost,
20 cfg.dbport,
21 cfg.dbopts,
22 cfg.dbtty,
23 cfg.dbname,
24 cfg.dbuser,
25 cfg.dbpass)
26
27
28 begin
29 # Get a list of domains from the Postfixadmin database.
30 db_domains = pgadb.get_domains_from_db()
31 rescue DatabaseError => e
32 puts "There was an error connecting to the database: #{e.to_s}"
33 Kernel.exit(ExitCodes::DATABASE_ERROR)
34 end
35
36 begin
37 # And the accounts.
38 db_accounts = pgadb.get_accounts_from_db()
39 rescue DatabaseError => e
40 puts "There was an error connecting to the database: #{e.to_s}"
41 Kernel.exit(ExitCodes::DATABASE_ERROR)
42 end
43
44
45 Plugin.includers.each do |plugin_class|
46 plugin = plugin_class.new()
47
48 begin
49 leftover_domains = plugin.get_leftover_domains(db_domains)
50 rescue StandardError => e
51 puts "There was an error retrieving domains from the filesystem: #{e.to_s}"
52 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
53 end
54
55 begin
56 leftover_accounts = plugin.get_leftover_accounts(db_accounts)
57 rescue StandardError => e
58 puts "There was an error retrieving accounts from the filesystem: #{e.to_s}"
59 Kernel.exit(ExitCodes::FILESYSTEM_ERROR)
60 end
61
62 if leftover_domains.size > 0 or leftover_accounts.size > 0
63 # The header that we output before the list of domains/accounts.
64 # Just the path of this script, the current time, and the plugin name.
65 header = "#{$0}, "
66
67 current_time = Time.now()
68 if current_time.respond_to?(:iso8601)
69 # Somehow this method is missing on some machines.
70 header += current_time.iso8601.to_s
71 else
72 # Fall back to whatever this looks like.
73 header += current_time.to_s
74 end
75
76 header += '(Plugin: ' + plugin_class.to_s + ")\n"
77 puts header
78 puts '-' * header.size # Underline the header.
79
80 leftover_domains.each do |domain|
81 puts "Found: #{domain} (#{plugin.describe_domain(domain)})"
82 end
83
84 leftover_accounts.each do |account|
85 puts "Found: #{account} (#{plugin.describe_account(account)})"
86 end
87
88 if cfg.i_mean_business
89 # We have to delete the accounts before the domain,
90 # otherwise they'd already be gone.
91 leftover_accounts.each do |account|
92 # Get the description before we delete the domain.
93 # This can still fail if the account's domain is gone.
94 account_description = plugin.describe_account(account)
95 plugin.delete_account(account)
96 puts "Removed: #{account} (#{account_description})"
97 end
98
99 leftover_domains.each do |domain|
100 # Get the description before we delete the domain.
101 domain_description = plugin.describe_domain(domain)
102 plugin.delete_domain(domain)
103 puts "Removed: #{domain} (#{domain_description})"
104 end
105 end
106
107 puts ""
108 end
109
110 end