]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_plugin.rb
2496bae2eade64be5889c4a5057b8ac00e4b5228
1 require 'common/plugin'
2 require 'common/filesystem'
5 # Code that all Dovecot plugins (Prune, Rm, Mv...) will
6 # share. That is, we implement the Plugin interface.
10 cfg
= Configuration
.new()
11 @domain_root = cfg
.mail_root
14 def describe_domain(domain
)
16 domain_path
= get_domain_path(domain
)
18 rescue NonexistentDomainError
=> e
19 return "Doesn't exist: #{e.to_s}"
23 def describe_account(account
)
25 account_path
= get_account_path(account
)
27 rescue NonexistentAccountError
=> e
28 return "Doesn't exist: #{e.to_s}"
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
)
41 if File
.directory
?(domain_path
)
44 raise NonexistentDomainError
.new(domain
)
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
)
57 account_parts
= account
.split('@')
58 user_part
= account_parts
[0]
59 domain_part
= account_parts
[1]
62 domain_path
= get_domain_path(domain_part
)
63 rescue NonexistentDomainError
64 raise NonexistentAccountError
.new(account
)
67 account_path
= File
.join(domain_path
, user_part
)
69 if File
.directory
?(account_path
)
72 raise NonexistentAccountError
.new(account
)
78 return Filesystem
.get_subdirs(@domain_root)
81 def list_domains_users(domains
)
84 domains
.each
do |domain
|
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
)
92 usernames
.each
do |username
|
93 accounts
<< "#{username}@#{domain}"
95 rescue NonexistentDomainError
105 domains
= list_domains()
106 users
= list_domains_users(domains
)