From c2559dc9fa65e05bf1e2aac0c56897d144184a22 Mon Sep 17 00:00:00 2001 From: Jason Stephenson Date: Thu, 21 Jul 2022 12:20:42 +0100 Subject: [PATCH] Refactor before adding support to extract option --- lualib/rspamadm/mime.lua | 75 +++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/lualib/rspamadm/mime.lua b/lualib/rspamadm/mime.lua index 5f063666d..96aed5ca4 100644 --- a/lualib/rspamadm/mime.lua +++ b/lualib/rspamadm/mime.lua @@ -878,52 +878,55 @@ local function sign_handler(opts) end end ---Strips directories and .extensions from a filename/path -local function filename_only(filename) - stripped_filename = filename:match(".*%/([^%.]*)") - if not stripped_filename then - stripped_filename = filename:match("(.*)%.") +--Strips directories and .extensions (if present) from a filepath +local function filename_only(filepath) + filename = filepath:match(".*%/([^%.]*)") + if not filename then + filename = filepath:match("(.*)%.") end - return stripped_filename + return filename end -local function dump_handler(opts) - load_config(opts) - rspamd_url.init(rspamd_config:get_tld_path()) +local function get_dump_content(task, opts) + if opts.ucl or opts.json or opts.messagepack then + local ucl_object = lua_mime.message_to_ucl(task) + local extension = output_fmt(opts) + return ucl.to_format(ucl_object, out_extension), extension + end + return tostring(task:get_content()), "mime" +end - for _,fname in ipairs(opts.file) do +--Write the dump content to file or standard out +local function write_dump_content(data, fname, extension, outdir) + if outdir then + if outdir:sub(-1) ~= "/" then + outdir = outdir .. "/" + end - local task = load_task(opts, fname) + local outpath = string.format("%s%s.%s", outdir, filename_only(fname), extension) + local outfile = io.open(outpath, "w") - local data_to_write = nil - local out_extension = nil - if opts.ucl or opts.json or opts.messagepack then - local ucl_object = lua_mime.message_to_ucl(task) - out_extension = output_fmt(opts) - data_to_write = ucl.to_format(ucl_object, out_extension) + if outfile then + outfile:write(data) + outfile:close() + io.write(outpath.."\n") else - out_extension = "mime" - data_to_write = tostring(task:get_content()) + io.stderr:write(string.format("Unable to open: %s\n", outpath)) end + else + io.write(data) + end +end - if opts.outdir then - if opts.outdir:sub(-1) ~= "/" then - opts.outdir = opts.outdir .. "/" - end +local function dump_handler(opts) + load_config(opts) + rspamd_url.init(rspamd_config:get_tld_path()) - local outpath = string.format("%s%s.%s", opts.outdir, filename_only(fname), out_extension) - local outfile = io.open(outpath, "w") - - if outfile then - outfile:write(data_to_write) - outfile:close() - io.write(outpath.."\n") - else - io.stderr:write(string.format("Unable to open: %s\n", outpath)) - end - else - io.write(data_to_write) - end + for _,fname in ipairs(opts.file) do + local task = load_task(opts, fname) + + local data, extension = get_dump_content(task, opts) + write_dump_content(data, fname, extension, opts.outdir) task:destroy() -- No automatic dtor end -- 2.39.5