]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mailshears/plugins/dovecot_mailstore.rb
4 require 'mailshears/configuration'
5 require 'mailshears/errors'
6 require 'mailshears/filesystem'
7 require 'mailshears/mailstore'
8 require 'mailshears/plugin'
10 class DovecotMailstore
< Mailstore
15 cfg
= Configuration
.new()
16 @domain_root = cfg
.mail_root
19 def describe_domain(domain
)
21 domain_path
= get_domain_path(domain
)
23 rescue NonexistentDomainError
=> e
24 return "Doesn't exist: #{e.to_s}"
28 def describe_account(account
)
30 account_path
= get_account_path(account
)
32 rescue NonexistentAccountError
=> e
33 return "Doesn't exist: #{e.to_s}"
37 def delete_domain(domain
)
38 domain_path
= self.get_domain_path(domain
)
39 FileUtils
.rm_rf(domain_path
)
42 def delete_account(account
)
43 account_path
= self.get_account_path(account
)
44 FileUtils
.rm_rf(account_path
)
47 def get_leftover_domains(db_domains
)
48 # Get the list of domains according to the filesystem.
49 fs_domains
= self.get_domains_from_filesystem()
51 # Return the list of domains on the filesystem that aren't in the DB.
52 return (fs_domains
- db_domains
)
55 def get_leftover_accounts(db_accounts
)
56 # Get the list of accounts according to the filesystem.
57 fs_domains
= self.get_domains_from_filesystem()
58 fs_accounts
= self.get_accounts_from_filesystem(fs_domains
)
60 # And return the accounts on the filesystem that aren't in the DB.
61 return (fs_accounts
- db_accounts
)
66 def get_domains_from_filesystem()
67 return Filesystem
.get_subdirs(@domain_root)
70 def get_accounts_from_filesystem(domains
)
73 domains
.each
do |domain
|
75 # Throws a NonexistentDomainError if the domain's path
76 # doesn't exist on the filesystem. In this case, we want
77 # to report zero accounts.
78 domain_path
= get_domain_path(domain
)
79 usernames
= Filesystem
.get_subdirs(domain_path
)
81 usernames
.each
do |username
|
82 accounts
<< "#{username}@#{domain}"
84 rescue NonexistentDomainError
93 def get_domain_path(domain
)
94 # Return the filesystem path for the given domain.
95 # That is, the directory where its mail is stored.
96 # Only works if the domain directory exists!
97 domain_path
= File
.join(@domain_root, domain
)
99 if File
.directory
?(domain_path
)
102 raise NonexistentDomainError
.new(domain
)
107 def get_account_path(account
)
108 # Return the filesystem path of this account's mailbox.
109 # Only works if the account exists!
110 if not account
.include?('@')
111 raise InvalidAccountError
.new("#{account}: Accounts must contain an '@' symbol.")
114 account_parts
= account
.split('@')
115 user_part
= account_parts
[0]
116 domain_part
= account_parts
[1]
119 domain_path
= get_domain_path(domain_part
)
120 rescue NonexistentDomainError
121 raise NonexistentAccountError
.new(account
)
124 account_path
= File
.join(domain_path
, user_part
)
126 if File
.directory
?(account_path
)
129 raise NonexistentAccountError(account
)