]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_plugin.rb
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 @domain_root = cfg
.mail_root
13 def describe_domain(domain
)
15 domain_path
= get_domain_path(domain
)
17 rescue NonexistentDomainError
=> e
18 return "Doesn't exist: #{e.to_s}"
22 def describe_user(user
)
24 user_path
= get_user_path(user
)
26 rescue NonexistentUserError
=> e
27 return "Doesn't exist: #{e.to_s}"
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
)
40 if File
.directory
?(domain_path
)
43 raise NonexistentDomainError
.new(domain
)
48 def get_user_path(user
)
49 # Return the filesystem path of this user's mailbox.
50 # Only works if the user exists!
51 if not user
.include?('@')
52 msg
= "#{user}: Users must contain an '@' symbol."
53 raise InvalidUserError
.new(msg
)
56 user_parts
= user
.split('@')
57 local_part
= user_parts
[0]
58 domain_part
= user_parts
[1]
61 domain_path
= get_domain_path(domain_part
)
62 rescue NonexistentDomainError
63 raise NonexistentUserError
.new(user
)
66 user_path
= File
.join(domain_path
, local_part
)
68 if File
.directory
?(user_path
)
71 raise NonexistentUserError
.new(user
)
77 return Filesystem
.get_subdirs(@domain_root)
80 def list_domains_users(domains
)
83 domains
.each
do |domain
|
85 # Throws a NonexistentDomainError if the domain's path
86 # doesn't exist on the filesystem. In this case, we want
87 # to report zero users.
88 domain_path
= get_domain_path(domain
)
89 usernames
= Filesystem
.get_subdirs(domain_path
)
91 usernames
.each
do |username
|
92 users
<< "#{username}@#{domain}"
94 rescue NonexistentDomainError
104 domains
= list_domains()
105 users
= list_domains_users(domains
)