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
32 self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this."
33 raise SourceError, self.broken_msg
35 header = MBox::read_header @f
40 def load_message offset
44 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
45 return RMail::Parser.read(input)
47 rescue RMail::Parser::Error => e
48 raise SourceError, "error parsing message with rmail: #{e.message}"
57 until @f.eof? || (l = @f.gets) =~ /^$/
64 def raw_full_message offset
68 @f.gets # skip mbox header
69 until @f.eof? || (l = @f.gets) =~ BREAK_RE
78 next_offset = cur_offset
83 ## cur_offset could be at one of two places here:
85 ## 1. before a \n and a mbox separator, if it was previously at
86 ## EOF and a new message was added; or,
87 ## 2. at the beginning of an mbox separator (in all other
90 l = @f.gets or raise "next while at EOF"
91 if l =~ /^\s*$/ # case 1
92 returned_offset = @f.tell
93 @f.gets # now we're at a BREAK_RE, so skip past it
95 returned_offset = cur_offset
96 ## we've already skipped past the BREAK_RE, to just go
100 break if line =~ BREAK_RE
101 next_offset = @f.tell
105 self.cur_offset = next_offset
106 [returned_offset, labels]
110 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))