/[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.20 by sysadm, Thu Jun 12 12:14:28 2025 UTC Revision 1.66 by sysadm, Mon Nov 3 07:14:25 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list_display.h"  
 #include "section_list_loader.h"  
17  #include "article_cache.h"  #include "article_cache.h"
18    #include "article_favor.h"
19    #include "article_op.h"
20  #include "article_post.h"  #include "article_post.h"
21    #include "article_view_log.h"
22    #include "article_del.h"
23  #include "common.h"  #include "common.h"
24  #include "io.h"  #include "io.h"
 #include "screen.h"  
25  #include "log.h"  #include "log.h"
26  #include "user_priv.h"  #include "login.h"
27  #include "article_view_log.h"  #include "menu.h"
28    #include "menu_proc.h"
29    #include "section_list_display.h"
30    #include "section_list_loader.h"
31    #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"
36    #include <ctype.h>
37    #include <errno.h>
38    #include <string.h>
39  #include <time.h>  #include <time.h>
40  #include <sys/param.h>  #include <sys/param.h>
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
41    
42    #define TITLE_SEARCH_MAX_LEN 60
43    
44    static int32_t section_aid_locations[BBS_max_section] = {0};
45  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
46  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
47    
48  enum select_cmd_t  enum select_cmd_t
49  {  {
50          EXIT_SECTION = 0,          EXIT_SECTION = 0,
51          VIEW_ARTICLE = 1,          VIEW_ARTICLE,
52          CHANGE_PAGE = 2,          CHANGE_PAGE,
53          REFRESH_SCREEN = 3,          SHOW_HELP,
54          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY,
55          POST_ARTICLE = 5,          POST_ARTICLE,
56          EDIT_ARTICLE = 6,          EDIT_ARTICLE,
57            DELETE_ARTICLE,
58            QUERY_ARTICLE,
59            QUERY_USER,
60            SET_FAVOR_ARTICLE,
61            UNSET_FAVOR_ARTICLE,
62            FIRST_TOPIC_ARTICLE,
63            LAST_TOPIC_ARTICLE,
64            SCAN_NEW_ARTICLE,
65            SCAN_ARTICLE_BACKWARD_BY_USER,
66            SCAN_ARTICLE_FORWARD_BY_USER,
67            SCAN_ARTICLE_BACKWARD_BY_TITLE,
68            SCAN_ARTICLE_FORWARD_BY_TITLE,
69            VIEW_EX_DIR,
70            SHOW_TOP10,
71            SEARCH_USER,
72  };  };
73    
74  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname, int ontop_start_offset)
75  {  {
76          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
77          struct tm tm_sub;          struct tm tm_sub;
# Line 53  static int section_list_draw_items(int p Line 80  static int section_list_draw_items(int p
80          int eol;          int eol;
81          int len;          int len;
82          int i;          int i;
83            size_t j;
84          char article_flag;          char article_flag;
85          int is_viewed;          int is_viewed;
86            int is_favor;
87          time_t tm_now;          time_t tm_now;
88    
89          time(&tm_now);          time(&tm_now);
# Line 77  static int section_list_draw_items(int p Line 106  static int section_list_draw_items(int p
106                          }                          }
107                  }                  }
108    
109                  article_flag = (is_viewed ? ' ' : 'N');                  if (p_articles[i]->tid == 0)
110                    {
111                            is_favor = article_favor_check(p_articles[i]->aid, &BBS_article_favor);
112                            if (is_favor < 0)
113                            {
114                                    log_error("article_favor_check(aid=%d) error\n", p_articles[i]->aid);
115                                    is_favor = 0;
116                            }
117                    }
118                    else
119                    {
120                            is_favor = 0;
121                    }
122    
123                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
124                  {                  {
# Line 87  static int section_list_draw_items(int p Line 128  static int section_list_draw_items(int p
128                  {                  {
129                          article_flag = 'x';                          article_flag = 'x';
130                  }                  }
131                    else
132                    {
133                            article_flag = (is_viewed ? ' ' : 'N');
134                    }
135    
136                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);
137                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)
# Line 98  static int section_list_draw_items(int p Line 143  static int section_list_draw_items(int p
143                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
144                  }                  }
145    
146                  strncpy(title_f, (p_articles[i]->tid == 0 ? " " : ""), sizeof(title_f) - 1);                  strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
147                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
148                  strncat(title_f, (p_articles[i]->transship ? "[ת]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
149                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));  
150                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);                  // Rewrite title with "Re: Re: " prefix into "Re: ... "
151                    j = 0;
152                    if (p_articles[i]->tid != 0)
153                    {
154                            while (strncmp(p_articles[i]->title + j, "Re: ", strlen("Re: ")) == 0)
155                            {
156                                    j += strlen("Re: ");
157                            }
158                            if (j >= strlen("Re: Re: "))
159                            {
160                                    strncat(title_f, "Re: ... ", sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
161                            }
162                            else
163                            {
164                                    j = 0;
165                            }
166                    }
167                    strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
168    
169                    len = split_line(title_f, 59 - (display_nickname ? BBS_nickname_max_len / 2 : BBS_username_max_len), &eol, &title_f_len, 1);
170                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
171                  {                  {
172                          title_f[len] = '\0';                          title_f[len] = '\0';
173                  }                  }
174    
175                  moveto(4 + i, 1);                  moveto(4 + i, 1);
176                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",                  if (i >= ontop_start_offset)
177                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),                  {
178                             p_articles[i]->aid,                          prints("   \033[1;33m[提示]\033[m%c%c %s%*s %s %s%s\033[m",
179                             article_flag,                                     (is_favor ? '@' : ' '),
180                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     article_flag,
181                             (display_nickname ? BBS_nickname_max_len - (int)strnlen(p_articles[i]->nickname, sizeof(p_articles[i]->nickname))                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
182                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
183                             "",                                                                           : BBS_username_max_len - str_length(p_articles[i]->username, 1)),
184                             str_time,                                     "",
185                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),                                     str_time,
186                             title_f);                                     (p_articles[i]->aid == section_topic_view_tid
187                                                    ? "\033[1;33m"
188                                                    : (p_articles[i]->tid == section_topic_view_tid
189                                                               ? "\033[1;36m"
190                                                               : "")),
191                                       title_f);
192                    }
193                    else
194                    {
195                            prints("  %s%7d\033[m%c%c %s%*s %s %s%s\033[m",
196                                       (p_articles[i]->aid == section_topic_view_tid
197                                                    ? "\033[1;33m"
198                                                    : (p_articles[i]->tid == section_topic_view_tid
199                                                               ? "\033[1;36m"
200                                                               : "")),
201                                       p_articles[i]->aid,
202                                       (is_favor ? '@' : ' '),
203                                       article_flag,
204                                       (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
205                                       (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
206                                                                             : BBS_username_max_len - str_length(p_articles[i]->username, 1)),
207                                       "",
208                                       str_time,
209                                       (p_articles[i]->aid == section_topic_view_tid
210                                                    ? "\033[1;33m"
211                                                    : (p_articles[i]->tid == section_topic_view_tid
212                                                               ? "\033[1;36m"
213                                                               : "")),
214                                       title_f);
215                    }
216          }          }
217    
218          return 0;          return 0;
# Line 127  static int section_list_draw_items(int p Line 220  static int section_list_draw_items(int p
220    
221  static int section_list_draw_screen(const char *sname, const char *stitle, const char *master_list, int display_nickname)  static int section_list_draw_screen(const char *sname, const char *stitle, const char *master_list, int display_nickname)
222  {  {
223          char str_section_master[LINE_BUFFER_LEN] = "";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
224          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
225    
226          if (master_list[0] != '\0')          if (master_list[0] != '\0')
227          {          {
228                  snprintf(str_section_master, sizeof(str_section_master), "%s", master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
229          }          }
230          snprintf(str_section_name, sizeof(str_section_name), " [%s]", sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
231    
232          clearscr();          clearscr();
233          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
234          moveto(2, 0);          moveto(2, 0);
235          prints("[\033[1;32m\033[0;37m,\033[1;32mESC\033[0;37m] ѡ[\033[1;32m\033[0;37m,\033[1;32m\033[0;37m] "          prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] "
236                     "Ķ[\033[1;32m\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 发表[\033[1;32mCtrl-P\033[0;37m] "
237                     (display_nickname ? "ʾû" : "ʾdz"));                     "%s[\033[1;32mn\033[0;37m] 精华区[\033[1;32mx\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
238                       (display_nickname ? "用户名" : "昵称"));
239          moveto(3, 0);          moveto(3, 0);
240          if (display_nickname)          if (display_nickname)
241          {          {
242                  prints("\033[44;37m  \033[1;37m     dz              ±                               \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
243          }          }
244          else          else
245          {          {
246                  prints("\033[44;37m  \033[1;37m            ±                                       \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
247          }          }
248    
249          return 0;          return 0;
# Line 160  static enum select_cmd_t section_list_se Line 254  static enum select_cmd_t section_list_se
254          int old_page_id = *p_page_id;          int old_page_id = *p_page_id;
255          int old_selected_index = *p_selected_index;          int old_selected_index = *p_selected_index;
256          int ch;          int ch;
257            time_t last_refresh_tm = time(NULL);
         BBS_last_access_tm = time(0);  
258    
259          if (item_count > 0 && *p_selected_index >= 0)          if (item_count > 0 && *p_selected_index >= 0)
260          {          {
# Line 174  static enum select_cmd_t section_list_se Line 267  static enum select_cmd_t section_list_se
267          {          {
268                  ch = igetch(100);                  ch = igetch(100);
269    
270                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
271                    {
272                            BBS_last_access_tm = time(NULL);
273                    }
274    
275                  switch (ch)                  switch (ch)
276                  {                  {
277                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
278                  case KEY_ESC:                          log_error("KEY_NULL\n");
279                  case KEY_LEFT:                          return EXIT_SECTION;
                         return EXIT_SECTION; // exit section  
280                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
281                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
282                          {                          {
283                                  return EXIT_SECTION; // exit section                                  log_error("User input timeout\n");
284                                    return EXIT_SECTION;
285                          }                          }
286                          continue;                          continue;
287                    case KEY_ESC:
288                    case KEY_LEFT:
289                            return EXIT_SECTION;
290                  case 'n':                  case 'n':
291                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
292                  case CR:                  case CR:
293                          igetch_reset();                  case 'r':
294                  case KEY_RIGHT:                  case KEY_RIGHT:
295                          if (item_count > 0)                          if (item_count > 0)
296                          {                          {
# Line 199  static enum select_cmd_t section_list_se Line 300  static enum select_cmd_t section_list_se
300                  case Ctrl('P'):                  case Ctrl('P'):
301                          return POST_ARTICLE;                          return POST_ARTICLE;
302                  case 'E':                  case 'E':
303                          return EDIT_ARTICLE;                          if (item_count > 0)
304                            {
305                                    return EDIT_ARTICLE;
306                            }
307                            break;
308                    case 'd':
309                            if (item_count > 0)
310                            {
311                                    return DELETE_ARTICLE;
312                            }
313                            break;
314                    case Ctrl('Q'):
315                            if (item_count > 0)
316                            {
317                                    return QUERY_ARTICLE;
318                            }
319                            break;
320                    case Ctrl('A'):
321                            if (item_count > 0)
322                            {
323                                    return QUERY_USER;
324                            }
325                            break;
326                    case 'F':
327                            if (item_count > 0)
328                            {
329                                    return SET_FAVOR_ARTICLE;
330                            }
331                            break;
332                    case '-':
333                            if (item_count > 0)
334                            {
335                                    return UNSET_FAVOR_ARTICLE;
336                            }
337                            break;
338                  case KEY_HOME:                  case KEY_HOME:
339                          *p_page_id = 0;                          *p_page_id = 0;
340                    case 'P':
341                  case KEY_PGUP:                  case KEY_PGUP:
342                          *p_selected_index = 0;                          *p_selected_index = 0;
343                    case 'k':
344                  case KEY_UP:                  case KEY_UP:
345                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
346                          {                          {
# Line 212  static enum select_cmd_t section_list_se Line 349  static enum select_cmd_t section_list_se
349                                          (*p_page_id)--;                                          (*p_page_id)--;
350                                          *p_selected_index = BBS_article_limit_per_page - 1;                                          *p_selected_index = BBS_article_limit_per_page - 1;
351                                  }                                  }
352                                    else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of section list
353                                    {
354                                            if (total_page > 0)
355                                            {
356                                                    *p_page_id = total_page - 1;
357                                            }
358                                            if (item_count > 0)
359                                            {
360                                                    *p_selected_index = item_count - 1;
361                                            }
362                                    }
363                          }                          }
364                          else                          else
365                          {                          {
366                                  (*p_selected_index)--;                                  (*p_selected_index)--;
367                          }                          }
368                          break;                          break;
369                    case '$':
370                  case KEY_END:                  case KEY_END:
371                          if (total_page > 0)                          if (total_page > 0)
372                          {                          {
373                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
374                          }                          }
375                    case 'N':
376                  case KEY_PGDN:                  case KEY_PGDN:
377                          if (item_count > 0)                          if (item_count > 0)
378                          {                          {
379                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
380                          }                          }
381                    case 'j':
382                  case KEY_DOWN:                  case KEY_DOWN:
383                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
384                          {                          {
# Line 236  static enum select_cmd_t section_list_se Line 387  static enum select_cmd_t section_list_se
387                                          (*p_page_id)++;                                          (*p_page_id)++;
388                                          *p_selected_index = 0;                                          *p_selected_index = 0;
389                                  }                                  }
390                                    else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of section list
391                                    {
392                                            *p_page_id = 0;
393                                            *p_selected_index = 0;
394                                    }
395                          }                          }
396                          else                          else
397                          {                          {
398                                  (*p_selected_index)++;                                  (*p_selected_index)++;
399                          }                          }
400                          break;                          break;
401                    case '=':
402                            if (item_count > 0)
403                            {
404                                    return FIRST_TOPIC_ARTICLE;
405                            }
406                            break;
407                    case '\\':
408                            if (item_count > 0)
409                            {
410                                    return LAST_TOPIC_ARTICLE;
411                            }
412                            break;
413                    case 'S':
414                            if (item_count > 0)
415                            {
416                                    return SCAN_NEW_ARTICLE;
417                            }
418                            break;
419                    case 'A':
420                            if (item_count > 0)
421                            {
422                                    return SCAN_ARTICLE_BACKWARD_BY_USER;
423                            }
424                            break;
425                    case 'a':
426                            if (item_count > 0)
427                            {
428                                    return SCAN_ARTICLE_FORWARD_BY_USER;
429                            }
430                            break;
431                    case '?':
432                            if (item_count > 0)
433                            {
434                                    return SCAN_ARTICLE_BACKWARD_BY_TITLE;
435                            }
436                            break;
437                    case '/':
438                            if (item_count > 0)
439                            {
440                                    return SCAN_ARTICLE_FORWARD_BY_TITLE;
441                            }
442                            break;
443                    case 'u':
444                            return SEARCH_USER;
445                    case 'h':
446                            return SHOW_HELP;
447                    case 'x':
448                            return VIEW_EX_DIR;
449                    case 'H':
450                            return SHOW_TOP10;
451                  default:                  default:
452                            break;
453                  }                  }
454    
455                  if (old_page_id != *p_page_id)                  if (old_page_id != *p_page_id)
# Line 267  static enum select_cmd_t section_list_se Line 474  static enum select_cmd_t section_list_se
474                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
475                  }                  }
476    
477                  BBS_last_access_tm = time(0);                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
478                    {
479                            return CHANGE_PAGE; // force section list refresh
480                    }
481          }          }
482    
483          return EXIT_SECTION;          return EXIT_SECTION;
# Line 278  static int display_article_key_handler(i Line 488  static int display_article_key_handler(i
488          switch (*p_key)          switch (*p_key)
489          {          {
490          case 'p':          case 'p':
491            case Ctrl('X'):
492                  section_topic_view_mode = !section_topic_view_mode;                  section_topic_view_mode = !section_topic_view_mode;
493          case 0: // Set msg          case 0: // Set msg
494                  if (section_topic_view_mode)                  if (section_topic_view_mode)
495                  {                  {
496                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
497                                           "| [\033[32m\033[33m,\033[32mESC\033[33m] | "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
498                                           "ͬĶ[\033[32m\033[33m/\033[32m\033[33m] | "                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
499                                           "ģʽ[\033[32mp\033[33m] | [\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
500                  }                  }
501                  else                  else
502                  {                  {
503                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
504                                           "| [\033[32m\033[33m,\033[32mESC\033[33m] | "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
505                                           "ƶ[\033[32m\033[33m/\033[32m\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "                                           "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] "
506                                           "ģʽ[\033[32mp\033[33m] | [\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
507                  }                  }
508                  *p_key = 0;                  *p_key = 0;
509                  break;                  break;
510          case 'r': // Reply article          case 'r': // Reply article
511                  return 1;                  return 1;
512            case '=': // First topic article
513                    return 1;
514            case '\\': // Last topic article
515                    return 1;
516          case KEY_UP:          case KEY_UP:
517          case KEY_PGUP:          case KEY_PGUP:
518          case KEY_HOME:          case KEY_HOME:
# Line 314  static int display_article_key_handler(i Line 529  static int display_article_key_handler(i
529                          return 1;                          return 1;
530                  }                  }
531                  break;                  break;
532            case 'k':
533                    if (section_topic_view_mode)
534                    {
535                            *p_key = KEY_PGUP;
536                    }
537                    else
538                    {
539                            *p_key = KEY_UP;
540                    }
541                    return 1;
542          case KEY_DOWN:          case KEY_DOWN:
543          case KEY_PGDN:          case KEY_PGDN:
544          case KEY_END:          case KEY_END:
# Line 330  static int display_article_key_handler(i Line 555  static int display_article_key_handler(i
555                          return 1;                          return 1;
556                  }                  }
557                  break;                  break;
558            case 'j':
559                    if (section_topic_view_mode)
560                    {
561                            *p_key = KEY_PGDN;
562                    }
563                    else
564                    {
565                            *p_key = KEY_DOWN;
566                    }
567                    return 1;
568          }          }
569    
570          return 0;          return 0;
571  }  }
572    
573  int section_list_display(const char *sname)  int section_list_display(const char *sname, int32_t aid)
574  {  {
575          static int display_nickname = 0;          static int display_nickname = 0;
576    
577          SECTION_LIST *p_section;          SECTION_LIST *p_section;
578            int64_t section_index;
579            int32_t aid_location;
580          char stitle[BBS_section_title_max_len + 1];          char stitle[BBS_section_title_max_len + 1];
581          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
582          char page_info_str[LINE_BUFFER_LEN];          char page_info_str[LINE_BUFFER_LEN];
583          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
584          int article_count;          int article_count;
585          int page_count;          int page_count;
586            int ontop_start_offset;
587          int page_id = 0;          int page_id = 0;
588          int selected_index = 0;          int selected_index = 0;
589          ARTICLE_CACHE cache;          ARTICLE_CACHE cache;
590          int ret;          int ret;
591          int loop;          int loop;
592          int direction;          int direction;
593            ARTICLE article_new;
594            int page_id_cur;
595            const ARTICLE *p_article_locate;
596            USER_INFO user_info;
597            char user_intro[BBS_user_intro_max_len + 1];
598            char username[BBS_username_max_len + 1];
599            char username_list[1][BBS_username_max_len + 1];
600            int32_t uid;
601            int i;
602            int ok;
603            char title[BBS_article_title_max_len + 1] = "\0";
604    
605          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
606          if (p_section == NULL)          if (p_section == NULL)
# Line 360  int section_list_display(const char *sna Line 609  int section_list_display(const char *sna
609                  return -1;                  return -1;
610          }          }
611    
612          if ((ret = section_list_rd_lock(p_section)) < 0)          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
613          {          {
614                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
615                  return -2;                                    p_section->sid, BBS_priv.uid);
616                    return -1;
617          }          }
618    
619          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';  
620    
621          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
622          {          {
623                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
624                  return -2;                  return -4;
625            }
626    
627            if (aid == 0)
628            {
629                    aid_location = section_aid_locations[section_index];
630            }
631            else
632            {
633                    aid_location = aid;
634            }
635    
636            // Locate at article with aid_locate
637            if (aid_location > 0)
638            {
639                    p_article_locate = article_block_find_by_aid(aid_location);
640                    if (p_article_locate == NULL)
641                    {
642                            log_error("article_block_find_by_aid(%d) error\n", aid_location);
643                            return -3;
644                    }
645    
646                    ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
647                                                                                    &page_id, &selected_index, &article_count);
648                    if (ret < 0)
649                    {
650                            log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
651                                              p_section->sid, p_article_locate->aid);
652                            return -3;
653                    }
654          }          }
655    
656          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
# Line 383  int section_list_display(const char *sna Line 659  int section_list_display(const char *sna
659                  return -2;                  return -2;
660          }          }
661    
662          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
663          if (ret < 0)          if (ret < 0)
664          {          {
665                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
666                  return -3;                  return -3;
667          }          }
668    
669            section_topic_view_tid = -1;
670    
671          if (article_count == 0) // empty section          if (article_count == 0) // empty section
672          {          {
673                  selected_index = 0;                  selected_index = 0;
674          }          }
675            else if (aid > 0)
676            {
677                    // Update current topic
678                    section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
679    
680                    // Update current aid location
681                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
682            }
683    
684          while (!SYS_server_exit)          while (!SYS_server_exit)
685          {          {
686                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname);                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname, ontop_start_offset);
687                  if (ret < 0)                  if (ret < 0)
688                  {                  {
689                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
# Line 405  int section_list_display(const char *sna Line 691  int section_list_display(const char *sna
691                  }                  }
692    
693                  snprintf(page_info_str, sizeof(page_info_str),                  snprintf(page_info_str, sizeof(page_info_str),
694                                   "\033[33m[\033[36m%d\033[33m/\033[36m%d\033[33mҳ]",                                   "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
695                                   page_id + 1, MAX(page_count, 1));                                   page_id + 1, MAX(page_count, 1));
696    
697                  show_bottom(page_info_str);                  show_bottom(page_info_str);
698                  iflush();                  iflush();
699    
700                    if (user_online_update(sname) < 0)
701                    {
702                            log_error("user_online_update(%s) error\n", sname);
703                    }
704    
705                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
706    
707                  switch (ret)                  switch (ret)
708                  {                  {
709                  case EXIT_SECTION:                  case EXIT_SECTION:
710                            // Update current aid location
711                            if (p_articles[selected_index] != NULL)
712                            {
713                                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
714                            }
715                            else
716                            {
717                                    log_error("p_articles[selected_index=%d] is NULL when exit section [%s]\n", selected_index, sname);
718                            }
719                          return 0;                          return 0;
720                  case CHANGE_PAGE:                  case CHANGE_PAGE:
721                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
722                          if (ret < 0)                          if (ret < 0)
723                          {                          {
724                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
# Line 443  int section_list_display(const char *sna Line 744  int section_list_display(const char *sna
744                                          break;                                          break;
745                                  }                                  }
746    
747                                    if (user_online_update("VIEW_ARTICLE") < 0)
748                                    {
749                                            log_error("user_online_update(VIEW_ARTICLE) error\n");
750                                    }
751    
752                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
753                                                                     display_article_key_handler, DATA_READ_HELP);                                                                     display_article_key_handler, DATA_READ_HELP);
754    
# Line 468  int section_list_display(const char *sna Line 774  int section_list_display(const char *sna
774                                                          page_id--;                                                          page_id--;
775                                                          selected_index = BBS_article_limit_per_page - 1;                                                          selected_index = BBS_article_limit_per_page - 1;
776    
777                                                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);                                                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
778                                                          if (ret < 0)                                                          if (ret < 0)
779                                                          {                                                          {
780                                                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
# Line 499  int section_list_display(const char *sna Line 805  int section_list_display(const char *sna
805                                                          page_id++;                                                          page_id++;
806                                                          selected_index = 0;                                                          selected_index = 0;
807    
808                                                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);                                                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
809                                                          if (ret < 0)                                                          if (ret < 0)
810                                                          {                                                          {
811                                                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
# Line 525  int section_list_display(const char *sna Line 831  int section_list_display(const char *sna
831                                  case KEY_PGUP:                                  case KEY_PGUP:
832                                  case KEY_PGDN:                                  case KEY_PGDN:
833                                          direction = (ret == KEY_PGUP ? -1 : 1);                                          direction = (ret == KEY_PGUP ? -1 : 1);
834                                          ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);                                          ret = locate_article_in_section(p_section, p_articles[selected_index], direction, 1,
835                                                                                                            &page_id, &selected_index, &article_count);
836                                          if (ret < 0)                                          if (ret < 0)
837                                          {                                          {
838                                                  log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",                                                  log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=1) error\n",
839                                                                    p_section->sid, p_articles[selected_index]->aid, direction);                                                                    p_section->sid, p_articles[selected_index]->aid, direction);
840                                                  return -3;                                                  return -3;
841                                          }                                          }
842                                          else if (ret > 0)                                          else if (ret > 0) // found
843                                          {                                          {
844                                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);                                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
845                                                  if (ret < 0)                                                  if (ret < 0)
846                                                  {                                                  {
847                                                          log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                                          log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
# Line 544  int section_list_display(const char *sna Line 851  int section_list_display(const char *sna
851                                          }                                          }
852                                          break;                                          break;
853                                  case 'r': // Reply article                                  case 'r': // Reply article
854                                          if (article_post(p_section, p_articles[selected_index], ARTICLE_POST_REPLY) < 0)                                          if (user_online_update("REPLY_ARTICLE") < 0)
855                                          {                                          {
856                                                  log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);                                                  log_error("user_online_update(REPLY_ARTICLE) error\n");
857                                            }
858    
859                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
860                                            {
861                                                    log_error("article_reply(aid=%d) error\n", p_articles[selected_index]->aid);
862                                          }                                          }
863                                          loop = 1;                                          loop = 1;
864                                          break;                                          break;
865                                    case '=':  // First topic article
866                                    case '\\': // Last topic article
867                                            page_id_cur = page_id;
868                                            direction = (ret == '=' ? -1 : 1);
869                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
870                                                                                                            &page_id, &selected_index, &article_count);
871                                            if (ret < 0)
872                                            {
873                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
874                                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
875                                                    return -3;
876                                            }
877                                            else if (ret > 0) // found
878                                            {
879                                                    if (page_id != page_id_cur) // page changed
880                                                    {
881                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
882                                                            if (ret < 0)
883                                                            {
884                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
885                                                                    return -3;
886                                                            }
887                                                    }
888                                                    loop = 1;
889                                            }
890                                            break;
891                                  }                                  }
892                          } while (loop);                          } while (loop);
893    
894                          // Update current topic                          // Update current topic
895                          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);
896                  case REFRESH_SCREEN:  
897                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
898                          {                          {
899                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 571  int section_list_display(const char *sna Line 909  int section_list_display(const char *sna
909                          }                          }
910                          break;                          break;
911                  case POST_ARTICLE:                  case POST_ARTICLE:
912                          if (article_post(p_section, NULL, ARTICLE_POST_NEW) < 0)                          if (user_online_update("POST_ARTICLE") < 0)
913                          {                          {
914                                  log_error("article_post(sid=%d, NEW) error\n", p_section->sid);                                  log_error("user_online_update(POST_ARTICLE) error\n");
915                            }
916    
917                            if ((ret = article_post(p_section, &article_new)) < 0)
918                            {
919                                    log_error("article_post(sid=%d) error\n", p_section->sid);
920                            }
921                            else if (ret > 0) // New article posted
922                            {
923                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
924                                    if (ret < 0)
925                                    {
926                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
927                                            return -3;
928                                    }
929                          }                          }
930                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
931                          {                          {
# Line 582  int section_list_display(const char *sna Line 934  int section_list_display(const char *sna
934                          }                          }
935                          break;                          break;
936                  case EDIT_ARTICLE:                  case EDIT_ARTICLE:
937                          if (p_articles[selected_index]->uid != BBS_priv.uid)                          if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
938                                    p_articles[selected_index]->uid != BBS_priv.uid)
939                            {
940                                    break; // No permission
941                            }
942    
943                            if (user_online_update("EDIT_ARTICLE") < 0)
944                            {
945                                    log_error("user_online_update() error\n");
946                            }
947    
948                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
949                            {
950                                    log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
951                            }
952                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
953                            {
954                                    log_error("section_list_draw_screen() error\n");
955                                    return -2;
956                            }
957                            break;
958                    case DELETE_ARTICLE:
959                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
960                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
961                            {
962                                    break; // No permission
963                            }
964                            if ((ret = article_del(p_section, p_articles[selected_index])) < 0)
965                            {
966                                    log_error("article_del(aid=%d) error\n", p_articles[selected_index]->aid);
967                            }
968                            else if (ret > 0) // Article deleted
969                            {
970                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
971                                    if (ret < 0)
972                                    {
973                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
974                                            return -3;
975                                    }
976                            }
977                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
978                            {
979                                    log_error("section_list_draw_screen() error\n");
980                                    return -2;
981                            }
982                            break;
983                    case QUERY_ARTICLE:
984                            if ((ret = display_article_meta(p_articles[selected_index]->aid)) < 0)
985                            {
986                                    log_error("display_article_meta(aid=%d) error\n", p_articles[selected_index]->aid);
987                            }
988                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
989                            {
990                                    log_error("section_list_draw_screen() error\n");
991                                    return -2;
992                            }
993                            break;
994                    case QUERY_USER:
995                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info, user_intro, sizeof(user_intro))) < 0)
996                            {
997                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
998                                    return -2;
999                            }
1000                            else if (ret == 0)
1001                            {
1002                                    clearscr();
1003                                    prints("该用户已升天");
1004                                    press_any_key();
1005                            }
1006                            else if (user_info_display(&user_info) < 0) // && ret > 0
1007                            {
1008                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
1009                            }
1010    
1011                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1012                            {
1013                                    log_error("section_list_draw_screen() error\n");
1014                                    return -2;
1015                            }
1016                            break;
1017                    case SET_FAVOR_ARTICLE:
1018                            ret = article_favor_set(p_articles[selected_index]->tid == 0
1019                                                                                    ? p_articles[selected_index]->aid
1020                                                                                    : p_articles[selected_index]->tid,
1021                                                                            &BBS_article_favor, 1);
1022                            if (ret < 0)
1023                            {
1024                                    log_error("article_favor_set(aid=%d, 1) error\n",
1025                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
1026                            }
1027                            break;
1028                    case UNSET_FAVOR_ARTICLE:
1029                            ret = article_favor_set(p_articles[selected_index]->tid == 0
1030                                                                                    ? p_articles[selected_index]->aid
1031                                                                                    : p_articles[selected_index]->tid,
1032                                                                            &BBS_article_favor, 0);
1033                            if (ret < 0)
1034                            {
1035                                    log_error("article_favor_set(aid=%d, 0) error\n",
1036                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
1037                            }
1038                            break;
1039                    case FIRST_TOPIC_ARTICLE:
1040                    case LAST_TOPIC_ARTICLE:
1041                            page_id_cur = page_id;
1042                            direction = (ret == FIRST_TOPIC_ARTICLE ? -1 : 1);
1043                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
1044                                                                                            &page_id, &selected_index, &article_count);
1045                            if (ret < 0)
1046                            {
1047                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
1048                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
1049                                    return -3;
1050                            }
1051                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1052                            {
1053                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1054                                    if (ret < 0)
1055                                    {
1056                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1057                                            return -3;
1058                                    }
1059                            }
1060                            break;
1061                    case SCAN_NEW_ARTICLE:
1062                            ret = scan_unread_article_in_section(p_section, p_articles[selected_index], &p_article_locate);
1063                            if (ret < 0)
1064                            {
1065                                    log_error("scan_unread_article_in_section(sid=%d, aid=%d) error\n",
1066                                                      p_section->sid, p_articles[selected_index]->aid);
1067                                    return -3;
1068                            }
1069                            else if (ret == 0) // not found
1070                            {
1071                                    break;
1072                            }
1073                            page_id_cur = page_id;
1074                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1075                                                                                            &page_id, &selected_index, &article_count);
1076                            if (ret < 0)
1077                            {
1078                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1079                                                      p_section->sid, p_article_locate->aid);
1080                                    return -3;
1081                            }
1082                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1083                            {
1084                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1085                                    if (ret < 0)
1086                                    {
1087                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1088                                            return -3;
1089                                    }
1090                            }
1091                            break;
1092                    case SCAN_ARTICLE_BACKWARD_BY_USER:
1093                    case SCAN_ARTICLE_FORWARD_BY_USER:
1094                            direction = (ret == SCAN_ARTICLE_FORWARD_BY_USER ? 1 : -1);
1095                            strncpy(username, p_articles[selected_index]->username, sizeof(username) - 1);
1096                            username[sizeof(username) - 1] = '\0';
1097                            uid = 0;
1098    
1099                            moveto(SCREEN_ROWS, 1);
1100                            clrtoeol();
1101                            get_data(SCREEN_ROWS, 1,
1102                                             (direction == 1 ? "向下搜寻作者: " : "向上搜寻作者: "),
1103                                             username, sizeof(username), BBS_username_max_len);
1104    
1105                            if (username[0] == '\0')
1106                          {                          {
1107                                  break;                                  break;
1108                          }                          }
1109                          if (article_post(p_section, p_articles[selected_index], ARTICLE_POST_EDIT) < 0)  
1110                            // Verify format
1111                            for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
1112                            {
1113                                    if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
1114                                    {
1115                                            ok = 0;
1116                                    }
1117                            }
1118                            if (ok && i > BBS_username_max_len)
1119                            {
1120                                    ok = 0;
1121                            }
1122                            if (!ok)
1123                            {
1124                                    break;
1125                            }
1126    
1127                            ret = query_user_info_by_username(username, 1, &uid, username_list);
1128                            if (ret < 0)
1129                            {
1130                                    log_error("query_user_info_by_username(%s) error\n", username);
1131                                    break;
1132                            }
1133    
1134                            if (uid > 0)
1135                            {
1136                                    ret = scan_article_in_section_by_uid(p_section, p_articles[selected_index],
1137                                                                                                             direction, uid, &p_article_locate);
1138                                    if (ret < 0)
1139                                    {
1140                                            log_error("scan_article_in_section_by_uid(sid=%d, aid=%d, direction=%d, uid=%d) error\n",
1141                                                              p_section->sid, p_articles[selected_index]->aid, direction, uid);
1142                                            return -3;
1143                                    }
1144                                    else if (ret == 0) // not found
1145                                    {
1146                                            break;
1147                                    }
1148                            }
1149                            else // uid == 0
1150                            {
1151                                    ret = scan_article_in_section_by_username(p_section, p_articles[selected_index],
1152                                                                                                                      direction, username, &p_article_locate);
1153                                    if (ret < 0)
1154                                    {
1155                                            log_error("scan_article_in_section_by_username(sid=%d, aid=%d, direction=%d, username=%s) error\n",
1156                                                              p_section->sid, p_articles[selected_index]->aid, direction, username);
1157                                            return -3;
1158                                    }
1159                                    else if (ret == 0) // not found
1160                                    {
1161                                            break;
1162                                    }
1163                            }
1164    
1165                            page_id_cur = page_id;
1166                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1167                                                                                            &page_id, &selected_index, &article_count);
1168                            if (ret < 0)
1169                            {
1170                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1171                                                      p_section->sid, p_article_locate->aid);
1172                                    return -3;
1173                            }
1174                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1175                            {
1176                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1177                                    if (ret < 0)
1178                                    {
1179                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1180                                            return -3;
1181                                    }
1182                            }
1183                            break;
1184                    case SCAN_ARTICLE_BACKWARD_BY_TITLE:
1185                    case SCAN_ARTICLE_FORWARD_BY_TITLE:
1186                            direction = (ret == SCAN_ARTICLE_FORWARD_BY_TITLE ? 1 : -1);
1187    
1188                            moveto(SCREEN_ROWS, 1);
1189                            clrtoeol();
1190                            get_data(SCREEN_ROWS, 1,
1191                                             (direction == 1 ? "向下搜寻标题: " : "向上搜寻标题: "),
1192                                             title, sizeof(title), TITLE_SEARCH_MAX_LEN);
1193    
1194                            if (title[0] == '\0')
1195                            {
1196                                    break;
1197                            }
1198    
1199                            ret = scan_article_in_section_by_title(p_section, p_articles[selected_index],
1200                                                                                                       direction, title, &p_article_locate);
1201                            if (ret < 0)
1202                            {
1203                                    log_error("scan_article_in_section_by_title(sid=%d, aid=%d, direction=%d, title=%s) error\n",
1204                                                      p_section->sid, p_articles[selected_index]->aid, direction, title);
1205                                    return -3;
1206                            }
1207                            else if (ret == 0) // not found
1208                            {
1209                                    break;
1210                            }
1211    
1212                            page_id_cur = page_id;
1213                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1214                                                                                            &page_id, &selected_index, &article_count);
1215                            if (ret < 0)
1216                            {
1217                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1218                                                      p_section->sid, p_article_locate->aid);
1219                                    return -3;
1220                            }
1221                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1222                            {
1223                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1224                                    if (ret < 0)
1225                                    {
1226                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1227                                            return -3;
1228                                    }
1229                            }
1230                            break;
1231                    case SEARCH_USER:
1232                            user_list_search();
1233                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1234                            {
1235                                    log_error("section_list_draw_screen() error\n");
1236                                    return -2;
1237                            }
1238                            break;
1239                    case SHOW_HELP:
1240                            // Display help information
1241                            display_file(DATA_READ_HELP, 1);
1242                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1243                            {
1244                                    log_error("section_list_draw_screen() error\n");
1245                                    return -2;
1246                            }
1247                            break;
1248                    case VIEW_EX_DIR:
1249                            if (section_list_ex_dir_display(p_section) < 0)
1250                            {
1251                                    log_error("section_list_ex_dir_display(sid=%d) error\n", p_section->sid);
1252                            }
1253                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1254                          {                          {
1255                                  log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);                                  log_error("section_list_draw_screen() error\n");
1256                                    return -2;
1257                          }                          }
1258                            break;
1259                    case SHOW_TOP10:
1260                            show_top10_menu(NULL);
1261                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1262                          {                          {
1263                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 603  int section_list_display(const char *sna Line 1271  int section_list_display(const char *sna
1271    
1272          return 0;          return 0;
1273  }  }
1274    
1275    int section_list_ex_dir_display(SECTION_LIST *p_section)
1276    {
1277            MENU_SET ex_menu_set;
1278            int ch = 0;
1279    
1280            if (p_section == NULL)
1281            {
1282                    log_error("NULL pointer error\n");
1283                    return -1;
1284            }
1285    
1286            if (p_section->ex_menu_tm == 0) // N/A
1287            {
1288                    moveto(2, 1);
1289                    clrtoeol();
1290                    prints("该版块精华区未开放");
1291                    press_any_key();
1292                    return 0;
1293            }
1294    
1295            if (get_section_ex_menu_set(p_section, &ex_menu_set) < 0)
1296            {
1297                    log_error("get_section_ex_menu_set(sid=%d) error\n", p_section->sid);
1298                    return -3;
1299            }
1300            if (get_menu_shm_readonly(&ex_menu_set) < 0)
1301            {
1302                    log_error("get_menu_shm_readonly(sid=%d) error\n", p_section->sid);
1303                    return -3;
1304            }
1305    
1306            clearscr();
1307            show_bottom("");
1308    
1309            if (display_menu(&ex_menu_set) == 0)
1310            {
1311                    while (!SYS_server_exit)
1312                    {
1313                            iflush();
1314                            ch = igetch(100);
1315    
1316                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1317                            {
1318                                    BBS_last_access_tm = time(NULL);
1319                            }
1320    
1321                            switch (ch)
1322                            {
1323                            case KEY_NULL: // broken pipe
1324                                    log_error("KEY_NULL\n");
1325                                    return 0;
1326                            case KEY_TIMEOUT:
1327                                    if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
1328                                    {
1329                                            log_error("User input timeout\n");
1330                                            return 0;
1331                                    }
1332                                    continue;
1333                            case CR:
1334                            default:
1335                                    switch (menu_control(&ex_menu_set, ch))
1336                                    {
1337                                    case EXITMENU:
1338                                            ch = EXITMENU;
1339                                            break;
1340                                    case REDRAW:
1341                                            clearscr();
1342                                            show_bottom("");
1343                                            display_menu(&ex_menu_set);
1344                                            break;
1345                                    case NOREDRAW:
1346                                    case UNKNOWN_CMD:
1347                                    default:
1348                                            break;
1349                                    }
1350                            }
1351    
1352                            if (ch == EXITMENU)
1353                            {
1354                                    break;
1355                            }
1356                    }
1357            }
1358    
1359            detach_menu_shm(&ex_menu_set);
1360    
1361            return 0;
1362    }
1363    
1364    int section_aid_locations_save(int uid)
1365    {
1366            char filename[FILE_PATH_LEN];
1367            FILE *fp;
1368            int i;
1369            int ret = 0;
1370    
1371            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1372    
1373            if ((fp = fopen(filename, "wb")) == NULL)
1374            {
1375                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1376                    return -1;
1377            }
1378    
1379            for (i = 0; i < p_section_list_pool->section_count; i++)
1380            {
1381                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1382                    {
1383                            log_error("fwrite(%s, sid) error\n", filename);
1384                            ret = -2;
1385                            break;
1386                    }
1387    
1388                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1389                    {
1390                            log_error("fwrite(%s, aid) error\n", filename);
1391                            ret = -2;
1392                            break;
1393                    }
1394            }
1395    
1396            if (fclose(fp) < 0)
1397            {
1398                    log_error("fclose(%s) error: %d\n", filename, errno);
1399                    ret = -1;
1400            }
1401    
1402            return ret;
1403    }
1404    
1405    int section_aid_locations_load(int uid)
1406    {
1407            char filename[FILE_PATH_LEN];
1408            FILE *fp;
1409            int i;
1410            int32_t sid;
1411            int32_t aid;
1412            SECTION_LIST *p_section;
1413            int ret = 0;
1414    
1415            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1416    
1417            if ((fp = fopen(filename, "rb")) == NULL)
1418            {
1419                    if (errno == ENOENT) // file not exist
1420                    {
1421                            return 0;
1422                    }
1423                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1424                    return -1;
1425            }
1426    
1427            while (!feof(fp))
1428            {
1429                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1430                    {
1431                            if (ferror(fp) == 0)
1432                            {
1433                                    break;
1434                            }
1435                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1436                            ret = -2;
1437                            break;
1438                    }
1439    
1440                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1441                    {
1442                            if (ferror(fp) == 0)
1443                            {
1444                                    break;
1445                            }
1446                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1447                            ret = -2;
1448                            break;
1449                    }
1450    
1451                    p_section = section_list_find_by_sid(sid);
1452                    if (p_section == NULL)
1453                    {
1454                            continue; // skip section no longer exist
1455                    }
1456    
1457                    i = get_section_index(p_section);
1458                    if (i < 0)
1459                    {
1460                            log_error("get_section_index(sid=%d) error\n", sid);
1461                            ret = -3;
1462                            break;
1463                    }
1464                    section_aid_locations[i] = aid;
1465            }
1466    
1467            if (fclose(fp) < 0)
1468            {
1469                    log_error("fclose(%s) error: %d\n", filename, errno);
1470                    ret = -1;
1471            }
1472    
1473            return ret;
1474    }


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

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