/[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.31 by sysadm, Tue Jun 17 07:00:32 2025 UTC Revision 1.60 by sysadm, Sun Nov 2 08:35:23 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _POSIX_C_SOURCE 200809L  
   
 #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"  #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_priv.h"
35    #include <errno.h>
36  #include <string.h>  #include <string.h>
37  #include <time.h>  #include <time.h>
38  #include <sys/param.h>  #include <sys/param.h>
39    
40    static int32_t section_aid_locations[BBS_max_section] = {0};
41  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
42  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
43    
44  enum select_cmd_t  enum select_cmd_t
45  {  {
46          EXIT_SECTION = 0,          EXIT_SECTION = 0,
47          VIEW_ARTICLE = 1,          VIEW_ARTICLE,
48          CHANGE_PAGE = 2,          CHANGE_PAGE,
49          SHOW_HELP = 3,          SHOW_HELP,
50          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY,
51          POST_ARTICLE = 5,          POST_ARTICLE,
52          EDIT_ARTICLE = 6,          EDIT_ARTICLE,
53          DELETE_ARTICLE = 7,          DELETE_ARTICLE,
54          FIRST_TOPIC_ARTICLE = 8,          QUERY_ARTICLE,
55          LAST_TOPIC_ARTICLE = 9,          QUERY_USER,
56            SET_FAVOR_ARTICLE,
57            UNSET_FAVOR_ARTICLE,
58            FIRST_TOPIC_ARTICLE,
59            LAST_TOPIC_ARTICLE,
60            SCAN_NEW_ARTICLE,
61            VIEW_EX_DIR,
62            SHOW_TOP10,
63  };  };
64    
65  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)
66  {  {
67          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
68          struct tm tm_sub;          struct tm tm_sub;
# Line 58  static int section_list_draw_items(int p Line 71  static int section_list_draw_items(int p
71          int eol;          int eol;
72          int len;          int len;
73          int i;          int i;
74            size_t j;
75          char article_flag;          char article_flag;
76          int is_viewed;          int is_viewed;
77            int is_favor;
78          time_t tm_now;          time_t tm_now;
79    
80          time(&tm_now);          time(&tm_now);
# Line 82  static int section_list_draw_items(int p Line 97  static int section_list_draw_items(int p
97                          }                          }
98                  }                  }
99    
100                  article_flag = (is_viewed ? ' ' : 'N');                  if (p_articles[i]->tid == 0)
101                    {
102                            is_favor = article_favor_check(p_articles[i]->aid, &BBS_article_favor);
103                            if (is_favor < 0)
104                            {
105                                    log_error("article_favor_check(aid=%d) error\n", p_articles[i]->aid);
106                                    is_favor = 0;
107                            }
108                    }
109                    else
110                    {
111                            is_favor = 0;
112                    }
113    
114                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
115                  {                  {
# Line 92  static int section_list_draw_items(int p Line 119  static int section_list_draw_items(int p
119                  {                  {
120                          article_flag = 'x';                          article_flag = 'x';
121                  }                  }
122                    else
123                    {
124                            article_flag = (is_viewed ? ' ' : 'N');
125                    }
126    
127                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);
128                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)
# Line 103  static int section_list_draw_items(int p Line 134  static int section_list_draw_items(int p
134                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
135                  }                  }
136    
137                  strncpy(title_f, (p_articles[i]->tid == 0 ? " " : ""), sizeof(title_f) - 1);                  strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
138                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
139                  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)));
140                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));  
141                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);                  // Rewrite title with "Re: Re: " prefix into "Re: ... "
142                    j = 0;
143                    if (p_articles[i]->tid != 0)
144                    {
145                            while (strncmp(p_articles[i]->title + j, "Re: ", strlen("Re: ")) == 0)
146                            {
147                                    j += strlen("Re: ");
148                            }
149                            if (j >= strlen("Re: Re: "))
150                            {
151                                    strncat(title_f, "Re: ... ", sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
152                            }
153                            else
154                            {
155                                    j = 0;
156                            }
157                    }
158                    strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
159    
160                    len = split_line(title_f, 59 - (display_nickname ? BBS_nickname_max_len / 2 : BBS_username_max_len), &eol, &title_f_len, 1);
161                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
162                  {                  {
163                          title_f[len] = '\0';                          title_f[len] = '\0';
164                  }                  }
165    
166                  moveto(4 + i, 1);                  moveto(4 + i, 1);
167                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",                  if (i >= ontop_start_offset)
168                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),                  {
169                             p_articles[i]->aid,                          prints("   \033[1;33m[提示]\033[m%c%c %s%*s %s %s%s\033[m",
170                             article_flag,                                     (is_favor ? '@' : ' '),
171                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     article_flag,
172                             (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),
173                                                                   : 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)
174                             "",                                                                           : BBS_username_max_len - str_length(p_articles[i]->username, 1)),
175                             str_time,                                     "",
176                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),                                     str_time,
177                             title_f);                                     (p_articles[i]->aid == section_topic_view_tid
178                                                    ? "\033[1;33m"
179                                                    : (p_articles[i]->tid == section_topic_view_tid
180                                                               ? "\033[1;36m"
181                                                               : "")),
182                                       title_f);
183                    }
184                    else
185                    {
186                            prints("  %s%7d\033[m%c%c %s%*s %s %s%s\033[m",
187                                       (p_articles[i]->aid == section_topic_view_tid
188                                                    ? "\033[1;33m"
189                                                    : (p_articles[i]->tid == section_topic_view_tid
190                                                               ? "\033[1;36m"
191                                                               : "")),
192                                       p_articles[i]->aid,
193                                       (is_favor ? '@' : ' '),
194                                       article_flag,
195                                       (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
196                                       (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
197                                                                             : BBS_username_max_len - str_length(p_articles[i]->username, 1)),
198                                       "",
199                                       str_time,
200                                       (p_articles[i]->aid == section_topic_view_tid
201                                                    ? "\033[1;33m"
202                                                    : (p_articles[i]->tid == section_topic_view_tid
203                                                               ? "\033[1;36m"
204                                                               : "")),
205                                       title_f);
206                    }
207          }          }
208    
209          return 0;          return 0;
# Line 132  static int section_list_draw_items(int p Line 211  static int section_list_draw_items(int p
211    
212  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)
213  {  {
214          char str_section_master[LINE_BUFFER_LEN] = "";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
215          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
216    
217          if (master_list[0] != '\0')          if (master_list[0] != '\0')
218          {          {
219                  snprintf(str_section_master, sizeof(str_section_master), "%s", master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
220          }          }
221          snprintf(str_section_name, sizeof(str_section_name), " [%s]", sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
222    
223          clearscr();          clearscr();
224          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
225          moveto(2, 0);          moveto(2, 0);
226          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] "
227                     "Ķ[\033[1;32m\033[0;37m,\033[1;32mENTER\033[0;37m] [\033[1;32mCtrl-P\033[0;37m] "                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 发表[\033[1;32mCtrl-P\033[0;37m] "
228                     "%s[\033[1;32mn\033[0;37m] [\033[1;32mh\033[0;37m]\033[m",                     "%s[\033[1;32mn\033[0;37m] 精华区[\033[1;32mx\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
229                     (display_nickname ? "ʾû" : "ʾdz"));                     (display_nickname ? "用户名" : "昵称"));
230          moveto(3, 0);          moveto(3, 0);
231          if (display_nickname)          if (display_nickname)
232          {          {
233                  prints("\033[44;37m  \033[1;37m     dz              ±                               \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
234          }          }
235          else          else
236          {          {
237                  prints("\033[44;37m  \033[1;37m            ±                                       \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
238          }          }
239    
240          return 0;          return 0;
# Line 179  static enum select_cmd_t section_list_se Line 258  static enum select_cmd_t section_list_se
258          {          {
259                  ch = igetch(100);                  ch = igetch(100);
260    
261                  switch (ch)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
262                  {                  {
                 case KEY_ESC:  
                 case KEY_LEFT:  
263                          BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
264                  case KEY_NULL:                   // broken pipe                  }
265                          return EXIT_SECTION; // exit section  
266                    switch (ch)
267                    {
268                    case KEY_NULL: // broken pipe
269                            log_error("KEY_NULL\n");
270                            return EXIT_SECTION;
271                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
272                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
273                          {                          {
274                                  return EXIT_SECTION; // exit section                                  log_error("User input timeout\n");
275                                    return EXIT_SECTION;
276                          }                          }
277                          continue;                          continue;
278                    case KEY_ESC:
279                    case KEY_LEFT:
280                            return EXIT_SECTION;
281                  case 'n':                  case 'n':
                         BBS_last_access_tm = time(NULL);  
282                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
283                  case CR:                  case CR:
                         igetch_reset();  
284                  case 'r':                  case 'r':
285                  case KEY_RIGHT:                  case KEY_RIGHT:
286                          if (item_count > 0)                          if (item_count > 0)
287                          {                          {
                                 BBS_last_access_tm = time(NULL);  
288                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
289                          }                          }
290                          break;                          break;
291                  case Ctrl('P'):                  case Ctrl('P'):
292                          return POST_ARTICLE;                          return POST_ARTICLE;
293                  case 'E':                  case 'E':
294                          return EDIT_ARTICLE;                          if (item_count > 0)
295                            {
296                                    return EDIT_ARTICLE;
297                            }
298                            break;
299                  case 'd':                  case 'd':
300                          return DELETE_ARTICLE;                          if (item_count > 0)
301                            {
302                                    return DELETE_ARTICLE;
303                            }
304                            break;
305                    case Ctrl('Q'):
306                            if (item_count > 0)
307                            {
308                                    return QUERY_ARTICLE;
309                            }
310                            break;
311                    case Ctrl('A'):
312                            if (item_count > 0)
313                            {
314                                    return QUERY_USER;
315                            }
316                            break;
317                    case 'F':
318                            if (item_count > 0)
319                            {
320                                    return SET_FAVOR_ARTICLE;
321                            }
322                            break;
323                    case '-':
324                            if (item_count > 0)
325                            {
326                                    return UNSET_FAVOR_ARTICLE;
327                            }
328                            break;
329                  case KEY_HOME:                  case KEY_HOME:
330                          *p_page_id = 0;                          *p_page_id = 0;
331                  case 'P':                  case 'P':
# Line 225  static enum select_cmd_t section_list_se Line 340  static enum select_cmd_t section_list_se
340                                          (*p_page_id)--;                                          (*p_page_id)--;
341                                          *p_selected_index = BBS_article_limit_per_page - 1;                                          *p_selected_index = BBS_article_limit_per_page - 1;
342                                  }                                  }
343                                    else if (ch == KEY_UP || ch == 'k') // Rotate to the tail of section list
344                                    {
345                                            if (total_page > 0)
346                                            {
347                                                    *p_page_id = total_page - 1;
348                                            }
349                                            if (item_count > 0)
350                                            {
351                                                    *p_selected_index = item_count - 1;
352                                            }
353                                    }
354                          }                          }
355                          else                          else
356                          {                          {
# Line 252  static enum select_cmd_t section_list_se Line 378  static enum select_cmd_t section_list_se
378                                          (*p_page_id)++;                                          (*p_page_id)++;
379                                          *p_selected_index = 0;                                          *p_selected_index = 0;
380                                  }                                  }
381                                  else // end of last page                                  else if (ch == KEY_DOWN || ch == 'j') // Rotate to the head of section list
382                                  {                                  {
383                                          return CHANGE_PAGE; // force refresh pages                                          *p_page_id = 0;
384                                            *p_selected_index = 0;
385                                  }                                  }
386                          }                          }
387                          else                          else
# Line 263  static enum select_cmd_t section_list_se Line 390  static enum select_cmd_t section_list_se
390                          }                          }
391                          break;                          break;
392                  case '=':                  case '=':
393                          return FIRST_TOPIC_ARTICLE;                          if (item_count > 0)
394                            {
395                                    return FIRST_TOPIC_ARTICLE;
396                            }
397                            break;
398                  case '\\':                  case '\\':
399                          return LAST_TOPIC_ARTICLE;                          if (item_count > 0)
400                            {
401                                    return LAST_TOPIC_ARTICLE;
402                            }
403                            break;
404                    case 'S':
405                            if (item_count > 0)
406                            {
407                                    return SCAN_NEW_ARTICLE;
408                            }
409                            break;
410                  case 'h':                  case 'h':
411                          return SHOW_HELP;                          return SHOW_HELP;
412                    case 'x':
413                            return VIEW_EX_DIR;
414                    case 'H':
415                            return SHOW_TOP10;
416                  default:                  default:
417                            break;
418                  }                  }
419    
420                  if (old_page_id != *p_page_id)                  if (old_page_id != *p_page_id)
# Line 293  static enum select_cmd_t section_list_se Line 439  static enum select_cmd_t section_list_se
439                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
440                  }                  }
441    
                 BBS_last_access_tm = time(NULL);  
442                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
443                  {                  {
444                          return CHANGE_PAGE; // force section list refresh                          return CHANGE_PAGE; // force section list refresh
# Line 314  static int display_article_key_handler(i Line 459  static int display_article_key_handler(i
459                  if (section_topic_view_mode)                  if (section_topic_view_mode)
460                  {                  {
461                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
462                                           "| [\033[32m\033[33m,\033[32mESC\033[33m] "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
463                                           "ͬĶ[\033[32m\033[33m/\033[32m\033[33m] "                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
464                                           "л[\033[32mp\033[33m] ظ[\033[32mr\033[33m] [\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
465                  }                  }
466                  else                  else
467                  {                  {
468                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
469                                           "| [\033[32m\033[33m,\033[32mESC\033[33m] "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
470                                           "ƶ[\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] "
471                                           "л[\033[32mp\033[33m] ظ[\033[32mr\033[33m] [\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
472                  }                  }
473                  *p_key = 0;                  *p_key = 0;
474                  break;                  break;
# Line 390  static int display_article_key_handler(i Line 535  static int display_article_key_handler(i
535          return 0;          return 0;
536  }  }
537    
538  int section_list_display(const char *sname)  int section_list_display(const char *sname, int32_t aid)
539  {  {
540          static int display_nickname = 0;          static int display_nickname = 0;
541    
542          SECTION_LIST *p_section;          SECTION_LIST *p_section;
543            int64_t section_index;
544            int32_t aid_location;
545          char stitle[BBS_section_title_max_len + 1];          char stitle[BBS_section_title_max_len + 1];
546          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
547          char page_info_str[LINE_BUFFER_LEN];          char page_info_str[LINE_BUFFER_LEN];
548          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
549          int article_count;          int article_count;
550          int page_count;          int page_count;
551            int ontop_start_offset;
552          int page_id = 0;          int page_id = 0;
553          int selected_index = 0;          int selected_index = 0;
554          ARTICLE_CACHE cache;          ARTICLE_CACHE cache;
# Line 409  int section_list_display(const char *sna Line 557  int section_list_display(const char *sna
557          int direction;          int direction;
558          ARTICLE article_new;          ARTICLE article_new;
559          int page_id_cur;          int page_id_cur;
560            const ARTICLE *p_article_locate;
561            USER_INFO user_info;
562    
563          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
564          if (p_section == NULL)          if (p_section == NULL)
# Line 417  int section_list_display(const char *sna Line 567  int section_list_display(const char *sna
567                  return -1;                  return -1;
568          }          }
569    
570          if ((ret = section_list_rd_lock(p_section)) < 0)          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
571          {          {
572                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
573                  return -2;                                    p_section->sid, BBS_priv.uid);
574                    return -1;
575          }          }
576    
577          strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);          section_index = get_section_index(p_section);
         stitle[sizeof(stitle) - 1] = '\0';  
         strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);  
         master_list[sizeof(master_list) - 1] = '\0';  
578    
579          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
580          {          {
581                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
582                  return -2;                  return -4;
583            }
584    
585            if (aid == 0)
586            {
587                    aid_location = section_aid_locations[section_index];
588            }
589            else
590            {
591                    aid_location = aid;
592            }
593    
594            // Locate at article with aid_locate
595            if (aid_location > 0)
596            {
597                    p_article_locate = article_block_find_by_aid(aid_location);
598                    if (p_article_locate == NULL)
599                    {
600                            log_error("article_block_find_by_aid(%d) error\n", aid_location);
601                            return -3;
602                    }
603    
604                    ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
605                                                                                    &page_id, &selected_index, &article_count);
606                    if (ret < 0)
607                    {
608                            log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
609                                              p_section->sid, p_article_locate->aid);
610                            return -3;
611                    }
612          }          }
613    
614          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 440  int section_list_display(const char *sna Line 617  int section_list_display(const char *sna
617                  return -2;                  return -2;
618          }          }
619    
620          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);
621          if (ret < 0)          if (ret < 0)
622          {          {
623                  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);
624                  return -3;                  return -3;
625          }          }
626    
627            section_topic_view_tid = -1;
628    
629          if (article_count == 0) // empty section          if (article_count == 0) // empty section
630          {          {
631                  selected_index = 0;                  selected_index = 0;
632          }          }
633            else if (aid > 0)
634            {
635                    // Update current topic
636                    section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
637    
638                    // Update current aid location
639                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
640            }
641    
642          while (!SYS_server_exit)          while (!SYS_server_exit)
643          {          {
644                  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);
645                  if (ret < 0)                  if (ret < 0)
646                  {                  {
647                          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 462  int section_list_display(const char *sna Line 649  int section_list_display(const char *sna
649                  }                  }
650    
651                  snprintf(page_info_str, sizeof(page_info_str),                  snprintf(page_info_str, sizeof(page_info_str),
652                                   "\033[33m[\033[36m%d\033[33m/\033[36m%d\033[33mҳ]",                                   "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
653                                   page_id + 1, MAX(page_count, 1));                                   page_id + 1, MAX(page_count, 1));
654    
655                  show_bottom(page_info_str);                  show_bottom(page_info_str);
656                  iflush();                  iflush();
657    
658                    if (user_online_update(sname) < 0)
659                    {
660                            log_error("user_online_update(%s) error\n", sname);
661                    }
662    
663                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
664    
665                    // Update current aid location
666                    section_aid_locations[section_index] = p_articles[selected_index]->aid;
667    
668                  switch (ret)                  switch (ret)
669                  {                  {
670                  case EXIT_SECTION:                  case EXIT_SECTION:
671                          return 0;                          return 0;
672                  case CHANGE_PAGE:                  case CHANGE_PAGE:
673                          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);
674                          if (ret < 0)                          if (ret < 0)
675                          {                          {
676                                  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 500  int section_list_display(const char *sna Line 696  int section_list_display(const char *sna
696                                          break;                                          break;
697                                  }                                  }
698    
699                                    if (user_online_update("VIEW_ARTICLE") < 0)
700                                    {
701                                            log_error("user_online_update(VIEW_ARTICLE) error\n");
702                                    }
703    
704                                  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,
705                                                                     display_article_key_handler, DATA_READ_HELP);                                                                     display_article_key_handler, DATA_READ_HELP);
706    
# Line 525  int section_list_display(const char *sna Line 726  int section_list_display(const char *sna
726                                                          page_id--;                                                          page_id--;
727                                                          selected_index = BBS_article_limit_per_page - 1;                                                          selected_index = BBS_article_limit_per_page - 1;
728    
729                                                          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);
730                                                          if (ret < 0)                                                          if (ret < 0)
731                                                          {                                                          {
732                                                                  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 556  int section_list_display(const char *sna Line 757  int section_list_display(const char *sna
757                                                          page_id++;                                                          page_id++;
758                                                          selected_index = 0;                                                          selected_index = 0;
759    
760                                                          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);
761                                                          if (ret < 0)                                                          if (ret < 0)
762                                                          {                                                          {
763                                                                  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 592  int section_list_display(const char *sna Line 793  int section_list_display(const char *sna
793                                          }                                          }
794                                          else if (ret > 0) // found                                          else if (ret > 0) // found
795                                          {                                          {
796                                                  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);
797                                                  if (ret < 0)                                                  if (ret < 0)
798                                                  {                                                  {
799                                                          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 602  int section_list_display(const char *sna Line 803  int section_list_display(const char *sna
803                                          }                                          }
804                                          break;                                          break;
805                                  case 'r': // Reply article                                  case 'r': // Reply article
806                                            if (user_online_update("REPLY_ARTICLE") < 0)
807                                            {
808                                                    log_error("user_online_update(REPLY_ARTICLE) error\n");
809                                            }
810    
811                                          if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)                                          if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
812                                          {                                          {
813                                                  log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);                                                  log_error("article_reply(aid=%d) error\n", p_articles[selected_index]->aid);
814                                          }                                          }
815                                          loop = 1;                                          loop = 1;
816                                          break;                                          break;
# Line 624  int section_list_display(const char *sna Line 830  int section_list_display(const char *sna
830                                          {                                          {
831                                                  if (page_id != page_id_cur) // page changed                                                  if (page_id != page_id_cur) // page changed
832                                                  {                                                  {
833                                                          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);
834                                                          if (ret < 0)                                                          if (ret < 0)
835                                                          {                                                          {
836                                                                  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 639  int section_list_display(const char *sna Line 845  int section_list_display(const char *sna
845    
846                          // Update current topic                          // Update current topic
847                          section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);                          section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
848    
849                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
850                            {
851                                    log_error("section_list_draw_screen() error\n");
852                                    return -2;
853                            }
854                            break;
855                  case CHANGE_NAME_DISPLAY:                  case CHANGE_NAME_DISPLAY:
856                          display_nickname = !display_nickname;                          display_nickname = !display_nickname;
857                          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 648  int section_list_display(const char *sna Line 861  int section_list_display(const char *sna
861                          }                          }
862                          break;                          break;
863                  case POST_ARTICLE:                  case POST_ARTICLE:
864                            if (user_online_update("POST_ARTICLE") < 0)
865                            {
866                                    log_error("user_online_update(POST_ARTICLE) error\n");
867                            }
868    
869                          if ((ret = article_post(p_section, &article_new)) < 0)                          if ((ret = article_post(p_section, &article_new)) < 0)
870                          {                          {
871                                  log_error("article_post(sid=%d) error\n", p_section->sid);                                  log_error("article_post(sid=%d) error\n", p_section->sid);
872                          }                          }
873                          else if (ret > 0) // New article posted                          else if (ret > 0) // New article posted
874                          {                          {
875                                  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);
876                                  if (ret < 0)                                  if (ret < 0)
877                                  {                                  {
878                                          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 673  int section_list_display(const char *sna Line 891  int section_list_display(const char *sna
891                          {                          {
892                                  break; // No permission                                  break; // No permission
893                          }                          }
894    
895                            if (user_online_update("EDIT_ARTICLE") < 0)
896                            {
897                                    log_error("user_online_update() error\n");
898                            }
899    
900                          if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)                          if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
901                          {                          {
902                                  log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);                                  log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
# Line 695  int section_list_display(const char *sna Line 919  int section_list_display(const char *sna
919                          }                          }
920                          else if (ret > 0) // Article deleted                          else if (ret > 0) // Article deleted
921                          {                          {
922                                  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);
923                                  if (ret < 0)                                  if (ret < 0)
924                                  {                                  {
925                                          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 708  int section_list_display(const char *sna Line 932  int section_list_display(const char *sna
932                                  return -2;                                  return -2;
933                          }                          }
934                          break;                          break;
935                    case QUERY_ARTICLE:
936                            if ((ret = display_article_meta(p_articles[selected_index]->aid)) < 0)
937                            {
938                                    log_error("display_article_meta(aid=%d) error\n", p_articles[selected_index]->aid);
939                            }
940                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
941                            {
942                                    log_error("section_list_draw_screen() error\n");
943                                    return -2;
944                            }
945                            break;
946                    case QUERY_USER:
947                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info)) < 0)
948                            {
949                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
950                                    return -2;
951                            }
952                            else if (ret == 0)
953                            {
954                                    clearscr();
955                                    prints("该用户已升天");
956                                    press_any_key();
957                            }
958                            else if (user_info_display(&user_info) < 0) // && ret > 0
959                            {
960                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
961                            }
962    
963                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
964                            {
965                                    log_error("section_list_draw_screen() error\n");
966                                    return -2;
967                            }
968                            break;
969                    case SET_FAVOR_ARTICLE:
970                            ret = article_favor_set(p_articles[selected_index]->tid == 0
971                                                                                    ? p_articles[selected_index]->aid
972                                                                                    : p_articles[selected_index]->tid,
973                                                                            &BBS_article_favor, 1);
974                            if (ret < 0)
975                            {
976                                    log_error("article_favor_set(aid=%d, 1) error\n",
977                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
978                            }
979                            break;
980                    case UNSET_FAVOR_ARTICLE:
981                            ret = article_favor_set(p_articles[selected_index]->tid == 0
982                                                                                    ? p_articles[selected_index]->aid
983                                                                                    : p_articles[selected_index]->tid,
984                                                                            &BBS_article_favor, 0);
985                            if (ret < 0)
986                            {
987                                    log_error("article_favor_set(aid=%d, 0) error\n",
988                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
989                            }
990                            break;
991                  case FIRST_TOPIC_ARTICLE:                  case FIRST_TOPIC_ARTICLE:
992                  case LAST_TOPIC_ARTICLE:                  case LAST_TOPIC_ARTICLE:
993                          page_id_cur = page_id;                          page_id_cur = page_id;
# Line 722  int section_list_display(const char *sna Line 1002  int section_list_display(const char *sna
1002                          }                          }
1003                          else if (ret > 0 && page_id != page_id_cur) // found and page changed                          else if (ret > 0 && page_id != page_id_cur) // found and page changed
1004                          {                          {
1005                                  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);
1006                                    if (ret < 0)
1007                                    {
1008                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1009                                            return -3;
1010                                    }
1011                            }
1012                            break;
1013                    case SCAN_NEW_ARTICLE:
1014                            ret = scan_unread_article_in_section(p_section, p_articles[selected_index], &p_article_locate);
1015                            if (ret < 0)
1016                            {
1017                                    log_error("scan_unread_article_in_section(sid=%d, aid=%d) error\n",
1018                                                      p_section->sid, p_articles[selected_index]->aid);
1019                                    return -3;
1020                            }
1021                            else if (ret == 0) // not found
1022                            {
1023                                    break;
1024                            }
1025                            page_id_cur = page_id;
1026                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1027                                                                                            &page_id, &selected_index, &article_count);
1028                            if (ret < 0)
1029                            {
1030                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1031                                                      p_section->sid, p_article_locate->aid);
1032                                    return -3;
1033                            }
1034                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1035                            {
1036                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1037                                  if (ret < 0)                                  if (ret < 0)
1038                                  {                                  {
1039                                          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 739  int section_list_display(const char *sna Line 1050  int section_list_display(const char *sna
1050                                  return -2;                                  return -2;
1051                          }                          }
1052                          break;                          break;
1053                    case VIEW_EX_DIR:
1054                            if (section_list_ex_dir_display(p_section) < 0)
1055                            {
1056                                    log_error("section_list_ex_dir_display(sid=%d) error\n", p_section->sid);
1057                            }
1058                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1059                            {
1060                                    log_error("section_list_draw_screen() error\n");
1061                                    return -2;
1062                            }
1063                            break;
1064                    case SHOW_TOP10:
1065                            show_top10_menu(NULL);
1066                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1067                            {
1068                                    log_error("section_list_draw_screen() error\n");
1069                                    return -2;
1070                            }
1071                            break;
1072                  default:                  default:
1073                          log_error("Unknown command %d\n", ret);                          log_error("Unknown command %d\n", ret);
1074                  }                  }
# Line 746  int section_list_display(const char *sna Line 1076  int section_list_display(const char *sna
1076    
1077          return 0;          return 0;
1078  }  }
1079    
1080    int section_list_ex_dir_display(SECTION_LIST *p_section)
1081    {
1082            MENU_SET ex_menu_set;
1083            int ch = 0;
1084    
1085            if (p_section == NULL)
1086            {
1087                    log_error("NULL pointer error\n");
1088                    return -1;
1089            }
1090    
1091            if (p_section->ex_menu_tm == 0) // N/A
1092            {
1093                    moveto(2, 1);
1094                    clrtoeol();
1095                    prints("该版块精华区未开放");
1096                    press_any_key();
1097                    return 0;
1098            }
1099    
1100            if (get_section_ex_menu_set(p_section, &ex_menu_set) < 0)
1101            {
1102                    log_error("get_section_ex_menu_set(sid=%d) error\n", p_section->sid);
1103                    return -3;
1104            }
1105            if (get_menu_shm_readonly(&ex_menu_set) < 0)
1106            {
1107                    log_error("get_menu_shm_readonly(sid=%d) error\n", p_section->sid);
1108                    return -3;
1109            }
1110    
1111            clearscr();
1112            show_bottom("");
1113    
1114            if (display_menu(&ex_menu_set) == 0)
1115            {
1116                    while (!SYS_server_exit)
1117                    {
1118                            iflush();
1119                            ch = igetch(100);
1120    
1121                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1122                            {
1123                                    BBS_last_access_tm = time(NULL);
1124                            }
1125    
1126                            switch (ch)
1127                            {
1128                            case KEY_NULL: // broken pipe
1129                                    log_error("KEY_NULL\n");
1130                                    return 0;
1131                            case KEY_TIMEOUT:
1132                                    if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
1133                                    {
1134                                            log_error("User input timeout\n");
1135                                            return 0;
1136                                    }
1137                                    continue;
1138                            case CR:
1139                            default:
1140                                    switch (menu_control(&ex_menu_set, ch))
1141                                    {
1142                                    case EXITMENU:
1143                                            ch = EXITMENU;
1144                                            break;
1145                                    case REDRAW:
1146                                            clearscr();
1147                                            show_bottom("");
1148                                            display_menu(&ex_menu_set);
1149                                            break;
1150                                    case NOREDRAW:
1151                                    case UNKNOWN_CMD:
1152                                    default:
1153                                            break;
1154                                    }
1155                            }
1156    
1157                            if (ch == EXITMENU)
1158                            {
1159                                    break;
1160                            }
1161                    }
1162            }
1163    
1164            detach_menu_shm(&ex_menu_set);
1165    
1166            return 0;
1167    }
1168    
1169    int section_aid_locations_save(int uid)
1170    {
1171            char filename[FILE_PATH_LEN];
1172            FILE *fp;
1173            int i;
1174            int ret = 0;
1175    
1176            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1177    
1178            if ((fp = fopen(filename, "wb")) == NULL)
1179            {
1180                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1181                    return -1;
1182            }
1183    
1184            for (i = 0; i < p_section_list_pool->section_count; i++)
1185            {
1186                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1187                    {
1188                            log_error("fwrite(%s, sid) error\n", filename);
1189                            ret = -2;
1190                            break;
1191                    }
1192    
1193                    if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1194                    {
1195                            log_error("fwrite(%s, aid) error\n", filename);
1196                            ret = -2;
1197                            break;
1198                    }
1199            }
1200    
1201            if (fclose(fp) < 0)
1202            {
1203                    log_error("fclose(%s) error: %d\n", filename, errno);
1204                    ret = -1;
1205            }
1206    
1207            return ret;
1208    }
1209    
1210    int section_aid_locations_load(int uid)
1211    {
1212            char filename[FILE_PATH_LEN];
1213            FILE *fp;
1214            int i;
1215            int32_t sid;
1216            int32_t aid;
1217            SECTION_LIST *p_section;
1218            int ret = 0;
1219    
1220            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1221    
1222            if ((fp = fopen(filename, "rb")) == NULL)
1223            {
1224                    if (errno == ENOENT) // file not exist
1225                    {
1226                            return 0;
1227                    }
1228                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1229                    return -1;
1230            }
1231    
1232            while (!feof(fp))
1233            {
1234                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1235                    {
1236                            if (ferror(fp) == 0)
1237                            {
1238                                    break;
1239                            }
1240                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1241                            ret = -2;
1242                            break;
1243                    }
1244    
1245                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1246                    {
1247                            if (ferror(fp) == 0)
1248                            {
1249                                    break;
1250                            }
1251                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1252                            ret = -2;
1253                            break;
1254                    }
1255    
1256                    p_section = section_list_find_by_sid(sid);
1257                    if (p_section == NULL)
1258                    {
1259                            continue; // skip section no longer exist
1260                    }
1261    
1262                    i = get_section_index(p_section);
1263                    if (i < 0)
1264                    {
1265                            log_error("get_section_index(sid=%d) error\n", sid);
1266                            ret = -3;
1267                            break;
1268                    }
1269                    section_aid_locations[i] = aid;
1270            }
1271    
1272            if (fclose(fp) < 0)
1273            {
1274                    log_error("fclose(%s) error: %d\n", filename, errno);
1275                    ret = -1;
1276            }
1277    
1278            return ret;
1279    }


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

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