diff options
Diffstat (limited to 'src/libmime/mime_expressions.c')
-rw-r--r-- | src/libmime/mime_expressions.c | 262 |
1 files changed, 98 insertions, 164 deletions
diff --git a/src/libmime/mime_expressions.c b/src/libmime/mime_expressions.c index fd7e2e216..592667346 100644 --- a/src/libmime/mime_expressions.c +++ b/src/libmime/mime_expressions.c @@ -790,6 +790,7 @@ rspamd_mime_expr_process_regexp (struct rspamd_regexp_atom *re, const gchar *in; gint ret = 0; GList *cur, *headerlist; + guint i; rspamd_regexp_t *regexp; struct url_regexp_param callback_param = { .task = task, @@ -888,12 +889,11 @@ rspamd_mime_expr_process_regexp (struct rspamd_regexp_atom *re, break; case REGEXP_MIME: /* Iterate throught text parts */ - cur = g_list_first (task->text_parts); - while (cur) { - part = (struct mime_text_part *)cur->data; + for (i = 0; i < task->text_parts->len; i ++) { + part = g_ptr_array_index (task->text_parts, i); + /* Skip empty parts */ if (IS_PART_EMPTY (part)) { - cur = g_list_next (cur); continue; } @@ -918,11 +918,8 @@ rspamd_mime_expr_process_regexp (struct rspamd_regexp_atom *re, if (!re->is_multiple && ret) { break; } - - cur = g_list_next (cur); } - break; case REGEXP_MESSAGE: raw = TRUE; @@ -1179,7 +1176,6 @@ rspamd_parts_distance (struct rspamd_task * task, GArray * args, void *unused) { gint threshold, threshold2 = -1, diff; struct mime_text_part *p1, *p2; - GList *cur; struct expression_argument *arg; GMimeObject *parent; const GMimeContentType *ct; @@ -1244,18 +1240,12 @@ rspamd_parts_distance (struct rspamd_task * task, GArray * args, void *unused) } } - if (g_list_length (task->text_parts) == 2) { - cur = g_list_first (task->text_parts); - p1 = cur->data; - cur = g_list_next (cur); + if (task->text_parts->len == 2) { + p1 = g_ptr_array_index (task->text_parts, 0); + p2 = g_ptr_array_index (task->text_parts, 1); pdiff = rspamd_mempool_alloc (task->task_pool, sizeof (gint)); *pdiff = -1; - if (cur == NULL) { - msg_info ("bad parts list"); - return FALSE; - } - p2 = cur->data; /* First of all check parent object */ if (p1->parent && p1->parent == p2->parent) { parent = p1->parent; @@ -1462,20 +1452,17 @@ rspamd_has_only_html_part (struct rspamd_task * task, GArray * args, void *unused) { struct mime_text_part *p; - GList *cur; gboolean res = FALSE; - cur = g_list_first (task->text_parts); - while (cur) { - p = cur->data; + if (task->text_parts->len == 1) { + p = g_ptr_array_index (task->text_parts, 0); + if (IS_PART_HTML (p)) { res = TRUE; } else { res = FALSE; - break; } - cur = g_list_next (cur); } return res; @@ -1633,12 +1620,12 @@ gboolean rspamd_is_html_balanced (struct rspamd_task * task, GArray * args, void *unused) { struct mime_text_part *p; - GList *cur; + guint i; gboolean res = TRUE; - cur = g_list_first (task->text_parts); - while (cur) { - p = cur->data; + for (i = 0; i < task->text_parts->len; i ++) { + + p = g_ptr_array_index (task->text_parts, i); if (!IS_PART_EMPTY (p) && IS_PART_HTML (p)) { if (p->flags & RSPAMD_MIME_PART_FLAG_BALANCED) { res = TRUE; @@ -1648,7 +1635,6 @@ rspamd_is_html_balanced (struct rspamd_task * task, GArray * args, void *unused) break; } } - cur = g_list_next (cur); } return res; @@ -1681,9 +1667,9 @@ gboolean rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) { struct mime_text_part *p; - GList *cur; struct expression_argument *arg; struct html_tag *tag; + guint i; gboolean res = FALSE; struct html_callback_data cd; @@ -1705,12 +1691,12 @@ rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) return FALSE; } - cur = g_list_first (task->text_parts); cd.res = &res; cd.tag = tag; - while (cur && res == FALSE) { - p = cur->data; + for (i = 0; i < task->text_parts->len && res; i ++) { + p = g_ptr_array_index (task->text_parts, i); + if (!IS_PART_EMPTY (p) && IS_PART_HTML (p) && p->html_nodes) { g_node_traverse (p->html_nodes, G_PRE_ORDER, @@ -1719,7 +1705,6 @@ rspamd_has_html_tag (struct rspamd_task * task, GArray * args, void *unused) search_html_node_callback, &cd); } - cur = g_list_next (cur); } return res; @@ -1730,17 +1715,15 @@ gboolean rspamd_has_fake_html (struct rspamd_task * task, GArray * args, void *unused) { struct mime_text_part *p; - GList *cur; + guint i; gboolean res = FALSE; - cur = g_list_first (task->text_parts); + for (i = 0; i < task->text_parts->len && res; i ++) { + p = g_ptr_array_index (task->text_parts, i); - while (cur && res == FALSE) { - p = cur->data; if (!IS_PART_EMPTY (p) && IS_PART_HTML (p) && p->html_nodes == NULL) { res = TRUE; } - cur = g_list_next (cur); } return res; @@ -1921,8 +1904,8 @@ rspamd_content_type_compare_param (struct rspamd_task * task, GMimeObject *part; GMimeContentType *ct; gint r; + guint i; gboolean recursive = FALSE, result = FALSE; - GList *cur = NULL; struct mime_part *cur_part; if (args == NULL || args->len < 2) { @@ -1935,14 +1918,15 @@ rspamd_content_type_compare_param (struct rspamd_task * task, param_name = arg->data; arg_pattern = &g_array_index (args, struct expression_argument, 1); - - part = g_mime_message_get_mime_part (task->message); - if (part) { + for (i = 0; i < task->text_parts->len; i ++) { + cur_part = g_ptr_array_index (task->text_parts, i); + part = cur_part->mime; ct = (GMimeContentType *)g_mime_object_get_content_type (part); + if (args->len >= 3) { arg1 = &g_array_index (args, struct expression_argument, 2); if (g_ascii_strncasecmp (arg1->data, "true", - sizeof ("true") - 1) == 0) { + sizeof ("true") - 1) == 0) { recursive = TRUE; } } @@ -1955,55 +1939,38 @@ rspamd_content_type_compare_param (struct rspamd_task * task, recursive = TRUE; } } - - if (recursive) { - cur = task->parts; - } - #ifndef GMIME24 g_object_unref (part); #endif - for (;; ) { - if ((param_data = + + if ((param_data = g_mime_content_type_get_parameter ((GMimeContentType *)ct, - param_name)) == NULL) { - result = FALSE; - } - else { - if (arg_pattern->type == EXPRESSION_ARGUMENT_REGEXP) { - re = arg_pattern->data; - - if ((r = rspamd_task_re_cache_check (task, - rspamd_regexp_get_pattern (re))) == -1) { - r = rspamd_regexp_search (re, param_data, 0, - NULL, NULL, FALSE, NULL); - rspamd_task_re_cache_add (task, - rspamd_regexp_get_pattern (re), r); - } - } - else { - /* Just do strcasecmp */ - if (g_ascii_strcasecmp (param_data, arg_pattern->data) == 0) { - return TRUE; - } - } - } - /* Get next part */ - if (!recursive) { - return result; - } - else if (cur != NULL) { - cur_part = cur->data; - if (cur_part->type != NULL) { - ct = cur_part->type; + param_name)) == NULL) { + result = FALSE; + } + else { + if (arg_pattern->type == EXPRESSION_ARGUMENT_REGEXP) { + re = arg_pattern->data; + + if ((r = rspamd_task_re_cache_check (task, + rspamd_regexp_get_pattern (re))) == -1) { + r = rspamd_regexp_search (re, param_data, 0, + NULL, NULL, FALSE, NULL); + rspamd_task_re_cache_add (task, + rspamd_regexp_get_pattern (re), r); } - cur = g_list_next (cur); } else { - /* All is done */ - return result; + /* Just do strcasecmp */ + if (g_ascii_strcasecmp (param_data, arg_pattern->data) == 0) { + return TRUE; + } } } + /* Get next part */ + if (!recursive) { + break; + } } return FALSE; @@ -2020,7 +1987,7 @@ rspamd_content_type_has_param (struct rspamd_task * task, GMimeObject *part; GMimeContentType *ct; gboolean recursive = FALSE, result = FALSE; - GList *cur = NULL; + guint i; struct mime_part *cur_part; if (args == NULL || args->len < 1) { @@ -2032,9 +1999,11 @@ rspamd_content_type_has_param (struct rspamd_task * task, g_assert (arg->type == EXPRESSION_ARGUMENT_NORMAL); param_name = arg->data; - part = g_mime_message_get_mime_part (task->message); - if (part) { + for (i = 0; i < task->text_parts->len; i ++) { + cur_part = g_ptr_array_index (task->text_parts, i); + part = cur_part->mime; ct = (GMimeContentType *)g_mime_object_get_content_type (part); + if (args->len >= 2) { arg1 = &g_array_index (args, struct expression_argument, 2); if (g_ascii_strncasecmp (arg1->data, "true", @@ -2052,39 +2021,21 @@ rspamd_content_type_has_param (struct rspamd_task * task, } } - if (recursive) { - cur = task->parts; - } - #ifndef GMIME24 g_object_unref (part); #endif - for (;; ) { - if ((param_data = + if ((param_data = g_mime_content_type_get_parameter ((GMimeContentType *)ct, - param_name)) != NULL) { - return TRUE; - } - /* Get next part */ - if (!recursive) { - return result; - } - else if (cur != NULL) { - cur_part = cur->data; - if (cur_part->type != NULL) { - ct = cur_part->type; - } - cur = g_list_next (cur); - } - else { - /* All is done */ - return result; - } + param_name)) != NULL) { + return TRUE; + } + /* Get next part */ + if (!recursive) { + break; } - } - return TRUE; + return result; } static gboolean @@ -2098,19 +2049,22 @@ rspamd_content_type_check (struct rspamd_task *task, GMimeObject *part; GMimeContentType *ct; gint r; - gboolean recursive = FALSE, result = FALSE; - GList *cur = NULL; + guint i; + gboolean recursive = FALSE; struct mime_part *cur_part; - if (args == NULL) { + if (args == NULL || args->len < 1) { msg_warn ("no parameters to function"); return FALSE; } + arg_pattern = &g_array_index (args, struct expression_argument, 0); - part = g_mime_message_get_mime_part (task->message); - if (part) { + for (i = 0; i < task->text_parts->len; i ++) { + cur_part = g_ptr_array_index (task->text_parts, i); + part = cur_part->mime; ct = (GMimeContentType *)g_mime_object_get_content_type (part); + if (args->len >= 2) { arg1 = &g_array_index (args, struct expression_argument, 1); if (g_ascii_strncasecmp (arg1->data, "true", @@ -2128,56 +2082,38 @@ rspamd_content_type_check (struct rspamd_task *task, } } - if (recursive) { - cur = task->parts; - } - #ifndef GMIME24 g_object_unref (part); #endif - for (;;) { - - if (check_subtype) { - param_data = ct->subtype; - } - else { - param_data = ct->type; - } + if (check_subtype) { + param_data = ct->subtype; + } + else { + param_data = ct->type; + } - if (arg_pattern->type == EXPRESSION_ARGUMENT_REGEXP) { - re = arg_pattern->data; + if (arg_pattern->type == EXPRESSION_ARGUMENT_REGEXP) { + re = arg_pattern->data; - if ((r = rspamd_task_re_cache_check (task, - rspamd_regexp_get_pattern (re))) == -1) { - r = rspamd_regexp_search (re, param_data, 0, - NULL, NULL, FALSE, NULL); - rspamd_task_re_cache_add (task, - rspamd_regexp_get_pattern (re), r); - } - } - else { - /* Just do strcasecmp */ - if (g_ascii_strcasecmp (param_data, arg_pattern->data) == 0) { - return TRUE; - } - } - /* Get next part */ - if (!recursive) { - return result; - } - else if (cur != NULL) { - cur_part = cur->data; - if (cur_part->type != NULL) { - ct = cur_part->type; - } - cur = g_list_next (cur); + if ((r = rspamd_task_re_cache_check (task, + rspamd_regexp_get_pattern (re))) == -1) { + r = rspamd_regexp_search (re, param_data, 0, + NULL, NULL, FALSE, NULL); + rspamd_task_re_cache_add (task, + rspamd_regexp_get_pattern (re), r); } - else { - /* All is done */ - return result; + } + else { + /* Just do strcasecmp */ + if (g_ascii_strcasecmp (param_data, arg_pattern->data) == 0) { + return TRUE; } } + /* Get next part */ + if (!recursive) { + break; + } } return FALSE; @@ -2258,16 +2194,15 @@ common_has_content_part (struct rspamd_task * task, { rspamd_regexp_t *re; struct mime_part *part; - GList *cur; GMimeContentType *ct; gint r; + guint i; - cur = g_list_first (task->parts); - while (cur) { - part = cur->data; + for (i = 0; i < task->text_parts->len; i ++) { + part = g_ptr_array_index (task->text_parts, i); ct = part->type; + if (ct == NULL) { - cur = g_list_next (cur); continue; } @@ -2304,7 +2239,6 @@ common_has_content_part (struct rspamd_task * task, } } } - cur = g_list_next (cur); } return FALSE; |