7 def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
15 raise ArgumentError, "not an mbox uri" unless uri_or_fp =~ %r!mbox://!
17 fn = uri_or_fp.sub(%r!^mbox://!, "")
18 ## heuristic: use the filename as a label, unless the file
19 ## has a path that probably represents an inbox.
20 @labels << File.basename(fn).intern unless File.dirname(fn) =~ /\b(var|usr|spool)\b/
27 def start_offset; 0; end
28 def end_offset; File.size @f; end
30 def load_header offset
36 Redwood::log "#{to_s}: offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}"
37 self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this."
38 raise SourceError, self.broken_msg
40 header = MBox::read_header @f
45 def load_message offset
46 raise SourceError, self.broken_msg if broken?
50 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
51 return RMail::Parser.read(input)
53 rescue RMail::Parser::Error => e
54 raise SourceError, "error parsing message with rmail: #{e.message}"
60 raise SourceError, self.broken_msg if broken?
64 until @f.eof? || (l = @f.gets) =~ /^$/
71 def raw_full_message offset
72 raise SourceError, self.broken_msg if broken?
76 @f.gets # skip mbox header
77 until @f.eof? || (l = @f.gets) =~ BREAK_RE
85 raise SourceError, self.broken_msg if broken?
87 next_offset = cur_offset
92 ## cur_offset could be at one of two places here:
94 ## 1. before a \n and a mbox separator, if it was previously at
95 ## EOF and a new message was added; or,
96 ## 2. at the beginning of an mbox separator (in all other
99 l = @f.gets or raise "next while at EOF"
100 if l =~ /^\s*$/ # case 1
101 returned_offset = @f.tell
102 @f.gets # now we're at a BREAK_RE, so skip past it
104 returned_offset = cur_offset
105 ## we've already skipped past the BREAK_RE, so just go
108 while(line = @f.gets)
109 break if line =~ BREAK_RE
110 next_offset = @f.tell
114 self.cur_offset = next_offset
115 [returned_offset, @labels.clone]
119 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))