commit 3fd7452: [Minor] Further polishing

Vsevolod Stakhov vsevolod at highsecure.ru
Sun Oct 17 11:49:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-10-16 13:43:29 +0100
URL: https://github.com/rspamd/rspamd/commit/3fd74520f054e05a74f9b7d58b4c1b081e49fa96

[Minor] Further polishing

---
 src/plugins/fuzzy_check.c | 190 ++++++++++++++++++++++------------------------
 1 file changed, 91 insertions(+), 99 deletions(-)

diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c
index 3f5daea75..b1439357c 100644
--- a/src/plugins/fuzzy_check.c
+++ b/src/plugins/fuzzy_check.c
@@ -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;
 }
 


More information about the Commits mailing list