aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_task.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-04-26 15:24:00 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-04-26 15:24:00 +0100
commitfa7dcbab1fb25fe90048a73c62cd23a918a55805 (patch)
treea45b48524c948a5a33422e09b74b82d35e794352 /src/lua/lua_task.c
parentb7bc313a9db19a9ef13d841483715ad316670006 (diff)
downloadrspamd-fa7dcbab1fb25fe90048a73c62cd23a918a55805.tar.gz
rspamd-fa7dcbab1fb25fe90048a73c62cd23a918a55805.zip
[Minor] Move rspamd_text to a sepatate unit
Diffstat (limited to 'src/lua/lua_task.c')
-rw-r--r--src/lua/lua_task.c319
1 files changed, 0 insertions, 319 deletions
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 50e35434c..945e06e08 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -1185,35 +1185,6 @@ static const struct luaL_reg archivelib_m[] = {
{NULL, NULL}
};
-/* Blob methods */
-LUA_FUNCTION_DEF (text, fromstring);
-LUA_FUNCTION_DEF (text, fromtable);
-LUA_FUNCTION_DEF (text, len);
-LUA_FUNCTION_DEF (text, str);
-LUA_FUNCTION_DEF (text, ptr);
-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),
- LUA_INTERFACE_DEF (text, fromtable),
- {NULL, NULL}
-};
-
-static const struct luaL_reg textlib_m[] = {
- LUA_INTERFACE_DEF (text, len),
- LUA_INTERFACE_DEF (text, str),
- LUA_INTERFACE_DEF (text, ptr),
- LUA_INTERFACE_DEF (text, take_ownership),
- LUA_INTERFACE_DEF (text, save_in_file),
- {"write", lua_text_save_in_file},
- {"__len", lua_text_len},
- {"__tostring", lua_text_str},
- {"__gc", lua_text_gc},
- {NULL, NULL}
-};
-
/* Utility functions */
struct rspamd_task *
lua_check_task (lua_State * L, gint pos)
@@ -5978,277 +5949,6 @@ lua_archive_get_filename (lua_State *L)
return 1;
}
-/* Text methods */
-static gint
-lua_text_fromstring (lua_State *L)
-{
- LUA_TRACE_POINT;
- const gchar *str;
- gsize l = 0;
- struct rspamd_lua_text *t;
-
- str = luaL_checklstring (L, 1, &l);
-
- if (str) {
- t = lua_newuserdata (L, 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;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
-
- return 1;
-}
-
-static gint
-lua_text_fromtable (lua_State *L)
-{
- LUA_TRACE_POINT;
- const gchar *delim = "", *st;
- struct rspamd_lua_text *t, *elt;
- gsize textlen = 0, dlen, stlen, tblen;
- gchar *dest;
-
- if (!lua_istable (L, 1)) {
- return luaL_error (L, "invalid arguments");
- }
-
- if (lua_type (L, 2) == LUA_TSTRING) {
- delim = lua_tolstring (L, 2, &dlen);
- }
- else {
- dlen = strlen (delim);
- }
-
- /* Calculate length needed */
- tblen = rspamd_lua_table_size (L, 1);
-
- for (guint i = 0; i < tblen; i ++) {
- lua_rawgeti (L, 1, i + 1);
-
- if (lua_type (L, -1) == LUA_TSTRING) {
-#if LUA_VERSION_NUM >= 502
- stlen = lua_rawlen (L, -1);
-#else
- stlen = lua_objlen (L, -1);
-#endif
- textlen += stlen;
- }
- else {
- elt = lua_check_text (L, -1);
-
- if (elt) {
- textlen += elt->len;
- }
- }
-
- lua_pop (L, 1);
- textlen += dlen;
- }
-
- /* Allocate new text */
- t = lua_newuserdata (L, sizeof (*t));
- dest = g_malloc (textlen);
- t->start = dest;
- t->len = textlen;
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
-
- for (guint i = 0; i < tblen; i ++) {
- lua_rawgeti (L, 1, i + 1);
-
- if (lua_type (L, -1) == LUA_TSTRING) {
- st = lua_tolstring (L, -1, &stlen);
- memcpy (dest, st, stlen);
- dest += stlen;
- }
- else {
- elt = lua_check_text (L, -1);
-
- if (elt) {
- memcpy (dest, elt->start, elt->len);
- }
- }
-
- memcpy (dest, delim, dlen);
- lua_pop (L, 1);
- }
-
- return 1;
-}
-
-static gint
-lua_text_len (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
- gsize l = 0;
-
- if (t != NULL) {
- l = t->len;
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- lua_pushinteger (L, l);
-
- return 1;
-}
-
-static gint
-lua_text_str (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
-
- if (t != NULL) {
- lua_pushlstring (L, t->start, t->len);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
-}
-
-static gint
-lua_text_ptr (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
-
- if (t != NULL) {
- lua_pushlightuserdata (L, (gpointer)t->start);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
-}
-
-static gint
-lua_text_take_ownership (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
- gchar *dest;
-
- if (t != NULL) {
- if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
- /* We already own it */
- lua_pushboolean (L, true);
- }
- else {
- dest = g_malloc (t->len);
- memcpy (dest, t->start, t->len);
- t->start = dest;
- t->flags |= RSPAMD_TEXT_FLAG_OWN;
- lua_pushboolean (L, true);
- }
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
-}
-
-static gint
-lua_text_save_in_file (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
- const gchar *fname = NULL;
- guint mode = 00644;
- gint fd = -1;
- gboolean need_close = FALSE;
-
- if (t != NULL) {
- if (lua_type (L, 2) == LUA_TSTRING) {
- fname = luaL_checkstring (L, 2);
-
- if (lua_type (L, 3) == LUA_TNUMBER) {
- mode = lua_tonumber (L, 3);
- }
- }
- else if (lua_type (L, 2) == LUA_TNUMBER) {
- /* Created fd */
- fd = lua_tonumber (L, 2);
- }
-
- if (fd == -1) {
- if (fname) {
- fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0);
-
- if (fd == -1) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
- need_close = TRUE;
- }
- else {
- fd = STDOUT_FILENO;
- }
- }
-
- if (write (fd, t->start, t->len) == -1) {
- if (fd != STDOUT_FILENO) {
- close (fd);
- }
-
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- if (need_close) {
- close (fd);
- }
-
- lua_pushboolean (L, true);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
-}
-
-static gint
-lua_text_gc (lua_State *L)
-{
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = lua_check_text (L, 1);
-
- if (t != NULL) {
- if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
- if (t->flags & RSPAMD_TEXT_FLAG_WIPE) {
- rspamd_explicit_memzero ((guchar *)t->start, t->len);
- }
-
- if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) {
- munmap ((gpointer)t->start, t->len);
- }
- else {
- g_free ((gpointer)t->start);
- }
- }
-
- }
-
- return 0;
-}
-
/* Init part */
static gint
@@ -6260,16 +5960,6 @@ 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)
{
@@ -6296,15 +5986,6 @@ luaopen_image (lua_State * L)
}
void
-luaopen_text (lua_State *L)
-{
- rspamd_lua_new_class (L, "rspamd{text}", textlib_m);
- lua_pop (L, 1);
-
- rspamd_lua_add_preload (L, "rspamd_text", lua_load_text);
-}
-
-void
rspamd_lua_task_push (lua_State *L, struct rspamd_task *task)
{
struct rspamd_task **ptask;