commit 1e5de80: [Minor] Add task:has_header method

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jun 8 16:14:15 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-06-08 15:19:48 +0100
URL: https://github.com/rspamd/rspamd/commit/1e5de80791c519775755d612a542b7b0860a609f

[Minor] Add task:has_header method

---
 src/lua/lua_common.h |  1 +
 src/lua/lua_task.c   | 41 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index f9ec8e2e3..76680cbc5 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -256,6 +256,7 @@ enum rspamd_lua_task_header_type {
 	RSPAMD_TASK_HEADER_PUSH_RAW,
 	RSPAMD_TASK_HEADER_PUSH_FULL,
 	RSPAMD_TASK_HEADER_PUSH_COUNT,
+	RSPAMD_TASK_HEADER_PUSH_HAS,
 };
 
 gint rspamd_lua_push_header (lua_State *L,
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 7e2c00a25..6437b0dad 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -341,6 +341,15 @@ LUA_FUNCTION_DEF (task, get_subject);
  * @return {string} decoded value of a header
  */
 LUA_FUNCTION_DEF (task, get_header);
+/***
+ * @method task:has_header(name[, case_sensitive])
+ * Get decoded value of a header specified with optional case_sensitive flag.
+ * By default headers are searched in caseless matter.
+ * @param {string} name name of header to get
+ * @param {boolean} case_sensitive case sensitiveness flag to search for a header
+ * @return {boolean},{number} true if header exists, the second value is number of headers with this name
+ */
+LUA_FUNCTION_DEF (task, has_header);
 /***
  * @method task:get_header_raw(name[, case_sensitive])
  * Get raw value of a header specified with optional case_sensitive flag.
@@ -1182,6 +1191,7 @@ static const struct luaL_reg tasklib_m[] = {
 	LUA_INTERFACE_DEF (task, get_request_header),
 	LUA_INTERFACE_DEF (task, set_request_header),
 	LUA_INTERFACE_DEF (task, get_header),
+	LUA_INTERFACE_DEF (task, has_header),
 	LUA_INTERFACE_DEF (task, get_header_raw),
 	LUA_INTERFACE_DEF (task, get_header_full),
 	LUA_INTERFACE_DEF (task, get_header_count),
@@ -2745,16 +2755,22 @@ rspamd_lua_push_header_array (lua_State *L,
 	LUA_TRACE_POINT;
 	struct rspamd_mime_header *cur;
 	guint i;
+	gint nret = 1;
 
 	if (rh == NULL) {
-		if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) {
+		if (how == RSPAMD_TASK_HEADER_PUSH_HAS) {
+			lua_pushboolean (L, false);
+			lua_pushnumber (L, 0);
+			nret = 2;
+		}
+		else if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) {
 			lua_pushnumber (L, 0);
 		}
 		else {
 			lua_pushnil (L);
 		}
 
-		return 1;
+		return nret;
 	}
 
 	if (how == RSPAMD_TASK_HEADER_PUSH_FULL) {
@@ -2779,6 +2795,19 @@ rspamd_lua_push_header_array (lua_State *L,
 
 		lua_pushinteger (L, i);
 	}
+	else if (how == RSPAMD_TASK_HEADER_PUSH_HAS) {
+		i = 0;
+		nret = 2;
+
+		DL_FOREACH (rh, cur) {
+			if (!strong || strcmp (name, cur->name) == 0) {
+				i++;
+			}
+		}
+
+		lua_pushboolean (L, true);
+		lua_pushinteger (L, i);
+	}
 	else {
 		DL_FOREACH (rh, cur) {
 			if (!strong || strcmp (name, cur->name) == 0) {
@@ -2790,7 +2819,7 @@ rspamd_lua_push_header_array (lua_State *L,
 		lua_pushnil (L);
 	}
 
-	return 1;
+	return nret;
 }
 
 static gint
@@ -2842,6 +2871,12 @@ lua_task_get_header_count (lua_State * L)
 	return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_COUNT);
 }
 
+static gint
+lua_task_has_header (lua_State * L)
+{
+	return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_HAS);
+}
+
 static gint
 lua_task_get_raw_headers (lua_State *L)
 {


More information about the Commits mailing list