8 yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
11 ## uri_or_fp is horrific. need to refactor.
12 def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
14 @labels = ((labels || []) - LabelManager::RESERVED_LABELS).uniq.freeze
18 uri = URI(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 raise ArgumentError, "mbox URI must have a path component" unless uri.path
22 @f = File.open uri.path
26 @path = uri_or_fp.path
29 super uri_or_fp, start_offset, usual, archived, id
32 def file_path; @path end
33 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
35 def self.suggest_labels_for path
36 ## heuristic: use the filename as a label, unless the file
37 ## has a path that probably represents an inbox.
38 if File.dirname(path) =~ /\b(var|usr|spool)\b/
41 [File.basename(path).downcase.intern]
46 if (cur_offset ||= start_offset) > end_offset
47 raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
51 def start_offset; 0; end
52 def end_offset; File.size @f; end
54 def load_header offset
60 raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}."
62 header = MBox::read_header @f
67 def load_message offset
71 ## don't use RMail::Mailbox::MBoxReader because it doesn't properly ignore
72 ## "From" at the start of a message body line.
75 string << l until @f.eof? || (l = @f.gets) =~ BREAK_RE
76 RMail::Parser.read string
77 rescue RMail::Parser::Error => e
78 raise FatalSourceError, "error parsing mbox file: #{e.message}"
87 until @f.eof? || (l = @f.gets) =~ /^\r*$/
94 def raw_message offset
96 each_raw_message_line(offset) { |l| ret << l }
100 ## apparently it's a million times faster to call this directly if
101 ## we're just moving messages around on disk, than reading things
102 ## into memory with raw_message.
104 ## i hoped never to have to move shit around on disk but
105 ## sup-sync-back has to do it.
106 def each_raw_message_line offset
107 @mutex.synchronize do
110 until @f.eof? || (l = @f.gets) =~ BREAK_RE
117 returned_offset = nil
118 next_offset = cur_offset
121 @mutex.synchronize do
124 ## cur_offset could be at one of two places here:
126 ## 1. before a \n and a mbox separator, if it was previously at
127 ## EOF and a new message was added; or,
128 ## 2. at the beginning of an mbox separator (in all other
131 l = @f.gets or return nil
132 if l =~ /^\s*$/ # case 1
133 returned_offset = @f.tell
134 @f.gets # now we're at a BREAK_RE, so skip past it
136 returned_offset = cur_offset
137 ## we've already skipped past the BREAK_RE, so just go
140 while(line = @f.gets)
141 break if line =~ BREAK_RE
142 next_offset = @f.tell
145 rescue SystemCallError, IOError => e
146 raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
149 self.cur_offset = next_offset
150 [returned_offset, (self.labels + [:unread]).uniq]