+require 'src/errors'
require 'src/filesystem'
require 'src/mailstore'
class DovecotMailstore < Mailstore
+ def initialize
+ @domain_root = Configuration::MAIL_ROOT
+ end
+
+ def describe_domain(domain)
+ return get_domain_path(domain)
+ end
+
+ def describe_account(account)
+ return get_account_path(account)
+ end
+
+ def delete_domain(domain)
+ domain_path = self.get_domain_path(domain)
+ FileUtils.rm_rf(domain_path)
+ end
+
+ def delete_account(account)
+ account_path = self.get_account_path(account)
+ FileUtils.rm_rf(account_path)
+ end
+
+ def get_leftover_domains(db_domains)
+ # Get the list of domains according to the filesystem.
+ fs_domains = self.get_domains_from_filesystem()
+
+ # Return the list of domains on the filesystem that aren't in the DB.
+ return (fs_domains - db_domains)
+ end
+
+ def get_leftover_accounts(db_accounts)
+ # Get the list of accounts according to the filesystem.
+ fs_domains = self.get_domains_from_filesystem()
+ fs_accounts = self.get_accounts_from_filesystem(fs_domains)
+
+ # And return the accounts on the filesystem that aren't in the DB.
+ return (fs_accounts - db_accounts)
+ end
+
+ protected;
+
def get_domains_from_filesystem()
return Filesystem.get_subdirs(@domain_root)
end
-
def get_accounts_from_filesystem(domains)
accounts = []
domains.each do |domain|
- domain_path = File.join(@domain_root, domain)
- usernames = Filesystem.get_subdirs(domain_path)
+ begin
+ # Throws a NonexistentDomainError if the domain's path
+ # doesn't exist on the filesystem. In this case, we want
+ # to report zero accounts.
+ domain_path = get_domain_path(domain)
+ usernames = Filesystem.get_subdirs(domain_path)
- usernames.each do |username|
- accounts << "#{username}@#{domain}"
+ usernames.each do |username|
+ accounts << "#{username}@#{domain}"
+ end
+ rescue NonexistentDomainError => e
+ # Party hard.
end
end
return accounts
end
+
+ 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!
+ domain_path = File.join(@domain_root, domain)
+
+ if File.directory?(domain_path)
+ return domain_path
+ else
+ raise NonexistentDomainError.new(domain_path)
+ end
+ end
+
+
+ def get_account_path(account)
+ # Return the filesystem path of this account's mailbox.
+ # Only works if the account exists!
+ if not account.include?('@')
+ raise InvalidAccountError.new("#{account}: Accounts must contain an '@' symbol.")
+ end
+
+ account_parts = account.split('@')
+ user_part = account_parts[0]
+ domain_part = account_parts[1]
+
+ domain_path = get_domain_path(domain_part)
+ account_path = File.join(domain_path, user_part)
+
+ if File.directory?(account_path)
+ return account_path
+ else
+ raise NonexistentAccountError.new(account_path)
+ end
+ end
+
end