]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to get digest of a mime part from lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 15 Aug 2016 16:58:07 +0000 (17:58 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 15 Aug 2016 16:58:07 +0000 (17:58 +0100)
src/lua/lua_mimepart.c

index 6cf0b8f23b74472673bc9e02c267c2ca1c2b4043..b8f7d8f85e0018021ff808834c5c016398e73995 100644 (file)
@@ -282,6 +282,13 @@ LUA_FUNCTION_DEF (mimepart, is_text);
  */
 LUA_FUNCTION_DEF (mimepart, get_text);
 
+/***
+ * @method mime_part:get_digest()
+ * Returns the unique digest for this mime part
+ * @return {string} 128 characters hex string with digest of the part
+ */
+LUA_FUNCTION_DEF (mimepart, get_digest);
+
 static const struct luaL_reg mimepartlib_m[] = {
        LUA_INTERFACE_DEF (mimepart, get_content),
        LUA_INTERFACE_DEF (mimepart, get_length),
@@ -296,6 +303,7 @@ static const struct luaL_reg mimepartlib_m[] = {
        LUA_INTERFACE_DEF (mimepart, get_archive),
        LUA_INTERFACE_DEF (mimepart, is_text),
        LUA_INTERFACE_DEF (mimepart, get_text),
+       LUA_INTERFACE_DEF (mimepart, get_digest),
        {"__tostring", rspamd_lua_class_tostring},
        {NULL, NULL}
 };
@@ -802,6 +810,24 @@ lua_mimepart_get_text (lua_State * L)
        return 1;
 }
 
+static gint
+lua_mimepart_get_digest (lua_State * L)
+{
+       struct rspamd_mime_part *part = lua_check_mimepart (L);
+       gchar digestbuf[rspamd_cryptobox_HASHBYTES * 2 + 1];
+
+       if (part == NULL) {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       memset (digestbuf, 0, sizeof (digestbuf));
+       rspamd_encode_hex_buf (part->digest, sizeof (part->digest),
+                       digestbuf, sizeof (digestbuf));
+       lua_pushstring (L, digestbuf);
+
+       return 1;
+}
+
 void
 luaopen_textpart (lua_State * L)
 {