/[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.78 by sysadm, Mon Oct 13 00:46:03 2025 UTC Revision 1.96 by sysadm, Sun Nov 2 08:13:50 2025 UTC
# Line 28  Line 28 
28  #include "menu.h"  #include "menu.h"
29  #include "screen.h"  #include "screen.h"
30  #include "section_list.h"  #include "section_list.h"
31    #include "section_list_display.h"
32  #include "trie_dict.h"  #include "trie_dict.h"
33    #include "user_list.h"
34  #include "user_priv.h"  #include "user_priv.h"
35  #include <errno.h>  #include <errno.h>
36  #include <signal.h>  #include <signal.h>
# Line 47  int bbs_info() Line 49  int bbs_info()
49    
50  int bbs_welcome(void)  int bbs_welcome(void)
51  {  {
52          char sql[SQL_BUFFER_LEN];          int u_online = 0;
53            int u_anonymous = 0;
54            int u_total = 0;
55            int u_login_count = 0;
56    
57          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)  
58          {          {
59                  return -1;                  log_error("get_user_online_list_count() error\n");
60                    u_online = 0;
61          }          }
62            u_online += u_anonymous;
63          snprintf(sql, sizeof(sql),          u_online++; // current user
64                           "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)  
65          {          {
66                  log_error("Query user_online error: %s\n", mysql_error(db));                  u_anonymous++;
                 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;  
67          }          }
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_online = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
68    
69          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)  
70          {          {
71                  log_error("Get user_online data failed\n");                  log_error("get_user_list_count() error\n");
72                  mysql_close(db);                  u_total = 0;
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_anonymous = (u_int32_t)atoi(row[0]);  
73          }          }
         mysql_free_result(rs);  
74    
75          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");          if (get_user_login_count(&u_login_count) < 0)
         if (mysql_query(db, sql) != 0)  
76          {          {
77                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("get_user_login_count() error\n");
78                  mysql_close(db);                  u_login_count = 0;
                 return -2;  
79          }          }
         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)))  
         {  
                 u_total = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
   
         snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");  
         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)))  
         {  
                 u_login_count = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
   
         mysql_close(db);  
   
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
80    
81          // Display logo          // Display logo
82          display_file(DATA_WELCOME, 2);          display_file(DATA_WELCOME, 2);
# Line 164  int bbs_welcome(void) Line 88  int bbs_welcome(void)
88                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
89                     "从 [\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",
90                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
91                     BBS_max_user, BBS_start_dt, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
92    
93          iflush();          iflush();
94    
# Line 181  int bbs_logout(void) Line 105  int bbs_logout(void)
105                  return -1;                  return -1;
106          }          }
107    
108          if (user_online_del(db) < 0)          if (user_online_exp(db) < 0)
109          {          {
110                  return -2;                  return -2;
111          }          }
112    
113            if (user_online_del(db) < 0)
114            {
115                    return -3;
116            }
117    
118          mysql_close(db);          mysql_close(db);
119    
120          display_file(DATA_GOODBYE, 1);          display_file(DATA_GOODBYE, 1);
121    
122          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);
123    
124          return 0;          return 0;
125  }  }
# Line 214  int bbs_center() Line 143  int bbs_center()
143          {          {
144                  ch = igetch(100);                  ch = igetch(100);
145    
146                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
147                    {
148                            BBS_last_access_tm = time(NULL);
149                    }
150    
151                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
152                  {                  {
153                          t_last_action = time(NULL);                          t_last_action = time(NULL);
# Line 232  int bbs_center() Line 166  int bbs_center()
166                  switch (ch)                  switch (ch)
167                  {                  {
168                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
169                            log_error("KEY_NULL\n");
170                          return 0;                          return 0;
171                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
172                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
173                          {                          {
174                                    log_error("User input timeout\n");
175                                  return 0;                                  return 0;
176                          }                          }
177                          continue;                          continue;
178                  case CR:                  case CR:
                         igetch_reset();  
179                  default:                  default:
180                          switch (menu_control(&bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
181                          {                          {
# Line 262  int bbs_center() Line 197  int bbs_center()
197                          }                          }
198                          iflush();                          iflush();
199                  }                  }
200            }
201    
202            return 0;
203    }
204    
205    int bbs_charset_select()
206    {
207            char msg[LINE_BUFFER_LEN];
208            int ch;
209    
210                  BBS_last_access_tm = time(NULL);          snprintf(msg, sizeof(msg),
211                             "\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
212    
213            while (!SYS_server_exit)
214            {
215                    ch = press_any_key_ex(msg, 5);
216                    switch (ch)
217                    {
218                    case KEY_NULL:
219                            return -1;
220                    case KEY_TIMEOUT:
221                    case CR:
222                    case 'u':
223                    case 'U':
224                            return 0;
225                    case 'g':
226                    case 'G':
227                            if (io_conv_init("GBK") < 0)
228                            {
229                                    log_error("io_conv_init(%s) error\n", "GBK");
230                                    return -1;
231                            }
232                            return 0;
233                    default:
234                            continue;
235                    }
236          }          }
237    
238          return 0;          return 0;
# Line 272  int bbs_center() Line 241  int bbs_center()
241  int bbs_main()  int bbs_main()
242  {  {
243          struct sigaction act = {0};          struct sigaction act = {0};
244            char msg[LINE_BUFFER_LEN];
245    
246          // Set signal handler          // Set signal handler
247          act.sa_handler = SIG_IGN;          act.sa_handler = SIG_IGN;
# Line 300  int bbs_main() Line 270  int bbs_main()
270          {          {
271                  goto cleanup;                  goto cleanup;
272          }          }
273            if (set_user_list_pool_shm_readonly() < 0)
274            {
275                    goto cleanup;
276            }
277    
278          // Load menu in shared memory          // Load menu in shared memory
279          if (set_menu_shm_readonly(&bbs_menu) < 0)          if (set_menu_shm_readonly(&bbs_menu) < 0)
# Line 307  int bbs_main() Line 281  int bbs_main()
281                  goto cleanup;                  goto cleanup;
282          }          }
283    
284            // Set default charset
285            if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
286            {
287                    log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
288                    goto cleanup;
289            }
290    
291          set_input_echo(0);          set_input_echo(0);
292    
293            // Set user charset
294            bbs_charset_select();
295    
296          // System info          // System info
297          if (bbs_info() < 0)          if (bbs_info() < 0)
298          {          {
# Line 324  int bbs_main() Line 308  int bbs_main()
308          // User login          // User login
309          if (SSH_v2)          if (SSH_v2)
310          {          {
311                  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);
312                  iflush();                  press_any_key_ex(msg, 60);
                 igetch_t(MAX_DELAY_TIME);  
313          }          }
314          else if (bbs_login() < 0)          else if (bbs_login() < 0)
315          {          {
316                  goto cleanup;                  goto cleanup;
317          }          }
318            log_common("User [%s] login\n", BBS_username);
319    
320            // Load section aid locations
321            if (section_aid_locations_load(BBS_priv.uid) < 0)
322            {
323                    log_error("article_view_log_load() error\n");
324                    goto cleanup;
325            }
326            
327          // Load article view log          // Load article view log
328          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)
329          {          {
# Line 365  int bbs_main() Line 356  int bbs_main()
356          // Logout          // Logout
357          bbs_logout();          bbs_logout();
358    
359            // Save section aid locations
360            if (section_aid_locations_save(BBS_priv.uid) < 0)
361            {
362                    log_error("article_view_log_save() error\n");
363            }
364    
365          // Save incremental article view log          // Save incremental article view log
366          if (article_view_log_save_inc(&BBS_article_view_log) < 0)          if (article_view_log_save_inc(&BBS_article_view_log) < 0)
367          {          {
# Line 378  int bbs_main() Line 375  int bbs_main()
375          }          }
376    
377  cleanup:  cleanup:
378            // Cleanup iconv
379            io_conv_cleanup();
380    
381          // Cleanup editor memory pool          // Cleanup editor memory pool
382          editor_memory_pool_cleanup();          editor_memory_pool_cleanup();
383    
# Line 389  cleanup: Line 389  cleanup:
389    
390          // Detach menu in shared memory          // Detach menu in shared memory
391          detach_menu_shm(&bbs_menu);          detach_menu_shm(&bbs_menu);
392            detach_menu_shm(&top10_menu);
393    
394          // Detach data pools shm          // Detach data pools shm
395            detach_user_list_pool_shm();
396          detach_section_list_shm();          detach_section_list_shm();
397          detach_article_block_shm();          detach_article_block_shm();
398          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