| 25 |
#include "screen.h" |
#include "screen.h" |
| 26 |
#include "menu.h" |
#include "menu.h" |
| 27 |
#include "bbs_cmd.h" |
#include "bbs_cmd.h" |
| 28 |
|
#include "section_list.h" |
| 29 |
|
#include "trie_dict.h" |
| 30 |
#include <unistd.h> |
#include <unistd.h> |
| 31 |
#include <time.h> |
#include <time.h> |
| 32 |
#include <string.h> |
#include <string.h> |
| 35 |
int bbs_info() |
int bbs_info() |
| 36 |
{ |
{ |
| 37 |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
| 38 |
BBS_name, BBS_server, app_version); |
BBS_name, BBS_server, APP_NAME_VER); |
| 39 |
|
|
| 40 |
return iflush(); |
return iflush(); |
| 41 |
} |
} |
| 42 |
|
|
| 43 |
int bbs_welcome(MYSQL *db) |
int bbs_welcome(void) |
| 44 |
{ |
{ |
| 45 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 46 |
|
|
| 50 |
u_int32_t max_u_online = 0; |
u_int32_t max_u_online = 0; |
| 51 |
u_int32_t u_login_count = 0; |
u_int32_t u_login_count = 0; |
| 52 |
|
|
| 53 |
|
MYSQL *db; |
| 54 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 55 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 56 |
|
|
| 57 |
|
db = db_open(); |
| 58 |
|
if (db == NULL) |
| 59 |
|
{ |
| 60 |
|
return -1; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 64 |
"SELECT COUNT(*) AS cc FROM " |
"SELECT COUNT(*) AS cc FROM " |
| 65 |
"(SELECT DISTINCT SID FROM user_online " |
"(SELECT DISTINCT SID FROM user_online " |
| 68 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 69 |
{ |
{ |
| 70 |
log_error("Query user_online error: %s\n", mysql_error(db)); |
log_error("Query user_online error: %s\n", mysql_error(db)); |
| 71 |
|
mysql_close(db); |
| 72 |
return -2; |
return -2; |
| 73 |
} |
} |
| 74 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 75 |
{ |
{ |
| 76 |
log_error("Get user_online data failed\n"); |
log_error("Get user_online data failed\n"); |
| 77 |
|
mysql_close(db); |
| 78 |
return -2; |
return -2; |
| 79 |
} |
} |
| 80 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 91 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 92 |
{ |
{ |
| 93 |
log_error("Query user_online error: %s\n", mysql_error(db)); |
log_error("Query user_online error: %s\n", mysql_error(db)); |
| 94 |
|
mysql_close(db); |
| 95 |
return -2; |
return -2; |
| 96 |
} |
} |
| 97 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 98 |
{ |
{ |
| 99 |
log_error("Get user_online data failed\n"); |
log_error("Get user_online data failed\n"); |
| 100 |
|
mysql_close(db); |
| 101 |
return -2; |
return -2; |
| 102 |
} |
} |
| 103 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 110 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 111 |
{ |
{ |
| 112 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s\n", mysql_error(db)); |
| 113 |
|
mysql_close(db); |
| 114 |
return -2; |
return -2; |
| 115 |
} |
} |
| 116 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 117 |
{ |
{ |
| 118 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 119 |
|
mysql_close(db); |
| 120 |
return -2; |
return -2; |
| 121 |
} |
} |
| 122 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 129 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 130 |
{ |
{ |
| 131 |
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
| 132 |
|
mysql_close(db); |
| 133 |
return -2; |
return -2; |
| 134 |
} |
} |
| 135 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 136 |
{ |
{ |
| 137 |
log_error("Get user_login_log data failed\n"); |
log_error("Get user_login_log data failed\n"); |
| 138 |
|
mysql_close(db); |
| 139 |
return -2; |
return -2; |
| 140 |
} |
} |
| 141 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 144 |
} |
} |
| 145 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 146 |
|
|
| 147 |
|
mysql_close(db); |
| 148 |
|
|
| 149 |
// Log max user_online |
// Log max user_online |
| 150 |
FILE *fin, *fout; |
FILE *fin, *fout; |
| 151 |
if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL) |
if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL) |
| 170 |
u_anonymous++; |
u_anonymous++; |
| 171 |
|
|
| 172 |
// Display logo |
// Display logo |
| 173 |
display_file_ex(DATA_WELCOME, 1, 0); |
display_file(DATA_WELCOME, 1, 2); |
| 174 |
|
|
| 175 |
// Display welcome message |
// Display welcome message |
| 176 |
prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n" |
prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n" |
| 178 |
"匿名游客[\033[36m%d\033[32m] " |
"匿名游客[\033[36m%d\033[32m] " |
| 179 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 180 |
"从 [\033[36m%s\033[32m] 起,最高人数记录:" |
"从 [\033[36m%s\033[32m] 起,最高人数记录:" |
| 181 |
"[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\r\n", |
"[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\033[m\r\n", |
| 182 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 183 |
BBS_max_user, BBS_start_dt, max_u_online, u_login_count); |
BBS_max_user, BBS_start_dt, max_u_online, u_login_count); |
| 184 |
|
|
| 185 |
|
iflush(); |
| 186 |
|
|
| 187 |
return 0; |
return 0; |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
int bbs_logout(MYSQL *db) |
int bbs_logout(void) |
| 191 |
{ |
{ |
| 192 |
if (user_online_del(db) < 0) |
MYSQL *db; |
| 193 |
|
|
| 194 |
|
db = db_open(); |
| 195 |
|
if (db == NULL) |
| 196 |
{ |
{ |
| 197 |
return -1; |
return -1; |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
display_file_ex(DATA_GOODBYE, 1, 1); |
if (user_online_del(db) < 0) |
| 201 |
|
{ |
| 202 |
|
return -2; |
| 203 |
|
} |
| 204 |
|
|
| 205 |
|
mysql_close(db); |
| 206 |
|
|
| 207 |
log_std("User logout\n"); |
display_file(DATA_GOODBYE, 1, 1); |
| 208 |
|
|
| 209 |
|
log_common("User logout\n"); |
| 210 |
|
|
| 211 |
return 0; |
return 0; |
| 212 |
} |
} |
| 220 |
|
|
| 221 |
clearscr(); |
clearscr(); |
| 222 |
|
|
| 223 |
show_top(""); |
show_top("", BBS_name, ""); |
| 224 |
show_active_board(); |
show_active_board(); |
| 225 |
show_bottom(""); |
show_bottom(""); |
| 226 |
display_menu(p_bbs_menu); |
display_menu(p_bbs_menu); |
| 236 |
|
|
| 237 |
show_active_board(); |
show_active_board(); |
| 238 |
show_bottom(""); |
show_bottom(""); |
| 239 |
|
display_menu_cursor(p_bbs_menu, 1); |
| 240 |
iflush(); |
iflush(); |
| 241 |
} |
} |
| 242 |
|
|
| 260 |
case REDRAW: |
case REDRAW: |
| 261 |
t_last_action = time(0); |
t_last_action = time(0); |
| 262 |
clearscr(); |
clearscr(); |
| 263 |
show_top(""); |
show_top("", BBS_name, ""); |
| 264 |
show_active_board(); |
show_active_board(); |
| 265 |
show_bottom(""); |
show_bottom(""); |
| 266 |
display_menu(p_bbs_menu); |
display_menu(p_bbs_menu); |
| 281 |
|
|
| 282 |
int bbs_main() |
int bbs_main() |
| 283 |
{ |
{ |
| 284 |
MYSQL *db; |
// Set data pools in shared memory readonly |
| 285 |
|
if (set_trie_dict_shm_readonly() < 0) |
| 286 |
|
{ |
| 287 |
|
goto cleanup; |
| 288 |
|
} |
| 289 |
|
if (set_article_block_shm_readonly() < 0) |
| 290 |
|
{ |
| 291 |
|
goto cleanup; |
| 292 |
|
} |
| 293 |
|
if (set_section_list_shm_readonly() < 0) |
| 294 |
|
{ |
| 295 |
|
goto cleanup; |
| 296 |
|
} |
| 297 |
|
|
| 298 |
|
// Load menu in shared memory |
| 299 |
|
if (set_menu_shm_readonly(p_bbs_menu) < 0) |
| 300 |
|
{ |
| 301 |
|
goto cleanup; |
| 302 |
|
} |
| 303 |
|
|
| 304 |
set_input_echo(0); |
set_input_echo(0); |
| 305 |
|
|
| 306 |
// System info |
// System info |
| 307 |
if (bbs_info() < 0) |
if (bbs_info() < 0) |
| 308 |
{ |
{ |
| 309 |
return -1; |
goto cleanup; |
|
} |
|
|
|
|
|
db = db_open(); |
|
|
if (db == NULL) |
|
|
{ |
|
|
prints("无法连接数据库\n"); |
|
|
return -2; |
|
| 310 |
} |
} |
| 311 |
|
|
| 312 |
// Welcome |
// Welcome |
| 313 |
if (bbs_welcome(db) < 0) |
if (bbs_welcome() < 0) |
| 314 |
{ |
{ |
| 315 |
mysql_close(db); |
goto cleanup; |
|
return -3; |
|
| 316 |
} |
} |
| 317 |
|
|
| 318 |
// User login |
// User login |
| 319 |
if (bbs_login(db) < 0) |
if (SSH_v2) |
| 320 |
{ |
{ |
| 321 |
mysql_close(db); |
prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); |
| 322 |
return -4; |
iflush(); |
| 323 |
|
igetch_t(MAX_DELAY_TIME); |
| 324 |
|
} |
| 325 |
|
else if (bbs_login() < 0) |
| 326 |
|
{ |
| 327 |
|
goto cleanup; |
| 328 |
} |
} |
| 329 |
|
|
| 330 |
|
// Load article_view_log |
| 331 |
|
|
| 332 |
clearscr(); |
clearscr(); |
| 333 |
|
|
| 334 |
// BBS Top 10 |
// BBS Top 10 |
| 335 |
display_file_ex(VAR_BBS_TOP, 1, 1); |
display_file(VAR_BBS_TOP, 1, 1); |
|
|
|
|
// Load menu in shared memory |
|
|
if (set_menu_shm_readonly(p_bbs_menu) < 0) |
|
|
{ |
|
|
return -5; |
|
|
} |
|
| 336 |
|
|
| 337 |
// Main |
// Main |
| 338 |
bbs_center(); |
bbs_center(); |
| 339 |
|
|
| 340 |
// Unload menu in shared memory |
// Logout |
| 341 |
|
bbs_logout(); |
| 342 |
|
|
| 343 |
|
cleanup: |
| 344 |
|
// Detach menu in shared memory |
| 345 |
detach_menu_shm(p_bbs_menu); |
detach_menu_shm(p_bbs_menu); |
| 346 |
free(p_bbs_menu); |
free(p_bbs_menu); |
| 347 |
p_bbs_menu = NULL; |
p_bbs_menu = NULL; |
| 348 |
|
|
| 349 |
// Logout |
// Detach data pools shm |
| 350 |
bbs_logout(db); |
detach_section_list_shm(); |
| 351 |
|
detach_article_block_shm(); |
| 352 |
// Unload file_loader and trie_dict |
detach_trie_dict_shm(); |
|
// Do nothing explictly - SHM detached automatically on process exit |
|
|
|
|
|
mysql_close(db); |
|
| 353 |
|
|
| 354 |
return 0; |
return 0; |
| 355 |
} |
} |