From 5cc8d34451974d69d87e7c86a57476197bb192ef Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 15 May 2018 11:31:25 +0100 Subject: [PATCH] [Feature] Allow to get multipart children in Lua --- src/libmime/content_type.h | 6 ++-- src/lua/lua_mimepart.c | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/libmime/content_type.h b/src/libmime/content_type.h index 9d5393e09..e71077911 100644 --- a/src/libmime/content_type.h +++ b/src/libmime/content_type.h @@ -30,9 +30,9 @@ enum rspamd_content_type_flags { RSPAMD_CONTENT_TYPE_MISSING = 1 << 5, }; -#define IS_CT_MULTIPART(ct) ((ct)->flags & RSPAMD_CONTENT_TYPE_MULTIPART) -#define IS_CT_TEXT(ct) ((ct)->flags & RSPAMD_CONTENT_TYPE_TEXT) -#define IS_CT_MESSAGE(ct) (((ct)->flags & RSPAMD_CONTENT_TYPE_MESSAGE)) +#define IS_CT_MULTIPART(ct) ((ct) && ((ct)->flags & RSPAMD_CONTENT_TYPE_MULTIPART)) +#define IS_CT_TEXT(ct) ((ct) && ((ct)->flags & RSPAMD_CONTENT_TYPE_TEXT)) +#define IS_CT_MESSAGE(ct) ((ct) &&((ct)->flags & RSPAMD_CONTENT_TYPE_MESSAGE)) struct rspamd_content_type_param { rspamd_ftok_t name; diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index d89acf80c..9c1d826d7 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -326,6 +326,7 @@ LUA_FUNCTION_DEF (mimepart, get_image); * @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 @@ -340,6 +341,19 @@ LUA_FUNCTION_DEF (mimepart, is_archive); * @return {rspamd_archive} archive structure or nil if a part is not an archive */ LUA_FUNCTION_DEF (mimepart, get_archive); +/*** + * @method mime_part:is_multipart() + * Returns true if mime part is a multipart part + * @return {bool} true if a part is is a multipart part + */ +LUA_FUNCTION_DEF (mimepart, is_multipart); +/*** + * @method mime_part:get_children() + * Returns rspamd_mimepart table of part's childer. Returns nil if mime part is not multipart + * or a message part. + * @return {rspamd_mimepart} table of children + */ +LUA_FUNCTION_DEF (mimepart, get_children); /*** * @method mime_part:is_text() * Returns true if mime part is a text part @@ -393,6 +407,8 @@ static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, get_image), LUA_INTERFACE_DEF (mimepart, is_archive), LUA_INTERFACE_DEF (mimepart, get_archive), + LUA_INTERFACE_DEF (mimepart, is_multipart), + LUA_INTERFACE_DEF (mimepart, get_children), LUA_INTERFACE_DEF (mimepart, is_text), LUA_INTERFACE_DEF (mimepart, is_broken), LUA_INTERFACE_DEF (mimepart, get_text), @@ -1080,6 +1096,20 @@ lua_mimepart_is_archive (lua_State * L) return 1; } +static gint +lua_mimepart_is_multipart (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + lua_pushboolean (L, IS_CT_MULTIPART (part->ct) ? true : false); + + return 1; +} + static gint lua_mimepart_is_text (lua_State * L) { @@ -1158,6 +1188,35 @@ lua_mimepart_get_archive (lua_State * L) return 1; } +static gint +lua_mimepart_get_children (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + struct rspamd_mime_part **pcur, *cur; + guint i; + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + if (!IS_CT_MULTIPART (part->ct) || part->specific.mp.children == NULL) { + lua_pushnil (L); + } + else { + lua_createtable (L, part->specific.mp.children->len, 0); + + PTR_ARRAY_FOREACH (part->specific.mp.children, i, cur) { + pcur = lua_newuserdata (L, sizeof (*pcur)); + *pcur = cur; + rspamd_lua_setclass (L, "rspamd{mimepart}", -1); + lua_rawseti (L, -2, i + 1); + } + } + + return 1; +} + + static gint lua_mimepart_get_text (lua_State * L) { -- 2.39.5