| 28 |
#include "menu.h" |
#include "menu.h" |
| 29 |
#include "screen.h" |
#include "screen.h" |
| 30 |
#include "section_list.h" |
#include "section_list.h" |
| 31 |
|
#include "section_list_display.h" |
| 32 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 33 |
|
#include "user_list.h" |
| 34 |
#include "user_priv.h" |
#include "user_priv.h" |
| 35 |
#include <errno.h> |
#include <errno.h> |
| 36 |
#include <signal.h> |
#include <signal.h> |
| 49 |
|
|
| 50 |
int bbs_welcome(void) |
int bbs_welcome(void) |
| 51 |
{ |
{ |
| 52 |
char sql[SQL_BUFFER_LEN]; |
int u_online = 0; |
| 53 |
|
int u_anonymous = 0; |
| 54 |
|
int u_total = 0; |
| 55 |
|
int u_login_count = 0; |
| 56 |
|
|
| 57 |
u_int32_t u_online = 0; |
if (get_user_online_list_count(&u_online, &u_anonymous) < 0) |
|
u_int32_t u_anonymous = 0; |
|
|
u_int32_t u_total = 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 " |
|
|
"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 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))) |
|
|
{ |
|
|
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 error: %s\n", mysql_error(db)); |
|
|
mysql_close(db); |
|
|
return -2; |
|
|
} |
|
|
if ((rs = mysql_store_result(db)) == NULL) |
|
| 58 |
{ |
{ |
| 59 |
log_error("Get user_online data failed\n"); |
log_error("get_user_online_list_count() error\n"); |
| 60 |
mysql_close(db); |
u_online = 0; |
|
return -2; |
|
| 61 |
} |
} |
| 62 |
if ((row = mysql_fetch_row(rs))) |
u_online += u_anonymous; |
| 63 |
|
u_online++; // current user |
| 64 |
|
if (BBS_priv.uid == 0) |
| 65 |
{ |
{ |
| 66 |
u_anonymous = (u_int32_t)atoi(row[0]); |
u_anonymous++; |
| 67 |
} |
} |
|
mysql_free_result(rs); |
|
| 68 |
|
|
| 69 |
snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); |
if (get_user_list_count(&u_total) < 0) |
|
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) |
|
| 70 |
{ |
{ |
| 71 |
log_error("Get user_list data failed\n"); |
log_error("get_user_list_count() error\n"); |
| 72 |
mysql_close(db); |
u_total = 0; |
|
return -2; |
|
| 73 |
} |
} |
|
if ((row = mysql_fetch_row(rs))) |
|
|
{ |
|
|
u_total = (u_int32_t)atoi(row[0]); |
|
|
} |
|
|
mysql_free_result(rs); |
|
| 74 |
|
|
| 75 |
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
if (get_user_login_count(&u_login_count) < 0) |
|
if (mysql_query(db, sql) != 0) |
|
| 76 |
{ |
{ |
| 77 |
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
log_error("get_user_login_count() error\n"); |
| 78 |
mysql_close(db); |
u_login_count = 0; |
|
return -2; |
|
| 79 |
} |
} |
|
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))) |
|
|
{ |
|
|
u_login_count = (u_int32_t)atoi(row[0]); |
|
|
} |
|
|
mysql_free_result(rs); |
|
|
|
|
|
mysql_close(db); |
|
|
|
|
|
// Count current user before login |
|
|
u_online++; |
|
|
u_anonymous++; |
|
| 80 |
|
|
| 81 |
// Display logo |
// Display logo |
| 82 |
display_file(DATA_WELCOME, 2); |
display_file(DATA_WELCOME, 2); |
| 88 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 89 |
"从 [\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", |
| 90 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 91 |
BBS_max_user, BBS_start_dt, u_login_count); |
BBS_max_user_count, BBS_start_dt, u_login_count); |
| 92 |
|
|
| 93 |
iflush(); |
iflush(); |
| 94 |
|
|
| 119 |
|
|
| 120 |
display_file(DATA_GOODBYE, 1); |
display_file(DATA_GOODBYE, 1); |
| 121 |
|
|
| 122 |
log_common("User [%s] logout\n", BBS_username); |
log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm); |
| 123 |
|
|
| 124 |
return 0; |
return 0; |
| 125 |
} |
} |
| 202 |
return 0; |
return 0; |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
|
int bbs_charset_select() |
| 206 |
|
{ |
| 207 |
|
char msg[LINE_BUFFER_LEN]; |
| 208 |
|
int ch; |
| 209 |
|
|
| 210 |
|
snprintf(msg, sizeof(msg), |
| 211 |
|
"\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]"); |
| 212 |
|
|
| 213 |
|
while (!SYS_server_exit) |
| 214 |
|
{ |
| 215 |
|
ch = press_any_key_ex(msg, 5); |
| 216 |
|
switch (ch) |
| 217 |
|
{ |
| 218 |
|
case KEY_NULL: |
| 219 |
|
return -1; |
| 220 |
|
case KEY_TIMEOUT: |
| 221 |
|
case CR: |
| 222 |
|
case 'u': |
| 223 |
|
case 'U': |
| 224 |
|
return 0; |
| 225 |
|
case 'g': |
| 226 |
|
case 'G': |
| 227 |
|
if (io_conv_init("GBK") < 0) |
| 228 |
|
{ |
| 229 |
|
log_error("io_conv_init(%s) error\n", "GBK"); |
| 230 |
|
return -1; |
| 231 |
|
} |
| 232 |
|
return 0; |
| 233 |
|
default: |
| 234 |
|
continue; |
| 235 |
|
} |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
return 0; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
int bbs_main() |
int bbs_main() |
| 242 |
{ |
{ |
| 243 |
struct sigaction act = {0}; |
struct sigaction act = {0}; |
| 244 |
|
char msg[LINE_BUFFER_LEN]; |
| 245 |
|
|
| 246 |
// Set signal handler |
// Set signal handler |
| 247 |
act.sa_handler = SIG_IGN; |
act.sa_handler = SIG_IGN; |
| 270 |
{ |
{ |
| 271 |
goto cleanup; |
goto cleanup; |
| 272 |
} |
} |
| 273 |
|
if (set_user_list_pool_shm_readonly() < 0) |
| 274 |
|
{ |
| 275 |
|
goto cleanup; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
// Load menu in shared memory |
// Load menu in shared memory |
| 279 |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
| 281 |
goto cleanup; |
goto cleanup; |
| 282 |
} |
} |
| 283 |
|
|
| 284 |
|
// Set default charset |
| 285 |
|
if (io_conv_init(BBS_DEFAULT_CHARSET) < 0) |
| 286 |
|
{ |
| 287 |
|
log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET); |
| 288 |
|
goto cleanup; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
set_input_echo(0); |
set_input_echo(0); |
| 292 |
|
|
| 293 |
|
// Set user charset |
| 294 |
|
bbs_charset_select(); |
| 295 |
|
|
| 296 |
// System info |
// System info |
| 297 |
if (bbs_info() < 0) |
if (bbs_info() < 0) |
| 298 |
{ |
{ |
| 308 |
// User login |
// User login |
| 309 |
if (SSH_v2) |
if (SSH_v2) |
| 310 |
{ |
{ |
| 311 |
prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); |
snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username); |
| 312 |
iflush(); |
press_any_key_ex(msg, 60); |
|
igetch_reset(); |
|
|
while (!SYS_server_exit && igetch_t(MAX_DELAY_TIME) == 0) |
|
|
; |
|
| 313 |
} |
} |
| 314 |
else if (bbs_login() < 0) |
else if (bbs_login() < 0) |
| 315 |
{ |
{ |
| 317 |
} |
} |
| 318 |
log_common("User [%s] login\n", BBS_username); |
log_common("User [%s] login\n", BBS_username); |
| 319 |
|
|
| 320 |
|
// Load section aid locations |
| 321 |
|
if (section_aid_locations_load(BBS_priv.uid) < 0) |
| 322 |
|
{ |
| 323 |
|
log_error("article_view_log_load() error\n"); |
| 324 |
|
goto cleanup; |
| 325 |
|
} |
| 326 |
|
|
| 327 |
// Load article view log |
// Load article view log |
| 328 |
if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0) |
if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0) |
| 329 |
{ |
{ |
| 356 |
// Logout |
// Logout |
| 357 |
bbs_logout(); |
bbs_logout(); |
| 358 |
|
|
| 359 |
|
// Save section aid locations |
| 360 |
|
if (section_aid_locations_save(BBS_priv.uid) < 0) |
| 361 |
|
{ |
| 362 |
|
log_error("article_view_log_save() error\n"); |
| 363 |
|
} |
| 364 |
|
|
| 365 |
// Save incremental article view log |
// Save incremental article view log |
| 366 |
if (article_view_log_save_inc(&BBS_article_view_log) < 0) |
if (article_view_log_save_inc(&BBS_article_view_log) < 0) |
| 367 |
{ |
{ |
| 375 |
} |
} |
| 376 |
|
|
| 377 |
cleanup: |
cleanup: |
| 378 |
|
// Cleanup iconv |
| 379 |
|
io_conv_cleanup(); |
| 380 |
|
|
| 381 |
// Cleanup editor memory pool |
// Cleanup editor memory pool |
| 382 |
editor_memory_pool_cleanup(); |
editor_memory_pool_cleanup(); |
| 383 |
|
|
| 392 |
detach_menu_shm(&top10_menu); |
detach_menu_shm(&top10_menu); |
| 393 |
|
|
| 394 |
// Detach data pools shm |
// Detach data pools shm |
| 395 |
|
detach_user_list_pool_shm(); |
| 396 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 397 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 398 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |