/[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.77 by sysadm, Sat Jul 5 01:02:34 2025 UTC Revision 1.94 by sysadm, Sat Nov 1 10:35:42 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>  #include <errno.h>
35  #include <signal.h>  #include <signal.h>
# Line 48  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 u_login_count = 0;          int 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)  
         {  
                 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)  
63          {          {
64                  log_error("Query user_online error: %s\n", mysql_error(db));                  log_error("get_user_online_list_count() error\n");
65                  mysql_close(db);                  u_online = 0;
                 return -2;  
66          }          }
67          if ((rs = mysql_store_result(db)) == NULL)          u_online += u_anonymous;
68            u_online++; // current user
69            if (BBS_priv.uid == 0)
70          {          {
71                  log_error("Get user_online data failed\n");                  u_anonymous++;
                 mysql_close(db);  
                 return -2;  
72          }          }
         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 143  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    
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
   
107          // Display logo          // Display logo
108          display_file(DATA_WELCOME, 2);          display_file(DATA_WELCOME, 2);
109    
# Line 163  int bbs_welcome(void) Line 114  int bbs_welcome(void)
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%d\033[32m]\033[m\r\n",                     "从 [\033[36m%s\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, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
118    
119          iflush();          iflush();
120    
# Line 180  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);          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 213  int bbs_center() Line 169  int bbs_center()
169          {          {
170                  ch = igetch(100);                  ch = igetch(100);
171    
172                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
173                    {
174                            BBS_last_access_tm = time(NULL);
175                    }
176    
177                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
178                  {                  {
179                          t_last_action = time(NULL);                          t_last_action = time(NULL);
# Line 231  int bbs_center() Line 192  int bbs_center()
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(NULL) - 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(&bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
207                          {                          {
# Line 261  int bbs_center() Line 223  int bbs_center()
223                          }                          }
224                          iflush();                          iflush();
225                  }                  }
226            }
227    
228            return 0;
229    }
230    
231                  BBS_last_access_tm = time(NULL);  int bbs_charset_select()
232    {
233            char msg[LINE_BUFFER_LEN];
234            int ch;
235    
236            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 271  int bbs_center() Line 267  int bbs_center()
267  int bbs_main()  int bbs_main()
268  {  {
269          struct sigaction act = {0};          struct sigaction act = {0};
270            char msg[LINE_BUFFER_LEN];
271    
272          // Set signal handler          // Set signal handler
273          act.sa_handler = SIG_IGN;          act.sa_handler = SIG_IGN;
# Line 299  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(&bbs_menu) < 0)          if (set_menu_shm_readonly(&bbs_menu) < 0)
# Line 306  int bbs_main() Line 307  int bbs_main()
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 323  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          // Load article view log
347          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 339  int bbs_main() Line 350  int bbs_main()
350                  goto cleanup;                  goto cleanup;
351          }          }
352    
353            // 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          // Init editor memory pool
361          if (editor_memory_pool_init() < 0)          if (editor_memory_pool_init() < 0)
362          {          {
# Line 363  int bbs_main() Line 381  int bbs_main()
381                  log_error("article_view_log_save_inc() error\n");                  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          // Cleanup editor memory pool
395          editor_memory_pool_cleanup();          editor_memory_pool_cleanup();
396    
397          // Unload article view log          // Unload article view log
398          article_view_log_unload(&BBS_article_view_log);          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(&bbs_menu);          detach_menu_shm(&bbs_menu);
405            detach_menu_shm(&top10_menu);
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