Commit 6b0d354e authored by Vysheng's avatar Vysheng

Added partial lua support

parent 4b92fc1c
srcdir=.
CFLAGS=-g -O2
CFLAGS=-g -O2 -I/usr/include/lua5.2
LDFLAGS=
CPPFLAGS=
DEFS=-DHAVE_CONFIG_H
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -Wno-deprecated -fno-strict-aliasing -fno-omit-frame-pointer -ggdb
EXTRA_LIBS= -lreadline -lrt -lconfig
EXTRA_LIBS= -lreadline -lrt -lconfig -llua5.2 -ldl
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -rdynamic -ggdb ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h ${srcdir}/tools.h ${srcdir}/lua-tg.h
INCLUDE=-I. -I${srcdir}
CC=gcc
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o
.SUFFIXES:
......
......@@ -10,10 +10,10 @@ EXTRA_LIBS=@EXTRA_LIBS@
LOCAL_LDFLAGS=-lm -lcrypto -lz -lssl -rdynamic -ggdb ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h
HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h ${srcdir}/tools.h ${srcdir}/lua-tg.h
INCLUDE=-I. -I${srcdir}
CC=@CC@
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o
OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o
.SUFFIXES:
......
This diff is collapsed.
......@@ -16,6 +16,10 @@
Copyright Vitaly Valtman 2013
*/
#include "config.h"
#ifdef USE_LUA
# include "lua-tg.h"
#endif
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
......@@ -126,6 +130,9 @@ void replay_log_event (void) {
rptr ++;
{
our_id = *(rptr ++);
#ifdef USE_LUA
lua_our_id (our_id);
#endif
}
break;
case LOG_DC_SIGNED:
......@@ -280,6 +287,9 @@ void replay_log_event (void) {
struct secret_chat *U = (void *)user_chat_get (id);
assert (U);
U->state = sc_ok;
#ifdef USE_LUA
lua_secret_chat_created (U);
#endif
}
break;
case CODE_binlog_new_user:
......@@ -307,6 +317,10 @@ void replay_log_event (void) {
if (fetch_int ()) {
U->flags |= FLAG_USER_CONTACT;
}
#ifdef USE_LUA
lua_user_update (U);
#endif
}
rptr = in_ptr;
break;
......@@ -337,6 +351,10 @@ void replay_log_event (void) {
assert (U);
if (U->user.phone) { tfree_str (U->user.phone); }
U->user.phone = fetch_str_dup ();
#ifdef USE_LUA
lua_user_update (&U->user);
#endif
}
rptr = in_ptr;
break;
......@@ -383,6 +401,10 @@ void replay_log_event (void) {
if (U->user.real_last_name) { tfree_str (U->user.real_last_name); }
U->user.real_first_name = fetch_str_dup ();
U->user.real_last_name = fetch_str_dup ();
#ifdef USE_LUA
lua_user_update (&U->user);
#endif
}
rptr = in_ptr;
break;
......@@ -519,7 +541,6 @@ void replay_log_event (void) {
memcpy (encr_prime, rptr, 256);
rptr += 64;
encr_param_version = *(rptr ++);
}
break;
case CODE_binlog_encr_chat_init:
......@@ -579,6 +600,10 @@ void replay_log_event (void) {
C->version = fetch_int ();
fetch_data (&C->photo_big, sizeof (struct file_location));
fetch_data (&C->photo_small, sizeof (struct file_location));
#ifdef USE_LUA
lua_chat_update (C);
#endif
};
rptr = in_ptr;
break;
......@@ -600,6 +625,9 @@ void replay_log_event (void) {
if (C->title) { tfree_str (C->title); }
C->title = fetch_str_dup ();
C->print_title = create_print_name (C->id, C->title, 0, 0, 0);
#ifdef USE_LUA
lua_chat_update (C);
#endif
};
rptr = in_ptr;
break;
......@@ -619,6 +647,9 @@ void replay_log_event (void) {
peer_t *C = user_chat_get (MK_CHAT (*(rptr ++)));
assert (C && (C->flags & FLAG_CREATED));
C->chat.date = *(rptr ++);
#ifdef USE_LUA
lua_chat_update (&C->chat);
#endif
};
break;
case CODE_binlog_set_chat_version:
......@@ -628,6 +659,9 @@ void replay_log_event (void) {
assert (C && (C->flags & FLAG_CREATED));
C->chat.version = *(rptr ++);
C->chat.users_num = *(rptr ++);
#ifdef USE_LUA
lua_chat_update (&C->chat);
#endif
};
break;
case CODE_binlog_set_chat_admin:
......@@ -636,6 +670,9 @@ void replay_log_event (void) {
peer_t *C = user_chat_get (MK_CHAT (*(rptr ++)));
assert (C && (C->flags & FLAG_CREATED));
C->chat.admin_id = *(rptr ++);
#ifdef USE_LUA
lua_chat_update (&C->chat);
#endif
};
break;
case CODE_binlog_set_chat_participants:
......@@ -649,6 +686,9 @@ void replay_log_event (void) {
C->chat.user_list = talloc (12 * C->chat.user_list_size);
memcpy (C->chat.user_list, rptr, 12 * C->chat.user_list_size);
rptr += 3 * C->chat.user_list_size;
#ifdef USE_LUA
lua_chat_update (&C->chat);
#endif
};
break;
case CODE_binlog_chat_full_photo:
......@@ -688,6 +728,9 @@ void replay_log_event (void) {
C->user_list[C->user_list_size - 1].inviter_id = inviter;
C->user_list[C->user_list_size - 1].date = date;
C->user_list_version = version;
#ifdef USE_LUA
lua_chat_update (C);
#endif
}
break;
case CODE_binlog_del_chat_participant:
......@@ -715,6 +758,9 @@ void replay_log_event (void) {
C->user_list_size --;
C->user_list = trealloc (C->user_list, 12 * C->user_list_size + 12, 12 * C->user_list_size);
C->user_list_version = version;
#ifdef USE_LUA
lua_chat_update (C);
#endif
}
break;
case CODE_binlog_create_message_text:
......@@ -764,6 +810,10 @@ void replay_log_event (void) {
message_insert_unsent (M);
M->flags |= FLAG_PENDING;
}
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -799,6 +849,9 @@ void replay_log_event (void) {
M->out = get_peer_id (M->from_id) == our_id;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -832,6 +885,9 @@ void replay_log_event (void) {
M->out = get_peer_id (M->from_id) == our_id;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -867,6 +923,9 @@ void replay_log_event (void) {
M->out = get_peer_id (M->from_id) == our_id;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -902,6 +961,9 @@ void replay_log_event (void) {
M->out = get_peer_id (M->from_id) == our_id;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -930,6 +992,9 @@ void replay_log_event (void) {
M->service = 1;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -959,6 +1024,9 @@ void replay_log_event (void) {
M->service = 1;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -988,6 +1056,9 @@ void replay_log_event (void) {
M->service = 1;
message_insert (M);
#ifdef USE_LUA
lua_new_msg (M);
#endif
}
rptr = in_ptr;
break;
......@@ -1043,6 +1114,7 @@ void replay_log_event (void) {
}
message_remove_tree (M);
message_del_peer (M);
message_del_use (M);
free_message (M);
tfree (M, sizeof (*M));
}
......
......@@ -10,6 +10,9 @@
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* Define to 1 if you have the <lauxlib.h> header file. */
#define HAVE_LAUXLIB_H 1
/* Define to 1 if you have the `crypto' library (-lcrypto). */
#define HAVE_LIBCRYPTO 1
......@@ -22,6 +25,15 @@
/* Define to 1 if you have the `m' library (-lm). */
#define HAVE_LIBM 1
/* Define to 1 if you have the <luaconf.h> header file. */
#define HAVE_LUACONF_H 1
/* Define to 1 if you have the <lualib.h> header file. */
#define HAVE_LUALIB_H 1
/* Define to 1 if you have the <lua.h> header file. */
#define HAVE_LUA_H 1
/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
to 0 otherwise. */
#define HAVE_MALLOC 1
......@@ -99,6 +111,9 @@
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* use lua */
#define USE_LUA 1
/* Define to rpl_malloc if the replacement function should be used. */
/* #undef malloc */
......
......@@ -9,6 +9,9 @@
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <lauxlib.h> header file. */
#undef HAVE_LAUXLIB_H
/* Define to 1 if you have the `crypto' library (-lcrypto). */
#undef HAVE_LIBCRYPTO
......@@ -21,6 +24,15 @@
/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM
/* Define to 1 if you have the <luaconf.h> header file. */
#undef HAVE_LUACONF_H
/* Define to 1 if you have the <lualib.h> header file. */
#undef HAVE_LUALIB_H
/* Define to 1 if you have the <lua.h> header file. */
#undef HAVE_LUA_H
/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
to 0 otherwise. */
#undef HAVE_MALLOC
......@@ -98,6 +110,9 @@
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* use lua */
#undef USE_LUA
/* Define to rpl_malloc if the replacement function should be used. */
#undef malloc
......
This diff is collapsed.
......@@ -3,6 +3,8 @@ AC_INIT([telegram], [0.1])
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])
m4_include([ax_lua.m4])
# Checks for programs.
AC_PROG_CC
......@@ -46,7 +48,7 @@ AC_CHECK_LIB([rt], [clock_gettime],
]
)
AC_MSG_CHECKING([Checking for libconfig])
AC_MSG_CHECKING([for libconfig])
AC_ARG_ENABLE(libconfig,[--enable-libconfig/--disable-libconfig],
[
if test "x$enableval" = "xno" ; then
......@@ -61,6 +63,40 @@ AC_ARG_ENABLE(libconfig,[--enable-libconfig/--disable-libconfig],
AC_CHECK_LIB([config],[config_init],AC_DEFINE(ENABLE_LIBCONFIG,1,[enable libconfig]),AC_MSG_ERROR([No libconfig found. Try --disable-libconfig]))
[EXTRA_LIBS="${EXTRA_LIBS} -lconfig" ; ]
])
AC_MSG_CHECKING([for liblua])
AC_ARG_ENABLE(liblua,[--enable-liblua/--disable-liblua],
[
if test "x$enableval" = "xno" ; then
AC_MSG_RESULT([disabled])
else
AC_MSG_RESULT([enabled])
AX_PROG_LUA([],[],
[
AX_LUA_HEADERS([],[AC_MSG_ERROR([No lua headers found. Try --disable-liblua])])
AX_LUA_LIBS([],[AC_MSG_ERROR([No lua libs found. Try --disable-liblua])])
[EXTRA_LIBS="${EXTRA_LIBS} ${LUA_LIB}" ; ]
[CFLAGS="${CFLAGS} ${LUA_INCLUDE}" ; ]
AC_DEFINE(USE_LUA,1,[use lua])
],
[
AC_MSG_ERROR([No lua found. Try --disable-liblua])
])
fi
],[
AC_MSG_RESULT([enabled])
AX_PROG_LUA([],[],
[
AX_LUA_HEADERS([],[AC_MSG_ERROR([No lua headers found. Try --disable-liblua])])
AX_LUA_LIBS([],[AC_MSG_ERROR([No lua libs found. Try --disable-liblua])])
[EXTRA_LIBS="${EXTRA_LIBS} ${LUA_LIB}" ; ]
[CFLAGS="${CFLAGS} ${LUA_INCLUDE}" ; ]
AC_DEFINE(USE_LUA,1,[use lua])
],
[
AC_MSG_ERROR([No lua found. Try --disable-liblua])
])
])
# Checks for header files.
AC_CHECK_HEADERS([fcntl.h malloc.h stdlib.h string.h sys/socket.h unistd.h])
......
......@@ -48,6 +48,7 @@
#include "telegram.h"
#include "loop.h"
#include "binlog.h"
#include "lua-tg.h"
extern char *default_username;
extern char *auth_token;
......@@ -95,6 +96,9 @@ void net_loop (int flags, int (*is_end)(void)) {
}
}
connections_poll_result (fds + cc, x - cc);
#ifdef USE_LUA
lua_do_all ();
#endif
if (safe_quit && !queries_num) {
printf ("All done. Exit\n");
rl_callback_handler_remove ();
......@@ -448,6 +452,9 @@ int loop (void) {
replay_log ();
logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
write_binlog ();
#ifdef USE_LUA
lua_binlog_end ();
#endif
} else {
read_auth_file ();
}
......@@ -580,8 +587,12 @@ int loop (void) {
do_get_difference ();
net_loop (0, dgot);
#ifdef USE_LUA
lua_diff_end ();
#endif
send_all_unsent ();
do_get_dialog_list ();
return main_loop ();
......
This diff is collapsed.
#ifndef __LUA_TG_H__
#define __LUA_TG_H__
#include <string.h>
#include "structures.h"
void lua_init (const char *file);
void lua_new_msg (struct message *M);
void lua_our_id (int id);
void lua_secret_chat_created (struct secret_chat *U);
void lua_user_update (struct user *U);
void lua_chat_update (struct chat *C);
void lua_binlog_end (void);
void lua_diff_end (void);
void lua_do_all (void);
#endif
......@@ -47,6 +47,10 @@
#include "interface.h"
#include "tools.h"
#ifdef USE_LUA
# include "lua-tg.h"
#endif
#define PROGNAME "telegram-client"
#define VERSION "0.01"
......@@ -349,9 +353,12 @@ FILE *log_net_f;
int register_mode;
int disable_auto_accept;
char *lua_file;
void args_parse (int argc, char **argv) {
int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vn:Nc:p:l:RfBL:E")) != -1) {
while ((opt = getopt (argc, argv, "u:hk:vn:Nc:p:l:RfBL:Es:")) != -1) {
switch (opt) {
case 'u':
set_default_username (optarg);
......@@ -398,6 +405,9 @@ void args_parse (int argc, char **argv) {
case 'w':
allow_weak_random = 1;
break;
case 's':
lua_file = tstrdup (optarg);
break;
case 'h':
default:
usage ();
......@@ -410,13 +420,13 @@ void print_backtrace (void) {
void *buffer[255];
const int calls = backtrace (buffer, sizeof (buffer) / sizeof (void *));
backtrace_symbols_fd (buffer, calls, 1);
exit(EXIT_FAILURE);
}
void sig_handler (int signum) {
set_terminal_attributes ();
printf ("Signal %d received\n", signum);
print_backtrace ();
exit(EXIT_FAILURE);
}
......@@ -439,6 +449,11 @@ int main (int argc, char **argv) {
get_terminal_attributes ();
#ifdef USE_LUA
if (lua_file) {
lua_init (lua_file);
}
#endif
inner_main ();
......
......@@ -306,10 +306,15 @@ static inline char *fetch_str (int len) {
static inline char *fetch_str_dup (void) {
int l = prefetch_strlen ();
assert (l >= 0);
char *s = talloc (l + 1);
memcpy (s, fetch_str (l), l);
s[l] = 0;
return s;
int i;
char *s = fetch_str (l);
for (i = 0; i < l; i++) {
if (!s[i]) { break; }
}
char *r = talloc (i + 1);
memcpy (r, s, i);
r[i] = 0;
return r;
}
static inline int fetch_update_str (char **s) {
......@@ -361,6 +366,7 @@ static inline int set_update_int (int *value, int new_value) {
static inline void fetch_skip (int n) {
in_ptr += n;
assert (in_ptr <= in_end);
}
static inline void fetch_skip_str (void) {
......@@ -382,6 +388,7 @@ static inline long have_prefetch_ints (void) {
int fetch_bignum (BIGNUM *x);
static inline int fetch_int (void) {
assert (in_ptr + 1 <= in_end);
if (verbosity > 6) {
logprintf ("fetch_int: 0x%08x (%d)\n", *in_ptr, *in_ptr);
}
......@@ -392,37 +399,44 @@ static inline int fetch_bool (void) {
if (verbosity > 6) {
logprintf ("fetch_bool: 0x%08x (%d)\n", *in_ptr, *in_ptr);
}
assert (in_ptr + 1 <= in_end);
assert (*(in_ptr) == (int)CODE_bool_true || *(in_ptr) == (int)CODE_bool_false);
return *(in_ptr ++) == (int)CODE_bool_true;
}
static inline int prefetch_int (void) {
assert (in_ptr < in_end);
return *(in_ptr);
}
static inline void prefetch_data (void *data, int size) {
assert (in_ptr + (size >> 2) <= in_end);
memcpy (data, in_ptr, size);
}
static inline void fetch_data (void *data, int size) {
assert (in_ptr + (size >> 2) <= in_end);
memcpy (data, in_ptr, size);
assert (!(size & 3));
in_ptr += (size >> 2);
}
static inline long long fetch_long (void) {
assert (in_ptr + 2 <= in_end);
long long r = *(long long *)in_ptr;
in_ptr += 2;
return r;
}
static inline double fetch_double (void) {
assert (in_ptr + 2 <= in_end);
double r = *(double *)in_ptr;
in_ptr += 2;
return r;
}
static inline void fetch_ints (void *data, int count) {
assert (in_ptr + count <= in_end);
memcpy (data, in_ptr, 4 * count);
in_ptr += count;
}
......
......@@ -904,7 +904,9 @@ void do_send_message (peer_id_t id, const char *msg, int len) {
return;
}
}
long long t = -lrand48 () * (1ll << 32) - lrand48 ();
long long t;
secure_random (&t, 8);
logprintf ("t = %lld, len = %d\n", t, len);
bl_do_send_message_text (t, our_id, get_peer_type (id), get_peer_id (id), time (0), len, msg);
struct message *M = message_get (t);
assert (M);
......
......@@ -78,6 +78,9 @@ void tfree (void *ptr, int size __attribute__ ((unused))) {
#ifdef DEBUG
total_allocated_bytes -= 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);
......
......@@ -24,6 +24,7 @@
#include <tools.h>
#include <assert.h>
#pragma pack(push,4)
#define DEFINE_TREE(X_NAME, X_TYPE, X_CMP, X_UNSET) \
struct tree_ ## X_NAME { \
struct tree_ ## X_NAME *left, *right;\
......@@ -58,6 +59,7 @@ void tree_split_ ## X_NAME (struct tree_ ## X_NAME *T, X_TYPE x, struct tree_ ##
}\
}\
\
struct tree_ ## X_NAME *tree_insert_ ## X_NAME (struct tree_ ## X_NAME *T, X_TYPE x, int y) __attribute__ ((warn_unused_result));\
struct tree_ ## X_NAME *tree_insert_ ## X_NAME (struct tree_ ## X_NAME *T, X_TYPE x, int y) {\
if (!T) {\
return new_tree_node_ ## X_NAME (x, y);\
......@@ -93,6 +95,7 @@ struct tree_ ## X_NAME *tree_merge_ ## X_NAME (struct tree_ ## X_NAME *L, struct
}\
}\
\
struct tree_ ## X_NAME *tree_delete_ ## X_NAME (struct tree_ ## X_NAME *T, X_TYPE x) __attribute__ ((warn_unused_result));\
struct tree_ ## X_NAME *tree_delete_ ## X_NAME (struct tree_ ## X_NAME *T, X_TYPE x) {\
assert (T);\
int c = X_CMP (x, T->x);\
......@@ -150,4 +153,5 @@ void tree_check_ ## X_NAME (struct tree_ ## X_NAME *T) { \
}\
#define int_cmp(a,b) ((a) - (b))
#pragma pack(pop)
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment