| 1 |
/***************************************************************************
|
| 2 |
bbs_main.c - description
|
| 3 |
-------------------
|
| 4 |
Copyright : (C) 2004-2025 by Leaflet
|
| 5 |
Email : leaflet@leafok.com
|
| 6 |
***************************************************************************/
|
| 7 |
|
| 8 |
/***************************************************************************
|
| 9 |
* *
|
| 10 |
* This program is free software; you can redistribute it and/or modify *
|
| 11 |
* it under the terms of the GNU General Public License as published by *
|
| 12 |
* the Free Software Foundation; either version 3 of the License, or *
|
| 13 |
* (at your option) any later version. *
|
| 14 |
* *
|
| 15 |
***************************************************************************/
|
| 16 |
|
| 17 |
#include "article_favor.h"
|
| 18 |
#include "article_view_log.h"
|
| 19 |
#include "bbs.h"
|
| 20 |
#include "bbs_cmd.h"
|
| 21 |
#include "bbs_main.h"
|
| 22 |
#include "common.h"
|
| 23 |
#include "database.h"
|
| 24 |
#include "editor.h"
|
| 25 |
#include "io.h"
|
| 26 |
#include "log.h"
|
| 27 |
#include "login.h"
|
| 28 |
#include "menu.h"
|
| 29 |
#include "screen.h"
|
| 30 |
#include "section_list.h"
|
| 31 |
#include "trie_dict.h"
|
| 32 |
#include "user_list.h"
|
| 33 |
#include "user_priv.h"
|
| 34 |
#include <errno.h>
|
| 35 |
#include <signal.h>
|
| 36 |
#include <stdlib.h>
|
| 37 |
#include <string.h>
|
| 38 |
#include <time.h>
|
| 39 |
#include <unistd.h>
|
| 40 |
|
| 41 |
int bbs_info()
|
| 42 |
{
|
| 43 |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\033[m\r\n",
|
| 44 |
BBS_name, BBS_server, APP_INFO);
|
| 45 |
|
| 46 |
return iflush();
|
| 47 |
}
|
| 48 |
|
| 49 |
int bbs_welcome(void)
|
| 50 |
{
|
| 51 |
int u_online = 0;
|
| 52 |
int u_anonymous = 0;
|
| 53 |
int u_total = 0;
|
| 54 |
int u_login_count = 0;
|
| 55 |
|
| 56 |
if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
|
| 57 |
{
|
| 58 |
log_error("get_user_online_list_count() error\n");
|
| 59 |
u_online = 0;
|
| 60 |
}
|
| 61 |
u_online += u_anonymous;
|
| 62 |
u_online++; // current user
|
| 63 |
if (BBS_priv.uid == 0)
|
| 64 |
{
|
| 65 |
u_anonymous++;
|
| 66 |
}
|
| 67 |
|
| 68 |
if (get_user_list_count(&u_total) < 0)
|
| 69 |
{
|
| 70 |
log_error("get_user_list_count() error\n");
|
| 71 |
u_total = 0;
|
| 72 |
}
|
| 73 |
|
| 74 |
if (get_user_login_count(&u_login_count) < 0)
|
| 75 |
{
|
| 76 |
log_error("get_user_login_count() error\n");
|
| 77 |
u_login_count = 0;
|
| 78 |
}
|
| 79 |
|
| 80 |
// Display logo
|
| 81 |
display_file(DATA_WELCOME, 2);
|
| 82 |
|
| 83 |
// Display welcome message
|
| 84 |
prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
|
| 85 |
"\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
|
| 86 |
"匿名游客[\033[36m%d\033[32m] "
|
| 87 |
"注册用户数[\033[36m%d/%d\033[32m]\r\n"
|
| 88 |
"从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
|
| 89 |
BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
|
| 90 |
BBS_max_user_count, BBS_start_dt, u_login_count);
|
| 91 |
|
| 92 |
iflush();
|
| 93 |
|
| 94 |
return 0;
|
| 95 |
}
|
| 96 |
|
| 97 |
int bbs_logout(void)
|
| 98 |
{
|
| 99 |
MYSQL *db;
|
| 100 |
|
| 101 |
db = db_open();
|
| 102 |
if (db == NULL)
|
| 103 |
{
|
| 104 |
return -1;
|
| 105 |
}
|
| 106 |
|
| 107 |
if (user_online_exp(db) < 0)
|
| 108 |
{
|
| 109 |
return -2;
|
| 110 |
}
|
| 111 |
|
| 112 |
if (user_online_del(db) < 0)
|
| 113 |
{
|
| 114 |
return -3;
|
| 115 |
}
|
| 116 |
|
| 117 |
mysql_close(db);
|
| 118 |
|
| 119 |
display_file(DATA_GOODBYE, 1);
|
| 120 |
|
| 121 |
log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
|
| 122 |
|
| 123 |
return 0;
|
| 124 |
}
|
| 125 |
|
| 126 |
int bbs_center()
|
| 127 |
{
|
| 128 |
int ch;
|
| 129 |
time_t t_last_action;
|
| 130 |
|
| 131 |
BBS_last_access_tm = t_last_action = time(NULL);
|
| 132 |
|
| 133 |
clearscr();
|
| 134 |
|
| 135 |
show_top("", BBS_name, "");
|
| 136 |
show_active_board();
|
| 137 |
show_bottom("");
|
| 138 |
display_menu(&bbs_menu);
|
| 139 |
iflush();
|
| 140 |
|
| 141 |
while (!SYS_server_exit)
|
| 142 |
{
|
| 143 |
ch = igetch(100);
|
| 144 |
|
| 145 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT)
|
| 146 |
{
|
| 147 |
BBS_last_access_tm = time(NULL);
|
| 148 |
}
|
| 149 |
|
| 150 |
if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
|
| 151 |
{
|
| 152 |
t_last_action = time(NULL);
|
| 153 |
|
| 154 |
show_active_board();
|
| 155 |
show_bottom("");
|
| 156 |
display_menu_cursor(&bbs_menu, 1);
|
| 157 |
iflush();
|
| 158 |
}
|
| 159 |
|
| 160 |
if (user_online_update("MENU") < 0)
|
| 161 |
{
|
| 162 |
log_error("user_online_update(MENU) error\n");
|
| 163 |
}
|
| 164 |
|
| 165 |
switch (ch)
|
| 166 |
{
|
| 167 |
case KEY_NULL: // broken pipe
|
| 168 |
log_error("KEY_NULL\n");
|
| 169 |
return 0;
|
| 170 |
case KEY_TIMEOUT:
|
| 171 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
|
| 172 |
{
|
| 173 |
log_error("User input timeout\n");
|
| 174 |
return 0;
|
| 175 |
}
|
| 176 |
continue;
|
| 177 |
case CR:
|
| 178 |
default:
|
| 179 |
switch (menu_control(&bbs_menu, ch))
|
| 180 |
{
|
| 181 |
case EXITBBS:
|
| 182 |
case EXITMENU:
|
| 183 |
return 0;
|
| 184 |
case REDRAW:
|
| 185 |
t_last_action = time(NULL);
|
| 186 |
clearscr();
|
| 187 |
show_top("", BBS_name, "");
|
| 188 |
show_active_board();
|
| 189 |
show_bottom("");
|
| 190 |
display_menu(&bbs_menu);
|
| 191 |
break;
|
| 192 |
case NOREDRAW:
|
| 193 |
case UNKNOWN_CMD:
|
| 194 |
default:
|
| 195 |
break;
|
| 196 |
}
|
| 197 |
iflush();
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
return 0;
|
| 202 |
}
|
| 203 |
|
| 204 |
int bbs_charset_select()
|
| 205 |
{
|
| 206 |
char msg[LINE_BUFFER_LEN];
|
| 207 |
int ch;
|
| 208 |
|
| 209 |
snprintf(msg, sizeof(msg),
|
| 210 |
"\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
|
| 211 |
|
| 212 |
while (!SYS_server_exit)
|
| 213 |
{
|
| 214 |
ch = press_any_key_ex(msg, 5);
|
| 215 |
switch (ch)
|
| 216 |
{
|
| 217 |
case KEY_NULL:
|
| 218 |
return -1;
|
| 219 |
case KEY_TIMEOUT:
|
| 220 |
case CR:
|
| 221 |
case 'u':
|
| 222 |
case 'U':
|
| 223 |
return 0;
|
| 224 |
case 'g':
|
| 225 |
case 'G':
|
| 226 |
if (io_conv_init("GBK") < 0)
|
| 227 |
{
|
| 228 |
log_error("io_conv_init(%s) error\n", "GBK");
|
| 229 |
return -1;
|
| 230 |
}
|
| 231 |
return 0;
|
| 232 |
default:
|
| 233 |
continue;
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
| 237 |
return 0;
|
| 238 |
}
|
| 239 |
|
| 240 |
int bbs_main()
|
| 241 |
{
|
| 242 |
struct sigaction act = {0};
|
| 243 |
char msg[LINE_BUFFER_LEN];
|
| 244 |
|
| 245 |
// Set signal handler
|
| 246 |
act.sa_handler = SIG_IGN;
|
| 247 |
if (sigaction(SIGHUP, &act, NULL) == -1)
|
| 248 |
{
|
| 249 |
log_error("set signal action of SIGHUP error: %d\n", errno);
|
| 250 |
goto cleanup;
|
| 251 |
}
|
| 252 |
act.sa_handler = SIG_DFL;
|
| 253 |
if (sigaction(SIGCHLD, &act, NULL) == -1)
|
| 254 |
{
|
| 255 |
log_error("set signal action of SIGCHLD error: %d\n", errno);
|
| 256 |
goto cleanup;
|
| 257 |
}
|
| 258 |
|
| 259 |
// Set data pools in shared memory readonly
|
| 260 |
if (set_trie_dict_shm_readonly() < 0)
|
| 261 |
{
|
| 262 |
goto cleanup;
|
| 263 |
}
|
| 264 |
if (set_article_block_shm_readonly() < 0)
|
| 265 |
{
|
| 266 |
goto cleanup;
|
| 267 |
}
|
| 268 |
if (set_section_list_shm_readonly() < 0)
|
| 269 |
{
|
| 270 |
goto cleanup;
|
| 271 |
}
|
| 272 |
if (set_user_list_pool_shm_readonly() < 0)
|
| 273 |
{
|
| 274 |
goto cleanup;
|
| 275 |
}
|
| 276 |
|
| 277 |
// Load menu in shared memory
|
| 278 |
if (set_menu_shm_readonly(&bbs_menu) < 0)
|
| 279 |
{
|
| 280 |
goto cleanup;
|
| 281 |
}
|
| 282 |
|
| 283 |
// Set default charset
|
| 284 |
if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
|
| 285 |
{
|
| 286 |
log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
|
| 287 |
goto cleanup;
|
| 288 |
}
|
| 289 |
|
| 290 |
set_input_echo(0);
|
| 291 |
|
| 292 |
// Set user charset
|
| 293 |
bbs_charset_select();
|
| 294 |
|
| 295 |
// System info
|
| 296 |
if (bbs_info() < 0)
|
| 297 |
{
|
| 298 |
goto cleanup;
|
| 299 |
}
|
| 300 |
|
| 301 |
// Welcome
|
| 302 |
if (bbs_welcome() < 0)
|
| 303 |
{
|
| 304 |
goto cleanup;
|
| 305 |
}
|
| 306 |
|
| 307 |
// User login
|
| 308 |
if (SSH_v2)
|
| 309 |
{
|
| 310 |
snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
|
| 311 |
press_any_key_ex(msg, 60);
|
| 312 |
}
|
| 313 |
else if (bbs_login() < 0)
|
| 314 |
{
|
| 315 |
goto cleanup;
|
| 316 |
}
|
| 317 |
log_common("User [%s] login\n", BBS_username);
|
| 318 |
|
| 319 |
// Load article view log
|
| 320 |
if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
|
| 321 |
{
|
| 322 |
log_error("article_view_log_load() error\n");
|
| 323 |
goto cleanup;
|
| 324 |
}
|
| 325 |
|
| 326 |
// Load article favorite
|
| 327 |
if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
|
| 328 |
{
|
| 329 |
log_error("article_favor_load() error\n");
|
| 330 |
goto cleanup;
|
| 331 |
}
|
| 332 |
|
| 333 |
// Init editor memory pool
|
| 334 |
if (editor_memory_pool_init() < 0)
|
| 335 |
{
|
| 336 |
log_error("editor_memory_pool_init() error\n");
|
| 337 |
goto cleanup;
|
| 338 |
}
|
| 339 |
|
| 340 |
clearscr();
|
| 341 |
|
| 342 |
// BBS Top 10
|
| 343 |
display_file(VAR_BBS_TOP, 1);
|
| 344 |
|
| 345 |
// Main
|
| 346 |
bbs_center();
|
| 347 |
|
| 348 |
// Logout
|
| 349 |
bbs_logout();
|
| 350 |
|
| 351 |
// Save incremental article view log
|
| 352 |
if (article_view_log_save_inc(&BBS_article_view_log) < 0)
|
| 353 |
{
|
| 354 |
log_error("article_view_log_save_inc() error\n");
|
| 355 |
}
|
| 356 |
|
| 357 |
// Save incremental article favorite
|
| 358 |
if (article_favor_save_inc(&BBS_article_favor) < 0)
|
| 359 |
{
|
| 360 |
log_error("article_favor_save_inc() error\n");
|
| 361 |
}
|
| 362 |
|
| 363 |
cleanup:
|
| 364 |
// Cleanup iconv
|
| 365 |
io_conv_cleanup();
|
| 366 |
|
| 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 |
// Unload article favor
|
| 374 |
article_favor_unload(&BBS_article_favor);
|
| 375 |
|
| 376 |
// Detach menu in shared memory
|
| 377 |
detach_menu_shm(&bbs_menu);
|
| 378 |
detach_menu_shm(&top10_menu);
|
| 379 |
|
| 380 |
// Detach data pools shm
|
| 381 |
detach_user_list_pool_shm();
|
| 382 |
detach_section_list_shm();
|
| 383 |
detach_article_block_shm();
|
| 384 |
detach_trie_dict_shm();
|
| 385 |
|
| 386 |
return 0;
|
| 387 |
}
|