| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbs_main.c - description |
bbs_main.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#include "bbs_main.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
|
#include "login.h" |
| 20 |
|
#include "user_priv.h" |
| 21 |
#include "common.h" |
#include "common.h" |
| 22 |
|
#include "database.h" |
| 23 |
|
#include "log.h" |
| 24 |
#include "io.h" |
#include "io.h" |
| 25 |
|
#include "screen.h" |
| 26 |
#include "menu.h" |
#include "menu.h" |
| 27 |
#include <time.h> |
#include "bbs_cmd.h" |
|
#include <fcntl.h> |
|
| 28 |
#include <unistd.h> |
#include <unistd.h> |
| 29 |
|
#include <time.h> |
| 30 |
|
#include <string.h> |
| 31 |
|
|
| 32 |
#define ACTIVE_BOARD_HEIGHT 8 |
int bbs_info() |
|
|
|
|
int |
|
|
bbs_main () |
|
| 33 |
{ |
{ |
| 34 |
char temp[256]; |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
| 35 |
int ret; |
BBS_name, BBS_server, app_version); |
| 36 |
|
|
| 37 |
set_input_echo (0); |
return iflush(); |
| 38 |
|
} |
| 39 |
|
|
| 40 |
bbs_info (); |
int bbs_welcome(MYSQL *db) |
| 41 |
|
{ |
| 42 |
|
char sql[SQL_BUFFER_LEN]; |
| 43 |
|
|
| 44 |
//Welcome |
u_int32_t u_online = 0; |
| 45 |
bbs_welcome (); |
u_int32_t u_anonymous = 0; |
| 46 |
|
u_int32_t u_total = 0; |
| 47 |
|
u_int32_t max_u_online = 0; |
| 48 |
|
u_int32_t u_login_count = 0; |
| 49 |
|
|
| 50 |
|
MYSQL_RES *rs; |
| 51 |
|
MYSQL_ROW row; |
| 52 |
|
|
| 53 |
|
snprintf(sql, sizeof(sql), |
| 54 |
|
"SELECT COUNT(*) AS cc FROM " |
| 55 |
|
"(SELECT DISTINCT SID FROM user_online " |
| 56 |
|
"WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1", |
| 57 |
|
BBS_user_off_line); |
| 58 |
|
if (mysql_query(db, sql) != 0) |
| 59 |
|
{ |
| 60 |
|
log_error("Query user_online failed\n"); |
| 61 |
|
return -2; |
| 62 |
|
} |
| 63 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 64 |
|
{ |
| 65 |
|
log_error("Get user_online data failed\n"); |
| 66 |
|
return -2; |
| 67 |
|
} |
| 68 |
|
if ((row = mysql_fetch_row(rs))) |
| 69 |
|
{ |
| 70 |
|
u_online = (u_int32_t)atoi(row[0]); |
| 71 |
|
} |
| 72 |
|
mysql_free_result(rs); |
| 73 |
|
|
| 74 |
//Login |
snprintf(sql, sizeof(sql), |
| 75 |
ret = bbs_login (); |
"SELECT COUNT(*) AS cc FROM " |
| 76 |
if (ret < 0) |
"(SELECT DISTINCT SID FROM user_online " |
| 77 |
return -1; |
"WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1", |
| 78 |
clearscr (); |
BBS_user_off_line); |
| 79 |
|
if (mysql_query(db, sql) != 0) |
| 80 |
|
{ |
| 81 |
|
log_error("Query user_online failed\n"); |
| 82 |
|
return -2; |
| 83 |
|
} |
| 84 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 85 |
|
{ |
| 86 |
|
log_error("Get user_online data failed\n"); |
| 87 |
|
return -2; |
| 88 |
|
} |
| 89 |
|
if ((row = mysql_fetch_row(rs))) |
| 90 |
|
{ |
| 91 |
|
u_anonymous = (u_int32_t)atoi(row[0]); |
| 92 |
|
} |
| 93 |
|
mysql_free_result(rs); |
| 94 |
|
|
| 95 |
//BBS Top 10 |
snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); |
| 96 |
strcpy (temp, app_home_dir); |
if (mysql_query(db, sql) != 0) |
| 97 |
strcat (temp, "data/bbs_top.txt"); |
{ |
| 98 |
display_file_ex (temp, 1, 1); |
log_error("Query user_list failed\n"); |
| 99 |
|
return -2; |
| 100 |
|
} |
| 101 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 102 |
|
{ |
| 103 |
|
log_error("Get user_list data failed\n"); |
| 104 |
|
return -2; |
| 105 |
|
} |
| 106 |
|
if ((row = mysql_fetch_row(rs))) |
| 107 |
|
{ |
| 108 |
|
u_total = (u_int32_t)atoi(row[0]); |
| 109 |
|
} |
| 110 |
|
mysql_free_result(rs); |
| 111 |
|
|
| 112 |
//Main |
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
| 113 |
bbs_center (); |
if (mysql_query(db, sql) != 0) |
| 114 |
|
{ |
| 115 |
|
log_error("Query user_login_log failed\n"); |
| 116 |
|
return -2; |
| 117 |
|
} |
| 118 |
|
if ((rs = mysql_store_result(db)) == NULL) |
| 119 |
|
{ |
| 120 |
|
log_error("Get user_login_log data failed\n"); |
| 121 |
|
return -2; |
| 122 |
|
} |
| 123 |
|
if ((row = mysql_fetch_row(rs))) |
| 124 |
|
{ |
| 125 |
|
u_login_count = (u_int32_t)atoi(row[0]); |
| 126 |
|
} |
| 127 |
|
mysql_free_result(rs); |
| 128 |
|
|
| 129 |
//Logout |
// Log max user_online |
| 130 |
bbs_exit (); |
FILE *fin, *fout; |
| 131 |
|
if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL) |
| 132 |
|
{ |
| 133 |
|
fscanf(fin, "%d", &max_u_online); |
| 134 |
|
fclose(fin); |
| 135 |
|
} |
| 136 |
|
if (u_online > max_u_online) |
| 137 |
|
{ |
| 138 |
|
max_u_online = u_online; |
| 139 |
|
if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL) |
| 140 |
|
{ |
| 141 |
|
log_error("Open max_user_online.dat failed\n"); |
| 142 |
|
return -3; |
| 143 |
|
} |
| 144 |
|
fprintf(fout, "%d\n", max_u_online); |
| 145 |
|
fclose(fout); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
return 0; |
// Count current user before login |
| 149 |
|
u_online++; |
| 150 |
|
u_anonymous++; |
| 151 |
|
|
| 152 |
|
// Display logo |
| 153 |
|
display_file_ex(DATA_WELCOME, 1, 0); |
| 154 |
|
|
| 155 |
|
// Display welcome message |
| 156 |
|
prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n" |
| 157 |
|
"\033[32m目前上站人数 [\033[36m%d/%d\033[32m] " |
| 158 |
|
"匿名游客[\033[36m%d\033[32m] " |
| 159 |
|
"注册用户数[\033[36m%d/%d\033[32m]\r\n" |
| 160 |
|
"从 [\033[36m%s\033[32m] 起,最高人数记录:" |
| 161 |
|
"[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\r\n", |
| 162 |
|
BBS_name, u_online, BBS_max_client, u_anonymous, u_total, |
| 163 |
|
BBS_max_user, BBS_start_dt, max_u_online, u_login_count); |
| 164 |
|
|
| 165 |
|
return 0; |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
int |
int bbs_logout(MYSQL *db) |
|
bbs_info () |
|
| 169 |
{ |
{ |
| 170 |
prints ("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
if (user_online_del(db) < 0) |
| 171 |
BBS_name, BBS_server, app_version); |
{ |
| 172 |
|
return -1; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
iflush (); |
display_file_ex(DATA_GOODBYE, 1, 0); |
| 176 |
|
|
| 177 |
return 0; |
log_std("User logout\n"); |
| 178 |
|
|
| 179 |
|
return 0; |
| 180 |
} |
} |
| 181 |
|
|
| 182 |
int |
int bbs_center() |
|
bbs_exit () |
|
| 183 |
{ |
{ |
| 184 |
char temp[256]; |
int ch; |
| 185 |
|
time_t t_last_action; |
| 186 |
|
|
| 187 |
strcpy (temp, app_home_dir); |
BBS_last_access_tm = t_last_action = time(0); |
|
strcat (temp, "data/goodbye.txt"); |
|
|
display_file_ex (temp, 1, 0); |
|
| 188 |
|
|
| 189 |
sleep (1); |
clearscr(); |
| 190 |
|
|
| 191 |
return 0; |
show_top(""); |
| 192 |
} |
show_active_board(); |
| 193 |
|
show_bottom(""); |
| 194 |
|
display_menu(p_bbs_menu); |
| 195 |
|
|
| 196 |
int |
while (!SYS_server_exit) |
| 197 |
bbs_center () |
{ |
| 198 |
{ |
ch = igetch(100); |
|
int ch, result; |
|
|
char key; |
|
|
time_t t_last_action; |
|
|
fd_set inputs, testfds; |
|
|
struct timeval timeout; |
|
|
|
|
|
FD_ZERO (&inputs); |
|
|
FD_SET (0, &inputs); |
|
|
|
|
|
t_last_action = time (0); |
|
|
|
|
|
clearscr (); |
|
|
|
|
|
show_top ("测试"); |
|
|
show_active_board (); |
|
|
display_menu (&bbs_main_menu); |
|
|
show_bottom ("测试"); |
|
|
|
|
|
while (1) |
|
|
{ |
|
|
testfds = inputs; |
|
|
timeout.tv_sec = 0; |
|
|
timeout.tv_usec = 100000; |
|
|
|
|
|
result = select (FD_SETSIZE, &testfds, (fd_set *) NULL, |
|
|
(fd_set *) NULL, &timeout); |
|
|
|
|
|
switch (result) |
|
|
{ |
|
|
case 0: |
|
|
break; |
|
|
case -1: |
|
|
log_error ("select() error!\n"); |
|
|
break; |
|
|
default: |
|
|
if (FD_ISSET (0, &testfds)) |
|
|
{ |
|
|
ch = igetch (); |
|
| 199 |
|
|
| 200 |
switch (ch) |
if (time(0) - t_last_action >= 10) |
| 201 |
{ |
{ |
| 202 |
case KEY_LEFT: |
t_last_action = time(0); |
| 203 |
ch = 'G'; |
show_active_board(); |
| 204 |
|
show_bottom(""); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
switch (ch) |
| 208 |
|
{ |
| 209 |
|
case KEY_NULL: // broken pipe |
| 210 |
|
return 0; |
| 211 |
|
case KEY_TIMEOUT: |
| 212 |
|
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 213 |
|
{ |
| 214 |
|
return 0; |
| 215 |
|
} |
| 216 |
|
continue; |
| 217 |
|
case CR: |
| 218 |
|
igetch_reset(); |
| 219 |
default: |
default: |
| 220 |
key = menu_control (&bbs_main_menu, ch); |
switch (menu_control(p_bbs_menu, ch)) |
| 221 |
switch (key) |
{ |
| 222 |
{ |
case EXITBBS: |
| 223 |
case 'G': |
return 0; |
| 224 |
return 0; |
case REDRAW: |
| 225 |
} |
clearscr(); |
| 226 |
|
show_top(""); |
| 227 |
|
show_active_board(); |
| 228 |
|
show_bottom(""); |
| 229 |
|
display_menu(p_bbs_menu); |
| 230 |
|
break; |
| 231 |
|
case NOREDRAW: |
| 232 |
|
case UNKNOWN_CMD: |
| 233 |
|
default: |
| 234 |
|
break; |
| 235 |
|
} |
| 236 |
} |
} |
| 237 |
} |
BBS_last_access_tm = time(0); |
|
break; |
|
|
} |
|
|
if (time (0) - t_last_action >= 10) |
|
|
{ |
|
|
t_last_action = time (0); |
|
|
show_active_board (); |
|
| 238 |
} |
} |
|
} |
|
| 239 |
|
|
| 240 |
return 0; |
return 0; |
| 241 |
} |
} |
| 242 |
|
|
| 243 |
int |
int bbs_main() |
|
show_top (char *status) |
|
| 244 |
{ |
{ |
| 245 |
char buffer[256]; |
MYSQL *db; |
| 246 |
|
|
| 247 |
str_space (buffer, 20 - strlen (BBS_current_section_name)); |
set_input_echo(0); |
| 248 |
|
|
| 249 |
moveto (1, 0); |
// System info |
| 250 |
prints ("\033[1;44;33m%-20s \033[37m%20s" |
if (bbs_info() < 0) |
| 251 |
" %s\033[33m讨论区 [%s]\033[m", |
{ |
| 252 |
status, BBS_name, buffer, BBS_current_section_name); |
return -1; |
| 253 |
iflush (); |
} |
| 254 |
|
|
| 255 |
return 0; |
db = db_open(); |
| 256 |
} |
if (db == NULL) |
| 257 |
|
{ |
| 258 |
|
prints("无法连接数据库\n"); |
| 259 |
|
return -2; |
| 260 |
|
} |
| 261 |
|
|
| 262 |
int |
// Welcome |
| 263 |
show_bottom (char *msg) |
if (bbs_welcome(db) < 0) |
| 264 |
{ |
{ |
| 265 |
char str_time[256], str_time_onine[20], buffer[256]; |
mysql_close(db); |
| 266 |
time_t time_online; |
return -3; |
| 267 |
struct tm *tm_online; |
} |
|
|
|
|
get_time_str (str_time, 256); |
|
|
str_space (buffer, 33 - strlen (BBS_username)); |
|
|
|
|
|
time_online = time (0) - BBS_login_tm; |
|
|
tm_online = gmtime (&time_online); |
|
|
|
|
|
moveto (screen_lines, 0); |
|
|
prints ("\033[1;44;33m[\033[36m%s\033[33m]" |
|
|
"%s帐号[\033[36m%s\033[33m]" |
|
|
"[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m", |
|
|
str_time, buffer, BBS_username, tm_online->tm_mday - 1, |
|
|
tm_online->tm_hour, tm_online->tm_min); |
|
|
iflush (); |
|
| 268 |
|
|
| 269 |
return 0; |
// User login |
| 270 |
} |
if (bbs_login(db) < 0) |
| 271 |
|
{ |
| 272 |
|
mysql_close(db); |
| 273 |
|
return -4; |
| 274 |
|
} |
| 275 |
|
clearscr(); |
| 276 |
|
|
| 277 |
int |
// BBS Top 10 |
| 278 |
show_active_board () |
display_file_ex("./var/bbs_top.txt", 1, 1); |
|
{ |
|
|
char filename[256], buffer[260]; |
|
|
FILE *fin; |
|
|
int i, j; |
|
|
static long int line; |
|
| 279 |
|
|
| 280 |
sprintf (filename, "%sdata/active_board.txt", app_home_dir); |
// Load menu in shared memory |
| 281 |
|
if (load_menu_shm(p_bbs_menu) < 0) |
| 282 |
|
{ |
| 283 |
|
return -5; |
| 284 |
|
} |
| 285 |
|
|
| 286 |
clrline (3, 2 + ACTIVE_BOARD_HEIGHT); |
// Main |
| 287 |
|
bbs_center(); |
| 288 |
|
|
| 289 |
moveto (3, 0); |
// Unload menu in shared memory |
| 290 |
|
unload_menu_shm(p_bbs_menu); |
| 291 |
|
free(p_bbs_menu); |
| 292 |
|
p_bbs_menu = NULL; |
| 293 |
|
|
| 294 |
if ((fin = fopen (filename, "r")) != NULL) |
// Logout |
| 295 |
{ |
bbs_logout(db); |
|
for (j = 0; j < line; j++) |
|
|
{ |
|
|
if (fgets (buffer, 255, fin) == NULL) |
|
|
{ |
|
|
line = 0; |
|
|
rewind (fin); |
|
|
break; |
|
|
} |
|
|
} |
|
| 296 |
|
|
| 297 |
for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++) |
mysql_close(db); |
|
{ |
|
|
if (fgets (buffer, 255, fin) == NULL) |
|
|
{ |
|
|
line = 0; |
|
|
if (j == 0) |
|
|
{ |
|
|
rewind (fin); |
|
|
if (fgets (buffer, 255, fin) == NULL) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
break; |
|
|
} |
|
|
} |
|
|
line++; |
|
|
i = strlen (buffer); |
|
|
if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r') |
|
|
{ |
|
|
buffer[i - 1] = '\r'; |
|
|
buffer[i] = '\n'; |
|
|
buffer[i + 1] = '\0'; |
|
|
} |
|
|
prints (buffer); |
|
|
iflush (); |
|
|
} |
|
|
fclose (fin); |
|
|
} |
|
| 298 |
|
|
| 299 |
return 0; |
return 0; |
| 300 |
} |
} |