diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-10-05 15:16:25 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-10-05 15:16:25 +0100 |
commit | 7ccbfa1124ff6bb71f3fefb346c26d71d52a7e29 (patch) | |
tree | 91fd3904e448f55d92436a1f03e01570538ce40d /src/lua/lua_util.c | |
parent | a61871cccb5a86c4e9ad264c045b4403abc2b183 (diff) | |
download | rspamd-7ccbfa1124ff6bb71f3fefb346c26d71d52a7e29.tar.gz rspamd-7ccbfa1124ff6bb71f3fefb346c26d71d52a7e29.zip |
[Minor] Lua_util: Add parse_smtp_date utility
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 40 |
1 files changed, 40 insertions, 0 deletions
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) { |