| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
bbs_main.c - description |
/* |
| 3 |
------------------- |
* bbs_main |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - entry and major procedures of user interactive access |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "article_favor.h" |
#include "article_favor.h" |
| 10 |
#include "article_view_log.h" |
#include "article_view_log.h" |
| 20 |
#include "menu.h" |
#include "menu.h" |
| 21 |
#include "screen.h" |
#include "screen.h" |
| 22 |
#include "section_list.h" |
#include "section_list.h" |
| 23 |
|
#include "section_list_display.h" |
| 24 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 25 |
|
#include "user_list.h" |
| 26 |
#include "user_priv.h" |
#include "user_priv.h" |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <signal.h> |
#include <signal.h> |
| 41 |
|
|
| 42 |
int bbs_welcome(void) |
int bbs_welcome(void) |
| 43 |
{ |
{ |
| 44 |
char sql[SQL_BUFFER_LEN]; |
int u_online = 0; |
| 45 |
|
int u_anonymous = 0; |
| 46 |
|
int u_total = 0; |
| 47 |
|
int u_login_count = 0; |
| 48 |
|
|
| 49 |
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) |
|
| 50 |
{ |
{ |
| 51 |
log_error("Get user_online data failed\n"); |
log_error("get_user_online_list_count() error\n"); |
| 52 |
mysql_close(db); |
u_online = 0; |
|
return -2; |
|
| 53 |
} |
} |
| 54 |
if ((row = mysql_fetch_row(rs))) |
u_online += u_anonymous; |
| 55 |
|
u_online++; // current user |
| 56 |
|
if (BBS_priv.uid == 0) |
| 57 |
{ |
{ |
| 58 |
u_online = (u_int32_t)atoi(row[0]); |
u_anonymous++; |
| 59 |
} |
} |
|
mysql_free_result(rs); |
|
| 60 |
|
|
| 61 |
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) |
|
| 62 |
{ |
{ |
| 63 |
log_error("Get user_online data failed\n"); |
log_error("get_user_list_count() error\n"); |
| 64 |
mysql_close(db); |
u_total = 0; |
|
return -2; |
|
| 65 |
} |
} |
|
if ((row = mysql_fetch_row(rs))) |
|
|
{ |
|
|
u_anonymous = (u_int32_t)atoi(row[0]); |
|
|
} |
|
|
mysql_free_result(rs); |
|
| 66 |
|
|
| 67 |
snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); |
if (get_user_login_count(&u_login_count) < 0) |
|
if (mysql_query(db, sql) != 0) |
|
| 68 |
{ |
{ |
| 69 |
log_error("Query user_list error: %s\n", mysql_error(db)); |
log_error("get_user_login_count() error\n"); |
| 70 |
mysql_close(db); |
u_login_count = 0; |
|
return -2; |
|
| 71 |
} |
} |
|
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))) |
|
|
{ |
|
|
u_total = (u_int32_t)atoi(row[0]); |
|
|
} |
|
|
mysql_free_result(rs); |
|
|
|
|
|
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
|
|
if (mysql_query(db, sql) != 0) |
|
|
{ |
|
|
log_error("Query user_login_log error: %s\n", mysql_error(db)); |
|
|
mysql_close(db); |
|
|
return -2; |
|
|
} |
|
|
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++; |
|
| 72 |
|
|
| 73 |
// Display logo |
// Display logo |
| 74 |
display_file(DATA_WELCOME, 2); |
display_file(DATA_WELCOME, 2); |
| 80 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 81 |
"从 [\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", |
| 82 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 83 |
BBS_max_user, BBS_start_dt, u_login_count); |
BBS_max_user_count, BBS_start_dt, u_login_count); |
| 84 |
|
|
| 85 |
iflush(); |
iflush(); |
| 86 |
|
|
| 111 |
|
|
| 112 |
display_file(DATA_GOODBYE, 1); |
display_file(DATA_GOODBYE, 1); |
| 113 |
|
|
| 114 |
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); |
| 115 |
|
|
| 116 |
return 0; |
return 0; |
| 117 |
} |
} |
| 135 |
{ |
{ |
| 136 |
ch = igetch(100); |
ch = igetch(100); |
| 137 |
|
|
| 138 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 139 |
{ |
{ |
| 140 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 141 |
} |
} |
| 142 |
|
|
| 143 |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10) |
| 144 |
{ |
{ |
| 161 |
log_error("KEY_NULL\n"); |
log_error("KEY_NULL\n"); |
| 162 |
return 0; |
return 0; |
| 163 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 164 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 165 |
{ |
{ |
| 166 |
log_error("User input timeout\n"); |
log_error("User input timeout\n"); |
| 167 |
return 0; |
return 0; |
| 168 |
} |
} |
| 169 |
continue; |
continue; |
| 170 |
case CR: |
case CR: |
|
igetch_reset(); |
|
| 171 |
default: |
default: |
| 172 |
switch (menu_control(&bbs_menu, ch)) |
switch (menu_control(&bbs_menu, ch)) |
| 173 |
{ |
{ |
| 194 |
return 0; |
return 0; |
| 195 |
} |
} |
| 196 |
|
|
| 197 |
|
int bbs_charset_select() |
| 198 |
|
{ |
| 199 |
|
char msg[LINE_BUFFER_LEN]; |
| 200 |
|
int ch; |
| 201 |
|
|
| 202 |
|
snprintf(msg, sizeof(msg), |
| 203 |
|
"\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]"); |
| 204 |
|
|
| 205 |
|
while (!SYS_server_exit) |
| 206 |
|
{ |
| 207 |
|
ch = press_any_key_ex(msg, 5); |
| 208 |
|
switch (ch) |
| 209 |
|
{ |
| 210 |
|
case KEY_NULL: |
| 211 |
|
return -1; |
| 212 |
|
case KEY_TIMEOUT: |
| 213 |
|
case CR: |
| 214 |
|
case 'u': |
| 215 |
|
case 'U': |
| 216 |
|
return 0; |
| 217 |
|
case 'g': |
| 218 |
|
case 'G': |
| 219 |
|
if (io_conv_init("GBK") < 0) |
| 220 |
|
{ |
| 221 |
|
log_error("io_conv_init(%s) error\n", "GBK"); |
| 222 |
|
return -1; |
| 223 |
|
} |
| 224 |
|
return 0; |
| 225 |
|
default: |
| 226 |
|
continue; |
| 227 |
|
} |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
return 0; |
| 231 |
|
} |
| 232 |
|
|
| 233 |
int bbs_main() |
int bbs_main() |
| 234 |
{ |
{ |
| 235 |
struct sigaction act = {0}; |
struct sigaction act = {0}; |
| 236 |
|
char msg[LINE_BUFFER_LEN]; |
| 237 |
|
|
| 238 |
// Set signal handler |
// Set signal handler |
| 239 |
act.sa_handler = SIG_IGN; |
act.sa_handler = SIG_IGN; |
| 262 |
{ |
{ |
| 263 |
goto cleanup; |
goto cleanup; |
| 264 |
} |
} |
| 265 |
|
if (set_user_list_pool_shm_readonly() < 0) |
| 266 |
|
{ |
| 267 |
|
goto cleanup; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
// Load menu in shared memory |
// Load menu in shared memory |
| 271 |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
if (set_menu_shm_readonly(&bbs_menu) < 0) |
| 273 |
goto cleanup; |
goto cleanup; |
| 274 |
} |
} |
| 275 |
|
|
| 276 |
|
// Set default charset |
| 277 |
|
if (io_conv_init(BBS_DEFAULT_CHARSET) < 0) |
| 278 |
|
{ |
| 279 |
|
log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET); |
| 280 |
|
goto cleanup; |
| 281 |
|
} |
| 282 |
|
|
| 283 |
set_input_echo(0); |
set_input_echo(0); |
| 284 |
|
|
| 285 |
|
// Set user charset |
| 286 |
|
bbs_charset_select(); |
| 287 |
|
|
| 288 |
// System info |
// System info |
| 289 |
if (bbs_info() < 0) |
if (bbs_info() < 0) |
| 290 |
{ |
{ |
| 300 |
// User login |
// User login |
| 301 |
if (SSH_v2) |
if (SSH_v2) |
| 302 |
{ |
{ |
| 303 |
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); |
| 304 |
iflush(); |
press_any_key_ex(msg, 60); |
|
igetch_t(MAX_DELAY_TIME); |
|
| 305 |
} |
} |
| 306 |
else if (bbs_login() < 0) |
else if (bbs_login() < 0) |
| 307 |
{ |
{ |
| 309 |
} |
} |
| 310 |
log_common("User [%s] login\n", BBS_username); |
log_common("User [%s] login\n", BBS_username); |
| 311 |
|
|
| 312 |
|
// Load section aid locations |
| 313 |
|
if (section_aid_locations_load(BBS_priv.uid) < 0) |
| 314 |
|
{ |
| 315 |
|
log_error("article_view_log_load() error\n"); |
| 316 |
|
goto cleanup; |
| 317 |
|
} |
| 318 |
|
|
| 319 |
// Load article view log |
// Load article view log |
| 320 |
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) |
| 321 |
{ |
{ |
| 348 |
// Logout |
// Logout |
| 349 |
bbs_logout(); |
bbs_logout(); |
| 350 |
|
|
| 351 |
|
// Save section aid locations |
| 352 |
|
if (section_aid_locations_save(BBS_priv.uid) < 0) |
| 353 |
|
{ |
| 354 |
|
log_error("article_view_log_save() error\n"); |
| 355 |
|
} |
| 356 |
|
|
| 357 |
// Save incremental article view log |
// Save incremental article view log |
| 358 |
if (article_view_log_save_inc(&BBS_article_view_log) < 0) |
if (article_view_log_save_inc(&BBS_article_view_log) < 0) |
| 359 |
{ |
{ |
| 367 |
} |
} |
| 368 |
|
|
| 369 |
cleanup: |
cleanup: |
| 370 |
|
// Cleanup iconv |
| 371 |
|
io_conv_cleanup(); |
| 372 |
|
|
| 373 |
// Cleanup editor memory pool |
// Cleanup editor memory pool |
| 374 |
editor_memory_pool_cleanup(); |
editor_memory_pool_cleanup(); |
| 375 |
|
|
| 384 |
detach_menu_shm(&top10_menu); |
detach_menu_shm(&top10_menu); |
| 385 |
|
|
| 386 |
// Detach data pools shm |
// Detach data pools shm |
| 387 |
|
detach_user_list_pool_shm(); |
| 388 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 389 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 390 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |