From f5942ac84b940d370ab3a6c57fab7fd6454537ab Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 22 Jan 2018 20:47:27 +0000 Subject: [PATCH] [Minor] Add lua_textpart:get_languages method --- src/lua/lua_mimepart.c | 53 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 7fc8f74ac..d89acf80c 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -15,7 +15,8 @@ */ #include "lua_common.h" -#include "message.h" +#include "libmime/message.h" +#include "libmime/lang_detection.h" #include "libstat/stat_api.h" /* Textpart methods */ @@ -159,6 +160,14 @@ LUA_FUNCTION_DEF (textpart, get_html); * @return {string} short abbreviation (such as `ru`) for the script's language */ LUA_FUNCTION_DEF (textpart, get_language); +/*** + * @method text_part:get_languages() + * Returns array of tables of all languages detected for a part: + * - 'code': language code (short string) + * - 'prob': logarithm of probability + * @return {array|tables} all languages detected for the part + */ +LUA_FUNCTION_DEF (textpart, get_languages); /*** * @method text_part:get_mimepart() * Returns the mime part object corresponding to this text part @@ -183,6 +192,7 @@ static const struct luaL_reg textpartlib_m[] = { LUA_INTERFACE_DEF (textpart, is_html), LUA_INTERFACE_DEF (textpart, get_html), LUA_INTERFACE_DEF (textpart, get_language), + LUA_INTERFACE_DEF (textpart, get_languages), LUA_INTERFACE_DEF (textpart, get_mimepart), LUA_INTERFACE_DEF (textpart, get_stats), {"__tostring", rspamd_lua_class_tostring}, @@ -749,9 +759,48 @@ lua_textpart_get_language (lua_State * L) lua_pushstring (L, part->language); return 1; } + else { + lua_pushnil (L); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + +static gint +lua_textpart_get_languages (lua_State * L) +{ + struct rspamd_mime_text_part *part = lua_check_textpart (L); + guint i; + struct rspamd_lang_detector_res *cur; + + if (part != NULL) { + if (part->languages != NULL) { + lua_createtable (L, part->languages->len, 0); + + PTR_ARRAY_FOREACH (part->languages, i, cur) { + lua_createtable (L, 0, 2); + lua_pushstring (L, "code"); + lua_pushstring (L, cur->lang); + lua_settable (L, -3); + lua_pushstring (L, "prob"); + lua_pushnumber (L, cur->prob); + lua_settable (L, -3); + + lua_rawseti (L, -2, i + 1); + } + } + else { + lua_newtable (L); + } + } + else { + luaL_error (L, "invalid arguments"); } - lua_pushnil (L); return 1; } -- 2.39.5