]> source.dussan.org Git - rspamd.git/commitdiff
* Add ability to get difference between two parts from lua code
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 12 Jul 2011 17:13:10 +0000 (21:13 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 12 Jul 2011 17:13:10 +0000 (21:13 +0400)
src/fuzzy.c
src/lua/lua_task.c

index cdc9922cc27b6a735d08f4acef316be1e34b61da..5a2decb345bb487e0ba7e99d65b33e3f5b06e806 100644 (file)
@@ -321,6 +321,7 @@ fuzzy_init_part (struct mime_text_part *part, memory_pool_t *pool)
        GList                          *cur_offset;
        struct process_exception       *cur_ex = NULL;
        gunichar                        uc;
+       GString *debug;
 
        cur_offset = part->urls_offset;
        if (cur_offset != NULL) {
@@ -370,6 +371,7 @@ fuzzy_init_part (struct mime_text_part *part, memory_pool_t *pool)
                }
        }
 
+       debug = g_string_sized_new (real_len);
        new->block_size = fuzzy_blocksize (real_len);
        new2->block_size = new->block_size * 2;
 
@@ -395,6 +397,7 @@ fuzzy_init_part (struct mime_text_part *part, memory_pool_t *pool)
                                uc = g_utf8_get_char (c);
                                if (g_unichar_isalnum (uc)) {
                                        fuzzy_update2 (new, new2, uc);
+                                       g_string_append_unichar (debug, uc);
                                }
                                c = g_utf8_next_char (c);
                        }
@@ -412,11 +415,13 @@ fuzzy_init_part (struct mime_text_part *part, memory_pool_t *pool)
                        else {
                                if (!g_ascii_isspace (*c) && !g_ascii_ispunct (*c)) {
                                        fuzzy_update2 (new, new2, *c);
+                                       g_string_append_c (debug, *c);
                                }
                                c++;
                        }
                }
        }
+       msg_info ("make hash of string: %v", debug);
        /* Check whether we have more bytes in a rolling window */
        if (new->rh != 0) {
                new->hash_pipe[new->hi] = b64[new->h % 64];
index 8c90feb4b88555b9cd6c9505e65ec0aee0b0c46f..4916a50ad18cddc9c0aa6abd9379b581ad2b2418 100644 (file)
@@ -110,6 +110,7 @@ LUA_FUNCTION_DEF (textpart, is_empty);
 LUA_FUNCTION_DEF (textpart, is_html);
 LUA_FUNCTION_DEF (textpart, get_fuzzy);
 LUA_FUNCTION_DEF (textpart, get_language);
+LUA_FUNCTION_DEF (textpart, compare_distance);
 
 static const struct luaL_reg    textpartlib_m[] = {
        LUA_INTERFACE_DEF (textpart, get_content),
@@ -117,6 +118,7 @@ static const struct luaL_reg    textpartlib_m[] = {
        LUA_INTERFACE_DEF (textpart, is_html),
        LUA_INTERFACE_DEF (textpart, get_fuzzy),
        LUA_INTERFACE_DEF (textpart, get_language),
+       LUA_INTERFACE_DEF (textpart, compare_distance),
        {"__tostring", lua_class_tostring},
        {NULL, NULL}
 };
@@ -1341,6 +1343,49 @@ lua_textpart_get_language (lua_State * L)
        return 1;
 }
 
+static gint
+lua_textpart_compare_distance (lua_State * L)
+{
+       struct mime_text_part          *part = lua_check_textpart (L), *other;
+       void                           *ud = luaL_checkudata (L, 2, "rspamd{textpart}");
+       gint                            diff = -1;
+       GMimeObject                    *parent;
+       const GMimeContentType         *ct;
+
+       luaL_argcheck (L, ud != NULL, 2, "'textpart' expected");
+       other = *((struct mime_text_part **)ud);
+
+       if (part->parent && part->parent == other->parent) {
+               parent = part->parent;
+               ct = g_mime_object_get_content_type (parent);
+#ifndef GMIME24
+               if (ct == NULL || ! g_mime_content_type_is_type (ct, "multipart", "alternative")) {
+#else
+               if (ct == NULL || ! g_mime_content_type_is_type ((GMimeContentType *)ct, "multipart", "alternative")) {
+#endif
+                       diff = -1;
+
+               }
+               else {
+                       if (!part->is_empty && !other->is_empty) {
+                               diff = fuzzy_compare_parts (part, other);
+                       }
+                       else if ((part->is_empty && !other->is_empty) || (!part->is_empty && other->is_empty)) {
+                               /* Empty and non empty parts are different */
+                               diff = 0;
+                       }
+               }
+       }
+       else {
+                       diff = -1;
+       }
+
+
+       lua_pushinteger (L, diff);
+
+       return 1;
+}
+
 /* Image functions */
 static gint
 lua_image_get_width (lua_State *L)