From f9de08822c9dac84c30d1e7885d1400aa584a0b1 Mon Sep 17 00:00:00 2001 From: Andrew Lewis Date: Thu, 15 Dec 2016 11:38:26 +0200 Subject: [PATCH] [Minor] Avoid some table lookups in Lua parts --- src/plugins/lua/clickhouse.lua | 4 +++- src/plugins/lua/dmarc.lua | 8 +++++--- src/plugins/lua/forged_recipients.lua | 4 +++- src/plugins/lua/mid.lua | 4 +++- src/plugins/lua/mx_check.lua | 4 +++- src/plugins/lua/ratelimit.lua | 8 +++++--- src/plugins/lua/rbl.lua | 4 +++- src/plugins/lua/spamassassin.lua | 8 +++++--- src/plugins/lua/whitelist.lua | 6 ++++-- 9 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/plugins/lua/clickhouse.lua b/src/plugins/lua/clickhouse.lua index d45f0657b..78b83ad2d 100644 --- a/src/plugins/lua/clickhouse.lua +++ b/src/plugins/lua/clickhouse.lua @@ -17,6 +17,8 @@ limitations under the License. local rspamd_logger = require 'rspamd_logger' local rspamd_http = require "rspamd_http" +local E = {} + local rows = {} local attachment_rows = {} local urls_rows = {} @@ -444,7 +446,7 @@ local function clickhouse_collect(task) if settings['from_map'] and dkim == 'allow' then -- Use dkim local das = task:get_symbol(settings['dkim_allow_symbols'][1]) - if das and das[1] and das[1]['options'] then + if ((das or E)[1] or E).options then for _,dkim_domain in ipairs(das[1]['options']) do local specific = settings.from_map:get_key(dkim_domain) if specific then diff --git a/src/plugins/lua/dmarc.lua b/src/plugins/lua/dmarc.lua index 14e7bc52d..d1adce6a1 100644 --- a/src/plugins/lua/dmarc.lua +++ b/src/plugins/lua/dmarc.lua @@ -54,6 +54,8 @@ local dmarc_redis_key_expire = 60 * 60 * 24 * 2 local dmarc_reporting = false local dmarc_actions = {} +local E = {} + local function gen_dmarc_grammar() local lpeg = require "lpeg" lpeg.locale(lpeg) @@ -99,7 +101,7 @@ local function dmarc_callback(task) rspamd_logger.infox(task, "skip DMARC checks for local networks and authorized users"); return end - if from and from[1] and from[1]['domain'] and not from[2] then + if ((from or E)[1] or E).domain and not (from or E)[2] then dmarc_domain = rspamd_util.get_tld(from[1]['domain']) else task:insert_result(dmarc_symbols['na'], 1.0, 'No From header') @@ -260,7 +262,7 @@ local function dmarc_callback(task) local dkim_ok = false if task:has_symbol(symbols['spf_allow_symbol']) then local efrom = task:get_from(1) - if efrom and efrom[1] and efrom[1]['domain'] then + if ((efrom or E)[1] or E).domain then if strict_spf and rspamd_util.strequal_caseless(efrom[1]['domain'], from[1]['domain']) then spf_ok = true elseif strict_spf then @@ -279,7 +281,7 @@ local function dmarc_callback(task) table.insert(reason, "No valid SPF") end local das = task:get_symbol(symbols['dkim_allow_symbol']) - if das and das[1] and das[1]['options'] then + if ((das or E)[1] or E).options then for _,dkim_domain in ipairs(das[1]['options']) do if strict_dkim and rspamd_util.strequal_caseless(from[1]['domain'], dkim_domain) then dkim_ok = true diff --git a/src/plugins/lua/forged_recipients.lua b/src/plugins/lua/forged_recipients.lua index 7c3cd07f8..0b6ff7911 100644 --- a/src/plugins/lua/forged_recipients.lua +++ b/src/plugins/lua/forged_recipients.lua @@ -20,6 +20,8 @@ limitations under the License. local symbol_rcpt = 'FORGED_RECIPIENTS' local symbol_sender = 'FORGED_SENDER' +local E = {} + local function check_forged_headers(task) local auser = task:get_user() local smtp_rcpt = task:get_recipients(1) @@ -48,7 +50,7 @@ local function check_forged_headers(task) -- allow user to BCC themselves res = true break - elseif smtp_from and smtp_from[1] and smtp_from[1]['addr'] and + elseif ((smtp_from or E)[1] or E).addr and smtp_from[1]['addr'] == sr['addr'] then -- allow sender to BCC themselves res = true diff --git a/src/plugins/lua/mid.lua b/src/plugins/lua/mid.lua index 0ca26b91f..959697cd9 100644 --- a/src/plugins/lua/mid.lua +++ b/src/plugins/lua/mid.lua @@ -35,11 +35,13 @@ local settings = { local map = {} +local E = {} + local function known_mid_cb(task) local re = {} local header = task:get_header('Message-Id') local das = task:get_symbol(settings['symbol_dkim_allow']) - if das and das[1] and das[1]['options'] then + if ((das or E)[1] or E).options then for _,dkim_domain in ipairs(das[1]['options']) do local v = map:get_key(dkim_domain) if v then diff --git a/src/plugins/lua/mx_check.lua b/src/plugins/lua/mx_check.lua index db2e51369..f81d5ba93 100644 --- a/src/plugins/lua/mx_check.lua +++ b/src/plugins/lua/mx_check.lua @@ -32,6 +32,8 @@ local settings = { } local redis_params +local E = {} + local function mx_check(task) local ip_addr = task:get_ip() if task:get_user() or (ip_addr and ip_addr:is_local()) then @@ -40,7 +42,7 @@ local function mx_check(task) local from = task:get_from('smtp') local mx_domain - if from and from[1] and from[1]['domain'] and not from[2] then + if ((from or E)[1] or E).domain and not from[2] then mx_domain = from[1]['domain'] else mx_domain = task:get_helo() diff --git a/src/plugins/lua/ratelimit.lua b/src/plugins/lua/ratelimit.lua index 57ca0fdcc..f029732fa 100644 --- a/src/plugins/lua/ratelimit.lua +++ b/src/plugins/lua/ratelimit.lua @@ -16,6 +16,8 @@ limitations under the License. -- A plugin that implements ratelimits using redis or kvstorage server +local E = {} + -- Default settings for limits, 1-st member is burst, second is rate and the third is numeric type local settings = { } @@ -114,7 +116,7 @@ local keywords = { ['from'] = { ['get_value'] = function(task) local from = task:get_from(0) - if from and from[1] and from[1]['addr'] then + if ((from or E)[1] or E).addr then return from[1]['addr'] end return nil @@ -123,7 +125,7 @@ local keywords = { ['bounce'] = { ['get_value'] = function(task) local from = task:get_from(0) - if not from or not from[1] or not from[1]['user'] then + if not ((from or E)[1] or E).user then return '_' end if check_bounce(from[1]['user']) then return '_' else return nil end @@ -187,7 +189,7 @@ local function dynamic_rate_key(task, rtype) else local rate_keys = {} local rcpts = task:get_recipients(0) - if not rcpts or not rcpts[1] or not rcpts[1]['addr'] then + if not ((rcpts or E)[1] or E).addr then return nil end local key_s = table.concat(key_t, ":") diff --git a/src/plugins/lua/rbl.lua b/src/plugins/lua/rbl.lua index 002ee411a..5d3a70e63 100644 --- a/src/plugins/lua/rbl.lua +++ b/src/plugins/lua/rbl.lua @@ -19,7 +19,9 @@ limitations under the License. -- Documentation can be found here: -- https://rspamd.com/doc/modules/rbl.html +local E = {} local N = 'rbl' + local rbls = {} local local_exclusions = nil @@ -191,7 +193,7 @@ local function rbl_cb (task) end if not havegot['dkim'] then local das = task:get_symbol(symbols['dkim_allow_symbol']) - if das and das[1] and das[1]['options'] then + if ((das or E)[1] or E).options then havegot['dkim'] = das[1]['options'] else notgot['dkim'] = true diff --git a/src/plugins/lua/spamassassin.lua b/src/plugins/lua/spamassassin.lua index fc23707d9..a02ce8306 100644 --- a/src/plugins/lua/spamassassin.lua +++ b/src/plugins/lua/spamassassin.lua @@ -17,7 +17,9 @@ limitations under the License. -- This plugin is intended to read and parse spamassassin rules with regexp -- rules. SA plugins or statistics are not supported +local E = {} local N = 'spamassassin' + local rspamd_logger = require "rspamd_logger" local rspamd_regexp = require "rspamd_regexp" local rspamd_expression = require "rspamd_expression" @@ -455,7 +457,7 @@ local function gen_eval_rule(arg) 'check_from_in_blacklist', function(task) local from = task:get_from('mime') - if from and from[1] and from[1]['addr'] then + if ((from or E)[1] or E).addr then if sa_lists['from_blacklist'][string.lower(from[1]['addr'])] then return 1 end @@ -468,7 +470,7 @@ local function gen_eval_rule(arg) 'check_from_in_whitelist', function(task) local from = task:get_from('mime') - if from and from[1] and from[1]['addr'] then + if ((from or E)[1] or E).addr then if sa_lists['from_whitelist'][string.lower(from[1]['addr'])] then return 1 end @@ -481,7 +483,7 @@ local function gen_eval_rule(arg) 'check_from_in_default_whitelist', function(task) local from = task:get_from('mime') - if from and from[1] and from[1]['addr'] then + if ((from or E)[1] or E).addr then if sa_lists['from_def_whitelist'][string.lower(from[1]['addr'])] then return 1 end diff --git a/src/plugins/lua/whitelist.lua b/src/plugins/lua/whitelist.lua index 0272a03d9..710b22eff 100644 --- a/src/plugins/lua/whitelist.lua +++ b/src/plugins/lua/whitelist.lua @@ -26,6 +26,8 @@ local options = { rules = {} } +local E = {} + local function whitelist_cb(symbol, rule, task) local domains = {} @@ -73,7 +75,7 @@ local function whitelist_cb(symbol, rule, task) -- Now we can check from domain or helo local from = task:get_from(1) - if from and from[1] and from[1]['domain'] then + if ((from or E)[1] or E).domain then local tld = rspamd_util.get_tld(from[1]['domain']) if tld then @@ -126,7 +128,7 @@ local function whitelist_cb(symbol, rule, task) end local from = task:get_from(2) - if from and from[1] and from[1]['domain'] then + if ((from or E)[1] or E).domain then local tld = rspamd_util.get_tld(from[1]['domain']) if tld then -- 2.39.5