]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/mailshears/plugins/dovecot_mailstore.rb
Switch from constants to variables in the Configuration module. This should keep...
[mailshears.git] / lib / mailshears / plugins / dovecot_mailstore.rb
1 # Needed for rm_rf.
2 require 'fileutils'
3
4 require 'mailshears/errors'
5 require 'mailshears/filesystem'
6 require 'mailshears/mailstore'
7 require 'mailshears/plugin'
8
9 class DovecotMailstore < Mailstore
10
11 include Plugin
12
13 def initialize
14 @domain_root = Configuration::mail_root
15 end
16
17 def describe_domain(domain)
18 begin
19 domain_path = get_domain_path(domain)
20 return domain_path
21 rescue NonexistentDomainError => e
22 return "Doesn't Exist"
23 end
24 end
25
26 def describe_account(account)
27 begin
28 account_path = get_account_path(account)
29 return account_path
30 rescue NonexistentAccountError => e
31 return "Doesn't Exist"
32 end
33 end
34
35 def delete_domain(domain)
36 domain_path = self.get_domain_path(domain)
37 FileUtils.rm_rf(domain_path)
38 end
39
40 def delete_account(account)
41 account_path = self.get_account_path(account)
42 FileUtils.rm_rf(account_path)
43 end
44
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()
48
49 # Return the list of domains on the filesystem that aren't in the DB.
50 return (fs_domains - db_domains)
51 end
52
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)
57
58 # And return the accounts on the filesystem that aren't in the DB.
59 return (fs_accounts - db_accounts)
60 end
61
62 protected;
63
64 def get_domains_from_filesystem()
65 return Filesystem.get_subdirs(@domain_root)
66 end
67
68 def get_accounts_from_filesystem(domains)
69 accounts = []
70
71 domains.each do |domain|
72 begin
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)
78
79 usernames.each do |username|
80 accounts << "#{username}@#{domain}"
81 end
82 rescue NonexistentDomainError => e
83 # Party hard.
84 end
85 end
86
87 return accounts
88 end
89
90
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)
96
97 if File.directory?(domain_path)
98 return domain_path
99 else
100 raise NonexistentDomainError
101 end
102 end
103
104
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.")
110 end
111
112 account_parts = account.split('@')
113 user_part = account_parts[0]
114 domain_part = account_parts[1]
115
116 begin
117 domain_path = get_domain_path(domain_part)
118 rescue NonexistentDomainError
119 raise NonexistentAccountError
120 end
121
122 account_path = File.join(domain_path, user_part)
123
124 if File.directory?(account_path)
125 return account_path
126 else
127 raise NonexistentAccountError
128 end
129 end
130
131 end