/[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.48 by sysadm, Mon Oct 13 07:35:48 2025 UTC Revision 1.81 by sysadm, Wed Dec 17 03:47:00 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                          section_list_display.c  -  description  /*
3                                                           -------------------   * section_list_display
4          Copyright            : (C) 2004-2025 by Leaflet   *   - user interactive feature of section articles list
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "article_cache.h"  #include "article_cache.h"
14    #include "article_favor.h"
15  #include "article_op.h"  #include "article_op.h"
16  #include "article_post.h"  #include "article_post.h"
17  #include "article_view_log.h"  #include "article_view_log.h"
# Line 29  Line 26 
26  #include "section_list_loader.h"  #include "section_list_loader.h"
27  #include "screen.h"  #include "screen.h"
28  #include "str_process.h"  #include "str_process.h"
29    #include "user_info_display.h"
30    #include "user_list_display.h"
31  #include "user_priv.h"  #include "user_priv.h"
32    #include <ctype.h>
33    #include <errno.h>
34  #include <string.h>  #include <string.h>
35  #include <time.h>  #include <time.h>
36  #include <sys/param.h>  #include <sys/param.h>
37    
38  static int section_aid_locations[BBS_max_section] = {0};  enum _section_list_display_constant_t
39    {
40            TITLE_SEARCH_MAX_LEN = 60,
41    };
42    
43    static int32_t section_aid_locations[BBS_max_section] = {0};
44  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
45  static int section_topic_view_tid = -1;  static int section_topic_view_tid = -1;
46    
# Line 49  enum select_cmd_t Line 55  enum select_cmd_t
55          EDIT_ARTICLE,          EDIT_ARTICLE,
56          DELETE_ARTICLE,          DELETE_ARTICLE,
57          QUERY_ARTICLE,          QUERY_ARTICLE,
58            QUERY_USER,
59            SET_FAVOR_ARTICLE,
60            UNSET_FAVOR_ARTICLE,
61          FIRST_TOPIC_ARTICLE,          FIRST_TOPIC_ARTICLE,
62          LAST_TOPIC_ARTICLE,          LAST_TOPIC_ARTICLE,
63            LAST_SECTION_ARTICLE,
64          SCAN_NEW_ARTICLE,          SCAN_NEW_ARTICLE,
65            SCAN_ARTICLE_BACKWARD_BY_USER,
66            SCAN_ARTICLE_FORWARD_BY_USER,
67            SCAN_ARTICLE_BACKWARD_BY_TITLE,
68            SCAN_ARTICLE_FORWARD_BY_TITLE,
69          VIEW_EX_DIR,          VIEW_EX_DIR,
70          SHOW_TOP10,          SHOW_TOP10,
71            SEARCH_USER,
72  };  };
73    
74  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname, int ontop_start_offset)  static int section_list_draw_items(int page_id, const ARTICLE *p_articles[], int article_count, int display_nickname, int ontop_start_offset)
75  {  {
76          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
77          struct tm tm_sub;          struct tm tm_sub;
# Line 68  static int section_list_draw_items(int p Line 83  static int section_list_draw_items(int p
83          size_t j;          size_t j;
84          char article_flag;          char article_flag;
85          int is_viewed;          int is_viewed;
86            int is_favor;
87          time_t tm_now;          time_t tm_now;
88    
89          time(&tm_now);          time(&tm_now);
# Line 90  static int section_list_draw_items(int p Line 106  static int section_list_draw_items(int p
106                          }                          }
107                  }                  }
108    
109                    if (p_articles[i]->tid == 0)
110                    {
111                            is_favor = article_favor_check(p_articles[i]->aid, &BBS_article_favor);
112                            if (is_favor < 0)
113                            {
114                                    log_error("article_favor_check(aid=%d) error\n", p_articles[i]->aid);
115                                    is_favor = 0;
116                            }
117                    }
118                    else
119                    {
120                            is_favor = 0;
121                    }
122    
123                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
124                  {                  {
125                          article_flag = (is_viewed ? 'm' : 'M');                          article_flag = (is_viewed ? 'm' : 'M');
# Line 136  static int section_list_draw_items(int p Line 166  static int section_list_draw_items(int p
166                  }                  }
167                  strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, p_articles[i]->title + j, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
168    
169                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);                  len = split_line(title_f, 59 - (display_nickname ? BBS_nickname_max_len / 2 : BBS_username_max_len), &eol, &title_f_len, 1);
170                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
171                  {                  {
172                          title_f[len] = '\0';                          title_f[len] = '\0';
# Line 145  static int section_list_draw_items(int p Line 175  static int section_list_draw_items(int p
175                  moveto(4 + i, 1);                  moveto(4 + i, 1);
176                  if (i >= ontop_start_offset)                  if (i >= ontop_start_offset)
177                  {                  {
178                          prints("   \033[1;33m[提示]\033[m %c %s%*s %s %s%s\033[m",                          prints("   \033[1;33m[提示]\033[m%c%c %s%*s %s %s%s\033[m",
179                                       (is_favor ? '@' : ' '),
180                                     article_flag,                                     article_flag,
181                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
182                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
# Line 161  static int section_list_draw_items(int p Line 192  static int section_list_draw_items(int p
192                  }                  }
193                  else                  else
194                  {                  {
195                          prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",                          prints("  %s%7d\033[m%c%c %s%*s %s %s%s\033[m",
196                                     (p_articles[i]->aid == section_topic_view_tid                                     (p_articles[i]->aid == section_topic_view_tid
197                                                  ? "\033[1;33m"                                                  ? "\033[1;33m"
198                                                  : (p_articles[i]->tid == section_topic_view_tid                                                  : (p_articles[i]->tid == section_topic_view_tid
199                                                             ? "\033[1;36m"                                                             ? "\033[1;36m"
200                                                             : "")),                                                             : "")),
201                                     p_articles[i]->aid,                                     p_articles[i]->aid,
202                                       (is_favor ? '@' : ' '),
203                                     article_flag,                                     article_flag,
204                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                                     (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
205                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)                                     (display_nickname ? BBS_nickname_max_len / 2 - str_length(p_articles[i]->nickname, 1)
# Line 193  static int section_list_draw_screen(cons Line 225  static int section_list_draw_screen(cons
225    
226          if (master_list[0] != '\0')          if (master_list[0] != '\0')
227          {          {
228                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主: %s", master_list);
229          }          }
230          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
231    
# Line 235  static enum select_cmd_t section_list_se Line 267  static enum select_cmd_t section_list_se
267          {          {
268                  ch = igetch(100);                  ch = igetch(100);
269    
270                  switch (ch)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
271                  {                  {
                 case KEY_ESC:  
                 case KEY_LEFT:  
272                          BBS_last_access_tm = time(NULL);                          BBS_last_access_tm = time(NULL);
273                  case KEY_NULL:                   // broken pipe  
274                          return EXIT_SECTION; // exit section                          // Refresh current action
275                            if (user_online_update(NULL) < 0)
276                            {
277                                    log_error("user_online_update(NULL) error\n");
278                            }
279                    }
280    
281                    switch (ch)
282                    {
283                    case KEY_NULL: // broken pipe
284    #ifdef _DEBUG
285                            log_error("KEY_NULL\n");
286    #endif
287                            return EXIT_SECTION;
288                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
289                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
290                          {                          {
291                                  return EXIT_SECTION; // exit section                                  log_error("User input timeout\n");
292                                    return EXIT_SECTION;
293                          }                          }
294                          continue;                          continue;
295                    case KEY_ESC:
296                    case KEY_LEFT:
297                            return EXIT_SECTION;
298                  case 'n':                  case 'n':
                         BBS_last_access_tm = time(NULL);  
299                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
300                  case CR:                  case CR:
                         igetch_reset();  
301                  case 'r':                  case 'r':
302                  case KEY_RIGHT:                  case KEY_RIGHT:
303                          if (item_count > 0)                          if (item_count > 0)
304                          {                          {
                                 BBS_last_access_tm = time(NULL);  
305                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
306                          }                          }
307                          break;                          break;
# Line 281  static enum select_cmd_t section_list_se Line 325  static enum select_cmd_t section_list_se
325                                  return QUERY_ARTICLE;                                  return QUERY_ARTICLE;
326                          }                          }
327                          break;                          break;
328                    case Ctrl('A'):
329                            if (item_count > 0)
330                            {
331                                    return QUERY_USER;
332                            }
333                            break;
334                    case 'F':
335                            if (item_count > 0)
336                            {
337                                    return SET_FAVOR_ARTICLE;
338                            }
339                            break;
340                    case '-':
341                            if (item_count > 0)
342                            {
343                                    return UNSET_FAVOR_ARTICLE;
344                            }
345                            break;
346                  case KEY_HOME:                  case KEY_HOME:
347                          *p_page_id = 0;                          *p_page_id = 0;
348                  case 'P':                  case 'P':
# Line 314  static enum select_cmd_t section_list_se Line 376  static enum select_cmd_t section_list_se
376                          break;                          break;
377                  case '$':                  case '$':
378                  case KEY_END:                  case KEY_END:
379                            if (*p_page_id + 1 == total_page && *p_selected_index + 1 == item_count) // Press END at end of list
380                            {
381                                    return LAST_SECTION_ARTICLE;
382                            }
383                          if (total_page > 0)                          if (total_page > 0)
384                          {                          {
385                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
# Line 362  static enum select_cmd_t section_list_se Line 428  static enum select_cmd_t section_list_se
428                                  return SCAN_NEW_ARTICLE;                                  return SCAN_NEW_ARTICLE;
429                          }                          }
430                          break;                          break;
431                    case 'A':
432                            if (item_count > 0)
433                            {
434                                    return SCAN_ARTICLE_BACKWARD_BY_USER;
435                            }
436                            break;
437                    case 'a':
438                            if (item_count > 0)
439                            {
440                                    return SCAN_ARTICLE_FORWARD_BY_USER;
441                            }
442                            break;
443                    case '?':
444                            if (item_count > 0)
445                            {
446                                    return SCAN_ARTICLE_BACKWARD_BY_TITLE;
447                            }
448                            break;
449                    case '/':
450                            if (item_count > 0)
451                            {
452                                    return SCAN_ARTICLE_FORWARD_BY_TITLE;
453                            }
454                            break;
455                    case 'u':
456                            return SEARCH_USER;
457                  case 'h':                  case 'h':
458                          return SHOW_HELP;                          return SHOW_HELP;
459                  case 'x':                  case 'x':
460                          return VIEW_EX_DIR;                          return VIEW_EX_DIR;
461                  case Ctrl('H'):                  case 'H':
462                          return SHOW_TOP10;                          return SHOW_TOP10;
463                  default:                  default:
464                            break;
465                  }                  }
466    
467                  if (old_page_id != *p_page_id)                  if (old_page_id != *p_page_id)
# Line 393  static enum select_cmd_t section_list_se Line 486  static enum select_cmd_t section_list_se
486                          old_selected_index = *p_selected_index;                          old_selected_index = *p_selected_index;
487                  }                  }
488    
                 BBS_last_access_tm = time(NULL);  
489                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)                  if (BBS_last_access_tm - last_refresh_tm >= BBS_section_list_load_interval)
490                  {                  {
491                          return CHANGE_PAGE; // force section list refresh                          return CHANGE_PAGE; // force section list refresh
# Line 500  int section_list_display(const char *sna Line 592  int section_list_display(const char *sna
592          char stitle[BBS_section_title_max_len + 1];          char stitle[BBS_section_title_max_len + 1];
593          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
594          char page_info_str[LINE_BUFFER_LEN];          char page_info_str[LINE_BUFFER_LEN];
595          ARTICLE *p_articles[BBS_article_limit_per_page];          const ARTICLE *p_articles[BBS_article_limit_per_page];
596          int article_count;          int article_count;
597          int page_count;          int page_count;
598          int ontop_start_offset;          int ontop_start_offset;
# Line 513  int section_list_display(const char *sna Line 605  int section_list_display(const char *sna
605          ARTICLE article_new;          ARTICLE article_new;
606          int page_id_cur;          int page_id_cur;
607          const ARTICLE *p_article_locate;          const ARTICLE *p_article_locate;
608            USER_INFO user_info;
609            char user_intro[BBS_user_intro_max_len + 1];
610            char username[BBS_username_max_len + 1];
611            char username_list[1][BBS_username_max_len + 1];
612            int32_t uid;
613            int i;
614            int ok;
615            char title[BBS_article_title_max_len + 1] = "\0";
616    
617          p_section = section_list_find_by_name(sname, &section_index);          p_section = section_list_find_by_name(sname);
618          if (p_section == NULL)          if (p_section == NULL)
619          {          {
620                  log_error("Section %s not found\n", sname);                  log_error("Section %s not found\n", sname);
621                  return -1;                  return -1;
622          }          }
623    
624          if ((ret = section_list_rd_lock(p_section)) < 0)          if (!checkpriv(&BBS_priv, p_section->sid, S_LIST))
625          {          {
626                  log_error("section_list_rd_lock(sid = 0) error\n");                  log_error("Forbid access to unauthorized section, sid=%d, uid=%d\n",
627                  return -2;                                    p_section->sid, BBS_priv.uid);
628                    return -1;
629          }          }
630    
631          strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);          section_index = get_section_index(p_section);
         stitle[sizeof(stitle) - 1] = '\0';  
         strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);  
         master_list[sizeof(master_list) - 1] = '\0';  
632    
633          if ((ret = section_list_rd_unlock(p_section)) < 0)          if (get_section_info(p_section, NULL, stitle, master_list) < 0)
634          {          {
635                  log_error("section_list_rd_unlock(sid = 0) error\n");                  log_error("get_section_info(sid=%d) error\n", p_section->sid);
636                  return -2;                  return -4;
637          }          }
638    
639          if (aid == 0)          if (aid == 0)
# Line 545  int section_list_display(const char *sna Line 643  int section_list_display(const char *sna
643          else          else
644          {          {
645                  aid_location = aid;                  aid_location = aid;
646          }                }
647    
648          // Locate at article with aid_locate          // Locate at article with aid_locate
649          if (aid_location > 0)          if (aid_location > 0)
# Line 617  int section_list_display(const char *sna Line 715  int section_list_display(const char *sna
715                  }                  }
716    
717                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
718    
719                  switch (ret)                  switch (ret)
720                  {                  {
721                  case EXIT_SECTION:                  case EXIT_SECTION:
722                            // Update current aid location
723                            if (selected_index < article_count && p_articles[selected_index] != NULL)
724                            {
725                                    if (selected_index >= ontop_start_offset)
726                                    {
727                                            ret = last_article_in_section(p_section, &p_article_locate);
728                                            if (ret < 0)
729                                            {
730                                                    log_error("last_article_in_section(sid=%d) error\n", p_section->sid);
731                                                    return -3;
732                                            }
733                                            else if (ret == 0)
734                                            {
735                                                    section_aid_locations[section_index] = 0;
736                                            }
737                                            else // ret > 0
738                                            {
739                                                    section_aid_locations[section_index] = p_article_locate->aid;
740                                            }
741                                    }
742                                    else
743                                    {
744                                            section_aid_locations[section_index] = (p_articles[selected_index]->visible ? p_articles[selected_index]->aid : 0);
745                                    }
746                            }
747                          return 0;                          return 0;
748                  case CHANGE_PAGE:                  case CHANGE_PAGE:
749                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
# Line 764  int section_list_display(const char *sna Line 888  int section_list_display(const char *sna
888                                          {                                          {
889                                                  log_error("article_reply(aid=%d) error\n", p_articles[selected_index]->aid);                                                  log_error("article_reply(aid=%d) error\n", p_articles[selected_index]->aid);
890                                          }                                          }
891                                            else if (ret > 0) // Article replied
892                                            {
893                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
894                                                    if (ret < 0)
895                                                    {
896                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
897                                                            return -3;
898                                                    }
899                                            }
900                                          loop = 1;                                          loop = 1;
901                                          break;                                          break;
902                                  case '=':  // First topic article                                  case '=':  // First topic article
# Line 798  int section_list_display(const char *sna Line 931  int section_list_display(const char *sna
931                          // Update current topic                          // Update current topic
932                          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);
933    
                         // Update current aid location  
                         section_aid_locations[section_index] = p_articles[selected_index]->aid;  
   
934                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
935                          {                          {
936                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 830  int section_list_display(const char *sna Line 960  int section_list_display(const char *sna
960                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
961                                  if (ret < 0)                                  if (ret < 0)
962                                  {                                  {
963                                          log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                          log_error("query_section_articles(sid=%d, page_id=%d) error: %d\n", p_section->sid, page_id, ret);
964                                          return -3;                                          return -3;
965                                  }                                  }
966                          }                          }
# Line 856  int section_list_display(const char *sna Line 986  int section_list_display(const char *sna
986                          {                          {
987                                  log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);                                  log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
988                          }                          }
989                            else if (ret > 0) // Article modified
990                            {
991                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
992                                    if (ret < 0)
993                                    {
994                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
995                                            return -3;
996                                    }
997                            }
998                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
999                          {                          {
1000                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
# Line 874  int section_list_display(const char *sna Line 1013  int section_list_display(const char *sna
1013                          }                          }
1014                          else if (ret > 0) // Article deleted                          else if (ret > 0) // Article deleted
1015                          {                          {
1016                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);                                  do
                                 if (ret < 0)  
1017                                  {                                  {
1018                                          log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1019                                          return -3;                                          if (ret >= 0 && selected_index > article_count - 1)
1020                                  }                                          {
1021                                                    selected_index = article_count - 1;
1022                                                    break;
1023                                            }
1024                                            else if (ret < 0 && page_id > 0)
1025                                            {
1026                                                    page_id--;
1027                                                    selected_index = BBS_article_limit_per_page - 1;
1028                                            }
1029                                            else if (ret < 0 && page_id <= 0)
1030                                            {
1031                                                    selected_index = 0;
1032                                                    break;
1033                                            }
1034                                    } while (ret < 0);
1035                          }                          }
1036                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1037                          {                          {
# Line 898  int section_list_display(const char *sna Line 1050  int section_list_display(const char *sna
1050                                  return -2;                                  return -2;
1051                          }                          }
1052                          break;                          break;
1053                    case QUERY_USER:
1054                            if ((ret = query_user_info_by_uid(p_articles[selected_index]->uid, &user_info, user_intro, sizeof(user_intro))) < 0)
1055                            {
1056                                    log_error("query_user_info_by_uid(uid=%d) error\n", p_articles[selected_index]->uid);
1057                                    return -2;
1058                            }
1059                            else if (ret == 0)
1060                            {
1061                                    clearscr();
1062                                    prints("该用户已升天");
1063                                    press_any_key();
1064                            }
1065                            else if (user_info_display(&user_info) < 0) // && ret > 0
1066                            {
1067                                    log_error("user_info_display(uid=%d) error\n", p_articles[selected_index]->uid);
1068                            }
1069    
1070                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1071                            {
1072                                    log_error("section_list_draw_screen() error\n");
1073                                    return -2;
1074                            }
1075                            break;
1076                    case SET_FAVOR_ARTICLE:
1077                            ret = article_favor_set(p_articles[selected_index]->tid == 0
1078                                                                                    ? p_articles[selected_index]->aid
1079                                                                                    : p_articles[selected_index]->tid,
1080                                                                            &BBS_article_favor, 1);
1081                            if (ret < 0)
1082                            {
1083                                    log_error("article_favor_set(aid=%d, 1) error\n",
1084                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
1085                            }
1086                            break;
1087                    case UNSET_FAVOR_ARTICLE:
1088                            ret = article_favor_set(p_articles[selected_index]->tid == 0
1089                                                                                    ? p_articles[selected_index]->aid
1090                                                                                    : p_articles[selected_index]->tid,
1091                                                                            &BBS_article_favor, 0);
1092                            if (ret < 0)
1093                            {
1094                                    log_error("article_favor_set(aid=%d, 0) error\n",
1095                                                      p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
1096                            }
1097                            break;
1098                  case FIRST_TOPIC_ARTICLE:                  case FIRST_TOPIC_ARTICLE:
1099                  case LAST_TOPIC_ARTICLE:                  case LAST_TOPIC_ARTICLE:
1100                          page_id_cur = page_id;                          page_id_cur = page_id;
# Line 920  int section_list_display(const char *sna Line 1117  int section_list_display(const char *sna
1117                                  }                                  }
1118                          }                          }
1119                          break;                          break;
1120                    case LAST_SECTION_ARTICLE:
1121                            page_id_cur = page_id;
1122                            ret = last_article_in_section(p_section, &p_article_locate);
1123                            if (ret < 0)
1124                            {
1125                                    log_error("last_article_in_section(sid=%d) error\n", p_section->sid);
1126                                    return -3;
1127                            }
1128                            else if (ret == 0)
1129                            {
1130                                    break;
1131                            }
1132                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1133                                                                                            &page_id, &selected_index, &article_count);
1134                            if (ret < 0)
1135                            {
1136                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1137                                                      p_section->sid, p_article_locate->aid);
1138                                    return -3;
1139                            }
1140                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1141                            {
1142                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1143                                    if (ret < 0)
1144                                    {
1145                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1146                                            return -3;
1147                                    }
1148                            }
1149                            break;
1150                  case SCAN_NEW_ARTICLE:                  case SCAN_NEW_ARTICLE:
1151                          ret = scan_unread_article_in_section(p_section, p_articles[selected_index], &p_article_locate);                          ret = scan_unread_article_in_section(p_section, p_articles[selected_index], &p_article_locate);
1152                          if (ret < 0)                          if (ret < 0)
# Line 951  int section_list_display(const char *sna Line 1178  int section_list_display(const char *sna
1178                                  }                                  }
1179                          }                          }
1180                          break;                          break;
1181                    case SCAN_ARTICLE_BACKWARD_BY_USER:
1182                    case SCAN_ARTICLE_FORWARD_BY_USER:
1183                            direction = (ret == SCAN_ARTICLE_FORWARD_BY_USER ? 1 : -1);
1184                            strncpy(username, p_articles[selected_index]->username, sizeof(username) - 1);
1185                            username[sizeof(username) - 1] = '\0';
1186                            uid = 0;
1187    
1188                            moveto(SCREEN_ROWS, 1);
1189                            clrtoeol();
1190                            get_data(SCREEN_ROWS, 1,
1191                                             (direction == 1 ? "↓搜寻作者: " : "↑搜寻作者: "),
1192                                             username, sizeof(username), BBS_username_max_len);
1193    
1194                            if (username[0] == '\0')
1195                            {
1196                                    break;
1197                            }
1198    
1199                            // Verify format
1200                            for (i = 0, ok = 1; ok && username[i] != '\0'; i++)
1201                            {
1202                                    if (!(isalpha((int)username[i]) || (i > 0 && (isdigit((int)username[i]) || username[i] == '_'))))
1203                                    {
1204                                            ok = 0;
1205                                    }
1206                            }
1207                            if (ok && i > BBS_username_max_len)
1208                            {
1209                                    ok = 0;
1210                            }
1211                            if (!ok)
1212                            {
1213                                    break;
1214                            }
1215    
1216                            ret = query_user_info_by_username(username, 1, &uid, username_list);
1217                            if (ret < 0)
1218                            {
1219                                    log_error("query_user_info_by_username(%s) error\n", username);
1220                                    break;
1221                            }
1222    
1223                            if (uid > 0)
1224                            {
1225                                    ret = scan_article_in_section_by_uid(p_section, p_articles[selected_index],
1226                                                                                                             direction, uid, &p_article_locate);
1227                                    if (ret < 0)
1228                                    {
1229                                            log_error("scan_article_in_section_by_uid(sid=%d, aid=%d, direction=%d, uid=%d) error\n",
1230                                                              p_section->sid, p_articles[selected_index]->aid, direction, uid);
1231                                            return -3;
1232                                    }
1233                                    else if (ret == 0) // not found
1234                                    {
1235                                            break;
1236                                    }
1237                            }
1238                            else // uid == 0
1239                            {
1240                                    ret = scan_article_in_section_by_username(p_section, p_articles[selected_index],
1241                                                                                                                      direction, username, &p_article_locate);
1242                                    if (ret < 0)
1243                                    {
1244                                            log_error("scan_article_in_section_by_username(sid=%d, aid=%d, direction=%d, username=%s) error\n",
1245                                                              p_section->sid, p_articles[selected_index]->aid, direction, username);
1246                                            return -3;
1247                                    }
1248                                    else if (ret == 0) // not found
1249                                    {
1250                                            break;
1251                                    }
1252                            }
1253    
1254                            page_id_cur = page_id;
1255                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1256                                                                                            &page_id, &selected_index, &article_count);
1257                            if (ret < 0)
1258                            {
1259                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1260                                                      p_section->sid, p_article_locate->aid);
1261                                    return -3;
1262                            }
1263                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1264                            {
1265                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1266                                    if (ret < 0)
1267                                    {
1268                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1269                                            return -3;
1270                                    }
1271                            }
1272                            break;
1273                    case SCAN_ARTICLE_BACKWARD_BY_TITLE:
1274                    case SCAN_ARTICLE_FORWARD_BY_TITLE:
1275                            direction = (ret == SCAN_ARTICLE_FORWARD_BY_TITLE ? 1 : -1);
1276    
1277                            moveto(SCREEN_ROWS, 1);
1278                            clrtoeol();
1279                            get_data(SCREEN_ROWS, 1,
1280                                             (direction == 1 ? "↓搜寻标题: " : "↑搜寻标题: "),
1281                                             title, sizeof(title), TITLE_SEARCH_MAX_LEN);
1282    
1283                            if (title[0] == '\0')
1284                            {
1285                                    break;
1286                            }
1287    
1288                            ret = scan_article_in_section_by_title(p_section, p_articles[selected_index],
1289                                                                                                       direction, title, &p_article_locate);
1290                            if (ret < 0)
1291                            {
1292                                    log_error("scan_article_in_section_by_title(sid=%d, aid=%d, direction=%d, title=%s) error\n",
1293                                                      p_section->sid, p_articles[selected_index]->aid, direction, title);
1294                                    return -3;
1295                            }
1296                            else if (ret == 0) // not found
1297                            {
1298                                    break;
1299                            }
1300    
1301                            page_id_cur = page_id;
1302                            ret = locate_article_in_section(p_section, p_article_locate, 0, 0,
1303                                                                                            &page_id, &selected_index, &article_count);
1304                            if (ret < 0)
1305                            {
1306                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=0, step=0) error\n",
1307                                                      p_section->sid, p_article_locate->aid);
1308                                    return -3;
1309                            }
1310                            else if (ret > 0 && page_id != page_id_cur) // found and page changed
1311                            {
1312                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count, &ontop_start_offset);
1313                                    if (ret < 0)
1314                                    {
1315                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
1316                                            return -3;
1317                                    }
1318                            }
1319                            break;
1320                    case SEARCH_USER:
1321                            user_list_search();
1322                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
1323                            {
1324                                    log_error("section_list_draw_screen() error\n");
1325                                    return -2;
1326                            }
1327                            break;
1328                  case SHOW_HELP:                  case SHOW_HELP:
1329                          // Display help information                          // Display help information
1330                          display_file(DATA_READ_HELP, 1);                          display_file(DATA_READ_HELP, 1);
# Line 991  int section_list_ex_dir_display(SECTION_ Line 1365  int section_list_ex_dir_display(SECTION_
1365  {  {
1366          MENU_SET ex_menu_set;          MENU_SET ex_menu_set;
1367          int ch = 0;          int ch = 0;
1368            int loop;
1369    
1370          if (p_section == NULL)          if (p_section == NULL)
1371          {          {
# Line 1023  int section_list_ex_dir_display(SECTION_ Line 1398  int section_list_ex_dir_display(SECTION_
1398    
1399          if (display_menu(&ex_menu_set) == 0)          if (display_menu(&ex_menu_set) == 0)
1400          {          {
1401                  while (!SYS_server_exit)                  for (loop = 1; !SYS_server_exit && loop;)
1402                  {                  {
1403                          iflush();                          iflush();
1404                          ch = igetch(100);                          ch = igetch(100);
1405    
1406                            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
1407                            {
1408                                    BBS_last_access_tm = time(NULL);
1409    
1410                                    // Refresh current action
1411                                    if (user_online_update(NULL) < 0)
1412                                    {
1413                                            log_error("user_online_update(NULL) error\n");
1414                                    }
1415                            }
1416    
1417                          switch (ch)                          switch (ch)
1418                          {                          {
1419                          case KEY_NULL: // broken pipe                          case KEY_NULL: // broken pipe
1420                                  return 0;  #ifdef _DEBUG
1421                                    log_error("KEY_NULL\n");
1422    #endif
1423                                    loop = 0;
1424                                    break;
1425                          case KEY_TIMEOUT:                          case KEY_TIMEOUT:
1426                                  if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                                  if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
1427                                  {                                  {
1428                                          return 0;                                          log_error("User input timeout\n");
1429                                            loop = 0;
1430                                            break;
1431                                  }                                  }
1432                                  continue;                                  continue;
1433                          case CR:                          case CR:
                                 igetch_reset();  
1434                          default:                          default:
1435                                  switch (menu_control(&ex_menu_set, ch))                                  switch (menu_control(&ex_menu_set, ch))
1436                                  {                                  {
1437                                  case EXITMENU:                                  case EXITMENU:
1438                                          ch = EXITMENU;                                          loop = 0;
1439                                          break;                                          break;
1440                                  case REDRAW:                                  case REDRAW:
1441                                          clearscr();                                          clearscr();
# Line 1056  int section_list_ex_dir_display(SECTION_ Line 1448  int section_list_ex_dir_display(SECTION_
1448                                          break;                                          break;
1449                                  }                                  }
1450                          }                          }
1451                    }
1452            }
1453    
1454                          BBS_last_access_tm = time(NULL);          detach_menu_shm(&ex_menu_set);
1455    
1456            return 0;
1457    }
1458    
1459    int section_aid_locations_save(int uid)
1460    {
1461            char filename[FILE_PATH_LEN];
1462            FILE *fp;
1463            int i;
1464            int ret = 0;
1465    
1466            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1467    
1468            if ((fp = fopen(filename, "wb")) == NULL)
1469            {
1470                    log_error("fopen(%s, wb) error: %d\n", filename, errno);
1471                    return -1;
1472            }
1473    
1474            for (i = 0; i < p_section_list_pool->section_count; i++)
1475            {
1476                    if (fwrite(&(p_section_list_pool->sections[i].sid), sizeof(p_section_list_pool->sections[i].sid), 1, fp) != 1)
1477                    {
1478                            log_error("fwrite(%s, sid) error\n", filename);
1479                            ret = -2;
1480                            break;
1481                    }
1482    
1483                          if (ch == EXITMENU)                  if (fwrite(&(section_aid_locations[i]), sizeof(section_aid_locations[i]), 1, fp) != 1)
1484                    {
1485                            log_error("fwrite(%s, aid) error\n", filename);
1486                            ret = -2;
1487                            break;
1488                    }
1489            }
1490    
1491            if (fclose(fp) < 0)
1492            {
1493                    log_error("fclose(%s) error: %d\n", filename, errno);
1494                    ret = -1;
1495            }
1496    
1497            return ret;
1498    }
1499    
1500    int section_aid_locations_load(int uid)
1501    {
1502            char filename[FILE_PATH_LEN];
1503            FILE *fp;
1504            int i;
1505            int32_t sid;
1506            int32_t aid;
1507            SECTION_LIST *p_section;
1508            int ret = 0;
1509    
1510            snprintf(filename, sizeof(filename), "%s/%d", VAR_SECTION_AID_LOC_DIR, uid);
1511    
1512            if ((fp = fopen(filename, "rb")) == NULL)
1513            {
1514                    if (errno == ENOENT) // file not exist
1515                    {
1516                            return 0;
1517                    }
1518                    log_error("fopen(%s, rb) error: %d\n", filename, errno);
1519                    return -1;
1520            }
1521    
1522            while (!feof(fp))
1523            {
1524                    if (fread(&sid, sizeof(sid), 1, fp) != 1)
1525                    {
1526                            if (ferror(fp) == 0)
1527                            {
1528                                    break;
1529                            }
1530                            log_error("fread(%s, sid) error: %d\n", filename, ferror(fp));
1531                            ret = -2;
1532                            break;
1533                    }
1534    
1535                    if (fread(&aid, sizeof(aid), 1, fp) != 1)
1536                    {
1537                            if (ferror(fp) == 0)
1538                          {                          {
1539                                  break;                                  break;
1540                          }                          }
1541                            log_error("fread(%s, aid) error: %d\n", filename, ferror(fp));
1542                            ret = -2;
1543                            break;
1544                    }
1545    
1546                    p_section = section_list_find_by_sid(sid);
1547                    if (p_section == NULL)
1548                    {
1549                            continue; // skip section no longer exist
1550                    }
1551    
1552                    i = get_section_index(p_section);
1553                    if (i < 0)
1554                    {
1555                            log_error("get_section_index(sid=%d) error\n", sid);
1556                            ret = -3;
1557                            break;
1558                  }                  }
1559                    section_aid_locations[i] = aid;
1560          }          }
1561    
1562          detach_menu_shm(&ex_menu_set);          if (fclose(fp) < 0)
1563            {
1564                    log_error("fclose(%s) error: %d\n", filename, errno);
1565                    ret = -1;
1566            }
1567    
1568          return 0;          return ret;
1569  }  }


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

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