From a569705d3057b09684ffda4452d2bf6101d2d9f3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 12 Jul 2011 21:13:10 +0400 Subject: [PATCH] * Add ability to get difference between two parts from lua code --- src/fuzzy.c | 5 +++++ src/lua/lua_task.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/fuzzy.c b/src/fuzzy.c index cdc9922cc..5a2decb34 100644 --- a/src/fuzzy.c +++ b/src/fuzzy.c @@ -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]; diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 8c90feb4b..4916a50ad 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -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) -- 2.39.5