From bc354b96cedb94802cde2988cc23ba3ea4ce9083 Mon Sep 17 00:00:00 2001 From: vysheng Date: Tue, 28 Jan 2014 16:40:35 +0400 Subject: [PATCH] Added mark_read to lua functions --- lua-tg.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ tools.c | 15 +++++++++++++++ tools.h | 1 + 3 files changed, 60 insertions(+) diff --git a/lua-tg.c b/lua-tg.c index 264d565..73153e3 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -365,6 +365,13 @@ void lua_do_all (void) { do_forward_message (((peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1]); p += 2; break; + case 2: + #ifdef DEBUG + texists (lua_ptr[p], sizeof (peer_t)); + #endif + do_mark_read (((peer_t *)lua_ptr[p])->id); + p += 1; + break; default: assert (0); } @@ -384,6 +391,10 @@ static int send_msg_from_lua (lua_State *L) { return 1; } const char *s = lua_tostring (L, -2); + if (!s) { + lua_pushboolean (L, 0); + return 1; + } const char *msg = lua_tostring (L, -1); peer_t *P = get_peer (s); @@ -414,6 +425,10 @@ static int fwd_msg_from_lua (lua_State *L) { } const char *s = lua_tostring (L, -2); long long num = atoll (lua_tostring (L, -1)); + if (!s) { + lua_pushboolean (L, 0); + return 1; + } peer_t *P = get_peer (s); if (!P) { lua_pushboolean (L, 0); @@ -428,6 +443,34 @@ static int fwd_msg_from_lua (lua_State *L) { return 1; } +static int mark_read_from_lua (lua_State *L) { + if (MAX_LUA_COMMANDS - pos < 4) { + lua_pushboolean (L, 0); + return 1; + } + int n = lua_gettop (L); + if (n != 1) { + lua_pushboolean (L, 0); + return 1; + } + const char *s = lua_tostring (L, -1); + if (!s) { + lua_pushboolean (L, 0); + return 1; + } + peer_t *P = get_peer (s); + if (!P) { + lua_pushboolean (L, 0); + return 1; + } + + lua_ptr[pos ++] = (void *)1l; + lua_ptr[pos ++] = (void *)2l; + lua_ptr[pos ++] = P; + lua_pushboolean (L, 1); + return 1; +} + void lua_init (const char *file) { if (!file) { return; } have_file = 1; @@ -436,6 +479,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); int ret = luaL_dofile (luaState, file); if (ret) { diff --git a/tools.c b/tools.c index 74ff5d2..c9c4255 100644 --- a/tools.c +++ b/tools.c @@ -245,4 +245,19 @@ void tcheck (void) { } logprintf ("ok. Used_blocks = %d. Free blocks = %d\n", used_blocks, free_blocks_cnt); } + +void texists (void *ptr, int size) { + ptr -= RES_PRE; + if (size != (int)((*(int *)ptr) ^ 0xbedabeda)) { + logprintf ("size = %d, ptr = %d\n", size, (*(int *)ptr) ^ 0xbedabeda); + } + assert (*(int *)ptr == (int)((size) ^ 0xbedabeda)); + assert (*(int *)(ptr + RES_PRE + size) == (int)((size) ^ 0x7bed7bed)); + assert (*(int *)(ptr + 4) == size); + int block_num = *(int *)(ptr + 4 + RES_PRE + size); + if (block_num >= used_blocks) { + logprintf ("block_num = %d, used = %d\n", block_num, used_blocks); + } + assert (block_num < used_blocks); +} #endif diff --git a/tools.h b/tools.h index 09f2b2b..e63b133 100644 --- a/tools.h +++ b/tools.h @@ -39,5 +39,6 @@ int tasprintf (char **res, const char *format, ...) __attribute__ ((format (prin #ifdef DEBUG void tcheck (void); +void texists (void *ptr, int size); #endif #endif