]> git.cworth.org Git - sup/commitdiff
moved responsibility for labels out of source and back into subclasses.
authorwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Tue, 26 Dec 2006 17:49:11 +0000 (17:49 +0000)
committerwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Tue, 26 Dec 2006 17:49:11 +0000 (17:49 +0000)
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
lib/sup/mbox/loader.rb
lib/sup/source.rb

index c445beaf723bc2ee64b8fdb84587c38f814a03bd..8f93458a04bab9dbfb6bd5614f5a7b3ac6f28c19 100644 (file)
@@ -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
 
index 5f2c088c7691a55a52cd0a503d55a8d6c2194efa..ec7de85258618745fe54e622a6a592bb330e9aa8 100644 (file)
@@ -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
index 4ce1f9b9782988dfcf29d885b2bfd97ad74a0db5..7219f8ae7502ecbcd3b7e31042016a2efab6fab0 100644 (file)
@@ -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