]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
mbox loader bugfix
[sup] / lib / sup / mbox / loader.rb
index 49eb37b13e2b0e1382cfe2acf7dde0a68fcf945a..db6b16492aa761c6f61018ff7653d99bdc36f12b 100644 (file)
@@ -5,23 +5,38 @@ module Redwood
 module MBox
 
 class Loader < Source
-  def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
-    super
+  yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
 
+  ## uri_or_fp is horrific. need to refactor.
+  def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
     @mutex = Mutex.new
-    @labels = [:unread]
+    @labels = ((labels || []) + [:unread]).freeze
 
     case uri_or_fp
     when String
-      uri = URI(uri_or_fp)
+      uri = URI(Source.expand_filesystem_uri(uri_or_fp))
       raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
       raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
-      ## heuristic: use the filename as a label, unless the file
-      ## has a path that probably represents an inbox.
-      @labels << File.basename(uri.path).intern unless File.dirname(uri.path) =~ /\b(var|usr|spool)\b/
       @f = File.open uri.path
+      @path = uri.path
     else
       @f = uri_or_fp
+      @path = uri_or_fp.path
+    end
+
+    super uri_or_fp, start_offset, usual, archived, id
+  end
+
+  def file_path; @path end
+  def is_source_for? uri; super || (self.uri.is_a?(String) && (URI(Source.expand_filesystem_uri(uri)) == URI(Source.expand_filesystem_uri(self.uri)))) end
+
+  def self.suggest_labels_for path
+    ## heuristic: use the filename as a label, unless the file
+    ## has a path that probably represents an inbox.
+    if File.dirname(path) =~ /\b(var|usr|spool)\b/
+      []
+    else
+      [File.basename(path).intern]
     end
   end
 
@@ -71,19 +86,19 @@ class Loader < Source
     ret
   end
 
-  def raw_full_message offset
+  def raw_message offset
     ret = ""
-    each_raw_full_message_line(offset) { |l| ret += l }
+    each_raw_message_line(offset) { |l| ret += l }
     ret
   end
 
   ## apparently it's a million times faster to call this directly if
   ## we're just moving messages around on disk, than reading things
-  ## into memory with raw_full_message.
+  ## into memory with raw_message.
   ##
   ## i hoped never to have to move shit around on disk but
   ## sup-sync-back has to do it.
-  def each_raw_full_message_line offset
+  def each_raw_message_line offset
     @mutex.synchronize do
       @f.seek offset
       yield @f.gets
@@ -127,11 +142,9 @@ class Loader < Source
     end
 
     self.cur_offset = next_offset
-    [returned_offset, @labels.clone]
+    [returned_offset, @labels]
   end
 end
 
-Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
-
 end
 end