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