]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add content filtering support to multimap
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Jul 2016 13:18:14 +0000 (14:18 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Jul 2016 13:18:14 +0000 (14:18 +0100)
src/plugins/lua/multimap.lua

index 67e2930058537f21612b9088d290a75bd489c083..690953be05e027a03a5acdc97f271e5dc73b81f0 100644 (file)
@@ -269,6 +269,38 @@ local function multimap_callback(task, pre_filter)
     return fn
   end
 
+  local function apply_content_filter(filter, r)
+    if filter == 'body' then
+      return {task:get_rawbody()}
+    elseif filter == 'full' then
+      return {task:get_content()}
+    elseif filter == 'headers' then
+      return {task:get_raw_headers()}
+    elseif filter == 'text' then
+      local ret = {}
+      for i,p in ipairs(task:get_text_parts()) do
+        table.insert(ret, p:get_content())
+      end
+      return ret
+    elseif filter == 'rawtext' then
+      local ret = {}
+      for i,p in ipairs(task:get_text_parts()) do
+        table.insert(ret, p:get_raw_content())
+      end
+      return ret
+    elseif filter == 'oneline' then
+      local ret = {}
+      for i,p in ipairs(task:get_text_parts()) do
+        table.insert(ret, p:get_content_oneline())
+      end
+      return ret
+    else
+      rspamd_logger.errx(task, 'bad search filter: %s', filter)
+    end
+
+    return {}
+  end
+
   local function match_filename(r, fn)
     local value
     local ret = false
@@ -290,6 +322,28 @@ local function multimap_callback(task, pre_filter)
     end
   end
 
+  local function match_content(r)
+    local data = {}
+
+    if r['filter'] then
+      data = apply_content_filter(r['filter'], r)
+    else
+      data = {task:get_content()}
+    end
+
+    for i,v in ipairs(data) do
+      ret = match_element(r, v)
+
+      if ret then
+        task:insert_result(r['symbol'], 1)
+
+        if pre_filter then
+          task:set_pre_result(r['action'], 'Matched map: ' .. r['symbol'])
+        end
+      end
+    end
+  end
+
   -- IP rules
   local ip = task:get_from_ip()
   if ip:is_valid() then
@@ -352,6 +406,13 @@ local function multimap_callback(task, pre_filter)
           return pre_filter == r['prefilter'] and r['type'] == 'filename'
         end, rules))
   end
+  -- Body rules
+  _.each(function(r)
+    match_content(r)
+  end,
+  _.filter(function(r)
+    return pre_filter == r['prefilter'] and r['type'] == 'content'
+  end, rules))
 
   local parts = task:get_parts()
   for i,p in ipairs(parts) do
@@ -439,7 +500,8 @@ local function add_multimap_rule(key, newrule)
         or newrule['type'] == 'rcpt'
         or newrule['type'] == 'from'
         or newrule['type'] == 'filename'
-        or newrule['type'] == 'url' then
+        or newrule['type'] == 'url'
+        or newrule['type'] == 'content' then
       if newrule['regexp'] then
         newrule['hash'] = rspamd_config:add_map ({
           url = newrule['map'],