/[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.35 by sysadm, Sun May 4 14:54:55 2025 UTC Revision 1.69 by sysadm, Thu Jun 12 10:34:21 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    bbs_main.c  -  description                                                    bbs_main.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "bbs_main.h"  #include "bbs_main.h"
18  #include "bbs.h"  #include "bbs.h"
 #include "welcome.h"  
19  #include "login.h"  #include "login.h"
20  #include "user_priv.h"  #include "user_priv.h"
21  #include "common.h"  #include "common.h"
22  #include "database.h"  #include "database.h"
23    #include "article_view_log.h"
24  #include "log.h"  #include "log.h"
25  #include "io.h"  #include "io.h"
26  #include "screen.h"  #include "screen.h"
27  #include "menu.h"  #include "menu.h"
28  #include "bbs_cmd.h"  #include "bbs_cmd.h"
29    #include "section_list.h"
30    #include "trie_dict.h"
31    #include "editor.h"
32  #include <unistd.h>  #include <unistd.h>
33  #include <time.h>  #include <time.h>
34  #include <string.h>  #include <string.h>
35    #include <stdlib.h>
36    
37  int bbs_info()  int bbs_info()
38  {  {
39          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",
40                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_NAME_VER);
41    
42            return iflush();
43    }
44    
45    int bbs_welcome(void)
46    {
47            char sql[SQL_BUFFER_LEN];
48    
49            u_int32_t u_online = 0;
50            u_int32_t u_anonymous = 0;
51            u_int32_t u_total = 0;
52            u_int32_t u_login_count = 0;
53    
54            MYSQL *db;
55            MYSQL_RES *rs;
56            MYSQL_ROW row;
57    
58            db = db_open();
59            if (db == NULL)
60            {
61                    return -1;
62            }
63    
64            snprintf(sql, sizeof(sql),
65                             "SELECT COUNT(*) AS cc FROM "
66                             "(SELECT DISTINCT SID FROM user_online "
67                             "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
68                             BBS_user_off_line);
69            if (mysql_query(db, sql) != 0)
70            {
71                    log_error("Query user_online error: %s\n", mysql_error(db));
72                    mysql_close(db);
73                    return -2;
74            }
75            if ((rs = mysql_store_result(db)) == NULL)
76            {
77                    log_error("Get user_online data failed\n");
78                    mysql_close(db);
79                    return -2;
80            }
81            if ((row = mysql_fetch_row(rs)))
82            {
83                    u_online = (u_int32_t)atoi(row[0]);
84            }
85            mysql_free_result(rs);
86    
87            snprintf(sql, sizeof(sql),
88                             "SELECT COUNT(*) AS cc FROM "
89                             "(SELECT DISTINCT SID FROM user_online "
90                             "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
91                             BBS_user_off_line);
92            if (mysql_query(db, sql) != 0)
93            {
94                    log_error("Query user_online error: %s\n", mysql_error(db));
95                    mysql_close(db);
96                    return -2;
97            }
98            if ((rs = mysql_store_result(db)) == NULL)
99            {
100                    log_error("Get user_online data failed\n");
101                    mysql_close(db);
102                    return -2;
103            }
104            if ((row = mysql_fetch_row(rs)))
105            {
106                    u_anonymous = (u_int32_t)atoi(row[0]);
107            }
108            mysql_free_result(rs);
109    
110            snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
111            if (mysql_query(db, sql) != 0)
112            {
113                    log_error("Query user_list error: %s\n", mysql_error(db));
114                    mysql_close(db);
115                    return -2;
116            }
117            if ((rs = mysql_store_result(db)) == NULL)
118            {
119                    log_error("Get user_list data failed\n");
120                    mysql_close(db);
121                    return -2;
122            }
123            if ((row = mysql_fetch_row(rs)))
124            {
125                    u_total = (u_int32_t)atoi(row[0]);
126            }
127            mysql_free_result(rs);
128    
129            snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");
130            if (mysql_query(db, sql) != 0)
131            {
132                    log_error("Query user_login_log error: %s\n", mysql_error(db));
133                    mysql_close(db);
134                    return -2;
135            }
136            if ((rs = mysql_store_result(db)) == NULL)
137            {
138                    log_error("Get user_login_log data failed\n");
139                    mysql_close(db);
140                    return -2;
141            }
142            if ((row = mysql_fetch_row(rs)))
143            {
144                    u_login_count = (u_int32_t)atoi(row[0]);
145            }
146            mysql_free_result(rs);
147    
148            mysql_close(db);
149    
150            // Count current user before login
151            u_online++;
152            u_anonymous++;
153    
154            // Display logo
155            display_file(DATA_WELCOME, 2);
156    
157            // Display welcome message
158            prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
159                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
160                       "匿名游客[\033[36m%d\033[32m] "
161                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
162                       "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
163                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
164                       BBS_max_user, BBS_start_dt, u_login_count);
165    
166          iflush();          iflush();
167    
168          return 0;          return 0;
169  }  }
170    
171  int bbs_exit()  int bbs_logout(void)
172  {  {
173          display_file_ex(DATA_GOODBYE, 1, 0);          MYSQL *db;
174    
175          sleep(1);          db = db_open();
176            if (db == NULL)
177            {
178                    return -1;
179            }
180    
181            if (user_online_del(db) < 0)
182            {
183                    return -2;
184            }
185    
186            mysql_close(db);
187    
188            display_file(DATA_GOODBYE, 1);
189    
190            log_common("User logout\n");
191    
192          return 0;          return 0;
193  }  }
194    
195  int bbs_center()  int bbs_center()
196  {  {
197          int ch, redraw;          int ch;
198          time_t t_last_action;          time_t t_last_action;
199    
200          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(0);
201    
202          clearscr();          clearscr();
203    
204          show_top("");          show_top("", BBS_name, "");
205          show_active_board();          show_active_board();
206          show_bottom("");          show_bottom("");
207          display_menu(get_menu(&bbs_menu, "TOPMENU"));          display_menu(p_bbs_menu);
208            iflush();
209    
210          while (!SYS_exit)          while (!SYS_server_exit)
211          {          {
212                  ch = igetch(0);                  ch = igetch(100);
213    
214                  if (time(0) - t_last_action >= 10)                  if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)
215                  {                  {
216                          t_last_action = time(0);                          t_last_action = time(0);
217    
218                          show_active_board();                          show_active_board();
219                          show_bottom("");                          show_bottom("");
220                            display_menu_cursor(p_bbs_menu, 1);
221                            iflush();
222                  }                  }
223    
224                  switch (ch)                  switch (ch)
225                  {                  {
226                  case KEY_NULL:                  case KEY_NULL: // broken pipe
227                          return 0;                          return 0;
228                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
229                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
230                          {                          {
231                                  return -1;                                  return 0;
232                          }                          }
233                          continue;                          continue;
234                    case CR:
235                            igetch_reset();
236                  default:                  default:
237                          redraw = 1;                          switch (menu_control(p_bbs_menu, ch))
                         switch (menu_control(&bbs_menu, ch))  
238                          {                          {
239                          case EXITBBS:                          case EXITBBS:
240                                  return 0;                                  return 0;
241                          case REDRAW:                          case REDRAW:
242                                    t_last_action = time(0);
243                                    clearscr();
244                                    show_top("", BBS_name, "");
245                                    show_active_board();
246                                    show_bottom("");
247                                    display_menu(p_bbs_menu);
248                                  break;                                  break;
249                          case NOREDRAW:                          case NOREDRAW:
250                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
251                          default:                          default:
                                 redraw = 0;  
252                                  break;                                  break;
253                          }                          }
254                          if (redraw)                          iflush();
                         {  
                                 clearscr();  
                                 show_top("");  
                                 show_active_board();  
                                 show_bottom("");  
                                 display_current_menu(&bbs_menu);  
                         }  
255                  }                  }
256    
257                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);
258          }          }
259    
# Line 116  int bbs_center() Line 262  int bbs_center()
262    
263  int bbs_main()  int bbs_main()
264  {  {
265          int ret;          // Set data pools in shared memory readonly
266            if (set_trie_dict_shm_readonly() < 0)
267            {
268                    goto cleanup;
269            }
270            if (set_article_block_shm_readonly() < 0)
271            {
272                    goto cleanup;
273            }
274            if (set_section_list_shm_readonly() < 0)
275            {
276                    goto cleanup;
277            }
278    
279            // Load menu in shared memory
280            if (set_menu_shm_readonly(p_bbs_menu) < 0)
281            {
282                    goto cleanup;
283            }
284    
285          set_input_echo(0);          set_input_echo(0);
286    
287          bbs_info();          // System info
288            if (bbs_info() < 0)
289            {
290                    goto cleanup;
291            }
292    
293          // Welcome          // Welcome
294          bbs_welcome();          if (bbs_welcome() < 0)
295            {
296                    goto cleanup;
297            }
298    
299            // User login
300            if (SSH_v2)
301            {
302                    prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
303                    iflush();
304                    igetch_t(MAX_DELAY_TIME);
305            }
306            else if (bbs_login() < 0)
307            {
308                    goto cleanup;
309            }
310    
311            // Load article view log
312            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
313            {
314                    log_error("article_view_log_load() error\n");
315                    goto cleanup;
316            }
317    
318            // Init editor memory pool
319            if (editor_memory_pool_init() < 0)
320            {
321                    log_error("editor_memory_pool_init() error\n");
322                    goto cleanup;
323            }
324    
         // Login  
         ret = bbs_login();  
         if (ret < 0)  
                 return -1;  
         log_std("User \"%s\"(%ld) login from %s:%d\n",  
                         BBS_username, BBS_priv.uid, hostaddr_client, port_client);  
325          clearscr();          clearscr();
326    
327          // BBS Top 10          // BBS Top 10
328          display_file_ex("./var/bbs_top.txt", 1, 1);          display_file(VAR_BBS_TOP, 1);
329    
330          // Main          // Main
331          bbs_center();          bbs_center();
332    
333          // Logout          // Logout
334          bbs_exit();          bbs_logout();
         log_std("User logout\n");  
335    
336          MYSQL *db = db_open();          // Save incremental article view log
337          if (db == NULL)          if (article_view_log_save_inc(&BBS_article_view_log) < 0)
338          {          {
339                  return -1;                  log_error("article_view_log_save_inc() error\n");
340          }          }
341    
342          user_online_del(db);  cleanup:
343            // Cleanup editor memory pool
344          mysql_close(db);          editor_memory_pool_cleanup();
345            
346            // Unload article view log
347            article_view_log_unload(&BBS_article_view_log);
348    
349            // Detach menu in shared memory
350            detach_menu_shm(p_bbs_menu);
351            free(p_bbs_menu);
352            p_bbs_menu = NULL;
353    
354            // Detach data pools shm
355            detach_section_list_shm();
356            detach_article_block_shm();
357            detach_trie_dict_shm();
358    
359          return 0;          return 0;
360  }  }


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

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