commit a057dfc: [Minor] Add mime_part:get_urls method

Vsevolod Stakhov vsevolod at highsecure.ru
Tue May 5 14:56:09 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-05-05 15:17:52 +0100
URL: https://github.com/rspamd/rspamd/commit/a057dfc47818859f84da10d73f128c3852e19e99

[Minor] Add mime_part:get_urls method

---
 src/lua/lua_mimepart.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c
index 76e43b4c5..e183b6ae6 100644
--- a/src/lua/lua_mimepart.c
+++ b/src/lua/lua_mimepart.c
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#include <src/libmime/message.h>
 #include "lua_common.h"
+#include "lua_url.h"
 #include "libmime/message.h"
 #include "libmime/lang_detection.h"
 #include "libstat/stat_api.h"
@@ -544,6 +544,15 @@ LUA_FUNCTION_DEF (mimepart, set_specific);
  */
 LUA_FUNCTION_DEF (mimepart, is_specific);
 
+/***
+ * @method mime_part:get_urls([need_emails|list_protos][, need_images])
+ * Get all URLs found in a mime part. Telephone urls and emails are not included unless explicitly asked in `list_protos`
+ * @param {boolean} need_emails if `true` then reutrn also email urls, this can be a comma separated string of protocols desired or a table (e.g. `mailto` or `telephone`)
+ * @param {boolean} need_images return urls from images (<img src=...>) as well
+ * @return {table rspamd_url} list of all urls found
+ */
+LUA_FUNCTION_DEF (mimepart, get_urls);
+
 static const struct luaL_reg mimepartlib_m[] = {
 	LUA_INTERFACE_DEF (mimepart, get_content),
 	LUA_INTERFACE_DEF (mimepart, get_raw_content),
@@ -569,6 +578,7 @@ static const struct luaL_reg mimepartlib_m[] = {
 	LUA_INTERFACE_DEF (mimepart, is_message),
 	LUA_INTERFACE_DEF (mimepart, get_children),
 	LUA_INTERFACE_DEF (mimepart, get_parent),
+	LUA_INTERFACE_DEF (mimepart, get_urls),
 	LUA_INTERFACE_DEF (mimepart, is_text),
 	LUA_INTERFACE_DEF (mimepart, is_broken),
 	LUA_INTERFACE_DEF (mimepart, is_attachment),
@@ -2069,6 +2079,46 @@ lua_mimepart_get_specific (lua_State * L)
 	return 1;
 }
 
+static gint
+lua_mimepart_get_urls (lua_State * L)
+{
+	LUA_TRACE_POINT;
+	struct rspamd_mime_part *part = lua_check_mimepart (L);
+
+	if (part == NULL) {
+		return luaL_error (L, "invalid arguments");
+	}
+
+	struct lua_tree_cb_data cb;
+	struct rspamd_url *u;
+	static const gint default_protocols_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS|
+											   PROTOCOL_FILE|PROTOCOL_FTP;
+	gsize sz, max_urls = 0, i;
+
+	if (part->urls == NULL) {
+		lua_newtable (L);
+
+		return 1;
+	}
+
+	if (!lua_url_cbdata_fill (L, 2, &cb, default_protocols_mask,
+			~(0), max_urls)) {
+		return luaL_error (L, "invalid arguments");
+	}
+
+	sz = part->urls->len;
+
+	lua_createtable (L, sz, 0);
+
+	PTR_ARRAY_FOREACH (part->urls, i, u) {
+		lua_tree_url_callback (u, u, &cb);
+	}
+
+	lua_url_cbdata_dtor (&cb);
+
+	return 1;
+}
+
 static gint
 lua_mimepart_is_specific (lua_State * L)
 {


More information about the Commits mailing list