]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
finally do imap flags the right way, and clean up mbox and imap source flag duplicati...
[sup] / lib / sup / mbox / loader.rb
index 22518ca92a4b127a749b93374f8a4364eb1d2af7..c1392654e17aa7e00fe471efa2c978e41458d46b 100644 (file)
@@ -1,35 +1,53 @@
 require 'rmail'
+require 'uri'
 
 module Redwood
 module MBox
 
 class Loader < Source
-  attr_reader_cloned :labels
-
-  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 << :inbox unless archived?
+    @labels = ((labels || []) - LabelManager::RESERVED_LABELS).uniq.freeze
 
     case uri_or_fp
     when String
-      raise ArgumentError, "not an mbox uri" unless uri_or_fp =~ %r!mbox://!
-
-      fn = uri_or_fp.sub(%r!^mbox://!, "")
-      ## heuristic: use the filename as a label, unless the file
-      ## has a path that probably represents an inbox.
-      @labels << File.basename(fn).intern unless File.dirname(fn) =~ /\b(var|usr|spool)\b/
-      @f = File.open fn
+      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
+      @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
 
+  def check
+    if (cur_offset ||= start_offset) > end_offset
+      raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
+    end
+  end
+    
   def start_offset; 0; end
   def end_offset; File.size @f; end
-  def total; end_offset; end
 
   def load_header offset
     header = nil
@@ -37,8 +55,7 @@ class Loader < Source
       @f.seek offset
       l = @f.gets
       unless l =~ BREAK_RE
-        self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this." 
-        raise SourceError, self.broken_msg
+        raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}." 
       end
       header = MBox::read_header @f
     end
@@ -46,7 +63,6 @@ class Loader < Source
   end
 
   def load_message offset
-    raise SourceError, self.broken_msg if broken?
     @mutex.synchronize do
       @f.seek offset
       begin
@@ -54,13 +70,12 @@ class Loader < Source
           return RMail::Parser.read(input)
         end
       rescue RMail::Parser::Error => e
-        raise SourceError, "error parsing message with rmail: #{e.message}"
+        raise FatalSourceError, "error parsing mbox file: #{e.message}"
       end
     end
   end
 
   def raw_header offset
-    raise SourceError, self.broken_msg if broken?
     ret = ""
     @mutex.synchronize do
       @f.seek offset
@@ -71,55 +86,65 @@ class Loader < Source
     ret
   end
 
-  def raw_full_message offset
-    raise SourceError, self.broken_msg if broken?
+  def raw_message offset
     ret = ""
+    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_message.
+  ##
+  ## i hoped never to have to move shit around on disk but
+  ## sup-sync-back has to do it.
+  def each_raw_message_line offset
     @mutex.synchronize do
       @f.seek offset
-      @f.gets # skip mbox header
+      yield @f.gets
       until @f.eof? || (l = @f.gets) =~ BREAK_RE
-        ret += l
+        yield l
       end
     end
-    ret
   end
 
   def next
-    raise SourceError, self.broken_msg if broken?
     returned_offset = nil
     next_offset = cur_offset
 
-    @mutex.synchronize do
-      @f.seek cur_offset
-
-      ## cur_offset could be at one of two places here:
-
-      ## 1. before a \n and a mbox separator, if it was previously at
-      ##    EOF and a new message was added; or,
-      ## 2. at the beginning of an mbox separator (in all other
-      ##    cases).
-
-      l = @f.gets or raise "next while at EOF"
-      if l =~ /^\s*$/ # case 1
-        returned_offset = @f.tell
-        @f.gets # now we're at a BREAK_RE, so skip past it
-      else # case 2
-        returned_offset = cur_offset
-        ## we've already skipped past the BREAK_RE, to just go
-      end
+    begin
+      @mutex.synchronize do
+        @f.seek cur_offset
+
+        ## cur_offset could be at one of two places here:
 
-      while(line = @f.gets)
-        break if line =~ BREAK_RE
-        next_offset = @f.tell
+        ## 1. before a \n and a mbox separator, if it was previously at
+        ##    EOF and a new message was added; or,
+        ## 2. at the beginning of an mbox separator (in all other
+        ##    cases).
+
+        l = @f.gets or raise "next while at EOF"
+        if l =~ /^\s*$/ # case 1
+          returned_offset = @f.tell
+          @f.gets # now we're at a BREAK_RE, so skip past it
+        else # case 2
+          returned_offset = cur_offset
+          ## we've already skipped past the BREAK_RE, so just go
+        end
+
+        while(line = @f.gets)
+          break if line =~ BREAK_RE
+          next_offset = @f.tell
+        end
       end
+    rescue SystemCallError, IOError => e
+      raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
     end
 
     self.cur_offset = next_offset
-    [returned_offset, labels]
+    [returned_offset, (@labels + [:unread]).uniq]
   end
 end
 
-Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
-
 end
 end