Module: IRCSupport::Masks

Defined in:
lib/ircsupport/masks.rb

Constant Summary collapse

@@mask_wildcard =
'[\x01-\xFF]{0,}'
@@mask_optional =
'[\x01-\xFF]{1,1}'

Class Method Summary collapse

Class Method Details

.matches_mask(mask, string, casemapping = :rfc1459) ⇒ Boolean

Match strings to an IRC mask.

Parameters:

  • mask (String)

    The mask to match against.

  • string (String)

    The string to match against the mask.

  • casemapping (Symbol) (defaults to: :rfc1459)

    The IRC casemapping to use in the match.

Returns:

  • (Boolean)

    Will be true of the string matches the mask.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ircsupport/masks.rb', line 17

def matches_mask(mask, string, casemapping = :rfc1459)
  if mask =~ /\$/
    raise ArgumentError, "Extended bans are not supported"
  end
  string = IRCSupport::Case.irc_upcase(string, casemapping)
  mask = Regexp.quote(irc_upcase(mask, casemapping))
  mask.gsub!('\*', @@mask_wildcard)
  mask.gsub!('\?', @@mask_optional)
  mask = Regexp.new(mask, nil, 'n')
  return true if string =~ /\A#{mask}\z/
  return false
end

.matches_mask_array(masks, strings, casemapping = :rfc1459) ⇒ Hash

Match strings to multiple IRC masks.

Parameters:

  • mask (Array)

    The masks to match against.

  • strings (Array)

    The strings to match against the masks.

  • casemapping (Symbol) (defaults to: :rfc1459)

    The IRC casemapping to use in the match.

Returns:

  • (Hash)

    Each mask that was matched will be present as a key, and the values will be arrays of the strings that matched.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ircsupport/masks.rb', line 36

def matches_mask_array(masks, strings, casemapping = :rfc1459)
  results = {}
  masks.each do |mask|
    strings.each do |string|
      if matches_mask(mask, string, casemapping)
        results[mask] ||= []
        results[mask] << string
      end
    end
  end
  return results
end

.normalize_mask(mask) ⇒ String

Normalize (expand) an IRC mask.

Parameters:

  • mask (String)

    A partial mask (e.g. ‘foo*’).

Returns:

  • (String)

    A normalized mask (e.g. ‘foo*!*@*).



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ircsupport/masks.rb', line 52

def normalize_mask(mask)
  mask = mask.dup
  mask.gsub!(/\*{2,}/, '*')
  parts = []
  remainder = nil

  if mask !~ /!/ && mask =~ /@/
    remainder = mask
    parts[0] = '*'
  else
    parts[0], remainder = mask.split(/!/, 2)
  end

  if remainder
    remainder.gsub!(/!/, '')
    parts[1..2] = remainder.split(/@/, 2)
  end
  parts[2].gsub!(/@/, '') if parts[2]

  (1..2).each { |i| parts[i] ||= '*' }
  return parts[0] + "!" + parts[1] + "@" + parts[2]
end