]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to get multipart children in Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 15 May 2018 10:31:25 +0000 (11:31 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 15 May 2018 10:31:25 +0000 (11:31 +0100)
src/libmime/content_type.h
src/lua/lua_mimepart.c

index 9d5393e0996fc257857fc75365335291fd653217..e71077911752ddfd9b8975029a320eb90a5cf144 100644 (file)
@@ -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;
index d89acf80c0e3b5c5386d9f1ffbf8f67f0ad4d2c9..9c1d826d7b5aa66d664f54ef74251fe673a732a4 100644 (file)
@@ -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)
 {