diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-09-22 17:35:46 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-09-22 17:35:46 +0100 |
commit | 261d4e4a379119d40d6cc891548c99b4b517c6d5 (patch) | |
tree | 6b57f49550ca48a23c858c51661ce21997f13a67 /src | |
parent | ebac714dad20b4d4739855095d1160dc55eeffb1 (diff) | |
download | rspamd-261d4e4a379119d40d6cc891548c99b4b517c6d5.tar.gz rspamd-261d4e4a379119d40d6cc891548c99b4b517c6d5.zip |
[Feature] Allow to store settings in redis
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/lua/settings.lua | 85 |
1 files changed, 81 insertions, 4 deletions
diff --git a/src/plugins/lua/settings.lua b/src/plugins/lua/settings.lua index b25698f27..c9c7eeeba 100644 --- a/src/plugins/lua/settings.lua +++ b/src/plugins/lua/settings.lua @@ -19,10 +19,8 @@ limitations under the License. -- https://rspamd.com/doc/configuration/settings.html local rspamd_logger = require "rspamd_logger" -local set_section = rspamd_config:get_all_opt("settings") -if not (set_section and type(set_section) == 'table') then - rspamd_logger.infox(rspamd_config, 'Module is unconfigured') -end +local rspamd_redis = require 'rspamd_redis' +local redis_params local settings = { [1] = {}, @@ -523,6 +521,85 @@ local function process_settings_map(string) return res end +local function gen_redis_callback(handler, id) + return function(task) + local key = handler(task) + + local function redis_settings_cb(task, err, data) + if not err and type(data) == 'string' then + local ucl = require "ucl" + local parser = ucl.parser() + local res,err = parser:parse_string(data) + if not res then + rspamd_logger.warnx(rspamd_config, 'cannot parse settings from redis: %s', + err) + else + local obj = parser:get_object() + rspamd_logger.infox(task, "<%1> apply settings according to redis rule %2", + task:get_message_id(), id) + task:set_settings(obj) + end + end + end + + if not key then + rspamd_logger.errx(rspamd_config, 'Cannot execute handler number %s', id) + return + end + + local ret,_,_ = rspamd_redis_make_request(task, + redis_params, -- connect params + key, -- hash key + false, -- is write + redis_settings_cb, --callback + 'GET', -- command + {key} -- arguments + ) + end +end + +local redis_section = rspamd_config:get_all_opt("settings_redis") +local redis_key_handlers = {} + +if redis_section then + redis_params = rspamd_parse_redis_server('replies') + if redis_params then + local handlers = redis_section.handlers + + for _,h in ipairs(handlers) do + local chunk,err = loadstring(h) + + if not chunk then + rspamd_logger.errx(rspamd_config, 'Cannot load handler from string: %s', + tostring(err)) + else + local res,func = pcall(chunk) + if not res then + rspamd_logger.errx(rspamd_config, 'Cannot add handler from string: %s', + tostring(func)) + else + table.insert(redis_key_handlers, func) + end + end + end + end + + each(function(id, h) + rspamd_config:register_symbol({ + name = 'REDIS_SETTINGS' .. tostring(id), + type = 'prefilter', + callback = gen_redis_callback(h, id), + priority = 10 + }) + end, redis_key_handlers) +end + +local set_section = rspamd_config:get_all_opt("settings") +if not redis_params and not (set_section and type(set_section) == 'table') then + rspamd_logger.infox(rspamd_config, 'Module is unconfigured') + return +end + if set_section and set_section[1] and type(set_section[1]) == "string" then -- Just a map of ucl if not rspamd_config:add_map(set_section[1], "settings map", process_settings_map) then |