bc6166d6a1716ee7954083fb10460ad00e54b30d
[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
7 module DovecotPlugin
8 # Code that all Dovecot plugins (Prune, Rm, Mv...) will
9 # share. That is, we implement the Plugin interface.
10 include Plugin
11
12 def initialize(cfg)
13 @domain_root = cfg.mail_root
14 end
15
16 def describe_domain(domain)
17 return get_domain_path(domain)
18 end
19
20 def describe_user(user)
21 return get_user_path(user)
22 end
23
24
25 protected;
26
27 def get_domain_path(domain)
28 # Return the filesystem path for the given domain.
29 # That is, the directory where its mail is stored.
30 # Only works if the domain directory exists!
31 return File.join(@domain_root, domain.to_s())
32 end
33
34
35 def get_user_path(user)
36 # Return the filesystem path of this user's mailbox.
37 domain_path = get_domain_path(user.domain())
38 return File.join(domain_path, user.localpart())
39 end
40
41
42 def list_domains()
43 return Filesystem.get_subdirs(@domain_root).map{ |d| Domain.new(d) }
44 end
45
46 def list_domains_users(domains)
47 users = []
48
49 domains.each do |domain|
50 begin
51 # Throws a NonexistentDomainError if the domain's path
52 # doesn't exist on the filesystem. In this case, we want
53 # to report zero users.
54 domain_path = get_domain_path(domain)
55 usernames = Filesystem.get_subdirs(domain_path)
56
57 usernames.each do |username|
58 users << User.new("#{username}@#{domain}")
59 end
60 rescue NonexistentDomainError
61 # Party hard.
62 end
63 end
64
65 return users
66 end
67
68
69 def list_users()
70 domains = list_domains()
71 users = list_domains_users(domains)
72 return users
73 end
74
75 end