added imap status

This commit is contained in:
Cris Ward 2017-08-30 21:45:23 +01:00
parent 0dc0de50c2
commit d45f0b724a
2 changed files with 29 additions and 2 deletions

View file

@ -22,8 +22,8 @@ mailboxes = imap.get_mailboxes
if mailboxes.size > 0 if mailboxes.size > 0
mailbox = mailboxes[0] mailbox = mailboxes[0]
imap.select(mailbox) imap.select(mailbox)
message_count = imap.get_message_count status = imap.status(mailbox, ["MESSAGES", "UNSEEN"])
puts "There are #{message_count} message in #{mailbox}" puts "There are #{status["MESSAGES"]} message in #{mailbox} #{status["UNSEEN"]} unread."
end end
imap.close imap.close
``` ```

View file

@ -105,6 +105,33 @@ module Imap
return 0 return 0
end end
# Sends a STATUS command, and returns the status of the indicated
# `mailbox`. `attr` is a list of one or more attributes whose
# statuses are to be requested. Supported attributes include:
#
# * MESSAGES:: the number of messages in the mailbox.
# * RECENT:: the number of recent messages in the mailbox.
# * UNSEEN:: the number of unseen messages in the mailbox.
#
# The return value is a hash of attributes. For example:
# ```
# p imap.status("inbox", ["MESSAGES", "RECENT"])
# #=> {"RECENT"=>0, "MESSAGES"=>44}
#```
def status(mailbox, attr : Array(String))
param = "(#{attr.join(" ")})"
res = command("STATUS", mailbox, param)
vals = Hash(String, Int32).new
counts = res[0].match(/\(([^)]+)\)/)
if counts && counts[1]
counts[1].scan(/\w+ \d+/) do |match|
key, value = match[0].to_s.split(" ", 2)
vals[key] = value.to_i
end
end
return vals
end
private def process_mail_headers(res) private def process_mail_headers(res)
ip = nil ip = nil
from = nil from = nil