aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-17 20:01:05 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-17 20:01:05 +0100
commit60ec657d33880f7ade7cee0767e9162a132f3445 (patch)
tree125c94511325319946fe4692a4693fd06ab2d078 /src/lua
parent9af0afa4ccf81d0edff48ca44fed217244df8ea9 (diff)
downloadrspamd-60ec657d33880f7ade7cee0767e9162a132f3445.tar.gz
rspamd-60ec657d33880f7ade7cee0767e9162a132f3445.zip
[Minor] Allow to get words from text parts
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_mimepart.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c
index 28ef57f6a..cce78ff3a 100644
--- a/src/lua/lua_mimepart.c
+++ b/src/lua/lua_mimepart.c
@@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#include "lua_common.h"
#include "message.h"
+#include "libstat/stat_api.h"
/* Textpart methods */
/***
@@ -125,6 +127,14 @@ LUA_FUNCTION_DEF (textpart, get_stats);
* @return {integer} number of words in the part
*/
LUA_FUNCTION_DEF (textpart, get_words_count);
+
+/***
+ * @method mime_part:get_words()
+ * Get words in the part
+ * @return {table/strings} words in the part
+ */
+LUA_FUNCTION_DEF (textpart, get_words);
+
/***
* @method text_part:is_empty()
* Returns `true` if the specified part is empty
@@ -168,6 +178,7 @@ static const struct luaL_reg textpartlib_m[] = {
LUA_INTERFACE_DEF (textpart, get_urls_length),
LUA_INTERFACE_DEF (textpart, get_lines_count),
LUA_INTERFACE_DEF (textpart, get_words_count),
+ LUA_INTERFACE_DEF (textpart, get_words),
LUA_INTERFACE_DEF (textpart, is_empty),
LUA_INTERFACE_DEF (textpart, is_html),
LUA_INTERFACE_DEF (textpart, get_html),
@@ -653,6 +664,34 @@ lua_textpart_get_words_count (lua_State *L)
}
static gint
+lua_textpart_get_words (lua_State *L)
+{
+ struct rspamd_mime_text_part *part = lua_check_textpart (L);
+ rspamd_stat_token_t *w;
+ guint i;
+
+ if (part == NULL) {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ if (IS_PART_EMPTY (part) || part->normalized_words == NULL) {
+ lua_createtable (L, 0, 0);
+ }
+ else {
+ lua_createtable (L, part->normalized_words->len, 0);
+
+ for (i = 0; i < part->normalized_words->len; i ++) {
+ w = &g_array_index (part->normalized_words, rspamd_stat_token_t, i);
+
+ lua_pushlstring (L, w->begin, w->len);
+ lua_rawseti (L, -2, i + 1);
+ }
+ }
+
+ return 1;
+}
+
+static gint
lua_textpart_is_empty (lua_State * L)
{
struct rspamd_mime_text_part *part = lua_check_textpart (L);