]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mailshears/plugins/dovecot_mailstore.rb
4 require 'mailshears/errors'
5 require 'mailshears/filesystem'
6 require 'mailshears/mailstore'
7 require 'mailshears/plugin'
9 class DovecotMailstore
< Mailstore
14 @domain_root = Configuration
::MAIL_ROOT
17 def describe_domain(domain
)
19 domain_path
= get_domain_path(domain
)
21 rescue NonexistentDomainError
=> e
22 return "Doesn't Exist"
26 def describe_account(account
)
28 account_path
= get_account_path(account
)
30 rescue NonexistentAccountError
=> e
31 return "Doesn't Exist"
35 def delete_domain(domain
)
36 domain_path
= self.get_domain_path(domain
)
37 FileUtils
.rm_rf(domain_path
)
40 def delete_account(account
)
41 account_path
= self.get_account_path(account
)
42 FileUtils
.rm_rf(account_path
)
45 def get_leftover_domains(db_domains
)
46 # Get the list of domains according to the filesystem.
47 fs_domains
= self.get_domains_from_filesystem()
49 # Return the list of domains on the filesystem that aren't in the DB.
50 return (fs_domains
- db_domains
)
53 def get_leftover_accounts(db_accounts
)
54 # Get the list of accounts according to the filesystem.
55 fs_domains
= self.get_domains_from_filesystem()
56 fs_accounts
= self.get_accounts_from_filesystem(fs_domains
)
58 # And return the accounts on the filesystem that aren't in the DB.
59 return (fs_accounts
- db_accounts
)
64 def get_domains_from_filesystem()
65 return Filesystem
.get_subdirs(@domain_root)
68 def get_accounts_from_filesystem(domains
)
71 domains
.each
do |domain
|
73 # Throws a NonexistentDomainError if the domain's path
74 # doesn't exist on the filesystem. In this case, we want
75 # to report zero accounts.
76 domain_path
= get_domain_path(domain
)
77 usernames
= Filesystem
.get_subdirs(domain_path
)
79 usernames
.each
do |username
|
80 accounts
<< "#{username}@#{domain}"
82 rescue NonexistentDomainError
=> e
91 def get_domain_path(domain
)
92 # Return the filesystem path for the given domain.
93 # That is, the directory where its mail is stored.
94 # Only works if the domain directory exists!
95 domain_path
= File
.join(@domain_root, domain
)
97 if File
.directory
?(domain_path
)
100 raise NonexistentDomainError
105 def get_account_path(account
)
106 # Return the filesystem path of this account's mailbox.
107 # Only works if the account exists!
108 if not account
.include?('@')
109 raise InvalidAccountError
.new("#{account}: Accounts must contain an '@' symbol.")
112 account_parts
= account
.split('@')
113 user_part
= account_parts
[0]
114 domain_part
= account_parts
[1]
117 domain_path
= get_domain_path(domain_part
)
118 rescue NonexistentDomainError
119 raise NonexistentAccountError
122 account_path
= File
.join(domain_path
, user_part
)
124 if File
.directory
?(account_path
)
127 raise NonexistentAccountError