From 08f877d662302d9298917a02cf9488ec6f97cee9 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 4 Jul 2016 17:00:34 +0100 Subject: [PATCH] [Feature] Add new utility methods to mimepart object --- src/lua/lua_mimepart.c | 171 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 4 deletions(-) diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 933059603..a9f616458 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -27,8 +27,8 @@ rspamd_config.R_EMPTY_IMAGE = function (task) if parts then for _,part in ipairs(parts) do if part:is_empty() then - images = task:get_images() - if images then + texts = task:get_texts() + if texts then return true end return false @@ -209,6 +209,55 @@ LUA_FUNCTION_DEF (mimepart, get_type); * @return {string} filename or `nil` if no file is associated with this part */ LUA_FUNCTION_DEF (mimepart, get_filename); +/*** + * @method mime_part:is_image() + * Returns true if mime part is an image + * @return {bool} true if a part is an image + */ +LUA_FUNCTION_DEF (mimepart, is_image); +/*** + * @method mime_part:get_image() + * Returns rspamd_image structure associated with this part. This structure has + * the following methods: + * + * * `get_width` - return width of an image in pixels + * * `get_height` - return height of an image in pixels + * * `get_type` - return string representation of image's type (e.g. 'jpeg') + * * `get_filename` - return string with image's file name + * * `get_size` - return size in bytes + * @return {rspamd_image} image structure or nil if a part is not an image + */ +LUA_FUNCTION_DEF (mimepart, get_image); +/*** + * @method mime_part:is_archive() + * Returns true if mime part is an archive + * @return {bool} true if a part is an archive + */ +LUA_FUNCTION_DEF (mimepart, is_archive); +/*** + * @method mime_part:get_archive() + * Returns rspamd_archive structure associated with this part. This structure has + * the following methods: + * + * * `get_files` - return list of strings with filenames inside archive + * * `get_type` - return string representation of archive's type (e.g. 'zip') + * * `get_filename` - return string with archive's file name + * * `get_size` - return size in bytes + * @return {rspamd_archive} archive structure or nil if a part is not an archive + */ +LUA_FUNCTION_DEF (mimepart, get_archive); +/*** + * @method mime_part:is_text() + * Returns true if mime part is a text part + * @return {bool} true if a part is a text part + */ +LUA_FUNCTION_DEF (mimepart, is_text); +/*** + * @method mime_part:get_text() + * Returns rspamd_textpart structure associated with this part. + * @return {rspamd_textpart} textpart structure or nil if a part is not an text + */ +LUA_FUNCTION_DEF (mimepart, get_text); static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, get_content), @@ -218,6 +267,12 @@ static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, get_header), LUA_INTERFACE_DEF (mimepart, get_header_raw), LUA_INTERFACE_DEF (mimepart, get_header_full), + LUA_INTERFACE_DEF (mimepart, is_image), + LUA_INTERFACE_DEF (mimepart, get_image), + LUA_INTERFACE_DEF (mimepart, is_archive), + LUA_INTERFACE_DEF (mimepart, get_archive), + LUA_INTERFACE_DEF (mimepart, is_text), + LUA_INTERFACE_DEF (mimepart, get_text), {"__tostring", rspamd_lua_class_tostring}, {NULL, NULL} }; @@ -550,17 +605,125 @@ lua_mimepart_get_header_raw (lua_State * L) return lua_mimepart_get_header_common (L, FALSE, TRUE); } +static gint +lua_mimepart_is_image (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + lua_pushboolean (L, (part->flags & RSPAMD_MIME_PART_IMAGE) ? true : false); + + return 1; +} + +static gint +lua_mimepart_is_archive (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + lua_pushboolean (L, (part->flags & RSPAMD_MIME_PART_ARCHIVE) ? true : false); + + return 1; +} + +static gint +lua_mimepart_is_text (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + lua_pushboolean (L, (part->flags & RSPAMD_MIME_PART_TEXT) ? true : false); + + return 1; +} + +static gint +lua_mimepart_get_image (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_image **pimg; + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + if (!(part->flags & RSPAMD_MIME_PART_IMAGE)) { + lua_pushnil (L); + } + else { + pimg = lua_newuserdata (L, sizeof (*pimg)); + *pimg = part->specific_data; + rspamd_lua_setclass (L, "rspamd{image}", -1); + } + + return 1; +} + +static gint +lua_mimepart_get_archive (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_archive **parch; + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + if (!(part->flags & RSPAMD_MIME_PART_ARCHIVE)) { + lua_pushnil (L); + } + else { + parch = lua_newuserdata (L, sizeof (*parch)); + *parch = part->specific_data; + rspamd_lua_setclass (L, "rspamd{archive}", -1); + } + + return 1; +} + +static gint +lua_mimepart_get_text (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_text_part **ppart; + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + if (!(part->flags & RSPAMD_MIME_PART_TEXT)) { + lua_pushnil (L); + } + else { + ppart = lua_newuserdata (L, sizeof (*ppart)); + *ppart = part->specific_data; + rspamd_lua_setclass (L, "rspamd{textpart}", -1); + } + + return 1; +} + void luaopen_textpart (lua_State * L) { rspamd_lua_new_class (L, "rspamd{textpart}", textpartlib_m); - lua_pop (L, 1); /* remove metatable from stack */ + lua_pop (L, 1); } void luaopen_mimepart (lua_State * L) { rspamd_lua_new_class (L, "rspamd{mimepart}", mimepartlib_m); - lua_pop (L, 1); /* remove metatable from stack */ + lua_pop (L, 1); } -- 2.39.5