sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 34287a219b559303fb99cc9b54673171f84702eb
parent 7e7858c8e731daec7ab7ac758bff7a1ee884945c
Author: Paweł Wilk <siefca@gnu.org>
Date:   Mon, 25 Nov 2013 23:35:19 +0100

Merge #189: Added hidden_alternates config option

Addresses listed in hidden_alternates will not be shown in the
account_selector. They will still be treated as aliases to the account
in all other respects.

Pull request: #189

Section in sup-config left out for now, implementation available in #189.

Diffstat:
M History.txt | 2 ++
M ReleaseNotes | 2 +-
M lib/sup.rb | 1 +
M lib/sup/account.rb | 52 ++++++++++++++++++++++++++++++++++++++++++----------
4 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/History.txt b/History.txt
@@ -2,6 +2,8 @@
 
 * Use the form_driver_w routine for inputing multibyte chars when
   available.
+* Add hidden_alternates configuration option: hidden aliases for the
+  account.
 
 == 0.15.1 / 2013-12-04
 
diff --git a/ReleaseNotes b/ReleaseNotes
@@ -1,6 +1,6 @@
 Release 0.15.2:
 
-Use form_driver_w when available.
+Use form_driver_w when available. New hidden_alternates option.
 
 Release 0.15.1:
 
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -357,6 +357,7 @@ EOM
             :name => name.dup.fix_encoding!,
             :email => email.dup.fix_encoding!,
             :alternates => [],
+            :hidden_alternates => [],
             :sendmail => "/usr/sbin/sendmail -oem -ti",
             :signature => File.join(ENV["HOME"], ".signature"),
             :gpgkey => ""
diff --git a/lib/sup/account.rb b/lib/sup/account.rb
@@ -31,6 +31,8 @@ class AccountManager
 
   def initialize accounts
     @email_map = {}
+    @hidden_email_map = {}
+    @email_map_dirty = false
     @accounts = {}
     @regexen = {}
     @default_account = nil
@@ -40,7 +42,7 @@ class AccountManager
   end
 
   def user_accounts; @accounts.keys; end
-  def user_emails; @email_map.keys.select { |e| String === e }; end
+  def user_emails(type = :all); email_map(type).keys.select { |e| String === e }; end
 
   ## must be called first with the default account. fills in missing
   ## values from the default account.
@@ -50,7 +52,9 @@ class AccountManager
       [:name, :sendmail, :signature, :gpgkey].each { |k| hash[k] ||= @default_account.send(k) }
     end
     hash[:alternates] ||= []
+    hash[:hidden_alternates] ||= []
     fail "alternative emails are not an array: #{hash[:alternates]}" unless hash[:alternates].kind_of? Array
+    fail "hidden alternative emails are not an array: #{hash[:hidden_alternates]}" unless hash[:hidden_alternates].kind_of? Array
 
     [:name, :signature].each { |x| hash[x] ? hash[x].fix_encoding! : nil }
 
@@ -63,8 +67,11 @@ class AccountManager
     end
 
     ([hash[:email]] + hash[:alternates]).each do |email|
-      next if @email_map.member? email
-      @email_map[email] = a
+      add_email_to_map(:shown, email, a)
+    end
+
+    hash[:hidden_alternates].each do |email|
+      add_email_to_map(:hidden, email, a)
     end
 
     hash[:regexen].each do |re|
@@ -72,19 +79,44 @@ class AccountManager
     end if hash[:regexen]
   end
 
-  def is_account? p; is_account_email? p.email end
+  def is_account? p;    is_account_email? p.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
+    a = email_map[email]
+    a.nil? ? @regexen.argfind { |re, a| re =~ email && a } : a
   end
+
   def full_address_for email
     a = account_for email
     Person.full_address a.name, email
   end
-end
+
+  private
+
+  def add_email_to_map(type, email, acc)
+    type = :shown if type != :hidden
+    m = email_map(type)
+    unless m.member? email
+      m[email] = acc
+      @email_map_dirty = true
+    end
+  end
+
+  def email_map(type = nil)
+    case type
+    when :shown, :public  then @email_map
+    when :hidden          then @hidden_email_map
+    else
+      if @email_map_dirty
+        @email_map_all = @hidden_email_map.merge(@email_map)
+        if @email_map_all.count != @email_map.count + @hidden_email_map.count
+          @hidden_email_map.reject! { |m| @email_map.member? m }
+        end
+      end
+      @email_map_all ||= {}
+    end
+  end
+end # class AccountManager
 
 end