/[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.14 by sysadm, Sat May 31 12:46:08 2025 UTC Revision 1.29 by sysadm, Mon Jun 16 14:30:44 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"
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"
27  #include "log.h"  #include "log.h"
28    #include "user_priv.h"
29    #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 37  enum select_cmd_t Line 42  enum select_cmd_t
42          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
43          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
44          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
45            POST_ARTICLE = 5,
46            EDIT_ARTICLE = 6,
47            DELETE_ARTICLE = 7,
48            SHOW_HELP = 8,
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 49  static int section_list_draw_items(int p Line 58  static int section_list_draw_items(int p
58          int len;          int len;
59          int i;          int i;
60          char article_flag;          char article_flag;
61            int is_viewed;
62          time_t tm_now;          time_t tm_now;
63    
64          time(&tm_now);          time(&tm_now);
# Line 57  static int section_list_draw_items(int p Line 67  static int section_list_draw_items(int p
67    
68          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
69          {          {
70                  article_flag = ' ';                  if (p_articles[i]->uid == BBS_priv.uid)
71                    {
72                            is_viewed = 1;
73                    }
74                    else
75                    {
76                            is_viewed = article_view_log_is_viewed(p_articles[i]->aid, &BBS_article_view_log);
77                            if (is_viewed < 0)
78                            {
79                                    log_error("article_view_log_is_viewed(aid=%d) error\n", p_articles[i]->aid);
80                                    is_viewed = 0;
81                            }
82                    }
83    
84                    article_flag = (is_viewed ? ' ' : 'N');
85    
86                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
87                  {                  {
88                          article_flag = 'm';                          article_flag = (is_viewed ? 'm' : 'M');
89                  }                  }
90                  else if (p_articles[i]->lock)                  else if (p_articles[i]->lock && is_viewed)
91                  {                  {
92                          article_flag = 'x';                          article_flag = 'x';
93                  }                  }
# Line 82  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 120  static int section_list_draw_screen(cons Line 144  static int section_list_draw_screen(cons
144          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
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[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] "
148                       "%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 170  static enum select_cmd_t section_list_se Line 195  static enum select_cmd_t section_list_se
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                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
203                          }                          }
204                          break;                          break;
205                    case Ctrl('P'):
206                            return POST_ARTICLE;
207                    case 'E':
208                            return EDIT_ARTICLE;
209                    case 'd':
210                            return DELETE_ARTICLE;
211                  case KEY_HOME:                  case KEY_HOME:
212                          *p_page_id = 0;                          *p_page_id = 0;
213                    case 'P':
214                  case KEY_PGUP:                  case KEY_PGUP:
215                          *p_selected_index = 0;                          *p_selected_index = 0;
216                    case 'k':
217                  case KEY_UP:                  case KEY_UP:
218                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
219                          {                          {
# Line 194  static enum select_cmd_t section_list_se Line 228  static enum select_cmd_t section_list_se
228                                  (*p_selected_index)--;                                  (*p_selected_index)--;
229                          }                          }
230                          break;                          break;
231                    case '$':
232                  case KEY_END:                  case KEY_END:
233                          if (total_page > 0)                          if (total_page > 0)
234                          {                          {
235                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
236                          }                          }
237                    case 'N':
238                  case KEY_PGDN:                  case KEY_PGDN:
239                          if (item_count > 0)                          if (item_count > 0)
240                          {                          {
241                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
242                          }                          }
243                    case 'j':
244                  case KEY_DOWN:                  case KEY_DOWN:
245                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
246                          {                          {
# Line 212  static enum select_cmd_t section_list_se Line 249  static enum select_cmd_t section_list_se
249                                          (*p_page_id)++;                                          (*p_page_id)++;
250                                          *p_selected_index = 0;                                          *p_selected_index = 0;
251                                  }                                  }
252                                    else // end of last page
253                                    {
254                                            return CHANGE_PAGE; // force refresh pages
255                                    }
256                          }                          }
257                          else                          else
258                          {                          {
259                                  (*p_selected_index)++;                                  (*p_selected_index)++;
260                          }                          }
261                          break;                          break;
262                    case 'h':
263                            return SHOW_HELP;
264                  default:                  default:
265                  }                  }
266    
# Line 254  static int display_article_key_handler(i Line 297  static int display_article_key_handler(i
297          switch (*p_key)          switch (*p_key)
298          {          {
299          case 'p':          case 'p':
300            case Ctrl('X'):
301                  section_topic_view_mode = !section_topic_view_mode;                  section_topic_view_mode = !section_topic_view_mode;
302          case 0: // Set msg          case 0: // Set msg
303                  if (section_topic_view_mode)                  if (section_topic_view_mode)
304                  {                  {
305                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
306                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
307                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ "                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
308                                           "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
309                  }                  }
310                  else                  else
311                  {                  {
312                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
313                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
314                                           "移动[\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] "
315                                           "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
316                  }                  }
317                  *p_key = 0;                  *p_key = 0;
318                  break;                  break;
319            case 'r': // Reply article
320                    return 1;
321          case KEY_UP:          case KEY_UP:
322            case KEY_PGUP:
323            case KEY_HOME:
324                  if (p_ctx->reach_begin)                  if (p_ctx->reach_begin)
325                  {                  {
326                          if (section_topic_view_mode)                          if (section_topic_view_mode)
327                          {                          {
328                                  *p_key = KEY_PGUP;                                  *p_key = KEY_PGUP;
329                          }                          }
330                            else
331                            {
332                                    *p_key = KEY_UP;
333                            }
334                          return 1;                          return 1;
335                  }                  }
336                  break;                  break;
337            case 'k':
338                    if (section_topic_view_mode)
339                    {
340                            *p_key = KEY_PGUP;
341                    }
342                    else
343                    {
344                            *p_key = KEY_UP;
345                    }
346                    return 1;
347          case KEY_DOWN:          case KEY_DOWN:
348            case KEY_PGDN:
349            case KEY_END:
350                  if (p_ctx->reach_end)                  if (p_ctx->reach_end)
351                  {                  {
352                          if (section_topic_view_mode)                          if (section_topic_view_mode)
353                          {                          {
354                                  *p_key = KEY_PGDN;                                  *p_key = KEY_PGDN;
355                          }                          }
356                            else
357                            {
358                                    *p_key = KEY_DOWN;
359                            }
360                          return 1;                          return 1;
361                  }                  }
362                  break;                  break;
363            case 'j':
364                    if (section_topic_view_mode)
365                    {
366                            *p_key = KEY_PGDN;
367                    }
368                    else
369                    {
370                            *p_key = KEY_DOWN;
371                    }
372                    return 1;
373          }          }
374    
375          return 0;          return 0;
# Line 314  int section_list_display(const char *sna Line 392  int section_list_display(const char *sna
392          int ret;          int ret;
393          int loop;          int loop;
394          int direction;          int direction;
395            ARTICLE article_new;
396    
397          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
398          if (p_section == NULL)          if (p_section == NULL)
# Line 405  int section_list_display(const char *sna Line 484  int section_list_display(const char *sna
484                                          break;                                          break;
485                                  }                                  }
486    
487                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
488                                                                     display_article_key_handler, DATA_READ_HELP);                                                                     display_article_key_handler, DATA_READ_HELP);
489    
490                                  if (article_cache_unload(&cache) < 0)                                  if (article_cache_unload(&cache) < 0)
# Line 414  int section_list_display(const char *sna Line 493  int section_list_display(const char *sna
493                                          break;                                          break;
494                                  }                                  }
495    
496                                    // Update article_view_log
497                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
498                                    {
499                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
500                                    }
501    
502                                  switch (ret)                                  switch (ret)
503                                  {                                  {
504                                  case KEY_UP:                                  case KEY_UP:
# Line 499  int section_list_display(const char *sna Line 584  int section_list_display(const char *sna
584                                                  loop = 1;                                                  loop = 1;
585                                          }                                          }
586                                          break;                                          break;
587                                    case 'r': // Reply article
588                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
589                                            {
590                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
591                                            }
592                                            loop = 1;
593                                            break;
594                                  }                                  }
595                          } while (loop);                          } while (loop);
596    
# Line 516  int section_list_display(const char *sna Line 608  int section_list_display(const char *sna
608                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
609                          {                          {
610                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
611                                    return -2;
612                            }
613                            break;
614                    case POST_ARTICLE:
615                            ret = article_post(p_section, &article_new);
616                            if (ret < 0)
617                            {
618                                    log_error("article_post(sid=%d) error\n", p_section->sid);
619                            }
620                            else if (ret > 0) // New article posted
621                            {
622                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
623                                    if (ret < 0)
624                                    {
625                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
626                                            return -3;
627                                    }
628                            }
629                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
630                            {
631                                    log_error("section_list_draw_screen() error\n");
632                                    return -2;
633                            }
634                            break;
635                    case EDIT_ARTICLE:
636                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
637                                    p_articles[selected_index]->uid != BBS_priv.uid)
638                            {
639                                    break; // No permission
640                            }
641                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
642                            {
643                                    log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
644                            }
645                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
646                            {
647                                    log_error("section_list_draw_screen() error\n");
648                                    return -2;
649                            }
650                            break;
651                    case DELETE_ARTICLE:
652                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
653                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
654                            {
655                                    break; // No permission
656                            }
657                            if (article_del(p_section, p_articles[selected_index]) < 0)
658                            {
659                                    log_error("article_del(aid=%d) error\n", p_articles[selected_index]->aid);
660                            }
661                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
662                            {
663                                    log_error("section_list_draw_screen() error\n");
664                                    return -2;
665                            }
666                            break;
667                    case SHOW_HELP:
668                            // Display help information
669                            display_file(DATA_READ_HELP, 1);
670                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
671                            {
672                                    log_error("section_list_draw_screen() error\n");
673                                  return -2;                                  return -2;
674                          }                          }
675                          break;                          break;


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

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