]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Avoid some table lookups in Lua parts 1265/head
authorAndrew Lewis <nerf@judo.za.org>
Thu, 15 Dec 2016 09:38:26 +0000 (11:38 +0200)
committerAndrew Lewis <nerf@judo.za.org>
Thu, 15 Dec 2016 09:38:26 +0000 (11:38 +0200)
src/plugins/lua/clickhouse.lua
src/plugins/lua/dmarc.lua
src/plugins/lua/forged_recipients.lua
src/plugins/lua/mid.lua
src/plugins/lua/mx_check.lua
src/plugins/lua/ratelimit.lua
src/plugins/lua/rbl.lua
src/plugins/lua/spamassassin.lua
src/plugins/lua/whitelist.lua

index d45f0657b8e5c93e90048397122670830f85f245..78b83ad2db799440866ae70258d113e6c5d86e9c 100644 (file)
@@ -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
index 14e7bc52dd31933431de70764b9dcd0284e12058..d1adce6a167a0122af37c24ac06b96a0a329a3d1 100644 (file)
@@ -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
index 7c3cd07f821c6d0bb113a1d50c33bf5672931233..0b6ff7911c5362f4e77ac1bc3c47cea7a04b3752 100644 (file)
@@ -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
index 0ca26b91f182e4db69d59c497bd280416b9ef132..959697cd95059a6e689d496a3b42a86279e4bf71 100644 (file)
@@ -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
index db2e513695c07867c75047c74db3afd5d8b26832..f81d5ba932a5ad8a6038960e1bd6b4c9c0988238 100644 (file)
@@ -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()
index 57ca0fdcc17247b9fd85b564963b82805c25717c..f029732faf8188416aa4c1e50f9cdf1a73d93344 100644 (file)
@@ -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, ":")
index 002ee411a99c9bb32ec3ede74f6b804d97103b39..5d3a70e630d30d420d1e11ddc88916c4efe1438d 100644 (file)
@@ -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
index fc23707d92b5fd55e089d6bb5ebd9e9de940effe..a02ce8306d91822e678adcb99a794580178e6852 100644 (file)
@@ -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
index 0272a03d942c653e7801790e5d5c0712e6ebfc19..710b22effa5df67057a3bfd5110c7873181fbcb2 100644 (file)
@@ -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