Module: IRCSupport::Modes

Defined in:
lib/ircsupport/modes.rb

Class Method Summary collapse

Class Method Details

.condense_modes(modes) ⇒ Strings

Condense mode string by removing duplicates.

Parameters:

  • modes (String)

    A string of modes you want condensed.

Returns:

  • (Strings)

    A condensed mode string.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ircsupport/modes.rb', line 75

def condense_modes(modes)
  action = nil
  result = ''
  modes.split(//).each do |mode|
    if mode =~ /[+-]/ and (!action or mode != action)
      result += mode
      action = mode
      next
    end
    result += mode if mode =~ /[^+-]/
  end
  result.sub!(/[+-]\z/, '')
  return result
end

.diff_modes(before, after) ⇒ String

Calculate the difference between two mode strings.

Parameters:

  • before (String)

    The “before” mode string.

  • after (String)

    The “after” mode string.

Returns:

  • (String)

    A modestring representing the difference between the two mode strings.



95
96
97
98
99
100
101
102
103
# File 'lib/ircsupport/modes.rb', line 95

def diff_modes(before, after)
  before_modes = before.split(//)
  after_modes = after.split(//)
  removed = before_modes - after_modes
  added = after_modes - before_modes
  result = removed.map { |m| '-' + m }.join
  result << added.map { |m| '+' + m }.join
  return condense_modes(result)
end

.parse_channel_modes(modeparts, opts = {}) ⇒ Array

Parse channel mode changes.

Parameters:

  • modes (Array)

    The modes you want to parse.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :chanmodes (Hash)

    The channel modes which are allowed. This is the same as the “CHANMODES” isupport option.

  • :statmodes (Hash)

    The channel modes which are allowed. This is the same as the keys of the “PREFIX” isupport option.

Returns:

  • (Array)

    Each element will be a hash with three keys: ‘:set`, a boolean indicating whether the mode is being set (instead of unset); `:mode`, the mode character; and `:argument`, the argument to the mode, if any.



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
# File 'lib/ircsupport/modes.rb', line 33

def parse_channel_modes(modeparts, opts = {})
  chanmodes = opts[:chanmodes] || {
    'A' => %w{b e I},
    'B' => %w{k},
    'C' => %w{l},
    'D' => %w{i m n p s t a q r},
  }
  statmodes = opts[:statmodes] || %w{o h v}

  mode_changes = []
  modes, *args = modeparts
  parse_modes(modes).each do |mode_change|
    set, mode = mode_change[:set], mode_change[:mode]
    case
    when chanmodes["A"].include?(mode) || chanmodes["B"].include?(mode)
      mode_changes << {
        mode: mode,
        set: set,
        argument: args.shift
      }
    when chanmodes["C"].include?(mode)
      mode_changes << {
        mode: mode,
        set: set,
        argument: args.shift.to_i
      }
    when chanmodes["D"].include?(mode)
      mode_changes << {
        mode: mode,
        set: set,
      }
    else
      raise ArgumentError, "Unknown mode: #{mode}"
    end
  end

  return mode_changes
end

.parse_modes(modes) ⇒ Array

Parse mode changes.

Parameters:

  • modes (Array)

    The modes you want to parse. A string of mode changes (e.g. ‘’-i+k+l’‘) followed by any arguments (e.g. `’secret’, 25`).

Returns:

  • (Array)

    Each element will be a hash with two keys: ‘:set`, a boolean indicating whether the mode is being set (instead of unset); and `:mode`, the mode character.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ircsupport/modes.rb', line 11

def parse_modes(modes)
  mode_changes = []
  modes.scan(/[-+]\w+/).each do |modegroup|
    set, modegroup = modegroup.split '', 2
    set = set == '+' ? true : false
    modegroup.split('').each do |mode|
      mode_changes << { set: set, mode: mode }
    end
  end
  return mode_changes
end