From 23d5ced228b2c82d174efa09353fd1d18cee52bf Mon Sep 17 00:00:00 2001 From: wmorgan Date: Tue, 26 Dec 2006 17:49:11 +0000 Subject: [PATCH] moved responsibility for labels out of source and back into subclasses. minor tweaks on imap to be a little prettier. allowed mbox::loader to take iether a filename or an IO object; the latter will be used for mbox+ssh support git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@99 5c8cc53c-5e98-4d25-b20a-d8db53a31250 --- lib/sup/imap.rb | 6 ++++-- lib/sup/mbox/loader.rb | 23 +++++++++++++++-------- lib/sup/source.rb | 1 - 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/sup/imap.rb b/lib/sup/imap.rb index c445bea..8f93458 100644 --- a/lib/sup/imap.rb +++ b/lib/sup/imap.rb @@ -9,6 +9,7 @@ class IMAP < Source def initialize uri, username, password, last_uid=nil, usual=true, archived=false, id=nil raise ArgumentError, "username and password must be specified" unless username && password + raise ArgumentError, "not an imap uri" unless uri =~ %r!imaps?://! super uri, last_uid, usual, archived, id @@ -17,8 +18,8 @@ class IMAP < Source @password = password @imap = nil @labels = [:unread] - @labels << mailbox.intern unless mailbox =~ /inbox/i || mailbox.nil? @labels << :inbox unless archived? + @labels << mailbox.intern unless mailbox =~ /inbox/i || mailbox.nil? connect end @@ -43,7 +44,7 @@ class IMAP < Source ::Thread.new do begin #raise Net::IMAP::ByeResponseError, "simulated imap failure" - @imap = Net::IMAP.new @parsed_uri.host, ssl? ? 993 : 143, ssl? + @imap = Net::IMAP.new host, ssl? ? 993 : 143, ssl? @imap.authenticate 'LOGIN', @username, @password @imap.examine mailbox Redwood::log "successfully connected to #{@parsed_uri}, mailbox #{mailbox}" @@ -58,6 +59,7 @@ class IMAP < Source end private :connect + def host; @parsed_uri.host; end def mailbox; @parsed_uri.path[1..-1] end ##XXXX TODO handle nil def ssl?; @parsed_uri.scheme == 'imaps' end diff --git a/lib/sup/mbox/loader.rb b/lib/sup/mbox/loader.rb index 5f2c088..ec7de85 100644 --- a/lib/sup/mbox/loader.rb +++ b/lib/sup/mbox/loader.rb @@ -1,4 +1,3 @@ -require 'thread' require 'rmail' module Redwood @@ -7,17 +6,25 @@ module MBox class Loader < Source attr_reader :labels - def initialize uri, start_offset=nil, usual=true, archived=false, id=nil - raise ArgumentError, "not an mbox uri" unless uri =~ %r!mbox://! + def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil super @mutex = Mutex.new - @filename = uri.sub(%r!^mbox://!, "") - @f = File.open @filename - ## 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/ + @labels << :inbox unless archived? + + case uri_or_fp + when String + raise ArgumentError, "not an mbox uri" unless uri_or_fp =~ %r!mbox://! + + fn = uri_or_fp.sub(%r!^mbox://!, "") + ## heuristic: use the filename as a label, unless the file + ## has a path that probably represents an inbox. + @labels << File.basename(fn).intern unless File.dirname(fn) =~ /\b(var|usr|spool)\b/ + @f = File.open fn + else + @f = uri_or_fp + end end def start_offset; 0; end diff --git a/lib/sup/source.rb b/lib/sup/source.rb index 4ce1f9b..7219f8a 100644 --- a/lib/sup/source.rb +++ b/lib/sup/source.rb @@ -44,7 +44,6 @@ class Source until done? n, labels = self.next raise "no message" unless n - labels += [:inbox] unless archived? yield n, labels end end -- 2.45.2