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


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

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