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

Annotation of /lbbs/src/user_list_display.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Oct 21 11:11:28 2025 UTC (4 months, 3 weeks ago) by sysadm
Branch: MAIN
Content type: text/x-csrc
Add user_list_display

1 sysadm 1.1 /***************************************************************************
2     user_list_display.c - description
3     -------------------
4     Copyright : (C) 2004-2025 by Leaflet
5     Email : leaflet@leafok.com
6     ***************************************************************************/
7    
8     /***************************************************************************
9     * *
10     * This program is free software; you can redistribute it and/or modify *
11     * it under the terms of the GNU General Public License as published by *
12     * the Free Software Foundation; either version 3 of the License, or *
13     * (at your option) any later version. *
14     * *
15     ***************************************************************************/
16    
17     #include "common.h"
18     #include "io.h"
19     #include "log.h"
20     #include "login.h"
21     #include "screen.h"
22     #include "str_process.h"
23     #include "user_list.h"
24     #include "user_priv.h"
25     #include "user_list_display.h"
26     #include <string.h>
27     #include <time.h>
28     #include <sys/param.h>
29    
30     enum select_cmd_t
31     {
32     EXIT_LIST = 0,
33     VIEW_USER,
34     CHANGE_PAGE,
35     SHOW_HELP,
36     };
37    
38     static int user_list_draw_screen()
39     {
40     clearscr();
41     show_top("[已注册用户]", BBS_name, "");
42     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] "
44     "查看[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m");
45     moveto(3, 0);
46     prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 上次登陆距今 \033[m");
47    
48     return 0;
49     }
50    
51     static int user_list_draw_items(int page_id, USER_INFO *p_users,
52     int user_count)
53     {
54     char str_time[LINE_BUFFER_LEN];
55     time_t tm_now;
56     time_t tm_duration;
57     struct tm *p_tm;
58     int i;
59    
60     clrline(4, 23);
61    
62     time(&tm_now);
63    
64     for (i = 0; i < user_count; i++)
65     {
66     tm_duration = tm_now - p_users[i].last_login_dt;
67     p_tm = gmtime(&tm_duration);
68     if (p_tm->tm_year > 70)
69     {
70     snprintf(str_time, sizeof(str_time),
71     "%d年", p_tm->tm_year - 70);
72     }
73     else if (p_tm->tm_yday > 1)
74     {
75     snprintf(str_time, sizeof(str_time),
76     "%d天", p_tm->tm_yday);
77     }
78     else if (p_tm->tm_hour > 0)
79     {
80     snprintf(str_time, sizeof(str_time),
81     "%d时%d分", p_tm->tm_hour, p_tm->tm_min);
82     }
83     else
84     {
85     snprintf(str_time, sizeof(str_time),
86     "%d分%d秒", p_tm->tm_min, p_tm->tm_sec);
87     }
88    
89     moveto(4 + i, 1);
90    
91     prints(" %7d %s%*s %s%*s %s",
92     p_users[i].uid,
93     p_users[i].username,
94     BBS_username_max_len - str_length(p_users[i].username, 1),
95     "",
96     p_users[i].nickname,
97     BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1),
98     "",
99     str_time);
100     }
101    
102     return 0;
103     }
104    
105     static enum select_cmd_t user_list_select(int total_page, int item_count, int *p_page_id, int *p_selected_index)
106     {
107     int old_page_id = *p_page_id;
108     int old_selected_index = *p_selected_index;
109     int ch;
110    
111     if (item_count > 0 && *p_selected_index >= 0)
112     {
113     moveto(4 + *p_selected_index, 1);
114     outc('>');
115     iflush();
116     }
117    
118     while (!SYS_server_exit)
119     {
120     ch = igetch(100);
121    
122     if (ch != KEY_NULL && ch != KEY_TIMEOUT)
123     {
124     BBS_last_access_tm = time(NULL);
125     }
126    
127     switch (ch)
128     {
129     case KEY_NULL: // broken pipe
130     log_error("KEY_NULL\n");
131     case KEY_ESC:
132     case KEY_LEFT:
133     return EXIT_LIST; // exit list
134     case KEY_TIMEOUT:
135     if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
136     {
137     log_error("User input timeout\n");
138     return EXIT_LIST; // exit list
139     }
140     continue;
141     case CR:
142     case KEY_RIGHT:
143     if (item_count > 0)
144     {
145     return VIEW_USER;
146     }
147     break;
148     case KEY_HOME:
149     *p_page_id = 0;
150     case 'P':
151     case KEY_PGUP:
152     *p_selected_index = 0;
153     case 'k':
154     case KEY_UP:
155     if (*p_selected_index <= 0)
156     {
157     if (*p_page_id > 0)
158     {
159     (*p_page_id)--;
160     *p_selected_index = BBS_user_limit_per_page - 1;
161     }
162     else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of list
163     {
164     if (total_page > 0)
165     {
166     *p_page_id = total_page - 1;
167     }
168     if (item_count > 0)
169     {
170     *p_selected_index = item_count - 1;
171     }
172     }
173     }
174     else
175     {
176     (*p_selected_index)--;
177     }
178     break;
179     case '$':
180     case KEY_END:
181     if (total_page > 0)
182     {
183     *p_page_id = total_page - 1;
184     }
185     case 'N':
186     case KEY_PGDN:
187     if (item_count > 0)
188     {
189     *p_selected_index = item_count - 1;
190     }
191     case 'j':
192     case KEY_DOWN:
193     if (*p_selected_index + 1 >= item_count) // next page
194     {
195     if (*p_page_id + 1 < total_page)
196     {
197     (*p_page_id)++;
198     *p_selected_index = 0;
199     }
200     else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of list
201     {
202     *p_page_id = 0;
203     *p_selected_index = 0;
204     }
205     }
206     else
207     {
208     (*p_selected_index)++;
209     }
210     break;
211     case 'h':
212     return SHOW_HELP;
213     default:
214     break;
215     }
216    
217     if (old_page_id != *p_page_id)
218     {
219     return CHANGE_PAGE;
220     }
221    
222     if (item_count > 0 && old_selected_index != *p_selected_index)
223     {
224     if (old_selected_index >= 0)
225     {
226     moveto(4 + old_selected_index, 1);
227     outc(' ');
228     }
229     if (*p_selected_index >= 0)
230     {
231     moveto(4 + *p_selected_index, 1);
232     outc('>');
233     }
234     iflush();
235    
236     old_selected_index = *p_selected_index;
237     }
238     }
239    
240     return EXIT_LIST;
241     }
242    
243     int user_list_display(void)
244     {
245     char page_info_str[LINE_BUFFER_LEN];
246     USER_INFO users[BBS_user_limit_per_page];
247     int user_count;
248     int page_count;
249     int page_id = 0;
250     int selected_index = 0;
251     int ret;
252    
253     user_list_draw_screen();
254    
255     ret = query_user_list(page_id, users, &user_count, &page_count);
256     if (ret < 0)
257     {
258     log_error("query_user_list(page_id=%d) error\n", page_id);
259     return -2;
260     }
261    
262     if (user_count == 0) // empty list
263     {
264     selected_index = 0;
265     }
266    
267     while (!SYS_server_exit)
268     {
269     ret = user_list_draw_items(page_id, users, user_count);
270     if (ret < 0)
271     {
272     log_error("user_list_draw_items(page_id=%d) error\n", page_id);
273     return -3;
274     }
275    
276     snprintf(page_info_str, sizeof(page_info_str),
277     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
278     page_id + 1, MAX(page_count, 1));
279    
280     show_bottom(page_info_str);
281     iflush();
282    
283     if (user_online_update("USER_LIST") < 0)
284     {
285     log_error("user_online_update(USER_LIST) error\n");
286     }
287    
288     ret = user_list_select(page_count, user_count, &page_id, &selected_index);
289     switch (ret)
290     {
291     case EXIT_LIST:
292     return 0;
293     case CHANGE_PAGE:
294     ret = query_user_list(page_id, users, &user_count, &page_count);
295     if (ret < 0)
296     {
297     log_error("query_favor_articles(page_id=%d) error\n", page_id);
298     return -2;
299     }
300    
301     if (user_count == 0) // empty list
302     {
303     selected_index = 0;
304     }
305     else if (selected_index >= user_count)
306     {
307     selected_index = user_count - 1;
308     }
309     break;
310     case VIEW_USER:
311     clearscr();
312     press_any_key_ex("功能不可用,按任意键返回", 60);
313     user_list_draw_screen();
314     break;
315     case SHOW_HELP:
316     // Display help information
317     display_file(DATA_READ_HELP, 1);
318     user_list_draw_screen();
319     break;
320     default:
321     log_error("Unknown command %d\n", ret);
322     }
323     }
324    
325     return 0;
326     }

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