]> gitweb.michael.orlitzky.com - mailshears.git/blob - src/plugins/dovecot_mailstore.rb
d253c3250bb693c486e8891506983c137a606222
[mailshears.git] / src / plugins / dovecot_mailstore.rb
1 require 'src/errors'
2 require 'src/filesystem'
3 require 'src/mailstore'
4 require 'src/plugin'
5
6 class DovecotMailstore < Mailstore
7
8 include Plugin
9
10 def initialize
11 @domain_root = Configuration::MAIL_ROOT
12 end
13
14 def describe_domain(domain)
15 return get_domain_path(domain)
16 end
17
18 def describe_account(account)
19 begin
20 account_path = get_account_path(account)
21 return account_path
22 rescue NonexistentAccountError => e
23 return e.message
24 end
25 end
26
27 def delete_domain(domain)
28 domain_path = self.get_domain_path(domain)
29 FileUtils.rm_rf(domain_path)
30 end
31
32 def delete_account(account)
33 account_path = self.get_account_path(account)
34 FileUtils.rm_rf(account_path)
35 end
36
37 def get_leftover_domains(db_domains)
38 # Get the list of domains according to the filesystem.
39 fs_domains = self.get_domains_from_filesystem()
40
41 # Return the list of domains on the filesystem that aren't in the DB.
42 return (fs_domains - db_domains)
43 end
44
45 def get_leftover_accounts(db_accounts)
46 # Get the list of accounts according to the filesystem.
47 fs_domains = self.get_domains_from_filesystem()
48 fs_accounts = self.get_accounts_from_filesystem(fs_domains)
49
50 # And return the accounts on the filesystem that aren't in the DB.
51 return (fs_accounts - db_accounts)
52 end
53
54 protected;
55
56 def get_domains_from_filesystem()
57 return Filesystem.get_subdirs(@domain_root)
58 end
59
60 def get_accounts_from_filesystem(domains)
61 accounts = []
62
63 domains.each do |domain|
64 begin
65 # Throws a NonexistentDomainError if the domain's path
66 # doesn't exist on the filesystem. In this case, we want
67 # to report zero accounts.
68 domain_path = get_domain_path(domain)
69 usernames = Filesystem.get_subdirs(domain_path)
70
71 usernames.each do |username|
72 accounts << "#{username}@#{domain}"
73 end
74 rescue NonexistentDomainError => e
75 # Party hard.
76 end
77 end
78
79 return accounts
80 end
81
82
83 def get_domain_path(domain)
84 # Return the filesystem path for the given domain.
85 # That is, the directory where its mail is stored.
86 # Only works if the domain directory exists!
87 domain_path = File.join(@domain_root, domain)
88
89 if File.directory?(domain_path)
90 return domain_path
91 else
92 raise NonexistentDomainError.new(domain_path)
93 end
94 end
95
96
97 def get_account_path(account)
98 # Return the filesystem path of this account's mailbox.
99 # Only works if the account exists!
100 if not account.include?('@')
101 raise InvalidAccountError.new("#{account}: Accounts must contain an '@' symbol.")
102 end
103
104 account_parts = account.split('@')
105 user_part = account_parts[0]
106 domain_part = account_parts[1]
107
108 domain_path = get_domain_path(domain_part)
109 account_path = File.join(domain_path, user_part)
110
111 if File.directory?(account_path)
112 return account_path
113 else
114 raise NonexistentAccountError.new(account_path)
115 end
116 end
117
118 end