summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuuta Liang <yuuta@yuuta.moe>2023-08-19 13:51:41 +0800
committerYuuta Liang <yuuta@yuuta.moe>2023-08-19 13:51:41 +0800
commite5311d0810c6bcb77f872ade031357172b060d68 (patch)
tree5d27f5c9a16a21d41d3624fa4d7c55e60e778e3f
parentce4bc9ac9ca3002b5ad524f0df07b199f7f5a36a (diff)
downloadksyxbot-e5311d0810c6bcb77f872ade031357172b060d68.tar
ksyxbot-e5311d0810c6bcb77f872ade031357172b060d68.tar.gz
ksyxbot-e5311d0810c6bcb77f872ade031357172b060d68.tar.bz2
ksyxbot-e5311d0810c6bcb77f872ade031357172b060d68.zip
Update TDLib
Fix abort when disconnect Version: sync with bot API
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt3
-rw-r--r--botd.h2
-rw-r--r--main.c5
-rw-r--r--query.c15
-rw-r--r--refresh.c20
-rw-r--r--stat.c15
m---------td0
-rw-r--r--tdutils.c49
9 files changed, 62 insertions, 48 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 <stdbool.h>
#include <time.h>
-#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
-Subproject 461b740987101972cce65d1d5c996f455e4891e
+Subproject 203e8cf9c27ee703a8a90b225fc58c2b687bfe6
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;
}