/[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.65 by sysadm, Sat Jun 7 08:33:08 2025 UTC Revision 1.94 by sysadm, Sat Nov 1 10:35:42 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 "section_list.h"  #include "section_list.h"
31  #include "trie_dict.h"  #include "trie_dict.h"
32  #include <unistd.h>  #include "user_list.h"
33  #include <time.h>  #include "user_priv.h"
34  #include <string.h>  #include <errno.h>
35    #include <signal.h>
36  #include <stdlib.h>  #include <stdlib.h>
37    #include <string.h>
38    #include <time.h>
39    #include <unistd.h>
40    
41  int bbs_info()  int bbs_info()
42  {  {
43          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",
44                     BBS_name, BBS_server, APP_NAME_VER);                     BBS_name, BBS_server, APP_INFO);
45    
46          return iflush();          return iflush();
47  }  }
# Line 44  int bbs_welcome(void) Line 50  int bbs_welcome(void)
50  {  {
51          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
52    
53          u_int32_t u_online = 0;          int u_online = 0;
54          u_int32_t u_anonymous = 0;          int u_anonymous = 0;
55          u_int32_t u_total = 0;          int u_total = 0;
56          u_int32_t max_u_online = 0;          int u_login_count = 0;
         u_int32_t u_login_count = 0;  
57    
58          MYSQL *db;          MYSQL *db;
59          MYSQL_RES *rs;          MYSQL_RES *rs;
60          MYSQL_ROW row;          MYSQL_ROW row;
61    
62          db = db_open();          if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
         if (db == NULL)  
63          {          {
64                  return -1;                  log_error("get_user_online_list_count() error\n");
65                    u_online = 0;
66          }          }
67            u_online += u_anonymous;
68          snprintf(sql, sizeof(sql),          u_online++; // current user
69                           "SELECT COUNT(*) AS cc FROM "          if (BBS_priv.uid == 0)
                          "(SELECT DISTINCT SID FROM user_online "  
                          "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",  
                          BBS_user_off_line);  
         if (mysql_query(db, sql) != 0)  
70          {          {
71                  log_error("Query user_online error: %s\n", mysql_error(db));                  u_anonymous++;
                 mysql_close(db);  
                 return -2;  
72          }          }
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_online data failed\n");  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_online = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
73    
74          snprintf(sql, sizeof(sql),          if (get_user_list_count(&u_total) < 0)
                          "SELECT COUNT(*) AS cc FROM "  
                          "(SELECT DISTINCT SID FROM user_online "  
                          "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",  
                          BBS_user_off_line);  
         if (mysql_query(db, sql) != 0)  
75          {          {
76                  log_error("Query user_online error: %s\n", mysql_error(db));                  log_error("get_user_list_count() error\n");
77                  mysql_close(db);                  u_total = 0;
                 return -2;  
78          }          }
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_online data failed\n");  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_anonymous = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
79    
80          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");          db = db_open();
81          if (mysql_query(db, sql) != 0)          if (db == NULL)
         {  
                 log_error("Query user_list error: %s\n", mysql_error(db));  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_list data failed\n");  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
82          {          {
83                  u_total = (u_int32_t)atoi(row[0]);                  return -1;
84          }          }
         mysql_free_result(rs);  
85    
86          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");
87          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
# Line 140  int bbs_welcome(void) Line 98  int bbs_welcome(void)
98          }          }
99          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
100          {          {
101                  u_login_count = (u_int32_t)atoi(row[0]);                  u_login_count = atoi(row[0]);
102          }          }
103          mysql_free_result(rs);          mysql_free_result(rs);
104    
105          mysql_close(db);          mysql_close(db);
106    
         // Log max user_online  
         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);  
         }  
   
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
   
107          // Display logo          // Display logo
108          display_file(DATA_WELCOME, 1, 2);          display_file(DATA_WELCOME, 2);
109    
110          // Display welcome message          // Display welcome message
111          prints("\r\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
112                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
113                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
114                     "עû[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
115                     " [\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]\033[m\r\n",  
116                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
117                     BBS_max_user, BBS_start_dt, max_u_online, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
118    
119          iflush();          iflush();
120    
# Line 197  int bbs_logout(void) Line 131  int bbs_logout(void)
131                  return -1;                  return -1;
132          }          }
133    
134          if (user_online_del(db) < 0)          if (user_online_exp(db) < 0)
135          {          {
136                  return -2;                  return -2;
137          }          }
138    
139            if (user_online_del(db) < 0)
140            {
141                    return -3;
142            }
143    
144          mysql_close(db);          mysql_close(db);
145    
146          display_file(DATA_GOODBYE, 1, 1);          display_file(DATA_GOODBYE, 1);
147    
148          log_common("User logout\n");          log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
149    
150          return 0;          return 0;
151  }  }
# Line 216  int bbs_center() Line 155  int bbs_center()
155          int ch;          int ch;
156          time_t t_last_action;          time_t t_last_action;
157    
158          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
159    
160          clearscr();          clearscr();
161    
162          show_top("", BBS_name, "");          show_top("", BBS_name, "");
163          show_active_board();          show_active_board();
164          show_bottom("");          show_bottom("");
165          display_menu(p_bbs_menu);          display_menu(&bbs_menu);
166          iflush();          iflush();
167    
168          while (!SYS_server_exit)          while (!SYS_server_exit)
169          {          {
170                  ch = igetch(100);                  ch = igetch(100);
171    
172                  if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
173                  {                  {
174                          t_last_action = time(0);                          BBS_last_access_tm = time(NULL);
175                    }
176    
177                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
178                    {
179                            t_last_action = time(NULL);
180    
181                          show_active_board();                          show_active_board();
182                          show_bottom("");                          show_bottom("");
183                          display_menu_cursor(p_bbs_menu, 1);                          display_menu_cursor(&bbs_menu, 1);
184                          iflush();                          iflush();
185                  }                  }
186    
187                    if (user_online_update("MENU") < 0)
188                    {
189                            log_error("user_online_update(MENU) error\n");
190                    }
191    
192                  switch (ch)                  switch (ch)
193                  {                  {
194                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
195                            log_error("KEY_NULL\n");
196                          return 0;                          return 0;
197                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
198                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
199                          {                          {
200                                    log_error("User input timeout\n");
201                                  return 0;                                  return 0;
202                          }                          }
203                          continue;                          continue;
204                  case CR:                  case CR:
                         igetch_reset();  
205                  default:                  default:
206                          switch (menu_control(p_bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
207                          {                          {
208                          case EXITBBS:                          case EXITBBS:
209                            case EXITMENU:
210                                  return 0;                                  return 0;
211                          case REDRAW:                          case REDRAW:
212                                  t_last_action = time(0);                                  t_last_action = time(NULL);
213                                  clearscr();                                  clearscr();
214                                  show_top("", BBS_name, "");                                  show_top("", BBS_name, "");
215                                  show_active_board();                                  show_active_board();
216                                  show_bottom("");                                  show_bottom("");
217                                  display_menu(p_bbs_menu);                                  display_menu(&bbs_menu);
218                                  break;                                  break;
219                          case NOREDRAW:                          case NOREDRAW:
220                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
# Line 272  int bbs_center() Line 223  int bbs_center()
223                          }                          }
224                          iflush();                          iflush();
225                  }                  }
226            }
227    
228            return 0;
229    }
230    
231    int bbs_charset_select()
232    {
233            char msg[LINE_BUFFER_LEN];
234            int ch;
235    
236                  BBS_last_access_tm = time(0);          snprintf(msg, sizeof(msg),
237                             "\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
238    
239            while (!SYS_server_exit)
240            {
241                    ch = press_any_key_ex(msg, 5);
242                    switch (ch)
243                    {
244                    case KEY_NULL:
245                            return -1;
246                    case KEY_TIMEOUT:
247                    case CR:
248                    case 'u':
249                    case 'U':
250                            return 0;
251                    case 'g':
252                    case 'G':
253                            if (io_conv_init("GBK") < 0)
254                            {
255                                    log_error("io_conv_init(%s) error\n", "GBK");
256                                    return -1;
257                            }
258                            return 0;
259                    default:
260                            continue;
261                    }
262          }          }
263    
264          return 0;          return 0;
# Line 281  int bbs_center() Line 266  int bbs_center()
266    
267  int bbs_main()  int bbs_main()
268  {  {
269            struct sigaction act = {0};
270            char msg[LINE_BUFFER_LEN];
271    
272            // Set signal handler
273            act.sa_handler = SIG_IGN;
274            if (sigaction(SIGHUP, &act, NULL) == -1)
275            {
276                    log_error("set signal action of SIGHUP error: %d\n", errno);
277                    goto cleanup;
278            }
279            act.sa_handler = SIG_DFL;
280            if (sigaction(SIGCHLD, &act, NULL) == -1)
281            {
282                    log_error("set signal action of SIGCHLD error: %d\n", errno);
283                    goto cleanup;
284            }
285    
286          // Set data pools in shared memory readonly          // Set data pools in shared memory readonly
287          if (set_trie_dict_shm_readonly() < 0)          if (set_trie_dict_shm_readonly() < 0)
288          {          {
# Line 294  int bbs_main() Line 296  int bbs_main()
296          {          {
297                  goto cleanup;                  goto cleanup;
298          }          }
299            if (set_user_list_pool_shm_readonly() < 0)
300            {
301                    goto cleanup;
302            }
303    
304          // Load menu in shared memory          // Load menu in shared memory
305          if (set_menu_shm_readonly(p_bbs_menu) < 0)          if (set_menu_shm_readonly(&bbs_menu) < 0)
306          {          {
307                  goto cleanup;                  goto cleanup;
308          }          }
309    
310            // Set default charset
311            if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
312            {
313                    log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
314                    goto cleanup;
315            }
316    
317          set_input_echo(0);          set_input_echo(0);
318    
319            // Set user charset
320            bbs_charset_select();
321    
322          // System info          // System info
323          if (bbs_info() < 0)          if (bbs_info() < 0)
324          {          {
# Line 318  int bbs_main() Line 334  int bbs_main()
334          // User login          // User login
335          if (SSH_v2)          if (SSH_v2)
336          {          {
337                  prints("\033[1m%s ӭʹsshʽ \033[1;33m...\033[m", BBS_username);                  snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
338                  iflush();                  press_any_key_ex(msg, 60);
                 igetch_t(MAX_DELAY_TIME);  
339          }          }
340          else if (bbs_login() < 0)          else if (bbs_login() < 0)
341          {          {
342                  goto cleanup;                  goto cleanup;
343          }          }
344            log_common("User [%s] login\n", BBS_username);
345    
346            // Load article view log
347            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
348            {
349                    log_error("article_view_log_load() error\n");
350                    goto cleanup;
351            }
352    
353          // Load article_view_log          // Load article favorite
354            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                    log_error("editor_memory_pool_init() error\n");
364                    goto cleanup;
365            }
366    
367          clearscr();          clearscr();
368    
369          // BBS Top 10          // BBS Top 10
370          display_file(VAR_BBS_TOP, 1, 1);          display_file(VAR_BBS_TOP, 1);
371    
372          // Main          // Main
373          bbs_center();          bbs_center();
# Line 340  int bbs_main() Line 375  int bbs_main()
375          // Logout          // Logout
376          bbs_logout();          bbs_logout();
377    
378            // 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:  cleanup:
391            // Cleanup iconv
392            io_conv_cleanup();
393    
394            // Cleanup editor memory pool
395            editor_memory_pool_cleanup();
396    
397            // Unload article view log
398            article_view_log_unload(&BBS_article_view_log);
399    
400            // Unload article favor
401            article_favor_unload(&BBS_article_favor);
402    
403          // Detach menu in shared memory          // Detach menu in shared memory
404          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
405          free(p_bbs_menu);          detach_menu_shm(&top10_menu);
         p_bbs_menu = NULL;  
406    
407          // Detach data pools shm          // Detach data pools shm
408            detach_user_list_pool_shm();
409          detach_section_list_shm();          detach_section_list_shm();
410          detach_article_block_shm();          detach_article_block_shm();
411          detach_trie_dict_shm();          detach_trie_dict_shm();


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

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