]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add text_part:get_stats function
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 21 Jun 2017 18:06:01 +0000 (19:06 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 21 Jun 2017 18:06:30 +0000 (19:06 +0100)
src/libmime/message.c
src/libmime/message.h
src/lua/lua_mimepart.c

index 0d495fcd820733aa0c01cee0b1de7dde17090eef..b2d37890cf15c90214072161bcad7186207dc3d8 100644 (file)
@@ -459,7 +459,7 @@ rspamd_strip_newlines_parse (const gchar *begin, const gchar *pe,
                                        part->non_spaces ++;
 
                                        if (G_UNLIKELY (*p & 0x80)) {
-                                               part->non_aciii_chars ++;
+                                               part->non_ascii_chars ++;
                                        }
                                        else {
                                                part->ascii_chars ++;
@@ -515,7 +515,7 @@ rspamd_strip_newlines_parse (const gchar *begin, const gchar *pe,
                                        part->non_spaces ++;
 
                                        if (G_UNLIKELY (*p & 0x80)) {
-                                               part->non_aciii_chars ++;
+                                               part->non_ascii_chars ++;
                                        }
                                        else {
                                                part->ascii_chars ++;
index 599521f6f423ee90447c02ef33943bcacf469282..8dc06eb3a1271d364b8922eb1136dcbc96f13c42 100644 (file)
@@ -96,7 +96,7 @@ struct rspamd_mime_text_part {
        GArray *normalized_hashes;
        guint nlines;
        guint spaces;
-       guint non_aciii_chars;
+       guint non_ascii_chars;
        guint ascii_chars;
        guint double_spaces;
        guint non_spaces;
index d831250445dd4c3c64f314ff2f9dc9f07799b30d..75b2701895a0ccc6fefcb333c392173d1a8d331c 100644 (file)
@@ -107,6 +107,18 @@ LUA_FUNCTION_DEF (textpart, get_urls_length);
  * @return {integer} number of lines in the part
  */
 LUA_FUNCTION_DEF (textpart, get_lines_count);
+/***
+ * @method mime_part:get_stats()
+ * Returns a table with the following data:
+ * - `lines`: number of lines
+ * - `spaces`: number of spaces
+ * - `double_spaces`: double spaces
+ * - `empty_lines`: number of empty lines
+ * - `non_ascii_characters`: number of non ascii characters
+ * - `ascii_characters`: number of ascii characters
+ * @return {table} table of stats
+ */
+LUA_FUNCTION_DEF (textpart, get_stats);
 /***
  * @method mime_part:get_words_count()
  * Get words number in the part
@@ -161,6 +173,7 @@ static const struct luaL_reg textpartlib_m[] = {
        LUA_INTERFACE_DEF (textpart, get_html),
        LUA_INTERFACE_DEF (textpart, get_language),
        LUA_INTERFACE_DEF (textpart, get_mimepart),
+       LUA_INTERFACE_DEF (textpart, get_stats),
        {"__tostring", rspamd_lua_class_tostring},
        {NULL, NULL}
 };
@@ -715,6 +728,55 @@ lua_textpart_get_mimepart (lua_State * L)
        return 1;
 }
 
+/***
+ * @method mime_part:get_stats()
+ * Returns a table with the following data:
+ * -
+ * - `lines`: number of lines
+ * - `spaces`: number of spaces
+ * - `double_spaces`: double spaces
+ * - `empty_lines`: number of empty lines
+ * - `non_ascii_characters`: number of non ascii characters
+ * - `ascii_characters`: number of ascii characters
+ * @return {table} table of stats
+ */
+static gint
+lua_textpart_get_stats (lua_State * L)
+{
+       struct rspamd_mime_text_part *part = lua_check_textpart (L);
+
+       if (part != NULL) {
+               lua_createtable (L, 0, 7);
+
+               lua_pushstring (L, "lines");
+               lua_pushnumber (L, part->nlines);
+               lua_settable (L, -3);
+               lua_pushstring (L, "empty_lines");
+               lua_pushnumber (L, part->empty_lines);
+               lua_settable (L, -3);
+               lua_pushstring (L, "spaces");
+               lua_pushnumber (L, part->spaces);
+               lua_settable (L, -3);
+               lua_pushstring (L, "non_spaces");
+               lua_pushnumber (L, part->non_spaces);
+               lua_settable (L, -3);
+               lua_pushstring (L, "double_spaces");
+               lua_pushnumber (L, part->double_spaces);
+               lua_settable (L, -3);
+               lua_pushstring (L, "ascii_characters");
+               lua_pushnumber (L, part->ascii_chars);
+               lua_settable (L, -3);
+               lua_pushstring (L, "non_ascii_characters");
+               lua_pushnumber (L, part->non_ascii_chars);
+               lua_settable (L, -3);
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 /* Mimepart implementation */
 
 static gint