/[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.2 by sysadm, Tue Oct 21 12:35:03 2025 UTC Revision 1.29 by sysadm, Fri Dec 19 06:16:27 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 32  enum select_cmd_t Line 30  enum select_cmd_t
30          EXIT_LIST = 0,          EXIT_LIST = 0,
31          VIEW_USER,          VIEW_USER,
32          CHANGE_PAGE,          CHANGE_PAGE,
33            REFRESH_LIST,
34          SHOW_HELP,          SHOW_HELP,
35            SEARCH_USER,
36  };  };
37    
38  static int user_list_draw_screen()  static int user_list_draw_screen(int online_user)
39  {  {
40          clearscr();          clearscr();
41          show_top("[已注册用户]", 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          prints("\033[44;37m  \033[1;37m 编  号   用户名       昵称                 上次登陆距今                        \033[m");  
48            if (online_user)
49            {
50                    prints("\033[44;37m \033[1;37m 编  号  用户名       昵称                 在线时长 空闲   最后活动            \033[m");
51            }
52            else
53            {
54                    prints("\033[44;37m \033[1;37m 编  号  用户名       昵称                 上次登陆距今                        \033[m");
55            }
56    
57          return 0;          return 0;
58  }  }
59    
60  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)  
61  {  {
62          char str_time[LINE_BUFFER_LEN];          char str_time_login[LINE_BUFFER_LEN];
63          time_t tm_now;          time_t tm_now;
64          time_t tm_duration;          time_t tm_duration;
65          struct tm *p_tm;          struct tm *p_tm;
# Line 65  static int user_list_draw_items(int page Line 73  static int user_list_draw_items(int page
73          {          {
74                  tm_duration = tm_now - p_users[i].last_login_dt;                  tm_duration = tm_now - p_users[i].last_login_dt;
75                  p_tm = gmtime(&tm_duration);                  p_tm = gmtime(&tm_duration);
76                  if (p_tm->tm_year > 70)                  if (p_tm == NULL)
77                  {                  {
78                          snprintf(str_time, sizeof(str_time),                          log_error("Invalid time duration");
79                                           "%d年", p_tm->tm_year - 70);                          str_time_login[0] = '\0';
80                  }                  }
81                  else if (p_tm->tm_yday > 1)                  else if (p_tm->tm_year > 70)
82                  {                  {
83                          snprintf(str_time, sizeof(str_time),                          snprintf(str_time_login, sizeof(str_time_login),
84                                             "%d年%d天", p_tm->tm_year - 70, p_tm->tm_yday);
85                    }
86                    else if (p_tm->tm_yday > 0)
87                    {
88                            snprintf(str_time_login, sizeof(str_time_login),
89                                           "%d天", p_tm->tm_yday);                                           "%d天", p_tm->tm_yday);
90                  }                  }
91                  else if (p_tm->tm_hour > 0)                  else if (p_tm->tm_hour > 0)
92                  {                  {
93                          snprintf(str_time, sizeof(str_time),                          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, sizeof(str_time),                          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);
103    
104                  prints("  %7d   %s%*s %s%*s %s",                  prints("  %6d  %s%*s %s%*s %s",
105                             p_users[i].uid,                             p_users[i].id + 1,
106                             p_users[i].username,                             p_users[i].username,
107                             BBS_username_max_len - str_length(p_users[i].username, 1),                             BBS_username_max_len - str_length(p_users[i].username, 1),
108                             "",                             "",
109                             p_users[i].nickname,                             p_users[i].nickname,
110                             BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1),                             BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1),
111                             "",                             "",
112                             str_time);                             str_time_login);
113            }
114    
115            return 0;
116    }
117    
118    static int user_online_list_draw_items(int page_id, USER_ONLINE_INFO *p_users, int user_count)
119    {
120            char str_time_login[LINE_BUFFER_LEN];
121            char str_time_idle[LINE_BUFFER_LEN];
122            const char *p_action_title;
123            time_t tm_now;
124            time_t tm_duration;
125            struct tm *p_tm;
126            int i;
127    
128            clrline(4, 23);
129    
130            time(&tm_now);
131    
132            for (i = 0; i < user_count; i++)
133            {
134                    tm_duration = tm_now - p_users[i].login_tm;
135                    p_tm = gmtime(&tm_duration);
136                    if (p_tm == NULL)
137                    {
138                            log_error("Invalid time duration");
139                            str_time_login[0] = '\0';
140                    }
141                    else if (p_tm->tm_yday > 0)
142                    {
143                            snprintf(str_time_login, sizeof(str_time_login),
144                                             "%dd%dh", p_tm->tm_yday, p_tm->tm_hour);
145                    }
146                    else if (p_tm->tm_hour > 0)
147                    {
148                            snprintf(str_time_login, sizeof(str_time_login),
149                                             "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
150                    }
151                    else
152                    {
153                            snprintf(str_time_login, sizeof(str_time_login),
154                                             "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
155                    }
156    
157                    tm_duration = tm_now - p_users[i].last_tm;
158                    p_tm = gmtime(&tm_duration);
159                    if (p_tm == NULL)
160                    {
161                            log_error("Invalid time duration");
162                            str_time_idle[0] = '\0';
163                    }
164                    else if (p_tm->tm_min > 0)
165                    {
166                            snprintf(str_time_idle, sizeof(str_time_idle),
167                                             "%d\'%d\"", p_tm->tm_min, p_tm->tm_sec);
168                    }
169                    else
170                    {
171                            snprintf(str_time_idle, sizeof(str_time_idle),
172                                             "%d\"", p_tm->tm_sec);
173                    }
174    
175                    p_action_title = (p_users[i].current_action_title != NULL
176                                                              ? p_users[i].current_action_title
177                                                              : p_users[i].current_action);
178    
179                    moveto(4 + i, 1);
180    
181                    prints("  %6d  %s%*s %s%*s %s%*s %s%*s %s",
182                               p_users[i].id + 1,
183                               p_users[i].user_info.username,
184                               BBS_username_max_len - str_length(p_users[i].user_info.username, 1),
185                               "",
186                               p_users[i].user_info.nickname,
187                               BBS_nickname_max_len / 2 - str_length(p_users[i].user_info.nickname, 1),
188                               "",
189                               str_time_login,
190                               8 - str_length(str_time_login, 1),
191                               "",
192                               str_time_idle,
193                               6 - str_length(str_time_idle, 1),
194                               "",
195                               p_action_title);
196          }          }
197    
198          return 0;          return 0;
# Line 122  static enum select_cmd_t user_list_selec Line 218  static enum select_cmd_t user_list_selec
218                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
219                  {                  {
220                          BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
221    
222                            // Refresh current action
223                            if (user_online_update(NULL) < 0)
224                            {
225                                    log_error("user_online_update(NULL) error");
226                            }
227                  }                  }
228    
229                  switch (ch)                  switch (ch)
230                  {                  {
231                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
232                          log_error("KEY_NULL\n");                          log_debug("KEY_NULL");
233                  case KEY_ESC:                  case KEY_ESC:
234                  case KEY_LEFT:                  case KEY_LEFT:
235                          return EXIT_LIST; // exit list                          return EXIT_LIST; // exit list
236                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
237                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
238                          {                          {
239                                  log_error("User input timeout\n");                                  log_debug("User input timeout");
240                                  return EXIT_LIST; // exit list                                  return EXIT_LIST; // exit list
241                          }                          }
242                          continue;                          continue;
# Line 208  static enum select_cmd_t user_list_selec Line 310  static enum select_cmd_t user_list_selec
310                                  (*p_selected_index)++;                                  (*p_selected_index)++;
311                          }                          }
312                          break;                          break;
313                    case 's':
314                            return SEARCH_USER;
315                    case KEY_F5:
316                            return REFRESH_LIST;
317                  case 'h':                  case 'h':
318                          return SHOW_HELP;                          return SHOW_HELP;
319                  default:                  default:
# Line 240  static enum select_cmd_t user_list_selec Line 346  static enum select_cmd_t user_list_selec
346          return EXIT_LIST;          return EXIT_LIST;
347  }  }
348    
349  int user_list_display(void)  int user_list_display(int online_user)
350  {  {
351          char page_info_str[LINE_BUFFER_LEN];          char page_info_str[LINE_BUFFER_LEN];
352          USER_INFO users[BBS_user_limit_per_page];          USER_INFO users[BBS_user_limit_per_page];
353          USER_INFO user_info;          USER_ONLINE_INFO online_users[BBS_user_limit_per_page];
354          int user_count;          int user_count = 0;
355          int page_count;          int page_count = 0;
356          int page_id = 0;          int page_id = 0;
357          int selected_index = 0;          int selected_index = 0;
358          int ret;          int ret = 0;
359    
360          user_list_draw_screen();          user_list_draw_screen(online_user);
361    
362          ret = query_user_list(page_id, users, &user_count, &page_count);          if (online_user)
         if (ret < 0)  
363          {          {
364                  log_error("query_user_list(page_id=%d) error\n", page_id);                  ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
365                  return -2;                  if (ret < 0)
366                    {
367                            log_error("query_user_online_list(page_id=%d) error", page_id);
368                            return -2;
369                    }
370            }
371            else
372            {
373                    ret = query_user_list(page_id, users, &user_count, &page_count);
374                    if (ret < 0)
375                    {
376                            log_error("query_user_list(page_id=%d) error", page_id);
377                            return -2;
378                    }
379          }          }
380    
381          if (user_count == 0) // empty list          if (user_count == 0) // empty list
# Line 267  int user_list_display(void) Line 385  int user_list_display(void)
385    
386          while (!SYS_server_exit)          while (!SYS_server_exit)
387          {          {
388                  ret = user_list_draw_items(page_id, users, user_count);                  if (online_user)
                 if (ret < 0)  
389                  {                  {
390                          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);
391                          return -3;                          if (ret < 0)
392                            {
393                                    log_error("user_online_list_draw_items(page_id=%d) error", page_id);
394                                    return -3;
395                            }
396                    }
397                    else
398                    {
399                            ret = user_list_draw_items(page_id, users, user_count);
400                            if (ret < 0)
401                            {
402                                    log_error("user_list_draw_items(page_id=%d) error", page_id);
403                                    return -3;
404                            }
405                  }                  }
406    
407                  snprintf(page_info_str, sizeof(page_info_str),                  snprintf(page_info_str, sizeof(page_info_str),
# Line 281  int user_list_display(void) Line 411  int user_list_display(void)
411                  show_bottom(page_info_str);                  show_bottom(page_info_str);
412                  iflush();                  iflush();
413    
414                  if (user_online_update("USER_LIST") < 0)                  if (user_online_update(online_user ? "USER_ONLINE" : "USER_LIST") < 0)
415                  {                  {
416                          log_error("user_online_update(USER_LIST) error\n");                          log_error("user_online_update(%s) error",
417                                              (online_user ? "USER_ONLINE" : "USER_LIST"));
418                  }                  }
419    
420                  ret = user_list_select(page_count, user_count, &page_id, &selected_index);                  ret = user_list_select(page_count, user_count, &page_id, &selected_index);
# Line 291  int user_list_display(void) Line 422  int user_list_display(void)
422                  {                  {
423                  case EXIT_LIST:                  case EXIT_LIST:
424                          return 0;                          return 0;
425                    case REFRESH_LIST:
426                  case CHANGE_PAGE:                  case CHANGE_PAGE:
427                          ret = query_user_list(page_id, users, &user_count, &page_count);                          if (online_user)
                         if (ret < 0)  
428                          {                          {
429                                  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);
430                                  return -2;                                  if (ret < 0)
431                                    {
432                                            log_error("query_user_online_list(page_id=%d) error", page_id);
433                                            return -2;
434                                    }
435                            }
436                            else
437                            {
438                                    ret = query_user_list(page_id, users, &user_count, &page_count);
439                                    if (ret < 0)
440                                    {
441                                            log_error("query_user_list(page_id=%d) error", page_id);
442                                            return -2;
443                                    }
444                          }                          }
445    
446                          if (user_count == 0) // empty list                          if (user_count == 0) // empty list
# Line 309  int user_list_display(void) Line 453  int user_list_display(void)
453                          }                          }
454                          break;                          break;
455                  case VIEW_USER:                  case VIEW_USER:
456                          if ((ret = query_user_info(users[selected_index].uid, &user_info)) < 0)                          user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index]));
457                            user_list_draw_screen(online_user);
458                            break;
459                    case SEARCH_USER:
460                            user_list_search();
461                            user_list_draw_screen(online_user);
462                            break;
463                    case SHOW_HELP:
464                            // Display help information
465                            display_file(DATA_READ_HELP, 1);
466                            user_list_draw_screen(online_user);
467                            break;
468                    default:
469                            log_error("Unknown command %d", ret);
470                    }
471            }
472    
473            return 0;
474    }
475    
476    int user_list_search(void)
477    {
478            const int users_per_line = 5;
479            const int max_user_lines = 20;
480            const int max_user_cnt = users_per_line * max_user_lines + 1;
481    
482            char username[BBS_username_max_len + 1];
483            int32_t uid_list[max_user_cnt];
484            char username_list[max_user_cnt][BBS_username_max_len + 1];
485            int ret;
486            int i;
487            USER_INFO user_info;
488            char user_intro[BBS_user_intro_max_len + 1];
489            int ok;
490            int ch;
491    
492            username[0] = '\0';
493    
494            clearscr();
495    
496            while (!SYS_server_exit)
497            {
498                    clrline(3, SCREEN_ROWS);
499                    get_data(2, 1, "查找谁: ", username, sizeof(username), BBS_username_max_len);
500    
501                    if (username[0] == '\0')
502                    {
503                            return 0;
504                    }
505    
506                    // Verify format
507                    for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
508                    {
509                            if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
510                          {                          {
511                                  log_error("query_user_info(uid=%d) error: %d\n", users[selected_index].uid, ret);                                  ok = 0;
512                          }                          }
513                          else if (ret == 0)                  }
514                    if (ok && i > BBS_username_max_len)
515                    {
516                            ok = 0;
517                    }
518                    if (!ok)
519                    {
520                            moveto(3, 1);
521                            clrtoeol();
522                            prints("用户名格式非法");
523                            continue;
524                    }
525    
526                    clrline(3, SCREEN_ROWS);
527    
528                    ret = query_user_info_by_username(username, max_user_cnt, uid_list, username_list);
529    
530                    if (ret < 0)
531                    {
532                            log_error("query_user_info_by_username(%s) error", username);
533                            return -1;
534                    }
535                    else if (ret > 1)
536                    {
537                            for (i = 0; i < MIN(ret, users_per_line * max_user_lines); i++)
538                          {                          {
539                                  log_error("query_user_info(uid=%d) error: user not found\n", users[selected_index].uid);                                  moveto(4 + i / users_per_line, 3 + i % users_per_line * (BBS_username_max_len + 3));
540                                    prints("%s", username_list[i]);
541                          }                          }
542                          else if (users[selected_index].uid != user_info.uid)                          moveto(SCREEN_ROWS, 1);
543                            if (ret > users_per_line * max_user_lines)
544                          {                          {
545                                  log_error("query_user_info(uid=%d) error: inconsistent uid=%d\n", users[selected_index].uid, user_info.uid);                                  prints("还有更多...");
546                          }                          }
547                          else  
548                            moveto(3, 1);
549                            prints("存在多个匹配的用户,按\033[1;33mEnter\033[m精确查找");
550                            iflush();
551    
552                            ch = igetch_t(BBS_max_user_idle_time);
553                            switch (ch)
554                          {                          {
555                                  clearscr();                          case KEY_NULL:
556                                  press_any_key_ex("功能不可用,按任意键返回", 60);                          case KEY_TIMEOUT:
557                                  user_list_draw_screen();                                  return -1;
558                            case KEY_ESC:
559                                    return 0;
560                            case CR:
561                                    ret = (strcasecmp(username_list[0], username) == 0 ? 1 : 0);
562                                    break;
563                            default:
564                                    i = (int)strnlen(username, sizeof(username) - 1);
565                                    if (i + 1 <= BBS_username_max_len && (isalnum(ch) || ch == '_'))
566                                    {
567                                            username[i] = (char)ch;
568                                            username[i + 1] = '\0';
569                                    }
570                                    continue;
571                          }                          }
572                          break;                  }
573                  case SHOW_HELP:  
574                          // Display help information                  clrline(3, SCREEN_ROWS);
575                          display_file(DATA_READ_HELP, 1);                  if (ret == 0)
576                          user_list_draw_screen();                  {
577                          break;                          moveto(3, 1);
578                  default:                          prints("没有找到符合条件的用户");
579                          log_error("Unknown command %d\n", ret);                          press_any_key();
580                            return 0;
581                    }
582                    else // ret == 1
583                    {
584                            if (query_user_info_by_uid(uid_list[0], &user_info, user_intro, sizeof(user_intro)) <= 0)
585                            {
586                                    log_error("query_user_info_by_uid(uid=%d) error", uid_list[0]);
587                                    return -2;
588                            }
589                            else if (user_info_display(&user_info) < 0)
590                            {
591                                    log_error("user_info_display(uid=%d) error", uid_list[0]);
592                                    return -3;
593                            }
594                            return 1;
595                  }                  }
596          }          }
597    


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

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