]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to sort urls for Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 22 Feb 2021 16:02:13 +0000 (16:02 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 22 Feb 2021 16:02:13 +0000 (16:02 +0000)
src/lua/lua_task.c
src/lua/lua_url.c
src/lua/lua_url.h

index 31694df36b11f7869e388f2cee77f019b2fd719b..1095d1a60d215f6111e97f5f6cd1b8e78d57eaae 100644 (file)
@@ -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);
        }
index 945f3dbf4fb5722854531de5394220199dc11cab..7909444d5096203c7d6793a77bfaf3c83885882d 100644 (file)
@@ -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 */
index c074a579a2236187ec4df9d5e3cbecb7427eb332..705fe16153aec8905c3087398b8de33a2cfa05ad 100644 (file)
@@ -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);