| 29 |
#include "screen.h" |
#include "screen.h" |
| 30 |
#include "section_list.h" |
#include "section_list.h" |
| 31 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 32 |
|
#include "user_list.h" |
| 33 |
#include "user_priv.h" |
#include "user_priv.h" |
| 34 |
#include <errno.h> |
#include <errno.h> |
| 35 |
#include <signal.h> |
#include <signal.h> |
| 50 |
{ |
{ |
| 51 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 52 |
|
|
| 53 |
u_int32_t u_online = 0; |
int u_online = 0; |
| 54 |
u_int32_t u_anonymous = 0; |
int u_anonymous = 0; |
| 55 |
u_int32_t u_total = 0; |
int u_total = 0; |
| 56 |
u_int32_t u_login_count = 0; |
int u_login_count = 0; |
| 57 |
|
|
| 58 |
MYSQL *db; |
MYSQL *db; |
| 59 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 60 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 61 |
|
|
| 62 |
db = db_open(); |
if (get_user_online_list_count(&u_online, &u_anonymous) < 0) |
|
if (db == NULL) |
|
| 63 |
{ |
{ |
| 64 |
return -1; |
log_error("get_user_online_list_count() error\n"); |
| 65 |
|
u_online = 0; |
| 66 |
} |
} |
| 67 |
|
u_online += u_anonymous; |
| 68 |
snprintf(sql, sizeof(sql), |
u_online++; // current user |
| 69 |
"SELECT COUNT(*) AS cc FROM " |
if (BBS_priv.uid == 0) |
|
"(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) |
|
| 70 |
{ |
{ |
| 71 |
log_error("Query user_online error: %s\n", mysql_error(db)); |
u_anonymous++; |
|
mysql_close(db); |
|
|
return -2; |
|
| 72 |
} |
} |
|
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); |
|
| 73 |
|
|
| 74 |
snprintf(sql, sizeof(sql), |
if (get_user_list_count(&u_total) < 0) |
|
"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) |
|
|
{ |
|
|
log_error("Get user_online data failed\n"); |
|
|
mysql_close(db); |
|
|
return -2; |
|
|
} |
|
|
if ((row = mysql_fetch_row(rs))) |
|
| 75 |
{ |
{ |
| 76 |
u_anonymous = (u_int32_t)atoi(row[0]); |
log_error("get_user_list_count() error\n"); |
| 77 |
|
u_total = 0; |
| 78 |
} |
} |
|
mysql_free_result(rs); |
|
| 79 |
|
|
| 80 |
snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); |
db = db_open(); |
| 81 |
if (mysql_query(db, sql) != 0) |
if (db == NULL) |
|
{ |
|
|
log_error("Query user_list error: %s\n", mysql_error(db)); |
|
|
mysql_close(db); |
|
|
return -2; |
|
|
} |
|
|
if ((rs = mysql_store_result(db)) == NULL) |
|
|
{ |
|
|
log_error("Get user_list data failed\n"); |
|
|
mysql_close(db); |
|
|
return -2; |
|
|
} |
|
|
if ((row = mysql_fetch_row(rs))) |
|
| 82 |
{ |
{ |
| 83 |
u_total = (u_int32_t)atoi(row[0]); |
return -1; |
| 84 |
} |
} |
|
mysql_free_result(rs); |
|
| 85 |
|
|
| 86 |
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
| 87 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 98 |
} |
} |
| 99 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 100 |
{ |
{ |
| 101 |
u_login_count = (u_int32_t)atoi(row[0]); |
u_login_count = atoi(row[0]); |
| 102 |
} |
} |
| 103 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 104 |
|
|
| 105 |
mysql_close(db); |
mysql_close(db); |
| 106 |
|
|
|
// Count current user before login |
|
|
u_online++; |
|
|
u_anonymous++; |
|
|
|
|
| 107 |
// Display logo |
// Display logo |
| 108 |
display_file(DATA_WELCOME, 2); |
display_file(DATA_WELCOME, 2); |
| 109 |
|
|
| 114 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 115 |
"从 [\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", |
| 116 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 117 |
BBS_max_user, BBS_start_dt, u_login_count); |
BBS_max_user_count, BBS_start_dt, u_login_count); |
| 118 |
|
|
| 119 |
iflush(); |
iflush(); |
| 120 |
|
|
| 145 |
|
|
| 146 |
display_file(DATA_GOODBYE, 1); |
display_file(DATA_GOODBYE, 1); |
| 147 |
|
|
| 148 |
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); |
| 149 |
|
|
| 150 |
return 0; |
return 0; |
| 151 |
} |
} |
| 228 |
return 0; |
return 0; |
| 229 |
} |
} |
| 230 |
|
|
| 231 |
|
int bbs_charset_select() |
| 232 |
|
{ |
| 233 |
|
char msg[LINE_BUFFER_LEN]; |
| 234 |
|
int ch; |
| 235 |
|
|
| 236 |
|
snprintf(msg, sizeof(msg), |
| 237 |
|
"\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]"); |
| 238 |
|
|
| 239 |
|
while (!SYS_server_exit) |
| 240 |
|
{ |
| 241 |
|
ch = press_any_key_ex(msg, 5); |
| 242 |
|
switch (ch) |
| 243 |
|
{ |
| 244 |
|
case KEY_NULL: |
| 245 |
|
return -1; |
| 246 |
|
case KEY_TIMEOUT: |
| 247 |
|
case CR: |
| 248 |
|
case 'u': |
| 249 |
|
case 'U': |
| 250 |
|
return 0; |
| 251 |
|
case 'g': |
| 252 |
|
case 'G': |
| 253 |
|
if (io_conv_init("GBK") < 0) |
| 254 |
|
{ |
| 255 |
|
log_error("io_conv_init(%s) error\n", "GBK"); |
| 256 |
|
return -1; |
| 257 |
|
} |
| 258 |
|
return 0; |
| 259 |
|
default: |
| 260 |
|
continue; |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
return 0; |
| 265 |
|
} |
| 266 |
|
|
| 267 |
int bbs_main() |
int bbs_main() |
| 268 |
{ |
{ |
| 269 |
struct sigaction act = {0}; |
struct sigaction act = {0}; |
| 270 |
|
char msg[LINE_BUFFER_LEN]; |
| 271 |
|
|
| 272 |
// Set signal handler |
// Set signal handler |
| 273 |
act.sa_handler = SIG_IGN; |
act.sa_handler = SIG_IGN; |
| 296 |
{ |
{ |
| 297 |
goto cleanup; |
goto cleanup; |
| 298 |
} |
} |
| 299 |
|
if (set_user_list_pool_shm_readonly() < 0) |
| 300 |
|
{ |
| 301 |
|
goto cleanup; |
| 302 |
|
} |
| 303 |
|
|
| 304 |
// Load menu in shared memory |
// Load menu in shared memory |
| 305 |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
| 307 |
goto cleanup; |
goto cleanup; |
| 308 |
} |
} |
| 309 |
|
|
| 310 |
|
// Set default charset |
| 311 |
|
if (io_conv_init(BBS_DEFAULT_CHARSET) < 0) |
| 312 |
|
{ |
| 313 |
|
log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET); |
| 314 |
|
goto cleanup; |
| 315 |
|
} |
| 316 |
|
|
| 317 |
set_input_echo(0); |
set_input_echo(0); |
| 318 |
|
|
| 319 |
|
// Set user charset |
| 320 |
|
bbs_charset_select(); |
| 321 |
|
|
| 322 |
// System info |
// System info |
| 323 |
if (bbs_info() < 0) |
if (bbs_info() < 0) |
| 324 |
{ |
{ |
| 334 |
// User login |
// User login |
| 335 |
if (SSH_v2) |
if (SSH_v2) |
| 336 |
{ |
{ |
| 337 |
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); |
| 338 |
iflush(); |
press_any_key_ex(msg, 60); |
|
igetch_reset(); |
|
|
while (!SYS_server_exit && igetch_t(MAX_DELAY_TIME) == 0) |
|
|
; |
|
| 339 |
} |
} |
| 340 |
else if (bbs_login() < 0) |
else if (bbs_login() < 0) |
| 341 |
{ |
{ |
| 388 |
} |
} |
| 389 |
|
|
| 390 |
cleanup: |
cleanup: |
| 391 |
|
// Cleanup iconv |
| 392 |
|
io_conv_cleanup(); |
| 393 |
|
|
| 394 |
// Cleanup editor memory pool |
// Cleanup editor memory pool |
| 395 |
editor_memory_pool_cleanup(); |
editor_memory_pool_cleanup(); |
| 396 |
|
|
| 405 |
detach_menu_shm(&top10_menu); |
detach_menu_shm(&top10_menu); |
| 406 |
|
|
| 407 |
// Detach data pools shm |
// Detach data pools shm |
| 408 |
|
detach_user_list_pool_shm(); |
| 409 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 410 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 411 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |