| 3 |
* bbs_main |
* bbs_main |
| 4 |
* - entry and major procedures of user interactive access |
* - entry and major procedures of user interactive access |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "article_favor.h" |
#include "article_favor.h" |
| 14 |
#include "article_view_log.h" |
#include "article_view_log.h" |
| 15 |
#include "bbs.h" |
#include "bbs.h" |
| 16 |
#include "bbs_cmd.h" |
#include "bbs_cmd.h" |
| 17 |
#include "bbs_main.h" |
#include "bbs_main.h" |
| 18 |
|
#include "bwf.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "database.h" |
#include "database.h" |
| 21 |
#include "editor.h" |
#include "editor.h" |
| 26 |
#include "screen.h" |
#include "screen.h" |
| 27 |
#include "section_list.h" |
#include "section_list.h" |
| 28 |
#include "section_list_display.h" |
#include "section_list_display.h" |
| 29 |
|
#include "str_process.h" |
| 30 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 31 |
#include "user_list.h" |
#include "user_list.h" |
| 32 |
#include "user_priv.h" |
#include "user_priv.h" |
| 33 |
|
#include <ctype.h> |
| 34 |
#include <errno.h> |
#include <errno.h> |
| 35 |
#include <signal.h> |
#include <signal.h> |
| 36 |
#include <stdlib.h> |
#include <stdlib.h> |
| 38 |
#include <time.h> |
#include <time.h> |
| 39 |
#include <unistd.h> |
#include <unistd.h> |
| 40 |
|
|
| 41 |
|
static void child_proc_sig_usr1_handler(int i) |
| 42 |
|
{ |
| 43 |
|
// Restart log |
| 44 |
|
if (log_restart() < 0) |
| 45 |
|
{ |
| 46 |
|
log_error("Restart logging failed\n"); |
| 47 |
|
} |
| 48 |
|
} |
| 49 |
|
|
| 50 |
int bbs_info() |
int bbs_info() |
| 51 |
{ |
{ |
| 52 |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\033[m\r\n", |
prints("\r\n欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m(%s)\033[m\r\n", |
| 53 |
BBS_name, BBS_server, APP_INFO); |
BBS_name, BBS_server, APP_INFO); |
| 54 |
|
|
| 55 |
return iflush(); |
return iflush(); |
| 94 |
"\033[32m目前上站人数 [\033[36m%d/%d\033[32m] " |
"\033[32m目前上站人数 [\033[36m%d/%d\033[32m] " |
| 95 |
"匿名游客[\033[36m%d\033[32m] " |
"匿名游客[\033[36m%d\033[32m] " |
| 96 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 97 |
"从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n", |
"从 [\033[36m%s\033[32m] 起,累计访问人次: [\033[36m%d\033[32m]\033[m\r\n", |
| 98 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 99 |
BBS_max_user_count, BBS_start_dt, u_login_count); |
BBS_max_user_count, BBS_start_dt, u_login_count); |
| 100 |
|
|
| 118 |
return -2; |
return -2; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
|
mysql_close(db); |
| 122 |
|
|
| 123 |
|
display_file(DATA_GOODBYE, 1); |
| 124 |
|
|
| 125 |
|
log_common("User [%s] (uid=%d) logout, idle for %ld seconds since last input\n", |
| 126 |
|
BBS_username, BBS_priv.uid, time(NULL) - BBS_last_access_tm); |
| 127 |
|
|
| 128 |
|
return 0; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
int bbs_session_cleanup(void) |
| 132 |
|
{ |
| 133 |
|
MYSQL *db; |
| 134 |
|
|
| 135 |
|
db = db_open(); |
| 136 |
|
if (db == NULL) |
| 137 |
|
{ |
| 138 |
|
return -1; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
if (user_online_del(db) < 0) |
if (user_online_del(db) < 0) |
| 142 |
{ |
{ |
| 143 |
return -3; |
return -2; |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
mysql_close(db); |
mysql_close(db); |
| 147 |
|
|
|
display_file(DATA_GOODBYE, 1); |
|
|
|
|
|
log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm); |
|
|
|
|
| 148 |
return 0; |
return 0; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
int bbs_center() |
int bbs_center() |
| 152 |
{ |
{ |
| 153 |
int ch; |
int ch; |
| 154 |
|
int loop; |
| 155 |
time_t t_last_action; |
time_t t_last_action; |
| 156 |
|
|
| 157 |
BBS_last_access_tm = t_last_action = time(NULL); |
BBS_last_access_tm = t_last_action = time(NULL); |
| 164 |
display_menu(&bbs_menu); |
display_menu(&bbs_menu); |
| 165 |
iflush(); |
iflush(); |
| 166 |
|
|
| 167 |
while (!SYS_server_exit) |
for (loop = 1; !SYS_server_exit && loop;) |
| 168 |
{ |
{ |
| 169 |
ch = igetch(100); |
ch = igetch(100); |
| 170 |
|
|
| 171 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 172 |
{ |
{ |
| 173 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 174 |
|
log_debug("Debug: BBS_last_access_tm is updated\n"); |
| 175 |
} |
} |
| 176 |
|
|
| 177 |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) |
| 192 |
switch (ch) |
switch (ch) |
| 193 |
{ |
{ |
| 194 |
case KEY_NULL: // broken pipe |
case KEY_NULL: // broken pipe |
| 195 |
log_error("KEY_NULL\n"); |
log_debug("KEY_NULL\n"); |
| 196 |
return 0; |
loop = 0; |
| 197 |
|
break; |
| 198 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 199 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 200 |
{ |
{ |
| 201 |
log_error("User input timeout\n"); |
log_debug("User input timeout\n"); |
| 202 |
return 0; |
loop = 0; |
| 203 |
|
break; |
| 204 |
} |
} |
| 205 |
continue; |
continue; |
| 206 |
case CR: |
case CR: |
| 209 |
{ |
{ |
| 210 |
case EXITBBS: |
case EXITBBS: |
| 211 |
case EXITMENU: |
case EXITMENU: |
| 212 |
return 0; |
loop = 0; |
| 213 |
|
break; |
| 214 |
case REDRAW: |
case REDRAW: |
| 215 |
t_last_action = time(NULL); |
t_last_action = time(NULL); |
| 216 |
clearscr(); |
clearscr(); |
| 237 |
int ch; |
int ch; |
| 238 |
|
|
| 239 |
snprintf(msg, sizeof(msg), |
snprintf(msg, sizeof(msg), |
| 240 |
"\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]"); |
"\rChoose character set in 5 seconds, (U)UTF-8, (G)GBK [U]: "); |
| 241 |
|
|
| 242 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 243 |
{ |
{ |
| 244 |
ch = press_any_key_ex(msg, 5); |
ch = press_any_key_ex(msg, 5); |
| 245 |
switch (ch) |
switch (toupper(ch)) |
| 246 |
{ |
{ |
| 247 |
case KEY_NULL: |
case KEY_NULL: |
| 248 |
return -1; |
return -1; |
| 249 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 250 |
case CR: |
case CR: |
|
case 'u': |
|
| 251 |
case 'U': |
case 'U': |
| 252 |
return 0; |
break; |
|
case 'g': |
|
| 253 |
case 'G': |
case 'G': |
| 254 |
if (io_conv_init("GBK") < 0) |
if (io_conv_init("GBK") < 0) |
| 255 |
{ |
{ |
| 256 |
log_error("io_conv_init(%s) error\n", "GBK"); |
log_error("io_conv_init(%s) error\n", "GBK"); |
| 257 |
return -1; |
return -1; |
| 258 |
} |
} |
| 259 |
return 0; |
break; |
| 260 |
|
default: |
| 261 |
|
continue; |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
break; |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
snprintf(msg, sizeof(msg), |
| 268 |
|
"\r请在5秒内选择宽字符显示规则, (V)变宽, (F)定宽? [V]: "); |
| 269 |
|
|
| 270 |
|
while (!SYS_server_exit) |
| 271 |
|
{ |
| 272 |
|
ch = press_any_key_ex(msg, 5); |
| 273 |
|
switch (toupper(ch)) |
| 274 |
|
{ |
| 275 |
|
case KEY_NULL: |
| 276 |
|
return -1; |
| 277 |
|
case KEY_TIMEOUT: |
| 278 |
|
case CR: |
| 279 |
|
case 'V': |
| 280 |
|
UTF8_fixed_width = 0; |
| 281 |
|
break; |
| 282 |
|
case 'F': |
| 283 |
|
UTF8_fixed_width = 1; |
| 284 |
|
break; |
| 285 |
default: |
default: |
| 286 |
continue; |
continue; |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
|
break; |
| 290 |
} |
} |
| 291 |
|
|
| 292 |
return 0; |
return 0; |
| 310 |
log_error("set signal action of SIGCHLD error: %d\n", errno); |
log_error("set signal action of SIGCHLD error: %d\n", errno); |
| 311 |
goto cleanup; |
goto cleanup; |
| 312 |
} |
} |
| 313 |
|
act.sa_handler = child_proc_sig_usr1_handler; |
| 314 |
|
if (sigaction(SIGUSR1, &act, NULL) == -1) |
| 315 |
|
{ |
| 316 |
|
log_error("set signal action of SIGUSR1 error: %d\n", errno); |
| 317 |
|
goto cleanup; |
| 318 |
|
} |
| 319 |
|
|
| 320 |
// Set data pools in shared memory readonly |
// Set data pools in shared memory readonly |
| 321 |
if (set_trie_dict_shm_readonly() < 0) |
if (set_trie_dict_shm_readonly() < 0) |
| 326 |
{ |
{ |
| 327 |
goto cleanup; |
goto cleanup; |
| 328 |
} |
} |
| 329 |
|
#ifdef HAVE_SYSTEM_V |
| 330 |
if (set_section_list_shm_readonly() < 0) |
if (set_section_list_shm_readonly() < 0) |
| 331 |
{ |
{ |
| 332 |
goto cleanup; |
goto cleanup; |
| 335 |
{ |
{ |
| 336 |
goto cleanup; |
goto cleanup; |
| 337 |
} |
} |
| 338 |
|
#endif |
| 339 |
|
|
| 340 |
// Load menu in shared memory |
// Load menu in shared memory |
| 341 |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
| 344 |
} |
} |
| 345 |
|
|
| 346 |
// Set default charset |
// Set default charset |
| 347 |
if (io_conv_init(BBS_DEFAULT_CHARSET) < 0) |
if (io_conv_init(BBS_default_charset) < 0) |
| 348 |
{ |
{ |
| 349 |
log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET); |
log_error("io_conv_init(%s) error\n", BBS_default_charset); |
| 350 |
goto cleanup; |
goto cleanup; |
| 351 |
} |
} |
| 352 |
|
|
| 353 |
set_input_echo(0); |
set_input_echo(0); |
| 354 |
|
|
| 355 |
// Set user charset |
// Set user charset |
| 356 |
bbs_charset_select(); |
if (bbs_charset_select() < 0) |
| 357 |
|
{ |
| 358 |
|
goto cleanup; |
| 359 |
|
} |
| 360 |
|
|
| 361 |
// System info |
// System info |
| 362 |
if (bbs_info() < 0) |
if (bbs_info() < 0) |
| 380 |
{ |
{ |
| 381 |
goto cleanup; |
goto cleanup; |
| 382 |
} |
} |
| 383 |
log_common("User [%s] login\n", BBS_username); |
log_common("User [%s] (uid=%d) login from %s:%d\n", |
| 384 |
|
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
| 385 |
|
|
| 386 |
|
// Check EULA update status |
| 387 |
|
if (BBS_update_eula) |
| 388 |
|
{ |
| 389 |
|
user_update_agreement(); |
| 390 |
|
goto cleanup; |
| 391 |
|
} |
| 392 |
|
|
| 393 |
// Load section aid locations |
// Load section aid locations |
| 394 |
if (section_aid_locations_load(BBS_priv.uid) < 0) |
if (section_aid_locations_load(BBS_priv.uid) < 0) |
| 448 |
} |
} |
| 449 |
|
|
| 450 |
cleanup: |
cleanup: |
| 451 |
|
// Cleanup session |
| 452 |
|
bbs_session_cleanup(); |
| 453 |
|
|
| 454 |
// Cleanup iconv |
// Cleanup iconv |
| 455 |
io_conv_cleanup(); |
io_conv_cleanup(); |
| 456 |
|
|
| 473 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 474 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |
| 475 |
|
|
| 476 |
|
// Cleanup BWF |
| 477 |
|
bwf_cleanup(); |
| 478 |
|
|
| 479 |
return 0; |
return 0; |
| 480 |
} |
} |