diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-06-13 14:35:27 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-06-13 14:35:27 +0100 |
commit | c41acc4cb81ab3d526dd0cb968e93bca243cb64e (patch) | |
tree | de33711cb2053fc414d56d1385060da9531d4ee4 /lualib | |
parent | 08c54481e255f19af5ef8877381241bc0ca70998 (diff) | |
download | rspamd-c41acc4cb81ab3d526dd0cb968e93bca243cb64e.tar.gz rspamd-c41acc4cb81ab3d526dd0cb968e93bca243cb64e.zip |
[Minor] Use files instead of arguments for rspamc
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/rspamadm/classifier_test.lua | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/lualib/rspamadm/classifier_test.lua b/lualib/rspamadm/classifier_test.lua index 7bb9a22e6..21af14fc1 100644 --- a/lualib/rspamadm/classifier_test.lua +++ b/lualib/rspamadm/classifier_test.lua @@ -1,7 +1,6 @@ local rspamd_util = require "rspamd_util" local lua_util = require "lua_util" local argparse = require "argparse" -local fun = require "fun" local ucl = require "ucl" local rspamd_logger = require "rspamd_logger" @@ -57,36 +56,43 @@ local function split_table(t, fraction) return part1, part2 end -local function shell_quote(argument) - if argument:match('^[%w%+%-%.,:/=@_]+$') then - return argument - end - argument = argument:gsub('[$`"\\]', '\\%0') - return '"' .. argument .. '"' -end - -- Utility function to get all files in a directory local function get_files(dir) - return fun.totable(fun.map(shell_quote, rspamd_util.glob(dir .. '/*'))) + return rspamd_util.glob(dir .. '/*') +end + +local function list_to_file(list, fname) + local out = assert(io.open(fname, "w")) + for _, v in ipairs(list) do + out:write(v) + out:write("\n") + end + out:close() end -- Function to train the classifier with given files -local function train_classifier(files, command, connections) - local rspamc_command = string.format("%s --connect %s -j --compact -n %s -t %.3f %s %s", - opts.rspamc, opts.connect, opts.nconns, opts.timeout, command, table.concat(files, " ")) +local function train_classifier(files, command) + local fname = os.tmpname() + list_to_file(files, fname) + local rspamc_command = string.format("%s --connect %s -j --compact -n %s -t %.3f %s --files-list=%s", + opts.rspamc, opts.connect, opts.nconns, opts.timeout, command, fname) local result = assert(io.popen(rspamc_command)) result = result:read("*all") + os.remove(fname) end -- Function to classify files and return results local function classify_files(files) + local fname = os.tmpname() + list_to_file(files, fname) + local settings_header = '--header Settings=\"{symbols_enabled=[BAYES_SPAM, BAYES_HAM]}\"' - local rspamc_command = string.format("%s %s --connect %s --compact -n %s -t %.3f %s", + local rspamc_command = string.format("%s %s --connect %s --compact -n %s -t %.3f --files-list=%s", opts.rspamc, settings_header, opts.connect, opts.nconns, - opts.timeout, table.concat(files, " ")) + opts.timeout, fname) local result = assert(io.popen(rspamc_command)) local results = {} for line in result:lines() do @@ -94,6 +100,7 @@ local function classify_files(files) local is_good, err = ucl_parser:parse_string(line) if not is_good then rspamd_logger.errx("Parser error: %1", err) + os.remove(fname) return nil end local obj = ucl_parser:get_object() @@ -107,6 +114,8 @@ local function classify_files(files) end end + os.remove(fname) + return results end |