]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/loader.rb
labels now fully determined by sources.yaml, and lots of improvements to sup-config
[sup] / lib / sup / mbox / loader.rb
index 05ddbab09f6ae90153274add4c0b9083a2c78781..49e97036ef8e328c8b66ae86cd55c47068e6ed97 100644 (file)
@@ -5,27 +5,34 @@ module Redwood
 module MBox
 
 class Loader < Source
-  yaml_properties :uri, :cur_offset, :usual, :archived, :id
-  def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
-    super
+  yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
+  def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
+    super uri_or_fp, start_offset, usual, archived, id
 
     @mutex = Mutex.new
-    @labels = [:unread]
+    @labels = (labels || []).freeze
 
     case uri_or_fp
     when String
       uri = URI(uri_or_fp)
       raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
       raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
-      ## heuristic: use the filename as a label, unless the file
-      ## has a path that probably represents an inbox.
-      @labels << File.basename(uri.path).intern unless File.dirname(uri.path) =~ /\b(var|usr|spool)\b/
       @f = File.open uri.path
     else
       @f = uri_or_fp
     end
   end
 
+  def self.suggest_labels_for path
+    ## heuristic: use the filename as a label, unless the file
+    ## has a path that probably represents an inbox.
+    if File.dirname(path) =~ /\b(var|usr|spool)\b/
+      []
+    else
+      [File.basename(path).intern]
+    end
+  end
+
   def check
     if (cur_offset ||= start_offset) > end_offset
       raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
@@ -128,7 +135,7 @@ class Loader < Source
     end
 
     self.cur_offset = next_offset
-    [returned_offset, @labels.clone]
+    [returned_offset, @labels]
   end
 end