7 attr_reader_cloned :labels
9 def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
14 @labels << :inbox unless archived?
18 raise ArgumentError, "not an mbox uri" unless uri_or_fp =~ %r!mbox://!
20 fn = uri_or_fp.sub(%r!^mbox://!, "")
21 ## heuristic: use the filename as a label, unless the file
22 ## has a path that probably represents an inbox.
23 @labels << File.basename(fn).intern unless File.dirname(fn) =~ /\b(var|usr|spool)\b/
30 def start_offset; 0; end
31 def end_offset; File.size @f; end
33 def load_header offset
39 Redwood::log "#{to_s}: offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}"
40 self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this."
41 raise SourceError, self.broken_msg
43 header = MBox::read_header @f
48 def load_message offset
49 raise SourceError, self.broken_msg if broken?
53 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
54 return RMail::Parser.read(input)
56 rescue RMail::Parser::Error => e
57 raise SourceError, "error parsing message with rmail: #{e.message}"
63 raise SourceError, self.broken_msg if broken?
67 until @f.eof? || (l = @f.gets) =~ /^$/
74 def raw_full_message offset
75 raise SourceError, self.broken_msg if broken?
79 @f.gets # skip mbox header
80 until @f.eof? || (l = @f.gets) =~ BREAK_RE
88 raise SourceError, self.broken_msg if broken?
90 next_offset = cur_offset
95 ## cur_offset could be at one of two places here:
97 ## 1. before a \n and a mbox separator, if it was previously at
98 ## EOF and a new message was added; or,
99 ## 2. at the beginning of an mbox separator (in all other
102 l = @f.gets or raise "next while at EOF"
103 if l =~ /^\s*$/ # case 1
104 returned_offset = @f.tell
105 @f.gets # now we're at a BREAK_RE, so skip past it
107 returned_offset = cur_offset
108 ## we've already skipped past the BREAK_RE, so just go
111 while(line = @f.gets)
112 break if line =~ BREAK_RE
113 next_offset = @f.tell
117 self.cur_offset = next_offset
118 [returned_offset, labels]
122 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))