count messages in your imap mail with ruby

how?

[huned@huned tmp]$ ./mailcount huned@example.com mypassword
2

with this code:

#!/usr/bin/env ruby

require 'rubygems'
require 'optparse'
require 'net/imap'

class ImapHelper
  attr_accessor :imap

  def initialize host, user, password
    @imap = Net::IMAP.new host
    @imap.login user, password
  end

  def count
    h = @imap.status 'inbox', %w/MESSAGES/
    h['MESSAGES']
  end
end

ARGV.options do |opts|
  opts.banner = "Usage: #{$0} <user>@<host> <password>"
  opts.separator ''
  opts.separator 'Options:'
  opts.on_tail '-?', '--help',
    'Show this help message.' do puts opts; exit end
  if ARGV.empty?
    puts opts
    exit
  end
end

user, host = ARGV.shift.split '@'
password = ARGV.shift

imap = ImapHelper.new host, user, password
puts imap.count

About this entry