]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
move MBox.parse_header -> Source.parse_raw_email_header
[sup] / lib / sup / mbox / loader.rb
index 84752281b63091d5887cb470091340dc61d3ca25..c623239b290cf98b5d73d6db28223272ba994a0e 100644 (file)
@@ -6,17 +6,19 @@ module MBox
 
 class Loader < Source
   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 = (labels || []).freeze
+    @labels = ((labels || []) - LabelManager::RESERVED_LABELS).uniq.freeze
 
     case uri_or_fp
     when String
       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
+      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
@@ -36,7 +38,7 @@ class Loader < Source
     if File.dirname(path) =~ /\b(var|usr|spool)\b/
       []
     else
-      [File.basename(path).intern]
+      [File.basename(path).downcase.intern]
     end
   end
 
@@ -57,7 +59,7 @@ class Loader < Source
       unless l =~ BREAK_RE
         raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}." 
       end
-      header = MBox::read_header @f
+      header = parse_raw_email_header @f
     end
     header
   end
@@ -67,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}"
@@ -79,8 +85,8 @@ class Loader < Source
     ret = ""
     @mutex.synchronize do
       @f.seek offset
-      until @f.eof? || (l = @f.gets) =~ /^$/
-        ret += l
+      until @f.eof? || (l = @f.gets) =~ /^\r*$/
+        ret << l
       end
     end
     ret
@@ -88,7 +94,7 @@ class Loader < Source
 
   def raw_message offset
     ret = ""
-    each_raw_message_line(offset) { |l| ret += l }
+    each_raw_message_line(offset) { |l| ret << l }
     ret
   end
 
@@ -142,7 +148,7 @@ class Loader < Source
     end
 
     self.cur_offset = next_offset
-    [returned_offset, @labels]
+    [returned_offset, (self.labels + [:unread]).uniq]
   end
 end