/[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.45 by sysadm, Thu May 15 05:14:57 2025 UTC Revision 1.96 by sysadm, Sun Nov 2 08:13:50 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 "section_list_display.h"
32    #include "trie_dict.h"
33    #include "user_list.h"
34    #include "user_priv.h"
35    #include <errno.h>
36    #include <signal.h>
37    #include <stdlib.h>
38  #include <string.h>  #include <string.h>
39    #include <time.h>
40    #include <unistd.h>
41    
42  int bbs_info()  int bbs_info()
43  {  {
44          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",
45                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_INFO);
46    
47          return iflush();          return iflush();
48  }  }
49    
50  int bbs_welcome(MYSQL *db)  int bbs_welcome(void)
51  {  {
52          char sql[SQL_BUFFER_LEN];          int u_online = 0;
53            int u_anonymous = 0;
54          u_int32_t u_online = 0;          int u_total = 0;
55          u_int32_t u_anonymous = 0;          int u_login_count = 0;
         u_int32_t u_total = 0;  
         u_int32_t max_u_online = 0;  
         u_int32_t u_login_count = 0;  
   
         MYSQL_RES *rs;  
         MYSQL_ROW row;  
   
         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)  
         {  
                 log_error("Query user_online failed\n");  
                 return -2;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_online data failed\n");  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_online = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
56    
57          snprintf(sql, sizeof(sql),          if (get_user_online_list_count(&u_online, &u_anonymous) < 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)  
58          {          {
59                  log_error("Query user_online failed\n");                  log_error("get_user_online_list_count() error\n");
60                  return -2;                  u_online = 0;
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_online data failed\n");  
                 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 failed\n");  
                 return -2;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_list data failed\n");  
                 return -2;  
61          }          }
62          if ((row = mysql_fetch_row(rs)))          u_online += u_anonymous;
63            u_online++; // current user
64            if (BBS_priv.uid == 0)
65          {          {
66                  u_total = (u_int32_t)atoi(row[0]);                  u_anonymous++;
67          }          }
         mysql_free_result(rs);  
68    
69          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");          if (get_user_list_count(&u_total) < 0)
         if (mysql_query(db, sql) != 0)  
70          {          {
71                  log_error("Query user_login_log failed\n");                  log_error("get_user_list_count() error\n");
72                  return -2;                  u_total = 0;
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_login_log data failed\n");  
                 return -2;  
         }  
         if ((row = mysql_fetch_row(rs)))  
         {  
                 u_login_count = (u_int32_t)atoi(row[0]);  
73          }          }
         mysql_free_result(rs);  
74    
75          // Log max user_online          if (get_user_login_count(&u_login_count) < 0)
         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)  
76          {          {
77                  max_u_online = u_online;                  log_error("get_user_login_count() error\n");
78                  if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL)                  u_login_count = 0;
                 {  
                         log_error("Open max_user_online.dat failed\n");  
                         return -3;  
                 }  
                 fprintf(fout, "%d\n", max_u_online);  
                 fclose(fout);  
79          }          }
80    
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
   
81          // Display logo          // Display logo
82          display_file(DATA_WELCOME);          display_file(DATA_WELCOME, 2);
83    
84          // Display welcome message          // Display welcome message
85          prints("\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
86                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
87                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
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%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
                    "[\033[36m%d\033[32m]ۼƷ˴Σ[\033[36m%d\033[32m]\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, max_u_online, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
92    
93            iflush();
94    
95          return 0;          return 0;
96  }  }
97    
98  int bbs_logout(MYSQL *db)  int bbs_logout(void)
99  {  {
100          if (user_online_del(db) < 0)          MYSQL *db;
101    
102            db = db_open();
103            if (db == NULL)
104          {          {
105                  return -1;                  return -1;
106          }          }
107    
108          if (display_file_ex(DATA_GOODBYE, 1, 0) < 0)          if (user_online_exp(db) < 0)
109          {          {
110                  return -2;                  return -2;
111          }          }
112    
113          log_std("User logout\n");          if (user_online_del(db) < 0)
114            {
115                    return -3;
116            }
117    
118            mysql_close(db);
119    
120            display_file(DATA_GOODBYE, 1);
121    
122            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 187  int bbs_center() Line 129  int bbs_center()
129          int ch;          int ch;
130          time_t t_last_action;          time_t t_last_action;
131    
132          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
133    
134          clearscr();          clearscr();
135    
136          show_top("");          show_top("", BBS_name, "");
137          show_active_board();          show_active_board();
138          show_bottom("");          show_bottom("");
139          display_menu(p_bbs_menu);          display_menu(&bbs_menu);
140            iflush();
141    
142          while (!SYS_server_exit)          while (!SYS_server_exit)
143          {          {
144                  ch = igetch(100);                  ch = igetch(100);
145    
146                  if (time(0) - t_last_action >= 10)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
147                  {                  {
148                          t_last_action = time(0);                          BBS_last_access_tm = time(NULL);
149                    }
150    
151                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
152                    {
153                            t_last_action = time(NULL);
154    
155                          show_active_board();                          show_active_board();
156                          show_bottom("");                          show_bottom("");
157                            display_menu_cursor(&bbs_menu, 1);
158                            iflush();
159                    }
160    
161                    if (user_online_update("MENU") < 0)
162                    {
163                            log_error("user_online_update(MENU) error\n");
164                  }                  }
165    
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(0) - 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(p_bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
181                          {                          {
182                          case EXITBBS:                          case EXITBBS:
183                            case EXITMENU:
184                                  return 0;                                  return 0;
185                          case REDRAW:                          case REDRAW:
186                                    t_last_action = time(NULL);
187                                  clearscr();                                  clearscr();
188                                  show_top("");                                  show_top("", BBS_name, "");
189                                  show_active_board();                                  show_active_board();
190                                  show_bottom("");                                  show_bottom("");
191                                  display_menu(p_bbs_menu);                                  display_menu(&bbs_menu);
192                                  break;                                  break;
193                          case NOREDRAW:                          case NOREDRAW:
194                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
195                          default:                          default:
196                                  break;                                  break;
197                          }                          }
198                            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            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                  }                  }
                 BBS_last_access_tm = time(0);  
236          }          }
237    
238          return 0;          return 0;
# Line 245  int bbs_center() Line 240  int bbs_center()
240    
241  int bbs_main()  int bbs_main()
242  {  {
243          MYSQL *db;          struct sigaction act = {0};
244            char msg[LINE_BUFFER_LEN];
245    
246            // Set signal handler
247            act.sa_handler = SIG_IGN;
248            if (sigaction(SIGHUP, &act, NULL) == -1)
249            {
250                    log_error("set signal action of SIGHUP error: %d\n", errno);
251                    goto cleanup;
252            }
253            act.sa_handler = SIG_DFL;
254            if (sigaction(SIGCHLD, &act, NULL) == -1)
255            {
256                    log_error("set signal action of SIGCHLD error: %d\n", errno);
257                    goto cleanup;
258            }
259    
260            // Set data pools in shared memory readonly
261            if (set_trie_dict_shm_readonly() < 0)
262            {
263                    goto cleanup;
264            }
265            if (set_article_block_shm_readonly() < 0)
266            {
267                    goto cleanup;
268            }
269            if (set_section_list_shm_readonly() < 0)
270            {
271                    goto cleanup;
272            }
273            if (set_user_list_pool_shm_readonly() < 0)
274            {
275                    goto cleanup;
276            }
277    
278            // Load menu in shared memory
279            if (set_menu_shm_readonly(&bbs_menu) < 0)
280            {
281                    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          {          {
299                  return -1;                  goto cleanup;
300          }          }
301    
302          db = db_open();          // Welcome
303          if (db == NULL)          if (bbs_welcome() < 0)
304          {          {
305                  prints("޷ݿ\n");                  goto cleanup;
                 return -2;  
306          }          }
307    
308          // Welcome          // User login
309          if (bbs_welcome(db) < 0)          if (SSH_v2)
310          {          {
311                  mysql_close(db);                  snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
312                  return -3;                  press_any_key_ex(msg, 60);
313            }
314            else if (bbs_login() < 0)
315            {
316                    goto cleanup;
317          }          }
318            log_common("User [%s] login\n", BBS_username);
319    
320          // User login          // Load section aid locations
321          if (bbs_login(db) < 0)          if (section_aid_locations_load(BBS_priv.uid) < 0)
322          {          {
323                  mysql_close(db);                  log_error("article_view_log_load() error\n");
324                  return -4;                  goto cleanup;
325            }
326            
327            // Load article view log
328            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
329            {
330                    log_error("article_view_log_load() error\n");
331                    goto cleanup;
332          }          }
         clearscr();  
333    
334          // BBS Top 10          // Load article favorite
335          if (display_file_ex("./var/bbs_top.txt", 1, 1) < 0)          if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
336          {          {
337                  prints("޷رվʮ\n");                  log_error("article_favor_load() error\n");
338                    goto cleanup;
339          }          }
340    
341            // Init editor memory pool
342            if (editor_memory_pool_init() < 0)
343            {
344                    log_error("editor_memory_pool_init() error\n");
345                    goto cleanup;
346            }
347    
348            clearscr();
349    
350            // BBS Top 10
351            display_file(VAR_BBS_TOP, 1);
352    
353          // Main          // Main
354          bbs_center();          bbs_center();
355    
356          // Logout          // Logout
357          bbs_logout(db);          bbs_logout();
358    
359          mysql_close(db);          // 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
366            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
367            {
368                    log_error("article_view_log_save_inc() error\n");
369            }
370    
371            // Save incremental article favorite
372            if (article_favor_save_inc(&BBS_article_favor) < 0)
373            {
374                    log_error("article_favor_save_inc() error\n");
375            }
376    
377    cleanup:
378            // Cleanup iconv
379            io_conv_cleanup();
380    
381            // Cleanup editor memory pool
382            editor_memory_pool_cleanup();
383    
384            // Unload article view log
385            article_view_log_unload(&BBS_article_view_log);
386    
387            // Unload article favor
388            article_favor_unload(&BBS_article_favor);
389    
390            // Detach menu in shared memory
391            detach_menu_shm(&bbs_menu);
392            detach_menu_shm(&top10_menu);
393    
394            // Detach data pools shm
395            detach_user_list_pool_shm();
396            detach_section_list_shm();
397            detach_article_block_shm();
398            detach_trie_dict_shm();
399    
400          return 0;          return 0;
401  }  }


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

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