commit ea35232: [Project] Rework html visibility rule

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jun 14 13:14:07 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-06-14 14:06:56 +0100
URL: https://github.com/rspamd/rspamd/commit/ea35232b982f8ab4b3af5f286575c30780256168

[Project] Rework html visibility rule

---
 rules/html.lua                    | 48 ++++++++++++---------------------------
 src/libserver/html/html_block.hxx | 15 ++++++++----
 src/lua/lua_html.cxx              |  6 ++++-
 3 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/rules/html.lua b/rules/html.lua
index 83f53a4dc..84ef91606 100644
--- a/rules/html.lua
+++ b/rules/html.lua
@@ -199,47 +199,27 @@ local vis_check_id = rspamd_config:register_symbol{
         hc:foreach_tag({'font', 'span', 'div', 'p', 'td'}, function(tag, clen, is_leaf)
           local bl = tag:get_style()
           local rspamd_logger = require "rspamd_logger"
-          rspamd_logger.errx('hui: %s', bl)
           if bl then
-            if not bl['visible'] and is_leaf then
+            if not bl.visible and is_leaf then
               invisible_blocks = invisible_blocks + 1
             end
 
-            if bl['font_size'] and bl['font_size'] == 0 and is_leaf then
+            if (bl.font_size or 12) == 0 and is_leaf then
               zero_size_blocks = zero_size_blocks + 1
             end
 
-            if bl['bgcolor'] and bl['color'] and bl['visible'] and is_leaf then
-
-              local color = bl['color']
-              local bgcolor = bl['bgcolor']
-              -- Should use visual approach here some day
-              local diff_r = math.abs(color[1] - bgcolor[1])
-              local diff_g = math.abs(color[2] - bgcolor[2])
-              local diff_b = math.abs(color[3] - bgcolor[3])
-              local r_avg = (color[1] + bgcolor[1]) / 2.0
-              -- Square
-              diff_r = diff_r * diff_r
-              diff_g = diff_g * diff_g
-              diff_b = diff_b * diff_b
-
-              diff = math.sqrt(2*diff_r + 4*diff_g + 3 * diff_b +
-                  (r_avg * (diff_r - diff_b) / 256.0))
-              diff = diff / 256.0
-
-              if diff < 0.1 then
-                ret = true
-                invisible_blocks = invisible_blocks + 1 -- This block is invisible
-                transp_len = transp_len + clen * (0.1 - diff) * 10.0
-                normal_len = normal_len - clen
-                local tr = transp_len / (normal_len + transp_len)
-                if tr > transp_rate then
-                  transp_rate = tr
-                  arg = string.format('%s color #%x%x%x bgcolor #%x%x%x',
-                    tostring(tag:get_type()),
-                    color[1], color[2], color[3],
-                    bgcolor[1], bgcolor[2], bgcolor[3])
-                end
+            if bl.transparent and is_leaf then
+              ret = true
+              invisible_blocks = invisible_blocks + 1 -- This block is invisible
+              transp_len = transp_len + clen
+              normal_len = normal_len - clen
+              local tr = transp_len / (normal_len + transp_len)
+              if tr > transp_rate then
+                transp_rate = tr
+                arg = string.format('%s color #%x%x%x bgcolor #%x%x%x',
+                    tag:get_type(),
+                    bl.color[1], bl.color[2], bl.color[3],
+                    bl.bgcolor[1], bl.bgcolor[2], bl.bgcolor[3])
               end
             end
           end
diff --git a/src/libserver/html/html_block.hxx b/src/libserver/html/html_block.hxx
index 0958debdd..ac6de3200 100644
--- a/src/libserver/html/html_block.hxx
+++ b/src/libserver/html/html_block.hxx
@@ -41,6 +41,7 @@ struct html_block {
 	constexpr static const auto display_mask = 0x1 << 4;
 	constexpr static const auto font_size_mask = 0x1 << 5;
 	constexpr static const auto invisible_flag = 0x1 << 6;
+	constexpr static const auto transparent_flag = 0x1 << 7;
 
 	/* Helpers to set mask when setting the elements */
 	auto set_fgcolor(const rspamd::css::css_color &c) -> void {
@@ -182,7 +183,7 @@ struct html_block {
 		if ((mask & (bg_color_mask|fg_color_mask)) == (bg_color_mask|fg_color_mask)) {
 			if (fg_color.alpha < 10) {
 				/* Too transparent */
-				mask |= invisible_flag;
+				mask |= invisible_flag|transparent_flag;
 
 				return;
 			}
@@ -201,17 +202,21 @@ struct html_block {
 						  (ravg * (diff_r - diff_b) / 256.0)) / 256.0;
 
 				if (diff < 0.1) {
-					mask |= invisible_flag;
+					mask |= invisible_flag|transparent_flag;
 					return;
 				}
 			}
 		}
 
-		mask &= ~invisible_flag;
+		mask &= ~(invisible_flag|transparent_flag);
+	}
+
+	constexpr auto is_visible(void) const -> bool {
+		return (mask & invisible_flag) == 0;
 	}
 
-	auto is_visible(void) const -> bool {
-		return (mask & invisible_flag) != 0;
+	constexpr auto is_transparent(void) const -> bool {
+		return (mask & transparent_flag) != 0;
 	}
 
 	/**
diff --git a/src/lua/lua_html.cxx b/src/lua/lua_html.cxx
index 0972fde98..376df9fbb 100644
--- a/src/lua/lua_html.cxx
+++ b/src/lua/lua_html.cxx
@@ -382,7 +382,11 @@ lua_html_push_block (lua_State *L, const struct rspamd::html::html_block *bl)
 	}
 
 	lua_pushstring(L, "visible");
-	lua_pushboolean(L, (bl->mask & rspamd::html::html_block::invisible_flag) == 0);
+	lua_pushboolean(L, bl->is_visible());
+	lua_settable(L, -3);
+
+	lua_pushstring(L, "transparent");
+	lua_pushboolean(L, bl->is_transparent());
 	lua_settable(L, -3);
 }
 


More information about the Commits mailing list