| 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 |
USER_INFO user_info;
|
| 248 |
int user_count;
|
| 249 |
int page_count;
|
| 250 |
int page_id = 0;
|
| 251 |
int selected_index = 0;
|
| 252 |
int ret;
|
| 253 |
|
| 254 |
user_list_draw_screen();
|
| 255 |
|
| 256 |
ret = query_user_list(page_id, users, &user_count, &page_count);
|
| 257 |
if (ret < 0)
|
| 258 |
{
|
| 259 |
log_error("query_user_list(page_id=%d) error\n", page_id);
|
| 260 |
return -2;
|
| 261 |
}
|
| 262 |
|
| 263 |
if (user_count == 0) // empty list
|
| 264 |
{
|
| 265 |
selected_index = 0;
|
| 266 |
}
|
| 267 |
|
| 268 |
while (!SYS_server_exit)
|
| 269 |
{
|
| 270 |
ret = user_list_draw_items(page_id, users, user_count);
|
| 271 |
if (ret < 0)
|
| 272 |
{
|
| 273 |
log_error("user_list_draw_items(page_id=%d) error\n", page_id);
|
| 274 |
return -3;
|
| 275 |
}
|
| 276 |
|
| 277 |
snprintf(page_info_str, sizeof(page_info_str),
|
| 278 |
"\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
|
| 279 |
page_id + 1, MAX(page_count, 1));
|
| 280 |
|
| 281 |
show_bottom(page_info_str);
|
| 282 |
iflush();
|
| 283 |
|
| 284 |
if (user_online_update("USER_LIST") < 0)
|
| 285 |
{
|
| 286 |
log_error("user_online_update(USER_LIST) error\n");
|
| 287 |
}
|
| 288 |
|
| 289 |
ret = user_list_select(page_count, user_count, &page_id, &selected_index);
|
| 290 |
switch (ret)
|
| 291 |
{
|
| 292 |
case EXIT_LIST:
|
| 293 |
return 0;
|
| 294 |
case CHANGE_PAGE:
|
| 295 |
ret = query_user_list(page_id, users, &user_count, &page_count);
|
| 296 |
if (ret < 0)
|
| 297 |
{
|
| 298 |
log_error("query_favor_articles(page_id=%d) error\n", page_id);
|
| 299 |
return -2;
|
| 300 |
}
|
| 301 |
|
| 302 |
if (user_count == 0) // empty list
|
| 303 |
{
|
| 304 |
selected_index = 0;
|
| 305 |
}
|
| 306 |
else if (selected_index >= user_count)
|
| 307 |
{
|
| 308 |
selected_index = user_count - 1;
|
| 309 |
}
|
| 310 |
break;
|
| 311 |
case VIEW_USER:
|
| 312 |
if ((ret = query_user_info(users[selected_index].uid, &user_info)) < 0)
|
| 313 |
{
|
| 314 |
log_error("query_user_info(uid=%d) error: %d\n", users[selected_index].uid, ret);
|
| 315 |
}
|
| 316 |
else if (ret == 0)
|
| 317 |
{
|
| 318 |
log_error("query_user_info(uid=%d) error: user not found\n", users[selected_index].uid);
|
| 319 |
}
|
| 320 |
else if (users[selected_index].uid != user_info.uid)
|
| 321 |
{
|
| 322 |
log_error("query_user_info(uid=%d) error: inconsistent uid=%d\n", users[selected_index].uid, user_info.uid);
|
| 323 |
}
|
| 324 |
else
|
| 325 |
{
|
| 326 |
clearscr();
|
| 327 |
press_any_key_ex("功能不可用,按任意键返回", 60);
|
| 328 |
user_list_draw_screen();
|
| 329 |
}
|
| 330 |
break;
|
| 331 |
case SHOW_HELP:
|
| 332 |
// Display help information
|
| 333 |
display_file(DATA_READ_HELP, 1);
|
| 334 |
user_list_draw_screen();
|
| 335 |
break;
|
| 336 |
default:
|
| 337 |
log_error("Unknown command %d\n", ret);
|
| 338 |
}
|
| 339 |
}
|
| 340 |
|
| 341 |
return 0;
|
| 342 |
}
|