Class: Imapcli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/imapcli/command.rb

Overview

Provides entry points for Imapcli.

Most of the methods in this class return

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Command

Returns a new instance of Command.

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/imapcli/command.rb', line 6

def initialize(client)
  raise ArgumentError, 'Imapcli::Client is required' unless client && client.is_a?(Imapcli::Client)
  @client = client
end

Class Method Details

.unknown_mailbox_prefixObject



77
78
79
# File 'lib/imapcli/command.rb', line 77

def self.unknown_mailbox_prefix
  '!!! '
end

Instance Method Details

#checkObject

Checks if the server accepts the login with the given credentials.

Returns true if successful, false if not.



14
15
16
# File 'lib/imapcli/command.rb', line 14

def check
  @client.
end

#infoObject

Collects basic information about the server.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/imapcli/command.rb', line 19

def info
  perform do
    output = []
    output << "greeting: #{@client.greeting}"
    output << "capability: #{@client.capability.join(' ')}"
    output << "hierarchy separator: #{@client.separator}"
    if @client.supports_quota
      usage = Filesize.from(@client.quota[0] + ' kB').pretty
      available = Filesize.from(@client.quota[1] + ' kB').pretty
      output << "quota: #{usage} used, #{available} available (#{@client.quota[2].round(1)}%)"
    else
      output << "quota: IMAP QUOTA extension not supported by this server"
    end
  end
end

#listObject

Lists all mailboxes



36
37
38
39
40
# File 'lib/imapcli/command.rb', line 36

def list
  perform do
    traverse_mailbox_tree(@client.mailbox_root)
  end
end

#stats(mailbox_names = [], options = {}) ⇒ Object

Collects statistics about mailboxes.

If a block is given, it is called with the current mailbox count and the total mailbox count so that current progress can be computed.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/imapcli/command.rb', line 46

def stats(mailbox_names = [], options = {})
  mailbox_names = [mailbox_names] unless mailbox_names.is_a? Array
  perform do
    output = []
    # Map the command line arguments to Imapcli::Mailbox objects
    mailboxes = find_mailboxes(mailbox_names)
    list = mailboxes.inject([]) do |ary, mailbox|
      ary + mailbox.to_list(determine_max_level(mailbox, options))
    end
    raise 'mailbox not found' unless list.count > 0
    current_count = 0
    yield list.length if block_given?
    total_stats = Stats.new
    list.each do |mailbox|
      # Since we are working on a flat list of mailboxes, set the maximum
      # level to 0 when collecting stats.
      mailbox.collect_stats(@client, 0) do |stats|
        total_stats.add(stats)
        current_count += 1
        yield current_count if block_given?
      end
    end
    sorted_list(list, options).each do |mailbox|
      output << stats_to_table(mailbox.full_name, mailbox.stats)
    end
    # output << Array.new(8, '======')
    output << stats_to_table('Total', total_stats) if list.length > 1
    output
  end
end