From 5f65d0bfdb47c28a5f6095f378dfb05810bdbdaf Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 3 Nov 2014 11:32:26 +0100 Subject: [PATCH] get and set --- bot/bot.lua | 43 ++++++++++++++++++++++++++++++++++++++----- bot/config.json | 9 +++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index d775537..63f9eda 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -189,6 +189,18 @@ function do_action(msg) return end + if string.starts(msg.text, '!save') then + local text = save_value(msg.text:sub(6,-1)) + send_msg(receiver, text, ok_cb, false) + return + end + + if string.starts(msg.text, '!get') then + local text = get_value(msg.text:sub(6,-1)) + send_msg(receiver, text, ok_cb, false) + return + end + if string.starts(msg.text, '!version') then text = 'James Bot '.. VERSION .. [[ Licencia GNU v2, código disponible en http://git.io/6jdjGg @@ -229,7 +241,7 @@ function load_config() local f = assert(io.open('./bot/config.json', "r")) local c = f:read "*a" local config = json:decode(c) - if config.torrent_path then + if config.sh_enabled then print ("!sh command is enabled") for v,user in pairs(config.sudo_users) do print("Allowed user: " .. user) @@ -240,6 +252,27 @@ function load_config() return config end +function save_value( text ) + local vars = split_by_space(text) + if (#vars < 2) then + return "Usage: !save var_name value" + end + config.values[vars[1]] = vars[2] + local json_text = json:encode_pretty(config) + file = io.open ("./bot/config.json", "w+") + file:write(json_text) + file:close() + return "Saved "..vars[1].."="..vars[2] +end + +function get_value( value_name ) + local value = config.values[value_name] + if ( value == nil) then + return "Cant find "..value_name + end + return value_name.." = "..value +end + function is_sudo(msg) local var = false -- Check users id in config @@ -461,10 +494,10 @@ function split_by_space ( text ) end function string:split(sep) - local sep, fields = sep or ":", {} - local pattern = string.format("([^%s]+)", sep) - self:gsub(pattern, function(c) fields[#fields+1] = c end) - return fields + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + self:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields end function vardump(value, depth, key) diff --git a/bot/config.json b/bot/config.json index 6ffc9d8..8bdb0a2 100644 --- a/bot/config.json +++ b/bot/config.json @@ -1,5 +1,6 @@ { - "sudo_users":[0, 1], - "sh_enabled":false, - "log_file":"/var/www/html/log.txt" -} + "log_file": "/var/www/html/log.txt", + "sh_enabled": false, + "sudo_users": [ 0, 1 ], + "values": { } +} \ No newline at end of file