]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_plugin.rb
1 require 'common/domain'
2 require 'common/filesystem'
3 require 'common/plugin'
6 # Code that all Dovecot plugins ({DovecotPrune}, {DovecotRm}, and
7 # {DovecotMv}) will share.
11 # We implement the Plugin "interface."
15 # Initialize this Dovecot {Plugin} with values in *cfg*.
17 # @param cfg [Configuration] the configuration for this plugin.
20 @domain_root = cfg
.dovecot_mail_root
23 # Describe the given Dovecot domain by its filesystem path. The
24 # domain need not exist to obtain its path.
26 # @param domain [Domain] the {Domain} object whose description we want.
28 # @return [String] a String giving the path under which this domain's
29 # mailboxes would reside on the filesystem.
31 def describe_domain(domain
)
32 return get_domain_path(domain
)
36 # Describe the given Dovecot user by its filesystem mailbox
37 # path. The user need not exist to obtain its mailbox path.
39 # @param user [User] the {User} object whose description we want.
41 # @return [String] a String giving the path where this user's
42 # mailbox would reside on the filesystem.
44 def describe_user(user
)
45 return get_user_path(user
)
51 # Return the filesystem path for the given {Domain} object.
53 # @param domain [Domain] the {Domain} whose path we want.
55 # @return [String] the filesystem path where this domain's mail
58 def get_domain_path(domain
)
59 return File
.join(@domain_root, domain
.to_s())
63 # Return the filesystem path of this {User}'s mailbox.
65 # @param user [User] the {User} whose mailbox path we want.
67 # @return [String] the filesystem path where this user's mail
70 def get_user_path(user
)
71 domain_path
= get_domain_path(user
.domain())
72 return File
.join(domain_path
, user
.localpart())
76 # Produce a list of domains that exist in the Dovecot mailstore.
78 # @return [Array<Domain>] an array of {Domain} objects that have
79 # corresponding directories within the Dovecot mailstore.
82 return Filesystem
.get_subdirs(@domain_root).map
{ |d
| Domain
.new(d
) }
86 # Produce a list of users belonging to the given *domains* in the
89 # @param domains [Array<Domain>] an array of {Domain} objects whose
90 # users we'd like to find.
92 # @return [Array<User>] an array of {User} objects that have
93 # corresponding directories within the Dovecot mailstore belonging
94 # to the specified *domains*.
96 def list_domains_users(domains
)
99 domains
.each
do |domain
|
101 # Throws a NonexistentDomainError if the domain's path
102 # doesn't exist on the filesystem. In this case, we want
103 # to report zero users.
104 domain_path
= get_domain_path(domain
)
105 usernames
= Filesystem
.get_subdirs(domain_path
)
107 usernames
.each
do |username
|
108 users
<< User
.new("#{username}@#{domain}")
110 rescue NonexistentDomainError
119 # Produce a list of all users in the Dovecot mailstore.
121 # @return [Array<User>] a list of users who have mailbox directories
122 # within the Dovecot mailstore.
125 domains
= list_domains()
126 users
= list_domains_users(domains
)