]> git.cworth.org Git - sup/blobdiff - lib/sup/account.rb
reduce quote parsing worst-case behavior
[sup] / lib / sup / account.rb
index 68e3c2b31a41a2865c7e0e58ff8138369edc5ae5..6f86129ca5d8997b0572f38512ba84a691ee4a7c 100644 (file)
@@ -3,8 +3,10 @@ module Redwood
 class Account < Person
   attr_accessor :sendmail, :signature
 
-  def initialize email, h
-    super h[:name], email, 0, true
+  def initialize h
+    raise ArgumentError, "no name for account" unless h[:name]
+    raise ArgumentError, "no email for account" unless h[:email]
+    super h[:name], h[:email]
     @sendmail = h[:sendmail]
     @signature = h[:signature]
   end
@@ -18,10 +20,11 @@ class AccountManager
   def initialize accounts
     @email_map = {}
     @accounts = {}
+    @regexen = {}
     @default_account = nil
 
     add_account accounts[:default], true
-    accounts.each { |k, v| add_account v unless k == :default }
+    accounts.each { |k, v| add_account v, false unless k == :default }
 
     self.class.i_am_the_instance self
   end
@@ -38,24 +41,33 @@ class AccountManager
     end
     hash[:alternates] ||= []
 
-    main_email = hash[:email]
+    a = Account.new hash
+    @accounts[a] = true
+
+    if default
+      raise ArgumentError, "multiple default accounts" if @default_account
+      @default_account = a
+    end
+
     ([hash[:email]] + hash[:alternates]).each do |email|
       next if @email_map.member? email
-      a = Account.new main_email, hash
-      PersonManager.register a
-      @accounts[a] = true
       @email_map[email] = a
     end
 
-    if default
-      raise ArgumentError, "multiple default accounts" if @default_account
-      @default_account = @email_map[main_email]
-    end
+    hash[:regexen].each do |re|
+      @regexen[Regexp.new(re)] = a
+    end if hash[:regexen]
   end
 
   def is_account? p; is_account_email? p.email end
-  def account_for email; @email_map[email] end
   def is_account_email? email; !account_for(email).nil? end
+  def account_for email
+    if(a = @email_map[email])
+      a
+    else
+      @regexen.argfind { |re, a| re =~ email && a }
+    end
+  end
 end
 
 end