#include "unicode/uspoof.h"
#include "unicode/uscript.h"
+#include "libmime/smtp_parsers.h"
#include "contrib/fastutf8/fastutf8.h"
/***
*/
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),
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}
};
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)
{