/[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.56 by sysadm, Tue May 27 01:11:14 2025 UTC Revision 1.111 by sysadm, Thu Nov 20 09:02:46 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    bbs_main.c  -  description  /*
3                                                           -------------------   * bbs_main
4          Copyright            : (C) 2004-2025 by Leaflet   *   - entry and major procedures of user interactive access
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs_main.h"  #include "article_favor.h"
14    #include "article_view_log.h"
15  #include "bbs.h"  #include "bbs.h"
16  #include "login.h"  #include "bbs_cmd.h"
17  #include "user_priv.h"  #include "bbs_main.h"
18    #include "bwf.h"
19  #include "common.h"  #include "common.h"
20  #include "database.h"  #include "database.h"
21  #include "log.h"  #include "editor.h"
22  #include "io.h"  #include "io.h"
23  #include "screen.h"  #include "log.h"
24    #include "login.h"
25  #include "menu.h"  #include "menu.h"
26  #include "bbs_cmd.h"  #include "screen.h"
27  #include "section_list.h"  #include "section_list.h"
28    #include "section_list_display.h"
29    #include "str_process.h"
30  #include "trie_dict.h"  #include "trie_dict.h"
31  #include <unistd.h>  #include "user_list.h"
32  #include <time.h>  #include "user_priv.h"
33  #include <string.h>  #include <ctype.h>
34    #include <errno.h>
35    #include <signal.h>
36  #include <stdlib.h>  #include <stdlib.h>
37    #include <string.h>
38    #include <time.h>
39    #include <unistd.h>
40    
41    static void child_proc_sig_usr1_handler(int i)
42    {
43            // Restart log
44            if (log_restart() < 0)
45            {
46                    log_error("Restart logging failed\n");
47            }
48    }
49    
50  int bbs_info()  int bbs_info()
51  {  {
52          prints("ӭ \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          prints("\r\n欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m(%s)\033[m\r\n",
53                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_INFO);
54    
55          return iflush();          return iflush();
56  }  }
57    
58  int bbs_welcome(MYSQL *db)  int bbs_welcome(void)
59  {  {
60          char sql[SQL_BUFFER_LEN];          int u_online = 0;
61            int u_anonymous = 0;
62          u_int32_t u_online = 0;          int u_total = 0;
63          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 error: %s\n", mysql_error(db));  
                 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);  
   
         snprintf(sql, sizeof(sql),  
                          "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));  
                 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_anonymous = (u_int32_t)atoi(row[0]);  
         }  
         mysql_free_result(rs);  
64    
65          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");          if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
         if (mysql_query(db, sql) != 0)  
66          {          {
67                  log_error("Query user_list error: %s\n", mysql_error(db));                  log_error("get_user_online_list_count() error\n");
68                  return -2;                  u_online = 0;
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_list data failed\n");  
                 return -2;  
69          }          }
70          if ((row = mysql_fetch_row(rs)))          u_online += u_anonymous;
71            u_online++; // current user
72            if (BBS_priv.uid == 0)
73          {          {
74                  u_total = (u_int32_t)atoi(row[0]);                  u_anonymous++;
75          }          }
         mysql_free_result(rs);  
76    
77          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)  
78          {          {
79                  log_error("Query user_login_log error: %s\n", mysql_error(db));                  log_error("get_user_list_count() error\n");
80                  return -2;                  u_total = 0;
81          }          }
         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]);  
         }  
         mysql_free_result(rs);  
82    
83          // 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)  
84          {          {
85                  fscanf(fin, "%d", &max_u_online);                  log_error("get_user_login_count() error\n");
86                  fclose(fin);                  u_login_count = 0;
         }  
         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);  
87          }          }
88    
         // Count current user before login  
         u_online++;  
         u_anonymous++;  
   
89          // Display logo          // Display logo
90          display_file_ex(DATA_WELCOME, 1, 0);          display_file(DATA_WELCOME, 2);
91    
92          // Display welcome message          // Display welcome message
93          prints("\r\033[1;35mӭ\033[33m %s \033[35mBBS\r\n"          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
94                     "\033[32mĿǰվ [\033[36m%d/%d\033[32m] "                     "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
95                     "ο[\033[36m%d\033[32m] "                     "匿名游客[\033[36m%d\033[32m] "
96                     "עû[\033[36m%d/%d\033[32m]\r\n"                     "注册用户数[\033[36m%d/%d\033[32m]\r\n"
97                     " [\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",  
98                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,                     BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
99                     BBS_max_user, BBS_start_dt, max_u_online, u_login_count);                     BBS_max_user_count, BBS_start_dt, u_login_count);
100    
101            iflush();
102    
103          return 0;          return 0;
104  }  }
105    
106  int bbs_logout(MYSQL *db)  int bbs_logout(void)
107  {  {
108          if (user_online_del(db) < 0)          MYSQL *db;
109    
110            db = db_open();
111            if (db == NULL)
112          {          {
113                  return -1;                  return -1;
114          }          }
115    
116          display_file_ex(DATA_GOODBYE, 1, 1);          if (user_online_exp(db) < 0)
117            {
118                    return -2;
119            }
120    
121            if (user_online_del(db) < 0)
122            {
123                    return -3;
124            }
125    
126            mysql_close(db);
127    
128          log_std("User logout\n");          display_file(DATA_GOODBYE, 1);
129    
130            log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
131    
132          return 0;          return 0;
133  }  }
# Line 187  int bbs_center() Line 137  int bbs_center()
137          int ch;          int ch;
138          time_t t_last_action;          time_t t_last_action;
139    
140          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(NULL);
141    
142          clearscr();          clearscr();
143    
144          show_top("");          show_top("", BBS_name, "");
145          show_active_board();          show_active_board();
146          show_bottom("");          show_bottom("");
147          display_menu(p_bbs_menu);          display_menu(&bbs_menu);
148          iflush();          iflush();
149    
150          while (!SYS_server_exit)          while (!SYS_server_exit)
151          {          {
152                  ch = igetch(100);                  ch = igetch(100);
153    
154                  if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
155                    {
156                            BBS_last_access_tm = time(NULL);
157    #ifdef _DEBUG
158                            log_error("Debug: BBS_last_access_tm is updated\n");
159    #endif
160                    }
161    
162                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
163                  {                  {
164                          t_last_action = time(0);                          t_last_action = time(NULL);
165    
166                          show_active_board();                          show_active_board();
167                          show_bottom("");                          show_bottom("");
168                            display_menu_cursor(&bbs_menu, 1);
169                          iflush();                          iflush();
170                  }                  }
171    
172                    if (user_online_update("MENU") < 0)
173                    {
174                            log_error("user_online_update(MENU) error\n");
175                    }
176    
177                  switch (ch)                  switch (ch)
178                  {                  {
179                  case KEY_NULL: // broken pipe                  case KEY_NULL: // broken pipe
180                            log_error("KEY_NULL\n");
181                          return 0;                          return 0;
182                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
183                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
184                          {                          {
185                                    log_error("User input timeout\n");
186                                  return 0;                                  return 0;
187                          }                          }
188                          continue;                          continue;
189                  case CR:                  case CR:
                         igetch_reset();  
190                  default:                  default:
191                          switch (menu_control(p_bbs_menu, ch))                          switch (menu_control(&bbs_menu, ch))
192                          {                          {
193                          case EXITBBS:                          case EXITBBS:
194                            case EXITMENU:
195                                  return 0;                                  return 0;
196                          case REDRAW:                          case REDRAW:
197                                  t_last_action = time(0);                                  t_last_action = time(NULL);
198                                  clearscr();                                  clearscr();
199                                  show_top("");                                  show_top("", BBS_name, "");
200                                  show_active_board();                                  show_active_board();
201                                  show_bottom("");                                  show_bottom("");
202                                  display_menu(p_bbs_menu);                                  display_menu(&bbs_menu);
203                                  break;                                  break;
204                          case NOREDRAW:                          case NOREDRAW:
205                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
# Line 242  int bbs_center() Line 208  int bbs_center()
208                          }                          }
209                          iflush();                          iflush();
210                  }                  }
211            }
212    
213                  BBS_last_access_tm = time(0);          return 0;
214    }
215    
216    int bbs_charset_select()
217    {
218            char msg[LINE_BUFFER_LEN];
219            int ch;
220    
221            snprintf(msg, sizeof(msg),
222                             "\rChoose character set in 5 seconds, (U)UTF-8, (G)GBK [U]: ");
223    
224            while (!SYS_server_exit)
225            {
226                    ch = press_any_key_ex(msg, 5);
227                    switch (toupper(ch))
228                    {
229                    case KEY_NULL:
230                            return -1;
231                    case KEY_TIMEOUT:
232                    case CR:
233                    case 'U':
234                            break;
235                    case 'G':
236                            if (io_conv_init("GBK") < 0)
237                            {
238                                    log_error("io_conv_init(%s) error\n", "GBK");
239                                    return -1;
240                            }
241                            break;
242                    default:
243                            continue;
244                    }
245    
246                    break;
247            }
248    
249            snprintf(msg, sizeof(msg),
250                             "\r请在5秒内选择宽字符显示规则, (V)变宽, (F)定宽? [V]: ");
251    
252            while (!SYS_server_exit)
253            {
254                    ch = press_any_key_ex(msg, 5);
255                    switch (toupper(ch))
256                    {
257                    case KEY_NULL:
258                            return -1;
259                    case KEY_TIMEOUT:
260                    case CR:
261                    case 'V':
262                            UTF8_fixed_width = 0;
263                            break;
264                    case 'F':
265                            UTF8_fixed_width = 1;
266                            break;
267                    default:
268                            continue;
269                    }
270    
271                    break;
272          }          }
273    
274          return 0;          return 0;
# Line 251  int bbs_center() Line 276  int bbs_center()
276    
277  int bbs_main()  int bbs_main()
278  {  {
279          MYSQL *db = NULL;          struct sigaction act = {0};
280            char msg[LINE_BUFFER_LEN];
281    
282            // Set signal handler
283            act.sa_handler = SIG_IGN;
284            if (sigaction(SIGHUP, &act, NULL) == -1)
285            {
286                    log_error("set signal action of SIGHUP error: %d\n", errno);
287                    goto cleanup;
288            }
289            act.sa_handler = SIG_DFL;
290            if (sigaction(SIGCHLD, &act, NULL) == -1)
291            {
292                    log_error("set signal action of SIGCHLD error: %d\n", errno);
293                    goto cleanup;
294            }
295            act.sa_handler = child_proc_sig_usr1_handler;
296            if (sigaction(SIGUSR1, &act, NULL) == -1)
297            {
298                    log_error("set signal action of SIGUSR1 error: %d\n", errno);
299                    goto cleanup;
300            }
301    
302          // Set data pools in shared memory readonly          // Set data pools in shared memory readonly
303          if (set_trie_dict_shm_readonly() < 0)          if (set_trie_dict_shm_readonly() < 0)
# Line 266  int bbs_main() Line 312  int bbs_main()
312          {          {
313                  goto cleanup;                  goto cleanup;
314          }          }
315    #ifdef HAVE_SYSTEM_V
316            if (set_user_list_pool_shm_readonly() < 0)
317            {
318                    goto cleanup;
319            }
320    #endif
321    
322          // Load menu in shared memory          // Load menu in shared memory
323          if (set_menu_shm_readonly(p_bbs_menu) < 0)          if (set_menu_shm_readonly(&bbs_menu) < 0)
324          {          {
325                  goto cleanup;                  goto cleanup;
326          }          }
327    
328            // Set default charset
329            if (io_conv_init(BBS_default_charset) < 0)
330            {
331                    log_error("io_conv_init(%s) error\n", BBS_default_charset);
332                    goto cleanup;
333            }
334    
335          set_input_echo(0);          set_input_echo(0);
336    
337          // System info          // Set user charset
338          if (bbs_info() < 0)          if (bbs_charset_select() < 0)
339          {          {
340                  goto cleanup;                  goto cleanup;
341          }          }
342    
343          db = db_open();          // System info
344          if (db == NULL)          if (bbs_info() < 0)
345          {          {
                 prints("޷ݿ\n");  
346                  goto cleanup;                  goto cleanup;
347          }          }
348    
349          // Welcome          // Welcome
350          if (bbs_welcome(db) < 0)          if (bbs_welcome() < 0)
351          {          {
352                  goto cleanup;                  goto cleanup;
353          }          }
354    
355          // User login          // User login
356          if (bbs_login(db) < 0)          if (SSH_v2)
357            {
358                    snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
359                    press_any_key_ex(msg, 60);
360            }
361            else if (bbs_login() < 0)
362            {
363                    goto cleanup;
364            }
365            log_common("User [%s] login\n", BBS_username);
366    
367            // Load section aid locations
368            if (section_aid_locations_load(BBS_priv.uid) < 0)
369            {
370                    log_error("article_view_log_load() error\n");
371                    goto cleanup;
372            }
373    
374            // Load article view log
375            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
376            {
377                    log_error("article_view_log_load() error\n");
378                    goto cleanup;
379            }
380    
381            // Load article favorite
382            if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
383            {
384                    log_error("article_favor_load() error\n");
385                    goto cleanup;
386            }
387    
388            // Init editor memory pool
389            if (editor_memory_pool_init() < 0)
390          {          {
391                    log_error("editor_memory_pool_init() error\n");
392                  goto cleanup;                  goto cleanup;
393          }          }
394    
395          clearscr();          clearscr();
396    
397          // BBS Top 10          // BBS Top 10
398          display_file_ex(VAR_BBS_TOP, 1, 1);          display_file(VAR_BBS_TOP, 1);
399    
400          // Main          // Main
401          bbs_center();          bbs_center();
402    
403          // Logout          // Logout
404          bbs_logout(db);          bbs_logout();
405    
406  cleanup:          // Save section aid locations
407          if (db != NULL)          if (section_aid_locations_save(BBS_priv.uid) < 0)
408            {
409                    log_error("article_view_log_save() error\n");
410            }
411    
412            // Save incremental article view log
413            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
414          {          {
415                  mysql_close(db);                  log_error("article_view_log_save_inc() error\n");
416          }          }
417    
418            // Save incremental article favorite
419            if (article_favor_save_inc(&BBS_article_favor) < 0)
420            {
421                    log_error("article_favor_save_inc() error\n");
422            }
423    
424    cleanup:
425            // Cleanup iconv
426            io_conv_cleanup();
427    
428            // Cleanup editor memory pool
429            editor_memory_pool_cleanup();
430    
431            // Unload article view log
432            article_view_log_unload(&BBS_article_view_log);
433    
434            // Unload article favor
435            article_favor_unload(&BBS_article_favor);
436    
437          // Detach menu in shared memory          // Detach menu in shared memory
438          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
439          free(p_bbs_menu);          detach_menu_shm(&top10_menu);
         p_bbs_menu = NULL;  
440    
441          // Detach data pools shm          // Detach data pools shm
442            detach_user_list_pool_shm();
443          detach_section_list_shm();          detach_section_list_shm();
444          detach_article_block_shm();          detach_article_block_shm();
445          detach_trie_dict_shm();          detach_trie_dict_shm();
446    
447            // Cleanup BWF
448            bwf_unload();
449    
450          return 0;          return 0;
451  }  }


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

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