/[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.48 by sysadm, Mon Oct 13 07:35:48 2025 UTC Revision 1.60 by sysadm, Sun Nov 2 08:35:23 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 29  Line 30 
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 49  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,
# Line 68  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 90  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 136  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 145  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 161  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 235  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 281  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 366  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 Ctrl('H'):                  case 'H':
415                          return SHOW_TOP10;                          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 393  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 513  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    
563          p_section = section_list_find_by_name(sname, &section_index);          p_section = section_list_find_by_name(sname);
564          if (p_section == NULL)          if (p_section == NULL)
565          {          {
566                  log_error("Section %s not found\n", sname);                  log_error("Section %s not found\n", sname);
567                  return -1;                  return -1;
568          }          }
569    
570          if ((ret = section_list_rd_lock(p_section)) < 0)          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
571          {          {
572                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
573                  return -2;                                    p_section->sid, BBS_priv.uid);
574                    return -1;
575          }          }
576    
577          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';  
578    
579          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
580          {          {
581                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
582                  return -2;                  return -4;
583          }          }
584    
585          if (aid == 0)          if (aid == 0)
# Line 545  int section_list_display(const char *sna Line 589  int section_list_display(const char *sna
589          else          else
590          {          {
591                  aid_location = aid;                  aid_location = aid;
592          }                }
593    
594          // Locate at article with aid_locate          // Locate at article with aid_locate
595          if (aid_location > 0)          if (aid_location > 0)
# Line 617  int section_list_display(const char *sna Line 661  int section_list_display(const char *sna
661                  }                  }
662    
663                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
664    
665                    // Update current aid location
666                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
667    
668                  switch (ret)                  switch (ret)
669                  {                  {
670                  case EXIT_SECTION:                  case EXIT_SECTION:
# Line 798  int section_list_display(const char *sna Line 846  int section_list_display(const char *sna
846                          // Update current topic                          // Update current topic
847                          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);
848    
                         // Update current aid location  
                         section_aid_locations[section_index] = p_articles[selected_index]->aid;  
   
849                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
850                          {                          {
851                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 898  int section_list_display(const char *sna Line 943  int section_list_display(const char *sna
943                                  return -2;                                  return -2;
944                          }                          }
945                          break;                          break;
946                    case QUERY_USER:
947                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info)) < 0)
948                            {
949                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
950                                    return -2;
951                            }
952                            else if (ret == 0)
953                            {
954                                    clearscr();
955                                    prints("该用户已升天");
956                                    press_any_key();
957                            }
958                            else if (user_info_display(&user_info) < 0) // && ret > 0
959                            {
960                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
961                            }
962    
963                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
964                            {
965                                    log_error("section_list_draw_screen() error\n");
966                                    return -2;
967                            }
968                            break;
969                    case SET_FAVOR_ARTICLE:
970                            ret = article_favor_set(p_articles[selected_index]->tid == 0
971                                                                                    ? p_articles[selected_index]->aid
972                                                                                    : p_articles[selected_index]->tid,
973                                                                            &BBS_article_favor, 1);
974                            if (ret < 0)
975                            {
976                                    log_error("article_favor_set(aid=%d, 1) error\n",
977                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
978                            }
979                            break;
980                    case UNSET_FAVOR_ARTICLE:
981                            ret = article_favor_set(p_articles[selected_index]->tid == 0
982                                                                                    ? p_articles[selected_index]->aid
983                                                                                    : p_articles[selected_index]->tid,
984                                                                            &BBS_article_favor, 0);
985                            if (ret < 0)
986                            {
987                                    log_error("article_favor_set(aid=%d, 0) error\n",
988                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
989                            }
990                            break;
991                  case FIRST_TOPIC_ARTICLE:                  case FIRST_TOPIC_ARTICLE:
992                  case LAST_TOPIC_ARTICLE:                  case LAST_TOPIC_ARTICLE:
993                          page_id_cur = page_id;                          page_id_cur = page_id;
# Line 1027  int section_list_ex_dir_display(SECTION_ Line 1117  int section_list_ex_dir_display(SECTION_
1117                  {                  {
1118                          iflush();                          iflush();
1119                          ch = igetch(100);                          ch = igetch(100);
1120    
1121                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1122                            {
1123                                    BBS_last_access_tm = time(NULL);
1124                            }
1125    
1126                          switch (ch)                          switch (ch)
1127                          {                          {
1128                          case KEY_NULL: // broken pipe                          case KEY_NULL: // broken pipe
1129                                    log_error("KEY_NULL\n");
1130                                  return 0;                                  return 0;
1131                          case KEY_TIMEOUT:                          case KEY_TIMEOUT:
1132                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
1133                                  {                                  {
1134                                            log_error("User input timeout\n");
1135                                          return 0;                                          return 0;
1136                                  }                                  }
1137                                  continue;                                  continue;
1138                          case CR:                          case CR:
                                 igetch_reset();  
1139                          default:                          default:
1140                                  switch (menu_control(&ex_menu_set, ch))                                  switch (menu_control(&ex_menu_set, ch))
1141                                  {                                  {
# Line 1057  int section_list_ex_dir_display(SECTION_ Line 1154  int section_list_ex_dir_display(SECTION_
1154                                  }                                  }
1155                          }                          }
1156    
                         BBS_last_access_tm = time(NULL);  
   
1157                          if (ch == EXITMENU)                          if (ch == EXITMENU)
1158                          {                          {
1159                                  break;                                  break;
# Line 1070  int section_list_ex_dir_display(SECTION_ Line 1165  int section_list_ex_dir_display(SECTION_
1165    
1166          return 0;          return 0;
1167  }  }
1168    
1169    int section_aid_locations_save(int uid)
1170    {
1171            char filename[FILE_PATH_LEN];
1172            FILE *fp;
1173            int i;
1174            int ret = 0;
1175    
1176            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1177    
1178            if ((fp = fopen(filename, "wb")) == NULL)
1179            {
1180                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1181                    return -1;
1182            }
1183    
1184            for (i = 0; i < p_section_list_pool->section_count; i++)
1185            {
1186                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1187                    {
1188                            log_error("fwrite(%s, sid) error\n", filename);
1189                            ret = -2;
1190                            break;
1191                    }
1192    
1193                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1194                    {
1195                            log_error("fwrite(%s, aid) error\n", filename);
1196                            ret = -2;
1197                            break;
1198                    }
1199            }
1200    
1201            if (fclose(fp) < 0)
1202            {
1203                    log_error("fclose(%s) error: %d\n", filename, errno);
1204                    ret = -1;
1205            }
1206    
1207            return ret;
1208    }
1209    
1210    int section_aid_locations_load(int uid)
1211    {
1212            char filename[FILE_PATH_LEN];
1213            FILE *fp;
1214            int i;
1215            int32_t sid;
1216            int32_t aid;
1217            SECTION_LIST *p_section;
1218            int ret = 0;
1219    
1220            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1221    
1222            if ((fp = fopen(filename, "rb")) == NULL)
1223            {
1224                    if (errno == ENOENT) // file not exist
1225                    {
1226                            return 0;
1227                    }
1228                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1229                    return -1;
1230            }
1231    
1232            while (!feof(fp))
1233            {
1234                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1235                    {
1236                            if (ferror(fp) == 0)
1237                            {
1238                                    break;
1239                            }
1240                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1241                            ret = -2;
1242                            break;
1243                    }
1244    
1245                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1246                    {
1247                            if (ferror(fp) == 0)
1248                            {
1249                                    break;
1250                            }
1251                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1252                            ret = -2;
1253                            break;
1254                    }
1255    
1256                    p_section = section_list_find_by_sid(sid);
1257                    if (p_section == NULL)
1258                    {
1259                            continue; // skip section no longer exist
1260                    }
1261    
1262                    i = get_section_index(p_section);
1263                    if (i < 0)
1264                    {
1265                            log_error("get_section_index(sid=%d) error\n", sid);
1266                            ret = -3;
1267                            break;
1268                    }
1269                    section_aid_locations[i] = aid;
1270            }
1271    
1272            if (fclose(fp) < 0)
1273            {
1274                    log_error("fclose(%s) error: %d\n", filename, errno);
1275                    ret = -1;
1276            }
1277    
1278            return ret;
1279    }


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

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