/[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.54 by sysadm, Thu Oct 16 11:26:16 2025 UTC Revision 1.64 by sysadm, Mon Nov 3 06:43:55 2025 UTC
# Line 30  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 50  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,          SET_FAVOR_ARTICLE,
58          UNSET_FAVOR_ARTICLE,          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            SCAN_ARTICLE_BACKWARD_BY_USER,
63            SCAN_ARTICLE_FORWARD_BY_USER,
64          VIEW_EX_DIR,          VIEW_EX_DIR,
65          SHOW_TOP10,          SHOW_TOP10,
66            SEARCH_USER,
67  };  };
68    
69  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 262  static enum select_cmd_t section_list_se Line 269  static enum select_cmd_t section_list_se
269    
270                  switch (ch)                  switch (ch)
271                  {                  {
272                  case KEY_NULL:                   // broken pipe                  case KEY_NULL: // broken pipe
273                          log_error("KEY_NULL\n");                          log_error("KEY_NULL\n");
274                          return EXIT_SECTION;                          return EXIT_SECTION;
275                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
# Line 278  static enum select_cmd_t section_list_se Line 285  static enum select_cmd_t section_list_se
285                  case 'n':                  case 'n':
286                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
287                  case CR:                  case CR:
                         igetch_reset();  
288                  case 'r':                  case 'r':
289                  case KEY_RIGHT:                  case KEY_RIGHT:
290                          if (item_count > 0)                          if (item_count > 0)
# Line 306  static enum select_cmd_t section_list_se Line 312  static enum select_cmd_t section_list_se
312                                  return QUERY_ARTICLE;                                  return QUERY_ARTICLE;
313                          }                          }
314                          break;                          break;
315                    case Ctrl('A'):
316                            if (item_count > 0)
317                            {
318                                    return QUERY_USER;
319                            }
320                            break;
321                  case 'F':                  case 'F':
322                          if (item_count > 0)                          if (item_count > 0)
323                          {                          {
# Line 399  static enum select_cmd_t section_list_se Line 411  static enum select_cmd_t section_list_se
411                                  return SCAN_NEW_ARTICLE;                                  return SCAN_NEW_ARTICLE;
412                          }                          }
413                          break;                          break;
414                    case 'A':
415                            if (item_count > 0)
416                            {
417                                    return SCAN_ARTICLE_BACKWARD_BY_USER;
418                            }
419                            break;
420                    case 'a':
421                            if (item_count > 0)
422                            {
423                                    return SCAN_ARTICLE_FORWARD_BY_USER;
424                            }
425                            break;
426                    case 'u':
427                            return SEARCH_USER;
428                  case 'h':                  case 'h':
429                          return SHOW_HELP;                          return SHOW_HELP;
430                  case 'x':                  case 'x':
# Line 406  static enum select_cmd_t section_list_se Line 432  static enum select_cmd_t section_list_se
432                  case 'H':                  case 'H':
433                          return SHOW_TOP10;                          return SHOW_TOP10;
434                  default:                  default:
435                            break;
436                  }                  }
437    
438                  if (old_page_id != *p_page_id)                  if (old_page_id != *p_page_id)
# Line 549  int section_list_display(const char *sna Line 576  int section_list_display(const char *sna
576          ARTICLE article_new;          ARTICLE article_new;
577          int page_id_cur;          int page_id_cur;
578          const ARTICLE *p_article_locate;          const ARTICLE *p_article_locate;
579            USER_INFO user_info;
580            char user_intro[BBS_user_intro_max_len];
581            char username[BBS_username_max_len + 1];
582            char username_list[1][BBS_username_max_len + 1];
583            int32_t uid;
584            int i;
585            int ok;
586    
587          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
588          if (p_section == NULL)          if (p_section == NULL)
# Line 566  int section_list_display(const char *sna Line 600  int section_list_display(const char *sna
600    
601          section_index = get_section_index(p_section);          section_index = get_section_index(p_section);
602    
603          if ((ret = section_list_rd_lock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
604          {          {
605                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
606                  return -2;                  return -4;
         }  
   
         strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);  
         stitle[sizeof(stitle) - 1] = '\0';  
         strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);  
         master_list[sizeof(master_list) - 1] = '\0';  
   
         if ((ret = section_list_rd_unlock(p_section)) < 0)  
         {  
                 log_error("section_list_rd_unlock(sid = 0) error\n");  
                 return -2;  
607          }          }
608    
609          if (aid == 0)          if (aid == 0)
# Line 662  int section_list_display(const char *sna Line 685  int section_list_display(const char *sna
685                  }                  }
686    
687                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
688    
689                  switch (ret)                  switch (ret)
690                  {                  {
691                  case EXIT_SECTION:                  case EXIT_SECTION:
692                            // Update current aid location
693                            if (p_articles[selected_index] != NULL)
694                            {
695                                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
696                            }
697                            else
698                            {
699                                    log_error("p_articles[selected_index=%d] is NULL when exit section [%s]\n", selected_index, sname);
700                            }
701                          return 0;                          return 0;
702                  case CHANGE_PAGE:                  case CHANGE_PAGE:
703                          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 843  int section_list_display(const char *sna Line 876  int section_list_display(const char *sna
876                          // Update current topic                          // Update current topic
877                          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);
878    
                         // Update current aid location  
                         section_aid_locations[section_index] = p_articles[selected_index]->aid;  
   
879                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
880                          {                          {
881                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 943  int section_list_display(const char *sna Line 973  int section_list_display(const char *sna
973                                  return -2;                                  return -2;
974                          }                          }
975                          break;                          break;
976                    case QUERY_USER:
977                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info, user_intro, sizeof(user_intro))) < 0)
978                            {
979                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
980                                    return -2;
981                            }
982                            else if (ret == 0)
983                            {
984                                    clearscr();
985                                    prints("该用户已升天");
986                                    press_any_key();
987                            }
988                            else if (user_info_display(&user_info) < 0) // && ret > 0
989                            {
990                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
991                            }
992    
993                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
994                            {
995                                    log_error("section_list_draw_screen() error\n");
996                                    return -2;
997                            }
998                            break;
999                  case SET_FAVOR_ARTICLE:                  case SET_FAVOR_ARTICLE:
1000                          ret = article_favor_set(p_articles[selected_index]->tid == 0                          ret = article_favor_set(p_articles[selected_index]->tid == 0
1001                                                                                  ? p_articles[selected_index]->aid                                                                                  ? p_articles[selected_index]->aid
# Line 1018  int section_list_display(const char *sna Line 1071  int section_list_display(const char *sna
1071                                  }                                  }
1072                          }                          }
1073                          break;                          break;
1074                    case SCAN_ARTICLE_BACKWARD_BY_USER:
1075                    case SCAN_ARTICLE_FORWARD_BY_USER:
1076                            direction = (ret == SCAN_ARTICLE_FORWARD_BY_USER ? 1 : -1);
1077                            strncpy(username, p_articles[selected_index]->username, sizeof(username) - 1);
1078                            username[sizeof(username) - 1] = '\0';
1079                            uid = 0;
1080    
1081                            moveto(SCREEN_ROWS, 1);
1082                            clrtoeol();
1083                            get_data(SCREEN_ROWS, 1,
1084                                             (direction == 1 ? "向下搜寻作者: " : "向上搜寻作者: "),
1085                                             username, sizeof(username), BBS_username_max_len);
1086    
1087                            if (username[0] == '\0')
1088                            {
1089                                    break;
1090                            }
1091    
1092                            // Verify format
1093                            for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
1094                            {
1095                                    if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
1096                                    {
1097                                            ok = 0;
1098                                    }
1099                            }
1100                            if (ok && i > BBS_username_max_len)
1101                            {
1102                                    ok = 0;
1103                            }
1104                            if (!ok)
1105                            {
1106                                    break;
1107                            }
1108    
1109                            ret = query_user_info_by_username(username, 1, &uid, username_list);
1110                            if (ret < 0)
1111                            {
1112                                    log_error("query_user_info_by_username(%s) error\n", username);
1113                                    break;
1114                            }
1115    
1116                            if (uid > 0)
1117                            {
1118                                    ret = scan_article_in_section_by_uid(p_section, p_articles[selected_index],
1119                                                                                                             direction, uid, &p_article_locate);
1120                                    if (ret < 0)
1121                                    {
1122                                            log_error("scan_article_in_section_by_uid(sid=%d, aid=%d, direction=%d, uid=%d) error\n",
1123                                                              p_section->sid, p_articles[selected_index]->aid, direction, uid);
1124                                            return -3;
1125                                    }
1126                                    else if (ret == 0) // not found
1127                                    {
1128                                            break;
1129                                    }
1130                            }
1131                            else // uid == 0
1132                            {
1133                                    ret = scan_article_in_section_by_username(p_section, p_articles[selected_index],
1134                                                                                                                      direction, username, &p_article_locate);
1135                                    if (ret < 0)
1136                                    {
1137                                            log_error("scan_article_in_section_by_username(sid=%d, aid=%d, direction=%d, username=%s) error\n",
1138                                                              p_section->sid, p_articles[selected_index]->aid, direction, username);
1139                                            return -3;
1140                                    }
1141                                    else if (ret == 0) // not found
1142                                    {
1143                                            break;
1144                                    }
1145                            }
1146    
1147                            page_id_cur = page_id;
1148                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1149                                                                                            &page_id, &selected_index, &article_count);
1150                            if (ret < 0)
1151                            {
1152                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1153                                                      p_section->sid, p_article_locate->aid);
1154                                    return -3;
1155                            }
1156                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1157                            {
1158                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1159                                    if (ret < 0)
1160                                    {
1161                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1162                                            return -3;
1163                                    }
1164                            }
1165                            break;
1166                    case SEARCH_USER:
1167                            user_list_search();
1168                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1169                            {
1170                                    log_error("section_list_draw_screen() error\n");
1171                                    return -2;
1172                            }
1173                            break;
1174                  case SHOW_HELP:                  case SHOW_HELP:
1175                          // Display help information                          // Display help information
1176                          display_file(DATA_READ_HELP, 1);                          display_file(DATA_READ_HELP, 1);
# Line 1113  int section_list_ex_dir_display(SECTION_ Line 1266  int section_list_ex_dir_display(SECTION_
1266                                  }                                  }
1267                                  continue;                                  continue;
1268                          case CR:                          case CR:
                                 igetch_reset();  
1269                          default:                          default:
1270                                  switch (menu_control(&ex_menu_set, ch))                                  switch (menu_control(&ex_menu_set, ch))
1271                                  {                                  {
# Line 1143  int section_list_ex_dir_display(SECTION_ Line 1295  int section_list_ex_dir_display(SECTION_
1295    
1296          return 0;          return 0;
1297  }  }
1298    
1299    int section_aid_locations_save(int uid)
1300    {
1301            char filename[FILE_PATH_LEN];
1302            FILE *fp;
1303            int i;
1304            int ret = 0;
1305    
1306            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1307    
1308            if ((fp = fopen(filename, "wb")) == NULL)
1309            {
1310                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1311                    return -1;
1312            }
1313    
1314            for (i = 0; i < p_section_list_pool->section_count; i++)
1315            {
1316                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1317                    {
1318                            log_error("fwrite(%s, sid) error\n", filename);
1319                            ret = -2;
1320                            break;
1321                    }
1322    
1323                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1324                    {
1325                            log_error("fwrite(%s, aid) error\n", filename);
1326                            ret = -2;
1327                            break;
1328                    }
1329            }
1330    
1331            if (fclose(fp) < 0)
1332            {
1333                    log_error("fclose(%s) error: %d\n", filename, errno);
1334                    ret = -1;
1335            }
1336    
1337            return ret;
1338    }
1339    
1340    int section_aid_locations_load(int uid)
1341    {
1342            char filename[FILE_PATH_LEN];
1343            FILE *fp;
1344            int i;
1345            int32_t sid;
1346            int32_t aid;
1347            SECTION_LIST *p_section;
1348            int ret = 0;
1349    
1350            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1351    
1352            if ((fp = fopen(filename, "rb")) == NULL)
1353            {
1354                    if (errno == ENOENT) // file not exist
1355                    {
1356                            return 0;
1357                    }
1358                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1359                    return -1;
1360            }
1361    
1362            while (!feof(fp))
1363            {
1364                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1365                    {
1366                            if (ferror(fp) == 0)
1367                            {
1368                                    break;
1369                            }
1370                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1371                            ret = -2;
1372                            break;
1373                    }
1374    
1375                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1376                    {
1377                            if (ferror(fp) == 0)
1378                            {
1379                                    break;
1380                            }
1381                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1382                            ret = -2;
1383                            break;
1384                    }
1385    
1386                    p_section = section_list_find_by_sid(sid);
1387                    if (p_section == NULL)
1388                    {
1389                            continue; // skip section no longer exist
1390                    }
1391    
1392                    i = get_section_index(p_section);
1393                    if (i < 0)
1394                    {
1395                            log_error("get_section_index(sid=%d) error\n", sid);
1396                            ret = -3;
1397                            break;
1398                    }
1399                    section_aid_locations[i] = aid;
1400            }
1401    
1402            if (fclose(fp) < 0)
1403            {
1404                    log_error("fclose(%s) error: %d\n", filename, errno);
1405                    ret = -1;
1406            }
1407    
1408            return ret;
1409    }


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

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