/[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.93 by sysadm, Tue Oct 21 06:24:51 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 "trie_dict.h"
32    #include "user_list.h"
33    #include "user_priv.h"
34    #include <errno.h>
35    #include <signal.h>
36    #include <stdlib.h>
37  #include <string.h>  #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_version);                     BBS_name, BBS_server, APP_INFO);
45    
46          return iflush();          return iflush();
47  }  }
48    
49  int bbs_welcome(MYSQL *db)  int bbs_welcome(void)
50  {  {
51          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
52    
53          u_int32_t u_online = 0;          u_int32_t u_online = 0;
54          u_int32_t u_anonymous = 0;          u_int32_t u_anonymous = 0;
55          u_int32_t u_total = 0;          u_int32_t u_total = 0;
         u_int32_t max_u_online = 0;  
56          u_int32_t u_login_count = 0;          u_int32_t u_login_count = 0;
57    
58            MYSQL *db;
59          MYSQL_RES *rs;          MYSQL_RES *rs;
60          MYSQL_ROW row;          MYSQL_ROW row;
61    
62            db = db_open();
63            if (db == NULL)
64            {
65                    return -1;
66            }
67    
68          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
69                           "SELECT COUNT(*) AS cc FROM "                           "SELECT COUNT(*) AS cc FROM "
70                           "(SELECT DISTINCT SID FROM user_online "                           "(SELECT DISTINCT SID FROM user_online "
# Line 57  int bbs_welcome(MYSQL *db) Line 72  int bbs_welcome(MYSQL *db)
72                           BBS_user_off_line);                           BBS_user_off_line);
73          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
74          {          {
75                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
76                    mysql_close(db);
77                  return -2;                  return -2;
78          }          }
79          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
80          {          {
81                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
82                    mysql_close(db);
83                  return -2;                  return -2;
84          }          }
85          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 78  int bbs_welcome(MYSQL *db) Line 95  int bbs_welcome(MYSQL *db)
95                           BBS_user_off_line);                           BBS_user_off_line);
96          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
97          {          {
98                  log_error("Query user_online failed\n");                  log_error("Query user_online error: %s\n", mysql_error(db));
99                    mysql_close(db);
100                  return -2;                  return -2;
101          }          }
102          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
103          {          {
104                  log_error("Get user_online data failed\n");                  log_error("Get user_online data failed\n");
105                    mysql_close(db);
106                  return -2;                  return -2;
107          }          }
108          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 95  int bbs_welcome(MYSQL *db) Line 114  int bbs_welcome(MYSQL *db)
114          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
115          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
116          {          {
117                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
118                    mysql_close(db);
119                  return -2;                  return -2;
120          }          }
121          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
122          {          {
123                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
124                    mysql_close(db);
125                  return -2;                  return -2;
126          }          }
127          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 112  int bbs_welcome(MYSQL *db) Line 133  int bbs_welcome(MYSQL *db)
133          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");
134          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
135          {          {
136                  log_error("Query user_login_log failed\n");                  log_error("Query user_login_log error: %s\n", mysql_error(db));
137                    mysql_close(db);
138                  return -2;                  return -2;
139          }          }
140          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
141          {          {
142                  log_error("Get user_login_log data failed\n");                  log_error("Get user_login_log data failed\n");
143                    mysql_close(db);
144                  return -2;                  return -2;
145          }          }
146          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 126  int bbs_welcome(MYSQL *db) Line 149  int bbs_welcome(MYSQL *db)
149          }          }
150          mysql_free_result(rs);          mysql_free_result(rs);
151    
152          // Log max user_online          mysql_close(db);
         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);  
         }  
153    
154          // Count current user before login          // Count current user before login
155          u_online++;          u_online++;
156          u_anonymous++;          u_anonymous++;
157    
158          // Display logo          // Display logo
159          display_file(DATA_WELCOME);          display_file(DATA_WELCOME, 2);
160    
161          // Display welcome message          // Display welcome message
162          prints("\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
163                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
164                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
165                     "עû[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
166                     " [\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",  
167                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
168                     BBS_max_user, BBS_start_dt, max_u_online, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
169    
170            iflush();
171    
172          return 0;          return 0;
173  }  }
174    
175  int bbs_logout(MYSQL *db)  int bbs_logout(void)
176  {  {
177          if (user_online_del(db) < 0)          MYSQL *db;
178    
179            db = db_open();
180            if (db == NULL)
181          {          {
182                  return -1;                  return -1;
183          }          }
184    
185          if (display_file_ex(DATA_GOODBYE, 1, 0) < 0)          if (user_online_exp(db) < 0)
186          {          {
187                  return -2;                  return -2;
188          }          }
189    
190          log_std("User logout\n");          if (user_online_del(db) < 0)
191            {
192                    return -3;
193            }
194    
195            mysql_close(db);
196    
197            display_file(DATA_GOODBYE, 1);
198    
199            log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
200    
201          return 0;          return 0;
202  }  }
# Line 187  int bbs_center() Line 206  int bbs_center()
206          int ch;          int ch;
207          time_t t_last_action;          time_t t_last_action;
208    
209          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
210    
211          clearscr();          clearscr();
212    
213          show_top("");          show_top("", BBS_name, "");
214          show_active_board();          show_active_board();
215          show_bottom("");          show_bottom("");
216          display_menu(p_bbs_menu);          display_menu(&bbs_menu);
217            iflush();
218    
219          while (!SYS_server_exit)          while (!SYS_server_exit)
220          {          {
221                  ch = igetch(100);                  ch = igetch(100);
222    
223                  if (time(0) - t_last_action >= 10)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
224                  {                  {
225                          t_last_action = time(0);                          BBS_last_access_tm = time(NULL);
226                    }
227    
228                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
229                    {
230                            t_last_action = time(NULL);
231    
232                          show_active_board();                          show_active_board();
233                          show_bottom("");                          show_bottom("");
234                            display_menu_cursor(&bbs_menu, 1);
235                            iflush();
236                    }
237    
238                    if (user_online_update("MENU") < 0)
239                    {
240                            log_error("user_online_update(MENU) error\n");
241                  }                  }
242    
243                  switch (ch)                  switch (ch)
244                  {                  {
245                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
246                            log_error("KEY_NULL\n");
247                          return 0;                          return 0;
248                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
249                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
250                          {                          {
251                                    log_error("User input timeout\n");
252                                  return 0;                                  return 0;
253                          }                          }
254                          continue;                          continue;
255                  case CR:                  case CR:
                         igetch_reset();  
256                  default:                  default:
257                          switch (menu_control(p_bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
258                          {                          {
259                          case EXITBBS:                          case EXITBBS:
260                            case EXITMENU:
261                                  return 0;                                  return 0;
262                          case REDRAW:                          case REDRAW:
263                                    t_last_action = time(NULL);
264                                  clearscr();                                  clearscr();
265                                  show_top("");                                  show_top("", BBS_name, "");
266                                  show_active_board();                                  show_active_board();
267                                  show_bottom("");                                  show_bottom("");
268                                  display_menu(p_bbs_menu);                                  display_menu(&bbs_menu);
269                                  break;                                  break;
270                          case NOREDRAW:                          case NOREDRAW:
271                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
272                          default:                          default:
273                                  break;                                  break;
274                          }                          }
275                            iflush();
276                    }
277            }
278    
279            return 0;
280    }
281    
282    int bbs_charset_select()
283    {
284            char msg[LINE_BUFFER_LEN];
285            int ch;
286    
287            snprintf(msg, sizeof(msg),
288                             "\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
289    
290            while (!SYS_server_exit)
291            {
292                    ch = press_any_key_ex(msg, 5);
293                    switch (ch)
294                    {
295                    case KEY_NULL:
296                            return -1;
297                    case KEY_TIMEOUT:
298                    case CR:
299                    case 'u':
300                    case 'U':
301                            return 0;
302                    case 'g':
303                    case 'G':
304                            if (io_conv_init("GBK") < 0)
305                            {
306                                    log_error("io_conv_init(%s) error\n", "GBK");
307                                    return -1;
308                            }
309                            return 0;
310                    default:
311                            continue;
312                  }                  }
                 BBS_last_access_tm = time(0);  
313          }          }
314    
315          return 0;          return 0;
# Line 245  int bbs_center() Line 317  int bbs_center()
317    
318  int bbs_main()  int bbs_main()
319  {  {
320          MYSQL *db;          struct sigaction act = {0};
321            char msg[LINE_BUFFER_LEN];
322    
323            // Set signal handler
324            act.sa_handler = SIG_IGN;
325            if (sigaction(SIGHUP, &act, NULL) == -1)
326            {
327                    log_error("set signal action of SIGHUP error: %d\n", errno);
328                    goto cleanup;
329            }
330            act.sa_handler = SIG_DFL;
331            if (sigaction(SIGCHLD, &act, NULL) == -1)
332            {
333                    log_error("set signal action of SIGCHLD error: %d\n", errno);
334                    goto cleanup;
335            }
336    
337            // Set data pools in shared memory readonly
338            if (set_trie_dict_shm_readonly() < 0)
339            {
340                    goto cleanup;
341            }
342            if (set_article_block_shm_readonly() < 0)
343            {
344                    goto cleanup;
345            }
346            if (set_section_list_shm_readonly() < 0)
347            {
348                    goto cleanup;
349            }
350            if (set_user_list_pool_shm_readonly() < 0)
351            {
352                    goto cleanup;
353            }
354    
355            // Load menu in shared memory
356            if (set_menu_shm_readonly(&bbs_menu) < 0)
357            {
358                    goto cleanup;
359            }
360    
361            // Set default charset
362            if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
363            {
364                    log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
365                    goto cleanup;
366            }
367    
368          set_input_echo(0);          set_input_echo(0);
369    
370            // Set user charset
371            bbs_charset_select();
372    
373          // System info          // System info
374          if (bbs_info() < 0)          if (bbs_info() < 0)
375          {          {
376                  return -1;                  goto cleanup;
377          }          }
378    
379          db = db_open();          // Welcome
380          if (db == NULL)          if (bbs_welcome() < 0)
381          {          {
382                  prints("޷ݿ\n");                  goto cleanup;
                 return -2;  
383          }          }
384    
385          // Welcome          // User login
386          if (bbs_welcome(db) < 0)          if (SSH_v2)
387          {          {
388                  mysql_close(db);                  snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
389                  return -3;                  press_any_key_ex(msg, 60);
390          }          }
391            else if (bbs_login() < 0)
392            {
393                    goto cleanup;
394            }
395            log_common("User [%s] login\n", BBS_username);
396    
397          // User login          // Load article view log
398          if (bbs_login(db) < 0)          if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
399          {          {
400                  mysql_close(db);                  log_error("article_view_log_load() error\n");
401                  return -4;                  goto cleanup;
402          }          }
         clearscr();  
403    
404          // BBS Top 10          // Load article favorite
405          if (display_file_ex("./var/bbs_top.txt", 1, 1) < 0)          if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
406            {
407                    log_error("article_favor_load() error\n");
408                    goto cleanup;
409            }
410    
411            // Init editor memory pool
412            if (editor_memory_pool_init() < 0)
413          {          {
414                  prints("޷رվʮ\n");                  log_error("editor_memory_pool_init() error\n");
415                    goto cleanup;
416          }          }
417    
418            clearscr();
419    
420            // BBS Top 10
421            display_file(VAR_BBS_TOP, 1);
422    
423          // Main          // Main
424          bbs_center();          bbs_center();
425    
426          // Logout          // Logout
427          bbs_logout(db);          bbs_logout();
428    
429          mysql_close(db);          // Save incremental article view log
430            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
431            {
432                    log_error("article_view_log_save_inc() error\n");
433            }
434    
435            // Save incremental article favorite
436            if (article_favor_save_inc(&BBS_article_favor) < 0)
437            {
438                    log_error("article_favor_save_inc() error\n");
439            }
440    
441    cleanup:
442            // Cleanup iconv
443            io_conv_cleanup();
444    
445            // Cleanup editor memory pool
446            editor_memory_pool_cleanup();
447    
448            // Unload article view log
449            article_view_log_unload(&BBS_article_view_log);
450    
451            // Unload article favor
452            article_favor_unload(&BBS_article_favor);
453    
454            // Detach menu in shared memory
455            detach_menu_shm(&bbs_menu);
456            detach_menu_shm(&top10_menu);
457    
458            // Detach data pools shm
459            detach_user_list_pool_shm();
460            detach_section_list_shm();
461            detach_article_block_shm();
462            detach_trie_dict_shm();
463    
464          return 0;          return 0;
465  }  }


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

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