--- lbbs/src/bbs_main.c 2025/06/04 14:01:29 1.63 +++ lbbs/src/bbs_main.c 2025/06/18 02:29:54 1.71 @@ -20,6 +20,7 @@ #include "user_priv.h" #include "common.h" #include "database.h" +#include "article_view_log.h" #include "log.h" #include "io.h" #include "screen.h" @@ -27,6 +28,7 @@ #include "bbs_cmd.h" #include "section_list.h" #include "trie_dict.h" +#include "editor.h" #include #include #include @@ -40,19 +42,25 @@ int bbs_info() return iflush(); } -int bbs_welcome(MYSQL *db) +int bbs_welcome(void) { char sql[SQL_BUFFER_LEN]; u_int32_t u_online = 0; u_int32_t u_anonymous = 0; u_int32_t u_total = 0; - u_int32_t max_u_online = 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 " @@ -61,11 +69,13 @@ int bbs_welcome(MYSQL *db) 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))) @@ -82,11 +92,13 @@ int bbs_welcome(MYSQL *db) 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))) @@ -99,11 +111,13 @@ int bbs_welcome(MYSQL *db) if (mysql_query(db, sql) != 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))) @@ -116,11 +130,13 @@ int bbs_welcome(MYSQL *db) 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))) @@ -129,53 +145,47 @@ int bbs_welcome(MYSQL *db) } mysql_free_result(rs); - // Log max user_online - FILE *fin, *fout; - if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL) - { - fscanf(fin, "%d", &max_u_online); - fclose(fin); - } - if (u_online > max_u_online) - { - max_u_online = u_online; - if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL) - { - log_error("Open max_user_online.dat failed\n"); - return -3; - } - fprintf(fout, "%d\n", max_u_online); - fclose(fout); - } + mysql_close(db); // Count current user before login u_online++; u_anonymous++; // Display logo - display_file(DATA_WELCOME, 1, 2); + display_file(DATA_WELCOME, 2); // Display welcome message prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n" "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] " "匿名游客[\033[36m%d\033[32m] " "注册用户数[\033[36m%d/%d\033[32m]\r\n" - "从 [\033[36m%s\033[32m] 起,最高人数记录:" - "[\033[36m%d\033[32m],累计访问人次:[\033[36m%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, max_u_online, u_login_count); + BBS_max_user, BBS_start_dt, u_login_count); + + iflush(); return 0; } -int bbs_logout(MYSQL *db) +int bbs_logout(void) { - if (user_online_del(db) < 0) + MYSQL *db; + + db = db_open(); + if (db == NULL) { return -1; } - display_file(DATA_GOODBYE, 1, 1); + if (user_online_del(db) < 0) + { + return -2; + } + + mysql_close(db); + + display_file(DATA_GOODBYE, 1); log_common("User logout\n"); @@ -187,7 +197,7 @@ int bbs_center() int ch; time_t t_last_action; - BBS_last_access_tm = t_last_action = time(0); + BBS_last_access_tm = t_last_action = time(NULL); clearscr(); @@ -201,9 +211,9 @@ int bbs_center() { ch = igetch(100); - if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10) + if (p_bbs_menu->choose_step == 0 && time(NULL) - t_last_action >= 10) { - t_last_action = time(0); + t_last_action = time(NULL); show_active_board(); show_bottom(""); @@ -211,12 +221,17 @@ int bbs_center() iflush(); } + if (user_online_update("MENU") < 0) + { + log_error("user_online_update(MENU) error\n"); + } + switch (ch) { case KEY_NULL: // broken pipe return 0; case KEY_TIMEOUT: - if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) + if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) { return 0; } @@ -229,7 +244,7 @@ int bbs_center() case EXITBBS: return 0; case REDRAW: - t_last_action = time(0); + t_last_action = time(NULL); clearscr(); show_top("", BBS_name, ""); show_active_board(); @@ -244,7 +259,7 @@ int bbs_center() iflush(); } - BBS_last_access_tm = time(0); + BBS_last_access_tm = time(NULL); } return 0; @@ -252,8 +267,6 @@ int bbs_center() int bbs_main() { - MYSQL *db = NULL; - // Set data pools in shared memory readonly if (set_trie_dict_shm_readonly() < 0) { @@ -282,41 +295,62 @@ int bbs_main() goto cleanup; } - db = db_open(); - if (db == NULL) + // Welcome + if (bbs_welcome() < 0) { - prints("无法连接数据库\n"); goto cleanup; } - // Welcome - if (bbs_welcome(db) < 0) + // User login + if (SSH_v2) + { + prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); + iflush(); + igetch_t(MAX_DELAY_TIME); + } + else if (bbs_login() < 0) { goto cleanup; } - // User login - if (bbs_login(db) < 0) + // Load article view log + if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0) + { + log_error("article_view_log_load() error\n"); + goto cleanup; + } + + // Init editor memory pool + if (editor_memory_pool_init() < 0) { + log_error("editor_memory_pool_init() error\n"); goto cleanup; } + clearscr(); // BBS Top 10 - display_file(VAR_BBS_TOP, 1, 1); + display_file(VAR_BBS_TOP, 1); // Main bbs_center(); // Logout - bbs_logout(db); + bbs_logout(); -cleanup: - if (db != NULL) + // Save incremental article view log + if (article_view_log_save_inc(&BBS_article_view_log) < 0) { - mysql_close(db); + log_error("article_view_log_save_inc() error\n"); } +cleanup: + // Cleanup editor memory pool + editor_memory_pool_cleanup(); + + // Unload article view log + article_view_log_unload(&BBS_article_view_log); + // Detach menu in shared memory detach_menu_shm(p_bbs_menu); free(p_bbs_menu);