/[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.43 by sysadm, Tue May 13 02:59:33 2025 UTC Revision 1.82 by sysadm, Thu Oct 16 11:26:16 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "bbs_main.h"  #include "article_favor.h"
18    #include "article_view_log.h"
19  #include "bbs.h"  #include "bbs.h"
20  #include "login.h"  #include "bbs_cmd.h"
21  #include "user_priv.h"  #include "bbs_main.h"
22  #include "common.h"  #include "common.h"
23  #include "database.h"  #include "database.h"
24  #include "log.h"  #include "editor.h"
25  #include "io.h"  #include "io.h"
26  #include "screen.h"  #include "log.h"
27    #include "login.h"
28  #include "menu.h"  #include "menu.h"
29  #include "bbs_cmd.h"  #include "screen.h"
30  #include <unistd.h>  #include "section_list.h"
31  #include <time.h>  #include "trie_dict.h"
32    #include "user_priv.h"
33    #include <errno.h>
34    #include <signal.h>
35    #include <stdlib.h>
36  #include <string.h>  #include <string.h>
37    #include <time.h>
38    #include <unistd.h>
39    
40  int bbs_info()  int bbs_info()
41  {  {
42          prints("ӭ \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\033[m\r\n",
43                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_INFO);
44    
45          return iflush();          return iflush();
46  }  }
47    
48  int bbs_welcome(MYSQL *db)  int bbs_welcome(void)
49  {  {
50          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
51    
52          u_int32_t u_online = 0;          u_int32_t u_online = 0;
53          u_int32_t u_anonymous = 0;          u_int32_t u_anonymous = 0;
54          u_int32_t u_total = 0;          u_int32_t u_total = 0;
         u_int32_t max_u_online = 0;  
55          u_int32_t u_login_count = 0;          u_int32_t u_login_count = 0;
56    
57            MYSQL *db;
58          MYSQL_RES *rs;          MYSQL_RES *rs;
59          MYSQL_ROW row;          MYSQL_ROW row;
60    
61            db = db_open();
62            if (db == NULL)
63            {
64                    return -1;
65            }
66    
67          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
68                           "SELECT COUNT(*) AS cc FROM "                           "SELECT COUNT(*) AS cc FROM "
69                           "(SELECT DISTINCT SID FROM user_online "                           "(SELECT DISTINCT SID FROM user_online "
# Line 57  int bbs_welcome(MYSQL *db) Line 71  int bbs_welcome(MYSQL *db)
71                           BBS_user_off_line);                           BBS_user_off_line);
72          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
73          {          {
74                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
75                    mysql_close(db);
76                  return -2;                  return -2;
77          }          }
78          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
79          {          {
80                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
81                    mysql_close(db);
82                  return -2;                  return -2;
83          }          }
84          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 78  int bbs_welcome(MYSQL *db) Line 94  int bbs_welcome(MYSQL *db)
94                           BBS_user_off_line);                           BBS_user_off_line);
95          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
96          {          {
97                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
98                    mysql_close(db);
99                  return -2;                  return -2;
100          }          }
101          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
102          {          {
103                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
104                    mysql_close(db);
105                  return -2;                  return -2;
106          }          }
107          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 95  int bbs_welcome(MYSQL *db) Line 113  int bbs_welcome(MYSQL *db)
113          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");
114          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
115          {          {
116                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
117                    mysql_close(db);
118                  return -2;                  return -2;
119          }          }
120          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
121          {          {
122                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
123                    mysql_close(db);
124                  return -2;                  return -2;
125          }          }
126          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 112  int bbs_welcome(MYSQL *db) Line 132  int bbs_welcome(MYSQL *db)
132          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");
133          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
134          {          {
135                  log_error("Query user_login_log failed\n");                  log_error("Query user_login_log error: %s\n", mysql_error(db));
136                    mysql_close(db);
137                  return -2;                  return -2;
138          }          }
139          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
140          {          {
141                  log_error("Get user_login_log data failed\n");                  log_error("Get user_login_log data failed\n");
142                    mysql_close(db);
143                  return -2;                  return -2;
144          }          }
145          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 126  int bbs_welcome(MYSQL *db) Line 148  int bbs_welcome(MYSQL *db)
148          }          }
149          mysql_free_result(rs);          mysql_free_result(rs);
150    
151          // 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);  
         }  
152    
153          // Count current user before login          // Count current user before login
154          u_online++;          u_online++;
155          u_anonymous++;          u_anonymous++;
156    
157          // Display logo          // Display logo
158          display_file(DATA_WELCOME);          display_file(DATA_WELCOME, 2);
159    
160          // Display welcome message          // Display welcome message
161          prints("\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
162                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
163                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
164                     "עû[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
165                     " [\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",  
166                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
167                     BBS_max_user, BBS_start_dt, max_u_online, u_login_count);                     BBS_max_user, BBS_start_dt, u_login_count);
168    
169            iflush();
170    
171          return 0;          return 0;
172  }  }
173    
174  int bbs_logout(MYSQL *db)  int bbs_logout(void)
175  {  {
176          if (user_online_del(db) < 0)          MYSQL *db;
177    
178            db = db_open();
179            if (db == NULL)
180          {          {
181                  return -1;                  return -1;
182          }          }
183    
184          if (display_file_ex(DATA_GOODBYE, 1, 0) < 0)          if (user_online_exp(db) < 0)
185          {          {
186                  return -2;                  return -2;
187          }          }
188    
189          log_std("User logout\n");          if (user_online_del(db) < 0)
190            {
191                    return -3;
192            }
193    
194            mysql_close(db);
195    
196            display_file(DATA_GOODBYE, 1);
197    
198            log_common("User [%s] logout\n", BBS_username);
199    
200          return 0;          return 0;
201  }  }
# Line 187  int bbs_center() Line 205  int bbs_center()
205          int ch;          int ch;
206          time_t t_last_action;          time_t t_last_action;
207    
208          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
209    
210          clearscr();          clearscr();
211    
212          show_top("");          show_top("", BBS_name, "");
213          show_active_board();          show_active_board();
214          show_bottom("");          show_bottom("");
215          display_menu(get_menu(&bbs_menu, "TOPMENU"));          display_menu(&bbs_menu);
216            iflush();
217    
218          while (!SYS_server_exit)          while (!SYS_server_exit)
219          {          {
220                  ch = igetch(0);                  ch = igetch(100);
221    
222            if (ch != KEY_NULL && ch != KEY_TIMEOUT)
223            {
224                BBS_last_access_tm = time(NULL);
225            }
226    
227                  if (time(0) - t_last_action >= 10)                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
228                  {                  {
229                          t_last_action = time(0);                          t_last_action = time(NULL);
230    
231                          show_active_board();                          show_active_board();
232                          show_bottom("");                          show_bottom("");
233                            display_menu_cursor(&bbs_menu, 1);
234                            iflush();
235                    }
236    
237                    if (user_online_update("MENU") < 0)
238                    {
239                            log_error("user_online_update(MENU) error\n");
240                  }                  }
241    
242                  switch (ch)                  switch (ch)
243                  {                  {
244                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
245                            log_error("KEY_NULL\n");
246                          return 0;                          return 0;
247                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
248                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
249                          {                          {
250                                    log_error("User input timeout\n");
251                                  return 0;                                  return 0;
252                          }                          }
253                          continue;                          continue;
254                    case CR:
255                            igetch_reset();
256                  default:                  default:
257                          switch (menu_control(&bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
258                          {                          {
259                          case EXITBBS:                          case EXITBBS:
260                            case EXITMENU:
261                                  return 0;                                  return 0;
262                          case REDRAW:                          case REDRAW:
263                                    t_last_action = time(NULL);
264                                  clearscr();                                  clearscr();
265                                  show_top("");                                  show_top("", BBS_name, "");
266                                  show_active_board();                                  show_active_board();
267                                  show_bottom("");                                  show_bottom("");
268                                  display_current_menu(&bbs_menu);                                  display_menu(&bbs_menu);
269                                  break;                                  break;
270                          case NOREDRAW:                          case NOREDRAW:
271                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
272                          default:                          default:
273                                  break;                                  break;
274                          }                          }
275                            iflush();
276                  }                  }
                 BBS_last_access_tm = time(0);  
277          }          }
278    
279          return 0;          return 0;
# Line 243  int bbs_center() Line 281  int bbs_center()
281    
282  int bbs_main()  int bbs_main()
283  {  {
284          MYSQL *db;          struct sigaction act = {0};
285    
286            // Set signal handler
287            act.sa_handler = SIG_IGN;
288            if (sigaction(SIGHUP, &act, NULL) == -1)
289            {
290                    log_error("set signal action of SIGHUP error: %d\n", errno);
291                    goto cleanup;
292            }
293            act.sa_handler = SIG_DFL;
294            if (sigaction(SIGCHLD, &act, NULL) == -1)
295            {
296                    log_error("set signal action of SIGCHLD error: %d\n", errno);
297                    goto cleanup;
298            }
299    
300            // Set data pools in shared memory readonly
301            if (set_trie_dict_shm_readonly() < 0)
302            {
303                    goto cleanup;
304            }
305            if (set_article_block_shm_readonly() < 0)
306            {
307                    goto cleanup;
308            }
309            if (set_section_list_shm_readonly() < 0)
310            {
311                    goto cleanup;
312            }
313    
314            // Load menu in shared memory
315            if (set_menu_shm_readonly(&bbs_menu) < 0)
316            {
317                    goto cleanup;
318            }
319    
320          set_input_echo(0);          set_input_echo(0);
321    
322          // System info          // System info
323          if (bbs_info() < 0)          if (bbs_info() < 0)
324          {          {
325                  return -1;                  goto cleanup;
326          }          }
327    
328          db = db_open();          // Welcome
329          if (db == NULL)          if (bbs_welcome() < 0)
330          {          {
331                  prints("޷ݿ\n");                  goto cleanup;
                 return -2;  
332          }          }
333    
334          // Welcome          // User login
335          if (bbs_welcome(db) < 0)          if (SSH_v2)
336          {          {
337                  mysql_close(db);                  prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
338                  return -3;                  iflush();
339                    igetch_t(MAX_DELAY_TIME);
340            }
341            else if (bbs_login() < 0)
342            {
343                    goto cleanup;
344          }          }
345    
346          // User login          // Load article view log
347          if (bbs_login(db) < 0)          if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
348          {          {
349                  mysql_close(db);                  log_error("article_view_log_load() error\n");
350                  return -4;                  goto cleanup;
351          }          }
         clearscr();  
352    
353          // BBS Top 10          // Load article favorite
354          if (display_file_ex("./var/bbs_top.txt", 1, 1) < 0)          if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
355            {
356                    log_error("article_favor_load() error\n");
357                    goto cleanup;
358            }
359    
360            // Init editor memory pool
361            if (editor_memory_pool_init() < 0)
362          {          {
363                  prints("޷رվʮ\n");                  log_error("editor_memory_pool_init() error\n");
364                    goto cleanup;
365          }          }
366    
367            clearscr();
368    
369            // BBS Top 10
370            display_file(VAR_BBS_TOP, 1);
371    
372          // Main          // Main
373          bbs_center();          bbs_center();
374    
375          // Logout          // Logout
376          bbs_logout(db);          bbs_logout();
377    
378          mysql_close(db);          // Save incremental article view log
379            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
380            {
381                    log_error("article_view_log_save_inc() error\n");
382            }
383    
384            // Save incremental article favorite
385            if (article_favor_save_inc(&BBS_article_favor) < 0)
386            {
387                    log_error("article_favor_save_inc() error\n");
388            }
389    
390    cleanup:
391            // Cleanup editor memory pool
392            editor_memory_pool_cleanup();
393    
394            // Unload article view log
395            article_view_log_unload(&BBS_article_view_log);
396    
397            // Unload article favor
398            article_favor_unload(&BBS_article_favor);
399    
400            // Detach menu in shared memory
401            detach_menu_shm(&bbs_menu);
402            detach_menu_shm(&top10_menu);
403    
404            // Detach data pools shm
405            detach_section_list_shm();
406            detach_article_block_shm();
407            detach_trie_dict_shm();
408    
409          return 0;          return 0;
410  }  }


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

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