/[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.24 - (show annotations)
Mon Nov 17 12:16:48 2025 UTC (3 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.23: +2 -2 lines
Content type: text/x-csrc
Add alternative implementation of shmat(..., SHM_REMAP) under MSYS2
Fix error reported by gcc under MSYS2

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
223 switch (ch)
224 {
225 case KEY_NULL: // broken pipe
226 log_error("KEY_NULL\n");
227 case KEY_ESC:
228 case KEY_LEFT:
229 return EXIT_LIST; // exit list
230 case KEY_TIMEOUT:
231 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
232 {
233 log_error("User input timeout\n");
234 return EXIT_LIST; // exit list
235 }
236 continue;
237 case CR:
238 case KEY_RIGHT:
239 if (item_count > 0)
240 {
241 return VIEW_USER;
242 }
243 break;
244 case KEY_HOME:
245 *p_page_id = 0;
246 case 'P':
247 case KEY_PGUP:
248 *p_selected_index = 0;
249 case 'k':
250 case KEY_UP:
251 if (*p_selected_index <= 0)
252 {
253 if (*p_page_id > 0)
254 {
255 (*p_page_id)--;
256 *p_selected_index = BBS_user_limit_per_page - 1;
257 }
258 else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of list
259 {
260 if (total_page > 0)
261 {
262 *p_page_id = total_page - 1;
263 }
264 if (item_count > 0)
265 {
266 *p_selected_index = item_count - 1;
267 }
268 }
269 }
270 else
271 {
272 (*p_selected_index)--;
273 }
274 break;
275 case '$':
276 case KEY_END:
277 if (total_page > 0)
278 {
279 *p_page_id = total_page - 1;
280 }
281 case 'N':
282 case KEY_PGDN:
283 if (item_count > 0)
284 {
285 *p_selected_index = item_count - 1;
286 }
287 case 'j':
288 case KEY_DOWN:
289 if (*p_selected_index + 1 >= item_count) // next page
290 {
291 if (*p_page_id + 1 < total_page)
292 {
293 (*p_page_id)++;
294 *p_selected_index = 0;
295 }
296 else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of list
297 {
298 *p_page_id = 0;
299 *p_selected_index = 0;
300 }
301 }
302 else
303 {
304 (*p_selected_index)++;
305 }
306 break;
307 case 's':
308 return SEARCH_USER;
309 case KEY_F5:
310 return REFRESH_LIST;
311 case 'h':
312 return SHOW_HELP;
313 default:
314 break;
315 }
316
317 if (old_page_id != *p_page_id)
318 {
319 return CHANGE_PAGE;
320 }
321
322 if (item_count > 0 && old_selected_index != *p_selected_index)
323 {
324 if (old_selected_index >= 0)
325 {
326 moveto(4 + old_selected_index, 1);
327 outc(' ');
328 }
329 if (*p_selected_index >= 0)
330 {
331 moveto(4 + *p_selected_index, 1);
332 outc('>');
333 }
334 iflush();
335
336 old_selected_index = *p_selected_index;
337 }
338 }
339
340 return EXIT_LIST;
341 }
342
343 int user_list_display(int online_user)
344 {
345 char page_info_str[LINE_BUFFER_LEN];
346 USER_INFO users[BBS_user_limit_per_page];
347 USER_ONLINE_INFO online_users[BBS_user_limit_per_page];
348 int user_count = 0;
349 int page_count = 0;
350 int page_id = 0;
351 int selected_index = 0;
352 int ret = 0;
353
354 user_list_draw_screen(online_user);
355
356 if (online_user)
357 {
358 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
359 if (ret < 0)
360 {
361 log_error("query_user_online_list(page_id=%d) error\n", page_id);
362 return -2;
363 }
364 }
365 else
366 {
367 ret = query_user_list(page_id, users, &user_count, &page_count);
368 if (ret < 0)
369 {
370 log_error("query_user_list(page_id=%d) error\n", page_id);
371 return -2;
372 }
373 }
374
375 if (user_count == 0) // empty list
376 {
377 selected_index = 0;
378 }
379
380 while (!SYS_server_exit)
381 {
382 if (online_user)
383 {
384 ret = user_online_list_draw_items(page_id, online_users, user_count);
385 if (ret < 0)
386 {
387 log_error("user_online_list_draw_items(page_id=%d) error\n", page_id);
388 return -3;
389 }
390 }
391 else
392 {
393 ret = user_list_draw_items(page_id, users, user_count);
394 if (ret < 0)
395 {
396 log_error("user_list_draw_items(page_id=%d) error\n", page_id);
397 return -3;
398 }
399 }
400
401 snprintf(page_info_str, sizeof(page_info_str),
402 "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
403 page_id + 1, MAX(page_count, 1));
404
405 show_bottom(page_info_str);
406 iflush();
407
408 if (user_online_update(online_user ? "USER_ONLINE" : "USER_LIST") < 0)
409 {
410 log_error("user_online_update(%s) error\n",
411 (online_user ? "USER_ONLINE" : "USER_LIST"));
412 }
413
414 ret = user_list_select(page_count, user_count, &page_id, &selected_index);
415 switch (ret)
416 {
417 case EXIT_LIST:
418 return 0;
419 case REFRESH_LIST:
420 case CHANGE_PAGE:
421 if (online_user)
422 {
423 ret = query_user_online_list(page_id, online_users, &user_count, &page_count);
424 if (ret < 0)
425 {
426 log_error("query_user_online_list(page_id=%d) error\n", page_id);
427 return -2;
428 }
429 }
430 else
431 {
432 ret = query_user_list(page_id, users, &user_count, &page_count);
433 if (ret < 0)
434 {
435 log_error("query_user_list(page_id=%d) error\n", page_id);
436 return -2;
437 }
438 }
439
440 if (user_count == 0) // empty list
441 {
442 selected_index = 0;
443 }
444 else if (selected_index >= user_count)
445 {
446 selected_index = user_count - 1;
447 }
448 break;
449 case VIEW_USER:
450 user_info_display(online_user ? &(online_users[selected_index].user_info) : &(users[selected_index]));
451 user_list_draw_screen(online_user);
452 break;
453 case SEARCH_USER:
454 user_list_search();
455 user_list_draw_screen(online_user);
456 break;
457 case SHOW_HELP:
458 // Display help information
459 display_file(DATA_READ_HELP, 1);
460 user_list_draw_screen(online_user);
461 break;
462 default:
463 log_error("Unknown command %d\n", ret);
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((int)username[i]) || (i > 0 && (isdigit((int)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(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;
593 }

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