2 -- Implementation of IRC server message parsing
5 local constants = require 'irc.constants'
6 local ctcp = require 'irc.ctcp'
7 local irc_debug = require 'irc.debug'
8 local misc = require 'irc.misc'
9 local socket = require 'socket'
10 local string = require 'string'
11 local table = require 'table'
15 -- This module contains parsing functions for IRC server messages.
18 -- internal functions {{{
21 -- Parse a server command.
22 -- @param str Command to parse
23 -- @return Table containing the parsed message. It contains:
25 -- <li><i>from:</i> The source of this message, in full usermask
26 -- form (nick!user@host) for messages originating
27 -- from users, and as a hostname for messages from
29 -- <li><i>command:</i> The command sent, in name form if possible,
30 -- otherwise as a numeric code</li>
31 -- <li><i>args:</i> Array of strings corresponding to the arguments
32 -- to the received command</li>
36 -- low-level ctcp quoting {{{
37 str = ctcp._low_dequote(str)
39 -- parse the from field, if it exists (leading :) {{{
41 if str:sub(1, 1) == ":" then
43 e, from = socket.skip(1, str:find("^:([^ ]*) "))
47 -- get the command name or numerical reply value {{{
48 local command, argstr = socket.skip(2, str:find("^([^ ]*) ?(.*)"))
50 if command:find("^%d%d%d$") then
52 if constants.replies[base.tonumber(command)] then
53 command = constants.replies[base.tonumber(command)]
55 irc_debug._warn("Unknown server reply: " .. command)
60 local args = misc._split(argstr, " ", ":")
61 -- the first arg in a reply is always your nick
62 if reply then table.remove(args, 1) end
64 -- return the parsed message {{{
65 return {from = from, command = command, args = args}