]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
Merge branch 'message-id-normalization' into next
[sup] / lib / sup / mbox / loader.rb
index 05ddbab09f6ae90153274add4c0b9083a2c78781..58f4fc7d8fefbf2379edf315e6bc97ad8cc187ea 100644 (file)
@@ -5,24 +5,40 @@ module Redwood
 module MBox
 
 class Loader < Source
-  yaml_properties :uri, :cur_offset, :usual, :archived, :id
-  def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
-    super
+  yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
+  attr_accessor :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 || []) - LabelManager::RESERVED_LABELS).uniq.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/
+      raise ArgumentError, "mbox URI ('#{uri}') cannot have a host: #{uri.host}" if uri.host
+      raise ArgumentError, "mbox URI must have a path component" unless uri.path
       @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).downcase.intern]
     end
   end
 
@@ -53,7 +69,11 @@ class Loader < Source
       @f.seek offset
       begin
         RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
-          return RMail::Parser.read(input)
+          m = RMail::Parser.read(input)
+          if m.body && m.body.is_a?(String)
+            m.body.gsub!(/^>From /, "From ")
+          end
+          return m
         end
       rescue RMail::Parser::Error => e
         raise FatalSourceError, "error parsing mbox file: #{e.message}"
@@ -72,19 +92,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
@@ -128,7 +148,7 @@ class Loader < Source
     end
 
     self.cur_offset = next_offset
-    [returned_offset, @labels.clone]
+    [returned_offset, (self.labels + [:unread]).uniq]
   end
 end