--- lbbs/src/user_info_display.c 2025/10/23 09:55:15 1.1 +++ lbbs/src/user_info_display.c 2025/10/23 11:58:00 1.5 @@ -14,18 +14,47 @@ * * ***************************************************************************/ +#include "ip_mask.h" #include "lml.h" #include "log.h" #include "screen.h" #include "str_process.h" #include "user_list.h" #include "user_info_display.h" +#include "user_priv.h" #include +#include #include #define BBS_max_sessions_per_user 10 +#define LAST_LOGIN_DT_MAX_LEN 50 -static int display_user_intro_key_handler(int *p_key, DISPLAY_CTX *p_ctx) +static const int astro_dates[] = { + 21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22}; + +static const char *astro_names[] = { + "摩羯", "水瓶", "双鱼", "白羊", "金牛", "双子", "巨蟹", "狮子", "处女", "天秤", "天蝎", "射手", "摩羯"}; + +const char *get_astro_name(time_t birthday) +{ + struct tm tm_birth; + + localtime_r(&birthday, &tm_birth); + + if (tm_birth.tm_mon < 1 || tm_birth.tm_mon > 12 || tm_birth.tm_mday < 1 || tm_birth.tm_mday > 31) + { + return astro_names[0]; + } + + if (tm_birth.tm_mday < astro_dates[tm_birth.tm_mon - 1]) + { + return astro_names[tm_birth.tm_mon - 1]; + } + + return astro_names[tm_birth.tm_mon]; +} + +static int display_user_info_key_handler(int *p_key, DISPLAY_CTX *p_ctx) { return 0; } @@ -34,11 +63,61 @@ int user_info_display(USER_INFO *p_user_ { USER_ONLINE_INFO sessions[BBS_max_sessions_per_user]; int session_cnt = BBS_max_sessions_per_user; + int article_cnt; + const char *astro_name; + char astro_str[LINE_BUFFER_LEN]; + struct tm tm_last_login; + char str_last_login_dt[LAST_LOGIN_DT_MAX_LEN + 1]; + struct tm tm_last_logout; + char str_last_logout_dt[LAST_LOGIN_DT_MAX_LEN + 1]; + char login_ip[IP_ADDR_LEN]; + int ip_mask_level; char action_str[LINE_BUFFER_LEN]; + char intro_f[BBS_user_intro_max_len]; + int intro_len; + char user_info_f[BUFSIZ]; + long line_offsets[BBS_user_intro_max_line + 1]; + long lines; char *p; + const char *q; int ret; int i; + article_cnt = get_user_article_cnt(p_user_info->uid); + + astro_name = get_astro_name(p_user_info->birthday); + if (p_user_info->gender_pub && toupper(p_user_info->gender) == 'M') + { + snprintf(astro_str, sizeof(astro_str), + "\033[1;36m%s座\033[m", + astro_name); + } + else if (p_user_info->gender_pub && toupper(p_user_info->gender) == 'F') + { + snprintf(astro_str, sizeof(astro_str), + "\033[1;35m%s座\033[m", + astro_name); + } + else + { + snprintf(astro_str, sizeof(astro_str), + "\033[1;33m%s座\033[m", + astro_name); + } + + localtime_r(&(p_user_info->last_login_dt), &tm_last_login); + strftime(str_last_login_dt, sizeof(str_last_login_dt), "%c", &tm_last_login); + if (p_user_info->last_logout_dt <= p_user_info->last_login_dt) + { + strncpy(str_last_logout_dt, str_last_login_dt, sizeof(str_last_logout_dt) - 1); + str_last_logout_dt[sizeof(str_last_logout_dt) - 1] = '\0'; + } + else + { + localtime_r(&(p_user_info->last_logout_dt), &tm_last_logout); + strftime(str_last_logout_dt, sizeof(str_last_logout_dt), "%c", &tm_last_logout); + } + action_str[0] = '\0'; ret = query_user_online_info_by_uid(p_user_info->uid, sessions, &session_cnt, 0); @@ -46,44 +125,60 @@ int user_info_display(USER_INFO *p_user_ { log_error("query_user_online_info_by_uid(uid=%d, cnt=%d) error: %d\n", p_user_info->uid, session_cnt, ret); + session_cnt = 0; } - else + + p = action_str; + for (i = 0; i < session_cnt; i++) { - p = action_str; - for (i = 0; i < session_cnt; i++) + if (p + strlen(sessions[i].current_action_title) + 4 >= action_str + sizeof(action_str)) // buffer overflow { - if (p + strlen(sessions[i].current_action_title) + 3 >= action_str + sizeof(action_str)) // buffer overflow - { - log_error("action_str of user(uid=%d) truncated at i=%d\n", p_user_info->uid, i); - break; - } - *p++ = '['; - p = stpncpy(p, sessions[i].current_action_title, strlen(sessions[i].current_action_title)); - *p++ = ']'; - *p++ = ' '; + log_error("action_str of user(uid=%d) truncated at i=%d\n", p_user_info->uid, i); + break; } - *p = '\0'; + *p++ = '['; + for (q = sessions[i].current_action_title; *q != '\0';) + { + *p++ = *q++; + } + *p++ = ']'; + *p++ = ' '; } + *p++ = '\n'; + *p = '\0'; - char intro_f[BBS_user_intro_max_len]; - char profile_f[BUFSIZ]; - long line_offsets[BBS_user_intro_max_line + 1]; - long lines; + if (session_cnt > 0) + { + strncpy(login_ip, sessions[0].ip, sizeof(login_ip) - 1); + login_ip[sizeof(login_ip) - 1] = '\0'; + + ip_mask_level = checklevel(&BBS_priv, P_ADMIN_M | P_ADMIN_S); + ip_mask(login_ip, (ip_mask_level ? 1 : 2), '*'); + } + else + { + login_ip[0] = '\0'; + } - lml_render(p_user_info->intro, intro_f, sizeof(intro_f), 0); + intro_len = lml_render(p_user_info->intro, intro_f, sizeof(intro_f), 0); - snprintf(profile_f, sizeof(profile_f), - "已选中用户 [%s]\n发帖数:%d\n%s\n%s\n", - p_user_info->username, - get_user_article_cnt(p_user_info->uid), - action_str, + snprintf(user_info_f, sizeof(user_info_f), + "\n%s (%s) 上站 [%d] 发文 [%d]\n" + "上次在 [%s] 从 [%s] 访问本站 经验值 [%d]\n" + "离线于 [%s] 等级 [%s] 星座 [%s]\n" + "%s\033[1m%s\033[m" + "%s\n%s\n", + p_user_info->username, p_user_info->nickname, p_user_info->visit_count, article_cnt, + str_last_login_dt, (session_cnt > 0 ? login_ip : "未知"), p_user_info->exp, + (session_cnt > 0 ? "在线或因断线不详" : str_last_logout_dt), "?", astro_str, + (session_cnt > 0 ? "目前在线,状态如下:\n" : ""), (session_cnt > 0 ? action_str : ""), + (intro_len > 0 ? "\033[0;36m个人说明档如下:\033[m" : "\033[0;36m没有个人说明档\033[m"), intro_f); - lines = split_data_lines(profile_f, SCREEN_COLS, line_offsets, MIN(SCREEN_ROWS - 1, BBS_user_intro_max_line + 4), 1, NULL); + lines = split_data_lines(user_info_f, SCREEN_COLS, line_offsets, MIN(SCREEN_ROWS - 1, BBS_user_intro_max_line + 8), 1, NULL); clearscr(); - display_data(profile_f, lines, line_offsets, 2, display_user_intro_key_handler, DATA_READ_HELP); - press_any_key(); + display_data(user_info_f, lines, line_offsets, 1, display_user_info_key_handler, DATA_READ_HELP); return 0; }