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