]> git.cworth.org Git - sup/commitdiff
move source-related methods to SourceManager
authorRich Lane <rlane@club.cc.cmu.edu>
Sat, 20 Jun 2009 20:50:06 +0000 (13:50 -0700)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 24 Jun 2009 13:44:48 +0000 (09:44 -0400)
bin/sup
bin/sup-add
bin/sup-config
bin/sup-recover-sources
bin/sup-sync
bin/sup-sync-back
bin/sup-tweak-labels
lib/sup.rb
lib/sup/index.rb
lib/sup/poll.rb
lib/sup/source.rb

diff --git a/bin/sup b/bin/sup
index 302ad7c9764673f17fafb3b9948fe1624c80959a..1febefdd5bc0e06e033f6da7a86d1f5bdf2e710f 100755 (executable)
--- a/bin/sup
+++ b/bin/sup
@@ -160,17 +160,17 @@ begin
   Redwood::start
   Index.load
 
-  if(s = Index.source_for DraftManager.source_name)
+  if(s = Redwood::SourceManager.source_for DraftManager.source_name)
     DraftManager.source = s
   else
     Redwood::log "no draft source, auto-adding..."
-    Index.add_source DraftManager.new_source
+    Redwood::SourceManager.add_source DraftManager.new_source
   end
 
-  if(s = Index.source_for SentManager.source_uri)
+  if(s = Redwood::SourceManager.source_for SentManager.source_uri)
     SentManager.source = s
   else
-    Index.add_source SentManager.default_source
+    Redwood::SourceManager.add_source SentManager.default_source
   end
 
   HookManager.run "startup"
@@ -190,7 +190,7 @@ begin
 
   bm.draw_screen
 
-  Index.usual_sources.each do |s|
+  Redwood::SourceManager.usual_sources.each do |s|
     next unless s.respond_to? :connect
     reporting_thread("call #connect on #{s}") do
       begin
index 50bbb291d4247ef3bea89baeab9ec8f4482c8b25..3ab7c4db0a18bf4bdb07bf2b9b7f9af375172e78 100755 (executable)
@@ -82,12 +82,12 @@ index = Redwood::Index.new
 index.lock_or_die
 
 begin
-  index.load_sources
+  Redwood::SourceManager.load_sources
 
   ARGV.each do |uri|
     labels = $opts[:labels] ? $opts[:labels].split(/\s*,\s*/).uniq : []
 
-    if !$opts[:force_new] && index.source_for(uri) 
+    if !$opts[:force_new] && Redwood::SourceManager.source_for(uri)
       say "Already know about #{uri}; skipping."
       next
     end
@@ -99,10 +99,10 @@ begin
       when "mbox+ssh"
         say "For SSH connections, if you will use public key authentication, you may leave the username and password blank."
         say ""
-        username, password = get_login_info uri, index.sources
+        username, password = get_login_info uri, Redwood::SourceManager.sources
         Redwood::MBox::SSHLoader.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
       when "imap", "imaps"
-        username, password = get_login_info uri, index.sources
+        username, password = get_login_info uri, Redwood::SourceManager.sources
         Redwood::IMAP.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
       when "maildir"
         Redwood::Maildir.new uri, nil, !$opts[:unusual], $opts[:archive], nil, labels
@@ -114,7 +114,7 @@ begin
         Trollop::die "Unknown source type #{parsed_uri.scheme.inspect}"      
       end
     say "Adding #{source}..."
-    index.add_source source
+    Redwood::SourceManager.add_source source
   end
 ensure
   index.save
index 398197f512205207a2045c374752372ce486d9a5..9fcbee60e527250700b6aaaf0e64a4746b22dda2 100755 (executable)
@@ -152,7 +152,7 @@ end
 $terminal.wrap_at = :auto
 Redwood::start
 index = Redwood::Index.new
-index.load_sources
+Redwood::SourceManager.load_sources
 
 say <<EOS
 Howdy neighbor! This here's sup-config, ready to help you jack in to
@@ -191,12 +191,12 @@ $config[:editor] = editor
 done = false
 until done
   say "\nNow, we'll tell Sup where to find all your email."
-  index.load_sources
+  Redwood::SourceManager.load_sources
   say "Current sources:"
-  if index.sources.empty?
+  if Redwood::SourceManager.sources.empty?
     say "  No sources!"
   else
-    index.sources.each { |s| puts "* #{s}" }
+    Redwood::SourceManager.sources.each { |s| puts "* #{s}" }
   end
 
   say "\n"
@@ -210,8 +210,8 @@ end
 say "\nSup needs to know where to store your sent messages."
 say "Only sources capable of storing mail will be listed.\n\n"
 
-index.load_sources
-if index.sources.empty?
+Redwood::SourceManager.load_sources
+if Redwood::SourceManager.sources.empty?
   say "\nUsing the default sup://sent, since you haven't configured other sources yet."
   $config[:sent_source] = 'sup://sent'
 else
@@ -222,7 +222,7 @@ else
   choose do |menu|
     menu.prompt = "Store my sent mail in? "
 
-    valid_sents = index.sources.each do |s|
+    valid_sents = Redwood::SourceManager.sources.each do |s|
       have_sup_sent = true if s.to_s.eql?('sup://sent')
 
       menu.choice(s.to_s) { $config[:sent_source] = s.to_s } if s.respond_to? :store_message
index 6e3810cdc145246a674cf13c0685c7af484b475f..db75b11a392f8043346d7af4769d6a6244ec3b2b 100755 (executable)
@@ -48,13 +48,14 @@ EOS
 end.parse(ARGV)
 
 require "sup"
+Redwood::start
 puts "loading index..."
 index = Redwood::Index.new
 index.load
 puts "loaded index of #{index.size} messages"
 
 ARGV.each do |fn|
-  next if index.source_for fn
+  next if Redwood::SourceManager.source_for fn
 
   ## TODO: merge this code with the same snippet in import
   source = 
@@ -74,7 +75,7 @@ ARGV.each do |fn|
   source.each do |offset, labels|
     m = Redwood::Message.new :source => source, :source_info => offset
     m.load_from_source!
-    source_id = index.source_for_id m.id
+    source_id = Redwood::SourceManager.source_for_id m.id
     next unless source_id
     source_ids[source_id] += 1
     count += 1
@@ -85,7 +86,7 @@ ARGV.each do |fn|
     id = source_ids.keys.first.to_i
     puts "assigned #{source} to #{source_ids.keys.first}"
     source.id = id
-    index.add_source source
+    Redwood::SourceManager.add_source source
   else
     puts ">> unable to determine #{source}: #{source_ids.inspect}"
   end
index 18a3cabeb87ada9a1bc75b2101df12d6038f9f13..270524ae692783e88aa30eb0cde099eccad1c33d 100755 (executable)
@@ -116,11 +116,11 @@ begin
   index.load
 
   sources = ARGV.map do |uri|
-    index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
+    Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
   end
   
-  sources = index.usual_sources if sources.empty?
-  sources = index.sources if opts[:all_sources]
+  sources = Redwood::SourceManager.usual_sources if sources.empty?
+  sources = Redwood::SourceManager.sources if opts[:all_sources]
 
   unless target == :new
     if opts[:start_at]
index 05b9e8c0f5a27dd5781e537e99420bc340a76c76..f09ae5b51a35f3a1e71d05c1fc7f050f5fdd0007 100755 (executable)
@@ -80,13 +80,13 @@ begin
   index.load
 
   sources = ARGV.map do |uri|
-    s = index.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?"
+    s = Redwood::SourceManager.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?"
     s.is_a?(Redwood::MBox::Loader) or die "#{uri} is not an mbox source."
     s
   end
 
   if sources.empty?
-    sources = index.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader } 
+    sources = Redwood::SourceManager.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader }
   end
 
   unless sources.all? { |s| s.file_path.nil? } || File.executable?(dotlockfile) || opts[:dont_use_dotlockfile]
index 6f603e25fa579a74d36ff5c8c663fe9114378615..95a3b0379e0f8710fef5748dea5f4a42dd994c9d 100755 (executable)
@@ -66,10 +66,10 @@ begin
 
   source_ids = 
     if opts[:all_sources]
-      index.sources
+      Redwood::SourceManager.sources
     else
       ARGV.map do |uri|
-        index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
+        Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
       end
   end.map { |s| s.id }
   Trollop::die "nothing to do: no sources" if source_ids.empty?
index 83738204ed0ba6cee8bb466b5eeb15085cc1ad09..5689c2b9fa0237385b8e3ef7814767a7dc48ab39 100644 (file)
@@ -115,6 +115,7 @@ module Redwood
     Redwood::SuicideManager.new Redwood::SUICIDE_FN
     Redwood::CryptoManager.new
     Redwood::UndoManager.new
+    Redwood::SourceManager.new
   end
 
   def finish
@@ -130,7 +131,7 @@ module Redwood
   def report_broken_sources opts={}
     return unless BufferManager.instantiated?
 
-    broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
+    broken_sources = SourceManager.sources.select { |s| s.error.is_a? FatalSourceError }
     unless broken_sources.empty?
       BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
         TextMode.new(<<EOM)
@@ -147,7 +148,7 @@ EOM
       end
     end
 
-    desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
+    desynced_sources = SourceManager.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
     unless desynced_sources.empty?
       BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
         TextMode.new(<<EOM)
index 3df97bc8c31208a8fb7361ac03294ecb50356d9f..f258d81d3ecc857d2ea67b9f7a0ba70b584cf993 100644 (file)
@@ -26,11 +26,7 @@ class Index
 
   def initialize dir=BASE_DIR
     @index_mutex = Monitor.new
-
     @dir = dir
-    @sources = {}
-    @sources_dirty = false
-    @source_mutex = Monitor.new
 
     wsa = Ferret::Analysis::WhiteSpaceAnalyzer.new false
     sa = Ferret::Analysis::StandardAnalyzer.new [], true
@@ -112,36 +108,17 @@ EOS
   end
 
   def load
-    load_sources
+    SourceManager.load_sources
     load_index
   end
 
   def save
     Redwood::log "saving index and sources..."
     FileUtils.mkdir_p @dir unless File.exists? @dir
-    save_sources
+    SourceManager.save_sources
     save_index
   end
 
-  def add_source source
-    @source_mutex.synchronize do
-      raise "duplicate source!" if @sources.include? source
-      @sources_dirty = true
-      max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id }
-      source.id ||= (max || 0) + 1
-      ##source.id += 1 while @sources.member? source.id
-      @sources[source.id] = source
-    end
-  end
-
-  def sources
-    ## favour the inbox by listing non-archived sources first
-    @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten
-  end
-
-  def source_for uri; sources.find { |s| s.is_source_for? uri }; end
-  def usual_sources; sources.find_all { |s| s.usual? }; end
-
   def load_index dir=File.join(@dir, "ferret")
     if File.exists? dir
       Redwood::log "loading index..."
@@ -383,7 +360,7 @@ EOS
     @index_mutex.synchronize do
       doc = @index[docid] or return
 
-      source = @source_mutex.synchronize { @sources[doc[:source_id].to_i] }
+      source = SourceManager[doc[:source_id].to_i]
       raise "invalid source #{doc[:source_id]}" unless source
 
       #puts "building message #{doc[:message_id]} (#{source}##{doc[:source_info]})"
@@ -442,14 +419,6 @@ EOS
     contacts.keys.compact
   end
 
-  def load_sources fn=Redwood::SOURCE_FN
-    source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o }
-    @source_mutex.synchronize do
-      @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten]
-      @sources_dirty = false
-    end
-  end
-
   def each_docid query={}
     ferret_query = build_ferret_query query
     results = @index_mutex.synchronize { @index.search ferret_query, :limit => (query[:limit] || :all) }
@@ -604,21 +573,6 @@ private
     q.add_query Ferret::Search::TermQuery.new("source_id", query[:source_id]), :must if query[:source_id]
     q
   end
-
-  def save_sources fn=Redwood::SOURCE_FN
-    @source_mutex.synchronize do
-      if @sources_dirty || @sources.any? { |id, s| s.dirty? }
-        bakfn = fn + ".bak"
-        if File.exists? fn
-          File.chmod 0600, fn
-          FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
-        end
-        Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
-        File.chmod 0600, fn
-      end
-      @sources_dirty = false
-    end
-  end
 end
 
 end
index bbad5f2d580d6fc95287949e128034fde1c24352..c83290cc073ecac3d7e9dc3797a0d432a3f482de 100644 (file)
@@ -83,7 +83,7 @@ EOS
     from_and_subj_inbox = []
 
     @mutex.synchronize do
-      Index.usual_sources.each do |source|
+      SourceManager.usual_sources.each do |source|
 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
         begin
           yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
index fb98dbc71a1bdfee155225a74ab0f860706893bf..1bb77973f0d609d6441c726ada6c7b2236a28d03 100644 (file)
@@ -155,4 +155,61 @@ protected
   end
 end
 
+class SourceManager
+  include Singleton
+
+  def initialize
+    @sources = {}
+    @sources_dirty = false
+    @source_mutex = Monitor.new
+    self.class.i_am_the_instance self
+  end
+
+  def [](id)
+    @source_mutex.synchronize { @sources[id] }
+  end
+
+  def add_source source
+    @source_mutex.synchronize do
+      raise "duplicate source!" if @sources.include? source
+      @sources_dirty = true
+      max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id }
+      source.id ||= (max || 0) + 1
+      ##source.id += 1 while @sources.member? source.id
+      @sources[source.id] = source
+    end
+  end
+
+  def sources
+    ## favour the inbox by listing non-archived sources first
+    @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten
+  end
+
+  def source_for uri; sources.find { |s| s.is_source_for? uri }; end
+  def usual_sources; sources.find_all { |s| s.usual? }; end
+
+  def load_sources fn=Redwood::SOURCE_FN
+    source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o }
+    @source_mutex.synchronize do
+      @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten]
+      @sources_dirty = false
+    end
+  end
+
+  def save_sources fn=Redwood::SOURCE_FN
+    @source_mutex.synchronize do
+      if @sources_dirty || @sources.any? { |id, s| s.dirty? }
+        bakfn = fn + ".bak"
+        if File.exists? fn
+          File.chmod 0600, fn
+          FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
+        end
+        Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
+        File.chmod 0600, fn
+      end
+      @sources_dirty = false
+    end
+  end
+end
+
 end