8ab507d674c3c77739d854ad9d95439b702531ee
[mailshears.git] / lib / rm / plugins / dovecot_mailstore.rb
1 # Needed for rm_r.
2 require 'fileutils'
3
4 require 'common/filesystem'
5 require 'common/mailstore'
6 require 'common/dovecot_mailstore_plugin'
7 require 'rm/rm_plugin'
8
9 class DovecotMailstoreRm < Mailstore
10
11 include DovecotMailstorePlugin
12 include RmPlugin
13
14
15 def delete_domain(domain)
16 domain_path = self.get_domain_path(domain)
17 FileUtils.rm_r(domain_path)
18 end
19
20 def delete_account(account)
21 account_path = self.get_account_path(account)
22 FileUtils.rm_r(account_path)
23 end
24
25 def get_leftover_domains(db_domains)
26 # Get the list of domains according to the filesystem.
27 fs_domains = self.get_domains_from_filesystem()
28
29 # Return the list of domains on the filesystem that aren't in the DB.
30 return (fs_domains - db_domains)
31 end
32
33 def get_leftover_accounts(db_accounts)
34 # Get the list of accounts according to the filesystem.
35 fs_domains = self.get_domains_from_filesystem()
36 fs_accounts = self.get_accounts_from_filesystem(fs_domains)
37
38 # And return the accounts on the filesystem that aren't in the DB.
39 return (fs_accounts - db_accounts)
40 end
41
42
43 def get_domain_usernames(domain)
44 return get_accounts_from_filesystem([domain])
45 end
46
47 protected;
48
49 def get_domains_from_filesystem()
50 return Filesystem.get_subdirs(@domain_root)
51 end
52
53 def get_accounts_from_filesystem(domains)
54 accounts = []
55
56 domains.each do |domain|
57 begin
58 # Throws a NonexistentDomainError if the domain's path
59 # doesn't exist on the filesystem. In this case, we want
60 # to report zero accounts.
61 domain_path = get_domain_path(domain)
62 usernames = Filesystem.get_subdirs(domain_path)
63
64 usernames.each do |username|
65 accounts << "#{username}@#{domain}"
66 end
67 rescue NonexistentDomainError
68 # Party hard.
69 end
70 end
71
72 return accounts
73 end
74
75 end