From: Vsevolod Stakhov Date: Thu, 13 Jul 2017 18:07:43 +0000 (+0100) Subject: [Feature] Add method to get all content-type attributes in Lua X-Git-Tag: 1.7.0~856 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=67ccec834e9be395a187da73e5db19df7321031e;p=rspamd.git [Feature] Add method to get all content-type attributes in Lua --- diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 362b43046..8223b9fc5 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -260,6 +260,13 @@ LUA_FUNCTION_DEF (mimepart, get_length); */ LUA_FUNCTION_DEF (mimepart, get_type); +/*** + * @method mime_part:get_type_full() + * Extract content-type string of the mime part with all attributes + * @return {string,string,table} content type in form 'type','subtype', {attrs} + */ +LUA_FUNCTION_DEF (mimepart, get_type_full); + /*** * @method mime_part:get_cte() * Extract content-transfer-encoding for a part @@ -355,6 +362,7 @@ static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, get_content), LUA_INTERFACE_DEF (mimepart, get_length), LUA_INTERFACE_DEF (mimepart, get_type), + LUA_INTERFACE_DEF (mimepart, get_type_full), LUA_INTERFACE_DEF (mimepart, get_cte), LUA_INTERFACE_DEF (mimepart, get_filename), LUA_INTERFACE_DEF (mimepart, get_header), @@ -815,9 +823,12 @@ lua_mimepart_get_length (lua_State * L) } static gint -lua_mimepart_get_type (lua_State * L) +lua_mimepart_get_type_common (lua_State * L, gboolean full) { struct rspamd_mime_part *part = lua_check_mimepart (L); + GHashTableIter it; + gpointer k, v; + rspamd_ftok_t *akey, *avalue; if (part == NULL) { lua_pushnil (L); @@ -828,7 +839,53 @@ lua_mimepart_get_type (lua_State * L) lua_pushlstring (L, part->ct->type.begin, part->ct->type.len); lua_pushlstring (L, part->ct->subtype.begin, part->ct->subtype.len); - return 2; + if (!full) { + return 2; + } + + lua_createtable (L, 0, 2 + (part->ct->attrs ? + g_hash_table_size (part->ct->attrs) : 0)); + + if (part->ct->charset.len > 0) { + lua_pushstring (L, "charset"); + lua_pushlstring (L, part->ct->charset.begin, part->ct->charset.len); + lua_settable (L, -3); + } + + if (part->ct->boundary.len > 0) { + lua_pushstring (L, "charset"); + lua_pushlstring (L, part->ct->boundary.begin, part->ct->boundary.len); + lua_settable (L, -3); + } + + if (part->ct->attrs) { + g_hash_table_iter_init (&it, part->ct->attrs); + + while (g_hash_table_iter_next (&it, &k, &v)) { + akey = k; + avalue = v; + + if (akey->len > 0 && avalue->len > 0) { + lua_pushlstring (L, akey->begin, akey->len); + lua_pushlstring (L, avalue->begin, avalue->len); + lua_settable (L, -3); + } + } + } + + return 3; +} + +static gint +lua_mimepart_get_type (lua_State * L) +{ + return lua_mimepart_get_type_common (L, FALSE); +} + +static gint +lua_mimepart_get_type_full (lua_State * L) +{ + return lua_mimepart_get_type_common (L, TRUE); } static gint