| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
user_list_display.c - description |
/* |
| 3 |
------------------- |
* user_list_display |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - user interactive list of (online) users |
| 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 "common.h" |
#include "common.h" |
| 10 |
#include "io.h" |
#include "io.h" |
| 14 |
#include "str_process.h" |
#include "str_process.h" |
| 15 |
#include "user_list.h" |
#include "user_list.h" |
| 16 |
#include "user_priv.h" |
#include "user_priv.h" |
| 17 |
|
#include "user_info_display.h" |
| 18 |
#include "user_list_display.h" |
#include "user_list_display.h" |
| 19 |
|
#include <ctype.h> |
| 20 |
#include <string.h> |
#include <string.h> |
| 21 |
#include <time.h> |
#include <time.h> |
| 22 |
#include <sys/param.h> |
#include <sys/param.h> |
| 26 |
EXIT_LIST = 0, |
EXIT_LIST = 0, |
| 27 |
VIEW_USER, |
VIEW_USER, |
| 28 |
CHANGE_PAGE, |
CHANGE_PAGE, |
| 29 |
|
REFRESH_LIST, |
| 30 |
SHOW_HELP, |
SHOW_HELP, |
| 31 |
|
SEARCH_USER, |
| 32 |
}; |
}; |
| 33 |
|
|
| 34 |
static int user_list_draw_screen(int online_user) |
static int user_list_draw_screen(int online_user) |
| 35 |
{ |
{ |
| 36 |
clearscr(); |
clearscr(); |
| 37 |
show_top((online_user ? "[在线用户]" : "[已注册用户]"), BBS_name, ""); |
show_top((online_user ? "[线上使用者]" : "[已注册用户]"), BBS_name, ""); |
| 38 |
moveto(2, 0); |
moveto(2, 0); |
| 39 |
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] " |
| 40 |
"查看[\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;32ms\033[0;37m] " |
| 41 |
|
"帮助[\033[1;32mh\033[0;37m]\033[m"); |
| 42 |
moveto(3, 0); |
moveto(3, 0); |
| 43 |
|
|
| 44 |
if (online_user) |
if (online_user) |
| 77 |
else if (p_tm->tm_year > 70) |
else if (p_tm->tm_year > 70) |
| 78 |
{ |
{ |
| 79 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 80 |
"%d年", p_tm->tm_year - 70); |
"%d年%d天", p_tm->tm_year - 70, p_tm->tm_yday); |
| 81 |
} |
} |
| 82 |
else if (p_tm->tm_yday > 0) |
else if (p_tm->tm_yday > 0) |
| 83 |
{ |
{ |
| 87 |
else if (p_tm->tm_hour > 0) |
else if (p_tm->tm_hour > 0) |
| 88 |
{ |
{ |
| 89 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 90 |
"%d时%d分", p_tm->tm_hour, p_tm->tm_min); |
"%d:%.2d", p_tm->tm_hour, p_tm->tm_min); |
| 91 |
} |
} |
| 92 |
else |
else |
| 93 |
{ |
{ |
| 94 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 95 |
"%d分%d秒", p_tm->tm_min, p_tm->tm_sec); |
"%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec); |
| 96 |
} |
} |
| 97 |
|
|
| 98 |
moveto(4 + i, 1); |
moveto(4 + i, 1); |
| 99 |
|
|
| 100 |
prints(" %6d %s%*s %s%*s %s", |
prints(" %6d %s%*s %s%*s %s", |
| 101 |
p_users[i].uid, |
p_users[i].id + 1, |
| 102 |
p_users[i].username, |
p_users[i].username, |
| 103 |
BBS_username_max_len - str_length(p_users[i].username, 1), |
BBS_username_max_len - str_length(p_users[i].username, 1), |
| 104 |
"", |
"", |
| 137 |
else if (p_tm->tm_yday > 0) |
else if (p_tm->tm_yday > 0) |
| 138 |
{ |
{ |
| 139 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 140 |
"%d天%d时", p_tm->tm_yday, p_tm->tm_hour); |
"%dd%dh", p_tm->tm_yday, p_tm->tm_hour); |
| 141 |
} |
} |
| 142 |
else if (p_tm->tm_hour > 0) |
else if (p_tm->tm_hour > 0) |
| 143 |
{ |
{ |
| 144 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 145 |
"%d时%d分", p_tm->tm_hour, p_tm->tm_min); |
"%d:%.2d", p_tm->tm_hour, p_tm->tm_min); |
| 146 |
} |
} |
| 147 |
else |
else |
| 148 |
{ |
{ |
| 149 |
snprintf(str_time_login, sizeof(str_time_login), |
snprintf(str_time_login, sizeof(str_time_login), |
| 150 |
"%d\'%d\"", p_tm->tm_min, p_tm->tm_sec); |
"%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec); |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
tm_duration = tm_now - p_users[i].last_tm; |
tm_duration = tm_now - p_users[i].last_tm; |
| 175 |
moveto(4 + i, 1); |
moveto(4 + i, 1); |
| 176 |
|
|
| 177 |
prints(" %6d %s%*s %s%*s %s%*s %s%*s %s", |
prints(" %6d %s%*s %s%*s %s%*s %s%*s %s", |
| 178 |
p_users[i].user_info.uid, |
p_users[i].id + 1, |
| 179 |
p_users[i].user_info.username, |
p_users[i].user_info.username, |
| 180 |
BBS_username_max_len - str_length(p_users[i].user_info.username, 1), |
BBS_username_max_len - str_length(p_users[i].user_info.username, 1), |
| 181 |
"", |
"", |
| 224 |
case KEY_LEFT: |
case KEY_LEFT: |
| 225 |
return EXIT_LIST; // exit list |
return EXIT_LIST; // exit list |
| 226 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 227 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 228 |
{ |
{ |
| 229 |
log_error("User input timeout\n"); |
log_error("User input timeout\n"); |
| 230 |
return EXIT_LIST; // exit list |
return EXIT_LIST; // exit list |
| 300 |
(*p_selected_index)++; |
(*p_selected_index)++; |
| 301 |
} |
} |
| 302 |
break; |
break; |
| 303 |
|
case 's': |
| 304 |
|
return SEARCH_USER; |
| 305 |
|
case KEY_F5: |
| 306 |
|
return REFRESH_LIST; |
| 307 |
case 'h': |
case 'h': |
| 308 |
return SHOW_HELP; |
return SHOW_HELP; |
| 309 |
default: |
default: |
| 341 |
char page_info_str[LINE_BUFFER_LEN]; |
char page_info_str[LINE_BUFFER_LEN]; |
| 342 |
USER_INFO users[BBS_user_limit_per_page]; |
USER_INFO users[BBS_user_limit_per_page]; |
| 343 |
USER_ONLINE_INFO online_users[BBS_user_limit_per_page]; |
USER_ONLINE_INFO online_users[BBS_user_limit_per_page]; |
| 344 |
int user_count; |
int user_count = 0; |
| 345 |
int page_count; |
int page_count = 0; |
| 346 |
int page_id = 0; |
int page_id = 0; |
| 347 |
int selected_index = 0; |
int selected_index = 0; |
| 348 |
int ret; |
int ret = 0; |
| 349 |
|
|
| 350 |
user_list_draw_screen(online_user); |
user_list_draw_screen(online_user); |
| 351 |
|
|
| 412 |
{ |
{ |
| 413 |
case EXIT_LIST: |
case EXIT_LIST: |
| 414 |
return 0; |
return 0; |
| 415 |
|
case REFRESH_LIST: |
| 416 |
case CHANGE_PAGE: |
case CHANGE_PAGE: |
| 417 |
ret = query_user_list(page_id, users, &user_count, &page_count); |
if (online_user) |
|
if (ret < 0) |
|
| 418 |
{ |
{ |
| 419 |
log_error("query_favor_articles(page_id=%d) error\n", page_id); |
ret = query_user_online_list(page_id, online_users, &user_count, &page_count); |
| 420 |
return -2; |
if (ret < 0) |
| 421 |
|
{ |
| 422 |
|
log_error("query_user_online_list(page_id=%d) error\n", page_id); |
| 423 |
|
return -2; |
| 424 |
|
} |
| 425 |
|
} |
| 426 |
|
else |
| 427 |
|
{ |
| 428 |
|
ret = query_user_list(page_id, users, &user_count, &page_count); |
| 429 |
|
if (ret < 0) |
| 430 |
|
{ |
| 431 |
|
log_error("query_user_list(page_id=%d) error\n", page_id); |
| 432 |
|
return -2; |
| 433 |
|
} |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
if (user_count == 0) // empty list |
if (user_count == 0) // empty list |
| 443 |
} |
} |
| 444 |
break; |
break; |
| 445 |
case VIEW_USER: |
case VIEW_USER: |
| 446 |
log_error("View user (uid=%d)\n", |
user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index])); |
| 447 |
(online_user ? online_users[selected_index].user_info.uid : users[selected_index].uid)); |
user_list_draw_screen(online_user); |
| 448 |
clearscr(); |
break; |
| 449 |
press_any_key_ex("功能不可用,按任意键返回", 60); |
case SEARCH_USER: |
| 450 |
|
user_list_search(); |
| 451 |
user_list_draw_screen(online_user); |
user_list_draw_screen(online_user); |
| 452 |
break; |
break; |
| 453 |
case SHOW_HELP: |
case SHOW_HELP: |
| 460 |
} |
} |
| 461 |
} |
} |
| 462 |
|
|
| 463 |
|
return 0; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
|
int user_list_search(void) |
| 467 |
|
{ |
| 468 |
|
const int users_per_line = 5; |
| 469 |
|
const int max_user_lines = 20; |
| 470 |
|
const int max_user_cnt = users_per_line * max_user_lines + 1; |
| 471 |
|
|
| 472 |
|
char username[BBS_username_max_len + 1]; |
| 473 |
|
int32_t uid_list[max_user_cnt]; |
| 474 |
|
char username_list[max_user_cnt][BBS_username_max_len + 1]; |
| 475 |
|
int ret; |
| 476 |
|
int i; |
| 477 |
|
USER_INFO user_info; |
| 478 |
|
char user_intro[BBS_user_intro_max_len + 1]; |
| 479 |
|
int ok; |
| 480 |
|
int ch; |
| 481 |
|
|
| 482 |
|
username[0] = '\0'; |
| 483 |
|
|
| 484 |
|
clearscr(); |
| 485 |
|
|
| 486 |
|
while (!SYS_server_exit) |
| 487 |
|
{ |
| 488 |
|
clrline(3, SCREEN_ROWS); |
| 489 |
|
get_data(2, 1, "查找谁: ", username, sizeof(username), BBS_username_max_len); |
| 490 |
|
|
| 491 |
|
if (username[0] == '\0') |
| 492 |
|
{ |
| 493 |
|
return 0; |
| 494 |
|
} |
| 495 |
|
|
| 496 |
|
// Verify format |
| 497 |
|
for (i = 0, ok = 1; ok && username[i] != '\0'; i++) |
| 498 |
|
{ |
| 499 |
|
if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_')))) |
| 500 |
|
{ |
| 501 |
|
ok = 0; |
| 502 |
|
} |
| 503 |
|
} |
| 504 |
|
if (ok && i > BBS_username_max_len) |
| 505 |
|
{ |
| 506 |
|
ok = 0; |
| 507 |
|
} |
| 508 |
|
if (!ok) |
| 509 |
|
{ |
| 510 |
|
moveto(3, 1); |
| 511 |
|
clrtoeol(); |
| 512 |
|
prints("用户名格式非法"); |
| 513 |
|
continue; |
| 514 |
|
} |
| 515 |
|
|
| 516 |
|
clrline(3, SCREEN_ROWS); |
| 517 |
|
|
| 518 |
|
ret = query_user_info_by_username(username, max_user_cnt, uid_list, username_list); |
| 519 |
|
|
| 520 |
|
if (ret < 0) |
| 521 |
|
{ |
| 522 |
|
log_error("query_user_info_by_username(%s) error\n", username); |
| 523 |
|
return -1; |
| 524 |
|
} |
| 525 |
|
else if (ret > 1) |
| 526 |
|
{ |
| 527 |
|
for (i = 0; i < MIN(ret, users_per_line * max_user_lines); i++) |
| 528 |
|
{ |
| 529 |
|
moveto(4 + i / users_per_line, 3 + i % users_per_line * (BBS_username_max_len + 3)); |
| 530 |
|
prints("%s", username_list[i]); |
| 531 |
|
} |
| 532 |
|
moveto(SCREEN_ROWS, 1); |
| 533 |
|
if (ret > users_per_line * max_user_lines) |
| 534 |
|
{ |
| 535 |
|
prints("还有更多..."); |
| 536 |
|
} |
| 537 |
|
|
| 538 |
|
moveto(3, 1); |
| 539 |
|
prints("存在多个匹配的用户,按\033[1;33mEnter\033[m精确查找"); |
| 540 |
|
iflush(); |
| 541 |
|
|
| 542 |
|
ch = igetch_t(BBS_max_user_idle_time); |
| 543 |
|
switch (ch) |
| 544 |
|
{ |
| 545 |
|
case KEY_NULL: |
| 546 |
|
case KEY_TIMEOUT: |
| 547 |
|
return -1; |
| 548 |
|
case KEY_ESC: |
| 549 |
|
return 0; |
| 550 |
|
case CR: |
| 551 |
|
ret = (strcasecmp(username_list[0], username) == 0 ? 1 : 0); |
| 552 |
|
break; |
| 553 |
|
default: |
| 554 |
|
i = (int)strnlen(username, sizeof(username) - 1); |
| 555 |
|
if (i + 1 <= BBS_username_max_len && (isalnum((char)ch) || ch == '_')) |
| 556 |
|
{ |
| 557 |
|
username[i] = (char)ch; |
| 558 |
|
username[i + 1] = '\0'; |
| 559 |
|
} |
| 560 |
|
continue; |
| 561 |
|
} |
| 562 |
|
} |
| 563 |
|
|
| 564 |
|
clrline(3, SCREEN_ROWS); |
| 565 |
|
if (ret == 0) |
| 566 |
|
{ |
| 567 |
|
moveto(3, 1); |
| 568 |
|
prints("没有找到符合条件的用户"); |
| 569 |
|
press_any_key(); |
| 570 |
|
return 0; |
| 571 |
|
} |
| 572 |
|
else // ret == 1 |
| 573 |
|
{ |
| 574 |
|
if (query_user_info_by_uid(uid_list[0], &user_info, user_intro, sizeof(user_intro)) <= 0) |
| 575 |
|
{ |
| 576 |
|
log_error("query_user_info_by_uid(uid=%d) error\n", uid_list[0]); |
| 577 |
|
return -2; |
| 578 |
|
} |
| 579 |
|
else if (user_info_display(&user_info) < 0) |
| 580 |
|
{ |
| 581 |
|
log_error("user_info_display(uid=%d) error\n", uid_list[0]); |
| 582 |
|
return -3; |
| 583 |
|
} |
| 584 |
|
return 1; |
| 585 |
|
} |
| 586 |
|
} |
| 587 |
|
|
| 588 |
return 0; |
return 0; |
| 589 |
} |
} |