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


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

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