]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Support opacity style
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 23 Jun 2018 15:02:02 +0000 (16:02 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 23 Jun 2018 15:02:02 +0000 (16:02 +0100)
src/libserver/html.c
src/libserver/html.h

index 2ae4a3ba2be17503de76c467de299582c1e0a3bf..27aea6a1193bb1802554137af1ca2eec0fa7b5ac 100644 (file)
@@ -1164,6 +1164,9 @@ rspamd_html_parse_tag_component (rspamd_mempool_t *pool,
                        if (g_ascii_strncasecmp (p, "bgcolor", len) == 0) {
                                NEW_COMPONENT (RSPAMD_HTML_COMPONENT_BGCOLOR);
                        }
+                       else if (g_ascii_strncasecmp (p, "opacity", len) == 0) {
+                               NEW_COMPONENT (RSPAMD_HTML_COMPONENT_OPACITY);
+                       }
                }
                else if (len == 4) {
                        if (g_ascii_strncasecmp (p, "size", len) == 0) {
@@ -2152,6 +2155,7 @@ rspamd_html_process_style (rspamd_mempool_t *pool, struct html_block *bl,
                skip_spaces,
        } state = skip_spaces, next_state = read_key;
        guint klen = 0;
+       gdouble opacity = 1.0;
 
        p = style;
        c = p;
@@ -2209,15 +2213,28 @@ rspamd_html_process_style (rspamd_mempool_t *pool, struct html_block *bl,
                                                        msg_debug_html ("tag is not visible");
                                                }
                                        }
-                                       else if (klen == 9 && g_ascii_strncasecmp (key, "font-size", 9) == 0) {
+                                       else if (klen == 9 &&
+                                                        g_ascii_strncasecmp (key, "font-size", 9) == 0) {
                                                rspamd_html_process_font_size (c, p - c,
                                                                &bl->font_size, TRUE);
                                                msg_debug_html ("got font size: %u", bl->font_size);
+                                       }
+                                       else if (klen == 7 &&
+                                                        g_ascii_strncasecmp (key, "opacity", 7) == 0) {
+                                               gchar numbuf[64];
 
-                                               if (bl->font_size < 3) {
-                                                       bl->visible = FALSE;
-                                                       msg_debug_html ("tag is not visible");
+                                               rspamd_strlcpy (numbuf, c,
+                                                               MIN (sizeof (numbuf), p - c + 1));
+                                               opacity = strtod (numbuf, NULL);
+
+                                               if (opacity > 1) {
+                                                       opacity = 1;
                                                }
+                                               else if (opacity < 0) {
+                                                       opacity = 0;
+                                               }
+
+                                               bl->font_color.d.comp.alpha = (guint8)(opacity * 255.0);
                                        }
                                }
 
@@ -2252,12 +2269,15 @@ rspamd_html_process_block_tag (rspamd_mempool_t *pool, struct html_tag *tag,
        struct html_block *bl;
        rspamd_ftok_t fstr;
        GList *cur;
+       gdouble opacity = 1.0;
+       gchar numbuf[64];
 
        cur = tag->params->head;
        bl = rspamd_mempool_alloc0 (pool, sizeof (*bl));
        bl->tag = tag;
        bl->visible = TRUE;
        bl->font_size = (guint)-1;
+       bl->font_color.d.comp.alpha = 255;
 
        while (cur) {
                comp = cur->data;
@@ -2304,6 +2324,20 @@ rspamd_html_process_block_tag (rspamd_mempool_t *pool, struct html_tag *tag,
                                                &bl->font_color);
                                msg_debug_html ("got color: %xd", bl->font_color.d.val);
                                break;
+                       case RSPAMD_HTML_COMPONENT_OPACITY:
+                               rspamd_strlcpy (numbuf, comp->start,
+                                               MIN (sizeof (numbuf), comp->len + 1));
+                               opacity = strtod (numbuf, NULL);
+
+                               if (opacity > 1) {
+                                       opacity = 1;
+                               }
+                               else if (opacity < 0) {
+                                       opacity = 0;
+                               }
+
+                               bl->font_color.d.comp.alpha = (guint8)(opacity * 255.0);
+                               break;
                        default:
                                /* NYI */
                                break;
@@ -2462,8 +2496,10 @@ rspamd_html_propagate_style (struct html_content *hc,
 
        /* Set bgcolor to the html bgcolor and font color to black as a last resort */
        if (!bl->font_color.valid) {
-               bl->font_color.d.val = 0;
-               bl->font_color.d.comp.alpha = 255;
+               /* Don't touch opacity as it can be set separately */
+               bl->font_color.d.comp.r = 0;
+               bl->font_color.d.comp.g = 0;
+               bl->font_color.d.comp.b = 0;
                bl->font_color.valid = TRUE;
        }
 
@@ -2951,13 +2987,21 @@ rspamd_html_process_part_full (rspamd_mempool_t *pool, struct html_content *hc,
                                                rspamd_html_process_block_tag (pool, cur_tag, hc);
                                                bl = cur_tag->extra;
 
-                                               if (bl && !bl->visible) {
-                                                       state = content_ignore;
-                                               }
-
                                                if (bl) {
                                                        rspamd_html_propagate_style (hc, cur_tag,
                                                                        cur_tag->extra, styles_blocks);
+
+                                                       /* Check visibility */
+                                                       if (bl->font_size < 3 ||
+                                                               bl->font_color.d.comp.alpha < 10) {
+
+                                                               bl->visible = FALSE;
+                                                               msg_debug_html ("tag is not visible");
+                                                       }
+
+                                                       if (!bl->visible) {
+                                                               state = content_ignore;
+                                                       }
                                                }
                                        }
                                }
index abe38606ecbef1beecd150c111b954e9dbd1afe3..24ea0d3c773d83e3e88abe1c63acfb6d94032a0f 100644 (file)
@@ -34,7 +34,8 @@ enum html_component_type {
        RSPAMD_HTML_COMPONENT_CLASS,
        RSPAMD_HTML_COMPONENT_WIDTH,
        RSPAMD_HTML_COMPONENT_HEIGHT,
-       RSPAMD_HTML_COMPONENT_SIZE
+       RSPAMD_HTML_COMPONENT_SIZE,
+       RSPAMD_HTML_COMPONENT_OPACITY
 };
 
 struct html_tag_component {