| 16 |
|
|
| 17 |
#include "common.h" |
#include "common.h" |
| 18 |
#include "io.h" |
#include "io.h" |
| 19 |
|
#include "lml.h" |
| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "login.h" |
#include "login.h" |
| 22 |
#include "screen.h" |
#include "screen.h" |
| 33 |
EXIT_LIST = 0, |
EXIT_LIST = 0, |
| 34 |
VIEW_USER, |
VIEW_USER, |
| 35 |
CHANGE_PAGE, |
CHANGE_PAGE, |
| 36 |
|
REFRESH_LIST, |
| 37 |
SHOW_HELP, |
SHOW_HELP, |
| 38 |
}; |
}; |
| 39 |
|
|
| 40 |
static int user_list_draw_screen() |
static int display_user_intro_key_handler(int *p_key, DISPLAY_CTX *p_ctx) |
| 41 |
|
{ |
| 42 |
|
return 0; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
static int user_list_draw_screen(int online_user) |
| 46 |
{ |
{ |
| 47 |
clearscr(); |
clearscr(); |
| 48 |
show_top("[已注册用户]", BBS_name, ""); |
show_top((online_user ? "[线上使用者]" : "[已注册用户]"), BBS_name, ""); |
| 49 |
moveto(2, 0); |
moveto(2, 0); |
| 50 |
prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] " |
prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] " |
| 51 |
"查看[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m"); |
"查看[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m"); |
| 52 |
moveto(3, 0); |
moveto(3, 0); |
| 53 |
prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 上次登陆距今 \033[m"); |
|
| 54 |
|
if (online_user) |
| 55 |
|
{ |
| 56 |
|
prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 在线时长 空闲 最后活动 \033[m"); |
| 57 |
|
} |
| 58 |
|
else |
| 59 |
|
{ |
| 60 |
|
prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 上次登陆距今 \033[m"); |
| 61 |
|
} |
| 62 |
|
|
| 63 |
return 0; |
return 0; |
| 64 |
} |
} |
| 65 |
|
|
| 66 |
static int user_list_draw_items(int page_id, USER_INFO *p_users, |
static int user_list_draw_items(int page_id, USER_INFO *p_users, int user_count) |
|
int user_count) |
|
| 67 |
{ |
{ |
| 68 |
char str_time[LINE_BUFFER_LEN]; |
char str_time_login[LINE_BUFFER_LEN]; |
| 69 |
time_t tm_now; |
time_t tm_now; |
| 70 |
time_t tm_duration; |
time_t tm_duration; |
| 71 |
struct tm *p_tm; |
struct tm *p_tm; |
| 79 |
{ |
{ |
| 80 |
tm_duration = tm_now - p_users[i].last_login_dt; |
tm_duration = tm_now - p_users[i].last_login_dt; |
| 81 |
p_tm = gmtime(&tm_duration); |
p_tm = gmtime(&tm_duration); |
| 82 |
if (p_tm->tm_year > 70) |
if (p_tm == NULL) |
| 83 |
{ |
{ |
| 84 |
snprintf(str_time, sizeof(str_time), |
log_error("Invalid time duration\n"); |
| 85 |
|
str_time_login[0] = '\0'; |
| 86 |
|
} |
| 87 |
|
else if (p_tm->tm_year > 70) |
| 88 |
|
{ |
| 89 |
|
snprintf(str_time_login, sizeof(str_time_login), |
| 90 |
"%d年", p_tm->tm_year - 70); |
"%d年", p_tm->tm_year - 70); |
| 91 |
} |
} |
| 92 |
else if (p_tm->tm_yday > 1) |
else if (p_tm->tm_yday > 0) |
| 93 |
{ |
{ |
| 94 |
snprintf(str_time, sizeof(str_time), |
snprintf(str_time_login, sizeof(str_time_login), |
| 95 |
"%d天", p_tm->tm_yday); |
"%d天", p_tm->tm_yday); |
| 96 |
} |
} |
| 97 |
else if (p_tm->tm_hour > 0) |
else if (p_tm->tm_hour > 0) |
| 98 |
{ |
{ |
| 99 |
snprintf(str_time, sizeof(str_time), |
snprintf(str_time_login, sizeof(str_time_login), |
| 100 |
"%d时%d分", p_tm->tm_hour, p_tm->tm_min); |
"%d时%d分", p_tm->tm_hour, p_tm->tm_min); |
| 101 |
} |
} |
| 102 |
else |
else |
| 103 |
{ |
{ |
| 104 |
snprintf(str_time, sizeof(str_time), |
snprintf(str_time_login, sizeof(str_time_login), |
| 105 |
"%d分%d秒", p_tm->tm_min, p_tm->tm_sec); |
"%d分%d秒", p_tm->tm_min, p_tm->tm_sec); |
| 106 |
} |
} |
| 107 |
|
|
| 108 |
moveto(4 + i, 1); |
moveto(4 + i, 1); |
| 109 |
|
|
| 110 |
prints(" %7d %s%*s %s%*s %s", |
prints(" %6d %s%*s %s%*s %s", |
| 111 |
p_users[i].uid, |
p_users[i].id + 1, |
| 112 |
p_users[i].username, |
p_users[i].username, |
| 113 |
BBS_username_max_len - str_length(p_users[i].username, 1), |
BBS_username_max_len - str_length(p_users[i].username, 1), |
| 114 |
"", |
"", |
| 115 |
p_users[i].nickname, |
p_users[i].nickname, |
| 116 |
BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1), |
BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1), |
| 117 |
"", |
"", |
| 118 |
str_time); |
str_time_login); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
return 0; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
static int user_online_list_draw_items(int page_id, USER_ONLINE_INFO *p_users, int user_count) |
| 125 |
|
{ |
| 126 |
|
char str_time_login[LINE_BUFFER_LEN]; |
| 127 |
|
char str_time_idle[LINE_BUFFER_LEN]; |
| 128 |
|
const char *p_action_title; |
| 129 |
|
time_t tm_now; |
| 130 |
|
time_t tm_duration; |
| 131 |
|
struct tm *p_tm; |
| 132 |
|
int i; |
| 133 |
|
|
| 134 |
|
clrline(4, 23); |
| 135 |
|
|
| 136 |
|
time(&tm_now); |
| 137 |
|
|
| 138 |
|
for (i = 0; i < user_count; i++) |
| 139 |
|
{ |
| 140 |
|
tm_duration = tm_now - p_users[i].login_tm; |
| 141 |
|
p_tm = gmtime(&tm_duration); |
| 142 |
|
if (p_tm == NULL) |
| 143 |
|
{ |
| 144 |
|
log_error("Invalid time duration\n"); |
| 145 |
|
str_time_login[0] = '\0'; |
| 146 |
|
} |
| 147 |
|
else if (p_tm->tm_yday > 0) |
| 148 |
|
{ |
| 149 |
|
snprintf(str_time_login, sizeof(str_time_login), |
| 150 |
|
"%d天%d时", p_tm->tm_yday, p_tm->tm_hour); |
| 151 |
|
} |
| 152 |
|
else if (p_tm->tm_hour > 0) |
| 153 |
|
{ |
| 154 |
|
snprintf(str_time_login, sizeof(str_time_login), |
| 155 |
|
"%d时%d分", p_tm->tm_hour, p_tm->tm_min); |
| 156 |
|
} |
| 157 |
|
else |
| 158 |
|
{ |
| 159 |
|
snprintf(str_time_login, sizeof(str_time_login), |
| 160 |
|
"%d\'%d\"", p_tm->tm_min, p_tm->tm_sec); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
tm_duration = tm_now - p_users[i].last_tm; |
| 164 |
|
p_tm = gmtime(&tm_duration); |
| 165 |
|
if (p_tm == NULL) |
| 166 |
|
{ |
| 167 |
|
log_error("Invalid time duration\n"); |
| 168 |
|
str_time_idle[0] = '\0'; |
| 169 |
|
} |
| 170 |
|
else if (p_tm->tm_min > 0) |
| 171 |
|
{ |
| 172 |
|
snprintf(str_time_idle, sizeof(str_time_idle), |
| 173 |
|
"%d\'%d\"", p_tm->tm_min, p_tm->tm_sec); |
| 174 |
|
} |
| 175 |
|
else |
| 176 |
|
{ |
| 177 |
|
snprintf(str_time_idle, sizeof(str_time_idle), |
| 178 |
|
"%d\"", p_tm->tm_sec); |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
p_action_title = (p_users[i].current_action_title != NULL |
| 182 |
|
? p_users[i].current_action_title |
| 183 |
|
: p_users[i].current_action); |
| 184 |
|
|
| 185 |
|
moveto(4 + i, 1); |
| 186 |
|
|
| 187 |
|
prints(" %6d %s%*s %s%*s %s%*s %s%*s %s", |
| 188 |
|
p_users[i].id + 1, |
| 189 |
|
p_users[i].user_info.username, |
| 190 |
|
BBS_username_max_len - str_length(p_users[i].user_info.username, 1), |
| 191 |
|
"", |
| 192 |
|
p_users[i].user_info.nickname, |
| 193 |
|
BBS_nickname_max_len / 2 - str_length(p_users[i].user_info.nickname, 1), |
| 194 |
|
"", |
| 195 |
|
str_time_login, |
| 196 |
|
8 - str_length(str_time_login, 1), |
| 197 |
|
"", |
| 198 |
|
str_time_idle, |
| 199 |
|
6 - str_length(str_time_idle, 1), |
| 200 |
|
"", |
| 201 |
|
p_action_title); |
| 202 |
} |
} |
| 203 |
|
|
| 204 |
return 0; |
return 0; |
| 310 |
(*p_selected_index)++; |
(*p_selected_index)++; |
| 311 |
} |
} |
| 312 |
break; |
break; |
| 313 |
|
case KEY_F5: |
| 314 |
|
return REFRESH_LIST; |
| 315 |
case 'h': |
case 'h': |
| 316 |
return SHOW_HELP; |
return SHOW_HELP; |
| 317 |
default: |
default: |
| 344 |
return EXIT_LIST; |
return EXIT_LIST; |
| 345 |
} |
} |
| 346 |
|
|
| 347 |
int user_list_display(void) |
int user_list_display(int online_user) |
| 348 |
{ |
{ |
| 349 |
char page_info_str[LINE_BUFFER_LEN]; |
char page_info_str[LINE_BUFFER_LEN]; |
| 350 |
USER_INFO users[BBS_user_limit_per_page]; |
USER_INFO users[BBS_user_limit_per_page]; |
| 351 |
USER_INFO user_info; |
USER_ONLINE_INFO online_users[BBS_user_limit_per_page]; |
| 352 |
int user_count; |
int user_count = 0; |
| 353 |
int page_count; |
int page_count = 0; |
| 354 |
int page_id = 0; |
int page_id = 0; |
| 355 |
int selected_index = 0; |
int selected_index = 0; |
| 356 |
int ret; |
int ret = 0; |
| 357 |
|
|
| 358 |
user_list_draw_screen(); |
user_list_draw_screen(online_user); |
| 359 |
|
|
| 360 |
ret = query_user_list(page_id, users, &user_count, &page_count); |
if (online_user) |
| 361 |
if (ret < 0) |
{ |
| 362 |
|
ret = query_user_online_list(page_id, online_users, &user_count, &page_count); |
| 363 |
|
if (ret < 0) |
| 364 |
|
{ |
| 365 |
|
log_error("query_user_online_list(page_id=%d) error\n", page_id); |
| 366 |
|
return -2; |
| 367 |
|
} |
| 368 |
|
} |
| 369 |
|
else |
| 370 |
{ |
{ |
| 371 |
log_error("query_user_list(page_id=%d) error\n", page_id); |
ret = query_user_list(page_id, users, &user_count, &page_count); |
| 372 |
return -2; |
if (ret < 0) |
| 373 |
|
{ |
| 374 |
|
log_error("query_user_list(page_id=%d) error\n", page_id); |
| 375 |
|
return -2; |
| 376 |
|
} |
| 377 |
} |
} |
| 378 |
|
|
| 379 |
if (user_count == 0) // empty list |
if (user_count == 0) // empty list |
| 383 |
|
|
| 384 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 385 |
{ |
{ |
| 386 |
ret = user_list_draw_items(page_id, users, user_count); |
if (online_user) |
|
if (ret < 0) |
|
| 387 |
{ |
{ |
| 388 |
log_error("user_list_draw_items(page_id=%d) error\n", page_id); |
ret = user_online_list_draw_items(page_id, online_users, user_count); |
| 389 |
return -3; |
if (ret < 0) |
| 390 |
|
{ |
| 391 |
|
log_error("user_online_list_draw_items(page_id=%d) error\n", page_id); |
| 392 |
|
return -3; |
| 393 |
|
} |
| 394 |
|
} |
| 395 |
|
else |
| 396 |
|
{ |
| 397 |
|
ret = user_list_draw_items(page_id, users, user_count); |
| 398 |
|
if (ret < 0) |
| 399 |
|
{ |
| 400 |
|
log_error("user_list_draw_items(page_id=%d) error\n", page_id); |
| 401 |
|
return -3; |
| 402 |
|
} |
| 403 |
} |
} |
| 404 |
|
|
| 405 |
snprintf(page_info_str, sizeof(page_info_str), |
snprintf(page_info_str, sizeof(page_info_str), |
| 409 |
show_bottom(page_info_str); |
show_bottom(page_info_str); |
| 410 |
iflush(); |
iflush(); |
| 411 |
|
|
| 412 |
if (user_online_update("USER_LIST") < 0) |
if (user_online_update(online_user ? "USER_ONLINE" : "USER_LIST") < 0) |
| 413 |
{ |
{ |
| 414 |
log_error("user_online_update(USER_LIST) error\n"); |
log_error("user_online_update(%s) error\n", |
| 415 |
|
(online_user ? "USER_ONLINE" : "USER_LIST")); |
| 416 |
} |
} |
| 417 |
|
|
| 418 |
ret = user_list_select(page_count, user_count, &page_id, &selected_index); |
ret = user_list_select(page_count, user_count, &page_id, &selected_index); |
| 420 |
{ |
{ |
| 421 |
case EXIT_LIST: |
case EXIT_LIST: |
| 422 |
return 0; |
return 0; |
| 423 |
|
case REFRESH_LIST: |
| 424 |
case CHANGE_PAGE: |
case CHANGE_PAGE: |
| 425 |
ret = query_user_list(page_id, users, &user_count, &page_count); |
if (online_user) |
| 426 |
if (ret < 0) |
{ |
| 427 |
|
ret = query_user_online_list(page_id, online_users, &user_count, &page_count); |
| 428 |
|
if (ret < 0) |
| 429 |
|
{ |
| 430 |
|
log_error("query_user_online_list(page_id=%d) error\n", page_id); |
| 431 |
|
return -2; |
| 432 |
|
} |
| 433 |
|
} |
| 434 |
|
else |
| 435 |
{ |
{ |
| 436 |
log_error("query_favor_articles(page_id=%d) error\n", page_id); |
ret = query_user_list(page_id, users, &user_count, &page_count); |
| 437 |
return -2; |
if (ret < 0) |
| 438 |
|
{ |
| 439 |
|
log_error("query_user_list(page_id=%d) error\n", page_id); |
| 440 |
|
return -2; |
| 441 |
|
} |
| 442 |
} |
} |
| 443 |
|
|
| 444 |
if (user_count == 0) // empty list |
if (user_count == 0) // empty list |
| 451 |
} |
} |
| 452 |
break; |
break; |
| 453 |
case VIEW_USER: |
case VIEW_USER: |
| 454 |
if ((ret = query_user_info(users[selected_index].uid, &user_info)) < 0) |
clearscr(); |
| 455 |
|
if (online_user) |
| 456 |
{ |
{ |
| 457 |
log_error("query_user_info(uid=%d) error: %d\n", users[selected_index].uid, ret); |
USER_ONLINE_INFO users[5]; |
| 458 |
} |
int user_cnt = 5; |
| 459 |
else if (ret == 0) |
ret = query_user_online_info_by_uid(online_users[selected_index].user_info.uid, users, &user_cnt, 0); |
| 460 |
{ |
if (ret <= 0) |
| 461 |
log_error("query_user_info(uid=%d) error: user not found\n", users[selected_index].uid); |
{ |
| 462 |
} |
log_error("query_user_online_info_by_uid(uid=%d, cnt=%d) error: %d\n", |
| 463 |
else if (users[selected_index].uid != user_info.uid) |
online_users[selected_index].user_info.uid, user_cnt, ret); |
| 464 |
{ |
} |
| 465 |
log_error("query_user_info(uid=%d) error: inconsistent uid=%d\n", users[selected_index].uid, user_info.uid); |
else |
| 466 |
|
{ |
| 467 |
|
moveto(1, 1); |
| 468 |
|
prints("已选中用户 [%s] 会话 %d", online_users[selected_index].user_info.username, online_users[selected_index].id); |
| 469 |
|
|
| 470 |
|
for (int i = 0; i < user_cnt; i++) |
| 471 |
|
{ |
| 472 |
|
moveto(2 + i, 1); |
| 473 |
|
prints("会话%d", users[i].id); |
| 474 |
|
} |
| 475 |
|
} |
| 476 |
} |
} |
| 477 |
else |
else |
| 478 |
{ |
{ |
| 479 |
clearscr(); |
char intro_f[BBS_user_intro_max_len]; |
| 480 |
press_any_key_ex("功能不可用,按任意键返回", 60); |
char profile_f[BUFSIZ]; |
| 481 |
user_list_draw_screen(); |
long line_offsets[BBS_user_intro_max_line + 1]; |
| 482 |
|
long lines; |
| 483 |
|
|
| 484 |
|
lml_render(users[selected_index].intro, intro_f, sizeof(intro_f), 0); |
| 485 |
|
|
| 486 |
|
snprintf(profile_f, sizeof(profile_f), |
| 487 |
|
"已选中用户 [%s]\n发帖数:%d\n\n%s\n", |
| 488 |
|
users[selected_index].username, |
| 489 |
|
user_stat_get_article_cnt(users[selected_index].uid), |
| 490 |
|
intro_f); |
| 491 |
|
|
| 492 |
|
lines = split_data_lines(profile_f, SCREEN_COLS, line_offsets, BBS_user_intro_max_line + 4, 1, NULL); |
| 493 |
|
display_data(profile_f, lines, line_offsets, 2, display_user_intro_key_handler, DATA_READ_HELP); |
| 494 |
} |
} |
| 495 |
|
press_any_key(); |
| 496 |
|
user_list_draw_screen(online_user); |
| 497 |
break; |
break; |
| 498 |
case SHOW_HELP: |
case SHOW_HELP: |
| 499 |
// Display help information |
// Display help information |
| 500 |
display_file(DATA_READ_HELP, 1); |
display_file(DATA_READ_HELP, 1); |
| 501 |
user_list_draw_screen(); |
user_list_draw_screen(online_user); |
| 502 |
break; |
break; |
| 503 |
default: |
default: |
| 504 |
log_error("Unknown command %d\n", ret); |
log_error("Unknown command %d\n", ret); |