[前][次][番号順一覧][スレッド一覧]

ruby-changes:12552

From: shugo <ko1@a...>
Date: Fri, 24 Jul 2009 00:25:37 +0900 (JST)
Subject: [ruby-changes:12552] Ruby:r24259 (trunk): * lib/net/imap.rb (resp_text_code): accepts response codes without

shugo	2009-07-24 00:20:47 +0900 (Fri, 24 Jul 2009)

  New Revision: 24259

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24259

  Log:
    * lib/net/imap.rb (resp_text_code): accepts response codes without
      text.  [ruby-core:24194]
    * lib/net/imap.rb (idle, idle_done): new methods for the IMAP4 IDLE
      command (RFC 2177).  Thanks, Eric Hodel.
    * lib/net/imap.rb (format_date, format_datetime): new method to
      format IMAP-style date/datetime. Thanks, Eric Hodel.

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/imap.rb
    trunk/test/net/imap/test_imap.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24258)
+++ ChangeLog	(revision 24259)
@@ -1,3 +1,14 @@
+Fri Jul 24 00:13:41 2009  Shugo Maeda  <shugo@r...>
+
+	* lib/net/imap.rb (resp_text_code): accepts response codes without
+	  text.  [ruby-core:24194]
+
+	* lib/net/imap.rb (idle, idle_done): new methods for the IMAP4 IDLE
+	  command (RFC 2177).  Thanks, Eric Hodel.
+
+	* lib/net/imap.rb (format_date, format_datetime): new method to
+	  format IMAP-style date/datetime. Thanks, Eric Hodel.
+
 Thu Jul 23 17:31:02 2009  Keiju Ishitsuka  <keiju@r...>
 
 	* lib/irb/ruby-lex.rb: make irb be able to parse
Index: lib/net/imap.rb
===================================================================
--- lib/net/imap.rb	(revision 24258)
+++ lib/net/imap.rb	(revision 24259)
@@ -838,6 +838,41 @@
       return thread_internal("UID THREAD", algorithm, search_keys, charset)
     end
 
+    # Sends an IDLE command that waits for notifications of new or expunged
+    # messages.  Yields responses from the server during the IDLE.
+    #
+    # Use #idle_done() to leave IDLE.
+    def idle(&response_handler)
+      raise LocalJumpError, "no block given" unless response_handler
+
+      response = nil
+
+      synchronize do
+        tag = Thread.current[:net_imap_tag] = generate_tag
+        put_string("#{tag} IDLE#{CRLF}")
+
+        add_response_handler(response_handler)
+        @idle_done_cond = new_cond
+        @idle_done_cond.wait
+        @idle_done_cond = nil
+        remove_response_handler(response_handler)
+        put_string("DONE#{CRLF}")
+        response = get_tagged_response(tag, "IDLE")
+      end
+
+      return response
+    end
+
+    # Leaves IDLE.
+    def idle_done
+      if @idle_done_cond.nil?
+        raise Net::IMAP::Error, "not during IDLE"
+      end
+      synchronize do
+        @idle_done_cond.signal
+      end
+    end
+
     # Decode a string from modified UTF-7 format to UTF-8.
     #
     # UTF-7 is a 7-bit encoding of Unicode [UTF7].  IMAP uses a
@@ -873,6 +908,16 @@
       }.force_encoding("ASCII-8BIT")
     end
 
+    # Formats +time+ as an IMAP-style date.
+    def self.format_date(time)
+      return time.strftime('%d-%b-%Y')
+    end
+
+    # Formats +time+ as an IMAP-style date-time.
+    def self.format_datetime(time)
+      return time.strftime('%d-%b-%Y %H:%M %z')
+    end
+
     private
 
     CRLF = "\r\n"      # :nodoc:
@@ -2776,11 +2821,16 @@
           match(T_SPACE)
           result = ResponseCode.new(name, number)
         else
-          match(T_SPACE)
-          @lex_state = EXPR_CTEXT
-          token = match(T_TEXT)
-          @lex_state = EXPR_BEG
-          result = ResponseCode.new(name, token.value)
+          token = lookahead
+          if token.symbol == T_SPACE
+            shift_token
+            @lex_state = EXPR_CTEXT
+            token = match(T_TEXT)
+            @lex_state = EXPR_BEG
+            result = ResponseCode.new(name, token.value)
+          else
+            result = ResponseCode.new(name, nil)
+          end
         end
         match(T_RBRA)
         @lex_state = EXPR_RTEXT
Index: test/net/imap/test_imap.rb
===================================================================
--- test/net/imap/test_imap.rb	(revision 24258)
+++ test/net/imap/test_imap.rb	(revision 24259)
@@ -18,6 +18,18 @@
     assert_equal(utf8, s)
   end
 
+  def test_format_date
+    time = Time.mktime(2009, 7, 24)
+    s = Net::IMAP.format_date(time)
+    assert_equal("24-Jul-2009", s)
+  end
+
+  def test_format_datetime
+    time = Time.mktime(2009, 7, 24, 1, 23, 45)
+    s = Net::IMAP.format_datetime(time)
+    assert_match(/\A24-Jul-2009 01:23 [+\-]\d{4}\z/, s)
+  end
+
   def test_imaps_unknown_ca
     if defined?(OpenSSL)
       assert_raise(OpenSSL::SSL::SSLError) do
@@ -112,6 +124,58 @@
     end
   end
 
+  def test_idle
+    server = TCPServer.new(0)
+    port = server.addr[1]
+    requests = []
+    Thread.start do
+      begin
+        sock = server.accept
+        begin
+          sock.print("* OK test server\r\n")
+          requests.push(sock.gets)
+          sock.print("+ idling\r\n")
+          sock.print("* 3 EXISTS\r\n")
+          sock.print("* 2 EXPUNGE\r\n")
+          requests.push(sock.gets)
+          sock.print("RUBY0001 OK IDLE terminated\r\n")
+          sock.gets
+          sock.print("* BYE terminating connection\r\n")
+          sock.print("RUBY0002 OK LOGOUT completed\r\n")
+        ensure
+          sock.close
+        end
+      rescue
+      end
+    end
+    begin
+      begin
+        imap = Net::IMAP.new("localhost", :port => port)
+        responses = []
+        imap.idle do |res|
+          responses.push(res)
+          if res.name == "EXPUNGE"
+            imap.idle_done
+          end
+        end
+        assert_equal(3, responses.length)
+        assert_instance_of(Net::IMAP::ContinuationRequest, responses[0])
+        assert_equal("EXISTS", responses[1].name)
+        assert_equal(3, responses[1].data)
+        assert_equal("EXPUNGE", responses[2].name)
+        assert_equal(2, responses[2].data)
+        assert_equal(2, requests.length)
+        assert_equal("RUBY0001 IDLE\r\n", requests[0])
+        assert_equal("DONE\r\n", requests[1])
+        imap.logout
+      ensure
+        imap.disconnect if imap
+      end
+    ensure
+      server.close
+    end
+  end
+
   private
 
   def imaps_test

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]