]>
gitweb.michael.orlitzky.com - mailshears.git/blob - src/plugins/dovecot_mailstore.rb
2 require 'src/filesystem'
3 require 'src/mailstore'
6 class DovecotMailstore
< Mailstore
11 @domain_root = Configuration
::MAIL_ROOT
14 def describe_domain(domain
)
16 domain_path
= get_domain_path(domain
)
18 rescue NonexistentDomainError
=> e
23 def describe_account(account
)
25 account_path
= get_account_path(account
)
27 rescue NonexistentAccountError
=> e
32 def delete_domain(domain
)
33 domain_path
= self.get_domain_path(domain
)
34 FileUtils
.rm_rf(domain_path
)
37 def delete_account(account
)
38 account_path
= self.get_account_path(account
)
39 FileUtils
.rm_rf(account_path
)
42 def get_leftover_domains(db_domains
)
43 # Get the list of domains according to the filesystem.
44 fs_domains
= self.get_domains_from_filesystem()
46 # Return the list of domains on the filesystem that aren't in the DB.
47 return (fs_domains
- db_domains
)
50 def get_leftover_accounts(db_accounts
)
51 # Get the list of accounts according to the filesystem.
52 fs_domains
= self.get_domains_from_filesystem()
53 fs_accounts
= self.get_accounts_from_filesystem(fs_domains
)
55 # And return the accounts on the filesystem that aren't in the DB.
56 return (fs_accounts
- db_accounts
)
61 def get_domains_from_filesystem()
62 return Filesystem
.get_subdirs(@domain_root)
65 def get_accounts_from_filesystem(domains
)
68 domains
.each
do |domain
|
70 # Throws a NonexistentDomainError if the domain's path
71 # doesn't exist on the filesystem. In this case, we want
72 # to report zero accounts.
73 domain_path
= get_domain_path(domain
)
74 usernames
= Filesystem
.get_subdirs(domain_path
)
76 usernames
.each
do |username
|
77 accounts
<< "#{username}@#{domain}"
79 rescue NonexistentDomainError
=> e
88 def get_domain_path(domain
)
89 # Return the filesystem path for the given domain.
90 # That is, the directory where its mail is stored.
91 # Only works if the domain directory exists!
92 domain_path
= File
.join(@domain_root, domain
)
94 if File
.directory
?(domain_path
)
97 raise NonexistentDomainError
.new(domain_path
)
102 def get_account_path(account
)
103 # Return the filesystem path of this account's mailbox.
104 # Only works if the account exists!
105 if not account
.include?('@')
106 raise InvalidAccountError
.new("#{account}: Accounts must contain an '@' symbol.")
109 account_parts
= account
.split('@')
110 user_part
= account_parts
[0]
111 domain_part
= account_parts
[1]
113 domain_path
= get_domain_path(domain_part
)
114 account_path
= File
.join(domain_path
, user_part
)
116 if File
.directory
?(account_path
)
119 raise NonexistentAccountError
.new(account_path
)