]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Further polishing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 16 Oct 2021 12:43:29 +0000 (13:43 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 17 Oct 2021 11:47:26 +0000 (12:47 +0100)
src/plugins/fuzzy_check.c

index 3f5daea75279a64f2a74d3dd81a1e8ecb6e411cf..b1439357cf3c57fe3f4fa13513df8ade5b1df299 100644 (file)
@@ -3777,73 +3777,67 @@ fuzzy_lua_learn_handler (lua_State *L)
        const gchar *symbol;
        struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg);
 
-       if (task) {
-               if (lua_type (L, 2) == LUA_TNUMBER) {
-                       flag = lua_tonumber (L, 2);
-               }
-               else if (lua_type (L, 2) == LUA_TSTRING) {
-                       struct fuzzy_rule *rule;
-                       guint i;
-                       GHashTableIter it;
-                       gpointer k, v;
-                       struct fuzzy_mapping *map;
+       if (lua_type (L, 2) == LUA_TNUMBER) {
+               flag = lua_tointeger (L, 2);
+       }
+       else if (lua_type (L, 2) == LUA_TSTRING) {
+               struct fuzzy_rule *rule;
+               guint i;
+               GHashTableIter it;
+               gpointer k, v;
+               struct fuzzy_mapping *map;
 
-                       symbol = lua_tostring (L, 2);
+               symbol = lua_tostring (L, 2);
 
-                       PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) {
-                               if (flag != 0) {
-                                       break;
-                               }
+               PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) {
+                       if (flag != 0) {
+                               break;
+                       }
 
-                               g_hash_table_iter_init (&it, rule->mappings);
+                       g_hash_table_iter_init (&it, rule->mappings);
 
-                               while (g_hash_table_iter_next (&it, &k, &v)) {
-                                       map = v;
+                       while (g_hash_table_iter_next (&it, &k, &v)) {
+                               map = v;
 
-                                       if (g_ascii_strcasecmp (symbol, map->symbol) == 0) {
-                                               flag = map->fuzzy_flag;
-                                               break;
-                                       }
+                               if (g_ascii_strcasecmp (symbol, map->symbol) == 0) {
+                                       flag = map->fuzzy_flag;
+                                       break;
                                }
                        }
                }
+       }
 
-               if (flag == 0) {
-                       return luaL_error (L, "bad flag");
-               }
+       if (flag == 0) {
+               return luaL_error (L, "bad flag");
+       }
 
-               if (lua_type (L, 3) == LUA_TNUMBER) {
-                       weight = lua_tonumber (L, 3);
-               }
+       if (lua_type (L, 3) == LUA_TNUMBER) {
+               weight = lua_tonumber (L, 3);
+       }
 
-               if (lua_type (L, 4) == LUA_TTABLE) {
-                       const gchar *sf;
+       if (lua_type (L, 4) == LUA_TTABLE) {
+               const gchar *sf;
 
-                       for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
-                               sf = lua_tostring (L, -1);
+               for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
+                       sf = lua_tostring (L, -1);
 
-                               if (sf) {
-                                       if (g_ascii_strcasecmp (sf, "noimages") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOIMAGES;
-                                       }
-                                       else if (g_ascii_strcasecmp (sf, "noattachments") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS;
-                                       }
-                                       else if (g_ascii_strcasecmp (sf, "notext") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOTEXT;
-                                       }
+                       if (sf) {
+                               if (g_ascii_strcasecmp (sf, "noimages") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOIMAGES;
+                               }
+                               else if (g_ascii_strcasecmp (sf, "noattachments") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS;
+                               }
+                               else if (g_ascii_strcasecmp (sf, "notext") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOTEXT;
                                }
                        }
                }
-
-               lua_pushboolean (L,
-                               fuzzy_check_lua_process_learn (task, FUZZY_WRITE, weight, flag,
-                                               send_flags));
-       }
-       else {
-               return luaL_error (L, "invalid arguments");
        }
 
+       lua_pushboolean (L,
+                       fuzzy_check_lua_process_learn (task, FUZZY_WRITE, weight, flag,
+                                       send_flags));
        return 1;
 }
 
@@ -3851,79 +3845,77 @@ static gint
 fuzzy_lua_unlearn_handler (lua_State *L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
-       g_assert (task != NULL);
+       if (task == NULL) {
+               return luaL_error(L, "invalid arguments");
+       }
+
        guint flag = 0, weight = 1.0, send_flags = 0;
        const gchar *symbol;
        struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context (task->cfg);
 
-       if (task) {
-               if (lua_type (L, 2) == LUA_TNUMBER) {
-                       flag = lua_tonumber (L, 1);
-               }
-               else if (lua_type (L, 2) == LUA_TSTRING) {
-                       struct fuzzy_rule *rule;
-                       guint i;
-                       GHashTableIter it;
-                       gpointer k, v;
-                       struct fuzzy_mapping *map;
+       if (lua_type (L, 2) == LUA_TNUMBER) {
+               flag = lua_tonumber (L, 1);
+       }
+       else if (lua_type (L, 2) == LUA_TSTRING) {
+               struct fuzzy_rule *rule;
+               guint i;
+               GHashTableIter it;
+               gpointer k, v;
+               struct fuzzy_mapping *map;
 
-                       symbol = lua_tostring (L, 2);
+               symbol = lua_tostring (L, 2);
 
-                       PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) {
+               PTR_ARRAY_FOREACH (fuzzy_module_ctx->fuzzy_rules, i, rule) {
 
-                               if (flag != 0) {
-                                       break;
-                               }
+                       if (flag != 0) {
+                               break;
+                       }
 
-                               g_hash_table_iter_init (&it, rule->mappings);
+                       g_hash_table_iter_init (&it, rule->mappings);
 
-                               while (g_hash_table_iter_next (&it, &k, &v)) {
-                                       map = v;
+                       while (g_hash_table_iter_next (&it, &k, &v)) {
+                               map = v;
 
-                                       if (g_ascii_strcasecmp (symbol, map->symbol) == 0) {
-                                               flag = map->fuzzy_flag;
-                                               break;
-                                       }
+                               if (g_ascii_strcasecmp (symbol, map->symbol) == 0) {
+                                       flag = map->fuzzy_flag;
+                                       break;
                                }
                        }
                }
+       }
 
-               if (flag == 0) {
-                       return luaL_error (L, "bad flag");
-               }
+       if (flag == 0) {
+               return luaL_error (L, "bad flag");
+       }
 
-               if (lua_type (L, 3) == LUA_TNUMBER) {
-                       weight = lua_tonumber (L, 3);
-               }
+       if (lua_type (L, 3) == LUA_TNUMBER) {
+               weight = lua_tonumber (L, 3);
+       }
 
-               if (lua_type (L, 4) == LUA_TTABLE) {
-                       const gchar *sf;
+       if (lua_type (L, 4) == LUA_TTABLE) {
+               const gchar *sf;
 
-                       for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
-                               sf = lua_tostring (L, -1);
+               for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
+                       sf = lua_tostring (L, -1);
 
-                               if (sf) {
-                                       if (g_ascii_strcasecmp (sf, "noimages") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOIMAGES;
-                                       }
-                                       else if (g_ascii_strcasecmp (sf, "noattachments") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS;
-                                       }
-                                       else if (g_ascii_strcasecmp (sf, "notext") == 0) {
-                                               send_flags |= FUZZY_CHECK_FLAG_NOTEXT;
-                                       }
+                       if (sf) {
+                               if (g_ascii_strcasecmp (sf, "noimages") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOIMAGES;
+                               }
+                               else if (g_ascii_strcasecmp (sf, "noattachments") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOATTACHMENTS;
+                               }
+                               else if (g_ascii_strcasecmp (sf, "notext") == 0) {
+                                       send_flags |= FUZZY_CHECK_FLAG_NOTEXT;
                                }
                        }
                }
-
-               lua_pushboolean (L,
-                               fuzzy_check_lua_process_learn (task, FUZZY_DEL, weight, flag,
-                                               send_flags));
-       }
-       else {
-               return luaL_error (L, "invalid arguments");
        }
 
+       lua_pushboolean (L,
+                       fuzzy_check_lua_process_learn (task, FUZZY_DEL, weight, flag,
+                                       send_flags));
+
        return 1;
 }