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 = URI(Source.expand_filesystem_uri(uri_or_fp))
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 @f = File.open uri.path
24 @path = uri_or_fp.path
27 super uri_or_fp, start_offset, usual, archived, id
30 def file_path; @path end
31 def is_source_for? uri; super || (self.uri.is_a?(String) && (URI(Source.expand_filesystem_uri(uri)) == URI(Source.expand_filesystem_uri(self.uri)))) end
33 def self.suggest_labels_for path
34 ## heuristic: use the filename as a label, unless the file
35 ## has a path that probably represents an inbox.
36 if File.dirname(path) =~ /\b(var|usr|spool)\b/
39 [File.basename(path).intern]
44 if (cur_offset ||= start_offset) > end_offset
45 raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
49 def start_offset; 0; end
50 def end_offset; File.size @f; end
52 def load_header offset
58 raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}."
60 header = MBox::read_header @f
65 def load_message offset
69 RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
70 return RMail::Parser.read(input)
72 rescue RMail::Parser::Error => e
73 raise FatalSourceError, "error parsing mbox file: #{e.message}"
82 until @f.eof? || (l = @f.gets) =~ /^$/
89 def raw_message offset
91 each_raw_message_line(offset) { |l| ret += l }
95 ## apparently it's a million times faster to call this directly if
96 ## we're just moving messages around on disk, than reading things
97 ## into memory with raw_message.
99 ## i hoped never to have to move shit around on disk but
100 ## sup-sync-back has to do it.
101 def each_raw_message_line offset
102 @mutex.synchronize do
105 until @f.eof? || (l = @f.gets) =~ BREAK_RE
112 returned_offset = nil
113 next_offset = cur_offset
116 @mutex.synchronize do
119 ## cur_offset could be at one of two places here:
121 ## 1. before a \n and a mbox separator, if it was previously at
122 ## EOF and a new message was added; or,
123 ## 2. at the beginning of an mbox separator (in all other
126 l = @f.gets or raise "next while at EOF"
127 if l =~ /^\s*$/ # case 1
128 returned_offset = @f.tell
129 @f.gets # now we're at a BREAK_RE, so skip past it
131 returned_offset = cur_offset
132 ## we've already skipped past the BREAK_RE, so just go
135 while(line = @f.gets)
136 break if line =~ BREAK_RE
137 next_offset = @f.tell
140 rescue SystemCallError, IOError => e
141 raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
144 self.cur_offset = next_offset
145 [returned_offset, @labels]