aboutsummaryrefslogtreecommitdiffstats
path: root/src/irc/ctcp.lua
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2007-08-26 22:07:38 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2007-08-26 22:07:38 -0500
commit86a0d68f9920cbcd382d8bb255639b022991def7 (patch)
treed9ecb2ec46ababa1654818d06d3015f5da746162 /src/irc/ctcp.lua
parent27ea3f3876546997cfc838f6d605b8f4ca5adcd7 (diff)
downloadluairc-86a0d68f9920cbcd382d8bb255639b022991def7.tar.gz
luairc-86a0d68f9920cbcd382d8bb255639b022991def7.zip
add all of the current files
Diffstat (limited to 'src/irc/ctcp.lua')
-rw-r--r--src/irc/ctcp.lua93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/irc/ctcp.lua b/src/irc/ctcp.lua
new file mode 100644
index 0000000..6a1877c
--- /dev/null
+++ b/src/irc/ctcp.lua
@@ -0,0 +1,93 @@
+-- initialization {{{
+local base = _G
+local table = require "table"
+-- }}}
+
+module "irc.ctcp"
+
+-- public functions {{{
+-- low_quote {{{
+-- applies low level quoting to a string (escaping characters which
+-- are illegal to appear in an irc packet)
+function low_quote(str)
+ return str:gsub("[%z\n\r\020]", {["\000"] = "\0200",
+ ["\n"] = "\020n",
+ ["\r"] = "\020r",
+ ["\020"] = "\020\020"})
+end
+-- }}}
+
+-- low_dequote {{{
+-- removes low level quoting done by low_quote
+function low_dequote(str)
+ return str:gsub("\020(.?)", function(s)
+ if s == "0" then return "\000" end
+ if s == "n" then return "\n" end
+ if s == "r" then return "\r" end
+ if s == "\020" then return "\020" end
+ return ""
+ end)
+end
+-- }}}
+
+-- ctcp_quote {{{
+-- applies ctcp quoting to a block of text which has been identified
+-- as ctcp data (by the calling program)
+function ctcp_quote(str)
+ local ret = str:gsub("[\001\\]", {["\001"] = "\\a",
+ ["\\"] = "\\\\"})
+ return "\001" .. ret .. "\001"
+end
+-- }}}
+
+-- ctcp_dequote {{{
+-- removes ctcp quoting from a block of text which has been
+-- identified as ctcp data (likely by ctcp_split)
+function ctcp_dequote(str)
+ local ret = str:gsub("^\001", ""):gsub("\001$", "")
+ return ret:gsub("\\(.?)", function(s)
+ if s == "a" then return "\001" end
+ if s == "\\" then return "\\" end
+ return ""
+ end)
+end
+-- }}}
+
+-- ctcp_split {{{
+-- takes in a mid_level (low level dequoted) string and splits it
+-- up into normal text and ctcp messages. it returns an array, where string
+-- values correspond to plain text and table values have t[1] as the ctcp
+-- message. if dequote is true, each ctcp message will also be ctcp dequoted.
+function ctcp_split(str, dequote)
+ local ret = {}
+ local iter = 1
+ while true do
+ local s, e = str:find("\001.*\001", iter)
+
+ local plain_string, ctcp_string
+ if not s then
+ plain_string = str:sub(iter, -1)
+ else
+ plain_string = str:sub(iter, s - 1)
+ ctcp_string = str:sub(s, e)
+ end
+
+ if plain_string ~= "" then
+ table.insert(ret, plain_string)
+ end
+ if not s then break end
+ if ctcp_string ~= "" then
+ if dequote then
+ table.insert(ret, {ctcp_dequote(ctcp_string)})
+ else
+ table.insert(ret, {ctcp_string})
+ end
+ end
+
+ iter = e + 1
+ end
+
+ return ret
+end
+-- }}}
+-- }}}