def find(options={}, &block)
options = validate_options(options)
start do |imap|
imap.select(options[:mailbox])
message_ids = imap.uid_search(options[:keys])
message_ids.reverse! if options[:what].to_sym == :last
message_ids = message_ids.first(options[:count]) if options[:count].is_a?(Integer)
message_ids.reverse! if (options[:what].to_sym == :last && options[:order].to_sym == :asc) ||
(options[:what].to_sym != :last && options[:order].to_sym == :desc)
if block_given?
message_ids.each do |message_id|
fetchdata = imap.uid_fetch(message_id, ['RFC822'])[0]
new_message = Mail.new(fetchdata.attr['RFC822'])
new_message.mark_for_delete = true if options[:delete_after_find]
yield new_message
imap.uid_store(message_id, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find] && new_message.is_marked_for_delete?
end
imap.expunge if options[:delete_after_find]
else
emails = []
message_ids.each do |message_id|
fetchdata = imap.uid_fetch(message_id, ['RFC822'])[0]
emails << Mail.new(fetchdata.attr['RFC822'])
imap.uid_store(message_id, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find]
end
imap.expunge if options[:delete_after_find]
emails.size == 1 && options[:count] == 1 ? emails.first : emails
end
end
end