Module: CamperVan::CommandParser

Included in:
IRCD
Defined in:
lib/camper_van/command_parser.rb

Overview

simplistic IRC command parser

Instance Method Summary collapse

Instance Method Details

#parse(line) ⇒ Object

returns hash, e.g.

malformed # => nil
NICK joe # => # { :nick => ["joe"] }
LIST # => # { :list => [] }
PRIVMSG #foo :test # => { :privmsg => ['#foo', 'test'] }


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/camper_van/command_parser.rb', line 13

def parse(line)
  line = line.dup
  match = /^([A-Z]+)(\b|$)/.match(line)
  cmd = match && match[0]

  return nil unless cmd

  # strip off the command and any whitespace
  line.sub! /^#{cmd}\s*/, ""

  args = []
  until line.empty? do
    line =~ /^(\S+)(\s|$)/
    if $1
      if $1.start_with?(":")
        args << line[1..-1]
        break
      else
        args << $1
        line = line[$1.size..-1]
        line = line.sub(/^\s+/,"")
      end
    else
      break
    end
  end

  return {cmd.downcase.to_sym => args }
end