commit c49ea09: [Feature] Allow to sort urls for Lua

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Feb 22 16:14:09 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-02-22 16:02:13 +0000
URL: https://github.com/rspamd/rspamd/commit/c49ea0960c5a1926defa93adec1c5fe1397ce500

[Feature] Allow to sort urls for Lua

---
 src/lua/lua_task.c | 29 ++++++++++++++++++++++++++---
 src/lua/lua_url.c  |  6 ++++++
 src/lua/lua_url.h  |  1 +
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 31694df36..1095d1a60 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -2407,9 +2407,32 @@ lua_task_get_urls (lua_State * L)
 
 		lua_createtable (L, sz, 0);
 
-		kh_foreach_key (MESSAGE_FIELD (task, urls), u, {
-			lua_tree_url_callback (u, u, &cb);
-		});
+		if (cb.sort) {
+			struct rspamd_url **urls_sorted;
+			gint i = 0;
+
+			urls_sorted = g_new0 (struct rspamd_url *, sz);
+
+			kh_foreach_key (MESSAGE_FIELD(task, urls), u, {
+				if (i < sz) {
+					urls_sorted[i] = u;
+					i ++;
+				}
+			});
+
+			qsort (urls_sorted, i, sizeof (struct rspamd_url *), rspamd_url_cmp_qsort);
+
+			for (int j = 0; j < i; j ++) {
+				lua_tree_url_callback (urls_sorted[j], urls_sorted[j], &cb);
+			}
+
+			g_free (urls_sorted);
+		}
+		else {
+			kh_foreach_key (MESSAGE_FIELD(task, urls), u, {
+				lua_tree_url_callback(u, u, &cb);
+			});
+		}
 
 		lua_url_cbdata_dtor (&cb);
 	}
diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c
index 945f3dbf4..7909444d5 100644
--- a/src/lua/lua_url.c
+++ b/src/lua/lua_url.c
@@ -1119,6 +1119,12 @@ lua_url_cbdata_fill (lua_State *L,
 				max_urls = lua_tonumber (L, -1);
 			}
 			lua_pop (L, 1);
+
+			lua_getfield (L, pos, "sort");
+			if (lua_isboolean (L, -1)) {
+				cbd->sort = TRUE;
+			}
+			lua_pop (L, 1);
 		}
 		else {
 			/* Plain table of the protocols */
diff --git a/src/lua/lua_url.h b/src/lua/lua_url.h
index c074a579a..705fe1615 100644
--- a/src/lua/lua_url.h
+++ b/src/lua/lua_url.h
@@ -35,6 +35,7 @@ struct lua_tree_cb_data {
 	gsize max_urls;
 	gdouble skip_prob;
 	guint64 xoroshiro_state[4];
+	gboolean sort;
 };
 
 void lua_tree_url_callback (gpointer key, gpointer value, gpointer ud);


More information about the Commits mailing list