/[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.37 by sysadm, Fri May 9 11:21:11 2025 UTC Revision 1.68 by sysadm, Sun Jun 8 09:25:24 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"
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 <unistd.h>  #include <unistd.h>
32  #include <time.h>  #include <time.h>
33  #include <string.h>  #include <string.h>
34    #include <stdlib.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 )\r\n",
39                     BBS_name, BBS_server, app_version);                     BBS_name, BBS_server, APP_NAME_VER);
40    
41            return iflush();
42    }
43    
44    int bbs_welcome(void)
45    {
46            char sql[SQL_BUFFER_LEN];
47    
48            u_int32_t u_online = 0;
49            u_int32_t u_anonymous = 0;
50            u_int32_t u_total = 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            // Count current user before login
150            u_online++;
151            u_anonymous++;
152    
153            // Display logo
154            display_file(DATA_WELCOME, 2);
155    
156            // Display welcome message
157            prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
158                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
159                       "匿名游客[\033[36m%d\033[32m] "
160                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
161                       "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
162                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
163                       BBS_max_user, BBS_start_dt, u_login_count);
164    
165          iflush();          iflush();
166    
167          return 0;          return 0;
168  }  }
169    
170  int bbs_exit()  int bbs_logout(void)
171  {  {
172          display_file_ex(DATA_GOODBYE, 1, 0);          MYSQL *db;
173    
174            db = db_open();
175            if (db == NULL)
176            {
177                    return -1;
178            }
179    
180            if (user_online_del(db) < 0)
181            {
182                    return -2;
183            }
184    
185            mysql_close(db);
186    
187          sleep(1);          display_file(DATA_GOODBYE, 1);
188    
189            log_common("User logout\n");
190    
191          return 0;          return 0;
192  }  }
193    
194  int bbs_center()  int bbs_center()
195  {  {
196          int ch, redraw;          int ch;
197          time_t t_last_action;          time_t t_last_action;
198    
199          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(0);
200    
201          clearscr();          clearscr();
202    
203          show_top("");          show_top("", BBS_name, "");
204          show_active_board();          show_active_board();
205          show_bottom("");          show_bottom("");
206          display_menu(get_menu(&bbs_menu, "TOPMENU"));          display_menu(p_bbs_menu);
207            iflush();
208    
209          while (!SYS_server_exit)          while (!SYS_server_exit)
210          {          {
211                  ch = igetch(0);                  ch = igetch(100);
212    
213                  if (time(0) - t_last_action >= 10)                  if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)
214                  {                  {
215                          t_last_action = time(0);                          t_last_action = time(0);
216    
217                          show_active_board();                          show_active_board();
218                          show_bottom("");                          show_bottom("");
219                            display_menu_cursor(p_bbs_menu, 1);
220                            iflush();
221                  }                  }
222    
223                  switch (ch)                  switch (ch)
224                  {                  {
225                  case KEY_NULL:                  case KEY_NULL: // broken pipe
226                          return 0;                          return 0;
227                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
228                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
229                          {                          {
230                                  return -1;                                  return 0;
231                          }                          }
232                          continue;                          continue;
233                    case CR:
234                            igetch_reset();
235                  default:                  default:
236                          redraw = 1;                          switch (menu_control(p_bbs_menu, ch))
                         switch (menu_control(&bbs_menu, ch))  
237                          {                          {
238                          case EXITBBS:                          case EXITBBS:
239                                  return 0;                                  return 0;
240                          case REDRAW:                          case REDRAW:
241                                    t_last_action = time(0);
242                                    clearscr();
243                                    show_top("", BBS_name, "");
244                                    show_active_board();
245                                    show_bottom("");
246                                    display_menu(p_bbs_menu);
247                                  break;                                  break;
248                          case NOREDRAW:                          case NOREDRAW:
249                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
250                          default:                          default:
                                 redraw = 0;  
251                                  break;                                  break;
252                          }                          }
253                          if (redraw)                          iflush();
                         {  
                                 clearscr();  
                                 show_top("");  
                                 show_active_board();  
                                 show_bottom("");  
                                 display_current_menu(&bbs_menu);  
                         }  
254                  }                  }
255    
256                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);
257          }          }
258    
# Line 115  int bbs_center() Line 261  int bbs_center()
261    
262  int bbs_main()  int bbs_main()
263  {  {
264          int ret;          // Set data pools in shared memory readonly
265            if (set_trie_dict_shm_readonly() < 0)
266            {
267                    goto cleanup;
268            }
269            if (set_article_block_shm_readonly() < 0)
270            {
271                    goto cleanup;
272            }
273            if (set_section_list_shm_readonly() < 0)
274            {
275                    goto cleanup;
276            }
277    
278            // Load menu in shared memory
279            if (set_menu_shm_readonly(p_bbs_menu) < 0)
280            {
281                    goto cleanup;
282            }
283    
284          set_input_echo(0);          set_input_echo(0);
285    
286          bbs_info();          // System info
287            if (bbs_info() < 0)
288            {
289                    goto cleanup;
290            }
291    
292          // Welcome          // Welcome
293          bbs_welcome();          if (bbs_welcome() < 0)
294            {
295                    goto cleanup;
296            }
297    
298            // User login
299            if (SSH_v2)
300            {
301                    prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
302                    iflush();
303                    igetch_t(MAX_DELAY_TIME);
304            }
305            else if (bbs_login() < 0)
306            {
307                    goto cleanup;
308            }
309    
310            // Load article view log
311            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
312            {
313                    log_error("article_view_log_load() error\n");
314                    goto cleanup;
315            }
316    
         // 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);  
317          clearscr();          clearscr();
318    
319          // BBS Top 10          // BBS Top 10
320          display_file_ex("./var/bbs_top.txt", 1, 1);          display_file(VAR_BBS_TOP, 1);
321    
322          // Main          // Main
323          bbs_center();          bbs_center();
324    
325          // Logout          // Logout
326          bbs_exit();          bbs_logout();
         log_std("User logout\n");  
327    
328          MYSQL *db = db_open();          // Save incremental article view log
329          if (db == NULL)          if (article_view_log_save_inc(&BBS_article_view_log) < 0)
330          {          {
331                  return -1;                  log_error("article_view_log_save_inc() error\n");
332          }          }
333    
334          user_online_del(db);  cleanup:
335            // Unload article view log
336          mysql_close(db);          article_view_log_unload(&BBS_article_view_log);
337    
338            // Detach menu in shared memory
339            detach_menu_shm(p_bbs_menu);
340            free(p_bbs_menu);
341            p_bbs_menu = NULL;
342    
343            // Detach data pools shm
344            detach_section_list_shm();
345            detach_article_block_shm();
346            detach_trie_dict_shm();
347    
348          return 0;          return 0;
349  }  }


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

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