| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
]]-- |
| 16 |
|
| 17 |
local argparse = require "argparse" |
| 18 |
local rspamd_logger = require "rspamd_logger" |
| 19 |
|
| 20 |
-- Define command line options |
| 21 |
local parser = argparse() |
| 22 |
:name 'rspamadm publicsuffix' |
| 23 |
:description 'Do manipulations with the publicsuffix list' |
| 24 |
:help_description_margin(30) |
| 25 |
:command_target('command') |
| 26 |
:require_command(true) |
| 27 |
|
| 28 |
parser:option '-c --config' |
| 29 |
:description 'Path to config file' |
| 30 |
:argname('config_file') |
| 31 |
:default(rspamd_paths['CONFDIR'] .. '/rspamd.conf') |
| 32 |
|
| 33 |
parser:command 'compile' |
| 34 |
:description 'Compile publicsuffix list if needed' |
| 35 |
|
| 36 |
local function load_config(config_file) |
| 37 |
local _r, err = rspamd_config:load_ucl(config_file) |
| 38 |
|
| 39 |
if not _r then |
| 40 |
rspamd_logger.errx('cannot load %s: %s', config_file, err) |
| 41 |
os.exit(1) |
| 42 |
end |
| 43 |
|
| 44 |
_r, err = rspamd_config:parse_rcl({ 'logging', 'worker' }) |
| 45 |
if not _r then |
| 46 |
rspamd_logger.errx('cannot process %s: %s', config_file, err) |
| 47 |
os.exit(1) |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
local function compile_handler(_) |
| 52 |
local rspamd_url = require "rspamd_url" |
| 53 |
local tld_file = rspamd_config:get_tld_path() |
| 54 |
|
| 55 |
if not tld_file then |
| 56 |
rspamd_logger.errx('missing `url_tld` option, cannot continue') |
| 57 |
os.exit(1) |
| 58 |
end |
| 59 |
|
| 60 |
rspamd_logger.messagex('loading public suffix file from %s', tld_file) |
| 61 |
rspamd_url.init(tld_file) |
| 62 |
rspamd_logger.messagex('public suffix file has been loaded') |
| 63 |
end |
| 64 |
|
| 65 |
local function handler(args) |
| 66 |
local cmd_opts = parser:parse(args) |
| 67 |
|
| 68 |
load_config(cmd_opts.config_file) |
| 69 |
|
| 70 |
if cmd_opts.command == 'compile' then |
| 71 |
compile_handler(cmd_opts) |
| 72 |
else |
| 73 |
rspamd_logger.errx('unknown command: %s', cmd_opts.command) |
| 74 |
os.exit(1) |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
return { |
| 79 |
handler = handler, |
| 80 |
description = parser._description, |
| 81 |
name = 'publicsuffix' |
| 82 |
} |