]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_mailstore_plugin.rb
90aa02c8fde2b582c93afda07902a36ea17c5722
[mailshears.git] / lib / common / dovecot_mailstore_plugin.rb
1 require 'common/plugin'
2 require 'common/filesystem'
3
4 module DovecotMailstorePlugin
5 # Code that all DovecotMailstore plugins (Prune, Rm, Mv...) will
6 # share. That is, we implement the Plugin interface.
7 include Plugin
8
9 def initialize
10 cfg = Configuration.new()
11 @domain_root = cfg.mail_root
12 end
13
14 def describe_domain(domain)
15 begin
16 domain_path = get_domain_path(domain)
17 return domain_path
18 rescue NonexistentDomainError => e
19 return "Doesn't exist: #{e.to_s}"
20 end
21 end
22
23 def describe_account(account)
24 begin
25 account_path = get_account_path(account)
26 return account_path
27 rescue NonexistentAccountError => e
28 return "Doesn't exist: #{e.to_s}"
29 end
30 end
31
32
33 protected;
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)
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 msg = "#{account}: Accounts must contain an '@' symbol."
54 raise InvalidAccountError.new(msg)
55 end
56
57 account_parts = account.split('@')
58 user_part = account_parts[0]
59 domain_part = account_parts[1]
60
61 begin
62 domain_path = get_domain_path(domain_part)
63 rescue NonexistentDomainError
64 raise NonexistentAccountError.new(account)
65 end
66
67 account_path = File.join(domain_path, user_part)
68
69 if File.directory?(account_path)
70 return account_path
71 else
72 raise NonexistentAccountError.new(account)
73 end
74 end
75
76
77 def list_domains()
78 return Filesystem.get_subdirs(@domain_root)
79 end
80
81 def list_domains_users(domains)
82 accounts = []
83
84 domains.each do |domain|
85 begin
86 # Throws a NonexistentDomainError if the domain's path
87 # doesn't exist on the filesystem. In this case, we want
88 # to report zero accounts.
89 domain_path = get_domain_path(domain)
90 usernames = Filesystem.get_subdirs(domain_path)
91
92 usernames.each do |username|
93 accounts << "#{username}@#{domain}"
94 end
95 rescue NonexistentDomainError
96 # Party hard.
97 end
98 end
99
100 return accounts
101 end
102
103
104 def list_users()
105 domains = list_domains()
106 users = list_domains_users(domains)
107 return users
108 end
109
110 end