/[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.23 by sysadm, Sat Jun 14 02:00:38 2025 UTC Revision 1.34 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list_display.h"  
 #include "section_list_loader.h"  
17  #include "article_cache.h"  #include "article_cache.h"
18  #include "article_post.h"  #include "article_post.h"
19    #include "article_view_log.h"
20    #include "article_del.h"
21  #include "common.h"  #include "common.h"
22  #include "io.h"  #include "io.h"
 #include "screen.h"  
23  #include "log.h"  #include "log.h"
24  #include "user_priv.h"  #include "login.h"
25  #include "article_view_log.h"  #include "section_list_display.h"
26    #include "section_list_loader.h"
27    #include "screen.h"
28  #include "str_process.h"  #include "str_process.h"
29    #include "user_priv.h"
30    #include <string.h>
31  #include <time.h>  #include <time.h>
32  #include <sys/param.h>  #include <sys/param.h>
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
33    
34  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
35  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
# Line 38  enum select_cmd_t Line 39  enum select_cmd_t
39          EXIT_SECTION = 0,          EXIT_SECTION = 0,
40          VIEW_ARTICLE = 1,          VIEW_ARTICLE = 1,
41          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
42          REFRESH_SCREEN = 3,          SHOW_HELP = 3,
43          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
44          POST_ARTICLE = 5,          POST_ARTICLE = 5,
45          EDIT_ARTICLE = 6,          EDIT_ARTICLE = 6,
46            DELETE_ARTICLE = 7,
47            FIRST_TOPIC_ARTICLE = 8,
48            LAST_TOPIC_ARTICLE = 9,
49  };  };
50    
51  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 106  static int section_list_draw_items(int p
106                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
107                  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)));
108                  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)));
109                  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);
110                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
111                  {                  {
112                          title_f[len] = '\0';                          title_f[len] = '\0';
# Line 141  static int section_list_draw_screen(cons Line 145  static int section_list_draw_screen(cons
145          moveto(2, 0);          moveto(2, 0);
146          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] "
147                     "阅读[\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] "
148                     "修改[\033[1;32mCtrl-E\033[0;37m] %s[\033[1;32mn\033[0;37m]\033[m",                     "%s[\033[1;32mn\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
149                     (display_nickname ? "显示用户名" : "显示昵称"));                     (display_nickname ? "显示用户名" : "显示昵称"));
150          moveto(3, 0);          moveto(3, 0);
151          if (display_nickname)          if (display_nickname)
# Line 161  static enum select_cmd_t section_list_se Line 165  static enum select_cmd_t section_list_se
165          int old_page_id = *p_page_id;          int old_page_id = *p_page_id;
166          int old_selected_index = *p_selected_index;          int old_selected_index = *p_selected_index;
167          int ch;          int ch;
168            time_t last_refresh_tm = time(NULL);
         BBS_last_access_tm = time(0);  
169    
170          if (item_count > 0 && *p_selected_index >= 0)          if (item_count > 0 && *p_selected_index >= 0)
171          {          {
# Line 177  static enum select_cmd_t section_list_se Line 180  static enum select_cmd_t section_list_se
180    
181                  switch (ch)                  switch (ch)
182                  {                  {
                 case KEY_NULL: // broken pipe  
183                  case KEY_ESC:                  case KEY_ESC:
184                  case KEY_LEFT:                  case KEY_LEFT:
185                            BBS_last_access_tm = time(NULL);
186                    case KEY_NULL:                   // broken pipe
187                          return EXIT_SECTION; // exit section                          return EXIT_SECTION; // exit section
188                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
189                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
190                          {                          {
191                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
192                          }                          }
193                          continue;                          continue;
194                  case 'n':                  case 'n':
195                            BBS_last_access_tm = time(NULL);
196                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
197                  case CR:                  case CR:
198                          igetch_reset();                          igetch_reset();
199                    case 'r':
200                  case KEY_RIGHT:                  case KEY_RIGHT:
201                          if (item_count > 0)                          if (item_count > 0)
202                          {                          {
203                                    BBS_last_access_tm = time(NULL);
204                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
205                          }                          }
206                          break;                          break;
# Line 201  static enum select_cmd_t section_list_se Line 208  static enum select_cmd_t section_list_se
208                          return POST_ARTICLE;                          return POST_ARTICLE;
209                  case 'E':                  case 'E':
210                          return EDIT_ARTICLE;                          return EDIT_ARTICLE;
211                    case 'd':
212                            return DELETE_ARTICLE;
213                  case KEY_HOME:                  case KEY_HOME:
214                          *p_page_id = 0;                          *p_page_id = 0;
215                    case 'P':
216                  case KEY_PGUP:                  case KEY_PGUP:
217                          *p_selected_index = 0;                          *p_selected_index = 0;
218                    case 'k':
219                  case KEY_UP:                  case KEY_UP:
220                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
221                          {                          {
# Line 219  static enum select_cmd_t section_list_se Line 230  static enum select_cmd_t section_list_se
230                                  (*p_selected_index)--;                                  (*p_selected_index)--;
231                          }                          }
232                          break;                          break;
233                    case '$':
234                  case KEY_END:                  case KEY_END:
235                          if (total_page > 0)                          if (total_page > 0)
236                          {                          {
237                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
238                          }                          }
239                    case 'N':
240                  case KEY_PGDN:                  case KEY_PGDN:
241                          if (item_count > 0)                          if (item_count > 0)
242                          {                          {
243                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
244                          }                          }
245                    case 'j':
246                  case KEY_DOWN:                  case KEY_DOWN:
247                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
248                          {                          {
# Line 237  static enum select_cmd_t section_list_se Line 251  static enum select_cmd_t section_list_se
251                                          (*p_page_id)++;                                          (*p_page_id)++;
252                                          *p_selected_index = 0;                                          *p_selected_index = 0;
253                                  }                                  }
254                                    else // end of last page
255                                    {
256                                            return CHANGE_PAGE; // force refresh pages
257                                    }
258                          }                          }
259                          else                          else
260                          {                          {
261                                  (*p_selected_index)++;                                  (*p_selected_index)++;
262                          }                          }
263                          break;                          break;
264                    case '=':
265                            return FIRST_TOPIC_ARTICLE;
266                    case '\\':
267                            return LAST_TOPIC_ARTICLE;
268                    case 'h':
269                            return SHOW_HELP;
270                  default:                  default:
271                  }                  }
272    
# Line 268  static enum select_cmd_t section_list_se Line 292  static enum select_cmd_t section_list_se
292                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
293                  }                  }
294    
295                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(NULL);
296                    if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
297                    {
298                            return CHANGE_PAGE; // force section list refresh
299                    }
300          }          }
301    
302          return EXIT_SECTION;          return EXIT_SECTION;
# Line 279  static int display_article_key_handler(i Line 307  static int display_article_key_handler(i
307          switch (*p_key)          switch (*p_key)
308          {          {
309          case 'p':          case 'p':
310            case Ctrl('X'):
311                  section_topic_view_mode = !section_topic_view_mode;                  section_topic_view_mode = !section_topic_view_mode;
312          case 0: // Set msg          case 0: // Set msg
313                  if (section_topic_view_mode)                  if (section_topic_view_mode)
# Line 299  static int display_article_key_handler(i Line 328  static int display_article_key_handler(i
328                  break;                  break;
329          case 'r': // Reply article          case 'r': // Reply article
330                  return 1;                  return 1;
331            case '=': // First topic article
332                    return 1;
333            case '\\': // Last topic article
334                    return 1;
335          case KEY_UP:          case KEY_UP:
336          case KEY_PGUP:          case KEY_PGUP:
337          case KEY_HOME:          case KEY_HOME:
# Line 315  static int display_article_key_handler(i Line 348  static int display_article_key_handler(i
348                          return 1;                          return 1;
349                  }                  }
350                  break;                  break;
351            case 'k':
352                    if (section_topic_view_mode)
353                    {
354                            *p_key = KEY_PGUP;
355                    }
356                    else
357                    {
358                            *p_key = KEY_UP;
359                    }
360                    return 1;
361          case KEY_DOWN:          case KEY_DOWN:
362          case KEY_PGDN:          case KEY_PGDN:
363          case KEY_END:          case KEY_END:
# Line 331  static int display_article_key_handler(i Line 374  static int display_article_key_handler(i
374                          return 1;                          return 1;
375                  }                  }
376                  break;                  break;
377            case 'j':
378                    if (section_topic_view_mode)
379                    {
380                            *p_key = KEY_PGDN;
381                    }
382                    else
383                    {
384                            *p_key = KEY_DOWN;
385                    }
386                    return 1;
387          }          }
388    
389          return 0;          return 0;
# Line 353  int section_list_display(const char *sna Line 406  int section_list_display(const char *sna
406          int ret;          int ret;
407          int loop;          int loop;
408          int direction;          int direction;
409            ARTICLE article_new;
410            int page_id_cur;
411    
412          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
413          if (p_section == NULL)          if (p_section == NULL)
# Line 412  int section_list_display(const char *sna Line 467  int section_list_display(const char *sna
467                  show_bottom(page_info_str);                  show_bottom(page_info_str);
468                  iflush();                  iflush();
469    
470                    if (user_online_update(sname) < 0)
471                    {
472                            log_error("user_online_update(%s) error\n", sname);
473                    }
474    
475                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
476                  switch (ret)                  switch (ret)
477                  {                  {
# Line 444  int section_list_display(const char *sna Line 504  int section_list_display(const char *sna
504                                          break;                                          break;
505                                  }                                  }
506    
507                                    if (user_online_update("VIEW_ARTICLE") < 0)
508                                    {
509                                            log_error("user_online_update(VIEW_ARTICLE) error\n");
510                                    }
511    
512                                  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,
513                                                                     display_article_key_handler, DATA_READ_HELP);                                                                     display_article_key_handler, DATA_READ_HELP);
514    
# Line 526  int section_list_display(const char *sna Line 591  int section_list_display(const char *sna
591                                  case KEY_PGUP:                                  case KEY_PGUP:
592                                  case KEY_PGDN:                                  case KEY_PGDN:
593                                          direction = (ret == KEY_PGUP ? -1 : 1);                                          direction = (ret == KEY_PGUP ? -1 : 1);
594                                          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,
595                                                                                                            &page_id, &selected_index, &article_count);
596                                          if (ret < 0)                                          if (ret < 0)
597                                          {                                          {
598                                                  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",
599                                                                    p_section->sid, p_articles[selected_index]->aid, direction);                                                                    p_section->sid, p_articles[selected_index]->aid, direction);
600                                                  return -3;                                                  return -3;
601                                          }                                          }
602                                          else if (ret > 0)                                          else if (ret > 0) // found
603                                          {                                          {
604                                                  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);
605                                                  if (ret < 0)                                                  if (ret < 0)
# Line 545  int section_list_display(const char *sna Line 611  int section_list_display(const char *sna
611                                          }                                          }
612                                          break;                                          break;
613                                  case 'r': // Reply article                                  case 'r': // Reply article
614                                          if (article_reply(p_section, p_articles[selected_index]) < 0)                                          if (user_online_update("REPLY_ARTICLE") < 0)
615                                            {
616                                                    log_error("user_online_update(REPLY_ARTICLE) error\n");
617                                            }
618    
619                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
620                                          {                                          {
621                                                  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);
622                                          }                                          }
623                                          loop = 1;                                          loop = 1;
624                                          break;                                          break;
625                                    case '=':  // First topic article
626                                    case '\\': // Last topic article
627                                            page_id_cur = page_id;
628                                            direction = (ret == '=' ? -1 : 1);
629                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
630                                                                                                            &page_id, &selected_index, &article_count);
631                                            if (ret < 0)
632                                            {
633                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
634                                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
635                                                    return -3;
636                                            }
637                                            else if (ret > 0) // found
638                                            {
639                                                    if (page_id != page_id_cur) // page changed
640                                                    {
641                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
642                                                            if (ret < 0)
643                                                            {
644                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
645                                                                    return -3;
646                                                            }
647                                                    }
648                                                    loop = 1;
649                                            }
650                                            break;
651                                  }                                  }
652                          } while (loop);                          } while (loop);
653    
654                          // Update current topic                          // Update current topic
655                          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);
656                  case REFRESH_SCREEN:                  case CHANGE_NAME_DISPLAY:
657                            display_nickname = !display_nickname;
658                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
659                          {                          {
660                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
661                                  return -2;                                  return -2;
662                          }                          }
663                          break;                          break;
664                  case CHANGE_NAME_DISPLAY:                  case POST_ARTICLE:
665                          display_nickname = !display_nickname;                          if (user_online_update("POST_ARTICLE") < 0)
666                            {
667                                    log_error("user_online_update(POST_ARTICLE) error\n");
668                            }
669    
670                            if ((ret = article_post(p_section, &article_new)) < 0)
671                            {
672                                    log_error("article_post(sid=%d) error\n", p_section->sid);
673                            }
674                            else if (ret > 0) // New article posted
675                            {
676                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
677                                    if (ret < 0)
678                                    {
679                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
680                                            return -3;
681                                    }
682                            }
683                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
684                          {                          {
685                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
686                                  return -2;                                  return -2;
687                          }                          }
688                          break;                          break;
689                  case POST_ARTICLE:                  case EDIT_ARTICLE:
690                          if (article_post(p_section) < 0)                          if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
691                                    p_articles[selected_index]->uid != BBS_priv.uid)
692                            {
693                                    break; // No permission
694                            }
695    
696                            if (user_online_update("EDIT_ARTICLE") < 0)
697                          {                          {
698                                  log_error("article_post(sid=%d, NEW) error\n", p_section->sid);                                  log_error("user_online_update() error\n");
699                            }
700    
701                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
702                            {
703                                    log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
704                          }                          }
705                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
706                          {                          {
# Line 582  int section_list_display(const char *sna Line 708  int section_list_display(const char *sna
708                                  return -2;                                  return -2;
709                          }                          }
710                          break;                          break;
711                  case EDIT_ARTICLE:                  case DELETE_ARTICLE:
712                          if (p_articles[selected_index]->uid != BBS_priv.uid)                          if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
713                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
714                          {                          {
715                                  break;                                  break; // No permission
716                          }                          }
717                          if (article_modify(p_section, p_articles[selected_index]) < 0)                          if ((ret = article_del(p_section, p_articles[selected_index])) < 0)
718                          {                          {
719                                  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);
720                          }                          }
721                            else if (ret > 0) // Article deleted
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                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
731                            {
732                                    log_error("section_list_draw_screen() error\n");
733                                    return -2;
734                            }
735                            break;
736                    case FIRST_TOPIC_ARTICLE:
737                    case LAST_TOPIC_ARTICLE:
738                            page_id_cur = page_id;
739                            direction = (ret == FIRST_TOPIC_ARTICLE ? -1 : 1);
740                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, BBS_article_limit_per_section,
741                                                                                            &page_id, &selected_index, &article_count);
742                            if (ret < 0)
743                            {
744                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d, step=%d) error\n",
745                                                      p_section->sid, p_articles[selected_index]->aid, direction, BBS_article_limit_per_section);
746                                    return -3;
747                            }
748                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
749                            {
750                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
751                                    if (ret < 0)
752                                    {
753                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
754                                            return -3;
755                                    }
756                            }
757                            break;
758                    case SHOW_HELP:
759                            // Display help information
760                            display_file(DATA_READ_HELP, 1);
761                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
762                          {                          {
763                                  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