f0824528b438c3ab7cf9f823ce7dc13209ea3741
[mailshears.git] / lib / mv / plugins / dovecot_mailstore.rb
1 # Needed for rm_rf.
2 require 'fileutils'
3
4 require 'common/filesystem'
5 require 'common/mailstore'
6 require 'common/plugin'
7 require 'mv/mv_plugin'
8
9 class DovecotMailstoreMv < Mailstore
10
11 include Plugin
12 include MvPlugin
13
14 def initialize
15 cfg = Configuration.new()
16 @domain_root = cfg.mail_root
17 end
18
19 def describe_domain(domain)
20 begin
21 domain_path = get_domain_path(domain)
22 return domain_path
23 rescue NonexistentDomainError => e
24 return "Doesn't exist: #{e.to_s}"
25 end
26 end
27
28 def describe_account(account)
29 begin
30 account_path = get_account_path(account)
31 return account_path
32 rescue NonexistentAccountError => e
33 return "Doesn't exist: #{e.to_s}"
34 end
35 end
36
37 def mv_domain(from, to)
38 from_path = self.get_domain_path(from)
39 to_path = self.get_domain_path(to)
40 FileUtils.mv(from_path, to_path)
41 end
42
43 def mv_account(from, to)
44 from_path = self.get_account_path(from)
45 to_path = self.get_account_path(to)
46 FileUtils.mv(from_path, to_path)
47 end
48
49 protected;
50
51
52 def get_domain_path(domain)
53 # Return the filesystem path for the given domain.
54 # That is, the directory where its mail is stored.
55 # Only works if the domain directory exists!
56 domain_path = File.join(@domain_root, domain)
57
58 if File.directory?(domain_path)
59 return domain_path
60 else
61 raise NonexistentDomainError.new(domain)
62 end
63 end
64
65
66 def get_account_path(account)
67 # Return the filesystem path of this account's mailbox.
68 # Only works if the account exists!
69 if not account.include?('@')
70 raise InvalidAccountError.new("#{account}: Accounts must contain an '@' symbol.")
71 end
72
73 account_parts = account.split('@')
74 user_part = account_parts[0]
75 domain_part = account_parts[1]
76
77 begin
78 domain_path = get_domain_path(domain_part)
79 rescue NonexistentDomainError
80 raise NonexistentAccountError.new(account)
81 end
82
83 account_path = File.join(domain_path, user_part)
84
85 if File.directory?(account_path)
86 return account_path
87 else
88 raise NonexistentAccountError(account)
89 end
90 end
91
92 end