--- lbbs/src/bbs_main.c 2025/04/30 09:18:19 1.32 +++ lbbs/src/bbs_main.c 2025/05/15 05:14:57 1.45 @@ -1,26 +1,25 @@ /*************************************************************************** bbs_main.c - description ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by Leaflet - email : leaflet@leafok.com + 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 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "bbs_main.h" #include "bbs.h" -#include "welcome.h" #include "login.h" #include "user_priv.h" #include "common.h" +#include "database.h" #include "log.h" #include "io.h" #include "screen.h" @@ -35,28 +34,157 @@ int bbs_info() prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", BBS_name, BBS_server, app_version); - iflush(); + return iflush(); +} + +int bbs_welcome(MYSQL *db) +{ + 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_RES *rs; + MYSQL_ROW row; + + 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 failed\n"); + return -2; + } + if ((rs = mysql_store_result(db)) == NULL) + { + log_error("Get user_online data failed\n"); + return -2; + } + if ((row = mysql_fetch_row(rs))) + { + 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 failed\n"); + return -2; + } + if ((rs = mysql_store_result(db)) == NULL) + { + log_error("Get user_online data failed\n"); + return -2; + } + if ((row = mysql_fetch_row(rs))) + { + u_anonymous = (u_int32_t)atoi(row[0]); + } + mysql_free_result(rs); + + snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); + if (mysql_query(db, sql) != 0) + { + log_error("Query user_list failed\n"); + return -2; + } + if ((rs = mysql_store_result(db)) == NULL) + { + log_error("Get user_list data failed\n"); + return -2; + } + if ((row = mysql_fetch_row(rs))) + { + u_total = (u_int32_t)atoi(row[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 failed\n"); + return -2; + } + if ((rs = mysql_store_result(db)) == NULL) + { + log_error("Get user_login_log data failed\n"); + return -2; + } + if ((row = mysql_fetch_row(rs))) + { + u_login_count = (u_int32_t)atoi(row[0]); + } + 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); + } + + // Count current user before login + u_online++; + u_anonymous++; + + // Display logo + display_file(DATA_WELCOME); + + // Display welcome message + prints("\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", + BBS_name, u_online, BBS_max_client, u_anonymous, u_total, + BBS_max_user, BBS_start_dt, max_u_online, u_login_count); return 0; } -int bbs_exit() +int bbs_logout(MYSQL *db) { - char temp[256]; + if (user_online_del(db) < 0) + { + return -1; + } - strcpy(temp, app_home_dir); - strcat(temp, "data/goodbye.txt"); - display_file_ex(temp, 1, 0); + if (display_file_ex(DATA_GOODBYE, 1, 0) < 0) + { + return -2; + } - sleep(1); + log_std("User logout\n"); return 0; } int bbs_center() { - int ch, result, redraw; - char temp[256]; + int ch; time_t t_last_action; BBS_last_access_tm = t_last_action = time(0); @@ -66,11 +194,11 @@ int bbs_center() show_top(""); show_active_board(); show_bottom(""); - display_menu(get_menu(&bbs_menu, "TOPMENU")); + display_menu(p_bbs_menu); - while (!SYS_exit) + while (!SYS_server_exit) { - ch = igetch(); + ch = igetch(100); if (time(0) - t_last_action >= 10) { @@ -81,36 +209,33 @@ int bbs_center() switch (ch) { - case KEY_NULL: + case KEY_NULL: // broken pipe return 0; case KEY_TIMEOUT: if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { - return -1; + return 0; } continue; + case CR: + igetch_reset(); default: - redraw = 1; - switch (menu_control(&bbs_menu, ch)) + switch (menu_control(p_bbs_menu, ch)) { case EXITBBS: return 0; case REDRAW: + clearscr(); + show_top(""); + show_active_board(); + show_bottom(""); + display_menu(p_bbs_menu); break; case NOREDRAW: case UNKNOWN_CMD: default: - redraw = 0; break; } - if (redraw) - { - clearscr(); - show_top(""); - show_active_board(); - show_bottom(""); - display_current_menu(&bbs_menu); - } } BBS_last_access_tm = time(0); } @@ -120,35 +245,51 @@ int bbs_center() int bbs_main() { - char temp[256]; - int ret; + MYSQL *db; set_input_echo(0); - bbs_info(); + // System info + if (bbs_info() < 0) + { + return -1; + } + + db = db_open(); + if (db == NULL) + { + prints("无法连接数据库\n"); + return -2; + } // Welcome - bbs_welcome(); + if (bbs_welcome(db) < 0) + { + mysql_close(db); + return -3; + } - // Login - ret = bbs_login(); - if (ret < 0) - return -1; - log_std("User \"%s\"(%ld) login from %s:%d\n", - BBS_username, BBS_priv.uid, hostaddr_client, port_client); + // User login + if (bbs_login(db) < 0) + { + mysql_close(db); + return -4; + } clearscr(); // BBS Top 10 - strcpy(temp, app_home_dir); - strcat(temp, "data/bbs_top.txt"); - display_file_ex(temp, 1, 1); + if (display_file_ex("./var/bbs_top.txt", 1, 1) < 0) + { + prints("无法加载本站十大\n"); + } // Main bbs_center(); // Logout - bbs_exit(); - log_std("User logout\n"); + bbs_logout(db); + + mysql_close(db); return 0; }