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

Contents of /lbbs/src/user_list_display.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Tue Nov 4 13:49:51 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.19: +7 -15 lines
Content type: text/x-csrc
Update file header information comments

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * user_list_display
4 * - user interactive list of (online) users
5 *
6 * Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com>
7 */
8
9 #include "common.h"
10 #include "io.h"
11 #include "log.h"
12 #include "login.h"
13 #include "screen.h"
14 #include "str_process.h"
15 #include "user_list.h"
16 #include "user_priv.h"
17 #include "user_info_display.h"
18 #include "user_list_display.h"
19 #include <ctype.h>
20 #include <string.h>
21 #include <time.h>
22 #include <sys/param.h>
23
24 enum select_cmd_t
25 {
26 EXIT_LIST = 0,
27 VIEW_USER,
28 CHANGE_PAGE,
29 REFRESH_LIST,
30 SHOW_HELP,
31 SEARCH_USER,
32 };
33
34 static int user_list_draw_screen(int online_user)
35 {
36 clearscr();
37 show_top((online_user ? "[线上使用者]" : "[已注册用户]"), BBS_name, "");
38 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] "
40 "查看[\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);
43
44 if (online_user)
45 {
46 prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 在线时长 空闲 最后活动 \033[m");
47 }
48 else
49 {
50 prints("\033[44;37m \033[1;37m 编 号 用户名 昵称 上次登陆距今 \033[m");
51 }
52
53 return 0;
54 }
55
56 static int user_list_draw_items(int page_id, USER_INFO *p_users, int user_count)
57 {
58 char str_time_login[LINE_BUFFER_LEN];
59 time_t tm_now;
60 time_t tm_duration;
61 struct tm *p_tm;
62 int i;
63
64 clrline(4, 23);
65
66 time(&tm_now);
67
68 for (i = 0; i < user_count; i++)
69 {
70 tm_duration = tm_now - p_users[i].last_login_dt;
71 p_tm = gmtime(&tm_duration);
72 if (p_tm == NULL)
73 {
74 log_error("Invalid time duration\n");
75 str_time_login[0] = '\0';
76 }
77 else if (p_tm->tm_year > 70)
78 {
79 snprintf(str_time_login, sizeof(str_time_login),
80 "%d年%d天", p_tm->tm_year - 70, p_tm->tm_yday);
81 }
82 else if (p_tm->tm_yday > 0)
83 {
84 snprintf(str_time_login, sizeof(str_time_login),
85 "%d天", p_tm->tm_yday);
86 }
87 else if (p_tm->tm_hour > 0)
88 {
89 snprintf(str_time_login, sizeof(str_time_login),
90 "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
91 }
92 else
93 {
94 snprintf(str_time_login, sizeof(str_time_login),
95 "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
96 }
97
98 moveto(4 + i, 1);
99
100 prints(" %6d %s%*s %s%*s %s",
101 p_users[i].id + 1,
102 p_users[i].username,
103 BBS_username_max_len - str_length(p_users[i].username, 1),
104 "",
105 p_users[i].nickname,
106 BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1),
107 "",
108 str_time_login);
109 }
110
111 return 0;
112 }
113
114 static int user_online_list_draw_items(int page_id, USER_ONLINE_INFO *p_users, int user_count)
115 {
116 char str_time_login[LINE_BUFFER_LEN];
117 char str_time_idle[LINE_BUFFER_LEN];
118 const char *p_action_title;
119 time_t tm_now;
120 time_t tm_duration;
121 struct tm *p_tm;
122 int i;
123
124 clrline(4, 23);
125
126 time(&tm_now);
127
128 for (i = 0; i < user_count; i++)
129 {
130 tm_duration = tm_now - p_users[i].login_tm;
131 p_tm = gmtime(&tm_duration);
132 if (p_tm == NULL)
133 {
134 log_error("Invalid time duration\n");
135 str_time_login[0] = '\0';
136 }
137 else if (p_tm->tm_yday > 0)
138 {
139 snprintf(str_time_login, sizeof(str_time_login),
140 "%dd%dh", p_tm->tm_yday, p_tm->tm_hour);
141 }
142 else if (p_tm->tm_hour > 0)
143 {
144 snprintf(str_time_login, sizeof(str_time_login),
145 "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
146 }
147 else
148 {
149 snprintf(str_time_login, sizeof(str_time_login),
150 "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
151 }
152
153 tm_duration = tm_now - p_users[i].last_tm;
154 p_tm = gmtime(&tm_duration);
155 if (p_tm == NULL)
156 {
157 log_error("Invalid time duration\n");
158 str_time_idle[0] = '\0';
159 }
160 else if (p_tm->tm_min > 0)
161 {
162 snprintf(str_time_idle, sizeof(str_time_idle),
163 "%d\'%d\"", p_tm->tm_min, p_tm->tm_sec);
164 }
165 else
166 {
167 snprintf(str_time_idle, sizeof(str_time_idle),
168 "%d\"", p_tm->tm_sec);
169 }
170
171 p_action_title = (p_users[i].current_action_title != NULL
172 ? p_users[i].current_action_title
173 : p_users[i].current_action);
174
175 moveto(4 + i, 1);
176
177 prints(" %6d %s%*s %s%*s %s%*s %s%*s %s",
178 p_users[i].id + 1,
179 p_users[i].user_info.username,
180 BBS_username_max_len - str_length(p_users[i].user_info.username, 1),
181 "",
182 p_users[i].user_info.nickname,
183 BBS_nickname_max_len / 2 - str_length(p_users[i].user_info.nickname, 1),
184 "",
185 str_time_login,
186 8 - str_length(str_time_login, 1),
187 "",
188 str_time_idle,
189 6 - str_length(str_time_idle, 1),
190 "",
191 p_action_title);
192 }
193
194 return 0;
195 }
196
197 static enum select_cmd_t user_list_select(int total_page, int item_count, int *p_page_id, int *p_selected_index)
198 {
199 int old_page_id = *p_page_id;
200 int old_selected_index = *p_selected_index;
201 int ch;
202
203 if (item_count > 0 && *p_selected_index >= 0)
204 {
205 moveto(4 + *p_selected_index, 1);
206 outc('>');
207 iflush();
208 }
209
210 while (!SYS_server_exit)
211 {
212 ch = igetch(100);
213
214 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
215 {
216 BBS_last_access_tm = time(NULL);
217 }
218
219 switch (ch)
220 {
221 case KEY_NULL: // broken pipe
222 log_error("KEY_NULL\n");
223 case KEY_ESC:
224 case KEY_LEFT:
225 return EXIT_LIST; // exit list
226 case KEY_TIMEOUT:
227 if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
228 {
229 log_error("User input timeout\n");
230 return EXIT_LIST; // exit list
231 }
232 continue;
233 case CR:
234 case KEY_RIGHT:
235 if (item_count > 0)
236 {
237 return VIEW_USER;
238 }
239 break;
240 case KEY_HOME:
241 *p_page_id = 0;
242 case 'P':
243 case KEY_PGUP:
244 *p_selected_index = 0;
245 case 'k':
246 case KEY_UP:
247 if (*p_selected_index <= 0)
248 {
249 if (*p_page_id > 0)
250 {
251 (*p_page_id)--;
252 *p_selected_index = BBS_user_limit_per_page - 1;
253 }
254 else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of list
255 {
256 if (total_page > 0)
257 {
258 *p_page_id = total_page - 1;
259 }
260 if (item_count > 0)
261 {
262 *p_selected_index = item_count - 1;
263 }
264 }
265 }
266 else
267 {
268 (*p_selected_index)--;
269 }
270 break;
271 case '$':
272 case KEY_END:
273 if (total_page > 0)
274 {
275 *p_page_id = total_page - 1;
276 }
277 case 'N':
278 case KEY_PGDN:
279 if (item_count > 0)
280 {
281 *p_selected_index = item_count - 1;
282 }
283 case 'j':
284 case KEY_DOWN:
285 if (*p_selected_index + 1 >= item_count) // next page
286 {
287 if (*p_page_id + 1 < total_page)
288 {
289 (*p_page_id)++;
290 *p_selected_index = 0;
291 }
292 else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of list
293 {
294 *p_page_id = 0;
295 *p_selected_index = 0;
296 }
297 }
298 else
299 {
300 (*p_selected_index)++;
301 }
302 break;
303 case 's':
304 return SEARCH_USER;
305 case KEY_F5:
306 return REFRESH_LIST;
307 case 'h':
308 return SHOW_HELP;
309 default:
310 break;
311 }
312
313 if (old_page_id != *p_page_id)
314 {
315 return CHANGE_PAGE;
316 }
317
318 if (item_count > 0 && old_selected_index != *p_selected_index)
319 {
320 if (old_selected_index >= 0)
321 {
322 moveto(4 + old_selected_index, 1);
323 outc(' ');
324 }
325 if (*p_selected_index >= 0)
326 {
327 moveto(4 + *p_selected_index, 1);
328 outc('>');
329 }
330 iflush();
331
332 old_selected_index = *p_selected_index;
333 }
334 }
335
336 return EXIT_LIST;
337 }
338
339 int user_list_display(int online_user)
340 {
341 char page_info_str[LINE_BUFFER_LEN];
342 USER_INFO users[BBS_user_limit_per_page];
343 USER_ONLINE_INFO online_users[BBS_user_limit_per_page];
344 int user_count = 0;
345 int page_count = 0;
346 int page_id = 0;
347 int selected_index = 0;
348 int ret = 0;
349
350 user_list_draw_screen(online_user);
351
352 if (online_user)
353 {
354 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
355 if (ret < 0)
356 {
357 log_error("query_user_online_list(page_id=%d) error\n", page_id);
358 return -2;
359 }
360 }
361 else
362 {
363 ret = query_user_list(page_id, users, &user_count, &page_count);
364 if (ret < 0)
365 {
366 log_error("query_user_list(page_id=%d) error\n", page_id);
367 return -2;
368 }
369 }
370
371 if (user_count == 0) // empty list
372 {
373 selected_index = 0;
374 }
375
376 while (!SYS_server_exit)
377 {
378 if (online_user)
379 {
380 ret = user_online_list_draw_items(page_id, online_users, user_count);
381 if (ret < 0)
382 {
383 log_error("user_online_list_draw_items(page_id=%d) error\n", page_id);
384 return -3;
385 }
386 }
387 else
388 {
389 ret = user_list_draw_items(page_id, users, user_count);
390 if (ret < 0)
391 {
392 log_error("user_list_draw_items(page_id=%d) error\n", page_id);
393 return -3;
394 }
395 }
396
397 snprintf(page_info_str, sizeof(page_info_str),
398 "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
399 page_id + 1, MAX(page_count, 1));
400
401 show_bottom(page_info_str);
402 iflush();
403
404 if (user_online_update(online_user ? "USER_ONLINE" : "USER_LIST") < 0)
405 {
406 log_error("user_online_update(%s) error\n",
407 (online_user ? "USER_ONLINE" : "USER_LIST"));
408 }
409
410 ret = user_list_select(page_count, user_count, &page_id, &selected_index);
411 switch (ret)
412 {
413 case EXIT_LIST:
414 return 0;
415 case REFRESH_LIST:
416 case CHANGE_PAGE:
417 if (online_user)
418 {
419 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
420 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
437 {
438 selected_index = 0;
439 }
440 else if (selected_index >= user_count)
441 {
442 selected_index = user_count - 1;
443 }
444 break;
445 case VIEW_USER:
446 user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index]));
447 user_list_draw_screen(online_user);
448 break;
449 case SEARCH_USER:
450 user_list_search();
451 user_list_draw_screen(online_user);
452 break;
453 case SHOW_HELP:
454 // Display help information
455 display_file(DATA_READ_HELP, 1);
456 user_list_draw_screen(online_user);
457 break;
458 default:
459 log_error("Unknown command %d\n", ret);
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(MAX_DELAY_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;
589 }

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