lib/clacks/service.rb
def imap_find(imap)
      options = Clacks.config[:find_options]
      delete_after_find = options[:delete_after_find]
      begin
        break if stopping?
        uids = imap.uid_search(options[:keys] || 'ALL')
        uids.reverse! if options[:what].to_sym == :last
        uids = uids.first(options[:count]) if options[:count].is_a?(Integer)
        uids.reverse! if (options[:what].to_sym == :last && options[:order].to_sym == :asc) ||
                         (options[:what].to_sym != :last && options[:order].to_sym == :desc)
        processed = 0
        expunge = false
        uids.each do |uid|
          break if stopping?
          source = imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822']
          mail = nil
          begin
            mail = Mail.new(source)
            mail.mark_for_delete = true if delete_after_find
            Clacks.config[:on_mail].call(mail)
          rescue StandardError => e
            Clacks.logger.error(e.message)
            Clacks.logger.error(e.backtrace)
          end
          begin
            imap.uid_copy(uid, options[:archivebox]) if options[:archivebox]
            if delete_after_find && (mail.nil? || mail.is_marked_for_delete?)
              expunge = true
              imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED])
            end
          rescue StandardError => e
            Clacks.logger.error(e.message)
          end
          processed += 1
        end
        imap.expunge if expunge
      end while uids.any? && processed == uids.length
    end
def imap_find(imap)
      options = Clacks.config[:find_options]
      delete_after_find = options[:delete_after_find]
      begin
        break if stopping?
        uids = imap.uid_search(options[:keys] || 'ALL')
        uids.reverse! if options[:what].to_sym == :last
        uids = uids.first(options[:count]) if options[:count].is_a?(Integer)
        uids.reverse! if (options[:what].to_sym == :last && options[:order].to_sym == :asc) ||
                         (options[:what].to_sym != :last && options[:order].to_sym == :desc)
        processed = 0
        expunge = false
        uids.each do |uid|
          break if stopping?
          source = imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822']
          mail = nil
          begin
            mail = Mail.new(source)
            mail.mark_for_delete = true if delete_after_find
            Clacks.config[:on_mail].call(mail)
          rescue StandardError => e
            Clacks.logger.error(e.message)
            Clacks.logger.error(e.backtrace)
          end
          begin
            imap.uid_copy(uid, options[:archivebox]) if options[:archivebox]
            if delete_after_find && (mail.nil? || mail.is_marked_for_delete?)
              expunge = true
              imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED])
            end
          rescue StandardError => e
            Clacks.logger.error(e.message)
          end
          processed += 1
        end
        imap.expunge if expunge
      end while uids.any? && processed == uids.length
    end
Keep processing emails until nothing is found anymore,
 or until a QUIT signal is received to stop the process.