# Support for IMAP IDLE in net/imap

**URL:** <https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480>\
**Category:** Ruby\
**Created:** [July 3, 2009, 10:17am UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480 "2009-07-03T10:17:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishiv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@abhishiv](https://www.ruby-forum.com/u/abhishiv)\
**Post date:** [July 3, 2009, 10:17am UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480/1 "2009-07-03T10:17:46Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![abhishiv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@abhishiv](https://www.ruby-forum.com/u/abhishiv)\
**Post date:** [July 3, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480/2 "2009-07-03T17:34:18Z")

</div>

On Jul 3, 2009, at 1:16, Abhishiv S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![abhishiv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@abhishiv](https://www.ruby-forum.com/u/abhishiv)\
**Post date:** [July 3, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480/3 "2009-07-03T18:44:59Z")

</div>

On Jul 3, 2009, at 01:16, Abhishiv S. 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

---

<div class="post-metadata">

**Author:** ![abhishiv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@abhishiv](https://www.ruby-forum.com/u/abhishiv)\
**Post date:** [July 4, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480/4 "2009-07-04T18:08:00Z")

</div>

On Jul 3, 2009, at 14:19, Abhishiv S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ☹

---

<div class="post-metadata">

**Author:** ![abhishiv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@abhishiv](https://www.ruby-forum.com/u/abhishiv)\
**Post date:** [July 3, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/support-for-imap-idle-in-net-imap/171480/5 "2009-07-03T23:19:45Z")

</div>

Eric, Thanks a lot that works like a charm.

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