]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_mailstore_plugin.rb
ff901ee43417e8e7d66562763792aeb75e13eb14
[mailshears.git] / lib / common / dovecot_mailstore_plugin.rb
1 require 'common/plugin'
2
3 module DovecotMailstorePlugin
4 # Code that all DovecotMailstore plugins (Prune, Rm, Mv...) will
5 # share. That is, we implement the Plugin interface.
6 include Plugin
7
8 def initialize
9 cfg = Configuration.new()
10 @domain_root = cfg.mail_root
11 end
12
13 def describe_domain(domain)
14 begin
15 domain_path = get_domain_path(domain)
16 return domain_path
17 rescue NonexistentDomainError => e
18 return "Doesn't exist: #{e.to_s}"
19 end
20 end
21
22 def describe_account(account)
23 begin
24 account_path = get_account_path(account)
25 return account_path
26 rescue NonexistentAccountError => e
27 return "Doesn't exist: #{e.to_s}"
28 end
29 end
30
31
32 protected;
33
34 def get_domain_path(domain)
35 # Return the filesystem path for the given domain.
36 # That is, the directory where its mail is stored.
37 # Only works if the domain directory exists!
38 domain_path = File.join(@domain_root, domain)
39
40 if File.directory?(domain_path)
41 return domain_path
42 else
43 raise NonexistentDomainError.new(domain)
44 end
45 end
46
47
48 def get_account_path(account)
49 # Return the filesystem path of this account's mailbox.
50 # Only works if the account exists!
51 if not account.include?('@')
52 raise InvalidAccountError.new("#{account}: Accounts must contain an '@' symbol.")
53 end
54
55 account_parts = account.split('@')
56 user_part = account_parts[0]
57 domain_part = account_parts[1]
58
59 begin
60 domain_path = get_domain_path(domain_part)
61 rescue NonexistentDomainError
62 raise NonexistentAccountError.new(account)
63 end
64
65 account_path = File.join(domain_path, user_part)
66
67 if File.directory?(account_path)
68 return account_path
69 else
70 raise NonexistentAccountError(account)
71 end
72 end
73
74 end