8 yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
10 ## uri_or_fp is horrific. need to refactor.
11 def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
13 @labels = (labels || []).freeze
17 uri_or_fp = Source.expand_filesystem_uri uri_or_fp
19 raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
20 raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
21 @f = File.open uri.path
26 super uri_or_fp, start_offset, usual, archived, id
29 def file_path; URI(uri).path end
30 def is_source_for? uri; super || (URI(Source.expand_filesystem_uri(uri)) == URI(self.uri)); end
32 def self.suggest_labels_for path
33 ## heuristic: use the filename as a label, unless the file
34 ## has a path that probably represents an inbox.
35 if File.dirname(path) =~ /\b(var|usr|spool)\b/
38 [File.basename(path).intern]
43 if (cur_offset ||= start_offset) > end_offset
44 raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
48 def start_offset; 0; end
49 def end_offset; File.size @f; end
51 def load_header offset
57 raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}."
59 header = MBox::read_header @f
64 def load_message offset
68 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
69 return RMail::Parser.read(input)
71 rescue RMail::Parser::Error => e
72 raise FatalSourceError, "error parsing mbox file: #{e.message}"
81 until @f.eof? || (l = @f.gets) =~ /^$/
88 def raw_full_message offset
90 each_raw_full_message_line(offset) { |l| ret += l }
94 ## apparently it's a million times faster to call this directly if
95 ## we're just moving messages around on disk, than reading things
96 ## into memory with raw_full_message.
98 ## i hoped never to have to move shit around on disk but
99 ## sup-sync-back has to do it.
100 def each_raw_full_message_line offset
101 @mutex.synchronize do
104 until @f.eof? || (l = @f.gets) =~ BREAK_RE
111 returned_offset = nil
112 next_offset = cur_offset
115 @mutex.synchronize do
118 ## cur_offset could be at one of two places here:
120 ## 1. before a \n and a mbox separator, if it was previously at
121 ## EOF and a new message was added; or,
122 ## 2. at the beginning of an mbox separator (in all other
125 l = @f.gets or raise "next while at EOF"
126 if l =~ /^\s*$/ # case 1
127 returned_offset = @f.tell
128 @f.gets # now we're at a BREAK_RE, so skip past it
130 returned_offset = cur_offset
131 ## we've already skipped past the BREAK_RE, so just go
134 while(line = @f.gets)
135 break if line =~ BREAK_RE
136 next_offset = @f.tell
139 rescue SystemCallError, IOError => e
140 raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
143 self.cur_offset = next_offset
144 [returned_offset, @labels]