]> git.cworth.org Git - sup/commitdiff
- created a PersonManager that keeps track of the names of all email addresses
authorwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Thu, 28 Dec 2006 23:10:58 +0000 (23:10 +0000)
committerwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Thu, 28 Dec 2006 23:10:58 +0000 (23:10 +0000)
  ever seen. this will probably need to be intelligently trimmed, but will be
  used for fast loading of messages from slow sources.

- moved initialization and shutdown to Redwood::start and ::finish in sup.rb.

- poll#initialize no longer automatically starts a thread. you must call
  #start_thread. (because sup-import now calls Redwood::start which initializes
  all managers, including pollmanager, and it certainly doesn't make sense to
  have anything threaded during import)

- misc. comment improvements

git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@113 5c8cc53c-5e98-4d25-b20a-d8db53a31250

bin/sup
bin/sup-import
lib/sup.rb
lib/sup/label.rb
lib/sup/person.rb
lib/sup/poll.rb
lib/sup/util.rb

diff --git a/bin/sup b/bin/sup
index 708e76eb7a1dabfe32202b67a0bd9112d7c275b6..1c602ac089a9b3e2743b6fd50284d9ddf714d9a1 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -52,13 +52,7 @@ def stop_cursing
 end
 module_function :start_cursing, :stop_cursing
 
-Redwood::SentManager.new Redwood::SENT_FN
-Redwood::ContactManager.new Redwood::CONTACT_FN
-Redwood::LabelManager.new Redwood::LABEL_FN
-Redwood::AccountManager.new $config[:accounts]
-Redwood::DraftManager.new Redwood::DRAFT_DIR
-Redwood::UpdateManager.new
-Redwood::PollManager.new
+Redwood::start
 
 Index.new.load
 log "loaded #{Index.size} messages from index"
@@ -74,7 +68,7 @@ if(s = Index.source_for SentManager.source_name)
 else
   Index.add_source SentManager.new_source
 end
-  
+
 begin
   log "starting curses"
   start_cursing
@@ -123,6 +117,7 @@ begin
   imode.load_more_threads ibuf.content_height
 
   reporting_thread { sleep 3; PollManager.poll }
+  PollManager.start_thread
 
   until $exception
     bm.draw_screen
@@ -185,11 +180,10 @@ begin
     end
   end
   bm.kill_all_buffers
-  Redwood::LabelManager.save
-  Redwood::ContactManager.save
 rescue Exception => e
   $exception ||= e
 ensure
+  Redwood::finish
   stop_cursing
 end
 
index 628367af92a98e96d8a2a173403d98731d83386b..3027e19a5d85482bf12aeb795e8e4fd567e2c4b8 100644 (file)
@@ -81,6 +81,8 @@ if(o = ARGV.find { |x| x =~ /^--/ })
   educate_user
 end
 
+Redwood::start
+
 puts "loading index..."
 index = Redwood::Index.new
 index.load
@@ -171,6 +173,7 @@ begin
   end
 ensure
   index.save
+  Redwood::finish
 end
 
 if rebuild || force_rebuild
index 2162c555ae40281ee00da9648bb9481a033c9170..c9a1cb4a485d56b302bcfe4fd6ac05be99213c4b 100644 (file)
@@ -19,6 +19,7 @@ module Redwood
   CONFIG_FN  = File.join(BASE_DIR, "config.yaml")
   SOURCE_FN  = File.join(BASE_DIR, "sources.yaml")
   LABEL_FN   = File.join(BASE_DIR, "labels.txt")
+  PERSON_FN   = File.join(BASE_DIR, "people.txt")
   CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
   DRAFT_DIR  = File.join(BASE_DIR, "drafts")
   SENT_FN    = File.join(BASE_DIR, "sent.mbox")
@@ -59,7 +60,24 @@ module Redwood
     end
   end
 
-  module_function :register_yaml, :save_yaml_obj, :load_yaml_obj
+  def start
+    Redwood::PersonManager.new Redwood::PERSON_FN
+    Redwood::SentManager.new Redwood::SENT_FN
+    Redwood::ContactManager.new Redwood::CONTACT_FN
+    Redwood::LabelManager.new Redwood::LABEL_FN
+    Redwood::AccountManager.new $config[:accounts]
+    Redwood::DraftManager.new Redwood::DRAFT_DIR
+    Redwood::UpdateManager.new
+    Redwood::PollManager.new
+  end
+
+  def finish
+    Redwood::LabelManager.save
+    Redwood::ContactManager.save
+    Redwood::PersonManager.save
+  end
+
+  module_function :register_yaml, :save_yaml_obj, :load_yaml_obj, :start, :finish
 end
 
 ## set up default configuration file
index a82313f26c768d0425ff4d5784257da85fbbef48..95c71fb764254187523986aaa1fea2147ac8c6bd 100644 (file)
@@ -3,7 +3,7 @@ module Redwood
 class LabelManager
   include Singleton
 
-  ## all labels that have special meaning. user will be unable to
+  ## labels that have special semantics. user will be unable to
   ## add/remove these via normal label mechanisms.
   RESERVED_LABELS = [ :starred, :spam, :draft, :unread, :killed, :sent ]
 
index 38fba31f7efa6e1efb9b96f1483bb46cbad5e21a..206885b5c8c1d97a164ae084a775f9c5b3b92b3e 100644 (file)
@@ -1,5 +1,30 @@
 module Redwood
 
+class PersonManager
+  include Singleton
+
+  def initialize fn
+    @fn = fn
+    @names = {}
+    IO.readlines(fn).map { |l| l =~ /^(.*)?:\s+(.*)$/ && @names[$1] = $2 } if File.exists? fn
+    self.class.i_am_the_instance self
+  end
+
+  def name_for email; @names[email]; end
+  def register email, name
+    return unless name
+
+    name = name.gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ")
+
+    ## all else being equal, prefer longer names, unless the prior name
+    ## doesn't contain any capitalization
+    oldname = @names[email]
+    @names[email] = name if oldname.nil? || oldname.length < name.length || (oldname !~ /[A-Z]/ && name =~ /[A-Z]/)
+  end
+
+  def save; File.open(@fn, "w") { |f| @names.each { |email, name| f.puts "#{email}: #{name}" } }; end
+end
+
 class Person
   @@email_map = {}
 
@@ -7,22 +32,14 @@ class Person
 
   def initialize name, email
     raise ArgumentError, "email can't be nil" unless email
-    @name = 
-      if name
-        name.gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ")
-      else
-        nil
-      end
     @email = email.gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ").downcase
-    @@email_map[@email] = self
+    PersonManager.register @email, name
+    @name = PersonManager.name_for @email
   end
 
   def == o; o && o.email == email; end
   alias :eql? :==
-
-  def hash
-    [name, email].hash
-  end
+  def hash; [name, email].hash; end
 
   def shortname
     case @name
@@ -31,9 +48,9 @@ class Person
     when /(\S+) \S+/
       $1
     when nil
-      @email #[0 ... 10]
+      @email
     else
-      @name #[0 ... 10]
+      @name
     end
   end
 
@@ -45,18 +62,12 @@ class Person
     end
   end
 
-  def mediumname
-    if @name
-      name
-    else
-      @email
-    end
-  end
+  def mediumname; @name || @email; end
 
   def full_address
     if @name && @email
       if @name =~ /"/
-        "#{@name.inspect} <#@email>"
+        "#{@name.inspect} <#@email>" # escape quotes
       else
         "#@name <#@email>"
       end
@@ -65,6 +76,7 @@ class Person
     end
   end
 
+  ## when sorting addresses, sort by this 
   def sort_by_me
     case @name
     when /^(\S+), \S+/
@@ -80,18 +92,10 @@ class Person
     end.downcase
   end
 
-  def self.for_several s
-    return [] if s.nil?
-
-    begin
-      s.split_on_commas.map { |ss| self.for ss }
-    rescue StandardError => e
-      raise "#{e.message}: for #{s.inspect}"
-    end
-  end
-
   def self.for s
     return nil if s.nil?
+
+    ## try and parse an email address and name
     name, email =
       case s
       when /["'](.*?)["'] <(.*?)>/, /([^,]+) <(.*?)>/
@@ -105,14 +109,16 @@ class Person
         [nil, s]
       end
 
-    if name && (p = @@email_map[email])
-      ## all else being equal, prefer longer names, unless the prior name
-      ## doesn't contain any capitalization
-      p.name = name if (p.name.nil? || p.name.length < name.length) unless
-        p.name =~ /[A-Z]/ || (AccountManager.instantiated? && AccountManager.is_account?(p))
-      p 
-    else
-      Person.new name, email
+    @@email_map[email] ||= Person.new name, email
+  end
+
+  def self.for_several s
+    return [] if s.nil?
+
+    begin
+      s.split_on_commas.map { |ss| self.for ss }
+    rescue StandardError => e
+      raise "#{e.message}: for #{s.inspect}"
     end
   end
 end
index cc401ff236cfaa61960e0c9790c7db1bf403e498..19e8d9ff40e33a10714ea6fa070dfb9e87533a41 100644 (file)
@@ -12,13 +12,6 @@ class PollManager
     @last_poll = nil
     
     self.class.i_am_the_instance self
-
-    Redwood::reporting_thread do
-      while true
-        sleep DELAY / 2
-        poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
-      end
-    end
   end
 
   def buffer
@@ -38,6 +31,15 @@ class PollManager
     [num, numi]
   end
 
+  def start_thread
+    Redwood::reporting_thread do
+      while true
+        sleep DELAY / 2
+        poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
+      end
+    end
+  end
+
   def do_poll
     return [0, 0] if @polling
     @polling = true
index f1cab57424fa8d3b30997ff6482a3438307c227f..46a10230982e6cb3348b8e92f6cca8c01dbd5a40 100644 (file)
@@ -43,7 +43,8 @@ class String
     self[0 .. 0].upcase + self[1 .. -1]
   end
 
-  ## found on teh internets
+  ## a very complicated regex found on teh internets to split on
+  ## commas, unless they occurr within double quotes.
   def split_on_commas
     split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/)
   end