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