/[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.26 - (show annotations)
Wed Dec 17 03:47:01 2025 UTC (2 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.25: +2 -0 lines
Content type: text/x-csrc
Refine debug log

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 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "common.h"
14 #include "io.h"
15 #include "log.h"
16 #include "login.h"
17 #include "screen.h"
18 #include "str_process.h"
19 #include "user_list.h"
20 #include "user_priv.h"
21 #include "user_info_display.h"
22 #include "user_list_display.h"
23 #include <ctype.h>
24 #include <string.h>
25 #include <time.h>
26 #include <sys/param.h>
27
28 enum select_cmd_t
29 {
30 EXIT_LIST = 0,
31 VIEW_USER,
32 CHANGE_PAGE,
33 REFRESH_LIST,
34 SHOW_HELP,
35 SEARCH_USER,
36 };
37
38 static int user_list_draw_screen(int online_user)
39 {
40 clearscr();
41 show_top((online_user ? "[线上使用者]" : "[已注册用户]"), 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;32ms\033[0;37m] "
45 "帮助[\033[1;32mh\033[0;37m]\033[m");
46 moveto(3, 0);
47
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;
58 }
59
60 static int user_list_draw_items(int page_id, USER_INFO *p_users, int user_count)
61 {
62 char str_time_login[LINE_BUFFER_LEN];
63 time_t tm_now;
64 time_t tm_duration;
65 struct tm *p_tm;
66 int i;
67
68 clrline(4, 23);
69
70 time(&tm_now);
71
72 for (i = 0; i < user_count; i++)
73 {
74 tm_duration = tm_now - p_users[i].last_login_dt;
75 p_tm = gmtime(&tm_duration);
76 if (p_tm == NULL)
77 {
78 log_error("Invalid time duration\n");
79 str_time_login[0] = '\0';
80 }
81 else if (p_tm->tm_year > 70)
82 {
83 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);
90 }
91 else if (p_tm->tm_hour > 0)
92 {
93 snprintf(str_time_login, sizeof(str_time_login),
94 "%d:%.2d", p_tm->tm_hour, p_tm->tm_min);
95 }
96 else
97 {
98 snprintf(str_time_login, sizeof(str_time_login),
99 "%d\'%.2d\"", p_tm->tm_min, p_tm->tm_sec);
100 }
101
102 moveto(4 + i, 1);
103
104 prints(" %6d %s%*s %s%*s %s",
105 p_users[i].id + 1,
106 p_users[i].username,
107 BBS_username_max_len - str_length(p_users[i].username, 1),
108 "",
109 p_users[i].nickname,
110 BBS_nickname_max_len / 2 - str_length(p_users[i].nickname, 1),
111 "",
112 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\n");
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\n");
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;
199 }
200
201 static enum select_cmd_t user_list_select(int total_page, int item_count, int *p_page_id, int *p_selected_index)
202 {
203 int old_page_id = *p_page_id;
204 int old_selected_index = *p_selected_index;
205 int ch;
206
207 if (item_count > 0 && *p_selected_index >= 0)
208 {
209 moveto(4 + *p_selected_index, 1);
210 outc('>');
211 iflush();
212 }
213
214 while (!SYS_server_exit)
215 {
216 ch = igetch(100);
217
218 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
219 {
220 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\n");
226 }
227 }
228
229 switch (ch)
230 {
231 case KEY_NULL: // broken pipe
232 #ifdef _DEBUG
233 log_error("KEY_NULL\n");
234 #endif
235 case KEY_ESC:
236 case KEY_LEFT:
237 return EXIT_LIST; // exit list
238 case KEY_TIMEOUT:
239 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
240 {
241 log_error("User input timeout\n");
242 return EXIT_LIST; // exit list
243 }
244 continue;
245 case CR:
246 case KEY_RIGHT:
247 if (item_count > 0)
248 {
249 return VIEW_USER;
250 }
251 break;
252 case KEY_HOME:
253 *p_page_id = 0;
254 case 'P':
255 case KEY_PGUP:
256 *p_selected_index = 0;
257 case 'k':
258 case KEY_UP:
259 if (*p_selected_index <= 0)
260 {
261 if (*p_page_id > 0)
262 {
263 (*p_page_id)--;
264 *p_selected_index = BBS_user_limit_per_page - 1;
265 }
266 else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of list
267 {
268 if (total_page > 0)
269 {
270 *p_page_id = total_page - 1;
271 }
272 if (item_count > 0)
273 {
274 *p_selected_index = item_count - 1;
275 }
276 }
277 }
278 else
279 {
280 (*p_selected_index)--;
281 }
282 break;
283 case '$':
284 case KEY_END:
285 if (total_page > 0)
286 {
287 *p_page_id = total_page - 1;
288 }
289 case 'N':
290 case KEY_PGDN:
291 if (item_count > 0)
292 {
293 *p_selected_index = item_count - 1;
294 }
295 case 'j':
296 case KEY_DOWN:
297 if (*p_selected_index + 1 >= item_count) // next page
298 {
299 if (*p_page_id + 1 < total_page)
300 {
301 (*p_page_id)++;
302 *p_selected_index = 0;
303 }
304 else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of list
305 {
306 *p_page_id = 0;
307 *p_selected_index = 0;
308 }
309 }
310 else
311 {
312 (*p_selected_index)++;
313 }
314 break;
315 case 's':
316 return SEARCH_USER;
317 case KEY_F5:
318 return REFRESH_LIST;
319 case 'h':
320 return SHOW_HELP;
321 default:
322 break;
323 }
324
325 if (old_page_id != *p_page_id)
326 {
327 return CHANGE_PAGE;
328 }
329
330 if (item_count > 0 && old_selected_index != *p_selected_index)
331 {
332 if (old_selected_index >= 0)
333 {
334 moveto(4 + old_selected_index, 1);
335 outc(' ');
336 }
337 if (*p_selected_index >= 0)
338 {
339 moveto(4 + *p_selected_index, 1);
340 outc('>');
341 }
342 iflush();
343
344 old_selected_index = *p_selected_index;
345 }
346 }
347
348 return EXIT_LIST;
349 }
350
351 int user_list_display(int online_user)
352 {
353 char page_info_str[LINE_BUFFER_LEN];
354 USER_INFO users[BBS_user_limit_per_page];
355 USER_ONLINE_INFO online_users[BBS_user_limit_per_page];
356 int user_count = 0;
357 int page_count = 0;
358 int page_id = 0;
359 int selected_index = 0;
360 int ret = 0;
361
362 user_list_draw_screen(online_user);
363
364 if (online_user)
365 {
366 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
367 if (ret < 0)
368 {
369 log_error("query_user_online_list(page_id=%d) error\n", page_id);
370 return -2;
371 }
372 }
373 else
374 {
375 ret = query_user_list(page_id, users, &user_count, &page_count);
376 if (ret < 0)
377 {
378 log_error("query_user_list(page_id=%d) error\n", page_id);
379 return -2;
380 }
381 }
382
383 if (user_count == 0) // empty list
384 {
385 selected_index = 0;
386 }
387
388 while (!SYS_server_exit)
389 {
390 if (online_user)
391 {
392 ret = user_online_list_draw_items(page_id, online_users, user_count);
393 if (ret < 0)
394 {
395 log_error("user_online_list_draw_items(page_id=%d) error\n", page_id);
396 return -3;
397 }
398 }
399 else
400 {
401 ret = user_list_draw_items(page_id, users, user_count);
402 if (ret < 0)
403 {
404 log_error("user_list_draw_items(page_id=%d) error\n", page_id);
405 return -3;
406 }
407 }
408
409 snprintf(page_info_str, sizeof(page_info_str),
410 "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
411 page_id + 1, MAX(page_count, 1));
412
413 show_bottom(page_info_str);
414 iflush();
415
416 if (user_online_update(online_user ? "USER_ONLINE" : "USER_LIST") < 0)
417 {
418 log_error("user_online_update(%s) error\n",
419 (online_user ? "USER_ONLINE" : "USER_LIST"));
420 }
421
422 ret = user_list_select(page_count, user_count, &page_id, &selected_index);
423 switch (ret)
424 {
425 case EXIT_LIST:
426 return 0;
427 case REFRESH_LIST:
428 case CHANGE_PAGE:
429 if (online_user)
430 {
431 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
432 if (ret < 0)
433 {
434 log_error("query_user_online_list(page_id=%d) error\n", page_id);
435 return -2;
436 }
437 }
438 else
439 {
440 ret = query_user_list(page_id, users, &user_count, &page_count);
441 if (ret < 0)
442 {
443 log_error("query_user_list(page_id=%d) error\n", page_id);
444 return -2;
445 }
446 }
447
448 if (user_count == 0) // empty list
449 {
450 selected_index = 0;
451 }
452 else if (selected_index >= user_count)
453 {
454 selected_index = user_count - 1;
455 }
456 break;
457 case VIEW_USER:
458 user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index]));
459 user_list_draw_screen(online_user);
460 break;
461 case SEARCH_USER:
462 user_list_search();
463 user_list_draw_screen(online_user);
464 break;
465 case SHOW_HELP:
466 // Display help information
467 display_file(DATA_READ_HELP, 1);
468 user_list_draw_screen(online_user);
469 break;
470 default:
471 log_error("Unknown command %d\n", ret);
472 }
473 }
474
475 return 0;
476 }
477
478 int user_list_search(void)
479 {
480 const int users_per_line = 5;
481 const int max_user_lines = 20;
482 const int max_user_cnt = users_per_line * max_user_lines + 1;
483
484 char username[BBS_username_max_len + 1];
485 int32_t uid_list[max_user_cnt];
486 char username_list[max_user_cnt][BBS_username_max_len + 1];
487 int ret;
488 int i;
489 USER_INFO user_info;
490 char user_intro[BBS_user_intro_max_len + 1];
491 int ok;
492 int ch;
493
494 username[0] = '\0';
495
496 clearscr();
497
498 while (!SYS_server_exit)
499 {
500 clrline(3, SCREEN_ROWS);
501 get_data(2, 1, "查找谁: ", username, sizeof(username), BBS_username_max_len);
502
503 if (username[0] == '\0')
504 {
505 return 0;
506 }
507
508 // Verify format
509 for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
510 {
511 if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
512 {
513 ok = 0;
514 }
515 }
516 if (ok && i > BBS_username_max_len)
517 {
518 ok = 0;
519 }
520 if (!ok)
521 {
522 moveto(3, 1);
523 clrtoeol();
524 prints("用户名格式非法");
525 continue;
526 }
527
528 clrline(3, SCREEN_ROWS);
529
530 ret = query_user_info_by_username(username, max_user_cnt, uid_list, username_list);
531
532 if (ret < 0)
533 {
534 log_error("query_user_info_by_username(%s) error\n", username);
535 return -1;
536 }
537 else if (ret > 1)
538 {
539 for (i = 0; i < MIN(ret, users_per_line * max_user_lines); i++)
540 {
541 moveto(4 + i / users_per_line, 3 + i % users_per_line * (BBS_username_max_len + 3));
542 prints("%s", username_list[i]);
543 }
544 moveto(SCREEN_ROWS, 1);
545 if (ret > users_per_line * max_user_lines)
546 {
547 prints("还有更多...");
548 }
549
550 moveto(3, 1);
551 prints("存在多个匹配的用户,按\033[1;33mEnter\033[m精确查找");
552 iflush();
553
554 ch = igetch_t(BBS_max_user_idle_time);
555 switch (ch)
556 {
557 case KEY_NULL:
558 case KEY_TIMEOUT:
559 return -1;
560 case KEY_ESC:
561 return 0;
562 case CR:
563 ret = (strcasecmp(username_list[0], username) == 0 ? 1 : 0);
564 break;
565 default:
566 i = (int)strnlen(username, sizeof(username) - 1);
567 if (i + 1 <= BBS_username_max_len && (isalnum(ch) || ch == '_'))
568 {
569 username[i] = (char)ch;
570 username[i + 1] = '\0';
571 }
572 continue;
573 }
574 }
575
576 clrline(3, SCREEN_ROWS);
577 if (ret == 0)
578 {
579 moveto(3, 1);
580 prints("没有找到符合条件的用户");
581 press_any_key();
582 return 0;
583 }
584 else // ret == 1
585 {
586 if (query_user_info_by_uid(uid_list[0], &user_info, user_intro, sizeof(user_intro)) <= 0)
587 {
588 log_error("query_user_info_by_uid(uid=%d) error\n", uid_list[0]);
589 return -2;
590 }
591 else if (user_info_display(&user_info) < 0)
592 {
593 log_error("user_info_display(uid=%d) error\n", uid_list[0]);
594 return -3;
595 }
596 return 1;
597 }
598 }
599
600 return 0;
601 }

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