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