From 159c71b504e538f0e8135172129dd5acdce7880b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 21 Jun 2017 19:06:01 +0100 Subject: [Feature] Add text_part:get_stats function --- src/lua/lua_mimepart.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/lua/lua_mimepart.c') diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index d83125044..75b270189 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -107,6 +107,18 @@ LUA_FUNCTION_DEF (textpart, get_urls_length); * @return {integer} number of lines in the part */ LUA_FUNCTION_DEF (textpart, get_lines_count); +/*** + * @method mime_part:get_stats() + * Returns a table with the following data: + * - `lines`: number of lines + * - `spaces`: number of spaces + * - `double_spaces`: double spaces + * - `empty_lines`: number of empty lines + * - `non_ascii_characters`: number of non ascii characters + * - `ascii_characters`: number of ascii characters + * @return {table} table of stats + */ +LUA_FUNCTION_DEF (textpart, get_stats); /*** * @method mime_part:get_words_count() * Get words number in the part @@ -161,6 +173,7 @@ static const struct luaL_reg textpartlib_m[] = { LUA_INTERFACE_DEF (textpart, get_html), LUA_INTERFACE_DEF (textpart, get_language), LUA_INTERFACE_DEF (textpart, get_mimepart), + LUA_INTERFACE_DEF (textpart, get_stats), {"__tostring", rspamd_lua_class_tostring}, {NULL, NULL} }; @@ -715,6 +728,55 @@ lua_textpart_get_mimepart (lua_State * L) return 1; } +/*** + * @method mime_part:get_stats() + * Returns a table with the following data: + * - + * - `lines`: number of lines + * - `spaces`: number of spaces + * - `double_spaces`: double spaces + * - `empty_lines`: number of empty lines + * - `non_ascii_characters`: number of non ascii characters + * - `ascii_characters`: number of ascii characters + * @return {table} table of stats + */ +static gint +lua_textpart_get_stats (lua_State * L) +{ + struct rspamd_mime_text_part *part = lua_check_textpart (L); + + if (part != NULL) { + lua_createtable (L, 0, 7); + + lua_pushstring (L, "lines"); + lua_pushnumber (L, part->nlines); + lua_settable (L, -3); + lua_pushstring (L, "empty_lines"); + lua_pushnumber (L, part->empty_lines); + lua_settable (L, -3); + lua_pushstring (L, "spaces"); + lua_pushnumber (L, part->spaces); + lua_settable (L, -3); + lua_pushstring (L, "non_spaces"); + lua_pushnumber (L, part->non_spaces); + lua_settable (L, -3); + lua_pushstring (L, "double_spaces"); + lua_pushnumber (L, part->double_spaces); + lua_settable (L, -3); + lua_pushstring (L, "ascii_characters"); + lua_pushnumber (L, part->ascii_chars); + lua_settable (L, -3); + lua_pushstring (L, "non_ascii_characters"); + lua_pushnumber (L, part->non_ascii_chars); + lua_settable (L, -3); + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + /* Mimepart implementation */ static gint -- cgit v1.2.3