]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Antivirus: ordered pattern matches
authorAndrew Lewis <nerf@judo.za.org>
Mon, 10 Jul 2017 12:42:12 +0000 (14:42 +0200)
committerAndrew Lewis <nerf@judo.za.org>
Mon, 10 Jul 2017 12:42:28 +0000 (14:42 +0200)
src/plugins/lua/antivirus.lua

index 0b9bf0b22115f738a44ee8a6cff4a077d8de5c0c..4802b239e1e812ee39f0d34335c24bb741a3cb19 100644 (file)
@@ -64,13 +64,24 @@ antivirus {
 end
 
 local function match_patterns(default_sym, found, patterns)
-  if not patterns then return default_sym end
-  for sym, pat in pairs(patterns) do
-    if pat:match(found) then
-      return sym
+  if type(patterns) ~= 'table' then return default_sym end
+  if not patterns[1] then
+    for sym, pat in pairs(patterns) do
+      if pat:match(found) then
+        return sym
+      end
+    end
+    return default_sym
+  else
+    for _, p in ipairs(patterns) do
+      for sym, pat in pairs(p) do
+        if pat:match(found) then
+          return sym
+        end
+      end
     end
+    return default_sym
   end
-  return default_sym
 end
 
 local function yield_result(task, rule, vname)
@@ -749,10 +760,24 @@ local function add_antivirus_rule(sym, opts)
     return nil
   end
 
-  if opts['patterns'] then
+  if type(opts['patterns']) == 'table' then
     rule['patterns'] = {}
-    for k, v in pairs(opts['patterns']) do
-      rule['patterns'][k] = rspamd_regexp.create_cached(v)
+    if opts['patterns'][1] then
+      for i, p in ipairs(opts['patterns']) do
+        if type(p) == 'table' then
+          local new_set = {}
+          for k, v in pairs(p) do
+            new_set[k] = rspamd_regexp.create_cached(v)
+          end
+          rule['patterns'][i] = new_set
+        else
+          rule['patterns'][i] = {}
+        end
+      end
+    else
+      for k, v in pairs(opts['patterns']) do
+        rule['patterns'][k] = rspamd_regexp.create_cached(v)
+      end
     end
   end
 
@@ -780,13 +805,27 @@ if opts and type(opts) == 'table' then
           name = m['symbol'],
           callback = cb,
         })
-        if m['patterns'] then
-          for sym in pairs(m['patterns']) do
-            rspamd_config:register_symbol({
-              type = 'virtual',
-              name = sym,
-              parent = id
-            })
+        if type(m['patterns']) == 'table' then
+          if m['patterns'][1] then
+            for _, p in ipairs(m['patterns']) do
+              if type(p) == 'table' then
+                for sym in pairs(p) do
+                  rspamd_config:register_symbol({
+                    type = 'virtual',
+                    name = sym,
+                    parent = id
+                  })
+                end
+              end
+            end
+          else
+            for sym in pairs(m['patterns']) do
+              rspamd_config:register_symbol({
+                type = 'virtual',
+                name = sym,
+                parent = id
+              })
+            end
           end
         end
         if m['score'] then