3 local table = require "table"
8 -- public functions {{{
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",
16 ["\020"] = "\020\020"})
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
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",
39 return "\001" .. ret .. "\001"
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
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)
65 local s, e = str:find("\001.*\001", iter)
67 local plain_string, ctcp_string
69 plain_string = str:sub(iter, -1)
71 plain_string = str:sub(iter, s - 1)
72 ctcp_string = str:sub(s, e)
75 if plain_string ~= "" then
76 table.insert(ret, plain_string)
78 if not s then break end
79 if ctcp_string ~= "" then
81 table.insert(ret, {ctcp_dequote(ctcp_string)})
83 table.insert(ret, {ctcp_string})