10 def initialize uri, start_offset=nil, usual=true, archived=false, id=nil
11 raise ArgumentError, "not an mbox uri" unless uri =~ %r!mbox://!
15 @filename = uri.sub(%r!^mbox://!, "")
16 @f = File.open @filename
17 ## heuristic: use the filename as a label, unless the file
18 ## has a path that probably represents an inbox.
20 @labels << File.basename(@filename).intern unless File.dirname(@filename) =~ /\b(var|usr|spool)\b/
23 def start_offset; 0; end
24 def end_offset; File.size @f; end
26 def load_header offset
31 raise SourceError, "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this." unless l =~ BREAK_RE
32 header = MBox::read_header @f
37 def load_message offset
41 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
42 return RMail::Parser.read(input)
44 rescue RMail::Parser::Error => e
45 raise SourceError, "error parsing message with rmail: #{e.message}"
54 until @f.eof? || (l = @f.gets) =~ /^$/
61 def raw_full_message offset
65 @f.gets # skip mbox header
66 until @f.eof? || (l = @f.gets) =~ BREAK_RE
75 next_offset = cur_offset
80 ## cur_offset could be at one of two places here:
82 ## 1. before a \n and a mbox separator, if it was previously at
83 ## EOF and a new message was added; or,
84 ## 2. at the beginning of an mbox separator (in all other
87 l = @f.gets or raise "next while at EOF"
88 if l =~ /^\s*$/ # case 1
89 returned_offset = @f.tell
90 @f.gets # now we're at a BREAK_RE, so skip past it
92 returned_offset = cur_offset
93 ## we've already skipped past the BREAK_RE, to just go
97 break if line =~ BREAK_RE
102 self.cur_offset = next_offset
103 [returned_offset, labels]
107 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))