sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit 19b9b8885ddec06fa8606268c8010fea3fcabb6d
parent dd129e12603f5c77846c5fa624b8f118d53fbd0e
Author: Tero Tilus <tero@tilus.net>
Date:   Tue, 25 Jan 2011 01:12:30 +0200

Always try to canonize person using ContactManager

Signed-off-by: Tero Tilus 
Signed-off-by: Hamish Downer 

Diffstat:
M lib/sup/contact.rb | 13 +++++++++----
M lib/sup/index.rb | 6 ++++--
M lib/sup/modes/thread-index-mode.rb | 4 ++--
M lib/sup/person.rb | 8 +++++++-
4 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/lib/sup/contact.rb b/lib/sup/contact.rb
@@ -12,14 +12,13 @@ class ContactManager
 
     @p2a = {} # person to alias
     @a2p = {} # alias to person
+    @e2p = {} # email to person
 
     if File.exists? fn
       IO.foreach(fn) do |l|
         l =~ /^([^:]*): (.*)$/ or raise "can't parse #{fn} line #{l.inspect}"
         aalias, addr = $1, $2
-        p = Person.from_address addr
-        @p2a[p] = aalias
-        @a2p[aalias] = p unless aalias.nil? || aalias.empty?
+        update_alias Person.from_address(addr), aalias
       end
     end
   end
@@ -30,9 +29,13 @@ class ContactManager
   def update_alias person, aalias=nil
     if(old_aalias = @p2a[person]) # remove old alias
       @a2p.delete old_aalias
+      @e2p.delete old_aalias.email
     end
     @p2a[person] = aalias
-    @a2p[aalias] = person unless aalias.nil? || aalias.empty?
+    unless aalias.nil? || aalias.empty?
+      @a2p[aalias] = person
+      @e2p[person.email] = person
+    end
   end
 
   ## this may not actually be called anywhere, since we still keep contacts
@@ -40,11 +43,13 @@ class ContactManager
   def drop_contact person
     aalias = @p2a[person]
     @p2a.delete person
+    @e2p.delete person.email
     @a2p.delete aalias if aalias
   end
 
   def contact_for aalias; @a2p[aalias] end
   def alias_for person; @p2a[person] end
+  def person_for email; @e2p[email] end
   def is_aliased_contact? person; !@p2a[person].nil? end
 
   def save
diff --git a/lib/sup/index.rb b/lib/sup/index.rb
@@ -210,7 +210,9 @@ EOS
                     :labels => entry[:labels],
                     :snippet => entry[:snippet]
 
-    mk_person = lambda { |x| Person.new(*x.reverse!) }
+    # Try to find person from contacts before falling back to
+    # generating it from the address.
+    mk_person = lambda { |x| Person.from_name_and_email(*x.reverse!) }
     entry[:from] = mk_person[entry[:from]]
     entry[:to].map!(&mk_person)
     entry[:cc].map!(&mk_person)
@@ -235,7 +237,7 @@ EOS
       m = b.call
       ([m.from]+m.to+m.cc+m.bcc).compact.each { |p| contacts << [p.name, p.email] }
     end
-    contacts.to_a.compact.map { |n,e| Person.new n, e }[0...num]
+    contacts.to_a.compact[0...num].map { |n,e| Person.from_name_and_email n, e }
   end
 
   ## Yield each message-id matching query
diff --git a/lib/sup/modes/thread-index-mode.rb b/lib/sup/modes/thread-index-mode.rb
@@ -795,8 +795,8 @@ protected
     authors = t.map do |m, *o|
       next unless m && m.from
       new[m.from] ||= m.has_label?(:unread)
-      next if seen[m.from.mediumname]
-      seen[m.from.mediumname] = true
+      next if seen[m.from]
+      seen[m.from] = true
       m.from
     end.compact
 
diff --git a/lib/sup/person.rb b/lib/sup/person.rb
@@ -76,6 +76,12 @@ class Person
     end.downcase
   end
 
+  ## return "canonical" person using contact manager or create one if
+  ## not found or contact manager not available
+  def self.from_name_and_email name, email
+    ContactManager.instantiated? && ContactManager.person_for(email) || Person.new(name, email)
+  end
+
   def self.from_address s
     return nil if s.nil?
 
@@ -105,7 +111,7 @@ class Person
         [nil, s]
       end
 
-    Person.new name, email
+    from_name_and_email name, email
   end
 
   def self.from_address_list ss