Class: Net::IRC::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/net/irc.rb

Direct Known Subclasses

Join, Mode, Nick, Notice, Part, Pass, Ping, Pong, Privmsg, Quit, Reply, User

Defined Under Namespace

Classes: Prefix

Constant Summary collapse

COMMAND_MAPS =
%w(rfc1459 rfc2812 isupport hybrid ircu hyperion)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
# File 'lib/net/irc.rb', line 152

def initialize(*args)
  raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" if args.size < 2

# puts ">>>>> args=#{args.inspect}"

  @prefix, @command, *parameters = args
# puts ">>>>> @prefix=#{@prefix.inspect}, command=#{@command.inspect}, parameters=#{parameters.inspect}"
  @parameters = Array(parameters)
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



148
149
150
# File 'lib/net/irc.rb', line 148

def command
  @command
end

#parametersObject

Returns the value of attribute parameters.



148
149
150
# File 'lib/net/irc.rb', line 148

def parameters
  @parameters
end

#prefixObject

Returns the value of attribute prefix.



147
148
149
# File 'lib/net/irc.rb', line 147

def prefix
  @prefix
end

Class Method Details

.command_for_number(number) ⇒ Object



252
253
254
255
# File 'lib/net/irc.rb', line 252

def command_for_number(number)
  @command_map ||= COMMAND_MAPS.inject({}) { |merged,map| merged.merge!(YAML.load_file("#{File.dirname(__FILE__)}/#{map}.yml")) }
  @command_map[number]
end

.parse(line) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/net/irc.rb', line 225

def parse(line)
  scanner = StringScanner.new(line)
  
  prefix = scanner.scan(/:([^ ]+) /) && scanner[1]
  command = scanner.scan(/[[:alpha:]]+|\d{3}/)
  params = []
  14.times do
    break if ! scanner.scan(/ ([^ :][^ ]*)/)
    params << scanner[1]
  end
  params << scanner[1] if scanner.scan(/ :(.+)/)

  message = nil
  command_name = command.to_i > 0 ? command_for_number(command.to_i) : command

  if command_name
    message_type = "#{command_name.downcase.split('_').collect { |w| w.capitalize }.join}"
    if Net::IRC.const_defined?(message_type)
      message_type = Net::IRC.const_get(message_type)
      message = message_type.new(*params)
      message.prefix = prefix
    end
  end

  message ||= Message.new(prefix, command_name || command, *params)
end

Instance Method Details

#prefix?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/net/irc.rb', line 197

def prefix?
  @prefix
end

#to_sObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/net/irc.rb', line 201

def to_s
# puts ">>>>> prefix=#{prefix.inspect}, command=#{command.inspect}, parameters=#{parameters.inspect}"
  str = prefix ? ":#{prefix} " : ""
  str << command
  if ! parameters.empty?
    parameters[0..-2].each do |param|
      str << " #{param}"
    end
    if parameters.last =~ /^:| /
      str << " :#{parameters.last}"
    else
      str << " #{parameters.last}"
    end
  end
  str
end

#write(socket) ⇒ Object



218
219
220
221
222
# File 'lib/net/irc.rb', line 218

def write(socket)
  line = to_s
  IRC.logger.debug ">>>>> #{line.inspect}"
  socket.writeline(line)
end