]> git.cworth.org Git - sup/blob - lib/sup/account.rb
bugfix: wrap thread loading in a mutex to avoid all sorts of crazy problems
[sup] / lib / sup / account.rb
1 module Redwood
2
3 class Account < Person
4   attr_accessor :sendmail, :signature
5
6   def initialize email, h
7     super h[:name], email, 0, true
8     @sendmail = h[:sendmail]
9     @signature = h[:signature]
10   end
11 end
12
13 class AccountManager
14   include Singleton
15
16   attr_accessor :default_account
17
18   def initialize accounts
19     @email_map = {}
20     @accounts = {}
21     @default_account = nil
22
23     add_account accounts[:default], true
24     accounts.each { |k, v| add_account v unless k == :default }
25
26     self.class.i_am_the_instance self
27   end
28
29   def user_accounts; @accounts.keys; end
30   def user_emails; @email_map.keys.select { |e| String === e }; end
31
32   ## must be called first with the default account. fills in missing
33   ## values from the default account.
34   def add_account hash, default=false
35     raise ArgumentError, "no email specified for account" unless hash[:email]
36     unless default
37       [:name, :sendmail, :signature].each { |k| hash[k] ||= @default_account.send(k) }
38     end
39     hash[:alternates] ||= []
40
41     main_email = hash[:email]
42     ([hash[:email]] + hash[:alternates]).each do |email|
43       next if @email_map.member? email
44       a = Account.new main_email, hash
45       PersonManager.register a
46       @accounts[a] = true
47       @email_map[email] = a
48     end
49
50     if default
51       raise ArgumentError, "multiple default accounts" if @default_account
52       @default_account = @email_map[main_email]
53     end
54   end
55
56   def is_account? p; is_account_email? p.email end
57   def account_for email; @email_map[email] end
58   def is_account_email? email; !account_for(email).nil? end
59 end
60
61 end