2 -- Implementation of IRC server message parsing
4 local constants = libs.constants
6 local irc_debug = libs.debug
8 local socket = libs.socket
12 -- This module contains parsing functions for IRC server messages.
15 -- internal functions {{{
18 -- Parse a server command.
19 -- @param str Command to parse
20 -- @return Table containing the parsed message. It contains:
22 -- <li><i>from:</i> The source of this message, in full usermask
23 -- form (nick!user@host) for messages originating
24 -- from users, and as a hostname for messages from
26 -- <li><i>command:</i> The command sent, in name form if possible,
27 -- otherwise as a numeric code</li>
28 -- <li><i>args:</i> Array of strings corresponding to the arguments
29 -- to the received command</li>
32 function message._parse(str)
33 -- low-level ctcp quoting {{{
34 str = ctcp._low_dequote(str)
36 -- parse the from field, if it exists (leading :) {{{
38 if str:sub(1, 1) == ":" then
40 e, from = socket.skip(1, str:find("^:([^ ]*) "))
44 -- get the command name or numerical reply value {{{
45 local command, argstr = socket.skip(2, str:find("^([^ ]*) ?(.*)"))
47 if command:find("^%d%d%d$") then
49 if constants.replies[tonumber(command)] then
50 command = constants.replies[tonumber(command)]
52 irc_debug._warn("Unknown server reply: " .. command)
57 local args = misc._split(argstr, " ", ":")
58 -- the first arg in a reply is always your nick
59 if reply then table.remove(args, 1) end
61 -- return the parsed message {{{
62 return {from = from, command = command, args = args}