]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
keybindings: ; -> buffer-list-mode, b, B, +
[sup] / lib / sup / mbox / loader.rb
index db6b16492aa761c6f61018ff7653d99bdc36f12b..7fe912900e16d477fc7d690873d92e9c623f48c2 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 || []) + [:unread]).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
 
@@ -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,7 +85,7 @@ class Loader < Source
     ret = ""
     @mutex.synchronize do
       @f.seek offset
-      until @f.eof? || (l = @f.gets) =~ /^$/
+      until @f.eof? || (l = @f.gets) =~ /^\r*$/
         ret += l
       end
     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