]> git.lizzy.rs Git - luairc.git/blob - src/irc/ctcp.lua
punctuation
[luairc.git] / src / irc / ctcp.lua
1 -- initialization {{{
2 local base = _G
3 local table = require "table"
4 -- }}}
5
6 module "irc.ctcp"
7
8 -- public functions {{{
9 -- low_quote {{{
10 -- applies low level quoting to a string (escaping characters which
11 -- are illegal to appear in an irc packet)
12 function low_quote(str)
13     return str:gsub("[%z\n\r\020]", {["\000"] = "\0200",
14                                      ["\n"]   = "\020n",
15                                      ["\r"]   = "\020r",
16                                      ["\020"] = "\020\020"})
17 end
18 -- }}}
19
20 -- low_dequote {{{
21 -- removes low level quoting done by low_quote
22 function low_dequote(str)
23     return str:gsub("\020(.?)", function(s)
24                                     if s == "0" then return "\000" end
25                                     if s == "n" then return "\n" end
26                                     if s == "r" then return "\r" end
27                                     if s == "\020" then return "\020" end
28                                     return ""
29                                 end)
30 end
31 -- }}}
32
33 -- ctcp_quote {{{
34 -- applies ctcp quoting to a block of text which has been identified
35 -- as ctcp data (by the calling program)
36 function ctcp_quote(str)
37     local ret = str:gsub("[\001\\]", {["\001"] = "\\a",
38                                       ["\\"]   = "\\\\"})
39     return "\001" .. ret .. "\001"
40 end
41 -- }}}
42
43 -- ctcp_dequote {{{
44 -- removes ctcp quoting from a block of text which has been
45 -- identified as ctcp data (likely by ctcp_split)
46 function ctcp_dequote(str)
47     local ret = str:gsub("^\001", ""):gsub("\001$", "")
48     return ret:gsub("\\(.?)", function(s)
49                                   if s == "a" then return "\001" end
50                                   if s == "\\" then return "\\" end
51                                   return ""
52                               end)
53 end
54 -- }}}
55
56 -- ctcp_split {{{
57 -- takes in a mid_level (low level dequoted) string and splits it
58 -- up into normal text and ctcp messages. it returns an array, where string
59 -- values correspond to plain text and table values have t[1] as the ctcp
60 -- message. if dequote is true, each ctcp message will also be ctcp dequoted.
61 function ctcp_split(str, dequote)
62     local ret = {}
63     local iter = 1
64     while true do
65         local s, e = str:find("\001.*\001", iter)
66
67         local plain_string, ctcp_string
68         if not s then
69             plain_string = str:sub(iter, -1)
70         else
71             plain_string = str:sub(iter, s - 1)
72             ctcp_string = str:sub(s, e)
73         end
74
75         if plain_string ~= "" then
76             table.insert(ret, plain_string)
77         end
78         if not s then break end
79         if ctcp_string ~= "" then
80             if dequote then
81                 table.insert(ret, {ctcp_dequote(ctcp_string)})
82             else
83                 table.insert(ret, {ctcp_string})
84             end
85         end
86
87         iter = e + 1
88     end
89
90     return ret
91 end
92 -- }}}
93 -- }}}