| 16 |
|
|
| 17 |
#include "bbs_main.h" |
#include "bbs_main.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
|
#include "welcome.h" |
|
| 19 |
#include "login.h" |
#include "login.h" |
| 20 |
#include "user_priv.h" |
#include "user_priv.h" |
| 21 |
#include "common.h" |
#include "common.h" |
| 34 |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
prints("欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m( %s )\r\n", |
| 35 |
BBS_name, BBS_server, app_version); |
BBS_name, BBS_server, app_version); |
| 36 |
|
|
| 37 |
iflush(); |
return iflush(); |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
int bbs_welcome(MYSQL *db) |
| 41 |
|
{ |
| 42 |
|
char sql[SQL_BUFFER_LEN]; |
| 43 |
|
|
| 44 |
|
u_int32_t u_online = 0; |
| 45 |
|
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 |
|
snprintf(sql, sizeof(sql), |
| 75 |
|
"SELECT COUNT(*) AS cc FROM " |
| 76 |
|
"(SELECT DISTINCT SID FROM user_online " |
| 77 |
|
"WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1", |
| 78 |
|
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 |
|
snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable"); |
| 96 |
|
if (mysql_query(db, sql) != 0) |
| 97 |
|
{ |
| 98 |
|
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 |
|
snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1"); |
| 113 |
|
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 |
|
// Log max user_online |
| 130 |
|
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 |
|
// 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; |
return 0; |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
int bbs_exit() |
int bbs_logout(MYSQL *db) |
| 169 |
{ |
{ |
| 170 |
|
if (user_online_del(db) < 0) |
| 171 |
|
{ |
| 172 |
|
return -1; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
display_file_ex(DATA_GOODBYE, 1, 0); |
display_file_ex(DATA_GOODBYE, 1, 0); |
| 176 |
|
|
| 177 |
sleep(1); |
log_std("User logout\n"); |
| 178 |
|
|
| 179 |
return 0; |
return 0; |
| 180 |
} |
} |
| 191 |
show_top(""); |
show_top(""); |
| 192 |
show_active_board(); |
show_active_board(); |
| 193 |
show_bottom(""); |
show_bottom(""); |
| 194 |
display_menu(get_menu(&bbs_menu, "TOPMENU")); |
display_menu(p_bbs_menu); |
| 195 |
|
|
| 196 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 197 |
{ |
{ |
| 198 |
ch = igetch(0); |
ch = igetch(100); |
| 199 |
|
|
| 200 |
if (time(0) - t_last_action >= 10) |
if (time(0) - t_last_action >= 10) |
| 201 |
{ |
{ |
| 206 |
|
|
| 207 |
switch (ch) |
switch (ch) |
| 208 |
{ |
{ |
| 209 |
case KEY_NULL: |
case KEY_NULL: // broken pipe |
| 210 |
|
return 0; |
| 211 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 212 |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) |
| 213 |
{ |
{ |
| 214 |
return 0; |
return 0; |
| 215 |
} |
} |
| 216 |
continue; |
continue; |
| 217 |
|
case CR: |
| 218 |
|
igetch_reset(); |
| 219 |
default: |
default: |
| 220 |
switch (menu_control(&bbs_menu, ch)) |
switch (menu_control(p_bbs_menu, ch)) |
| 221 |
{ |
{ |
| 222 |
case EXITBBS: |
case EXITBBS: |
| 223 |
return 0; |
return 0; |
| 226 |
show_top(""); |
show_top(""); |
| 227 |
show_active_board(); |
show_active_board(); |
| 228 |
show_bottom(""); |
show_bottom(""); |
| 229 |
display_current_menu(&bbs_menu); |
display_menu(p_bbs_menu); |
| 230 |
break; |
break; |
| 231 |
case NOREDRAW: |
case NOREDRAW: |
| 232 |
case UNKNOWN_CMD: |
case UNKNOWN_CMD: |
| 242 |
|
|
| 243 |
int bbs_main() |
int bbs_main() |
| 244 |
{ |
{ |
| 245 |
int ret; |
MYSQL *db; |
| 246 |
|
|
| 247 |
set_input_echo(0); |
set_input_echo(0); |
| 248 |
|
|
| 249 |
bbs_info(); |
// System info |
| 250 |
|
if (bbs_info() < 0) |
| 251 |
|
{ |
| 252 |
|
return -1; |
| 253 |
|
} |
| 254 |
|
|
| 255 |
|
db = db_open(); |
| 256 |
|
if (db == NULL) |
| 257 |
|
{ |
| 258 |
|
prints("无法连接数据库\n"); |
| 259 |
|
return -2; |
| 260 |
|
} |
| 261 |
|
|
| 262 |
// Welcome |
// Welcome |
| 263 |
bbs_welcome(); |
if (bbs_welcome(db) < 0) |
| 264 |
|
{ |
| 265 |
|
mysql_close(db); |
| 266 |
|
return -3; |
| 267 |
|
} |
| 268 |
|
|
| 269 |
// Login |
// User login |
| 270 |
ret = bbs_login(); |
if (bbs_login(db) < 0) |
| 271 |
if (ret < 0) |
{ |
| 272 |
return -1; |
mysql_close(db); |
| 273 |
log_std("User \"%s\"(%ld) login from %s:%d\n", |
return -4; |
| 274 |
BBS_username, BBS_priv.uid, hostaddr_client, port_client); |
} |
| 275 |
clearscr(); |
clearscr(); |
| 276 |
|
|
| 277 |
// BBS Top 10 |
// BBS Top 10 |
| 278 |
display_file_ex("./var/bbs_top.txt", 1, 1); |
display_file_ex("./var/bbs_top.txt", 1, 1); |
| 279 |
|
|
| 280 |
|
// Load menu in shared memory |
| 281 |
|
if (load_menu_shm(p_bbs_menu) < 0) |
| 282 |
|
{ |
| 283 |
|
return -5; |
| 284 |
|
} |
| 285 |
|
|
| 286 |
// Main |
// Main |
| 287 |
bbs_center(); |
bbs_center(); |
| 288 |
|
|
| 289 |
// Logout |
// Unload menu in shared memory |
| 290 |
bbs_exit(); |
unload_menu_shm(p_bbs_menu); |
| 291 |
log_std("User logout\n"); |
free(p_bbs_menu); |
| 292 |
|
p_bbs_menu = NULL; |
|
MYSQL *db = db_open(); |
|
|
if (db == NULL) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
| 293 |
|
|
| 294 |
user_online_del(db); |
// Logout |
| 295 |
|
bbs_logout(db); |
| 296 |
|
|
| 297 |
mysql_close(db); |
mysql_close(db); |
| 298 |
|
|