/[LeafOK_CVS]/lbbs/src/bbs_main.c
ViewVC logotype

Diff of /lbbs/src/bbs_main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.47 by sysadm, Thu May 15 14:17:53 2025 UTC Revision 1.73 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "bbs_main.h"  #include "article_view_log.h"
18  #include "bbs.h"  #include "bbs.h"
19  #include "login.h"  #include "bbs_cmd.h"
20  #include "user_priv.h"  #include "bbs_main.h"
21  #include "common.h"  #include "common.h"
22  #include "database.h"  #include "database.h"
23  #include "log.h"  #include "editor.h"
24  #include "io.h"  #include "io.h"
25  #include "screen.h"  #include "log.h"
26    #include "login.h"
27  #include "menu.h"  #include "menu.h"
28  #include "bbs_cmd.h"  #include "screen.h"
29  #include <unistd.h>  #include "section_list.h"
30  #include <time.h>  #include "trie_dict.h"
31    #include "user_priv.h"
32    #include <stdlib.h>
33  #include <string.h>  #include <string.h>
34    #include <time.h>
35    #include <unistd.h>
36    
37  int bbs_info()  int bbs_info()
38  {  {
39          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",
40                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_INFO);
41    
42          return iflush();          return iflush();
43  }  }
44    
45  int bbs_welcome(MYSQL *db)  int bbs_welcome(void)
46  {  {
47          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
48    
49          u_int32_t u_online = 0;          u_int32_t u_online = 0;
50          u_int32_t u_anonymous = 0;          u_int32_t u_anonymous = 0;
51          u_int32_t u_total = 0;          u_int32_t u_total = 0;
         u_int32_t max_u_online = 0;  
52          u_int32_t u_login_count = 0;          u_int32_t u_login_count = 0;
53    
54            MYSQL *db;
55          MYSQL_RES *rs;          MYSQL_RES *rs;
56          MYSQL_ROW row;          MYSQL_ROW row;
57    
58            db = db_open();
59            if (db == NULL)
60            {
61                    return -1;
62            }
63    
64          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
65                           "SELECT COUNT(*) AS cc FROM "                           "SELECT COUNT(*) AS cc FROM "
66                           "(SELECT DISTINCT SID FROM user_online "                           "(SELECT DISTINCT SID FROM user_online "
# Line 57  int bbs_welcome(MYSQL *db) Line 68  int bbs_welcome(MYSQL *db)
68                           BBS_user_off_line);                           BBS_user_off_line);
69          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
70          {          {
71                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
72                    mysql_close(db);
73                  return -2;                  return -2;
74          }          }
75          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
76          {          {
77                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
78                    mysql_close(db);
79                  return -2;                  return -2;
80          }          }
81          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 78  int bbs_welcome(MYSQL *db) Line 91  int bbs_welcome(MYSQL *db)
91                           BBS_user_off_line);                           BBS_user_off_line);
92          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
93          {          {
94                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
95                    mysql_close(db);
96                  return -2;                  return -2;
97          }          }
98          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
99          {          {
100                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
101                    mysql_close(db);
102                  return -2;                  return -2;
103          }          }
104          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 95  int bbs_welcome(MYSQL *db) Line 110  int bbs_welcome(MYSQL *db)
110          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
111          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
112          {          {
113                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
114                    mysql_close(db);
115                  return -2;                  return -2;
116          }          }
117          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
118          {          {
119                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
120                    mysql_close(db);
121                  return -2;                  return -2;
122          }          }
123          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 112  int bbs_welcome(MYSQL *db) Line 129  int bbs_welcome(MYSQL *db)
129          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");
130          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
131          {          {
132                  log_error("Query user_login_log failed\n");                  log_error("Query user_login_log error: %s\n", mysql_error(db));
133                    mysql_close(db);
134                  return -2;                  return -2;
135          }          }
136          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
137          {          {
138                  log_error("Get user_login_log data failed\n");                  log_error("Get user_login_log data failed\n");
139                    mysql_close(db);
140                  return -2;                  return -2;
141          }          }
142          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 126  int bbs_welcome(MYSQL *db) Line 145  int bbs_welcome(MYSQL *db)
145          }          }
146          mysql_free_result(rs);          mysql_free_result(rs);
147    
148          // Log max user_online          mysql_close(db);
         FILE *fin, *fout;  
         if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL)  
         {  
                 fscanf(fin, "%d", &max_u_online);  
                 fclose(fin);  
         }  
         if (u_online > max_u_online)  
         {  
                 max_u_online = u_online;  
                 if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL)  
                 {  
                         log_error("Open max_user_online.dat failed\n");  
                         return -3;  
                 }  
                 fprintf(fout, "%d\n", max_u_online);  
                 fclose(fout);  
         }  
149    
150          // Count current user before login          // Count current user before login
151          u_online++;          u_online++;
152          u_anonymous++;          u_anonymous++;
153    
154          // Display logo          // Display logo
155          display_file_ex(DATA_WELCOME, 1, 0);          display_file(DATA_WELCOME, 2);
156    
157          // Display welcome message          // Display welcome message
158          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
159                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
160                     "匿名游客[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
161                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
162                     "从 [\033[36m%s\033[32m] 起,最高人数记录:"                     "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
                    "[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\r\n",  
163                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
164                     BBS_max_user, BBS_start_dt, max_u_online, u_login_count);                     BBS_max_user, BBS_start_dt, u_login_count);
165    
166            iflush();
167    
168          return 0;          return 0;
169  }  }
170    
171  int bbs_logout(MYSQL *db)  int bbs_logout(void)
172  {  {
173          if (user_online_del(db) < 0)          MYSQL *db;
174    
175            db = db_open();
176            if (db == NULL)
177          {          {
178                  return -1;                  return -1;
179          }          }
180    
181          if (display_file_ex(DATA_GOODBYE, 1, 0) < 0)          if (user_online_del(db) < 0)
182          {          {
183                  return -2;                  return -2;
184          }          }
185    
186          log_std("User logout\n");          mysql_close(db);
187    
188            display_file(DATA_GOODBYE, 1);
189    
190            log_common("User logout\n");
191    
192          return 0;          return 0;
193  }  }
# Line 187  int bbs_center() Line 197  int bbs_center()
197          int ch;          int ch;
198          time_t t_last_action;          time_t t_last_action;
199    
200          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
201    
202          clearscr();          clearscr();
203    
204          show_top("");          show_top("", BBS_name, "");
205          show_active_board();          show_active_board();
206          show_bottom("");          show_bottom("");
207          display_menu(p_bbs_menu);          display_menu(p_bbs_menu);
208            iflush();
209    
210          while (!SYS_server_exit)          while (!SYS_server_exit)
211          {          {
212                  ch = igetch(100);                  ch = igetch(100);
213    
214                  if (time(0) - t_last_action >= 10)                  if (p_bbs_menu->choose_step == 0 && time(NULL) - t_last_action >= 10)
215                  {                  {
216                          t_last_action = time(0);                          t_last_action = time(NULL);
217    
218                          show_active_board();                          show_active_board();
219                          show_bottom("");                          show_bottom("");
220                            display_menu_cursor(p_bbs_menu, 1);
221                            iflush();
222                    }
223    
224                    if (user_online_update("MENU") < 0)
225                    {
226                            log_error("user_online_update(MENU) error\n");
227                  }                  }
228    
229                  switch (ch)                  switch (ch)
# Line 212  int bbs_center() Line 231  int bbs_center()
231                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
232                          return 0;                          return 0;
233                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
234                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
235                          {                          {
236                                  return 0;                                  return 0;
237                          }                          }
# Line 225  int bbs_center() Line 244  int bbs_center()
244                          case EXITBBS:                          case EXITBBS:
245                                  return 0;                                  return 0;
246                          case REDRAW:                          case REDRAW:
247                                    t_last_action = time(NULL);
248                                  clearscr();                                  clearscr();
249                                  show_top("");                                  show_top("", BBS_name, "");
250                                  show_active_board();                                  show_active_board();
251                                  show_bottom("");                                  show_bottom("");
252                                  display_menu(p_bbs_menu);                                  display_menu(p_bbs_menu);
# Line 236  int bbs_center() Line 256  int bbs_center()
256                          default:                          default:
257                                  break;                                  break;
258                          }                          }
259                            iflush();
260                  }                  }
261                  BBS_last_access_tm = time(0);  
262                    BBS_last_access_tm = time(NULL);
263          }          }
264    
265          return 0;          return 0;
# Line 245  int bbs_center() Line 267  int bbs_center()
267    
268  int bbs_main()  int bbs_main()
269  {  {
270          MYSQL *db;          // Set data pools in shared memory readonly
271            if (set_trie_dict_shm_readonly() < 0)
272            {
273                    goto cleanup;
274            }
275            if (set_article_block_shm_readonly() < 0)
276            {
277                    goto cleanup;
278            }
279            if (set_section_list_shm_readonly() < 0)
280            {
281                    goto cleanup;
282            }
283    
284            // Load menu in shared memory
285            if (set_menu_shm_readonly(p_bbs_menu) < 0)
286            {
287                    goto cleanup;
288            }
289    
290          set_input_echo(0);          set_input_echo(0);
291    
292          // System info          // System info
293          if (bbs_info() < 0)          if (bbs_info() < 0)
294          {          {
295                  return -1;                  goto cleanup;
         }  
   
         db = db_open();  
         if (db == NULL)  
         {  
                 prints("无法连接数据库\n");  
                 return -2;  
296          }          }
297    
298          // Welcome          // Welcome
299          if (bbs_welcome(db) < 0)          if (bbs_welcome() < 0)
300          {          {
301                  mysql_close(db);                  goto cleanup;
                 return -3;  
302          }          }
303    
304          // User login          // User login
305          if (bbs_login(db) < 0)          if (SSH_v2)
306          {          {
307                  mysql_close(db);                  prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
308                  return -4;                  iflush();
309                    igetch_t(MAX_DELAY_TIME);
310            }
311            else if (bbs_login() < 0)
312            {
313                    goto cleanup;
314          }          }
         clearscr();  
315    
316          // BBS Top 10          // Load article view log
317          if (display_file_ex("./var/bbs_top.txt", 1, 1) < 0)          if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
318          {          {
319                  prints("无法加载本站十大\n");                  log_error("article_view_log_load() error\n");
320                    goto cleanup;
321          }          }
322    
323          // Load menu in shared memory          // Init editor memory pool
324          if (load_menu_shm(p_bbs_menu) < 0)          if (editor_memory_pool_init() < 0)
325          {          {
326                  return -5;                  log_error("editor_memory_pool_init() error\n");
327                    goto cleanup;
328          }          }
329    
330            clearscr();
331    
332            // BBS Top 10
333            display_file(VAR_BBS_TOP, 1);
334    
335          // Main          // Main
336          bbs_center();          bbs_center();
337    
338          // Unload menu in shared memory          // Logout
339          unload_menu_shm(p_bbs_menu);          bbs_logout();
340    
341            // Save incremental article view log
342            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
343            {
344                    log_error("article_view_log_save_inc() error\n");
345            }
346    
347    cleanup:
348            // Cleanup editor memory pool
349            editor_memory_pool_cleanup();
350    
351            // Unload article view log
352            article_view_log_unload(&BBS_article_view_log);
353    
354            // Detach menu in shared memory
355            detach_menu_shm(p_bbs_menu);
356          free(p_bbs_menu);          free(p_bbs_menu);
357          p_bbs_menu = NULL;          p_bbs_menu = NULL;
358    
359          // Logout          // Detach data pools shm
360          bbs_logout(db);          detach_section_list_shm();
361            detach_article_block_shm();
362          mysql_close(db);          detach_trie_dict_shm();
363    
364          return 0;          return 0;
365  }  }


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

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