]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Parse ical files
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 8 Apr 2019 12:24:55 +0000 (13:24 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 8 Apr 2019 12:24:55 +0000 (13:24 +0100)
src/libmime/message.c

index 6825bc2f026a76ae68baf2697201f9151e71995c..9bbd25d32dd434485fdb93f353eddc52935c9ca4 100644 (file)
@@ -34,7 +34,8 @@
 
 #include <math.h>
 #include <unicode/uchar.h>
-#include <src/libserver/cfg_file_private.h>
+#include "libserver/cfg_file_private.h"
+#include "lua/lua_common.h"
 
 #define GTUBE_SYMBOL "GTUBE"
 
@@ -686,8 +687,69 @@ rspamd_message_process_plain_text_part (struct rspamd_task *task,
        rspamd_mime_text_part_maybe_convert (task, text_part);
 
        if (text_part->utf_raw_content != NULL) {
-               /* Different from HTML, where we also parse HTML and strip tags */
-               text_part->utf_content = text_part->utf_raw_content;
+               /* Check for ical */
+               rspamd_ftok_t cal_ct;
+
+               RSPAMD_FTOK_ASSIGN (&cal_ct, "calendar");
+
+               if (rspamd_ftok_casecmp (&cal_ct, &text_part->mime_part->ct->subtype) == 0) {
+                       lua_State *L = task->cfg->lua_state;
+                       gint err_idx;
+
+                       lua_pushcfunction (L, &rspamd_lua_traceback);
+                       err_idx = lua_gettop (L);
+
+                       /* Obtain function */
+                       if (!rspamd_lua_require_function (L, "lua_ical", "ical_txt_values")) {
+                               msg_err_task ("cannot require lua_ical.ical_txt_values");
+                               lua_settop (L, err_idx - 1);
+
+                               return FALSE;
+                       }
+
+                       lua_pushlstring (L, text_part->utf_raw_content->data,
+                                       text_part->utf_raw_content->len);
+
+                       if (lua_pcall (L, 1, 1, err_idx) != 0) {
+                               GString *tb;
+
+                               tb = lua_touserdata (L, -1);
+                               msg_err_task ("cannot call lua lua_ical.ical_txt_values: %s", tb->str);
+                               g_string_free (tb, TRUE);
+                               lua_settop (L, err_idx - 1);
+
+                               return FALSE;
+                       }
+
+                       if (lua_type (L, -1) == LUA_TSTRING) {
+                               const char *ndata;
+                               gsize nsize;
+
+                               ndata = lua_tolstring (L, -1, &nsize);
+                               text_part->utf_content = g_byte_array_sized_new (nsize);
+                               g_byte_array_append (text_part->utf_content, ndata, nsize);
+                               rspamd_mempool_add_destructor (task->task_pool,
+                                               (rspamd_mempool_destruct_t) free_byte_array_callback,
+                                               text_part->utf_content);
+                       }
+                       else if (lua_type (L, -1) == LUA_TNIL) {
+                               msg_info_task ("cannot convert text/calendar to plain text");
+                               text_part->utf_content = text_part->utf_raw_content;
+                       }
+                       else {
+                               msg_err_task ("invalid return type when calling lua_ical.ical_txt_values: %s",
+                                               lua_typename (L, lua_type (L, -1)));
+                               lua_settop (L, err_idx - 1);
+
+                               return FALSE;
+                       }
+
+                       lua_settop (L, err_idx - 1);
+               }
+               else {
+                       /* Just have the same content */
+                       text_part->utf_content = text_part->utf_raw_content;
+               }
        }
        else {
                /*