/[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.73 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.95 by sysadm, Sun Nov 2 04:30:29 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "article_favor.h"
18  #include "article_view_log.h"  #include "article_view_log.h"
19  #include "bbs.h"  #include "bbs.h"
20  #include "bbs_cmd.h"  #include "bbs_cmd.h"
# Line 28  Line 29 
29  #include "screen.h"  #include "screen.h"
30  #include "section_list.h"  #include "section_list.h"
31  #include "trie_dict.h"  #include "trie_dict.h"
32    #include "user_list.h"
33  #include "user_priv.h"  #include "user_priv.h"
34    #include <errno.h>
35    #include <signal.h>
36  #include <stdlib.h>  #include <stdlib.h>
37  #include <string.h>  #include <string.h>
38  #include <time.h>  #include <time.h>
# Line 36  Line 40 
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_INFO);                     BBS_name, BBS_server, APP_INFO);
45    
46          return iflush();          return iflush();
# Line 44  int bbs_info() Line 48  int bbs_info()
48    
49  int bbs_welcome(void)  int bbs_welcome(void)
50  {  {
51          char sql[SQL_BUFFER_LEN];          int u_online = 0;
52            int u_anonymous = 0;
53            int u_total = 0;
54            int u_login_count = 0;
55    
56          u_int32_t u_online = 0;          if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
         u_int32_t u_anonymous = 0;  
         u_int32_t u_total = 0;  
         u_int32_t u_login_count = 0;  
   
         MYSQL *db;  
         MYSQL_RES *rs;  
         MYSQL_ROW row;  
   
         db = db_open();  
         if (db == NULL)  
         {  
                 return -1;  
         }  
   
         snprintf(sql, sizeof(sql),  
                          "SELECT COUNT(*) AS cc FROM "  
                          "(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)  
57          {          {
58                  log_error("Query user_online error: %s\n", mysql_error(db));                  log_error("get_user_online_list_count() error\n");
59                  mysql_close(db);                  u_online = 0;
                 return -2;  
60          }          }
61          if ((rs = mysql_store_result(db)) == NULL)          u_online += u_anonymous;
62            u_online++; // current user
63            if (BBS_priv.uid == 0)
64          {          {
65                  log_error("Get user_online data failed\n");                  u_anonymous++;
                 mysql_close(db);  
                 return -2;  
66          }          }
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_online = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
67    
68          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)  
         {  
                 log_error("Query user_online error: %s\n", mysql_error(db));  
                 mysql_close(db);  
                 return -2;  
         }  
         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);  
   
         snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");  
         if (mysql_query(db, sql) != 0)  
         {  
                 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)))  
69          {          {
70                  u_total = (u_int32_t)atoi(row[0]);                  log_error("get_user_list_count() error\n");
71                    u_total = 0;
72          }          }
         mysql_free_result(rs);  
73    
74          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");          if (get_user_login_count(&u_login_count) < 0)
         if (mysql_query(db, sql) != 0)  
         {  
                 log_error("Query user_login_log error: %s\n", mysql_error(db));  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_login_log data failed\n");  
                 mysql_close(db);  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
75          {          {
76                  u_login_count = (u_int32_t)atoi(row[0]);                  log_error("get_user_login_count() error\n");
77                    u_login_count = 0;
78          }          }
         mysql_free_result(rs);  
   
         mysql_close(db);  
   
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
79    
80          // Display logo          // Display logo
81          display_file(DATA_WELCOME, 2);          display_file(DATA_WELCOME, 2);
82    
83          // Display welcome message          // Display welcome message
84          prints("\r\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
85                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
86                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
87                     "עû[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
88                     " [\033[36m%s\033[32m] ۼƷ˴Σ[\033[36m%d\033[32m]\033[m\r\n",                     "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
89                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
90                     BBS_max_user, BBS_start_dt, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
91    
92          iflush();          iflush();
93    
# Line 178  int bbs_logout(void) Line 104  int bbs_logout(void)
104                  return -1;                  return -1;
105          }          }
106    
107          if (user_online_del(db) < 0)          if (user_online_exp(db) < 0)
108          {          {
109                  return -2;                  return -2;
110          }          }
111    
112            if (user_online_del(db) < 0)
113            {
114                    return -3;
115            }
116    
117          mysql_close(db);          mysql_close(db);
118    
119          display_file(DATA_GOODBYE, 1);          display_file(DATA_GOODBYE, 1);
120    
121          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);
122    
123          return 0;          return 0;
124  }  }
# Line 204  int bbs_center() Line 135  int bbs_center()
135          show_top("", BBS_name, "");          show_top("", BBS_name, "");
136          show_active_board();          show_active_board();
137          show_bottom("");          show_bottom("");
138          display_menu(p_bbs_menu);          display_menu(&bbs_menu);
139          iflush();          iflush();
140    
141          while (!SYS_server_exit)          while (!SYS_server_exit)
142          {          {
143                  ch = igetch(100);                  ch = igetch(100);
144    
145                  if (p_bbs_menu->choose_step == 0 && time(NULL) - t_last_action >= 10)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
146                    {
147                            BBS_last_access_tm = time(NULL);
148                    }
149    
150                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
151                  {                  {
152                          t_last_action = time(NULL);                          t_last_action = time(NULL);
153    
154                          show_active_board();                          show_active_board();
155                          show_bottom("");                          show_bottom("");
156                          display_menu_cursor(p_bbs_menu, 1);                          display_menu_cursor(&bbs_menu, 1);
157                          iflush();                          iflush();
158                  }                  }
159    
# Line 229  int bbs_center() Line 165  int bbs_center()
165                  switch (ch)                  switch (ch)
166                  {                  {
167                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
168                            log_error("KEY_NULL\n");
169                          return 0;                          return 0;
170                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
171                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
172                          {                          {
173                                    log_error("User input timeout\n");
174                                  return 0;                                  return 0;
175                          }                          }
176                          continue;                          continue;
177                  case CR:                  case CR:
                         igetch_reset();  
178                  default:                  default:
179                          switch (menu_control(p_bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
180                          {                          {
181                          case EXITBBS:                          case EXITBBS:
182                            case EXITMENU:
183                                  return 0;                                  return 0;
184                          case REDRAW:                          case REDRAW:
185                                  t_last_action = time(NULL);                                  t_last_action = time(NULL);
# Line 249  int bbs_center() Line 187  int bbs_center()
187                                  show_top("", BBS_name, "");                                  show_top("", BBS_name, "");
188                                  show_active_board();                                  show_active_board();
189                                  show_bottom("");                                  show_bottom("");
190                                  display_menu(p_bbs_menu);                                  display_menu(&bbs_menu);
191                                  break;                                  break;
192                          case NOREDRAW:                          case NOREDRAW:
193                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
# Line 258  int bbs_center() Line 196  int bbs_center()
196                          }                          }
197                          iflush();                          iflush();
198                  }                  }
199            }
200    
201                  BBS_last_access_tm = time(NULL);          return 0;
202    }
203    
204    int bbs_charset_select()
205    {
206            char msg[LINE_BUFFER_LEN];
207            int ch;
208    
209            snprintf(msg, sizeof(msg),
210                             "\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
211    
212            while (!SYS_server_exit)
213            {
214                    ch = press_any_key_ex(msg, 5);
215                    switch (ch)
216                    {
217                    case KEY_NULL:
218                            return -1;
219                    case KEY_TIMEOUT:
220                    case CR:
221                    case 'u':
222                    case 'U':
223                            return 0;
224                    case 'g':
225                    case 'G':
226                            if (io_conv_init("GBK") < 0)
227                            {
228                                    log_error("io_conv_init(%s) error\n", "GBK");
229                                    return -1;
230                            }
231                            return 0;
232                    default:
233                            continue;
234                    }
235          }          }
236    
237          return 0;          return 0;
# Line 267  int bbs_center() Line 239  int bbs_center()
239    
240  int bbs_main()  int bbs_main()
241  {  {
242            struct sigaction act = {0};
243            char msg[LINE_BUFFER_LEN];
244    
245            // Set signal handler
246            act.sa_handler = SIG_IGN;
247            if (sigaction(SIGHUP, &act, NULL) == -1)
248            {
249                    log_error("set signal action of SIGHUP error: %d\n", errno);
250                    goto cleanup;
251            }
252            act.sa_handler = SIG_DFL;
253            if (sigaction(SIGCHLD, &act, NULL) == -1)
254            {
255                    log_error("set signal action of SIGCHLD error: %d\n", errno);
256                    goto cleanup;
257            }
258    
259          // Set data pools in shared memory readonly          // Set data pools in shared memory readonly
260          if (set_trie_dict_shm_readonly() < 0)          if (set_trie_dict_shm_readonly() < 0)
261          {          {
# Line 280  int bbs_main() Line 269  int bbs_main()
269          {          {
270                  goto cleanup;                  goto cleanup;
271          }          }
272            if (set_user_list_pool_shm_readonly() < 0)
273            {
274                    goto cleanup;
275            }
276    
277          // Load menu in shared memory          // Load menu in shared memory
278          if (set_menu_shm_readonly(p_bbs_menu) < 0)          if (set_menu_shm_readonly(&bbs_menu) < 0)
279            {
280                    goto cleanup;
281            }
282    
283            // Set default charset
284            if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
285          {          {
286                    log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
287                  goto cleanup;                  goto cleanup;
288          }          }
289    
290          set_input_echo(0);          set_input_echo(0);
291    
292            // Set user charset
293            bbs_charset_select();
294    
295          // System info          // System info
296          if (bbs_info() < 0)          if (bbs_info() < 0)
297          {          {
# Line 304  int bbs_main() Line 307  int bbs_main()
307          // User login          // User login
308          if (SSH_v2)          if (SSH_v2)
309          {          {
310                  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);
311                  iflush();                  press_any_key_ex(msg, 60);
                 igetch_t(MAX_DELAY_TIME);  
312          }          }
313          else if (bbs_login() < 0)          else if (bbs_login() < 0)
314          {          {
315                  goto cleanup;                  goto cleanup;
316          }          }
317            log_common("User [%s] login\n", BBS_username);
318    
319          // Load article view log          // Load article view log
320          if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)          if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
# Line 320  int bbs_main() Line 323  int bbs_main()
323                  goto cleanup;                  goto cleanup;
324          }          }
325    
326            // Load article favorite
327            if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
328            {
329                    log_error("article_favor_load() error\n");
330                    goto cleanup;
331            }
332    
333          // Init editor memory pool          // Init editor memory pool
334          if (editor_memory_pool_init() < 0)          if (editor_memory_pool_init() < 0)
335          {          {
# Line 344  int bbs_main() Line 354  int bbs_main()
354                  log_error("article_view_log_save_inc() error\n");                  log_error("article_view_log_save_inc() error\n");
355          }          }
356    
357            // Save incremental article favorite
358            if (article_favor_save_inc(&BBS_article_favor) < 0)
359            {
360                    log_error("article_favor_save_inc() error\n");
361            }
362    
363  cleanup:  cleanup:
364            // Cleanup iconv
365            io_conv_cleanup();
366    
367          // Cleanup editor memory pool          // Cleanup editor memory pool
368          editor_memory_pool_cleanup();          editor_memory_pool_cleanup();
369    
370          // Unload article view log          // Unload article view log
371          article_view_log_unload(&BBS_article_view_log);          article_view_log_unload(&BBS_article_view_log);
372    
373            // Unload article favor
374            article_favor_unload(&BBS_article_favor);
375    
376          // Detach menu in shared memory          // Detach menu in shared memory
377          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
378          free(p_bbs_menu);          detach_menu_shm(&top10_menu);
         p_bbs_menu = NULL;  
379    
380          // Detach data pools shm          // Detach data pools shm
381            detach_user_list_pool_shm();
382          detach_section_list_shm();          detach_section_list_shm();
383          detach_article_block_shm();          detach_article_block_shm();
384          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