--- lbbs/src/bbs_main.c 2025/10/17 07:07:55 1.85 +++ lbbs/src/bbs_main.c 2025/11/08 08:21:31 1.103 @@ -1,24 +1,17 @@ -/*************************************************************************** - bbs_main.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * bbs_main + * - entry and major procedures of user interactive access + * + * Copyright (C) 2004-2025 Leaflet + */ #include "article_favor.h" #include "article_view_log.h" #include "bbs.h" #include "bbs_cmd.h" #include "bbs_main.h" +#include "bwf.h" #include "common.h" #include "database.h" #include "editor.h" @@ -28,7 +21,10 @@ #include "menu.h" #include "screen.h" #include "section_list.h" +#include "section_list_display.h" +#include "str_process.h" #include "trie_dict.h" +#include "user_list.h" #include "user_priv.h" #include #include @@ -47,112 +43,34 @@ int bbs_info() int bbs_welcome(void) { - char sql[SQL_BUFFER_LEN]; + int u_online = 0; + int u_anonymous = 0; + int u_total = 0; + int u_login_count = 0; - u_int32_t u_online = 0; - u_int32_t u_anonymous = 0; - u_int32_t u_total = 0; - u_int32_t u_login_count = 0; - - MYSQL *db; - MYSQL_RES *rs; - MYSQL_ROW row; - - db = db_open(); - if (db == NULL) - { - return -1; - } - - snprintf(sql, sizeof(sql), - "SELECT COUNT(*) AS cc FROM " - "(SELECT DISTINCT SID FROM user_online " - "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1", - BBS_user_off_line); - if (mysql_query(db, sql) != 0) - { - log_error("Query user_online error: %s\n", mysql_error(db)); - mysql_close(db); - return -2; - } - if ((rs = mysql_store_result(db)) == NULL) - { - log_error("Get user_online data failed\n"); - mysql_close(db); - return -2; - } - if ((row = mysql_fetch_row(rs))) + if (get_user_online_list_count(&u_online, &u_anonymous) < 0) { - u_online = (u_int32_t)atoi(row[0]); - } - mysql_free_result(rs); - - snprintf(sql, sizeof(sql), - "SELECT COUNT(*) AS cc FROM " - "(SELECT DISTINCT SID FROM user_online " - "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1", - BBS_user_off_line); - if (mysql_query(db, sql) != 0) - { - log_error("Query user_online error: %s\n", mysql_error(db)); - mysql_close(db); - return -2; - } - if ((rs = mysql_store_result(db)) == NULL) - { - log_error("Get user_online data failed\n"); - mysql_close(db); - return -2; + log_error("get_user_online_list_count() error\n"); + u_online = 0; } - if ((row = mysql_fetch_row(rs))) + u_online += u_anonymous; + u_online++; // current user + if (BBS_priv.uid == 0) { - u_anonymous = (u_int32_t)atoi(row[0]); + u_anonymous++; } - mysql_free_result(rs); - snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); - if (mysql_query(db, sql) != 0) + if (get_user_list_count(&u_total) < 0) { - log_error("Query user_list error: %s\n", mysql_error(db)); - mysql_close(db); - return -2; - } - if ((rs = mysql_store_result(db)) == NULL) - { - log_error("Get user_list data failed\n"); - mysql_close(db); - return -2; - } - if ((row = mysql_fetch_row(rs))) - { - u_total = (u_int32_t)atoi(row[0]); + log_error("get_user_list_count() error\n"); + u_total = 0; } - mysql_free_result(rs); - snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); - if (mysql_query(db, sql) != 0) - { - log_error("Query user_login_log error: %s\n", mysql_error(db)); - mysql_close(db); - return -2; - } - if ((rs = mysql_store_result(db)) == NULL) - { - log_error("Get user_login_log data failed\n"); - mysql_close(db); - return -2; - } - if ((row = mysql_fetch_row(rs))) + if (get_user_login_count(&u_login_count) < 0) { - u_login_count = (u_int32_t)atoi(row[0]); + log_error("get_user_login_count() error\n"); + u_login_count = 0; } - mysql_free_result(rs); - - mysql_close(db); - - // Count current user before login - u_online++; - u_anonymous++; // Display logo display_file(DATA_WELCOME, 2); @@ -164,7 +82,7 @@ int bbs_welcome(void) "注册用户数[\033[36m%d/%d\033[32m]\r\n" "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n", BBS_name, u_online, BBS_max_client, u_anonymous, u_total, - BBS_max_user, BBS_start_dt, u_login_count); + BBS_max_user_count, BBS_start_dt, u_login_count); iflush(); @@ -195,7 +113,7 @@ int bbs_logout(void) display_file(DATA_GOODBYE, 1); - log_common("User [%s] logout\n", BBS_username); + log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm); return 0; } @@ -222,6 +140,9 @@ int bbs_center() if (ch != KEY_NULL && ch != KEY_TIMEOUT) { BBS_last_access_tm = time(NULL); +#ifdef _DEBUG + log_error("Debug: BBS_last_access_tm is updated\n"); +#endif } if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) @@ -245,7 +166,7 @@ int bbs_center() log_error("KEY_NULL\n"); return 0; case KEY_TIMEOUT: - if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) { log_error("User input timeout\n"); return 0; @@ -278,9 +199,71 @@ int bbs_center() return 0; } +int bbs_charset_select() +{ + char msg[LINE_BUFFER_LEN]; + int ch; + + snprintf(msg, sizeof(msg), + "\rChoose character set in 3 seconds [UTF-8, GBK]: [U/g]"); + + while (!SYS_server_exit) + { + ch = press_any_key_ex(msg, 3); + switch (toupper(ch)) + { + case KEY_NULL: + return -1; + case KEY_TIMEOUT: + case CR: + case 'U': + break; + case 'G': + if (io_conv_init("GBK") < 0) + { + log_error("io_conv_init(%s) error\n", "GBK"); + return -1; + } + break; + default: + continue; + } + + break; + } + + snprintf(msg, sizeof(msg), + "\rChoose wide-character display in 3 seconds [Fixed, Dynamic]: [F/d]"); + + while (!SYS_server_exit) + { + ch = press_any_key_ex(msg, 3); + switch (toupper(ch)) + { + case KEY_NULL: + return -1; + case KEY_TIMEOUT: + case CR: + case 'F': + UTF8_fixed_width = 1; + break; + case 'D': + UTF8_fixed_width = 0; + break; + default: + continue; + } + + break; + } + + return 0; +} + int bbs_main() { struct sigaction act = {0}; + char msg[LINE_BUFFER_LEN]; // Set signal handler act.sa_handler = SIG_IGN; @@ -309,6 +292,10 @@ int bbs_main() { goto cleanup; } + if (set_user_list_pool_shm_readonly() < 0) + { + goto cleanup; + } // Load menu in shared memory if (set_menu_shm_readonly(&bbs_menu) < 0) @@ -316,8 +303,18 @@ int bbs_main() goto cleanup; } + // Set default charset + if (io_conv_init(BBS_default_charset) < 0) + { + log_error("io_conv_init(%s) error\n", BBS_default_charset); + goto cleanup; + } + set_input_echo(0); + // Set user charset + bbs_charset_select(); + // System info if (bbs_info() < 0) { @@ -333,11 +330,8 @@ int bbs_main() // User login if (SSH_v2) { - prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); - iflush(); - igetch_reset(); - while (!SYS_server_exit && igetch_t(MAX_DELAY_TIME) == 0) - ; + snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); + press_any_key_ex(msg, 60); } else if (bbs_login() < 0) { @@ -345,6 +339,13 @@ int bbs_main() } log_common("User [%s] login\n", BBS_username); + // Load section aid locations + if (section_aid_locations_load(BBS_priv.uid) < 0) + { + log_error("article_view_log_load() error\n"); + goto cleanup; + } + // Load article view log if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0) { @@ -377,6 +378,12 @@ int bbs_main() // Logout bbs_logout(); + // Save section aid locations + if (section_aid_locations_save(BBS_priv.uid) < 0) + { + log_error("article_view_log_save() error\n"); + } + // Save incremental article view log if (article_view_log_save_inc(&BBS_article_view_log) < 0) { @@ -390,6 +397,9 @@ int bbs_main() } cleanup: + // Cleanup iconv + io_conv_cleanup(); + // Cleanup editor memory pool editor_memory_pool_cleanup(); @@ -404,9 +414,13 @@ cleanup: detach_menu_shm(&top10_menu); // Detach data pools shm + detach_user_list_pool_shm(); detach_section_list_shm(); detach_article_block_shm(); detach_trie_dict_shm(); + // Cleanup BWF + bwf_unload(); + return 0; }