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


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

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