sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit d496466daeef90657118c9c5a729d18296efa4b2
parent 704e6c7d3bae2c7ae15a5f86708ae05cdc1fb379
Author: William Morgan <wmorgan-sup@masanjin.net>
Date:   Sun, 20 Apr 2008 15:07:52 -0700

Merge branches 'completions-hook', 'find-horizontally', 'startup-hook' and 'message-id-normalization'

Diffstat:
M bin/sup | 10 ++++++++++
M bin/sup-sync | 2 +-
M lib/sup/buffer.rb | 10 ++++++++++
M lib/sup/index.rb | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
M lib/sup/mbox.rb | 34 +++++++++++++++++-----------------
M lib/sup/message.rb | 2 +-
M lib/sup/modes/inbox-mode.rb | 2 +-
M lib/sup/modes/scroll-mode.rb | 27 +++++++++++++++++++++------
M lib/sup/modes/thread-index-mode.rb | 5 +++--
M lib/sup/poll.rb | 4 ++--
M lib/sup/thread.rb | 10 ++--------
11 files changed, 122 insertions(+), 67 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -39,6 +39,12 @@ EOS
   opt :compose, "Compose message to this recipient upon startup", :type => String
 end
 
+Redwood::HookManager.register "startup", <<EOS
+Executes at startup
+No variables.
+No return value.
+EOS
+
 if $opts[:list_hooks]
   Redwood::HookManager.print_hooks
   exit
@@ -128,6 +134,8 @@ begin
     Index.add_source SentManager.new_source
   end
 
+  HookManager.run "startup"
+
   log "starting curses"
   start_cursing
 
@@ -138,6 +146,8 @@ begin
            Ncurses::A_BOLD
     c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
            Ncurses::A_BOLD
+    c.add :index_draft_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
+           Ncurses::A_BOLD
     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
            Ncurses::A_BOLD
diff --git a/bin/sup-sync b/bin/sup-sync
@@ -133,7 +133,7 @@ begin
     num_added = num_updated = num_scanned = num_restored = 0
     last_info_time = start_time = Time.now
 
-    Redwood::PollManager.add_messages_from source do |m, offset, entry|
+    Redwood::PollManager.add_messages_from source, :force_overwrite => true do |m, offset, entry|
       num_scanned += 1
       seen[m.id] = true
 
diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb
@@ -167,6 +167,15 @@ Variables: the same as status-bar-text hook.
 Return value: a string to be used as the terminal title.
 EOS
 
+  HookManager.register "extra-contact-addresses", <<EOS
+A list of extra addresses to propose for tab completion, etc. when the
+user is entering an email address. Can be plain email addresses or can
+be full "User Name <email@domain.tld>" entries.
+
+Variables: none
+Return value: an array of email address strings.
+EOS
+
   def initialize
     @name_map = {}
     @buffers = []
@@ -493,6 +502,7 @@ EOS
     contacts = ContactManager.contacts.map { |c| [ContactManager.alias_for(c), c.full_address, c.email] }
 
     completions = (recent + contacts).flatten.uniq.sort
+    completions += HookManager.run("extra-contact-addresses") || []
     answer = BufferManager.ask_many_emails_with_completions domain, question, completions, default
 
     if answer
diff --git a/lib/sup/index.rb b/lib/sup/index.rb
@@ -141,11 +141,11 @@ EOS
     else
       Redwood::log "creating index..."
       field_infos = Ferret::Index::FieldInfos.new :store => :yes
-      field_infos.add_field :message_id
+      field_infos.add_field :message_id, :index => :untokenized
       field_infos.add_field :source_id
       field_infos.add_field :source_info
       field_infos.add_field :date, :index => :untokenized
-      field_infos.add_field :body, :store => :no
+      field_infos.add_field :body
       field_infos.add_field :label
       field_infos.add_field :subject
       field_infos.add_field :from
@@ -161,7 +161,7 @@ EOS
   ## and adding either way. Index state will be determined by m.labels.
   ##
   ## docid and entry can be specified if they're already known.
-  def sync_message m, docid=nil, entry=nil
+  def sync_message m, docid=nil, entry=nil, opts={}
     docid, entry = load_entry_for_id m.id unless docid && entry
 
     raise "no source info for message #{m.id}" unless m.source && m.source_info
@@ -174,7 +174,6 @@ EOS
         m.source.id or raise "unregistered source #{m.source} (id #{m.source.id.inspect})"
       end
 
-    to = (m.to + m.cc + m.bcc).map { |x| x.email }.join(" ")
     snippet = 
       if m.snippet_contains_encrypted_content? && $config[:discard_snippets_from_encrypted_messages]
         ""
@@ -182,18 +181,52 @@ EOS
         m.snippet
       end
 
+    ## write the new document to the index. if the entry already exists in the
+    ## index, reuse it (which avoids having to reload the entry from the source,
+    ## which can be quite expensive for e.g. large threads of IMAP actions.)
+    ##
+    ## exception: if the index entry belongs to an earlier version of the
+    ## message, use everything from the new message instead, but union the
+    ## flags. this allows messages sent to mailing lists to have their header
+    ## updated and to have flags set properly.
+    ##
+    ## minor hack: messages in sources with lower ids have priority over
+    ## messages in sources with higher ids. so messages in the inbox will
+    ## override everyone, and messages in the sent box will be overridden
+    ## by everyone else.
+    ##
+    ## written in this manner to support previous versions of the index which
+    ## did not keep around the entry body. upgrading is thus seamless.
+    entry ||= {}
+    labels = m.labels.uniq # override because this is the new state, unless...
+
+    ## if we are a later version of a message, ignore what's in the index,
+    ## but merge in the labels.
+    if entry[:source_id] && entry[:source_info] && entry[:label] &&
+      ((entry[:source_id].to_i > source_id) || (entry[:source_info].to_i < m.source_info))
+      labels = (entry[:label].split(/\s+/).map { |l| l.intern } + m.labels).uniq
+      #Redwood::log "found updated version of message #{m.id}: #{m.subj}"
+      #Redwood::log "previous version was at #{entry[:source_id].inspect}:#{entry[:source_info].inspect}, this version at #{source_id.inspect}:#{m.source_info.inspect}"
+      #Redwood::log "merged labels are #{labels.inspect} (index #{entry[:label].inspect}, message #{m.labels.inspect})"
+      entry = {}
+    end
+
+    ## if force_overwite is true, ignore what's in the index. this is used
+    ## primarily by sup-sync to force index updates.
+    entry = {} if opts[:force_overwrite]
+
     d = {
       :message_id => m.id,
       :source_id => source_id,
       :source_info => m.source_info,
-      :date => m.date.to_indexable_s,
-      :body => m.indexable_content,
-      :snippet => snippet,
-      :label => m.labels.uniq.join(" "),
-      :from => m.from ? m.from.indexable_content : "",
-      :to => (m.to + m.cc + m.bcc).map { |x| x.indexable_content }.join(" "),
-      :subject => wrap_subj(m.subj),
-      :refs => (m.refs + m.replytos).uniq.join(" "),
+      :date => (entry[:date] || m.date.to_indexable_s),
+      :body => (entry[:body] || m.indexable_content),
+      :snippet => snippet, # always override
+      :label => labels.uniq.join(" "),
+      :from => (entry[:from] || (m.from ? m.from.indexable_content : "")),
+      :to => (entry[:to] || (m.to + m.cc + m.bcc).map { |x| x.indexable_content }.join(" ")),
+      :subject => (entry[:subject] || wrap_subj(Message.normalize_subj(m.subj))),
+      :refs => (entry[:refs] || (m.refs + m.replytos).uniq.join(" ")),
     }
 
     @index.delete docid if docid
@@ -254,6 +287,7 @@ EOS
     searched = {}
     num_queries = 0
 
+    pending = [m.id]
     if $config[:thread_by_subject] # do subject queries
       date_min = m.date - (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
       date_max = m.date + (SAME_SUBJECT_DATE_LIMIT * 12 * 3600)
@@ -268,10 +302,13 @@ EOS
 
       q = build_query :qobj => q
 
-      pending = @index.search(q).hits.map { |hit| @index[hit.doc][:message_id] }
-      Redwood::log "found #{pending.size} results for subject query #{q}"
-    else
-      pending = [m.id]
+      p1 = @index.search(q).hits.map { |hit| @index[hit.doc][:message_id] }
+      Redwood::log "found #{p1.size} results for subject query #{q}"
+
+      p2 = @index.search(q.to_s, :limit => :all).hits.map { |hit| @index[hit.doc][:message_id] }
+      Redwood::log "found #{p2.size} results in string form"
+
+      pending = (pending + p1 + p2).uniq
     end
 
     until pending.empty? || (opts[:limit] && messages.size >= opts[:limit])
@@ -400,18 +437,7 @@ protected
   def parse_user_query_string s
     extraopts = {}
 
-    ## this is a little hacky, but it works, at least until ferret changes
-    ## its api. we parse the user query string with ferret twice: the first
-    ## time we just turn the resulting object back into a string, which has
-    ## the next effect of transforming the original string into a nice
-    ## normalized form with + and - instead of AND, OR, etc. then we do some
-    ## string substitutions which depend on this normalized form, re-parse
-    ## the string with Ferret, and return the resulting query object.
-
-    norms = @qparser.parse(s).to_s
-    Redwood::log "normalized #{s.inspect} to #{norms.inspect}" unless s == norms
-
-    subs = norms.gsub(/\b(to|from):(\S+)\b/) do
+    subs = s.gsub(/\b(to|from):(\S+)\b/) do
       field, name = $1, $2
       if(p = ContactManager.contact_for(name))
         [field, p.email]
@@ -481,7 +507,6 @@ protected
       subs = nil if chronic_failure
     end
     
-    Redwood::log "translated #{norms.inspect} to #{subs.inspect}" unless subs == norms
     if subs
       [@qparser.parse(subs), extraopts]
     else
diff --git a/lib/sup/mbox.rb b/lib/sup/mbox.rb
@@ -20,26 +20,26 @@ module MBox
     ## when scanning over large mbox files.
     while(line = f.gets)
       case line
-      when /^(From):\s+(.*?)\s*$/i,
-        /^(To):\s+(.*?)\s*$/i,
-        /^(Cc):\s+(.*?)\s*$/i,
-        /^(Bcc):\s+(.*?)\s*$/i,
-        /^(Subject):\s+(.*?)\s*$/i,
-        /^(Date):\s+(.*?)\s*$/i,
-        /^(References):\s+(.*?)\s*$/i,
-        /^(In-Reply-To):\s+(.*?)\s*$/i,
-        /^(Reply-To):\s+(.*?)\s*$/i,
-        /^(List-Post):\s+(.*?)\s*$/i,
-        /^(List-Subscribe):\s+(.*?)\s*$/i,
-        /^(List-Unsubscribe):\s+(.*?)\s*$/i,
-        /^(Status):\s+(.*?)\s*$/i: header[last = $1] = $2
-      when /^(Message-Id):\s+(.*?)\s*$/i: header[mid_field = last = $1] = $2
+      when /^(From):\s*(.*?)\s*$/i,
+        /^(To):\s*(.*?)\s*$/i,
+        /^(Cc):\s*(.*?)\s*$/i,
+        /^(Bcc):\s*(.*?)\s*$/i,
+        /^(Subject):\s*(.*?)\s*$/i,
+        /^(Date):\s*(.*?)\s*$/i,
+        /^(References):\s*(.*?)\s*$/i,
+        /^(In-Reply-To):\s*(.*?)\s*$/i,
+        /^(Reply-To):\s*(.*?)\s*$/i,
+        /^(List-Post):\s*(.*?)\s*$/i,
+        /^(List-Subscribe):\s*(.*?)\s*$/i,
+        /^(List-Unsubscribe):\s*(.*?)\s*$/i,
+        /^(Status):\s*(.*?)\s*$/i: header[last = $1] = $2
+      when /^(Message-Id):\s*(.*?)\s*$/i: header[mid_field = last = $1] = $2
 
       ## these next three can occur multiple times, and we want the
       ## first one
-      when /^(Delivered-To):\s+(.*)$/i,
-        /^(X-Original-To):\s+(.*)$/i,
-        /^(Envelope-To):\s+(.*)$/i: header[last = $1] ||= $2
+      when /^(Delivered-To):\s*(.*)$/i,
+        /^(X-Original-To):\s*(.*)$/i,
+        /^(Envelope-To):\s*(.*)$/i: header[last = $1] ||= $2
 
       when /^\r*$/: break
       when /^\S+:/: last = nil # some other header we don't care about
diff --git a/lib/sup/message.rb b/lib/sup/message.rb
@@ -148,7 +148,7 @@ class Message
     @source.fn_for_offset @source_info
   end
 
-  def sanitize_message_id mid; mid.gsub(/\s/, "") end
+  def sanitize_message_id mid; mid.gsub(/\s+/, "")[0..254] end
 
   def save index
     return unless @dirty
diff --git a/lib/sup/modes/inbox-mode.rb b/lib/sup/modes/inbox-mode.rb
@@ -9,7 +9,7 @@ class InboxMode < ThreadIndexMode
   end
 
   def initialize
-    super [:inbox, :sent], { :label => :inbox, :skip_killed => true }
+    super [:inbox, :sent, :draft], { :label => :inbox, :skip_killed => true }
     raise "can't have more than one!" if defined? @@instance
     @@instance = self
   end
diff --git a/lib/sup/modes/scroll-mode.rb b/lib/sup/modes/scroll-mode.rb
@@ -64,14 +64,14 @@ class ScrollMode < Mode
     end
 
     start = @search_line || search_start_line
-    line = find_text @search_query, start
+    line, col = find_text @search_query, start
     if line.nil? && (start > 0)
-      line = find_text @search_query, 0
+      line, col = find_text @search_query, 0
       BufferManager.flash "Search wrapped to top!" if line
     end
     if line
       @search_line = line + 1
-      search_goto_line line
+      search_goto_pos line, col, col + @search_query.length
       buffer.mark_dirty
     else
       BufferManager.flash "Not found!"
@@ -86,7 +86,13 @@ class ScrollMode < Mode
   end
 
   ## subclasses can override these two!
-  def search_goto_line line; jump_to_line line end
+  def search_goto_pos line, leftcol, rightcol
+    jump_to_line line
+
+    if rightcol > self.rightcol # if it's occluded...
+      jump_to_col [rightcol - buffer.content_width + 1, 0].max # move right
+    end
+  end
   def search_start_line; @topline end
 
   def col_left
@@ -144,9 +150,18 @@ protected
     (start_line ... lines).each do |i|
       case(s = self[i])
       when String
-        return i if s =~ regex
+        match = s =~ regex
+        return [i, match] if match
       when Array
-        return i if s.any? { |color, string| string =~ regex }
+        offset = 0
+        s.each do |color, string|
+          match = string =~ regex
+          if match
+            return [i, offset + match]
+          else
+            offset += string.length
+          end
+        end
       end
     end
     nil
diff --git a/lib/sup/modes/thread-index-mode.rb b/lib/sup/modes/thread-index-mode.rb
@@ -655,7 +655,6 @@ protected
 
     date = t.date.to_nice_s
 
-    new = t.has_label?(:unread)
     starred = t.has_label?(:starred)
 
     ## format the from column
@@ -692,7 +691,9 @@ protected
     p = dp || t.participants.any? { |p| AccountManager.is_account? p }
 
     subj_color =
-      if new
+      if t.has_label?(:draft)
+        :index_draft_color
+      elsif t.has_label?(:unread)
         :index_new_color
       elsif starred
         :index_starred_color
diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb
@@ -134,7 +134,7 @@ EOS
   ## labels. it is likely that callers will want to replace these with
   ## the index labels, if they exist, so that state is not lost when
   ## e.g. a new version of a message from a mailing list comes in.
-  def add_messages_from source
+  def add_messages_from source, opts={}
     begin
       return if source.done? || source.has_errors?
       
@@ -157,7 +157,7 @@ EOS
           docid, entry = Index.load_entry_for_id m.id
           HookManager.run "before-add-message", :message => m
           m = yield(m, offset, entry) or next if block_given?
-          Index.sync_message m, docid, entry
+          Index.sync_message m, docid, entry, opts
           UpdateManager.relay self, :added, m unless entry
         rescue MessageFormatError => e
           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
diff --git a/lib/sup/thread.rb b/lib/sup/thread.rb
@@ -415,14 +415,8 @@ class ThreadSet
     ## that we first added a child message with a different
     ## subject)
     if root.thread
-      unless @threads[key] == root.thread
-        if @threads[key]
-          root.thread.empty!
-          @threads[key] << root
-          root.thread = @threads[key]
-        else
-          @threads[key] = root.thread
-        end
+      if @threads.member?(key) && @threads[key] != root.thread
+        @threads.delete key
       end
     else
       thread = @threads[key]