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 ## remind me never to use inheritance again.
15 yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels, :mtimes
16 def initialize uri, last_date=nil, usual=true, archived=false, id=nil, labels=[], mtimes={}
17 super uri, last_date, usual, archived, id
18 uri = URI(Source.expand_filesystem_uri(uri))
20 raise ArgumentError, "not a maildir URI" unless uri.scheme == "maildir"
21 raise ArgumentError, "maildir URI cannot have a host: #{uri.host}" if uri.host
22 raise ArgumentError, "maildir URI must have a path component" unless uri.path
25 @labels = (labels || []).freeze
30 #the mtime from the subdirs in the maildir with the unix epoch as default.
31 #these are used to determine whether scanning the directory for new mail
32 #is a worthwhile effort
33 @mtimes = { 'cur' => Time.at(0), 'new' => Time.at(0) }.merge(mtimes || {})
34 @dir_ids = { 'cur' => [], 'new' => [] }
37 def file_path; @dir end
38 def self.suggest_labels_for path; [] end
39 def is_source_for? uri; super || (URI(Source.expand_filesystem_uri(uri)) == URI(self.uri)); end
43 return unless start_offset
45 start = @ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}." # couldn't find the most recent email
48 def each_raw_message_line id
50 with_file_for(id) do |f|
59 with_file_for(id) { |f| MBox::read_header f }
64 with_file_for(id) { |f| RMail::Parser.read f }
70 with_file_for(id) do |f|
71 until f.eof? || (l = f.gets) =~ /^$/
80 with_file_for(id) { |f| f.read }
83 def scan_mailbox opts={}
84 return unless @ids.empty? || opts[:rescan]
85 return if @last_scan && (Time.now - @last_scan) < SCAN_INTERVAL
87 initial_poll = @ids.empty?
89 Redwood::log "scanning maildir #@dir..."
91 @mtimes.each_key do |d|
92 subdir = File.join(@dir, d)
93 raise FatalSourceError, "#{subdir} not a directory" unless File.directory? subdir
95 mtime = File.mtime subdir
97 #only scan the dir if the mtime is more recent (or we haven't polled
99 if @mtimes[d] < mtime || initial_poll
102 Dir[File.join(subdir, '*')].map do |fn|
108 Redwood::log "no poll on #{d}. mtime on indicates no new messages."
111 @ids = @dir_ids.values.flatten.uniq.sort!
112 rescue SystemCallError, IOError => e
113 raise FatalSourceError, "Problem scanning Maildir directories: #{e.message}."
116 Redwood::log "done scanning maildir"
117 @last_scan = Time.now
119 synchronized :scan_mailbox
123 return unless start_offset
125 start = @ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}." # couldn't find the most recent email
127 start.upto(@ids.length - 1) do |i|
130 yield id, @labels + (seen?(id) ? [] : [:unread]) + (trashed?(id) ? [:deleted] : []) + (flagged?(id) ? [:starred] : [])
140 scan_mailbox :rescan => true
144 def pct_done; 100.0 * (@ids.index(cur_offset) || 0).to_f / (@ids.length - 1).to_f; end
146 def draft? msg; maildir_data(msg)[2].include? "D"; end
147 def flagged? msg; maildir_data(msg)[2].include? "F"; end
148 def passed? msg; maildir_data(msg)[2].include? "P"; end
149 def replied? msg; maildir_data(msg)[2].include? "R"; end
150 def seen? msg; maildir_data(msg)[2].include? "S"; end
151 def trashed? msg; maildir_data(msg)[2].include? "T"; end
153 def mark_draft msg; maildir_mark_file msg, "D" unless draft? msg; end
154 def mark_flagged msg; maildir_mark_file msg, "F" unless flagged? msg; end
155 def mark_passed msg; maildir_mark_file msg, "P" unless passed? msg; end
156 def mark_replied msg; maildir_mark_file msg, "R" unless replied? msg; end
157 def mark_seen msg; maildir_mark_file msg, "S" unless seen? msg; end
158 def mark_trashed msg; maildir_mark_file msg, "T" unless trashed? msg; end
163 #doing this means 1 syscall instead of 2 (File.mtime, File.size).
164 #makes a noticeable difference on nfs.
166 # use 7 digits for the size. why 7? seems nice.
167 sprintf("%d%07d", stat.mtime, stat.size % 10000000).to_i
171 fn = @ids_to_fns[id] or raise OutOfSyncSourceError, "No such id: #{id.inspect}."
173 File.open(fn) { |f| yield f }
174 rescue SystemCallError, IOError => e
175 raise FatalSourceError, "Problem reading file for id #{id.inspect}: #{fn.inspect}: #{e.message}."
180 fn = File.basename @ids_to_fns[msg]
181 fn =~ %r{^([^:,]+):([12]),([DFPRST]*)$}
182 [($1 || fn), ($2 || "2"), ($3 || "")]
185 ## not thread-safe on msg
186 def maildir_mark_file msg, flag
187 orig_path = @ids_to_fns[msg]
188 orig_base, orig_fn = File.split(orig_path)
189 new_base = orig_base.slice(0..-4) + 'cur'
190 tmp_base = orig_base.slice(0..-4) + 'tmp'
191 md_base, md_ver, md_flags = maildir_data msg
192 md_flags += flag; md_flags = md_flags.split(//).sort.join.squeeze
193 new_path = File.join new_base, "#{md_base}:#{md_ver},#{md_flags}"
194 tmp_path = File.join tmp_base, "#{md_base}:#{md_ver},#{md_flags}"
195 File.link orig_path, tmp_path
196 File.unlink orig_path
197 File.link tmp_path, new_path
199 @ids_to_fns[msg] = new_path