/[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.2 by sysadm, Wed May 28 07:41:46 2025 UTC Revision 1.22 by sysadm, Fri Jun 13 12:17:21 2025 UTC
# Line 16  Line 16 
16    
17  #include "section_list_display.h"  #include "section_list_display.h"
18  #include "section_list_loader.h"  #include "section_list_loader.h"
19    #include "article_cache.h"
20    #include "article_post.h"
21  #include "common.h"  #include "common.h"
22  #include "io.h"  #include "io.h"
23  #include "screen.h"  #include "screen.h"
24  #include "log.h"  #include "log.h"
25    #include "user_priv.h"
26    #include "article_view_log.h"
27  #include "str_process.h"  #include "str_process.h"
28  #include <time.h>  #include <time.h>
29    #include <sys/param.h>
30  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
31  #include <string.h>  #include <string.h>
32    
33    static int section_topic_view_mode = 0;
34    static int section_topic_view_tid = -1;
35    
36  enum select_cmd_t  enum select_cmd_t
37  {  {
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,          REFRESH_SCREEN = 3,
42            CHANGE_NAME_DISPLAY = 4,
43            POST_ARTICLE = 5,
44            EDIT_ARTICLE = 6,
45  };  };
46    
47  static int section_list_draw_items(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int article_count)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)
48  {  {
49          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
50          struct tm tm_sub;          struct tm tm_sub;
# Line 42  static int section_list_draw_items(SECTI Line 53  static int section_list_draw_items(SECTI
53          int eol;          int eol;
54          int len;          int len;
55          int i;          int i;
56            char article_flag;
57            int is_viewed;
58            time_t tm_now;
59    
60            time(&tm_now);
61    
62          clrline(4, 23);          clrline(4, 23);
63    
64          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
65          {          {
66                    if (p_articles[i]->uid == BBS_priv.uid)
67                    {
68                            is_viewed = 1;
69                    }
70                    else
71                    {
72                            is_viewed = article_view_log_is_viewed(p_articles[i]->aid, &BBS_article_view_log);
73                            if (is_viewed < 0)
74                            {
75                                    log_error("article_view_log_is_viewed(aid=%d) error\n", p_articles[i]->aid);
76                                    is_viewed = 0;
77                            }
78                    }
79    
80                    article_flag = (is_viewed ? ' ' : 'N');
81    
82                    if (p_articles[i]->excerption)
83                    {
84                            article_flag = (is_viewed ? 'm' : 'M');
85                    }
86                    else if (p_articles[i]->lock && is_viewed)
87                    {
88                            article_flag = 'x';
89                    }
90    
91                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);
92                  strftime(str_time, sizeof(str_time), "%b %e", &tm_sub);                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)
93                  strncpy(title_f, p_articles[i]->title, sizeof(title_f) - 1);                  {
94                            strftime(str_time, sizeof(str_time), "%b %e ", &tm_sub);
95                    }
96                    else
97                    {
98                            strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
99                    }
100    
101                    strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
102                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
103                  len = split_line(title_f, (p_articles[i]->tid == 0 ? 46 : 49), &eol, &title_f_len);                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
104                    strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
105                    len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);
106                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
107                  {                  {
108                          title_f[len] = '\0';                          title_f[len] = '\0';
109                  }                  }
110    
111                  moveto(4 + i, 1);                  moveto(4 + i, 1);
112                  prints("  %7d %s%*s %s  %s%s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
113                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
114                             p_articles[i]->aid,                             p_articles[i]->aid,
115                             p_articles[i]->username,                             article_flag,
116                             BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username)),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
117                               (display_nickname ? BBS_nickname_max_len - (int)strnlen(p_articles[i]->nickname, sizeof(p_articles[i]->nickname))
118                                                                     : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
119                             "",                             "",
120                             str_time,                             str_time,
121                             (p_articles[i]->tid == 0 ? "● " : ""),                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
122                             title_f);                             title_f);
123          }          }
124    
125          return 0;          return 0;
126  }  }
127    
128  static int section_list_draw_screen(SECTION_LIST *p_section)  static int section_list_draw_screen(const char *sname, const char *stitle, const char *master_list, int display_nickname)
129  {  {
130          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
131          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
132    
133          if (p_section->master_list[0] != '\0')          if (master_list[0] != '\0')
134          {          {
135                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", p_section->master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
136          }          }
137          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", p_section->sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
138    
139          clearscr();          clearscr();
140          show_top(str_section_master, p_section->stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
141          moveto(2, 0);          moveto(2, 0);
142          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] 阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m");          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] "
143                       "阅读[\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",
144                       (display_nickname ? "显示用户名" : "显示昵称"));
145          moveto(3, 0);          moveto(3, 0);
146          prints("\033[44;37m  \033[1;37m 编 号  发 布 者     日  期  文 章 标 题                                      \033[m");          if (display_nickname)
147          show_bottom("");          {
148                    prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
149            }
150            else
151            {
152                    prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
153            }
154    
155          return 0;          return 0;
156  }  }
# Line 124  static enum select_cmd_t section_list_se Line 186  static enum select_cmd_t section_list_se
186                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
187                          }                          }
188                          continue;                          continue;
189                    case 'n':
190                            return CHANGE_NAME_DISPLAY;
191                  case CR:                  case CR:
192                          igetch_reset();                          igetch_reset();
193                  case KEY_RIGHT:                  case KEY_RIGHT:
# Line 132  static enum select_cmd_t section_list_se Line 196  static enum select_cmd_t section_list_se
196                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
197                          }                          }
198                          break;                          break;
199                    case Ctrl('P'):
200                            return POST_ARTICLE;
201                    case 'E':
202                            return EDIT_ARTICLE;
203                  case KEY_HOME:                  case KEY_HOME:
204                          *p_page_id = 0;                          *p_page_id = 0;
205                  case KEY_PGUP:                  case KEY_PGUP:
# Line 205  static enum select_cmd_t section_list_se Line 273  static enum select_cmd_t section_list_se
273          return EXIT_SECTION;          return EXIT_SECTION;
274  }  }
275    
276    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
277    {
278            switch (*p_key)
279            {
280            case 'p':
281                    section_topic_view_mode = !section_topic_view_mode;
282            case 0: // Set msg
283                    if (section_topic_view_mode)
284                    {
285                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
286                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
287                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] | "
288                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
289                    }
290                    else
291                    {
292                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
293                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
294                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
295                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
296                    }
297                    *p_key = 0;
298                    break;
299            case 'r': // Reply article
300                    return 1;
301            case KEY_UP:
302            case KEY_PGUP:
303            case KEY_HOME:
304                    if (p_ctx->reach_begin)
305                    {
306                            if (section_topic_view_mode)
307                            {
308                                    *p_key = KEY_PGUP;
309                            }
310                            else
311                            {
312                                    *p_key = KEY_UP;
313                            }
314                            return 1;
315                    }
316                    break;
317            case KEY_DOWN:
318            case KEY_PGDN:
319            case KEY_END:
320                    if (p_ctx->reach_end)
321                    {
322                            if (section_topic_view_mode)
323                            {
324                                    *p_key = KEY_PGDN;
325                            }
326                            else
327                            {
328                                    *p_key = KEY_DOWN;
329                            }
330                            return 1;
331                    }
332                    break;
333            }
334    
335            return 0;
336    }
337    
338  int section_list_display(const char *sname)  int section_list_display(const char *sname)
339  {  {
340            static int display_nickname = 0;
341    
342          SECTION_LIST *p_section;          SECTION_LIST *p_section;
343            char stitle[BBS_section_title_max_len + 1];
344            char master_list[(BBS_username_max_len + 1) * 3 + 1];
345            char page_info_str[LINE_BUFFER_LEN];
346          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
347          int article_count;          int article_count;
348            int page_count;
349          int page_id = 0;          int page_id = 0;
350          int selected_index = 0;          int selected_index = 0;
351            ARTICLE_CACHE cache;
352          int ret;          int ret;
353            int loop;
354            int direction;
355    
356          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
357          if (p_section == NULL)          if (p_section == NULL)
# Line 221  int section_list_display(const char *sna Line 360  int section_list_display(const char *sna
360                  return -1;                  return -1;
361          }          }
362    
363          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
364            {
365                    log_error("section_list_rd_lock(sid = 0) error\n");
366                    return -2;
367            }
368    
369            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
370            stitle[sizeof(stitle) - 1] = '\0';
371            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
372            master_list[sizeof(master_list) - 1] = '\0';
373    
374            if ((ret = section_list_rd_unlock(p_section)) < 0)
375            {
376                    log_error("section_list_rd_unlock(sid = 0) error\n");
377                    return -2;
378            }
379    
380            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
381          {          {
382                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
383                  return -2;                  return -2;
384          }          }
385    
386          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
387            if (ret < 0)
388          {          {
389                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
390                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
391          }          }
392          else // empty section  
393            if (article_count == 0) // empty section
394          {          {
                 article_count = 0;  
395                  selected_index = 0;                  selected_index = 0;
396          }          }
397    
398          while (!SYS_server_exit)          while (!SYS_server_exit)
399          {          {
400                  ret = section_list_draw_items(p_section, page_id, p_articles, article_count);                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname);
401                  if (ret < 0)                  if (ret < 0)
402                  {                  {
403                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
404                          return -4;                          return -4;
405                  }                  }
406    
407                    snprintf(page_info_str, sizeof(page_info_str),
408                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
409                                     page_id + 1, MAX(page_count, 1));
410    
411                    show_bottom(page_info_str);
412                  iflush();                  iflush();
413    
414                  ret = section_list_select(p_section->page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
415                  switch (ret)                  switch (ret)
416                  {                  {
417                  case EXIT_SECTION:                  case EXIT_SECTION:
418                          return 0;                          return 0;
419                  case CHANGE_PAGE:                  case CHANGE_PAGE:
420                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
421                            if (ret < 0)
422                          {                          {
423                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
424                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
425                          }                          }
426                          else // empty section                          if (article_count == 0) // empty section
427                          {                          {
                                 article_count = 0;  
428                                  selected_index = 0;                                  selected_index = 0;
429                          }                          }
430                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
431                          {                          {
432                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
433                          }                          }
434                          break;                          break;
435                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
436                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          do
437                            {
438                                    loop = 0;
439    
440                                    if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
441                                    {
442                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
443                                            break;
444                                    }
445    
446                                    ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
447                                                                       display_article_key_handler, DATA_READ_HELP);
448    
449                                    if (article_cache_unload(&cache) < 0)
450                                    {
451                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
452                                            break;
453                                    }
454    
455                                    // Update article_view_log
456                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
457                                    {
458                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
459                                    }
460    
461                                    switch (ret)
462                                    {
463                                    case KEY_UP:
464                                            if (selected_index <= 0)
465                                            {
466                                                    if (page_id > 0)
467                                                    {
468                                                            page_id--;
469                                                            selected_index = BBS_article_limit_per_page - 1;
470    
471                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
472                                                            if (ret < 0)
473                                                            {
474                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
475                                                                    return -3;
476                                                            }
477    
478                                                            if (article_count != BBS_article_limit_per_page) // page is not full
479                                                            {
480                                                                    selected_index = MAX(0, article_count - 1);
481                                                            }
482                                                            else
483                                                            {
484                                                                    loop = 1;
485                                                            }
486                                                    }
487                                            }
488                                            else
489                                            {
490                                                    selected_index--;
491                                                    loop = 1;
492                                            }
493                                            break;
494                                    case KEY_DOWN:
495                                            if (selected_index + 1 >= article_count) // next page
496                                            {
497                                                    if (page_id + 1 < page_count)
498                                                    {
499                                                            page_id++;
500                                                            selected_index = 0;
501    
502                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
503                                                            if (ret < 0)
504                                                            {
505                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
506                                                                    return -3;
507                                                            }
508    
509                                                            if (article_count == 0) // empty page
510                                                            {
511                                                                    selected_index = 0;
512                                                            }
513                                                            else
514                                                            {
515                                                                    loop = 1;
516                                                            }
517                                                    }
518                                            }
519                                            else
520                                            {
521                                                    selected_index++;
522                                                    loop = 1;
523                                            }
524                                            break;
525                                    case KEY_PGUP:
526                                    case KEY_PGDN:
527                                            direction = (ret == KEY_PGUP ? -1 : 1);
528                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
529                                            if (ret < 0)
530                                            {
531                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
532                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
533                                                    return -3;
534                                            }
535                                            else if (ret > 0)
536                                            {
537                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
538                                                    if (ret < 0)
539                                                    {
540                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
541                                                            return -3;
542                                                    }
543                                                    loop = 1;
544                                            }
545                                            break;
546                                    case 'r': // Reply article
547                                            if (article_reply(p_section, p_articles[selected_index]) < 0)
548                                            {
549                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
550                                            }
551                                            loop = 1;
552                                            break;
553                                    }
554                            } while (loop);
555    
556                            // Update current topic
557                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
558                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
559                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
560                            {
561                                    log_error("section_list_draw_screen() error\n");
562                                    return -2;
563                            }
564                            break;
565                    case CHANGE_NAME_DISPLAY:
566                            display_nickname = !display_nickname;
567                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
568                            {
569                                    log_error("section_list_draw_screen() error\n");
570                                    return -2;
571                            }
572                            break;
573                    case POST_ARTICLE:
574                            if (article_post(p_section) < 0)
575                            {
576                                    log_error("article_post(sid=%d, NEW) error\n", p_section->sid);
577                            }
578                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
579                            {
580                                    log_error("section_list_draw_screen() error\n");
581                                    return -2;
582                            }
583                            break;
584                    case EDIT_ARTICLE:
585                            if (p_articles[selected_index]->uid != BBS_priv.uid)
586                            {
587                                    break;
588                            }
589                            if (article_modify(p_section, p_articles[selected_index]) < 0)
590                            {
591                                    log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);
592                            }
593                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
594                          {                          {
595                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
596                                  return -2;                                  return -2;


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

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