| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "bbs_main.h" |
#include "article_view_log.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "login.h" |
#include "bbs_cmd.h" |
| 20 |
#include "user_priv.h" |
#include "bbs_main.h" |
| 21 |
#include "common.h" |
#include "common.h" |
| 22 |
#include "database.h" |
#include "database.h" |
| 23 |
#include "log.h" |
#include "editor.h" |
| 24 |
#include "io.h" |
#include "io.h" |
| 25 |
#include "screen.h" |
#include "log.h" |
| 26 |
|
#include "login.h" |
| 27 |
#include "menu.h" |
#include "menu.h" |
| 28 |
#include "bbs_cmd.h" |
#include "screen.h" |
| 29 |
#include "section_list.h" |
#include "section_list.h" |
| 30 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 31 |
#include <unistd.h> |
#include "user_priv.h" |
| 32 |
#include <time.h> |
#include <errno.h> |
| 33 |
#include <string.h> |
#include <signal.h> |
| 34 |
#include <stdlib.h> |
#include <stdlib.h> |
| 35 |
|
#include <string.h> |
| 36 |
|
#include <time.h> |
| 37 |
|
#include <unistd.h> |
| 38 |
|
|
| 39 |
int bbs_info() |
int bbs_info() |
| 40 |
{ |
{ |
| 41 |
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", |
| 42 |
BBS_name, BBS_server, app_version); |
BBS_name, BBS_server, APP_INFO); |
| 43 |
|
|
| 44 |
return iflush(); |
return iflush(); |
| 45 |
} |
} |
| 46 |
|
|
| 47 |
int bbs_welcome(MYSQL *db) |
int bbs_welcome(void) |
| 48 |
{ |
{ |
| 49 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 50 |
|
|
| 51 |
u_int32_t u_online = 0; |
u_int32_t u_online = 0; |
| 52 |
u_int32_t u_anonymous = 0; |
u_int32_t u_anonymous = 0; |
| 53 |
u_int32_t u_total = 0; |
u_int32_t u_total = 0; |
|
u_int32_t max_u_online = 0; |
|
| 54 |
u_int32_t u_login_count = 0; |
u_int32_t u_login_count = 0; |
| 55 |
|
|
| 56 |
|
MYSQL *db; |
| 57 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 58 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 59 |
|
|
| 60 |
|
db = db_open(); |
| 61 |
|
if (db == NULL) |
| 62 |
|
{ |
| 63 |
|
return -1; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 67 |
"SELECT COUNT(*) AS cc FROM " |
"SELECT COUNT(*) AS cc FROM " |
| 68 |
"(SELECT DISTINCT SID FROM user_online " |
"(SELECT DISTINCT SID FROM user_online " |
| 71 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 72 |
{ |
{ |
| 73 |
log_error("Query user_online error: %s\n", mysql_error(db)); |
log_error("Query user_online error: %s\n", mysql_error(db)); |
| 74 |
|
mysql_close(db); |
| 75 |
return -2; |
return -2; |
| 76 |
} |
} |
| 77 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 78 |
{ |
{ |
| 79 |
log_error("Get user_online data failed\n"); |
log_error("Get user_online data failed\n"); |
| 80 |
|
mysql_close(db); |
| 81 |
return -2; |
return -2; |
| 82 |
} |
} |
| 83 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 94 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 95 |
{ |
{ |
| 96 |
log_error("Query user_online error: %s\n", mysql_error(db)); |
log_error("Query user_online error: %s\n", mysql_error(db)); |
| 97 |
|
mysql_close(db); |
| 98 |
return -2; |
return -2; |
| 99 |
} |
} |
| 100 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 101 |
{ |
{ |
| 102 |
log_error("Get user_online data failed\n"); |
log_error("Get user_online data failed\n"); |
| 103 |
|
mysql_close(db); |
| 104 |
return -2; |
return -2; |
| 105 |
} |
} |
| 106 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 113 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 114 |
{ |
{ |
| 115 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("Query user_list error: %s\n", mysql_error(db)); |
| 116 |
|
mysql_close(db); |
| 117 |
return -2; |
return -2; |
| 118 |
} |
} |
| 119 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 120 |
{ |
{ |
| 121 |
log_error("Get user_list data failed\n"); |
log_error("Get user_list data failed\n"); |
| 122 |
|
mysql_close(db); |
| 123 |
return -2; |
return -2; |
| 124 |
} |
} |
| 125 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 132 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 133 |
{ |
{ |
| 134 |
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
| 135 |
|
mysql_close(db); |
| 136 |
return -2; |
return -2; |
| 137 |
} |
} |
| 138 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 139 |
{ |
{ |
| 140 |
log_error("Get user_login_log data failed\n"); |
log_error("Get user_login_log data failed\n"); |
| 141 |
|
mysql_close(db); |
| 142 |
return -2; |
return -2; |
| 143 |
} |
} |
| 144 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 147 |
} |
} |
| 148 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 149 |
|
|
| 150 |
// Log max user_online |
mysql_close(db); |
|
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); |
|
|
} |
|
| 151 |
|
|
| 152 |
// Count current user before login |
// Count current user before login |
| 153 |
u_online++; |
u_online++; |
| 154 |
u_anonymous++; |
u_anonymous++; |
| 155 |
|
|
| 156 |
// Display logo |
// Display logo |
| 157 |
display_file(DATA_WELCOME, 1, 0); |
display_file(DATA_WELCOME, 2); |
| 158 |
|
|
| 159 |
// Display welcome message |
// Display welcome message |
| 160 |
prints("\r\033[1;35mӭ\033[33m %s \033[35mBBS\r\n" |
prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n" |
| 161 |
"\033[32mĿǰվ [\033[36m%d/%d\033[32m] " |
"\033[32m目前上站人数 [\033[36m%d/%d\033[32m] " |
| 162 |
"ο[\033[36m%d\033[32m] " |
"匿名游客[\033[36m%d\033[32m] " |
| 163 |
"עû[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 164 |
" [\033[36m%s\033[32m] ¼" |
"从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n", |
|
"[\033[36m%d\033[32m]ۼƷ˴Σ[\033[36m%d\033[32m]\r\n", |
|
| 165 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 166 |
BBS_max_user, BBS_start_dt, max_u_online, u_login_count); |
BBS_max_user, BBS_start_dt, u_login_count); |
| 167 |
|
|
| 168 |
|
iflush(); |
| 169 |
|
|
| 170 |
return 0; |
return 0; |
| 171 |
} |
} |
| 172 |
|
|
| 173 |
int bbs_logout(MYSQL *db) |
int bbs_logout(void) |
| 174 |
{ |
{ |
| 175 |
if (user_online_del(db) < 0) |
MYSQL *db; |
| 176 |
|
|
| 177 |
|
db = db_open(); |
| 178 |
|
if (db == NULL) |
| 179 |
{ |
{ |
| 180 |
return -1; |
return -1; |
| 181 |
} |
} |
| 182 |
|
|
| 183 |
display_file(DATA_GOODBYE, 1, 1); |
if (user_online_del(db) < 0) |
| 184 |
|
{ |
| 185 |
|
return -2; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
mysql_close(db); |
| 189 |
|
|
| 190 |
log_std("User logout\n"); |
display_file(DATA_GOODBYE, 1); |
| 191 |
|
|
| 192 |
|
log_common("User logout\n"); |
| 193 |
|
|
| 194 |
return 0; |
return 0; |
| 195 |
} |
} |
| 199 |
int ch; |
int ch; |
| 200 |
time_t t_last_action; |
time_t t_last_action; |
| 201 |
|
|
| 202 |
BBS_last_access_tm = t_last_action = time(0); |
BBS_last_access_tm = t_last_action = time(NULL); |
| 203 |
|
|
| 204 |
clearscr(); |
clearscr(); |
| 205 |
|
|
| 206 |
show_top("", BBS_name, ""); |
show_top("", BBS_name, ""); |
| 207 |
show_active_board(); |
show_active_board(); |
| 208 |
show_bottom(""); |
show_bottom(""); |
| 209 |
display_menu(p_bbs_menu); |
display_menu(&bbs_menu); |
| 210 |
iflush(); |
iflush(); |
| 211 |
|
|
| 212 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 213 |
{ |
{ |
| 214 |
ch = igetch(100); |
ch = igetch(100); |
| 215 |
|
|
| 216 |
if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10) |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) |
| 217 |
{ |
{ |
| 218 |
t_last_action = time(0); |
t_last_action = time(NULL); |
| 219 |
|
|
| 220 |
show_active_board(); |
show_active_board(); |
| 221 |
show_bottom(""); |
show_bottom(""); |
| 222 |
display_menu_cursor(p_bbs_menu, 1); |
display_menu_cursor(&bbs_menu, 1); |
| 223 |
iflush(); |
iflush(); |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
|
if (user_online_update("MENU") < 0) |
| 227 |
|
{ |
| 228 |
|
log_error("user_online_update(MENU) error\n"); |
| 229 |
|
} |
| 230 |
|
|
| 231 |
switch (ch) |
switch (ch) |
| 232 |
{ |
{ |
| 233 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 234 |
return 0; |
return 0; |
| 235 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 236 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 237 |
{ |
{ |
| 238 |
return 0; |
return 0; |
| 239 |
} |
} |
| 241 |
case CR: |
case CR: |
| 242 |
igetch_reset(); |
igetch_reset(); |
| 243 |
default: |
default: |
| 244 |
switch (menu_control(p_bbs_menu, ch)) |
switch (menu_control(&bbs_menu, ch)) |
| 245 |
{ |
{ |
| 246 |
case EXITBBS: |
case EXITBBS: |
| 247 |
|
case EXITMENU: |
| 248 |
return 0; |
return 0; |
| 249 |
case REDRAW: |
case REDRAW: |
| 250 |
t_last_action = time(0); |
t_last_action = time(NULL); |
| 251 |
clearscr(); |
clearscr(); |
| 252 |
show_top("", BBS_name, ""); |
show_top("", BBS_name, ""); |
| 253 |
show_active_board(); |
show_active_board(); |
| 254 |
show_bottom(""); |
show_bottom(""); |
| 255 |
display_menu(p_bbs_menu); |
display_menu(&bbs_menu); |
| 256 |
break; |
break; |
| 257 |
case NOREDRAW: |
case NOREDRAW: |
| 258 |
case UNKNOWN_CMD: |
case UNKNOWN_CMD: |
| 262 |
iflush(); |
iflush(); |
| 263 |
} |
} |
| 264 |
|
|
| 265 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(NULL); |
| 266 |
} |
} |
| 267 |
|
|
| 268 |
return 0; |
return 0; |
| 270 |
|
|
| 271 |
int bbs_main() |
int bbs_main() |
| 272 |
{ |
{ |
| 273 |
MYSQL *db = NULL; |
struct sigaction act = {0}; |
| 274 |
|
|
| 275 |
|
// Set signal handler |
| 276 |
|
act.sa_handler = SIG_IGN; |
| 277 |
|
if (sigaction(SIGHUP, &act, NULL) == -1) |
| 278 |
|
{ |
| 279 |
|
log_error("set signal action of SIGHUP error: %d\n", errno); |
| 280 |
|
goto cleanup; |
| 281 |
|
} |
| 282 |
|
act.sa_handler = SIG_DFL; |
| 283 |
|
if (sigaction(SIGCHLD, &act, NULL) == -1) |
| 284 |
|
{ |
| 285 |
|
log_error("set signal action of SIGCHLD error: %d\n", errno); |
| 286 |
|
goto cleanup; |
| 287 |
|
} |
| 288 |
|
|
| 289 |
// Set data pools in shared memory readonly |
// Set data pools in shared memory readonly |
| 290 |
if (set_trie_dict_shm_readonly() < 0) |
if (set_trie_dict_shm_readonly() < 0) |
| 301 |
} |
} |
| 302 |
|
|
| 303 |
// Load menu in shared memory |
// Load menu in shared memory |
| 304 |
if (set_menu_shm_readonly(p_bbs_menu) < 0) |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
| 305 |
{ |
{ |
| 306 |
goto cleanup; |
goto cleanup; |
| 307 |
} |
} |
| 314 |
goto cleanup; |
goto cleanup; |
| 315 |
} |
} |
| 316 |
|
|
| 317 |
db = db_open(); |
// Welcome |
| 318 |
if (db == NULL) |
if (bbs_welcome() < 0) |
| 319 |
{ |
{ |
|
prints("ݿ\n"); |
|
| 320 |
goto cleanup; |
goto cleanup; |
| 321 |
} |
} |
| 322 |
|
|
| 323 |
// Welcome |
// User login |
| 324 |
if (bbs_welcome(db) < 0) |
if (SSH_v2) |
| 325 |
|
{ |
| 326 |
|
prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); |
| 327 |
|
iflush(); |
| 328 |
|
igetch_t(MAX_DELAY_TIME); |
| 329 |
|
} |
| 330 |
|
else if (bbs_login() < 0) |
| 331 |
{ |
{ |
| 332 |
goto cleanup; |
goto cleanup; |
| 333 |
} |
} |
| 334 |
|
|
| 335 |
// User login |
// Load article view log |
| 336 |
if (bbs_login(db) < 0) |
if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0) |
| 337 |
{ |
{ |
| 338 |
|
log_error("article_view_log_load() error\n"); |
| 339 |
goto cleanup; |
goto cleanup; |
| 340 |
} |
} |
| 341 |
|
|
| 342 |
|
// Init editor memory pool |
| 343 |
|
if (editor_memory_pool_init() < 0) |
| 344 |
|
{ |
| 345 |
|
log_error("editor_memory_pool_init() error\n"); |
| 346 |
|
goto cleanup; |
| 347 |
|
} |
| 348 |
|
|
| 349 |
clearscr(); |
clearscr(); |
| 350 |
|
|
| 351 |
// BBS Top 10 |
// BBS Top 10 |
| 352 |
display_file(VAR_BBS_TOP, 1, 1); |
display_file(VAR_BBS_TOP, 1); |
| 353 |
|
|
| 354 |
// Main |
// Main |
| 355 |
bbs_center(); |
bbs_center(); |
| 356 |
|
|
| 357 |
// Logout |
// Logout |
| 358 |
bbs_logout(db); |
bbs_logout(); |
| 359 |
|
|
| 360 |
cleanup: |
// Save incremental article view log |
| 361 |
if (db != NULL) |
if (article_view_log_save_inc(&BBS_article_view_log) < 0) |
| 362 |
{ |
{ |
| 363 |
mysql_close(db); |
log_error("article_view_log_save_inc() error\n"); |
| 364 |
} |
} |
| 365 |
|
|
| 366 |
|
cleanup: |
| 367 |
|
// Cleanup editor memory pool |
| 368 |
|
editor_memory_pool_cleanup(); |
| 369 |
|
|
| 370 |
|
// Unload article view log |
| 371 |
|
article_view_log_unload(&BBS_article_view_log); |
| 372 |
|
|
| 373 |
// Detach menu in shared memory |
// Detach menu in shared memory |
| 374 |
detach_menu_shm(p_bbs_menu); |
detach_menu_shm(&bbs_menu); |
|
free(p_bbs_menu); |
|
|
p_bbs_menu = NULL; |
|
| 375 |
|
|
| 376 |
// Detach data pools shm |
// Detach data pools shm |
| 377 |
detach_section_list_shm(); |
detach_section_list_shm(); |