/[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.4 by sysadm, Wed May 28 13:08:41 2025 UTC Revision 1.25 by sysadm, Sat Jun 14 13:33:48 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,
# Line 32  enum select_cmd_t Line 40  enum select_cmd_t
40          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
41          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
42          CHANGE_NAME_DISPLAY = 4,          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, int display_nickname)  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 44  static int section_list_draw_items(SECTI Line 54  static int section_list_draw_items(SECTI
54          int len;          int len;
55          int i;          int i;
56          char article_flag;          char article_flag;
57            int is_viewed;
58          time_t tm_now;          time_t tm_now;
59    
60          time(&tm_now);          time(&tm_now);
# Line 52  static int section_list_draw_items(SECTI Line 63  static int section_list_draw_items(SECTI
63    
64          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
65          {          {
66                  article_flag = ' ';                  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)                  if (p_articles[i]->excerption)
83                  {                  {
84                          article_flag = 'm';                          article_flag = (is_viewed ? 'm' : 'M');
85                  }                  }
86                  else if (p_articles[i]->lock)                  else if (p_articles[i]->lock && is_viewed)
87                  {                  {
88                          article_flag = 'x';                          article_flag = 'x';
89                  }                  }
# Line 84  static int section_list_draw_items(SECTI Line 109  static int section_list_draw_items(SECTI
109                  }                  }
110    
111                  moveto(4 + i, 1);                  moveto(4 + i, 1);
112                  prints("  %7d %c %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                             article_flag,                             article_flag,
116                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
# Line 92  static int section_list_draw_items(SECTI Line 118  static int section_list_draw_items(SECTI
118                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),                                                                   : 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]->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, int display_nickname)  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 昵称[\033[1;32mn\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] "
144                       "修改[\033[1;32mE\033[0;37m] %s[\033[1;32mn\033[0;37m]\033[m",
145                       (display_nickname ? "显示用户名" : "显示昵称"));
146          moveto(3, 0);          moveto(3, 0);
147          if (display_nickname)          if (display_nickname)
148          {          {
149                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者 昵 称       日  期  文 章 标 题                            \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
150          }          }
151          else          else
152          {          {
153                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文 章 标 题                                    \033[m");                  prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
154          }          }
         show_bottom("");  
155    
156          return 0;          return 0;
157  }  }
# Line 168  static enum select_cmd_t section_list_se Line 197  static enum select_cmd_t section_list_se
197                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
198                          }                          }
199                          break;                          break;
200                    case Ctrl('P'):
201                            return POST_ARTICLE;
202                    case 'E':
203                            return EDIT_ARTICLE;
204                  case KEY_HOME:                  case KEY_HOME:
205                          *p_page_id = 0;                          *p_page_id = 0;
206                  case KEY_PGUP:                  case KEY_PGUP:
# Line 204  static enum select_cmd_t section_list_se Line 237  static enum select_cmd_t section_list_se
237                                          (*p_page_id)++;                                          (*p_page_id)++;
238                                          *p_selected_index = 0;                                          *p_selected_index = 0;
239                                  }                                  }
240                                    else // end of last page
241                                    {
242                                            return CHANGE_PAGE; // force refresh pages
243                                    }
244                          }                          }
245                          else                          else
246                          {                          {
# Line 241  static enum select_cmd_t section_list_se Line 278  static enum select_cmd_t section_list_se
278          return EXIT_SECTION;          return EXIT_SECTION;
279  }  }
280    
281    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
282    {
283            switch (*p_key)
284            {
285            case 'p':
286                    section_topic_view_mode = !section_topic_view_mode;
287            case 0: // Set msg
288                    if (section_topic_view_mode)
289                    {
290                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
291                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
292                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
293                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
294                    }
295                    else
296                    {
297                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
298                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
299                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] "
300                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
301                    }
302                    *p_key = 0;
303                    break;
304            case 'r': // Reply article
305                    return 1;
306            case KEY_UP:
307            case KEY_PGUP:
308            case KEY_HOME:
309                    if (p_ctx->reach_begin)
310                    {
311                            if (section_topic_view_mode)
312                            {
313                                    *p_key = KEY_PGUP;
314                            }
315                            else
316                            {
317                                    *p_key = KEY_UP;
318                            }
319                            return 1;
320                    }
321                    break;
322            case KEY_DOWN:
323            case KEY_PGDN:
324            case KEY_END:
325                    if (p_ctx->reach_end)
326                    {
327                            if (section_topic_view_mode)
328                            {
329                                    *p_key = KEY_PGDN;
330                            }
331                            else
332                            {
333                                    *p_key = KEY_DOWN;
334                            }
335                            return 1;
336                    }
337                    break;
338            }
339    
340            return 0;
341    }
342    
343  int section_list_display(const char *sname)  int section_list_display(const char *sname)
344  {  {
345          static int display_nickname = 0;          static int display_nickname = 0;
346    
347          SECTION_LIST *p_section;          SECTION_LIST *p_section;
348            char stitle[BBS_section_title_max_len + 1];
349            char master_list[(BBS_username_max_len + 1) * 3 + 1];
350            char page_info_str[LINE_BUFFER_LEN];
351          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
352          int article_count;          int article_count;
353            int page_count;
354          int page_id = 0;          int page_id = 0;
355          int selected_index = 0;          int selected_index = 0;
356            ARTICLE_CACHE cache;
357          int ret;          int ret;
358            int loop;
359            int direction;
360            ARTICLE article_new;
361    
362          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
363          if (p_section == NULL)          if (p_section == NULL)
# Line 259  int section_list_display(const char *sna Line 366  int section_list_display(const char *sna
366                  return -1;                  return -1;
367          }          }
368    
369          if (section_list_draw_screen(p_section, display_nickname) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
370            {
371                    log_error("section_list_rd_lock(sid = 0) error\n");
372                    return -2;
373            }
374    
375            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
376            stitle[sizeof(stitle) - 1] = '\0';
377            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
378            master_list[sizeof(master_list) - 1] = '\0';
379    
380            if ((ret = section_list_rd_unlock(p_section)) < 0)
381            {
382                    log_error("section_list_rd_unlock(sid = 0) error\n");
383                    return -2;
384            }
385    
386            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
387          {          {
388                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
389                  return -2;                  return -2;
390          }          }
391    
392          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
393            if (ret < 0)
394          {          {
395                  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);
396                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
397          }          }
398          else // empty section  
399            if (article_count == 0) // empty section
400          {          {
                 article_count = 0;  
401                  selected_index = 0;                  selected_index = 0;
402          }          }
403    
404          while (!SYS_server_exit)          while (!SYS_server_exit)
405          {          {
406                  ret = section_list_draw_items(p_section, page_id, p_articles, article_count, display_nickname);                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname);
407                  if (ret < 0)                  if (ret < 0)
408                  {                  {
409                          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);
410                          return -4;                          return -4;
411                  }                  }
412    
413                    snprintf(page_info_str, sizeof(page_info_str),
414                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
415                                     page_id + 1, MAX(page_count, 1));
416    
417                    show_bottom(page_info_str);
418                  iflush();                  iflush();
419    
420                  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);
421                  switch (ret)                  switch (ret)
422                  {                  {
423                  case EXIT_SECTION:                  case EXIT_SECTION:
424                          return 0;                          return 0;
425                  case CHANGE_PAGE:                  case CHANGE_PAGE:
426                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
427                            if (ret < 0)
428                          {                          {
429                                  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);
430                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
431                          }                          }
432                          else // empty section                          if (article_count == 0) // empty section
433                          {                          {
                                 article_count = 0;  
434                                  selected_index = 0;                                  selected_index = 0;
435                          }                          }
436                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
437                          {                          {
438                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
439                          }                          }
440                          break;                          break;
441                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
442                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          do
443                            {
444                                    loop = 0;
445    
446                                    if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
447                                    {
448                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
449                                            break;
450                                    }
451    
452                                    ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
453                                                                       display_article_key_handler, DATA_READ_HELP);
454    
455                                    if (article_cache_unload(&cache) < 0)
456                                    {
457                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
458                                            break;
459                                    }
460    
461                                    // Update article_view_log
462                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
463                                    {
464                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
465                                    }
466    
467                                    switch (ret)
468                                    {
469                                    case KEY_UP:
470                                            if (selected_index <= 0)
471                                            {
472                                                    if (page_id > 0)
473                                                    {
474                                                            page_id--;
475                                                            selected_index = BBS_article_limit_per_page - 1;
476    
477                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
478                                                            if (ret < 0)
479                                                            {
480                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
481                                                                    return -3;
482                                                            }
483    
484                                                            if (article_count != BBS_article_limit_per_page) // page is not full
485                                                            {
486                                                                    selected_index = MAX(0, article_count - 1);
487                                                            }
488                                                            else
489                                                            {
490                                                                    loop = 1;
491                                                            }
492                                                    }
493                                            }
494                                            else
495                                            {
496                                                    selected_index--;
497                                                    loop = 1;
498                                            }
499                                            break;
500                                    case KEY_DOWN:
501                                            if (selected_index + 1 >= article_count) // next page
502                                            {
503                                                    if (page_id + 1 < page_count)
504                                                    {
505                                                            page_id++;
506                                                            selected_index = 0;
507    
508                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
509                                                            if (ret < 0)
510                                                            {
511                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
512                                                                    return -3;
513                                                            }
514    
515                                                            if (article_count == 0) // empty page
516                                                            {
517                                                                    selected_index = 0;
518                                                            }
519                                                            else
520                                                            {
521                                                                    loop = 1;
522                                                            }
523                                                    }
524                                            }
525                                            else
526                                            {
527                                                    selected_index++;
528                                                    loop = 1;
529                                            }
530                                            break;
531                                    case KEY_PGUP:
532                                    case KEY_PGDN:
533                                            direction = (ret == KEY_PGUP ? -1 : 1);
534                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
535                                            if (ret < 0)
536                                            {
537                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
538                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
539                                                    return -3;
540                                            }
541                                            else if (ret > 0)
542                                            {
543                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
544                                                    if (ret < 0)
545                                                    {
546                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
547                                                            return -3;
548                                                    }
549                                                    loop = 1;
550                                            }
551                                            break;
552                                    case 'r': // Reply article
553                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
554                                            {
555                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
556                                            }
557                                            loop = 1;
558                                            break;
559                                    }
560                            } while (loop);
561    
562                            // Update current topic
563                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
564                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
565                          if (section_list_draw_screen(p_section, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
566                          {                          {
567                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
568                                  return -2;                                  return -2;
# Line 326  int section_list_display(const char *sna Line 570  int section_list_display(const char *sna
570                          break;                          break;
571                  case CHANGE_NAME_DISPLAY:                  case CHANGE_NAME_DISPLAY:
572                          display_nickname = !display_nickname;                          display_nickname = !display_nickname;
573                          if (section_list_draw_screen(p_section, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
574                            {
575                                    log_error("section_list_draw_screen() error\n");
576                                    return -2;
577                            }
578                            break;
579                    case POST_ARTICLE:
580                            ret = article_post(p_section, &article_new);
581                            if (ret < 0)
582                            {
583                                    log_error("article_post(sid=%d, NEW) error\n", p_section->sid);
584                            }
585                            else if (ret > 0) // New article posted
586                            {
587                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
588                                    if (ret < 0)
589                                    {
590                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
591                                            return -3;
592                                    }
593                            }
594                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
595                            {
596                                    log_error("section_list_draw_screen() error\n");
597                                    return -2;
598                            }
599                            break;
600                    case EDIT_ARTICLE:
601                            if (p_articles[selected_index]->uid != BBS_priv.uid)
602                            {
603                                    break;
604                            }
605                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
606                            {
607                                    log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);
608                            }
609                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
610                          {                          {
611                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
612                                  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