支持ruby中的IMAP IDLE [英] Support for IMAP IDLE in ruby

查看:68
本文介绍了支持ruby中的IMAP IDLE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经吸了几个小时了.我以为使用ruby 1.9的net/imap.rb支持idle命令,但还不支持.

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, 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

但是imap.idle只是返回nil.

But imap.idle with that just return nil.

推荐答案

我遇到了这个老问题,想自己解决.原来的提问者已经消失了-哦.

I came across this old question and wanted to solve it myself. The original asker has disappeared - oh well.

这是让IMAP在Ruby上空闲的方式(这很酷).这使用原始问题中带引号的块,以及文档

Here's how you get IMAP idle working on Ruby (this is super cool). This uses the quoted block in the original question, and the documentation here.

imap = Net::IMAP.new SERVER, :ssl => true
imap.login USERNAME, PW
imap.select 'INBOX'

imap.add_response_handler do |resp|
  # modify this to do something more interesting.
  # called every time a response arrives from the server.
  if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
    puts "Mailbox now has #{resp.data} messages"
  end
end

imap.idle  # necessary to tell the server to start forwarding requests.

这篇关于支持ruby中的IMAP IDLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆