lib/sup/mbox/loader.rb in sup-0.0.2 vs lib/sup/mbox/loader.rb in sup-0.0.3
- old
+ new
@@ -1,29 +1,37 @@
-require 'thread'
require 'rmail'
module Redwood
module MBox
class Loader < Source
- attr_reader :labels
+ attr_reader_cloned :labels
- def initialize uri, start_offset=nil, usual=true, archived=false, id=nil
- raise ArgumentError, "not an mbox uri" unless uri =~ %r!mbox://!
+ def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
super
@mutex = Mutex.new
- @filename = uri.sub(%r!^mbox://!, "")
- @f = File.open @filename
- ## heuristic: use the filename as a label, unless the file
- ## has a path that probably represents an inbox.
@labels = [:unread]
- @labels << File.basename(@filename).intern unless File.dirname(@filename) =~ /\b(var|usr|spool)\b/
+ @labels << :inbox unless archived?
+
+ 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
+ else
+ @f = uri_or_fp
+ 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
@mutex.synchronize do
@f.seek offset
@@ -36,10 +44,11 @@
end
header
end
def load_message offset
+ raise SourceError, self.broken_msg if broken?
@mutex.synchronize do
@f.seek offset
begin
RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
return RMail::Parser.read(input)
@@ -49,10 +58,11 @@
end
end
end
def raw_header offset
+ raise SourceError, self.broken_msg if broken?
ret = ""
@mutex.synchronize do
@f.seek offset
until @f.eof? || (l = @f.gets) =~ /^$/
ret += l
@@ -60,10 +70,11 @@
end
ret
end
def raw_full_message offset
+ raise SourceError, self.broken_msg if broken?
ret = ""
@mutex.synchronize do
@f.seek offset
@f.gets # skip mbox header
until @f.eof? || (l = @f.gets) =~ BREAK_RE
@@ -72,9 +83,10 @@
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