From 0b49973d11481bab788ed7761dac7e586ab2bb18 Mon Sep 17 00:00:00 2001 From: Juraj Vajda Date: Tue, 13 Nov 2018 18:48:25 -0500 Subject: [PATCH] string escaping --- api.lua | 20 +++++++++++++++++++- chatcommands.lua | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/api.lua b/api.lua index 8ee7e57..7f6ec0a 100644 --- a/api.lua +++ b/api.lua @@ -20,6 +20,23 @@ function x_marketplace.isnan(value) return value ~= value end +local function esc(x) + return (x:gsub('%%', '%%%%') + :gsub('^%^', '%%^') + :gsub('%$$', '%%$') + :gsub('%(', '%%(') + :gsub('%)', '%%)') + :gsub('%.', '%%.') + :gsub('%,', '%%,') + :gsub('%;', '%%;') + :gsub('%[', '%%[') + :gsub('%]', '%%]') + :gsub('%*', '%%*') + :gsub('%+', '%%+') + :gsub('%-', '%%-') + :gsub('%?', '%%?')) +end + -- rounds a number to the nearest decimal places local function round(val, decimal) if (decimal) then @@ -34,8 +51,9 @@ end -- @return string with found items, if no items found returns boolean false function x_marketplace.store_find(string) local found = "" + local str = esc(string) for k, v in pairs(x_marketplace.store_list) do - if string.find(k, string) then + if string.find(k, str) then found = found..k.." buy: "..string.format("%.2f", v.buy).." sell: "..string.format("%.2f", v.sell).."\n" end end diff --git a/chatcommands.lua b/chatcommands.lua index 70d9fd1..e8323ad 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -22,7 +22,7 @@ minetest.register_chatcommand("mp", { return false, minetest.colorize(x_marketplace.colors.red, "MARKET PLACE: You need to write the item name you want to find. example: /mp find default:stone. See some suggestion from the store: ")..x_marketplace.store_get_random() end - local items = x_marketplace.store_find(minetest.formspec_escape(params[2])) + local items = x_marketplace.store_find(params[2]) if not items then return false, minetest.colorize(x_marketplace.colors.yellow, "MARKET PLACE: Oops there is no item like this in the store. Check out other items in the store: ")..x_marketplace.store_get_random() @@ -353,7 +353,7 @@ minetest.register_chatcommand("mp", { -- item not in store if not x_marketplace.store_list[params[2]] then - local suggestions = x_marketplace.store_find(minetest.formspec_escape(params[2])) + local suggestions = x_marketplace.store_find(params[2]) -- try suggest item from store, else show random items if suggestions then