summaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-02-22 17:15:18 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-02-22 17:15:18 +0000
commit2b6680df33ad709f82b05f64ada70ec6d4193d74 (patch)
treefb0aaeaf1a4e430fc5b733b123a17ce97d24cbf5 /src/lua
parente9df5fc9c75ebc8bdd352e94101e6103c90972a7 (diff)
downloadrspamd-2b6680df33ad709f82b05f64ada70ec6d4193d74.tar.gz
rspamd-2b6680df33ad709f82b05f64ada70ec6d4193d74.zip
[Minor] Add ability to save rspamd_text in file
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_task.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index adbb36adb..dd8495d8a 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -843,12 +843,14 @@ static const struct luaL_reg archivelib_m[] = {
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, gc);
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, save_in_file),
{"__len", lua_text_len},
{"__tostring", lua_text_str},
{"__gc", lua_text_gc},
@@ -3770,6 +3772,46 @@ lua_text_ptr (lua_State *L)
}
static gint
+lua_text_save_in_file (lua_State *L)
+{
+ struct rspamd_lua_text *t = lua_check_text (L, 1);
+ const gchar *fname = luaL_checkstring (L, 2);
+ guint mode = 00644;
+ gint fd;
+
+ if (t != NULL && fname != NULL) {
+ if (lua_type (L, 3) == LUA_TNUMBER) {
+ mode = lua_tonumber (L, 3);
+ }
+
+ fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode);
+
+ if (fd == -1) {
+ lua_pushboolean (L, false);
+ lua_pushstring (L, strerror (errno));
+
+ return 2;
+ }
+
+ if (write (fd, t->start, t->len) == -1) {
+ close (fd);
+ lua_pushboolean (L, false);
+ lua_pushstring (L, strerror (errno));
+
+ return 2;
+ }
+
+ close (fd);
+ lua_pushboolean (L, true);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
+static gint
lua_text_gc (lua_State *L)
{
struct rspamd_lua_text *t = lua_check_text (L, 1);