]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox/ssh-loader.rb
global search and replace: raw_full_message -> raw_message
[sup] / lib / sup / mbox / ssh-loader.rb
index 4ea10d890d58b942319dae12b7a1e9d15b0cc9ef..e422a481b5c3a5639ac023d0aa264b30509f465d 100644 (file)
@@ -3,14 +3,13 @@ require 'net/ssh'
 module Redwood
 module MBox
 
-## this is slightly complicated because SSHFile (and thus @f or
-## @loader) can throw a variety of exceptions, and we need to catch
-## those, reraise them as SourceErrors, and set ourselves as broken.
-
 class SSHLoader < Source
   attr_accessor :username, :password
 
-  def initialize uri, username=nil, password=nil, start_offset=nil, usual=true, archived=false, id=nil
+  yaml_properties :uri, :username, :password, :cur_offset, :usual, 
+                  :archived, :id, :labels
+
+  def initialize uri, username=nil, password=nil, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
     raise ArgumentError, "not an mbox+ssh uri: #{uri.inspect}" unless uri =~ %r!^mbox\+ssh://!
 
     super uri, start_offset, usual, archived, id
@@ -20,6 +19,7 @@ class SSHLoader < Source
     @password = password
     @uri = uri
     @cur_offset = start_offset
+    @labels = (labels || []).freeze
 
     opts = {}
     opts[:username] = @username if @username
@@ -30,16 +30,15 @@ class SSHLoader < Source
     
     ## heuristic: use the filename as a label, unless the file
     ## has a path that probably represents an inbox.
-    @labels = [:unread]
-    @labels << File.basename(filename).intern unless File.dirname(filename) =~ /\b(var|usr|spool)\b/
   end
 
+  def self.suggest_labels_for path; Loader.suggest_labels_for(path) end
+
   def connect; safely { @f.connect }; end
   def host; @parsed_uri.host; end
   def filename; @parsed_uri.path[1..-1] end
 
   def next
-    return if broken?
     safely do
       offset, labels = @loader.next
       self.cur_offset = @loader.cur_offset # superclass keeps @cur_offset which is used by yaml
@@ -60,20 +59,16 @@ class SSHLoader < Source
   def safely
     begin
       yield
-    rescue Net::SSH::Exception, SocketError, SSHFileError, SystemCallError => e
+    rescue Net::SSH::Exception, SocketError, SSHFileError, SystemCallError, IOError => e
       m = "error communicating with SSH server #{host} (#{e.class.name}): #{e.message}"
-      Redwood::log m
-      self.broken_msg = @loader.broken_msg = m
-      raise SourceError, m
+      raise FatalSourceError, m
     end
   end
 
-  [:start_offset, :load_header, :load_message, :raw_header, :raw_full_message].each do |meth|
+  [:start_offset, :load_header, :load_message, :raw_header, :raw_message].each do |meth|
     define_method(meth) { |*a| safely { @loader.send meth, *a } }
   end
 end
 
-Redwood::register_yaml(SSHLoader, %w(uri username password cur_offset usual archived id))
-
 end
 end