lib/sup/account.rb (2514B) - raw
1 module Redwood
2
3 class Account < Person
4 attr_accessor :sendmail, :signature, :gpgkey
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 @gpgkey = h[:gpgkey]
13 end
14
15 # Default sendmail command for bouncing mail,
16 # deduced from #sendmail
17 def bounce_sendmail
18 sendmail.sub(/\s(\-(ti|it|t))\b/) do |match|
19 case $1
20 when '-t' then ''
21 else ' -i'
22 end
23 end
24 end
25 end
26
27 class AccountManager
28 include Redwood::Singleton
29
30 attr_accessor :default_account
31
32 def initialize accounts
33 @email_map = {}
34 @accounts = {}
35 @regexen = {}
36 @default_account = nil
37
38 fail "default account missing in config" unless accounts[:default].kind_of? Hash
39 add_account accounts[:default], true
40 accounts.each { |k, v| add_account v, false unless k == :default }
41 end
42
43 def user_accounts; @accounts.keys; end
44 def user_emails; @email_map.keys.select { |e| String === e }; end
45
46 ## must be called first with the default account. fills in missing
47 ## values from the default account.
48 def add_account hash, default=false
49 raise ArgumentError, "no email specified for account" unless hash[:email]
50 unless default
51 [:name, :sendmail, :signature, :gpgkey].each { |k| hash[k] ||= @default_account.send(k) }
52 end
53 hash[:alternates] ||= []
54 fail "alternative emails are not an array: #{hash[:alternates]}" unless hash[:alternates].kind_of? Array
55 raise ArgumentError, "no sendmail command specified for account" unless hash[:sendmail]
56
57 [:name, :signature].each { |x| hash[x] ? hash[x].fix_encoding! : nil }
58
59 a = Account.new hash
60 @accounts[a] = true
61
62 if default
63 raise ArgumentError, "multiple default accounts" if @default_account
64 @default_account = a
65 end
66
67 ([hash[:email]] + hash[:alternates]).each do |email|
68 next if @email_map.member? email
69 @email_map[email] = a
70 end
71
72 hash[:regexen].each do |re|
73 @regexen[Regexp.new(re)] = a
74 end if hash[:regexen]
75 end
76
77 def is_account? p; is_account_email? p.email end
78 def is_account_email? email; !account_for(email).nil? end
79 def account_for email
80 if(a = @email_map[email])
81 a
82 else
83 @regexen.argfind { |re, a| re =~ email && a }
84 end
85 end
86 def full_address_for email
87 a = account_for email
88 Person.full_address a.name, email
89 end
90 end
91
92 end