2014-01-13 08:05:25 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2014-01-11 19:43:29 -05:00
|
|
|
#include "config.h"
|
2014-01-13 08:05:25 -05:00
|
|
|
#endif
|
2014-01-11 19:43:29 -05:00
|
|
|
|
|
|
|
#ifdef USE_LUA
|
|
|
|
#include "lua-tg.h"
|
|
|
|
|
|
|
|
#include "include.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <lua.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
2014-08-14 18:16:01 -04:00
|
|
|
#include <event2/event.h>
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_State *luaState;
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
//#include "interface.h"
|
|
|
|
//#include "auto/constants.h"
|
2014-08-13 11:55:16 -04:00
|
|
|
#include "tgl.h"
|
2014-08-21 11:38:51 -04:00
|
|
|
#include "interface.h"
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
#include <assert.h>
|
2014-01-11 19:43:29 -05:00
|
|
|
extern int verbosity;
|
|
|
|
|
|
|
|
static int have_file;
|
|
|
|
|
|
|
|
#define my_lua_checkstack(L,x) assert (lua_checkstack (L, x))
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_user (tgl_peer_t *P UU);
|
|
|
|
void push_peer (tgl_peer_id_t id, tgl_peer_t *P);
|
2014-01-11 19:43:29 -05:00
|
|
|
|
|
|
|
void lua_add_string_field (const char *name, const char *value) {
|
|
|
|
assert (name && strlen (name));
|
|
|
|
if (!value || !strlen (value)) { return; }
|
|
|
|
my_lua_checkstack (luaState, 3);
|
|
|
|
lua_pushstring (luaState, name);
|
|
|
|
lua_pushstring (luaState, value);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
void lua_add_string_field_arr (int num, const char *value) {
|
|
|
|
if (!value || !strlen (value)) { return; }
|
|
|
|
my_lua_checkstack (luaState, 3);
|
|
|
|
lua_pushnumber (luaState, num);
|
|
|
|
lua_pushstring (luaState, value);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
void lua_add_num_field (const char *name, double value) {
|
|
|
|
assert (name && strlen (name));
|
|
|
|
my_lua_checkstack (luaState, 3);
|
|
|
|
lua_pushstring (luaState, name);
|
|
|
|
lua_pushnumber (luaState, value);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_tgl_peer_type (int x) {
|
2014-01-11 19:43:29 -05:00
|
|
|
switch (x) {
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_USER:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "user");
|
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_CHAT:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "chat");
|
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_ENCR_CHAT:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "encr_chat");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_user (tgl_peer_t *P UU) {
|
2014-01-11 19:43:29 -05:00
|
|
|
my_lua_checkstack (luaState, 4);
|
|
|
|
lua_add_string_field ("first_name", P->user.first_name);
|
|
|
|
lua_add_string_field ("last_name", P->user.last_name);
|
|
|
|
lua_add_string_field ("real_first_name", P->user.real_first_name);
|
|
|
|
lua_add_string_field ("real_last_name", P->user.real_last_name);
|
|
|
|
lua_add_string_field ("phone", P->user.phone);
|
2014-08-21 11:38:51 -04:00
|
|
|
if (P->user.access_hash) {
|
|
|
|
lua_add_num_field ("access_hash", 1);
|
|
|
|
}
|
2014-01-11 19:43:29 -05:00
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_chat (tgl_peer_t *P) {
|
2014-01-11 19:43:29 -05:00
|
|
|
my_lua_checkstack (luaState, 4);
|
|
|
|
assert (P->chat.title);
|
|
|
|
lua_add_string_field ("title", P->chat.title);
|
|
|
|
lua_add_num_field ("members_num", P->chat.users_num);
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_encr_chat (tgl_peer_t *P) {
|
2014-01-11 19:43:29 -05:00
|
|
|
my_lua_checkstack (luaState, 4);
|
|
|
|
lua_pushstring (luaState, "user");
|
2014-08-13 11:55:16 -04:00
|
|
|
push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id)));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
void push_update_types (unsigned flags) {
|
|
|
|
my_lua_checkstack (luaState, 4);
|
|
|
|
lua_newtable (luaState);
|
|
|
|
int cc = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (flags & TGL_UPDATE_CREATED) {
|
|
|
|
lua_add_string_field_arr (cc++, "created");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_DELETED) {
|
|
|
|
lua_add_string_field_arr (cc++, "deleted");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_PHONE) {
|
|
|
|
lua_add_string_field_arr (cc++, "phone");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_CONTACT) {
|
|
|
|
lua_add_string_field_arr (cc++, "contact");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_PHOTO) {
|
|
|
|
lua_add_string_field_arr (cc++, "photo");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_BLOCKED) {
|
|
|
|
lua_add_string_field_arr (cc++, "blocked");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_REAL_NAME) {
|
|
|
|
lua_add_string_field_arr (cc++, "real_name");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_NAME) {
|
|
|
|
lua_add_string_field_arr (cc++, "name");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_REQUESTED) {
|
|
|
|
lua_add_string_field_arr (cc++, "requested");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_WORKING) {
|
|
|
|
lua_add_string_field_arr (cc++, "working");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_FLAGS) {
|
|
|
|
lua_add_string_field_arr (cc++, "flags");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_TITLE) {
|
|
|
|
lua_add_string_field_arr (cc++, "title");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_ADMIN) {
|
|
|
|
lua_add_string_field_arr (cc++, "admin");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_MEMBERS) {
|
|
|
|
lua_add_string_field_arr (cc++, "members");
|
|
|
|
}
|
|
|
|
if (flags & TGL_UPDATE_ACCESS_HASH) {
|
|
|
|
lua_add_string_field_arr (cc++, "access_hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_peer (tgl_peer_id_t id, tgl_peer_t *P) {
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_newtable (luaState);
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
lua_add_num_field ("id", tgl_get_peer_id (id));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "type");
|
2014-08-13 11:55:16 -04:00
|
|
|
push_tgl_peer_type (tgl_get_peer_type (id));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
|
|
|
|
if (!P || !(P->flags & FLAG_CREATED)) {
|
|
|
|
lua_pushstring (luaState, "print_name");
|
|
|
|
static char s[100];
|
2014-08-13 11:55:16 -04:00
|
|
|
switch (tgl_get_peer_type (id)) {
|
|
|
|
case TGL_PEER_USER:
|
|
|
|
sprintf (s, "user#%d", tgl_get_peer_id (id));
|
2014-01-11 19:43:29 -05:00
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_CHAT:
|
|
|
|
sprintf (s, "chat#%d", tgl_get_peer_id (id));
|
2014-01-11 19:43:29 -05:00
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_ENCR_CHAT:
|
|
|
|
sprintf (s, "encr_chat#%d", tgl_get_peer_id (id));
|
2014-01-11 19:43:29 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
lua_pushstring (luaState, s);
|
|
|
|
lua_settable (luaState, -3); // flags
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_add_string_field ("print_name", P->print_name);
|
|
|
|
lua_add_num_field ("flags", P->flags);
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
switch (tgl_get_peer_type (id)) {
|
|
|
|
case TGL_PEER_USER:
|
2014-01-11 19:43:29 -05:00
|
|
|
push_user (P);
|
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_CHAT:
|
2014-01-11 19:43:29 -05:00
|
|
|
push_chat (P);
|
|
|
|
break;
|
2014-08-13 11:55:16 -04:00
|
|
|
case TGL_PEER_ENCR_CHAT:
|
2014-01-11 19:43:29 -05:00
|
|
|
push_encr_chat (P);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_media (struct tgl_message_media *M) {
|
2014-01-11 19:43:29 -05:00
|
|
|
my_lua_checkstack (luaState, 4);
|
|
|
|
|
|
|
|
switch (M->type) {
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_photo:
|
|
|
|
case tgl_message_media_photo_encr:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "photo");
|
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_video:
|
|
|
|
case tgl_message_media_video_encr:
|
2014-01-11 19:43:29 -05:00
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_audio:
|
|
|
|
case tgl_message_media_audio_encr:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "audio");
|
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_document:
|
|
|
|
case tgl_message_media_document_encr:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "document");
|
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_unsupported:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "unsupported");
|
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_geo:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_newtable (luaState);
|
|
|
|
lua_add_num_field ("longitude", M->geo.longitude);
|
|
|
|
lua_add_num_field ("latitude", M->geo.latitude);
|
|
|
|
break;
|
2014-08-21 11:38:51 -04:00
|
|
|
case tgl_message_media_contact:
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_newtable (luaState);
|
|
|
|
lua_add_string_field ("phone", M->phone);
|
|
|
|
lua_add_string_field ("first_name", M->first_name);
|
|
|
|
lua_add_string_field ("last_name", M->last_name);
|
|
|
|
lua_add_num_field ("user_id", M->user_id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_pushstring (luaState, "???");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void push_message (struct tgl_message *M) {
|
2014-01-11 19:43:29 -05:00
|
|
|
assert (M);
|
|
|
|
my_lua_checkstack (luaState, 10);
|
|
|
|
lua_newtable (luaState);
|
|
|
|
|
|
|
|
static char s[30];
|
2014-08-14 18:16:01 -04:00
|
|
|
snprintf (s, 30, "%lld", M->id);
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_add_string_field ("id", s);
|
|
|
|
lua_add_num_field ("flags", M->flags);
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
if (tgl_get_peer_type (M->fwd_from_id)) {
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "fwd_from");
|
2014-08-13 11:55:16 -04:00
|
|
|
push_peer (M->fwd_from_id, tgl_peer_get (M->fwd_from_id));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_settable (luaState, -3); // fwd_from
|
|
|
|
|
|
|
|
lua_add_num_field ("fwd_date", M->fwd_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "from");
|
2014-08-13 11:55:16 -04:00
|
|
|
push_peer (M->from_id, tgl_peer_get (M->from_id));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "to");
|
2014-08-13 11:55:16 -04:00
|
|
|
push_peer (M->to_id, tgl_peer_get (M->to_id));
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "out");
|
|
|
|
lua_pushboolean (luaState, M->out);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "unread");
|
|
|
|
lua_pushboolean (luaState, M->unread);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "date");
|
|
|
|
lua_pushnumber (luaState, M->date);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
lua_pushstring (luaState, "service");
|
|
|
|
lua_pushboolean (luaState, M->service);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
|
|
|
|
if (!M->service) {
|
|
|
|
if (M->message_len && M->message) {
|
|
|
|
lua_pushstring (luaState, "text");
|
|
|
|
lua_pushlstring (luaState, M->message, M->message_len);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
2014-08-21 11:38:51 -04:00
|
|
|
if (M->media.type && M->media.type != tgl_message_media_none) {
|
2014-01-11 19:43:29 -05:00
|
|
|
lua_pushstring (luaState, "media");
|
|
|
|
push_media (&M->media);
|
|
|
|
lua_settable (luaState, -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lua_binlog_end (void) {
|
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_binlog_replay_end");
|
|
|
|
assert (lua_gettop (luaState) == 1);
|
|
|
|
|
|
|
|
int r = lua_pcall (luaState, 0, 0, 0);
|
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lua_diff_end (void) {
|
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_get_difference_end");
|
|
|
|
assert (lua_gettop (luaState) == 1);
|
|
|
|
|
|
|
|
int r = lua_pcall (luaState, 0, 0, 0);
|
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lua_our_id (int id) {
|
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_our_id");
|
|
|
|
lua_pushnumber (luaState, id);
|
|
|
|
assert (lua_gettop (luaState) == 2);
|
|
|
|
|
|
|
|
int r = lua_pcall (luaState, 1, 0, 0);
|
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
void lua_new_msg (struct tgl_message *M UU) {
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_msg_receive");
|
|
|
|
push_message (M);
|
|
|
|
assert (lua_gettop (luaState) == 2);
|
|
|
|
|
|
|
|
int r = lua_pcall (luaState, 1, 0, 0);
|
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
void lua_secret_chat_update (struct tgl_secret_chat *C, unsigned flags) {
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
2014-08-21 11:38:51 -04:00
|
|
|
lua_getglobal (luaState, "on_secret_chat_update");
|
2014-01-11 19:43:29 -05:00
|
|
|
push_peer (C->id, (void *)C);
|
2014-08-21 11:38:51 -04:00
|
|
|
push_update_types (flags);
|
|
|
|
assert (lua_gettop (luaState) == 3);
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
int r = lua_pcall (luaState, 2, 0, 0);
|
2014-01-11 19:43:29 -05:00
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
void lua_user_update (struct tgl_user *U, unsigned flags) {
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_user_update");
|
|
|
|
push_peer (U->id, (void *)U);
|
2014-08-21 11:38:51 -04:00
|
|
|
push_update_types (flags);
|
|
|
|
assert (lua_gettop (luaState) == 3);
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
int r = lua_pcall (luaState, 2, 0, 0);
|
2014-01-11 19:43:29 -05:00
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
void lua_chat_update (struct tgl_chat *C, unsigned flags) {
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!have_file) { return; }
|
|
|
|
lua_settop (luaState, 0);
|
|
|
|
//lua_checkstack (luaState, 20);
|
|
|
|
my_lua_checkstack (luaState, 20);
|
|
|
|
lua_getglobal (luaState, "on_chat_update");
|
|
|
|
push_peer (C->id, (void *)C);
|
2014-08-21 11:38:51 -04:00
|
|
|
push_update_types (flags);
|
|
|
|
assert (lua_gettop (luaState) == 3);
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
int r = lua_pcall (luaState, 2, 0, 0);
|
2014-01-11 19:43:29 -05:00
|
|
|
if (r) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
//extern tgl_peer_t *Peers[];
|
|
|
|
//extern int peer_num;
|
2014-01-11 19:43:29 -05:00
|
|
|
|
|
|
|
#define MAX_LUA_COMMANDS 1000
|
|
|
|
void *lua_ptr[MAX_LUA_COMMANDS];
|
|
|
|
static int pos;
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
static tgl_peer_t *get_peer (const char *s) {
|
|
|
|
return tgl_peer_get_by_name (s);
|
2014-01-11 19:43:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void lua_do_all (void) {
|
|
|
|
int p = 0;
|
|
|
|
while (p < pos) {
|
|
|
|
int l = (long)lua_ptr[p ++];
|
|
|
|
assert (p + l + 1 <= pos);
|
|
|
|
int f = (long)lua_ptr[p ++];
|
|
|
|
switch (f) {
|
|
|
|
case 0:
|
2014-08-14 18:16:01 -04:00
|
|
|
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1]), 0, 0);
|
|
|
|
free (lua_ptr[p + 1]);
|
2014-01-11 19:43:29 -05:00
|
|
|
p += 2;
|
|
|
|
break;
|
|
|
|
case 1:
|
2014-08-14 18:16:01 -04:00
|
|
|
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1], 0, 0);
|
2014-01-11 19:43:29 -05:00
|
|
|
p += 2;
|
|
|
|
break;
|
2014-01-28 07:40:35 -05:00
|
|
|
case 2:
|
|
|
|
#ifdef DEBUG
|
2014-08-13 11:55:16 -04:00
|
|
|
texists (lua_ptr[p], sizeof (tgl_peer_t));
|
2014-01-28 07:40:35 -05:00
|
|
|
#endif
|
2014-08-14 18:16:01 -04:00
|
|
|
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id, 0, 0);
|
2014-01-28 07:40:35 -05:00
|
|
|
p += 1;
|
|
|
|
break;
|
2014-01-11 19:43:29 -05:00
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int send_msg_from_lua (lua_State *L) {
|
|
|
|
if (MAX_LUA_COMMANDS - pos < 4) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int n = lua_gettop (L);
|
|
|
|
if (n != 2) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char *s = lua_tostring (L, -2);
|
2014-01-28 07:40:35 -05:00
|
|
|
if (!s) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-11 19:43:29 -05:00
|
|
|
const char *msg = lua_tostring (L, -1);
|
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_peer_t *P = get_peer (s);
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!P) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_ptr[pos ++] = (void *)2l;
|
|
|
|
lua_ptr[pos ++] = (void *)0l;
|
|
|
|
lua_ptr[pos ++] = P;
|
2014-08-14 18:16:01 -04:00
|
|
|
lua_ptr[pos ++] = strdup (msg);
|
2014-01-11 19:43:29 -05:00
|
|
|
logprintf ("msg = %s\n", msg);
|
|
|
|
|
|
|
|
lua_pushboolean (L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fwd_msg_from_lua (lua_State *L) {
|
|
|
|
if (MAX_LUA_COMMANDS - pos < 4) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int n = lua_gettop (L);
|
|
|
|
if (n != 2) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char *s = lua_tostring (L, -2);
|
|
|
|
long long num = atoll (lua_tostring (L, -1));
|
2014-01-28 07:40:35 -05:00
|
|
|
if (!s) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_peer_t *P = get_peer (s);
|
2014-01-11 19:43:29 -05:00
|
|
|
if (!P) {
|
|
|
|
lua_pushboolean (L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_ptr[pos ++] = (void *)2l;
|
|
|
|
lua_ptr[pos ++] = (void *)1l;
|
|
|
|
lua_ptr[pos ++] = P;
|
|
|
|
lua_ptr[pos ++] = (void *)(long)num;
|
|
|
|
lua_pushboolean (L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:40:35 -05:00
|
|
|
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;
|
|
|
|
}
|
2014-08-13 11:55:16 -04:00
|
|
|
tgl_peer_t *P = get_peer (s);
|
2014-01-28 07:40:35 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-14 18:16:01 -04:00
|
|
|
static void lua_postpone_alarm (evutil_socket_t fd, short what, void *arg) {
|
|
|
|
int *t = arg;
|
2014-02-06 13:09:43 -05:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2014-08-14 18:16:01 -04:00
|
|
|
|
2014-02-06 13:09:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-08-14 18:16:01 -04:00
|
|
|
int *t = malloc (16);
|
|
|
|
struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t);
|
2014-02-06 13:09:43 -05:00
|
|
|
t[0] = a1;
|
|
|
|
t[1] = a2;
|
|
|
|
*(void **)(t + 2) = ev;
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
struct timeval ts= { timeout, 0};
|
2014-08-14 18:16:01 -04:00
|
|
|
event_add (ev, &ts);
|
2014-02-06 13:09:43 -05:00
|
|
|
|
|
|
|
lua_pushboolean (L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
void lua_init (const char *file) {
|
|
|
|
if (!file) { return; }
|
|
|
|
have_file = 1;
|
|
|
|
luaState = luaL_newstate ();
|
|
|
|
luaL_openlibs (luaState);
|
|
|
|
|
|
|
|
lua_register (luaState, "send_msg", send_msg_from_lua);
|
|
|
|
lua_register (luaState, "fwd_msg", fwd_msg_from_lua);
|
2014-01-28 07:40:35 -05:00
|
|
|
lua_register (luaState, "mark_read", mark_read_from_lua);
|
2014-02-06 13:09:43 -05:00
|
|
|
lua_register (luaState, "postpone", postpone_from_lua);
|
2014-01-11 19:43:29 -05:00
|
|
|
|
|
|
|
int ret = luaL_dofile (luaState, file);
|
|
|
|
if (ret) {
|
|
|
|
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|