]> git.lizzy.rs Git - luairc.git/blob - src/irc/message.lua
Port to Lua 5.3
[luairc.git] / src / irc / message.lua
1 ---
2 -- Implementation of IRC server message parsing
3 -- initialization {{{
4 local constants = libs.constants
5 local ctcp =      libs.ctcp
6 local irc_debug = libs.debug
7 local misc =      libs.misc
8 local socket =    libs.socket
9 -- }}}
10
11 ---
12 -- This module contains parsing functions for IRC server messages.
13 local message = {}
14
15 -- internal functions {{{
16 -- _parse {{{
17 --
18 -- Parse a server command.
19 -- @param str Command to parse
20 -- @return Table containing the parsed message. It contains:
21 --         <ul>
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
25 --                             servers</li>
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>
30 --
31 --         </ul>
32 function message._parse(str)
33     -- low-level ctcp quoting {{{
34     str = ctcp._low_dequote(str)
35     -- }}}
36     -- parse the from field, if it exists (leading :) {{{
37     local from = ""
38     if str:sub(1, 1) == ":" then
39         local e
40         e, from = socket.skip(1, str:find("^:([^ ]*) "))
41         str = str:sub(e + 1)
42     end
43     -- }}}
44     -- get the command name or numerical reply value {{{
45     local command, argstr = socket.skip(2, str:find("^([^ ]*) ?(.*)"))
46     local reply = false
47     if command:find("^%d%d%d$") then
48         reply = true
49         if constants.replies[tonumber(command)] then
50             command = constants.replies[tonumber(command)]
51         else
52             irc_debug._warn("Unknown server reply: " .. command)
53         end
54     end
55     -- }}}
56     -- get the args {{{
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
60     -- }}}
61     -- return the parsed message {{{
62     return {from = from, command = command, args = args}
63     -- }}}
64 end
65 -- }}}
66 -- }}}
67
68 return message