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

Diff of /lbbs/src/section_list_display.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.46 by sysadm, Mon Oct 13 02:23:27 2025 UTC Revision 1.62 by sysadm, Sun Nov 2 15:11:47 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "article_cache.h"  #include "article_cache.h"
18    #include "article_favor.h"
19  #include "article_op.h"  #include "article_op.h"
20  #include "article_post.h"  #include "article_post.h"
21  #include "article_view_log.h"  #include "article_view_log.h"
# Line 24  Line 25 
25  #include "log.h"  #include "log.h"
26  #include "login.h"  #include "login.h"
27  #include "menu.h"  #include "menu.h"
28    #include "menu_proc.h"
29  #include "section_list_display.h"  #include "section_list_display.h"
30  #include "section_list_loader.h"  #include "section_list_loader.h"
31  #include "screen.h"  #include "screen.h"
32  #include "str_process.h"  #include "str_process.h"
33    #include "user_info_display.h"
34  #include "user_priv.h"  #include "user_priv.h"
35    #include <errno.h>
36  #include <string.h>  #include <string.h>
37  #include <time.h>  #include <time.h>
38  #include <sys/param.h>  #include <sys/param.h>
39    
40  static int section_aid_locations[BBS_max_section] = {0};  static int32_t section_aid_locations[BBS_max_section] = {0};
41  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
42  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
43    
# Line 48  enum select_cmd_t Line 52  enum select_cmd_t
52          EDIT_ARTICLE,          EDIT_ARTICLE,
53          DELETE_ARTICLE,          DELETE_ARTICLE,
54          QUERY_ARTICLE,          QUERY_ARTICLE,
55            QUERY_USER,
56            SET_FAVOR_ARTICLE,
57            UNSET_FAVOR_ARTICLE,
58          FIRST_TOPIC_ARTICLE,          FIRST_TOPIC_ARTICLE,
59          LAST_TOPIC_ARTICLE,          LAST_TOPIC_ARTICLE,
60          SCAN_NEW_ARTICLE,          SCAN_NEW_ARTICLE,
61          VIEW_EX_DIR,          VIEW_EX_DIR,
62            SHOW_TOP10,
63  };  };
64    
65  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname, int ontop_start_offset)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname, int ontop_start_offset)
# Line 66  static int section_list_draw_items(int p Line 74  static int section_list_draw_items(int p
74          size_t j;          size_t j;
75          char article_flag;          char article_flag;
76          int is_viewed;          int is_viewed;
77            int is_favor;
78          time_t tm_now;          time_t tm_now;
79    
80          time(&tm_now);          time(&tm_now);
# Line 88  static int section_list_draw_items(int p Line 97  static int section_list_draw_items(int p
97                          }                          }
98                  }                  }
99    
100                    if (p_articles[i]->tid == 0)
101                    {
102                            is_favor = article_favor_check(p_articles[i]->aid, &BBS_article_favor);
103                            if (is_favor < 0)
104                            {
105                                    log_error("article_favor_check(aid=%d) error\n", p_articles[i]->aid);
106                                    is_favor = 0;
107                            }
108                    }
109                    else
110                    {
111                            is_favor = 0;
112                    }
113    
114                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
115                  {                  {
116                          article_flag = (is_viewed ? 'm' : 'M');                          article_flag = (is_viewed ? 'm' : 'M');
# Line 134  static int section_list_draw_items(int p Line 157  static int section_list_draw_items(int p
157                  }                  }
158                  strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
159    
160                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);                  len = split_line(title_f, 59 - (display_nickname ? BBS_nickname_max_len / 2 : BBS_username_max_len), &eol, &title_f_len, 1);
161                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
162                  {                  {
163                          title_f[len] = '\0';                          title_f[len] = '\0';
# Line 143  static int section_list_draw_items(int p Line 166  static int section_list_draw_items(int p
166                  moveto(4 + i, 1);                  moveto(4 + i, 1);
167                  if (i >= ontop_start_offset)                  if (i >= ontop_start_offset)
168                  {                  {
169                          prints("   \033[1;33m[提示]\033[m %c %s%*s %s %s%s\033[m",                          prints("   \033[1;33m[提示]\033[m%c%c %s%*s %s %s%s\033[m",
170                                       (is_favor ? '@' : ' '),
171                                     article_flag,                                     article_flag,
172                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
173                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
# Line 159  static int section_list_draw_items(int p Line 183  static int section_list_draw_items(int p
183                  }                  }
184                  else                  else
185                  {                  {
186                          prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",                          prints("  %s%7d\033[m%c%c %s%*s %s %s%s\033[m",
187                                     (p_articles[i]->aid == section_topic_view_tid                                     (p_articles[i]->aid == section_topic_view_tid
188                                                  ? "\033[1;33m"                                                  ? "\033[1;33m"
189                                                  : (p_articles[i]->tid == section_topic_view_tid                                                  : (p_articles[i]->tid == section_topic_view_tid
190                                                             ? "\033[1;36m"                                                             ? "\033[1;36m"
191                                                             : "")),                                                             : "")),
192                                     p_articles[i]->aid,                                     p_articles[i]->aid,
193                                       (is_favor ? '@' : ' '),
194                                     article_flag,                                     article_flag,
195                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
196                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
# Line 233  static enum select_cmd_t section_list_se Line 258  static enum select_cmd_t section_list_se
258          {          {
259                  ch = igetch(100);                  ch = igetch(100);
260    
261                  switch (ch)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
262                  {                  {
                 case KEY_ESC:  
                 case KEY_LEFT:  
263                          BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
264                  case KEY_NULL:                   // broken pipe                  }
265                          return EXIT_SECTION; // exit section  
266                    switch (ch)
267                    {
268                    case KEY_NULL: // broken pipe
269                            log_error("KEY_NULL\n");
270                            return EXIT_SECTION;
271                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
272                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
273                          {                          {
274                                  return EXIT_SECTION; // exit section                                  log_error("User input timeout\n");
275                                    return EXIT_SECTION;
276                          }                          }
277                          continue;                          continue;
278                    case KEY_ESC:
279                    case KEY_LEFT:
280                            return EXIT_SECTION;
281                  case 'n':                  case 'n':
                         BBS_last_access_tm = time(NULL);  
282                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
283                  case CR:                  case CR:
                         igetch_reset();  
284                  case 'r':                  case 'r':
285                  case KEY_RIGHT:                  case KEY_RIGHT:
286                          if (item_count > 0)                          if (item_count > 0)
287                          {                          {
                                 BBS_last_access_tm = time(NULL);  
288                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
289                          }                          }
290                          break;                          break;
# Line 279  static enum select_cmd_t section_list_se Line 308  static enum select_cmd_t section_list_se
308                                  return QUERY_ARTICLE;                                  return QUERY_ARTICLE;
309                          }                          }
310                          break;                          break;
311                    case Ctrl('A'):
312                            if (item_count > 0)
313                            {
314                                    return QUERY_USER;
315                            }
316                            break;
317                    case 'F':
318                            if (item_count > 0)
319                            {
320                                    return SET_FAVOR_ARTICLE;
321                            }
322                            break;
323                    case '-':
324                            if (item_count > 0)
325                            {
326                                    return UNSET_FAVOR_ARTICLE;
327                            }
328                            break;
329                  case KEY_HOME:                  case KEY_HOME:
330                          *p_page_id = 0;                          *p_page_id = 0;
331                  case 'P':                  case 'P':
# Line 364  static enum select_cmd_t section_list_se Line 411  static enum select_cmd_t section_list_se
411                          return SHOW_HELP;                          return SHOW_HELP;
412                  case 'x':                  case 'x':
413                          return VIEW_EX_DIR;                          return VIEW_EX_DIR;
414                    case 'H':
415                            return SHOW_TOP10;
416                  default:                  default:
417                            break;
418                  }                  }
419    
420                  if (old_page_id != *p_page_id)                  if (old_page_id != *p_page_id)
# Line 389  static enum select_cmd_t section_list_se Line 439  static enum select_cmd_t section_list_se
439                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
440                  }                  }
441    
                 BBS_last_access_tm = time(NULL);  
442                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
443                  {                  {
444                          return CHANGE_PAGE; // force section list refresh                          return CHANGE_PAGE; // force section list refresh
# Line 509  int section_list_display(const char *sna Line 558  int section_list_display(const char *sna
558          ARTICLE article_new;          ARTICLE article_new;
559          int page_id_cur;          int page_id_cur;
560          const ARTICLE *p_article_locate;          const ARTICLE *p_article_locate;
561            USER_INFO user_info;
562            char user_intro[BBS_user_intro_max_len];
563    
564          p_section = section_list_find_by_name(sname, &section_index);          p_section = section_list_find_by_name(sname);
565          if (p_section == NULL)          if (p_section == NULL)
566          {          {
567                  log_error("Section %s not found\n", sname);                  log_error("Section %s not found\n", sname);
568                  return -1;                  return -1;
569          }          }
570    
571          if ((ret = section_list_rd_lock(p_section)) < 0)          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
572          {          {
573                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
574                  return -2;                                    p_section->sid, BBS_priv.uid);
575                    return -1;
576          }          }
577    
578          strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);          section_index = get_section_index(p_section);
         stitle[sizeof(stitle) - 1] = '\0';  
         strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);  
         master_list[sizeof(master_list) - 1] = '\0';  
579    
580          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
581          {          {
582                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
583                  return -2;                  return -4;
584          }          }
585    
586          if (aid == 0)          if (aid == 0)
# Line 541  int section_list_display(const char *sna Line 590  int section_list_display(const char *sna
590          else          else
591          {          {
592                  aid_location = aid;                  aid_location = aid;
593          }                }
594    
595          // Locate at article with aid_locate          // Locate at article with aid_locate
596          if (aid_location > 0)          if (aid_location > 0)
# Line 613  int section_list_display(const char *sna Line 662  int section_list_display(const char *sna
662                  }                  }
663    
664                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
665    
666                  switch (ret)                  switch (ret)
667                  {                  {
668                  case EXIT_SECTION:                  case EXIT_SECTION:
669                            // Update current aid location
670                            if (p_articles[selected_index] != NULL)
671                            {
672                                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
673                            }
674                            else
675                            {
676                                    log_error("p_articles[selected_index=%d] is NULL when exit section [%s]\n", selected_index, sname);
677                            }
678                          return 0;                          return 0;
679                  case CHANGE_PAGE:                  case CHANGE_PAGE:
680                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
# Line 793  int section_list_display(const char *sna Line 852  int section_list_display(const char *sna
852    
853                          // Update current topic                          // Update current topic
854                          section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);                          section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
                         log_error("topic_view_tid=%d\n", section_topic_view_tid);  
   
                         // Update current aid location  
                         section_aid_locations[section_index] = p_articles[selected_index]->aid;  
855    
856                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
857                          {                          {
# Line 895  int section_list_display(const char *sna Line 950  int section_list_display(const char *sna
950                                  return -2;                                  return -2;
951                          }                          }
952                          break;                          break;
953                    case QUERY_USER:
954                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info, user_intro, sizeof(user_intro))) < 0)
955                            {
956                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
957                                    return -2;
958                            }
959                            else if (ret == 0)
960                            {
961                                    clearscr();
962                                    prints("该用户已升天");
963                                    press_any_key();
964                            }
965                            else if (user_info_display(&user_info) < 0) // && ret > 0
966                            {
967                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
968                            }
969    
970                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
971                            {
972                                    log_error("section_list_draw_screen() error\n");
973                                    return -2;
974                            }
975                            break;
976                    case SET_FAVOR_ARTICLE:
977                            ret = article_favor_set(p_articles[selected_index]->tid == 0
978                                                                                    ? p_articles[selected_index]->aid
979                                                                                    : p_articles[selected_index]->tid,
980                                                                            &BBS_article_favor, 1);
981                            if (ret < 0)
982                            {
983                                    log_error("article_favor_set(aid=%d, 1) error\n",
984                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
985                            }
986                            break;
987                    case UNSET_FAVOR_ARTICLE:
988                            ret = article_favor_set(p_articles[selected_index]->tid == 0
989                                                                                    ? p_articles[selected_index]->aid
990                                                                                    : p_articles[selected_index]->tid,
991                                                                            &BBS_article_favor, 0);
992                            if (ret < 0)
993                            {
994                                    log_error("article_favor_set(aid=%d, 0) error\n",
995                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
996                            }
997                            break;
998                  case FIRST_TOPIC_ARTICLE:                  case FIRST_TOPIC_ARTICLE:
999                  case LAST_TOPIC_ARTICLE:                  case LAST_TOPIC_ARTICLE:
1000                          page_id_cur = page_id;                          page_id_cur = page_id;
# Line 968  int section_list_display(const char *sna Line 1068  int section_list_display(const char *sna
1068                                  return -2;                                  return -2;
1069                          }                          }
1070                          break;                          break;
1071                    case SHOW_TOP10:
1072                            show_top10_menu(NULL);
1073                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1074                            {
1075                                    log_error("section_list_draw_screen() error\n");
1076                                    return -2;
1077                            }
1078                            break;
1079                  default:                  default:
1080                          log_error("Unknown command %d\n", ret);                          log_error("Unknown command %d\n", ret);
1081                  }                  }
# Line 1016  int section_list_ex_dir_display(SECTION_ Line 1124  int section_list_ex_dir_display(SECTION_
1124                  {                  {
1125                          iflush();                          iflush();
1126                          ch = igetch(100);                          ch = igetch(100);
1127    
1128                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1129                            {
1130                                    BBS_last_access_tm = time(NULL);
1131                            }
1132    
1133                          switch (ch)                          switch (ch)
1134                          {                          {
1135                          case KEY_NULL: // broken pipe                          case KEY_NULL: // broken pipe
1136                                    log_error("KEY_NULL\n");
1137                                  return 0;                                  return 0;
1138                          case KEY_TIMEOUT:                          case KEY_TIMEOUT:
1139                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
1140                                  {                                  {
1141                                            log_error("User input timeout\n");
1142                                          return 0;                                          return 0;
1143                                  }                                  }
1144                                  continue;                                  continue;
1145                          case CR:                          case CR:
                                 igetch_reset();  
1146                          default:                          default:
1147                                  switch (menu_control(&ex_menu_set, ch))                                  switch (menu_control(&ex_menu_set, ch))
1148                                  {                                  {
# Line 1046  int section_list_ex_dir_display(SECTION_ Line 1161  int section_list_ex_dir_display(SECTION_
1161                                  }                                  }
1162                          }                          }
1163    
                         BBS_last_access_tm = time(NULL);  
   
1164                          if (ch == EXITMENU)                          if (ch == EXITMENU)
1165                          {                          {
1166                                  break;                                  break;
# Line 1059  int section_list_ex_dir_display(SECTION_ Line 1172  int section_list_ex_dir_display(SECTION_
1172    
1173          return 0;          return 0;
1174  }  }
1175    
1176    int section_aid_locations_save(int uid)
1177    {
1178            char filename[FILE_PATH_LEN];
1179            FILE *fp;
1180            int i;
1181            int ret = 0;
1182    
1183            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1184    
1185            if ((fp = fopen(filename, "wb")) == NULL)
1186            {
1187                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1188                    return -1;
1189            }
1190    
1191            for (i = 0; i < p_section_list_pool->section_count; i++)
1192            {
1193                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1194                    {
1195                            log_error("fwrite(%s, sid) error\n", filename);
1196                            ret = -2;
1197                            break;
1198                    }
1199    
1200                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1201                    {
1202                            log_error("fwrite(%s, aid) error\n", filename);
1203                            ret = -2;
1204                            break;
1205                    }
1206            }
1207    
1208            if (fclose(fp) < 0)
1209            {
1210                    log_error("fclose(%s) error: %d\n", filename, errno);
1211                    ret = -1;
1212            }
1213    
1214            return ret;
1215    }
1216    
1217    int section_aid_locations_load(int uid)
1218    {
1219            char filename[FILE_PATH_LEN];
1220            FILE *fp;
1221            int i;
1222            int32_t sid;
1223            int32_t aid;
1224            SECTION_LIST *p_section;
1225            int ret = 0;
1226    
1227            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1228    
1229            if ((fp = fopen(filename, "rb")) == NULL)
1230            {
1231                    if (errno == ENOENT) // file not exist
1232                    {
1233                            return 0;
1234                    }
1235                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1236                    return -1;
1237            }
1238    
1239            while (!feof(fp))
1240            {
1241                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1242                    {
1243                            if (ferror(fp) == 0)
1244                            {
1245                                    break;
1246                            }
1247                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1248                            ret = -2;
1249                            break;
1250                    }
1251    
1252                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1253                    {
1254                            if (ferror(fp) == 0)
1255                            {
1256                                    break;
1257                            }
1258                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1259                            ret = -2;
1260                            break;
1261                    }
1262    
1263                    p_section = section_list_find_by_sid(sid);
1264                    if (p_section == NULL)
1265                    {
1266                            continue; // skip section no longer exist
1267                    }
1268    
1269                    i = get_section_index(p_section);
1270                    if (i < 0)
1271                    {
1272                            log_error("get_section_index(sid=%d) error\n", sid);
1273                            ret = -3;
1274                            break;
1275                    }
1276                    section_aid_locations[i] = aid;
1277            }
1278    
1279            if (fclose(fp) < 0)
1280            {
1281                    log_error("fclose(%s) error: %d\n", filename, errno);
1282                    ret = -1;
1283            }
1284    
1285            return ret;
1286    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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