Support for IMAP IDLE in net/imap

Discussion in 'Ruby' started by Abhishiv Saxena, Jul 3, 2009.

  1. [Note: parts of this message were removed to make it a legal post.]

    Ok, I have been suck on it for hours. I thought net/imap.rb with ruby 1.9
    supported the idle command, but not yet.

    Can anyone help me in implementing that? From
    here<http://www.ruby-forum.com/topic/50828>,
    I though this would work:

    class Net::IMAP
    def idle
    cmd = "IDLE"
    synchronize do
    tag = generate_tag
    put_string(tag + " " + cmd)
    put_string(CRLF)
    end
    end

    def done
    cmd = "DONE"
    synchronize do
    put_string(cmd)
    put_string(CRLF)
    end
    end
    end

    But, when i issue imap.idle, it return true, but i have to press return
    twice to get the server response. And there's no notification for new
    messages.

    ?> imap.idle
    C: RUBY0005 IDLE
    => true
    >>

    ?>
    ?> S: + idling
     
    Abhishiv Saxena, Jul 3, 2009
    #1
    1. Advertising

  2. Abhishiv Saxena

    Eric Hodel Guest

    On Jul 3, 2009, at 1:16, Abhishiv Saxena <> wrote:

    > Ok, I have been suck on it for hours. I thought net/imap.rb with
    > ruby 1.9
    > supported the idle command, but not yet.
    >
    > Can anyone help me in implementing that?


    I have a working IDLE method, but it's not handy. Ill post it in a bit.
     
    Eric Hodel, Jul 3, 2009
    #2
    1. Advertising

  3. Abhishiv Saxena

    Eric Hodel Guest

    On Jul 3, 2009, at 01:16, Abhishiv Saxena wrote:

    > Ok, I have been suck on it for hours. I thought net/imap.rb with
    > ruby 1.9
    > supported the idle command, but not yet.
    >
    > Can anyone help me in implementing that?


    NOTE: There's no need to post to the mailing list and the forum, they
    both come to the same place.

    Try this:

    require 'net/imap'

    class Net::IMAP

    ##
    # Sends an IDLE command that waits for notifications of new or
    expunged
    # messages. Yields responses from the server during the IDLE.
    #
    # Use +break+ in the response handler 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

    begin
    response = get_tagged_response tag
    rescue LocalJumpError # can't break cross-threads or something
    ensure
    unless response then
    put_string "DONE#{CRLF}"
    response = get_tagged_response tag
    end

    remove_response_handler response_handler
    end
    end

    response
    end

    end
     
    Eric Hodel, Jul 3, 2009
    #3
  4. Eric, Thanks a lot that works like a charm.

    imap.idle { |resp|
    puts "Mailbox now has #{resp.data} messages"
    }

    --
    Posted via http://www.ruby-forum.com/.
     
    Abhishiv Saxena, Jul 3, 2009
    #4
  5. Abhishiv Saxena

    Eric Hodel Guest

    On Jul 3, 2009, at 14:19, Abhishiv Saxena <> wrote:

    > Eric, Thanks a lot that works like a charm.
    >
    > imap.idle { |resp|
    > puts "Mailbox now has #{resp.data} messages"
    > }


    Careful, you'll get both EXIST and EXPUNGE responses, so be sure to
    subtract when appropriate. Also note that EXPUNGE shifts all your
    recorded UIDs down by one. See RFC 3501 for all the crazy details :(
     
    Eric Hodel, Jul 4, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Payton Swick

    net/imap idle

    Payton Swick, Jan 5, 2006, in forum: Ruby
    Replies:
    0
    Views:
    202
    Payton Swick
    Jan 5, 2006
  2. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    178
    Henrik Ormåsen
    Aug 19, 2006
  3. Jon Fi
    Replies:
    4
    Views:
    548
    Kashia Buch
    Oct 21, 2006
  4. jasonnaylor
    Replies:
    1
    Views:
    257
    jasonnaylor
    Apr 16, 2008
  5. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    0
    Views:
    235
    Abhishiv Saxena
    Jul 3, 2009
Loading...

Share This Page