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
@password = password
@uri = uri
@cur_offset = start_offset
+ @labels = (labels || []).freeze
opts = {}
opts[:username] = @username if @username
## 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
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