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

rubocop:disable Metrics/ClassLength

Raises:



8
9
10
11
12
# File 'lib/imapcli/command.rb', line 8

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

  @client = client
end

Class Method Details

.unknown_mailbox_prefixObject



84
85
86
# File 'lib/imapcli/command.rb', line 84

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.



17
18
19
# File 'lib/imapcli/command.rb', line 17

def check
  @client.
end

#infoObject

Collects basic information about the server.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/imapcli/command.rb', line 22

def info # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  perform do
    output = []
    output << "greeting: #{@client.greeting}"
    output << "capability: #{@client.capability.join(' ')}"
    output << "hierarchy separator: #{@client.separator}"
    if @client.supports_quota
      usage = ActiveSupport::NumberHelper.number_to_human_size(@client.quota[0])
      available = ActiveSupport::NumberHelper.number_to_human_size(@client.quota[1])
      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



39
40
41
42
43
# File 'lib/imapcli/command.rb', line 39

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.



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
76
77
78
79
80
81
82
# File 'lib/imapcli/command.rb', line 49

def stats(mailbox_names = [], options = {}) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  mailbox_names = [mailbox_names] unless mailbox_names.is_a? Array
  perform do
    # 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.positive?

    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

    output = if options[:limit]
      sorted_list(list, options).last(options[:limit].to_i)
    else
      sorted_list(list, options)
    end.map do |mailbox|
      stats_to_table(mailbox.full_name, mailbox.stats)
    end
    output << stats_to_table('Total', total_stats) if list.length > 1
    output
  end
end