]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Rework html visibility rule
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Jun 2021 13:06:56 +0000 (14:06 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Jun 2021 13:07:29 +0000 (14:07 +0100)
rules/html.lua
src/libserver/html/html_block.hxx
src/lua/lua_html.cxx

index 83f53a4dc84896502ee44a490b384df9466f338a..84ef91606f552e214e2ecf3aa11ab38b68ef96c6 100644 (file)
@@ -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
index 0958debdd82bba5025641adc3036d4e87db39d38..ac6de3200993c33b0cbd4cb53f75b811891ebf1b 100644 (file)
@@ -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;
        }
 
        /**
index 0972fde984765cc26d26ec1c3d74a91f0f7e7926..376df9fbb26c3d9854373aeb96d2fc0c89e64475 100644 (file)
@@ -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);
 }