/[LeafOK_CVS]/lbbs/src/user_list_display.c
ViewVC logotype

Diff of /lbbs/src/user_list_display.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Wed Oct 22 07:39:02 2025 UTC Revision 1.23 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
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     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "common.h"  #include "common.h"
14  #include "io.h"  #include "io.h"
# Line 22  Line 18 
18  #include "str_process.h"  #include "str_process.h"
19  #include "user_list.h"  #include "user_list.h"
20  #include "user_priv.h"  #include "user_priv.h"
21    #include "user_info_display.h"
22  #include "user_list_display.h"  #include "user_list_display.h"
23    #include <ctype.h>
24  #include <string.h>  #include <string.h>
25  #include <time.h>  #include <time.h>
26  #include <sys/param.h>  #include <sys/param.h>
# Line 34  enum select_cmd_t Line 32  enum select_cmd_t
32          CHANGE_PAGE,          CHANGE_PAGE,
33          REFRESH_LIST,          REFRESH_LIST,
34          SHOW_HELP,          SHOW_HELP,
35            SEARCH_USER,
36  };  };
37    
38  static int user_list_draw_screen(int online_user)  static int user_list_draw_screen(int online_user)
# Line 42  static int user_list_draw_screen(int onl Line 41  static int user_list_draw_screen(int onl
41          show_top((online_user ? "[线上使用者]" : "[已注册用户]"), BBS_name, "");          show_top((online_user ? "[线上使用者]" : "[已注册用户]"), BBS_name, "");
42          moveto(2, 0);          moveto(2, 0);
43          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] "
44                     "查看[\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] "
45                       "帮助[\033[1;32mh\033[0;37m]\033[m");
46          moveto(3, 0);          moveto(3, 0);
47    
48          if (online_user)          if (online_user)
# Line 81  static int user_list_draw_items(int page Line 81  static int user_list_draw_items(int page
81                  else if (p_tm->tm_year > 70)                  else if (p_tm->tm_year > 70)
82                  {                  {
83                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
84                                           "%d年", p_tm->tm_year - 70);                                           "%d年%d天", p_tm->tm_year - 70, p_tm->tm_yday);
85                  }                  }
86                  else if (p_tm->tm_yday > 0)                  else if (p_tm->tm_yday > 0)
87                  {                  {
# Line 91  static int user_list_draw_items(int page Line 91  static int user_list_draw_items(int page
91                  else if (p_tm->tm_hour > 0)                  else if (p_tm->tm_hour > 0)
92                  {                  {
93                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
94                                           "%d时%d分", p_tm->tm_hour, p_tm->tm_min);                                           "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
95                  }                  }
96                  else                  else
97                  {                  {
98                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
99                                           "%d分%d秒", p_tm->tm_min, p_tm->tm_sec);                                           "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
100                  }                  }
101    
102                  moveto(4 + i, 1);                  moveto(4 + i, 1);
# Line 141  static int user_online_list_draw_items(i Line 141  static int user_online_list_draw_items(i
141                  else if (p_tm->tm_yday > 0)                  else if (p_tm->tm_yday > 0)
142                  {                  {
143                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
144                                           "%d天%d时", p_tm->tm_yday, p_tm->tm_hour);                                           "%dd%dh", p_tm->tm_yday, p_tm->tm_hour);
145                  }                  }
146                  else if (p_tm->tm_hour > 0)                  else if (p_tm->tm_hour > 0)
147                  {                  {
148                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
149                                           "%d时%d分", p_tm->tm_hour, p_tm->tm_min);                                           "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
150                  }                  }
151                  else                  else
152                  {                  {
153                          snprintf(str_time_login, sizeof(str_time_login),                          snprintf(str_time_login, sizeof(str_time_login),
154                                           "%d\'%d\"", p_tm->tm_min, p_tm->tm_sec);                                           "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
155                  }                  }
156    
157                  tm_duration = tm_now - p_users[i].last_tm;                  tm_duration = tm_now - p_users[i].last_tm;
# Line 228  static enum select_cmd_t user_list_selec Line 228  static enum select_cmd_t user_list_selec
228                  case KEY_LEFT:                  case KEY_LEFT:
229                          return EXIT_LIST; // exit list                          return EXIT_LIST; // exit list
230                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
231                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
232                          {                          {
233                                  log_error("User input timeout\n");                                  log_error("User input timeout\n");
234                                  return EXIT_LIST; // exit list                                  return EXIT_LIST; // exit list
# Line 304  static enum select_cmd_t user_list_selec Line 304  static enum select_cmd_t user_list_selec
304                                  (*p_selected_index)++;                                  (*p_selected_index)++;
305                          }                          }
306                          break;                          break;
307                    case 's':
308                            return SEARCH_USER;
309                  case KEY_F5:                  case KEY_F5:
310                          return REFRESH_LIST;                          return REFRESH_LIST;
311                  case 'h':                  case 'h':
# Line 343  int user_list_display(int online_user) Line 345  int user_list_display(int online_user)
345          char page_info_str[LINE_BUFFER_LEN];          char page_info_str[LINE_BUFFER_LEN];
346          USER_INFO users[BBS_user_limit_per_page];          USER_INFO users[BBS_user_limit_per_page];
347          USER_ONLINE_INFO online_users[BBS_user_limit_per_page];          USER_ONLINE_INFO online_users[BBS_user_limit_per_page];
348          int user_count;          int user_count = 0;
349          int page_count;          int page_count = 0;
350          int page_id = 0;          int page_id = 0;
351          int selected_index = 0;          int selected_index = 0;
352          int ret;          int ret = 0;
353    
354          user_list_draw_screen(online_user);          user_list_draw_screen(online_user);
355    
# Line 445  int user_list_display(int online_user) Line 447  int user_list_display(int online_user)
447                          }                          }
448                          break;                          break;
449                  case VIEW_USER:                  case VIEW_USER:
450                          clearscr();                          user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index]));
451                          prints("已选中用户 [%s]\n", online_user                          user_list_draw_screen(online_user);
452                                                                                          ? online_users[selected_index].user_info.username                          break;
453                                                                                          : users[selected_index].username);                  case SEARCH_USER:
454                          press_any_key();                          user_list_search();
455                          user_list_draw_screen(online_user);                          user_list_draw_screen(online_user);
456                          break;                          break;
457                  case SHOW_HELP:                  case SHOW_HELP:
# Line 462  int user_list_display(int online_user) Line 464  int user_list_display(int online_user)
464                  }                  }
465          }          }
466    
467            return 0;
468    }
469    
470    int user_list_search(void)
471    {
472            const int users_per_line = 5;
473            const int max_user_lines = 20;
474            const int max_user_cnt = users_per_line * max_user_lines + 1;
475    
476            char username[BBS_username_max_len + 1];
477            int32_t uid_list[max_user_cnt];
478            char username_list[max_user_cnt][BBS_username_max_len + 1];
479            int ret;
480            int i;
481            USER_INFO user_info;
482            char user_intro[BBS_user_intro_max_len + 1];
483            int ok;
484            int ch;
485    
486            username[0] = '\0';
487    
488            clearscr();
489    
490            while (!SYS_server_exit)
491            {
492                    clrline(3, SCREEN_ROWS);
493                    get_data(2, 1, "查找谁: ", username, sizeof(username), BBS_username_max_len);
494    
495                    if (username[0] == '\0')
496                    {
497                            return 0;
498                    }
499    
500                    // Verify format
501                    for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
502                    {
503                            if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
504                            {
505                                    ok = 0;
506                            }
507                    }
508                    if (ok && i > BBS_username_max_len)
509                    {
510                            ok = 0;
511                    }
512                    if (!ok)
513                    {
514                            moveto(3, 1);
515                            clrtoeol();
516                            prints("用户名格式非法");
517                            continue;
518                    }
519    
520                    clrline(3, SCREEN_ROWS);
521    
522                    ret = query_user_info_by_username(username, max_user_cnt, uid_list, username_list);
523    
524                    if (ret < 0)
525                    {
526                            log_error("query_user_info_by_username(%s) error\n", username);
527                            return -1;
528                    }
529                    else if (ret > 1)
530                    {
531                            for (i = 0; i < MIN(ret, users_per_line * max_user_lines); i++)
532                            {
533                                    moveto(4 + i / users_per_line, 3 + i % users_per_line * (BBS_username_max_len + 3));
534                                    prints("%s", username_list[i]);
535                            }
536                            moveto(SCREEN_ROWS, 1);
537                            if (ret > users_per_line * max_user_lines)
538                            {
539                                    prints("还有更多...");
540                            }
541    
542                            moveto(3, 1);
543                            prints("存在多个匹配的用户,按\033[1;33mEnter\033[m精确查找");
544                            iflush();
545    
546                            ch = igetch_t(BBS_max_user_idle_time);
547                            switch (ch)
548                            {
549                            case KEY_NULL:
550                            case KEY_TIMEOUT:
551                                    return -1;
552                            case KEY_ESC:
553                                    return 0;
554                            case CR:
555                                    ret = (strcasecmp(username_list[0], username) == 0 ? 1 : 0);
556                                    break;
557                            default:
558                                    i = (int)strnlen(username, sizeof(username) - 1);
559                                    if (i + 1 <= BBS_username_max_len && (isalnum((char)ch) || ch == '_'))
560                                    {
561                                            username[i] = (char)ch;
562                                            username[i + 1] = '\0';
563                                    }
564                                    continue;
565                            }
566                    }
567    
568                    clrline(3, SCREEN_ROWS);
569                    if (ret == 0)
570                    {
571                            moveto(3, 1);
572                            prints("没有找到符合条件的用户");
573                            press_any_key();
574                            return 0;
575                    }
576                    else // ret == 1
577                    {
578                            if (query_user_info_by_uid(uid_list[0], &user_info, user_intro, sizeof(user_intro)) <= 0)
579                            {
580                                    log_error("query_user_info_by_uid(uid=%d) error\n", uid_list[0]);
581                                    return -2;
582                            }
583                            else if (user_info_display(&user_info) < 0)
584                            {
585                                    log_error("user_info_display(uid=%d) error\n", uid_list[0]);
586                                    return -3;
587                            }
588                            return 1;
589                    }
590            }
591    
592          return 0;          return 0;
593  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1