require 'common/domain' require 'common/filesystem' require 'common/plugin' require 'common/user' module DovecotPlugin # Code that all Dovecot plugins (Prune, Rm, Mv...) will # share. That is, we implement the Plugin interface. include Plugin def initialize(cfg) @domain_root = cfg.dovecot_mail_root end def describe_domain(domain) return get_domain_path(domain) end def describe_user(user) return get_user_path(user) end protected; def get_domain_path(domain) # Return the filesystem path for the given domain. # That is, the directory where its mail is stored. # Only works if the domain directory exists! return File.join(@domain_root, domain.to_s()) end def get_user_path(user) # Return the filesystem path of this user's mailbox. domain_path = get_domain_path(user.domain()) return File.join(domain_path, user.localpart()) end def list_domains() return Filesystem.get_subdirs(@domain_root).map{ |d| Domain.new(d) } end def list_domains_users(domains) users = [] domains.each do |domain| begin # Throws a NonexistentDomainError if the domain's path # doesn't exist on the filesystem. In this case, we want # to report zero users. domain_path = get_domain_path(domain) usernames = Filesystem.get_subdirs(domain_path) usernames.each do |username| users << User.new("#{username}@#{domain}") end rescue NonexistentDomainError # Party hard. end end return users end def list_users() domains = list_domains() users = list_domains_users(domains) return users end end