]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to create rspamd_text from lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 11 Nov 2018 17:23:08 +0000 (17:23 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 11 Nov 2018 17:23:08 +0000 (17:23 +0000)
src/lua/lua_task.c

index 7aa4f3f1844c5a1f12ae736183badd75ebaab820..9aaf1742ed412544929ebb145f1cfd00fd4852a0 100644 (file)
@@ -1066,6 +1066,7 @@ static const struct luaL_reg archivelib_m[] = {
 };
 
 /* Blob methods */
+LUA_FUNCTION_DEF (text, fromstring);
 LUA_FUNCTION_DEF (text, len);
 LUA_FUNCTION_DEF (text, str);
 LUA_FUNCTION_DEF (text, ptr);
@@ -1073,6 +1074,11 @@ LUA_FUNCTION_DEF (text, save_in_file);
 LUA_FUNCTION_DEF (text, take_ownership);
 LUA_FUNCTION_DEF (text, gc);
 
+static const struct luaL_reg textlib_f[] = {
+       LUA_INTERFACE_DEF (text, fromstring),
+       {NULL, NULL}
+};
+
 static const struct luaL_reg textlib_m[] = {
        LUA_INTERFACE_DEF (text, len),
        LUA_INTERFACE_DEF (text, str),
@@ -5217,6 +5223,35 @@ lua_archive_get_filename (lua_State *L)
 }
 
 /* Text methods */
+static gint
+lua_text_fromstring (lua_State *L)
+{
+       LUA_TRACE_POINT;
+       const gchar *str;
+       gsize l = 0;
+       struct rspamd_lua_text *t, **pt;
+
+
+       str = luaL_checklstring (L, 1, &l);
+
+       if (str) {
+               t = g_malloc (sizeof (*t));
+               t->start = g_malloc (l + 1);
+               rspamd_strlcpy ((char *)t->start, str, l + 1);
+               t->len = l;
+               t->flags = RSPAMD_TEXT_FLAG_OWN;
+               pt = lua_newuserdata (L, sizeof (*pt));
+               *pt = t;
+               rspamd_lua_setclass (L, "rspamd{text}", -1);
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+
+       return 1;
+}
+
 static gint
 lua_text_len (lua_State *L)
 {
@@ -5382,6 +5417,16 @@ lua_load_task (lua_State * L)
        return 1;
 }
 
+static gint
+lua_load_text (lua_State * L)
+{
+       lua_newtable (L);
+       luaL_register (L, NULL, textlib_f);
+
+       return 1;
+}
+
+
 static void
 luaopen_archive (lua_State * L)
 {
@@ -5411,6 +5456,7 @@ void
 luaopen_text (lua_State *L)
 {
        rspamd_lua_new_class (L, "rspamd{text}", textlib_m);
+       rspamd_lua_add_preload (L, "rspamd_text", lua_load_text);
        lua_pop (L, 1);
 }