]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Allow to add unigramm metatokens from Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 13 Apr 2017 17:01:07 +0000 (18:01 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 13 Apr 2017 17:01:07 +0000 (18:01 +0100)
src/libstat/stat_process.c

index 0272c8fb7dc751171d1c8e67fadd6e6615106cda..f5c19e664575c4f727a41dbbfc1fb3d30f190f2f 100644 (file)
@@ -203,7 +203,8 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx,
                                                /* Iterate over table of tables */
                                                for (lua_pushnil (L); lua_next (L, -2);
                                                                lua_pop (L, 1)) {
-                                                       elt.flags |= RSPAMD_STAT_TOKEN_FLAG_LUA_META;
+                                                       elt.flags = RSPAMD_STAT_TOKEN_FLAG_META|
+                                                                       RSPAMD_STAT_TOKEN_FLAG_LUA_META;
 
                                                        if (lua_isnumber (L, -1)) {
                                                                gdouble num = lua_tonumber (L, -1);
@@ -233,6 +234,30 @@ rspamd_stat_tokenize_parts_metadata (struct rspamd_stat_ctx *st_ctx,
                                                                elt.len = tlen;
                                                                g_array_append_val (ar, elt);
                                                        }
+                                                       else if (lua_istable (L, -1)) {
+                                                               /* Treat that as unigramms */
+                                                               for (lua_pushnil (L); lua_next (L, -2);
+                                                                               lua_pop (L, 1)) {
+                                                                       if (lua_isstring (L, -1)) {
+                                                                               const gchar *str;
+                                                                               gsize tlen;
+
+                                                                               str = lua_tolstring (L, -1, &tlen);
+                                                                               guint8 *pstr = rspamd_mempool_alloc (
+                                                                                               task->task_pool,
+                                                                                               tlen);
+                                                                               memcpy (pstr, str, tlen);
+
+                                                                               msg_debug_task ("got unigramm "
+                                                                                               "metatoken string: %*s",
+                                                                                               (gint) tlen, str);
+                                                                               elt.begin = (gchar *) pstr;
+                                                                               elt.len = tlen;
+                                                                               elt.flags |= RSPAMD_STAT_TOKEN_FLAG_UNIGRAM;
+                                                                               g_array_append_val (ar, elt);
+                                                                       }
+                                                               }
+                                                       }
                                                }
                                        }
                                }