From cca977866939436fa886e49859031631d1de0a22 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 22 Apr 2011 18:57:52 +0400 Subject: [PATCH] * Add functions to lua API to detect message and task date (in GMT) --- src/lua/lua_common.h | 2 +- src/lua/lua_message.c | 21 +++++++++++++++++++++ src/lua/lua_task.c | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 47d6956b0..da3cc254e 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -16,7 +16,7 @@ extern const luaL_reg null_reg[]; -#define RSPAMD_LUA_API_VERSION 4 +#define RSPAMD_LUA_API_VERSION 5 /* Common utility functions */ void lua_newclass (lua_State *L, const gchar *classname, const struct luaL_reg *func); diff --git a/src/lua/lua_message.c b/src/lua/lua_message.c index 2848e082f..c1b7a734f 100644 --- a/src/lua/lua_message.c +++ b/src/lua/lua_message.c @@ -68,6 +68,7 @@ LUA_FUNCTION_DEF (message, set_reply_to); LUA_FUNCTION_DEF (message, get_header); LUA_FUNCTION_DEF (message, get_header_strong); LUA_FUNCTION_DEF (message, set_header); +LUA_FUNCTION_DEF (message, get_date); static const struct luaL_reg msglib_m[] = { LUA_INTERFACE_DEF (message, get_subject), @@ -81,6 +82,7 @@ static const struct luaL_reg msglib_m[] = { LUA_INTERFACE_DEF (message, get_header), LUA_INTERFACE_DEF (message, get_header_strong), LUA_INTERFACE_DEF (message, set_header), + LUA_INTERFACE_DEF (message, get_date), {"__tostring", lua_class_tostring}, {NULL, NULL} }; @@ -180,6 +182,25 @@ lua_message_set_header (lua_State * L) return 1; } +static gint +lua_message_get_date (lua_State * L) +{ + GMimeMessage *obj = lua_check_message (L); + time_t msg_time; + int offset; + + if (obj != NULL) { + g_mime_message_get_date (obj, &msg_time, &offset); + /* Convert result to GMT */ + msg_time -= (offset / 100) * 3600; + lua_pushnumber (L, msg_time); + } + else { + lua_pushnil (L); + } + + return 1; +} gint luaopen_message (lua_State * L) diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index a0e6a3992..1011612aa 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -65,6 +65,7 @@ LUA_FUNCTION_DEF (task, get_client_ip_num); LUA_FUNCTION_DEF (task, get_helo); LUA_FUNCTION_DEF (task, get_images); LUA_FUNCTION_DEF (task, get_symbol); +LUA_FUNCTION_DEF (task, get_date); LUA_FUNCTION_DEF (task, get_metric_score); LUA_FUNCTION_DEF (task, get_metric_action); LUA_FUNCTION_DEF (task, learn_statfile); @@ -93,6 +94,7 @@ static const struct luaL_reg tasklib_m[] = { LUA_INTERFACE_DEF (task, get_helo), LUA_INTERFACE_DEF (task, get_images), LUA_INTERFACE_DEF (task, get_symbol), + LUA_INTERFACE_DEF (task, get_date), LUA_INTERFACE_DEF (task, get_metric_score), LUA_INTERFACE_DEF (task, get_metric_action), LUA_INTERFACE_DEF (task, learn_statfile), @@ -1045,6 +1047,24 @@ lua_task_get_symbol (lua_State *L) return 1; } +static gint +lua_task_get_date (lua_State *L) +{ + struct worker_task *task = lua_check_task (L); + time_t task_time; + + if (task != NULL) { + /* Get GMT date and store it to time_t */ + task_time = mktime (gmtime (&task->tv.tv_sec)); + lua_pushnumber (L, task_time); + } + else { + lua_pushnil (L); + } + + return 1; +} + static gint lua_task_learn_statfile (lua_State *L) { -- 2.39.5