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


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

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