9 include SerializeLabelsNicely
10 yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
12 ## uri_or_fp is horrific. need to refactor.
13 def initialize uri_or_fp, start_offset=0, usual=true, archived=false, id=nil, labels=nil
15 @labels = Set.new((labels || []) - LabelManager::RESERVED_LABELS)
19 uri = URI(Source.expand_filesystem_uri(uri_or_fp))
20 raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
21 raise ArgumentError, "mbox URI ('#{uri}') cannot have a host: #{uri.host}" if uri.host
22 raise ArgumentError, "mbox URI must have a path component" unless uri.path
23 @f = File.open uri.path
27 @path = uri_or_fp.path
30 super uri_or_fp, start_offset, usual, archived, id
33 def file_path; @path end
34 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
36 def self.suggest_labels_for path
37 ## heuristic: use the filename as a label, unless the file
38 ## has a path that probably represents an inbox.
39 if File.dirname(path) =~ /\b(var|usr|spool)\b/
42 [File.basename(path).downcase.intern]
47 if (cur_offset ||= start_offset) > end_offset
48 raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
52 def start_offset; 0; end
53 def end_offset; File.size @f; end
55 def load_header offset
60 unless MBox::is_break_line? l
61 raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}."
63 header = parse_raw_email_header @f
68 def load_message offset
72 ## don't use RMail::Mailbox::MBoxReader because it doesn't properly ignore
73 ## "From" at the start of a message body line.
76 string << l until @f.eof? || MBox::is_break_line?(l = @f.gets)
77 RMail::Parser.read string
78 rescue RMail::Parser::Error => e
79 raise FatalSourceError, "error parsing mbox file: #{e.message}"
84 ## scan forward until we're at the valid start of a message
89 until @f.eof? || MBox::is_break_line?(l = @f.gets)
92 self.cur_offset += string.length
100 until @f.eof? || (l = @f.gets) =~ /^\r*$/
107 def raw_message offset
109 each_raw_message_line(offset) { |l| ret << l }
113 def store_message date, from_email, &block
114 need_blank = File.exists?(@filename) && !File.zero?(@filename)
115 File.open(@filename, "a") do |f|
117 f.puts "From #{from_email} #{date.utc}"
122 ## apparently it's a million times faster to call this directly if
123 ## we're just moving messages around on disk, than reading things
124 ## into memory with raw_message.
126 ## i hoped never to have to move shit around on disk but
127 ## sup-sync-back has to do it.
128 def each_raw_message_line offset
129 @mutex.synchronize do
132 until @f.eof? || MBox::is_break_line?(l = @f.gets)
139 returned_offset = nil
140 next_offset = cur_offset
143 @mutex.synchronize do
146 ## cur_offset could be at one of two places here:
148 ## 1. before a \n and a mbox separator, if it was previously at
149 ## EOF and a new message was added; or,
150 ## 2. at the beginning of an mbox separator (in all other
153 l = @f.gets or return nil
154 if l =~ /^\s*$/ # case 1
155 returned_offset = @f.tell
156 @f.gets # now we're at a BREAK_RE, so skip past it
158 returned_offset = cur_offset
159 ## we've already skipped past the BREAK_RE, so just go
162 while(line = @f.gets)
163 break if MBox::is_break_line? line
164 next_offset = @f.tell
167 rescue SystemCallError, IOError => e
168 raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
171 self.cur_offset = next_offset
172 [returned_offset, (@labels + [:unread])]