Module: IRC::RFC2812::Helpers

Defined in:
lib/irc/rfc2812/helpers.rb

Class Method Summary collapse

Class Method Details

.parse(message) ⇒ Object

Public: Parses an IRC message.

message - A message String.

Return an Array of form `[“prefix”, “command”, [“param”, “param”], “the trail”]`.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/irc/rfc2812/helpers.rb', line 36

def parse(message)
  if message.start_with? ":"
    prefix, command, parameters = message[1..-1].strip.split(" ", 3)
  else
    command, parameters = message.strip.split(" ", 2)
  end

  if parameters.nil?
    parameters = []
  elsif parameters.start_with? ":"
    trailing   = parameters[1..-1]
    parameters = []
  else
    parameters, trailing = parameters.split(" :", 2)
    parameters = parameters.split(" ")
  end

  return [prefix, command, parameters.compact, trailing]
end

.parse_prefix(prefix) ⇒ Object

Public: Parses an IRC prefix.

prefix - A prefix String.

Returns an Array of form [“nick”, “user”, “host.com”].



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/irc/rfc2812/helpers.rb', line 61

def parse_prefix(prefix)
  nick_and_user, host = String(prefix).split "@", 2

  if host.nil?
    if nick_and_user.include? "."
      servername = nick_and_user
    else
      nickname = nick_and_user
    end
  else
    nickname, user = nick_and_user.split "!", 2
  end

  [nickname, user, host || servername]
end

.valid?(message) ⇒ Boolean

Public: Checks if a String is a valid IRC message according to Section 2.3 of RFC 2812. Note that not all constraints are checked, yet.

message - A message String.

Returns a Boolean (true or false).

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/irc/rfc2812/helpers.rb', line 83

def valid?(message)
  prefix, command, parameters = parse message
  
  # Maximum of fifteen command parameters
  return false if parameters.length > 15

  # There MUST be NO whitespace between the colon and the prefix.
  return false if message =~ /^:\s/

  # The command MUST either be a valid IRC command or a three digit number 
  # represented in ASCII text.
  return false unless command =~ /^([A-Z]+|\d{3})$/

  # IRC messages are always lines of characters terminated with a CR-LF 
  # (Carriage Return - Line Feed)
  return false unless message.end_with? "\r\n"
  
  # Messages SHALL NOT exceed 512 characters in length, counting all 
  # characters including the trailing CR-LF.
  return false if message.length > 512
  
  # NUL (%x00) is not allowed within messages.
  return false if message.include? "\x00"

  # Hostnames can not be longer than 63 characters.
  hostname = prefix.split("@")[1] || (prefix if prefix.include? ".") || ""
  return false if hostname.length > 63

  return true
end

.valid_prefix?(prefix) ⇒ Boolean

Public: Checks if a String is a valid IRC prefix according to Section 2.3 of RFC 2812. Note that not all constraints are checked, yet.

prefix - A prefix String.

Returns a Boolean (true or false).

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/irc/rfc2812/helpers.rb', line 120

def valid_prefix?(prefix)
  return false if prefix.count("@") > 1
  return false if prefix.count("!") > 1

  nick, user, host = parse_prefix prefix

  # Hostnames can not be longer than 63 characters.
  return false if String(host).length > 63

  # User can not contain ["\x00", "\r", "\n", " ", "@"]
  return false if ["\x00", "\r", "\n", " ", "@"].any? do |char|
    String(user).include? char
  end

  # Nick can not contain characters other than A-Z, a-z, 0-9, "[", "]", 
  # "\", "`", "_", "^", "{", "|" or "}"
  return false if nick && nick !~ /^[\w\[\]\\`\^\{\|\}]+$/

  return true
end