]> git.lizzy.rs Git - luairc.git/blob - src/irc/message.lua
27698d802ca39e487d7d6b6e747f2ae31695ea6c
[luairc.git] / src / irc / message.lua
1 -- initialization {{{
2 local base =      _G
3 local constants = require 'irc.constants'
4 local ctcp =      require 'irc.ctcp'
5 local irc_debug = require 'irc.debug'
6 local misc =      require 'irc.misc'
7 local socket =    require 'socket'
8 local string =    require 'string'
9 local table =     require 'table'
10 -- }}}
11
12 module 'irc.message'
13
14 -- local functions {{{
15 -- parse() - parse a server command {{{
16 function parse(str)
17     -- low-level ctcp quoting {{{
18     str = ctcp.low_dequote(str)
19     -- }}}
20     -- parse the from field, if it exists (leading :) {{{
21     local from = ""
22     if str:sub(1, 1) == ":" then
23         local e
24         e, from = socket.skip(1, str:find("^:([^ ]*) "))
25         str = str:sub(e + 1)
26     end
27     -- }}}
28     -- get the command name or numerical reply value {{{
29     local command, argstr = socket.skip(2, str:find("^([^ ]*) ?(.*)"))
30     local reply = false
31     if command:find("^%d%d%d$") then
32         reply = true
33         if constants.replies[base.tonumber(command)] then
34             command = constants.replies[base.tonumber(command)]
35         else
36             irc_debug.warn("Unknown server reply: " .. command)
37         end
38     end
39     -- }}}
40     -- get the args {{{
41     local args = misc.split(argstr, " ", ":")
42     -- the first arg in a reply is always your nick
43     if reply then table.remove(args, 1) end
44     -- }}}
45     -- return the parsed message {{{
46     return {from = from, command = command, args = args}
47     -- }}}
48 end
49 -- }}}
50 -- }}}