/[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.22 by sysadm, Fri Jun 13 12:17:21 2025 UTC Revision 1.31 by sysadm, Tue Jun 17 07:00:32 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "section_list_display.h"  #include "section_list_display.h"
20  #include "section_list_loader.h"  #include "section_list_loader.h"
21  #include "article_cache.h"  #include "article_cache.h"
22  #include "article_post.h"  #include "article_post.h"
23    #include "article_del.h"
24  #include "common.h"  #include "common.h"
25  #include "io.h"  #include "io.h"
26  #include "screen.h"  #include "screen.h"
# Line 25  Line 28 
28  #include "user_priv.h"  #include "user_priv.h"
29  #include "article_view_log.h"  #include "article_view_log.h"
30  #include "str_process.h"  #include "str_process.h"
31    #include <string.h>
32  #include <time.h>  #include <time.h>
33  #include <sys/param.h>  #include <sys/param.h>
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
34    
35  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
36  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
# Line 38  enum select_cmd_t Line 40  enum select_cmd_t
40          EXIT_SECTION = 0,          EXIT_SECTION = 0,
41          VIEW_ARTICLE = 1,          VIEW_ARTICLE = 1,
42          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
43          REFRESH_SCREEN = 3,          SHOW_HELP = 3,
44          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
45          POST_ARTICLE = 5,          POST_ARTICLE = 5,
46          EDIT_ARTICLE = 6,          EDIT_ARTICLE = 6,
47            DELETE_ARTICLE = 7,
48            FIRST_TOPIC_ARTICLE = 8,
49            LAST_TOPIC_ARTICLE = 9,
50  };  };
51    
52  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)
# Line 102  static int section_list_draw_items(int p Line 107  static int section_list_draw_items(int p
107                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
108                  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)));
109                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
110                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);
111                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
112                  {                  {
113                          title_f[len] = '\0';                          title_f[len] = '\0';
# Line 140  static int section_list_draw_screen(cons Line 145  static int section_list_draw_screen(cons
145          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
146          moveto(2, 0);          moveto(2, 0);
147          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] "
148                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 发表文章[\033[1;32mCtrl-P\033[0;37m] %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] "
149                       "%s[\033[1;32mn\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
150                     (display_nickname ? "显示用户名" : "显示昵称"));                     (display_nickname ? "显示用户名" : "显示昵称"));
151          moveto(3, 0);          moveto(3, 0);
152          if (display_nickname)          if (display_nickname)
# Line 160  static enum select_cmd_t section_list_se Line 166  static enum select_cmd_t section_list_se
166          int old_page_id = *p_page_id;          int old_page_id = *p_page_id;
167          int old_selected_index = *p_selected_index;          int old_selected_index = *p_selected_index;
168          int ch;          int ch;
169            time_t last_refresh_tm = time(NULL);
         BBS_last_access_tm = time(0);  
170    
171          if (item_count > 0 && *p_selected_index >= 0)          if (item_count > 0 && *p_selected_index >= 0)
172          {          {
# Line 176  static enum select_cmd_t section_list_se Line 181  static enum select_cmd_t section_list_se
181    
182                  switch (ch)                  switch (ch)
183                  {                  {
                 case KEY_NULL: // broken pipe  
184                  case KEY_ESC:                  case KEY_ESC:
185                  case KEY_LEFT:                  case KEY_LEFT:
186                            BBS_last_access_tm = time(NULL);
187                    case KEY_NULL:                   // broken pipe
188                          return EXIT_SECTION; // exit section                          return EXIT_SECTION; // exit section
189                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
190                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
191                          {                          {
192                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
193                          }                          }
194                          continue;                          continue;
195                  case 'n':                  case 'n':
196                            BBS_last_access_tm = time(NULL);
197                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
198                  case CR:                  case CR:
199                          igetch_reset();                          igetch_reset();
200                    case 'r':
201                  case KEY_RIGHT:                  case KEY_RIGHT:
202                          if (item_count > 0)                          if (item_count > 0)
203                          {                          {
204                                    BBS_last_access_tm = time(NULL);
205                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
206                          }                          }
207                          break;                          break;
# Line 200  static enum select_cmd_t section_list_se Line 209  static enum select_cmd_t section_list_se
209                          return POST_ARTICLE;                          return POST_ARTICLE;
210                  case 'E':                  case 'E':
211                          return EDIT_ARTICLE;                          return EDIT_ARTICLE;
212                    case 'd':
213                            return DELETE_ARTICLE;
214                  case KEY_HOME:                  case KEY_HOME:
215                          *p_page_id = 0;                          *p_page_id = 0;
216                    case 'P':
217                  case KEY_PGUP:                  case KEY_PGUP:
218                          *p_selected_index = 0;                          *p_selected_index = 0;
219                    case 'k':
220                  case KEY_UP:                  case KEY_UP:
221                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
222                          {                          {
# Line 218  static enum select_cmd_t section_list_se Line 231  static enum select_cmd_t section_list_se
231                                  (*p_selected_index)--;                                  (*p_selected_index)--;
232                          }                          }
233                          break;                          break;
234                    case '$':
235                  case KEY_END:                  case KEY_END:
236                          if (total_page > 0)                          if (total_page > 0)
237                          {                          {
238                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
239                          }                          }
240                    case 'N':
241                  case KEY_PGDN:                  case KEY_PGDN:
242                          if (item_count > 0)                          if (item_count > 0)
243                          {                          {
244                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
245                          }                          }
246                    case 'j':
247                  case KEY_DOWN:                  case KEY_DOWN:
248                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
249                          {                          {
# Line 236  static enum select_cmd_t section_list_se Line 252  static enum select_cmd_t section_list_se
252                                          (*p_page_id)++;                                          (*p_page_id)++;
253                                          *p_selected_index = 0;                                          *p_selected_index = 0;
254                                  }                                  }
255                                    else // end of last page
256                                    {
257                                            return CHANGE_PAGE; // force refresh pages
258                                    }
259                          }                          }
260                          else                          else
261                          {                          {
262                                  (*p_selected_index)++;                                  (*p_selected_index)++;
263                          }                          }
264                          break;                          break;
265                    case '=':
266                            return FIRST_TOPIC_ARTICLE;
267                    case '\\':
268                            return LAST_TOPIC_ARTICLE;
269                    case 'h':
270                            return SHOW_HELP;
271                  default:                  default:
272                  }                  }
273    
# Line 267  static enum select_cmd_t section_list_se Line 293  static enum select_cmd_t section_list_se
293                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
294                  }                  }
295    
296                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
297                    if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
298                    {
299                            return CHANGE_PAGE; // force section list refresh
300                    }
301          }          }
302    
303          return EXIT_SECTION;          return EXIT_SECTION;
# Line 278  static int display_article_key_handler(i Line 308  static int display_article_key_handler(i
308          switch (*p_key)          switch (*p_key)
309          {          {
310          case 'p':          case 'p':
311            case Ctrl('X'):
312                  section_topic_view_mode = !section_topic_view_mode;                  section_topic_view_mode = !section_topic_view_mode;
313          case 0: // Set msg          case 0: // Set msg
314                  if (section_topic_view_mode)                  if (section_topic_view_mode)
315                  {                  {
316                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
317                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
318                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] | "                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
319                                           "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
320                  }                  }
321                  else                  else
322                  {                  {
323                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
324                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
325                                           "移动[\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] "
326                                           "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
327                  }                  }
328                  *p_key = 0;                  *p_key = 0;
329                  break;                  break;
330          case 'r': // Reply article          case 'r': // Reply article
331                  return 1;                  return 1;
332            case '=': // First topic article
333                    return 1;
334            case '\\': // Last topic article
335                    return 1;
336          case KEY_UP:          case KEY_UP:
337          case KEY_PGUP:          case KEY_PGUP:
338          case KEY_HOME:          case KEY_HOME:
# Line 314  static int display_article_key_handler(i Line 349  static int display_article_key_handler(i
349                          return 1;                          return 1;
350                  }                  }
351                  break;                  break;
352            case 'k':
353                    if (section_topic_view_mode)
354                    {
355                            *p_key = KEY_PGUP;
356                    }
357                    else
358                    {
359                            *p_key = KEY_UP;
360                    }
361                    return 1;
362          case KEY_DOWN:          case KEY_DOWN:
363          case KEY_PGDN:          case KEY_PGDN:
364          case KEY_END:          case KEY_END:
# Line 330  static int display_article_key_handler(i Line 375  static int display_article_key_handler(i
375                          return 1;                          return 1;
376                  }                  }
377                  break;                  break;
378            case 'j':
379                    if (section_topic_view_mode)
380                    {
381                            *p_key = KEY_PGDN;
382                    }
383                    else
384                    {
385                            *p_key = KEY_DOWN;
386                    }
387                    return 1;
388          }          }
389    
390          return 0;          return 0;
# Line 352  int section_list_display(const char *sna Line 407  int section_list_display(const char *sna
407          int ret;          int ret;
408          int loop;          int loop;
409          int direction;          int direction;
410            ARTICLE article_new;
411            int page_id_cur;
412    
413          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
414          if (p_section == NULL)          if (p_section == NULL)
# Line 525  int section_list_display(const char *sna Line 582  int section_list_display(const char *sna
582                                  case KEY_PGUP:                                  case KEY_PGUP:
583                                  case KEY_PGDN:                                  case KEY_PGDN:
584                                          direction = (ret == KEY_PGUP ? -1 : 1);                                          direction = (ret == KEY_PGUP ? -1 : 1);
585                                          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,
586                                                                                                            &page_id, &selected_index, &article_count);
587                                          if (ret < 0)                                          if (ret < 0)
588                                          {                                          {
589                                                  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",
590                                                                    p_section->sid, p_articles[selected_index]->aid, direction);                                                                    p_section->sid, p_articles[selected_index]->aid, direction);
591                                                  return -3;                                                  return -3;
592                                          }                                          }
593                                          else if (ret > 0)                                          else if (ret > 0) // found
594                                          {                                          {
595                                                  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);
596                                                  if (ret < 0)                                                  if (ret < 0)
# Line 544  int section_list_display(const char *sna Line 602  int section_list_display(const char *sna
602                                          }                                          }
603                                          break;                                          break;
604                                  case 'r': // Reply article                                  case 'r': // Reply article
605                                          if (article_reply(p_section, p_articles[selected_index]) < 0)                                          if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
606                                          {                                          {
607                                                  log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);                                                  log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
608                                          }                                          }
609                                          loop = 1;                                          loop = 1;
610                                          break;                                          break;
611                                    case '=':  // First topic article
612                                    case '\\': // Last topic article
613                                            page_id_cur = page_id;
614                                            direction = (ret == '=' ? -1 : 1);
615                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
616                                                                                                            &page_id, &selected_index, &article_count);
617                                            if (ret < 0)
618                                            {
619                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
620                                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
621                                                    return -3;
622                                            }
623                                            else if (ret > 0) // found
624                                            {
625                                                    if (page_id != page_id_cur) // page changed
626                                                    {
627                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
628                                                            if (ret < 0)
629                                                            {
630                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
631                                                                    return -3;
632                                                            }
633                                                    }
634                                                    loop = 1;
635                                            }
636                                            break;
637                                  }                                  }
638                          } while (loop);                          } while (loop);
639    
640                          // Update current topic                          // Update current topic
641                          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);
642                  case REFRESH_SCREEN:                  case CHANGE_NAME_DISPLAY:
643                            display_nickname = !display_nickname;
644                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
645                          {                          {
646                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
647                                  return -2;                                  return -2;
648                          }                          }
649                          break;                          break;
650                  case CHANGE_NAME_DISPLAY:                  case POST_ARTICLE:
651                          display_nickname = !display_nickname;                          if ((ret = article_post(p_section, &article_new)) < 0)
652                            {
653                                    log_error("article_post(sid=%d) error\n", p_section->sid);
654                            }
655                            else if (ret > 0) // New article posted
656                            {
657                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
658                                    if (ret < 0)
659                                    {
660                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
661                                            return -3;
662                                    }
663                            }
664                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
665                          {                          {
666                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
667                                  return -2;                                  return -2;
668                          }                          }
669                          break;                          break;
670                  case POST_ARTICLE:                  case EDIT_ARTICLE:
671                          if (article_post(p_section) < 0)                          if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
672                                    p_articles[selected_index]->uid != BBS_priv.uid)
673                            {
674                                    break; // No permission
675                            }
676                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
677                          {                          {
678                                  log_error("article_post(sid=%d, NEW) error\n", p_section->sid);                                  log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
679                          }                          }
680                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
681                          {                          {
# Line 581  int section_list_display(const char *sna Line 683  int section_list_display(const char *sna
683                                  return -2;                                  return -2;
684                          }                          }
685                          break;                          break;
686                  case EDIT_ARTICLE:                  case DELETE_ARTICLE:
687                          if (p_articles[selected_index]->uid != BBS_priv.uid)                          if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
688                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
689                          {                          {
690                                  break;                                  break; // No permission
691                          }                          }
692                          if (article_modify(p_section, p_articles[selected_index]) < 0)                          if ((ret = article_del(p_section, p_articles[selected_index])) < 0)
693                          {                          {
694                                  log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);                                  log_error("article_del(aid=%d) error\n", p_articles[selected_index]->aid);
695                          }                          }
696                            else if (ret > 0) // Article deleted
697                            {
698                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
699                                    if (ret < 0)
700                                    {
701                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
702                                            return -3;
703                                    }
704                            }
705                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
706                            {
707                                    log_error("section_list_draw_screen() error\n");
708                                    return -2;
709                            }
710                            break;
711                    case FIRST_TOPIC_ARTICLE:
712                    case LAST_TOPIC_ARTICLE:
713                            page_id_cur = page_id;
714                            direction = (ret == FIRST_TOPIC_ARTICLE ? -1 : 1);
715                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
716                                                                                            &page_id, &selected_index, &article_count);
717                            if (ret < 0)
718                            {
719                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
720                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
721                                    return -3;
722                            }
723                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
724                            {
725                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
726                                    if (ret < 0)
727                                    {
728                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
729                                            return -3;
730                                    }
731                            }
732                            break;
733                    case SHOW_HELP:
734                            // Display help information
735                            display_file(DATA_READ_HELP, 1);
736                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
737                          {                          {
738                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");


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

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