Module: Cinch::ModeParser Private

Defined in:
lib/cinch/mode_parser.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0

Defined Under Namespace

Classes: EmptySequenceError, MalformedError, NotEnoughParametersError, TooManyParametersError

Constant Summary collapse

ERR_EMPTY_STRING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0

"Empty mode string"

Class Method Summary collapse

Class Method Details

.parse_modes(modes, params, param_modes = {}) ⇒ (Array<(Symbol<:add, :remove>, String<char>, String<param>), foo)]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns (Array<(Symbol<:add, :remove>, String<char>, String<param>), foo)].

Parameters:

  • modes (String)

    The mode string as sent by the server

  • params (Array<String>)

    Parameters belonging to the modes

  • param_modes (Hash{:add, :remove => Array<String>}) (defaults to: {})

    A mapping describing which modes require parameters

Returns:

  • ((Array<(Symbol<:add, :remove>, String<char>, String<param>), foo)])

    (Array<(Symbol<:add, :remove>, String<char>, String<param>), foo)]

Since:

  • 1.1.0



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
76
# File 'lib/cinch/mode_parser.rb', line 20

def self.parse_modes(modes, params, param_modes = {})
  if modes.size == 0
    return nil, ERR_EMPTY_STRING
    # raise Exceptions::InvalidModeString, 'Empty mode string'
  end

  if !/[+-]/.match?(modes[0])
    return nil, MalformedError.new(modes)
    # raise Exceptions::InvalidModeString, "Malformed modes string: %s" % modes
  end

  changes = []

  direction = nil
  count = -1

  modes.each_char do |ch|
    if /[+-]/.match?(ch)
      if count == 0
        return changes, EmptySequenceError.new(modes)
        # raise Exceptions::InvalidModeString, 'Empty mode sequence: %s' % modes
      end

      direction = case ch
      when "+"
        :add
      when "-"
        :remove
      end
      count = 0
    else
      param = nil
      if param_modes.has_key?(direction) && param_modes[direction].include?(ch)
        if params.size > 0
          param = params.shift
        else
          return changes, NotEnoughParametersError.new(ch)
          # raise Exceptions::InvalidModeString, 'Not enough parameters: %s' % ch.inspect
        end
      end
      changes << [direction, ch, param]
      count += 1
    end
  end

  if params.size > 0
    return changes, TooManyParametersError.new(modes, params)
    # raise Exceptions::InvalidModeString, 'Too many parameters: %s %s' % [modes, params]
  end

  if count == 0
    return changes, EmptySequenceError.new(modes)
    # raise Exceptions::InvalidModeString, 'Empty mode sequence: %s' % modes
  end

  [changes, nil]
end