Added error handling to the domain/account path methods.
[mailshears.git] / src / dovecot_mailstore.rb
1 require 'src/errors'
2 require 'src/filesystem'
3 require 'src/mailstore'
4
5 class DovecotMailstore < Mailstore
6
7 def get_domains_from_filesystem()
8 return Filesystem.get_subdirs(@domain_root)
9 end
10
11
12 def get_accounts_from_filesystem(domains)
13 accounts = []
14
15 domains.each do |domain|
16 begin
17 # Throws a NonexistentDomainError if the domain's path
18 # doesn't exist on the filesystem. In this case, we want
19 # to report zero accounts.
20 domain_path = get_domain_path(domain)
21 usernames = Filesystem.get_subdirs(domain_path)
22
23 usernames.each do |username|
24 accounts << "#{username}@#{domain}"
25 end
26 rescue NonexistentDomainError => e
27 # Party hard.
28 end
29 end
30
31 return accounts
32 end
33
34
35 def get_domain_path(domain)
36 # Return the filesystem path for the given domain.
37 # That is, the directory where its mail is stored.
38 # Only works if the domain directory exists!
39 domain_path = File.join(@domain_root, domain)
40
41 if File.directory?(domain_path)
42 return domain_path
43 else
44 raise NonexistentDomainError.new(domain_path)
45 end
46 end
47
48
49 def get_account_path(account)
50 # Return the filesystem path of this account's mailbox.
51 # Only works if the account exists!
52 if not account.include?('@')
53 raise InvalidAccountError.new("#{account}: Accounts must contain an '@' symbol.")
54 end
55
56 account_parts = account.split('@')
57 user_part = account_parts[0]
58 domain_part = account_parts[1]
59
60 domain_path = get_domain_path(domain_part)
61 account_path = File.join(domain_path, user_part)
62
63 if File.directory?(account_path)
64 return account_path
65 else
66 raise NonexistentAccountError.new(account_path)
67 end
68 end
69
70 end