From 29c3237009537015e66875ab9ce1fbe3e101576b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 22 Dec 2010 18:08:14 +0300 Subject: [PATCH] * Add versions to lua API * Provide compatibility for lua plugins for old versions of rspamd --- src/lua/lua_common.h | 2 ++ src/lua/lua_config.c | 9 +++++++++ src/plugins/lua/forged_recipients.lua | 22 +++++++++++++++++----- src/plugins/lua/maillist.lua | 6 +++++- src/plugins/lua/multimap.lua | 20 ++++++++++++++++---- src/plugins/lua/once_received.lua | 16 +++++++++++----- src/plugins/lua/phishing.lua | 8 ++++++-- src/plugins/lua/received_rbl.lua | 12 +++++++++--- src/plugins/lua/whitelist.lua | 12 ++++++++---- 9 files changed, 83 insertions(+), 24 deletions(-) diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 823056316..3e0fbed88 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -16,6 +16,8 @@ extern const luaL_reg null_reg[]; +#define RSPAMD_LUA_API_VERSION 1 + /* Common utility functions */ void lua_newclass (lua_State *L, const gchar *classname, const struct luaL_reg *func); void lua_setclass (lua_State *L, const gchar *classname, gint objidx); diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index e05545355..b77c97bfc 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -44,6 +44,7 @@ LUA_FUNCTION_DEF (config, register_virtual_symbol); LUA_FUNCTION_DEF (config, register_callback_symbol); LUA_FUNCTION_DEF (config, register_post_filter); LUA_FUNCTION_DEF (config, register_module_option); +LUA_FUNCTION_DEF (config, get_api_version); static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_module_opt), @@ -57,6 +58,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, register_callback_symbol), LUA_INTERFACE_DEF (config, register_module_option), LUA_INTERFACE_DEF (config, register_post_filter), + LUA_INTERFACE_DEF (config, get_api_version), {"__tostring", lua_class_tostring}, {NULL, NULL} }; @@ -128,6 +130,13 @@ lua_check_trie (lua_State * L) } /*** Config functions ***/ +static gint +lua_config_get_api_version (lua_State *L) +{ + lua_pushinteger (L, RSPAMD_LUA_API_VERSION); + return 1; +} + static gint lua_config_get_module_opt (lua_State * L) { diff --git a/src/plugins/lua/forged_recipients.lua b/src/plugins/lua/forged_recipients.lua index d759960d3..6b6ec298b 100644 --- a/src/plugins/lua/forged_recipients.lua +++ b/src/plugins/lua/forged_recipients.lua @@ -64,8 +64,12 @@ function check_forged_headers(task) end -- Registration -rspamd_config:register_module_option('forged_recipients', 'symbol_rcpt', 'string') -rspamd_config:register_module_option('forged_recipients', 'symbol_sender', 'string') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('forged_recipients', 'symbol_rcpt', 'string') + rspamd_config:register_module_option('forged_recipients', 'symbol_sender', 'string') + end +end -- Configuration local opts = rspamd_config:get_all_opt('forged_recipients') @@ -73,13 +77,21 @@ if opts then if opts['symbol_rcpt'] or opts['symbol_sender'] then if opts['symbol_rcpt'] then symbol_rcpt = opts['symbol_rcpt'] - rspamd_config:register_virtual_symbol(symbol_rcpt, 1.0, 'check_forged_headers') + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(symbol_rcpt, 1.0, 'check_forged_headers') + end end if opts['symbol_sender'] then symbol_sender = opts['symbol_sender'] - rspamd_config:register_virtual_symbol(symbol_sender, 1.0) + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(symbol_sender, 1.0) + end + end + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_callback_symbol('FORGED_RECIPIENTS', 1.0, 'check_forged_headers') + else + rspamd_config:register_symbol('FORGED_RECIPIENTS', 1.0, 'check_forged_headers') end - rspamd_config:register_callback_symbol('FORGED_RECIPIENTS', 1.0, 'check_forged_headers') end end diff --git a/src/plugins/lua/maillist.lua b/src/plugins/lua/maillist.lua index 16be1764f..c7a4751eb 100644 --- a/src/plugins/lua/maillist.lua +++ b/src/plugins/lua/maillist.lua @@ -163,7 +163,11 @@ function check_maillist(task) end end -- Registration -rspamd_config:register_module_option('maillist', 'symbol', 'string') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('maillist', 'symbol', 'string') + end +end -- Configuration local opts = rspamd_config:get_all_opt('maillist') if opts then diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua index 97f122c48..6986c8c72 100644 --- a/src/plugins/lua/multimap.lua +++ b/src/plugins/lua/multimap.lua @@ -130,7 +130,11 @@ function add_rule(params) end -- Registration -rspamd_config:register_module_option('multimap', 'rule', 'string') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('multimap', 'rule', 'string') + end +end local opts = rspamd_config:get_all_opt('multimap') if opts then @@ -143,7 +147,9 @@ if opts then if not rule then rspamd_logger:err('cannot add rule: "'..value..'"') else - rspamd_config:register_virtual_symbol(rule['symbol'], 1.0) + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(rule['symbol'], 1.0) + end end end elseif type(strrules) == 'string' then @@ -152,7 +158,9 @@ if opts then if not rule then rspamd_logger:err('cannot add rule: "'..strrules..'"') else - rspamd_config:register_virtual_symbol(rule['symbol'], 1.0) + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(rule['symbol'], 1.0) + end end end end @@ -160,5 +168,9 @@ end if table.maxn(rules) > 0 then -- add fake symbol to check all maps inside a single callback - rspamd_config:register_callback_symbol('MULTIMAP', 1.0, 'check_multimap') + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_callback_symbol('MULTIMAP', 1.0, 'check_multimap') + else + rspamd_config:register_symbol('MULTIMAP', 1.0, 'check_multimap') + end end diff --git a/src/plugins/lua/once_received.lua b/src/plugins/lua/once_received.lua index 97df71da3..a7b2a2655 100644 --- a/src/plugins/lua/once_received.lua +++ b/src/plugins/lua/once_received.lua @@ -45,10 +45,14 @@ function check_quantity_received (task) end -- Registration -rspamd_config:register_module_option('once_received', 'symbol', 'string') -rspamd_config:register_module_option('once_received', 'symbol_strict', 'string') -rspamd_config:register_module_option('once_received', 'bad_host', 'string') -rspamd_config:register_module_option('once_received', 'good_host', 'string') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('once_received', 'symbol', 'string') + rspamd_config:register_module_option('once_received', 'symbol_strict', 'string') + rspamd_config:register_module_option('once_received', 'bad_host', 'string') + rspamd_config:register_module_option('once_received', 'good_host', 'string') + end +end -- Configuration local opts = rspamd_config:get_all_opt('once_received') @@ -59,7 +63,9 @@ if opts then for n,v in pairs(opts) do if n == 'symbol_strict' then symbol_strict = v - rspamd_config:register_virtual_symbol(symbol_strict, 1.0) + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(symbol_strict, 1.0) + end elseif n == 'bad_host' then bad_hosts = v elseif n == 'good_host' then diff --git a/src/plugins/lua/phishing.lua b/src/plugins/lua/phishing.lua index af5c91e15..115688750 100644 --- a/src/plugins/lua/phishing.lua +++ b/src/plugins/lua/phishing.lua @@ -28,8 +28,12 @@ function phishing_cb (task) end -- Registration -rspamd_config:register_module_option('phishing', 'symbol', 'string') -rspamd_config:register_module_option('phishing', 'domains', 'map') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('phishing', 'symbol', 'string') + rspamd_config:register_module_option('phishing', 'domains', 'map') + end +end local opts = rspamd_config:get_all_opt('phishing') if opts then diff --git a/src/plugins/lua/received_rbl.lua b/src/plugins/lua/received_rbl.lua index 162add51e..653b092ad 100644 --- a/src/plugins/lua/received_rbl.lua +++ b/src/plugins/lua/received_rbl.lua @@ -56,8 +56,12 @@ function received_cb (task) end -- Registration -rspamd_config:register_module_option('received_rbl', 'symbol', 'string') -rspamd_config:register_module_option('received_rbl', 'rbl', 'string') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('received_rbl', 'symbol', 'string') + rspamd_config:register_module_option('received_rbl', 'rbl', 'string') + end +end -- Configuration local opts = rspamd_config:get_all_opt('received_rbl') @@ -71,7 +75,9 @@ if opts then for _,rbl in ipairs(rbls) do local s, _ = string.find(rbl, ':') if s then - rspamd_config:register_virtual_symbol(string.sub(rbl, s + 1, -1), 1) + if type(rspamd_config.get_api_version) ~= 'nil' then + rspamd_config:register_virtual_symbol(string.sub(rbl, s + 1, -1), 1) + end end end -- Register symbol's callback diff --git a/src/plugins/lua/whitelist.lua b/src/plugins/lua/whitelist.lua index 1899809fe..0f4c41f85 100644 --- a/src/plugins/lua/whitelist.lua +++ b/src/plugins/lua/whitelist.lua @@ -33,10 +33,14 @@ function check_whitelist (task) end -- Registration -rspamd_config:register_module_option('whitelist', 'symbol_ip', 'string') -rspamd_config:register_module_option('whitelist', 'symbol_from', 'string') -rspamd_config:register_module_option('whitelist', 'ip_whitelist', 'map') -rspamd_config:register_module_option('whitelist', 'from_whitelist', 'map') +if type(rspamd_config.get_api_version) ~= 'nil' then + if rspamd_config:get_api_version() >= 1 then + rspamd_config:register_module_option('whitelist', 'symbol_ip', 'string') + rspamd_config:register_module_option('whitelist', 'symbol_from', 'string') + rspamd_config:register_module_option('whitelist', 'ip_whitelist', 'map') + rspamd_config:register_module_option('whitelist', 'from_whitelist', 'map') + end +end -- Configuration local opts = rspamd_config:get_all_opt('whitelist') -- 2.39.5