8 yaml_properties :uri, :cur_offset, :usual, :archived, :id
9 def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
18 raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
19 raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
20 ## heuristic: use the filename as a label, unless the file
21 ## has a path that probably represents an inbox.
22 @labels << File.basename(uri.path).intern unless File.dirname(uri.path) =~ /\b(var|usr|spool)\b/
23 @f = File.open uri.path
30 if (cur_offset ||= start_offset) > end_offset
31 raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
35 def start_offset; 0; end
36 def end_offset; File.size @f; end
38 def load_header offset
44 raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}."
46 header = MBox::read_header @f
51 def load_message offset
55 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
56 return RMail::Parser.read(input)
58 rescue RMail::Parser::Error => e
59 raise FatalSourceError, "error parsing mbox file: #{e.message}"
68 until @f.eof? || (l = @f.gets) =~ /^$/
75 def raw_full_message offset
77 each_raw_full_message_line(offset) { |l| ret += l }
81 ## apparently it's a million times faster to call this directly if
82 ## we're just moving messages around on disk, than reading things
83 ## into memory with raw_full_message.
85 ## i hoped never to have to move shit around on disk but
86 ## sup-sync-back has to do it.
87 def each_raw_full_message_line offset
91 until @f.eof? || (l = @f.gets) =~ BREAK_RE
99 next_offset = cur_offset
102 @mutex.synchronize do
105 ## cur_offset could be at one of two places here:
107 ## 1. before a \n and a mbox separator, if it was previously at
108 ## EOF and a new message was added; or,
109 ## 2. at the beginning of an mbox separator (in all other
112 l = @f.gets or raise "next while at EOF"
113 if l =~ /^\s*$/ # case 1
114 returned_offset = @f.tell
115 @f.gets # now we're at a BREAK_RE, so skip past it
117 returned_offset = cur_offset
118 ## we've already skipped past the BREAK_RE, so just go
121 while(line = @f.gets)
122 break if line =~ BREAK_RE
123 next_offset = @f.tell
126 rescue SystemCallError, IOError => e
127 raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
130 self.cur_offset = next_offset
131 [returned_offset, @labels.clone]