]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/dovecot_plugin.rb
Fix ownership issues in the agendav.sql test schema.
[mailshears.git] / lib / common / dovecot_plugin.rb
1 require 'common/domain'
2 require 'common/filesystem'
3 require 'common/plugin'
4 require 'common/user'
5
6 # Code that all Dovecot plugins ({DovecotPrune}, {DovecotRm}, and
7 # {DovecotMv}) will share.
8 #
9 module DovecotPlugin
10
11 # We implement the Plugin "interface."
12 include Plugin
13
14
15 # Initialize this Dovecot {Plugin} with values in *cfg*.
16 #
17 # @param cfg [Configuration] the configuration for this plugin.
18 #
19 def initialize(cfg)
20 @domain_root = cfg.dovecot_mail_root
21 end
22
23 # Describe the given Dovecot domain by its filesystem path. The
24 # domain need not exist to obtain its path.
25 #
26 # @param domain [Domain] the {Domain} object whose description we want.
27 #
28 # @return [String] a String giving the path under which this domain's
29 # mailboxes would reside on the filesystem.
30 #
31 def describe_domain(domain)
32 return get_domain_path(domain)
33 end
34
35
36 # Describe the given Dovecot user by its filesystem mailbox
37 # path. The user need not exist to obtain its mailbox path.
38 #
39 # @param user [User] the {User} object whose description we want.
40 #
41 # @return [String] a String giving the path where this user's
42 # mailbox would reside on the filesystem.
43 #
44 def describe_user(user)
45 return get_user_path(user)
46 end
47
48
49 protected;
50
51 # Return the filesystem path for the given {Domain} object.
52 #
53 # @param domain [Domain] the {Domain} whose path we want.
54 #
55 # @return [String] the filesystem path where this domain's mail
56 # would be located.
57 #
58 def get_domain_path(domain)
59 return File.join(@domain_root, domain.to_s())
60 end
61
62
63 # Return the filesystem path of this {User}'s mailbox.
64 #
65 # @param user [User] the {User} whose mailbox path we want.
66 #
67 # @return [String] the filesystem path where this user's mail
68 # would be located.
69 #
70 def get_user_path(user)
71 domain_path = get_domain_path(user.domain())
72 return File.join(domain_path, user.localpart())
73 end
74
75
76 # Produce a list of domains that exist in the Dovecot mailstore.
77 #
78 # @return [Array<Domain>] an array of {Domain} objects that have
79 # corresponding directories within the Dovecot mailstore.
80 #
81 def list_domains()
82 return Filesystem.get_subdirs(@domain_root).map{ |d| Domain.new(d) }
83 end
84
85
86 # Produce a list of users belonging to the given *domains* in the
87 # Dovecot mailstore.
88 #
89 # @param domains [Array<Domain>] an array of {Domain} objects whose
90 # users we'd like to find.
91 #
92 # @return [Array<User>] an array of {User} objects that have
93 # corresponding directories within the Dovecot mailstore belonging
94 # to the specified *domains*.
95 #
96 def list_domains_users(domains)
97 users = []
98
99 domains.each do |domain|
100 begin
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)
106
107 usernames.each do |username|
108 users << User.new("#{username}@#{domain}")
109 end
110 rescue NonexistentDomainError
111 # Party hard.
112 end
113 end
114
115 return users
116 end
117
118
119 # Produce a list of all users in the Dovecot mailstore.
120 #
121 # @return [Array<User>] a list of users who have mailbox directories
122 # within the Dovecot mailstore.
123 #
124 def list_users()
125 domains = list_domains()
126 users = list_domains_users(domains)
127 return users
128 end
129
130 end