/[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.51 by sysadm, Tue Oct 14 04:01:40 2025 UTC Revision 1.63 by sysadm, Mon Nov 3 02:32:11 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_list_display.h"
35  #include "user_priv.h"  #include "user_priv.h"
36    #include <errno.h>
37  #include <string.h>  #include <string.h>
38  #include <time.h>  #include <time.h>
39  #include <sys/param.h>  #include <sys/param.h>
40    
41  static int section_aid_locations[BBS_max_section] = {0};  static int32_t section_aid_locations[BBS_max_section] = {0};
42  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
43  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
44    
# Line 49  enum select_cmd_t Line 53  enum select_cmd_t
53          EDIT_ARTICLE,          EDIT_ARTICLE,
54          DELETE_ARTICLE,          DELETE_ARTICLE,
55          QUERY_ARTICLE,          QUERY_ARTICLE,
56            QUERY_USER,
57            SET_FAVOR_ARTICLE,
58            UNSET_FAVOR_ARTICLE,
59          FIRST_TOPIC_ARTICLE,          FIRST_TOPIC_ARTICLE,
60          LAST_TOPIC_ARTICLE,          LAST_TOPIC_ARTICLE,
61          SCAN_NEW_ARTICLE,          SCAN_NEW_ARTICLE,
62          VIEW_EX_DIR,          VIEW_EX_DIR,
63          SHOW_TOP10,          SHOW_TOP10,
64            SEARCH_USER,
65  };  };
66    
67  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 68  static int section_list_draw_items(int p Line 76  static int section_list_draw_items(int p
76          size_t j;          size_t j;
77          char article_flag;          char article_flag;
78          int is_viewed;          int is_viewed;
79            int is_favor;
80          time_t tm_now;          time_t tm_now;
81    
82          time(&tm_now);          time(&tm_now);
# Line 90  static int section_list_draw_items(int p Line 99  static int section_list_draw_items(int p
99                          }                          }
100                  }                  }
101    
102                    if (p_articles[i]->tid == 0)
103                    {
104                            is_favor = article_favor_check(p_articles[i]->aid, &BBS_article_favor);
105                            if (is_favor < 0)
106                            {
107                                    log_error("article_favor_check(aid=%d) error\n", p_articles[i]->aid);
108                                    is_favor = 0;
109                            }
110                    }
111                    else
112                    {
113                            is_favor = 0;
114                    }
115    
116                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
117                  {                  {
118                          article_flag = (is_viewed ? 'm' : 'M');                          article_flag = (is_viewed ? 'm' : 'M');
# Line 145  static int section_list_draw_items(int p Line 168  static int section_list_draw_items(int p
168                  moveto(4 + i, 1);                  moveto(4 + i, 1);
169                  if (i >= ontop_start_offset)                  if (i >= ontop_start_offset)
170                  {                  {
171                          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",
172                                       (is_favor ? '@' : ' '),
173                                     article_flag,                                     article_flag,
174                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
175                                     (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 185  static int section_list_draw_items(int p
185                  }                  }
186                  else                  else
187                  {                  {
188                          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",
189                                     (p_articles[i]->aid == section_topic_view_tid                                     (p_articles[i]->aid == section_topic_view_tid
190                                                  ? "\033[1;33m"                                                  ? "\033[1;33m"
191                                                  : (p_articles[i]->tid == section_topic_view_tid                                                  : (p_articles[i]->tid == section_topic_view_tid
192                                                             ? "\033[1;36m"                                                             ? "\033[1;36m"
193                                                             : "")),                                                             : "")),
194                                     p_articles[i]->aid,                                     p_articles[i]->aid,
195                                       (is_favor ? '@' : ' '),
196                                     article_flag,                                     article_flag,
197                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
198                                     (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 260  static enum select_cmd_t section_list_se
260          {          {
261                  ch = igetch(100);                  ch = igetch(100);
262    
263                  switch (ch)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
264                  {                  {
                 case KEY_ESC:  
                 case KEY_LEFT:  
265                          BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
266                  case KEY_NULL:                   // broken pipe                  }
267                          return EXIT_SECTION; // exit section  
268                    switch (ch)
269                    {
270                    case KEY_NULL: // broken pipe
271                            log_error("KEY_NULL\n");
272                            return EXIT_SECTION;
273                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
274                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
275                          {                          {
276                                  return EXIT_SECTION; // exit section                                  log_error("User input timeout\n");
277                                    return EXIT_SECTION;
278                          }                          }
279                          continue;                          continue;
280                    case KEY_ESC:
281                    case KEY_LEFT:
282                            return EXIT_SECTION;
283                  case 'n':                  case 'n':
                         BBS_last_access_tm = time(NULL);  
284                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
285                  case CR:                  case CR:
                         igetch_reset();  
286                  case 'r':                  case 'r':
287                  case KEY_RIGHT:                  case KEY_RIGHT:
288                          if (item_count > 0)                          if (item_count > 0)
289                          {                          {
                                 BBS_last_access_tm = time(NULL);  
290                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
291                          }                          }
292                          break;                          break;
# Line 281  static enum select_cmd_t section_list_se Line 310  static enum select_cmd_t section_list_se
310                                  return QUERY_ARTICLE;                                  return QUERY_ARTICLE;
311                          }                          }
312                          break;                          break;
313                    case Ctrl('A'):
314                            if (item_count > 0)
315                            {
316                                    return QUERY_USER;
317                            }
318                            break;
319                    case 'F':
320                            if (item_count > 0)
321                            {
322                                    return SET_FAVOR_ARTICLE;
323                            }
324                            break;
325                    case '-':
326                            if (item_count > 0)
327                            {
328                                    return UNSET_FAVOR_ARTICLE;
329                            }
330                            break;
331                  case KEY_HOME:                  case KEY_HOME:
332                          *p_page_id = 0;                          *p_page_id = 0;
333                  case 'P':                  case 'P':
# Line 362  static enum select_cmd_t section_list_se Line 409  static enum select_cmd_t section_list_se
409                                  return SCAN_NEW_ARTICLE;                                  return SCAN_NEW_ARTICLE;
410                          }                          }
411                          break;                          break;
412                    case 'u':
413                            return SEARCH_USER;
414                  case 'h':                  case 'h':
415                          return SHOW_HELP;                          return SHOW_HELP;
416                  case 'x':                  case 'x':
# Line 369  static enum select_cmd_t section_list_se Line 418  static enum select_cmd_t section_list_se
418                  case 'H':                  case 'H':
419                          return SHOW_TOP10;                          return SHOW_TOP10;
420                  default:                  default:
421                            break;
422                  }                  }
423    
424                  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 443  static enum select_cmd_t section_list_se
443                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
444                  }                  }
445    
                 BBS_last_access_tm = time(NULL);  
446                  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)
447                  {                  {
448                          return CHANGE_PAGE; // force section list refresh                          return CHANGE_PAGE; // force section list refresh
# Line 513  int section_list_display(const char *sna Line 562  int section_list_display(const char *sna
562          ARTICLE article_new;          ARTICLE article_new;
563          int page_id_cur;          int page_id_cur;
564          const ARTICLE *p_article_locate;          const ARTICLE *p_article_locate;
565            USER_INFO user_info;
566            char user_intro[BBS_user_intro_max_len];
567    
568          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
569          if (p_section == NULL)          if (p_section == NULL)
# Line 521  int section_list_display(const char *sna Line 572  int section_list_display(const char *sna
572                  return -1;                  return -1;
573          }          }
574    
575          section_index = get_section_index(p_section);          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
   
         if ((ret = section_list_rd_lock(p_section)) < 0)  
576          {          {
577                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
578                  return -2;                                    p_section->sid, BBS_priv.uid);
579                    return -1;
580          }          }
581    
582          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';  
583    
584          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
585          {          {
586                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
587                  return -2;                  return -4;
588          }          }
589    
590          if (aid == 0)          if (aid == 0)
# Line 619  int section_list_display(const char *sna Line 666  int section_list_display(const char *sna
666                  }                  }
667    
668                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
669    
670                  switch (ret)                  switch (ret)
671                  {                  {
672                  case EXIT_SECTION:                  case EXIT_SECTION:
673                            // Update current aid location
674                            if (p_articles[selected_index] != NULL)
675                            {
676                                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
677                            }
678                            else
679                            {
680                                    log_error("p_articles[selected_index=%d] is NULL when exit section [%s]\n", selected_index, sname);
681                            }
682                          return 0;                          return 0;
683                  case CHANGE_PAGE:                  case CHANGE_PAGE:
684                          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 800  int section_list_display(const char *sna Line 857  int section_list_display(const char *sna
857                          // Update current topic                          // Update current topic
858                          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);
859    
                         // Update current aid location  
                         section_aid_locations[section_index] = p_articles[selected_index]->aid;  
   
860                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
861                          {                          {
862                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 900  int section_list_display(const char *sna Line 954  int section_list_display(const char *sna
954                                  return -2;                                  return -2;
955                          }                          }
956                          break;                          break;
957                    case QUERY_USER:
958                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info, user_intro, sizeof(user_intro))) < 0)
959                            {
960                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
961                                    return -2;
962                            }
963                            else if (ret == 0)
964                            {
965                                    clearscr();
966                                    prints("该用户已升天");
967                                    press_any_key();
968                            }
969                            else if (user_info_display(&user_info) < 0) // && ret > 0
970                            {
971                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
972                            }
973    
974                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
975                            {
976                                    log_error("section_list_draw_screen() error\n");
977                                    return -2;
978                            }
979                            break;
980                    case SET_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, 1);
985                            if (ret < 0)
986                            {
987                                    log_error("article_favor_set(aid=%d, 1) error\n",
988                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
989                            }
990                            break;
991                    case UNSET_FAVOR_ARTICLE:
992                            ret = article_favor_set(p_articles[selected_index]->tid == 0
993                                                                                    ? p_articles[selected_index]->aid
994                                                                                    : p_articles[selected_index]->tid,
995                                                                            &BBS_article_favor, 0);
996                            if (ret < 0)
997                            {
998                                    log_error("article_favor_set(aid=%d, 0) error\n",
999                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
1000                            }
1001                            break;
1002                  case FIRST_TOPIC_ARTICLE:                  case FIRST_TOPIC_ARTICLE:
1003                  case LAST_TOPIC_ARTICLE:                  case LAST_TOPIC_ARTICLE:
1004                          page_id_cur = page_id;                          page_id_cur = page_id;
# Line 953  int section_list_display(const char *sna Line 1052  int section_list_display(const char *sna
1052                                  }                                  }
1053                          }                          }
1054                          break;                          break;
1055                    case SEARCH_USER:
1056                            user_list_search();
1057                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1058                            {
1059                                    log_error("section_list_draw_screen() error\n");
1060                                    return -2;
1061                            }
1062                            break;
1063                  case SHOW_HELP:                  case SHOW_HELP:
1064                          // Display help information                          // Display help information
1065                          display_file(DATA_READ_HELP, 1);                          display_file(DATA_READ_HELP, 1);
# Line 1029  int section_list_ex_dir_display(SECTION_ Line 1136  int section_list_ex_dir_display(SECTION_
1136                  {                  {
1137                          iflush();                          iflush();
1138                          ch = igetch(100);                          ch = igetch(100);
1139    
1140                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1141                            {
1142                                    BBS_last_access_tm = time(NULL);
1143                            }
1144    
1145                          switch (ch)                          switch (ch)
1146                          {                          {
1147                          case KEY_NULL: // broken pipe                          case KEY_NULL: // broken pipe
1148                                    log_error("KEY_NULL\n");
1149                                  return 0;                                  return 0;
1150                          case KEY_TIMEOUT:                          case KEY_TIMEOUT:
1151                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
1152                                  {                                  {
1153                                            log_error("User input timeout\n");
1154                                          return 0;                                          return 0;
1155                                  }                                  }
1156                                  continue;                                  continue;
1157                          case CR:                          case CR:
                                 igetch_reset();  
1158                          default:                          default:
1159                                  switch (menu_control(&ex_menu_set, ch))                                  switch (menu_control(&ex_menu_set, ch))
1160                                  {                                  {
# Line 1059  int section_list_ex_dir_display(SECTION_ Line 1173  int section_list_ex_dir_display(SECTION_
1173                                  }                                  }
1174                          }                          }
1175    
                         BBS_last_access_tm = time(NULL);  
   
1176                          if (ch == EXITMENU)                          if (ch == EXITMENU)
1177                          {                          {
1178                                  break;                                  break;
# Line 1072  int section_list_ex_dir_display(SECTION_ Line 1184  int section_list_ex_dir_display(SECTION_
1184    
1185          return 0;          return 0;
1186  }  }
1187    
1188    int section_aid_locations_save(int uid)
1189    {
1190            char filename[FILE_PATH_LEN];
1191            FILE *fp;
1192            int i;
1193            int ret = 0;
1194    
1195            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1196    
1197            if ((fp = fopen(filename, "wb")) == NULL)
1198            {
1199                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1200                    return -1;
1201            }
1202    
1203            for (i = 0; i < p_section_list_pool->section_count; i++)
1204            {
1205                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1206                    {
1207                            log_error("fwrite(%s, sid) error\n", filename);
1208                            ret = -2;
1209                            break;
1210                    }
1211    
1212                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1213                    {
1214                            log_error("fwrite(%s, aid) error\n", filename);
1215                            ret = -2;
1216                            break;
1217                    }
1218            }
1219    
1220            if (fclose(fp) < 0)
1221            {
1222                    log_error("fclose(%s) error: %d\n", filename, errno);
1223                    ret = -1;
1224            }
1225    
1226            return ret;
1227    }
1228    
1229    int section_aid_locations_load(int uid)
1230    {
1231            char filename[FILE_PATH_LEN];
1232            FILE *fp;
1233            int i;
1234            int32_t sid;
1235            int32_t aid;
1236            SECTION_LIST *p_section;
1237            int ret = 0;
1238    
1239            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1240    
1241            if ((fp = fopen(filename, "rb")) == NULL)
1242            {
1243                    if (errno == ENOENT) // file not exist
1244                    {
1245                            return 0;
1246                    }
1247                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1248                    return -1;
1249            }
1250    
1251            while (!feof(fp))
1252            {
1253                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1254                    {
1255                            if (ferror(fp) == 0)
1256                            {
1257                                    break;
1258                            }
1259                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1260                            ret = -2;
1261                            break;
1262                    }
1263    
1264                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1265                    {
1266                            if (ferror(fp) == 0)
1267                            {
1268                                    break;
1269                            }
1270                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1271                            ret = -2;
1272                            break;
1273                    }
1274    
1275                    p_section = section_list_find_by_sid(sid);
1276                    if (p_section == NULL)
1277                    {
1278                            continue; // skip section no longer exist
1279                    }
1280    
1281                    i = get_section_index(p_section);
1282                    if (i < 0)
1283                    {
1284                            log_error("get_section_index(sid=%d) error\n", sid);
1285                            ret = -3;
1286                            break;
1287                    }
1288                    section_aid_locations[i] = aid;
1289            }
1290    
1291            if (fclose(fp) < 0)
1292            {
1293                    log_error("fclose(%s) error: %d\n", filename, errno);
1294                    ret = -1;
1295            }
1296    
1297            return ret;
1298    }


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

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