aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_mimepart.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-07-13 19:07:43 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-07-13 19:07:43 +0100
commit67ccec834e9be395a187da73e5db19df7321031e (patch)
treedc006e13c9333fda9d355cc1d7b58ebabef6ef02 /src/lua/lua_mimepart.c
parent44674eba990be7afc1ea32bc0fcb31d637be34e2 (diff)
downloadrspamd-67ccec834e9be395a187da73e5db19df7321031e.tar.gz
rspamd-67ccec834e9be395a187da73e5db19df7321031e.zip
[Feature] Add method to get all content-type attributes in Lua
Diffstat (limited to 'src/lua/lua_mimepart.c')
-rw-r--r--src/lua/lua_mimepart.c61
1 files changed, 59 insertions, 2 deletions
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
@@ -261,6 +261,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
* @return {string} content transfer encoding (e.g. `base64` or `7bit`)
@@ -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