6 ## Maildir doesn't provide an ordered unique id, which is what Sup
7 ## requires to be really useful. So we must maintain, in memory, a
8 ## mapping between Sup "ids" (timestamps, essentially) and the
11 class Maildir < Source
12 SCAN_INTERVAL = 30 # seconds
14 def initialize uri, last_date=nil, usual=true, archived=false, id=nil
26 with_file_for(id) { |f| MBox::read_header f }
31 with_file_for(id) { |f| RMail::Parser.read f }
37 with_file_for(id) do |f|
38 until f.eof? || (l = f.gets) =~ /^$/
45 def raw_full_message id
47 with_file_for(id) { |f| f.readlines.join }
51 return if @last_scan && (Time.now - @last_scan) < SCAN_INTERVAL
53 cdir = File.join(@dir, 'cur')
54 ndir = File.join(@dir, 'new')
57 @ids, @ids_to_fns = @mutex.synchronize do
58 ids, ids_to_fns = [], {}
59 (Dir[File.join(cdir, "*")] + Dir[File.join(ndir, "*")]).map do |fn|
64 [ids.sort, ids_to_fns]
66 rescue SystemCallError => e
67 raise FatalSourceError, "Problem scanning Maildir directories: #{e.message}."
75 start = @ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}." # couldn't find the most recent email
77 start.upto(@ids.length - 1) do |i|
80 yield id, (@ids_to_fns[id] =~ /,.*R.*$/ ? [] : [:unread])
94 def pct_done; 100.0 * (@ids.index(cur_offset) || 0).to_f / (@ids.length - 1).to_f; end
99 # use 7 digits for the size. why 7? seems nice.
100 sprintf("%d%07d", File.mtime(fn), File.size(fn)).to_i
104 fn = @ids_to_fns[id] or raise OutOfSyncSourceError, "No such id: #{id.inspect}."
106 File.open(fn) { |f| yield f }
107 rescue SystemCallError => e
108 raise FatalSourceError, "Problem reading file for id #{id.inspect}: #{fn.inspect}: #{e.message}."
113 Redwood::register_yaml(Maildir, %w(uri cur_offset usual archived id))