class Maildir < Source
SCAN_INTERVAL = 30 # seconds
- def initialize uri, last_date=nil, usual=true, archived=false, id=nil
- super
-
- @dir = URI(uri).path
+ ## remind me never to use inheritance again.
+ yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
+ def initialize uri, last_date=nil, usual=true, archived=false, id=nil, labels=[]
+ uri = Source.expand_filesystem_uri uri
+ super uri, last_date, usual, archived, id
+ uri = URI(uri)
+
+ raise ArgumentError, "not a maildir URI" unless uri.scheme == "maildir"
+ raise ArgumentError, "maildir URI cannot have a host: #{uri.host}" if uri.host
+
+ @dir = uri.path
+ @labels = (labels || []).freeze
@ids = []
@ids_to_fns = {}
- @labels = [:unread]
@last_scan = nil
@mutex = Mutex.new
end
+ def file_path; @dir end
+ def self.suggest_labels_for path; [] end
+ def is_source_for? uri; super || (URI(Source.expand_filesystem_uri(uri)) == URI(self.uri)); end
+
+ def check
+ scan_mailbox
+ start = @ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}." # couldn't find the most recent email
+ end
+
def load_header id
scan_mailbox
with_file_for(id) { |f| MBox::read_header f }
return if @last_scan && (Time.now - @last_scan) < SCAN_INTERVAL
cdir = File.join(@dir, 'cur')
- ndir = File.join(@dir, 'cur')
+ ndir = File.join(@dir, 'new')
+
+ raise FatalSourceError, "#{cdir} not a directory" unless File.directory? cdir
+ raise FatalSourceError, "#{ndir} not a directory" unless File.directory? ndir
begin
@ids, @ids_to_fns = @mutex.synchronize do
end
[ids.sort, ids_to_fns]
end
- rescue SystemCallError => e
- die "Problem scanning Maildir directories: #{e.message}."
+ rescue SystemCallError, IOError => e
+ raise FatalSourceError, "Problem scanning Maildir directories: #{e.message}."
end
@last_scan = Time.now
def each
scan_mailbox
- start = @ids.index(cur_offset || start_offset) or die "Unknown message id #{cur_offset || start_offset}.", :suggest_rebuild => true # couldn't find the most recent email
+ start = @ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}." # couldn't find the most recent email
start.upto(@ids.length - 1) do |i|
id = @ids[i]
self.cur_offset = id
- yield id, @labels.clone
+ yield id, @labels + (seen?(id) ? [] : [:unread]) + (trashed?(id) ? [:deleted] : [])
end
end
def pct_done; 100.0 * (@ids.index(cur_offset) || 0).to_f / (@ids.length - 1).to_f; end
+ def draft? msg; maildir_data(msg)[2].include? "D"; end
+ def flagged? msg; maildir_data(msg)[2].include? "F"; end
+ def passed? msg; maildir_data(msg)[2].include? "P"; end
+ def replied? msg; maildir_data(msg)[2].include? "R"; end
+ def seen? msg; maildir_data(msg)[2].include? "S"; end
+ def trashed? msg; maildir_data(msg)[2].include? "T"; end
+
+ def mark_draft msg; maildir_mark_file msg, "D" unless draft? msg; end
+ def mark_flagged msg; maildir_mark_file msg, "F" unless flagged? msg; end
+ def mark_passed msg; maildir_mark_file msg, "P" unless passed? msg; end
+ def mark_replied msg; maildir_mark_file msg, "R" unless replied? msg; end
+ def mark_seen msg; maildir_mark_file msg, "S" unless seen? msg; end
+ def mark_trashed msg; maildir_mark_file msg, "T" unless trashed? msg; end
+
private
- def die message, opts={}
- message += " It is likely that messages have been deleted from this Maildir mailbox. Please run sup-import --rebuild #{to_s} to correct this problem." if opts[:suggest_rebuild]
- self.broken_msg = message
- Redwood::log message
- BufferManager.flash "Error communicating with Maildir. See log for details." if BufferManager.instantiated?
- raise SourceError, message
- end
-
def make_id fn
# use 7 digits for the size. why 7? seems nice.
- sprintf("%d%07d", File.ctime(fn), File.size(fn)).to_i
+ sprintf("%d%07d", File.mtime(fn), File.size(fn)).to_i
end
def with_file_for id
- fn = @ids_to_fns[id] or die "No such id: #{id.inspect}.", :suggest_rebuild => true
+ fn = @ids_to_fns[id] or raise OutOfSyncSourceError, "No such id: #{id.inspect}."
begin
File.open(fn) { |f| yield f }
- rescue SystemCallError => e
- die "Problem reading file for id #{id.inspect}: #{fn.inspect}: #{e.message}."
+ rescue SystemCallError, IOError => e
+ raise FatalSourceError, "Problem reading file for id #{id.inspect}: #{fn.inspect}: #{e.message}."
end
end
-end
-Redwood::register_yaml(Maildir, %w(uri cur_offset usual archived id))
+ def maildir_data msg
+ fn = File.basename @ids_to_fns[msg]
+ fn =~ %r{^([^:,]+):([12]),([DFPRST]*)$}
+ [($1 || fn), ($2 || "2"), ($3 || "")]
+ end
+
+ ## not thread-safe on msg
+ def maildir_mark_file msg, flag
+ orig_path = @ids_to_fns[msg]
+ orig_base, orig_fn = File.split(orig_path)
+ new_base = orig_base.slice(0..-4) + 'cur'
+ tmp_base = orig_base.slice(0..-4) + 'tmp'
+ md_base, md_ver, md_flags = maildir_data msg
+ md_flags += flag; md_flags = md_flags.split(//).sort.join.squeeze
+ new_path = File.join new_base, "#{md_base}:#{md_ver},#{md_flags}"
+ tmp_path = File.join tmp_base, "#{md_base}:#{md_ver},#{md_flags}"
+ File.link orig_path, tmp_path
+ File.unlink orig_path
+ File.link tmp_path, new_path
+ File.unlink tmp_path
+ @ids_to_fns[msg] = new_path
+ end
+end
end