]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Hfilter: Some refactoring in hfilter urls stuff
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 20 May 2022 21:15:14 +0000 (22:15 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 20 May 2022 21:15:14 +0000 (22:15 +0100)
src/plugins/lua/hfilter.lua

index c05499c4770c8f83af7820213b2bc2727af4b237..18205e37eae4d64e2909dfc5284cbe4a9aaf5257 100644 (file)
@@ -327,8 +327,8 @@ local function hfilter_callback(task)
   if config['url_enabled'] then
     local parts = task:get_text_parts()
     if parts then
-      local plain_text_part = nil
-      local html_text_part = nil
+      local plain_text_part, html_text_part
+
       for _,p in ipairs(parts) do
         if p:is_html() then
           html_text_part = p
@@ -336,36 +336,10 @@ local function hfilter_callback(task)
           plain_text_part = p
         end
       end
-      local hc = nil
-      if html_text_part then
-        hc = html_text_part:get_html()
-        if hc then
-          local url_len = 0
-          hc:foreach_tag('a', function(_, len)
-            url_len = url_len + len
-            return false
-          end)
-
-          local plen = html_text_part:get_length()
-
-          if url_len > 0 and plen > 0 then
-            local rel = url_len / plen
-            if rel > 0.8 then
-              local sc = (rel - 0.8) * 5.0
-              if sc > 1.0 then sc = 1.0 end
-              task:insert_result('HFILTER_URL_ONLY', sc, tostring(sc))
-              local lines =  html_text_part:get_lines_count()
-              if lines > 0 and lines < 2 then
-                task:insert_result('HFILTER_URL_ONELINE', 1.00,
-                  string.format('html:%d:%d', math.floor(sc), lines))
-              end
-            end
-          end
-        end
-      end
-      if not hc and plain_text_part then
-        local url_len = plain_text_part:get_urls_length()
-        local plen = plain_text_part:get_length()
+
+      local function check_text_part(part, ty)
+        local url_len = part:get_urls_length()
+        local plen = part:get_length()
 
         if plen > 0 and url_len > 0 then
           local rel = url_len / plen
@@ -373,14 +347,19 @@ local function hfilter_callback(task)
             local sc = (rel - 0.8) * 5.0
             if sc > 1.0 then sc = 1.0 end
             task:insert_result('HFILTER_URL_ONLY', sc, tostring(sc))
-            local lines = plain_text_part:get_lines_count()
+            local lines = part:get_lines_count()
             if lines > 0 and lines < 2 then
               task:insert_result('HFILTER_URL_ONELINE', 1.00,
-                string.format('plain:%d:%d', math.floor(rel), lines))
+                  string.format('%s:%d:%d', ty, math.floor(rel), lines))
             end
           end
         end
       end
+      if html_text_part then
+        check_text_part(html_text_part, 'html')
+      elseif plain_text_part then
+        check_text_part(plain_text_part, 'plain')
+      end
     end
   end
 
@@ -612,19 +591,13 @@ end
 --dumper(symbols_enabled)
 if #symbols_enabled > 0 then
   local id = rspamd_config:register_symbol{
-    name = 'HFILTER',
+    name = 'HFILTER_CHECK',
     callback = hfilter_callback,
-    type = 'callback,mime',
-    score = 0.0,
+    type = 'callback',
   }
-  rspamd_config:set_metric_symbol({
-    name = 'HFILTER',
-    score = 0.0,
-    group = 'hfilter'
-  })
   for _,sym in ipairs(symbols_enabled) do
     rspamd_config:register_symbol{
-      type = 'virtual,mime',
+      type = 'virtual',
       score = 1.0,
       parent = id,
       name = sym,
@@ -637,4 +610,4 @@ if #symbols_enabled > 0 then
   end
 else
   lua_util.disable_module(N, "config")
-end
+end
\ No newline at end of file