From 99ffbf90360564424c232dde0afec086abfc470e Mon Sep 17 00:00:00 2001 From: Vysheng Date: Thu, 6 Feb 2014 22:09:43 +0400 Subject: [PATCH] lua: added postpone message --- lua-tg.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ structures.c | 1 - 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lua-tg.c b/lua-tg.c index 73153e3..1d7b6e9 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -20,6 +20,7 @@ lua_State *luaState; #include "constants.h" #include "tools.h" #include "queries.h" +#include "net.h" extern int verbosity; @@ -471,6 +472,62 @@ static int mark_read_from_lua (lua_State *L) { return 1; } +int lua_postpone_alarm (void *self) { + int *t = self; + + lua_settop (luaState, 0); + //lua_checkstack (luaState, 20); + my_lua_checkstack (luaState, 20); + + lua_rawgeti (luaState, LUA_REGISTRYINDEX, t[1]); + lua_rawgeti (luaState, LUA_REGISTRYINDEX, t[0]); + assert (lua_gettop (luaState) == 2); + + int r = lua_pcall (luaState, 1, 0, 0); + + luaL_unref (luaState, LUA_REGISTRYINDEX, t[0]); + luaL_unref (luaState, LUA_REGISTRYINDEX, t[1]); + + if (r) { + logprintf ("lua: %s\n", lua_tostring (luaState, -1)); + } + tfree (*(void **)(t + 2), sizeof (struct event_timer)); + tfree (t, 16); + return 0; +} + +static int postpone_from_lua (lua_State *L) { + int n = lua_gettop (L); + if (n != 3) { + lua_pushboolean (L, 0); + return 1; + } + + double timeout = lua_tonumber (L, -1); + if (timeout < 0) { + lua_pushboolean (L, 0); + return 1; + } + + lua_pop (L, 1); + int a1 = luaL_ref (L, LUA_REGISTRYINDEX); + int a2 = luaL_ref (L, LUA_REGISTRYINDEX); + + struct event_timer *ev = talloc (sizeof (*ev)); + int *t = talloc (16); + t[0] = a1; + t[1] = a2; + *(void **)(t + 2) = ev; + + ev->timeout = get_double_time () + timeout; + ev->alarm = (void *)lua_postpone_alarm; + ev->self = t; + insert_event_timer (ev); + + lua_pushboolean (L, 1); + return 1; +} + void lua_init (const char *file) { if (!file) { return; } have_file = 1; @@ -480,6 +537,7 @@ void lua_init (const char *file) { lua_register (luaState, "send_msg", send_msg_from_lua); lua_register (luaState, "fwd_msg", fwd_msg_from_lua); lua_register (luaState, "mark_read", mark_read_from_lua); + lua_register (luaState, "postpone", postpone_from_lua); int ret = luaL_dofile (luaState, file); if (ret) { diff --git a/structures.c b/structures.c index cff88bb..71d5f2f 100644 --- a/structures.c +++ b/structures.c @@ -28,7 +28,6 @@ #include "telegram.h" #include "tree.h" #include "loop.h" -#include #include #include #include "queries.h"