]> git.cworth.org Git - sup/blobdiff - lib/sup/maildir.rb
Merge branches 'prev-next-improv', 'fix-warnings', 'mime-view', 'charset', 'join...
[sup] / lib / sup / maildir.rb
index cc487d75b1b15632c768d957ecc8bfd430441694..7b85383a8b15d694d1651195b81e473435fe7282 100644 (file)
@@ -11,13 +11,15 @@ module Redwood
 class Maildir < Source
   SCAN_INTERVAL = 30 # seconds
 
+  ## 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=[]
     super uri, last_date, usual, archived, id
-    uri = URI(uri)
+    uri = URI(Source.expand_filesystem_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
+    raise ArgumentError, "mbox URI must have a path component" unless uri.path
 
     @dir = uri.path
     @labels = (labels || []).freeze
@@ -29,11 +31,23 @@ class Maildir < Source
 
   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
+    return unless start_offset
+
     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 each_raw_message_line id
+    scan_mailbox
+    with_file_for(id) do |f|
+      until f.eof?
+        yield f.gets
+      end
+    end
+  end
 
   def load_header id
     scan_mailbox
@@ -56,7 +70,7 @@ class Maildir < Source
     ret
   end
 
-  def raw_full_message id
+  def raw_message id
     scan_mailbox
     with_file_for(id) { |f| f.readlines.join }
   end
@@ -89,12 +103,14 @@ class Maildir < Source
 
   def each
     scan_mailbox
+    return unless start_offset
+
     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 + (seen?(id) ? [] : [:unread]) + (trashed?(id) ? [:deleted] : [])
+      yield id, @labels + (seen?(id) ? [] : [:unread]) + (trashed?(id) ? [:deleted] : []) + (flagged?(id) ? [:starred] : [])
     end
   end
 
@@ -128,7 +144,7 @@ private
 
   def make_id fn
     # use 7 digits for the size. why 7? seems nice.
-    sprintf("%d%07d", File.mtime(fn), File.size(fn)).to_i
+    sprintf("%d%07d", File.mtime(fn), File.size(fn) % 10000000).to_i
   end
 
   def with_file_for id