From e5311d0810c6bcb77f872ade031357172b060d68 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Sat, 19 Aug 2023 13:51:41 +0800 Subject: Update TDLib Fix abort when disconnect Version: sync with bot API --- .gitignore | 1 + CMakeLists.txt | 3 ++- botd.h | 2 +- main.c | 5 ++++- query.c | 15 ++++++++------- refresh.c | 20 +++++++++++--------- stat.c | 15 +++++++++++---- td | 2 +- tdutils.c | 49 ++++++++++++++++++++++++------------------------- 9 files changed, 63 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index f846957..13225d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +build/ cmake-build-debug/ cmake-build-release/ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider diff --git a/CMakeLists.txt b/CMakeLists.txt index 6179bfe..e8a6e01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,4 +19,5 @@ add_definitions(-D_POSIX_C_SOURCE=200809L) add_executable(ksyxbotd main.c tdutils.c tdutils.h log.c log.h botd.h db.c db.h cmdline.c store.c logic.h query.c refresh.c stat.c utils.c utils.h) target_include_directories(ksyxbotd PUBLIC "${PROJECT_BINARY_DIR}") -target_link_libraries(ksyxbotd PRIVATE tdc tdsqlite m) +# Starting from whatever commit, tdsqlite3 functions are all prefixed with 'td', so use system SQLite3 instead. +target_link_libraries(ksyxbotd PRIVATE tdc sqlite3 m) diff --git a/botd.h b/botd.h index 84ae7f2..62efcae 100644 --- a/botd.h +++ b/botd.h @@ -8,7 +8,7 @@ #include #include -#define ADMIN 2122005901 +#define ADMIN 230103978 #define CHANNEL -1001794945713 extern time_t start_time; diff --git a/main.c b/main.c index 6b15ce5..ec6ebb7 100644 --- a/main.c +++ b/main.c @@ -32,8 +32,11 @@ int main(int argc, char **argv) { start_time = time(NULL); atexit(&ate); parse_cmdline(argc, argv); + LOGI("Initializing database ..."); db_init(); + LOGI("Initializing TDLib ..."); td_init(); + LOGI("Waiting for updates."); td_loop(); return 0; } @@ -57,4 +60,4 @@ int handle_message(struct TdUpdateNewMessage *update) { } store(msg, &cb_store); return 0; -} \ No newline at end of file +} diff --git a/query.c b/query.c index ff05ff2..3905ae2 100644 --- a/query.c +++ b/query.c @@ -125,15 +125,15 @@ int handle_inline(struct TdUpdateNewInlineQuery *update) { t); } - td_send(TdCreateObjectAnswerInlineQuery(update->id_, - false, - (struct TdVectorInputInlineQueryResult *) + td_send(TdCreateObjectAnswerInlineQuery(update->id_, /* inline_query_id */ + false, /* is_personal */ + NULL, /* button */ + (struct TdVectorInputInlineQueryResult *) /* results */ TdCreateObjectVectorObject(effective_items_count, (struct TdObject **) results_inline), - 0, - NULL, - NULL, - NULL), + 0, /* cache_time */ + NULL /* next_offset */ + ), &cb_answer, NULL); @@ -146,6 +146,7 @@ int handle_inline(struct TdUpdateNewInlineQuery *update) { } int handle_inline_chosen(struct TdUpdateNewChosenInlineResult *update) { + LOGDV("Handling %s", update->result_id_); if (!update->result_id_ || update->result_id_[0] != 'x' || update->result_id_[1] != '_') return 0; diff --git a/refresh.c b/refresh.c index ad932f4..60b4c58 100644 --- a/refresh.c +++ b/refresh.c @@ -36,13 +36,15 @@ static struct refresh_session current_session = { static void refresh_get(void); static void send_msg(long long chat_id, long long message_id, char *text) { - td_send(TdCreateObjectSendMessage(chat_id, - 0, - message_id, - TdCreateObjectMessageSendOptions(false, - false, - true, - NULL), + td_send(TdCreateObjectSendMessage(chat_id, /* chat_id */ + 0, /* message_thread_id */ + (struct TdMessageReplyTo *) TdCreateObjectMessageReplyToMessage(chat_id, message_id) /* reply_to */, + TdCreateObjectMessageSendOptions(false, /* disable_notification */ + false, /* from_background */ + true, /* protect_content */ + false, /* update_order_of_installed_sticker_sets */ + NULL /* scheduling_state */, + 0 /* sending_id */), NULL, (struct TdInputMessageContent *) TdCreateObjectInputMessageText( @@ -122,7 +124,7 @@ static void refresh_get(void) { return; } char url[64]; - sprintf(url, "https://t.me/ksyxsays/%u", current_session.progress++); + sprintf(url, "https://t.me/c/1794945713/%u", current_session.progress++); LOGDV("Get %s", url); td_send(TdCreateObjectGetInternalLinkType(url), &cb_refresh_link, @@ -250,4 +252,4 @@ void refresh(struct TdMessage *msg) { break; } } -} \ No newline at end of file +} diff --git a/stat.c b/stat.c index 0e5906f..8739600 100644 --- a/stat.c +++ b/stat.c @@ -47,10 +47,17 @@ void stat(struct TdMessage *msg) { sqlite3_reset(stmt_global_stat); goto send; send: - td_send(TdCreateObjectSendMessage(msg->chat_id_, - 0, - msg->id_, - TdCreateObjectMessageSendOptions(false, false, true, NULL), + td_send(TdCreateObjectSendMessage(msg->chat_id_, /* chat_id */ + 0, /* message_thread_id */ + (struct TdMessageReplyTo *) TdCreateObjectMessageReplyToMessage(msg->chat_id_, msg->id_), /* reply_to */ + TdCreateObjectMessageSendOptions( + false, /* disable_notification */ + false, /* from_background */ + true, /*protect_content */ + false, /* update_order_of_installed_sticker_sets */ + NULL, /* scheduling_state */ + 0 /* sending_id */ + ), NULL, (struct TdInputMessageContent *) TdCreateObjectInputMessageText( diff --git a/td b/td index 461b740..203e8cf 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit 461b740987101972cce65d1d5c996f455e4891ee +Subproject commit 203e8cf9c27ee703a8a90b225fc58c2b687bfe6d diff --git a/tdutils.c b/tdutils.c index f9a5502..18c1729 100644 --- a/tdutils.c +++ b/tdutils.c @@ -68,6 +68,7 @@ static void *main_sighandler(void *arg) { case SIGINT: case SIGTERM: if (td == -1) goto cleanup; + LOGI("Stopping ..."); /* Not safe */ tg_close(); goto cleanup; default: @@ -78,7 +79,7 @@ static void *main_sighandler(void *arg) { pthread_exit(NULL); } -static void sighandler_init() { +static void sighandler_init(void) { int r; sigemptyset(&set); sigaddset(&set, SIGTERM); @@ -200,10 +201,10 @@ static void tdcb_free() { cbs = NULL; } -void td_init() { +void td_init(void) { sighandler_init(); td = TdCClientCreateId(); - TdDestroyObjectObject(TdCClientExecute((struct TdFunction *) TdCreateObjectSetLogVerbosityLevel(0))); + TdDestroyObjectObject(TdCClientExecute((struct TdFunction *) TdCreateObjectSetLogVerbosityLevel(2))); td_send(TdCreateObjectGetOption("version"), &fetal_cb, NULL); } @@ -232,6 +233,7 @@ static void auth(bool successful, struct TdObject *result, struct TdError *error tg_close(); return; } + LOGI("Logging in ..."); td_send(TdCreateObjectCheckAuthenticationBotToken((char *) cmd.bot_token), &auth, NULL); @@ -240,23 +242,24 @@ static void auth(bool successful, struct TdObject *result, struct TdError *error static int handle_auth(const struct TdUpdateAuthorizationState *update) { switch (update->authorization_state_->ID) { case CODE_AuthorizationStateWaitTdlibParameters: - td_send(TdCreateObjectSetTdlibParameters(TdCreateObjectTdlibParameters( - cmd.test_dc, - (char *) cmd.td_path, - NULL, - false, - false, - false, - false, - cmd.api_id, - (char *) cmd.api_hash, - "en", - "Desktop", - "0.0", - "ksyxbotd "VER_MAJOR"."VER_MINOR, - false, - true - )), + td_send(TdCreateObjectSetTdlibParameters( + cmd.test_dc, /* use_test_dc */ + (char *) cmd.td_path, /* database_directory */ + NULL, /* files_directory */ + TdCreateObjectBytes((unsigned char *) {0x0}, 0), /* database_encryption_key */ + false, /* use_files_databse */ + false, /* use_chat_info_database */ + false, /* use_message_database */ + false, /* use_secret_chats */ + cmd.api_id, /* api_id */ + (char *) cmd.api_hash, /* api_hash */ + "en", /* system_language_code */ + "Desktop", /* device_model */ + "0.0", /* system_version */ + "ksyxbotd "VER_MAJOR"."VER_MINOR, /* application_version */ + false, /* enable_storage_optimizer */ + true /* ignore_file_names */ + ), &fetal_cb, NULL); return 0; @@ -275,11 +278,6 @@ static int handle_auth(const struct TdUpdateAuthorizationState *update) { } return post_auth(); } - case CODE_AuthorizationStateWaitEncryptionKey: { - td_send(TdCreateObjectCheckDatabaseEncryptionKey(TdCreateObjectBytes((unsigned char *) {0x0}, 0)), - &fetal_cb, NULL); - return 0; - } case CODE_AuthorizationStateLoggingOut: { return 0; } @@ -344,6 +342,7 @@ void td_loop() { ((struct TdUpdateAuthorizationState *) obj)->authorization_state_->ID == CODE_AuthorizationStateClosed) { closing = true; + LOGI("TDLib stopped."); TdDestroyObjectObject(obj); return; } -- cgit v1.2.3