commit 7ccbfa1: [Minor] Lua_util: Add parse_smtp_date utility

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Oct 5 14:21:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-10-05 15:16:25 +0100
URL: https://github.com/rspamd/rspamd/commit/7ccbfa1124ff6bb71f3fefb346c26d71d52a7e29 (HEAD -> master)

[Minor] Lua_util: Add parse_smtp_date utility

---
 src/lua/lua_util.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index 0787736d5..5509fc435 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -34,6 +34,7 @@
 
 #include "unicode/uspoof.h"
 #include "unicode/uscript.h"
+#include "libmime/smtp_parsers.h"
 #include "contrib/fastutf8/fastutf8.h"
 
 /***
@@ -638,6 +639,15 @@ LUA_FUNCTION_DEF (util, mime_header_encode);
  */
 LUA_FUNCTION_DEF (util, btc_polymod);
 
+/***
+ * @function util.parse_smtp_date(str[, local_tz])
+ * Converts an SMTP date string to unix timestamp
+ * @param {string} str input string
+ * @param {boolean} local_tz convert to local tz if `true`
+ * @return {number} time as unix timestamp (converted to float)
+ */
+LUA_FUNCTION_DEF (util, parse_smtp_date);
+
 
 static const struct luaL_reg utillib_f[] = {
 	LUA_INTERFACE_DEF (util, create_event_base),
@@ -703,6 +713,7 @@ static const struct luaL_reg utillib_f[] = {
 	LUA_INTERFACE_DEF (util, unpack),
 	LUA_INTERFACE_DEF (util, packsize),
 	LUA_INTERFACE_DEF (util, btc_polymod),
+	LUA_INTERFACE_DEF (util, parse_smtp_date),
 	{NULL, NULL}
 };
 
@@ -3952,6 +3963,35 @@ lua_util_btc_polymod (lua_State *L)
 	return 1;
 }
 
+static int
+lua_util_parse_smtp_date (lua_State *L)
+{
+	gsize slen;
+	const gchar *str = lua_tolstring (L, 1, &slen);
+
+	if (str == NULL) {
+		return luaL_argerror (L, 1, "invalid argument");
+	}
+
+	time_t tt = rspamd_parse_smtp_date (str, slen);
+
+	if (lua_isboolean (L, 2) && !!lua_toboolean (L, 2)) {
+		struct tm t;
+
+		rspamd_localtime (tt, &t);
+#if !defined(__sun)
+		t.tm_gmtoff = 0;
+#endif
+		t.tm_isdst = 0;
+		tt = mktime (&t);
+	}
+
+	lua_pushnumber (L, tt);
+
+	return 1;
+}
+
+
 static gint
 lua_load_util (lua_State * L)
 {


More information about the Commits mailing list