lex(msg) function

This commit is contained in:
yago 2015-03-14 17:23:38 +01:00
parent c58e507032
commit c189cfc93f

View File

@ -17,12 +17,15 @@ end
_values = read_file_values() _values = read_file_values()
function fetch_value(chat, value_name) function fetch_value(chat, value_name)
if (_values[chat] == nil) then -- Chat non exists
if _values[chat] == nil then
return nil return nil
end end
if (value_name == nil ) then
if value_name == nil then
return nil return nil
end end
local value = _values[chat][value_name] local value = _values[chat][value_name]
return value return value
end end
@ -57,18 +60,24 @@ function run(msg, matches)
return get_value(chat_id, matches[1]) return get_value(chat_id, matches[1])
end end
function lex(msg, text) function lex(msg)
local text = msg.text
local chat_id = tostring(msg.to.id) local chat_id = tostring(msg.to.id)
local s, e = text:find("%$%a+") local s, e = text:find("%$%a+")
if (s == nil) then
return nil if s then
local var = text:sub(s + 1, e)
local value = fetch_value(chat_id, var)
if (value == nil) then
value = "(unknown value " .. var .. ")"
end
msg.text = text:sub(0, s - 1) .. value .. text:sub(e + 1)
end end
local var = text:sub(s + 1, e)
local value = fetch_value(chat_id, var) return msg
if (value == nil) then
value = "(unknown value " .. var .. ")"
end
return text:sub(0, s - 1) .. value .. text:sub(e + 1)
end end
return { return {