From e8a127093213a52b0f8513f72fe7c0566b786158 Mon Sep 17 00:00:00 2001 From: Er2 Date: Sat, 12 Feb 2022 21:46:36 +0300 Subject: [PATCH] add quoted arguments --- core.lua | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/core.lua b/core.lua index e98e1b6..65877ef 100644 --- a/core.lua +++ b/core.lua @@ -35,9 +35,40 @@ local function receiveUpdate(self, update) if (msg.chat.type == 'group' or msg.chat.type == 'supergroup') and not to then return end - local args = {} + -- remove needn't text msg.text = msg.text:sub(#cmd + #(to or '') + 3) - for s in msg.text:gmatch '%S+' do table.insert(args, s) end + + -- "quoted" and not quoted args support + -- inside quotes, buffer and final args + local iq, buf, args = false, '', {} + local qt, esc = nil, false -- quote type and escape, addition + + -- helper function, add args and clean buffer + local function psh() + if #buf > 0 then table.insert(args, buf) end + buf = '' + qt = nil + end + + for i = 1, #msg.text do + local v = msg.text:sub(i, i) + print(v, buf, iq) + -- if space and not inside quotes + if (v == ' ' or v == '\n' or v == '\t') + and not iq then psh() + + elseif esc then esc, buf = false, buf.. v + elseif v == '\\' then esc = true + + elseif (not qt or qt == v) + and (v == '"' or v == "'") then + psh() + qt, iq = v, not iq -- quote type, inside quote + + else buf = buf.. v -- append buffer + end + end + psh() -- if has something msg.cmd = cmd msg.args = args