/[LeafOK_CVS]/lbbs/src/bbs_main.c
ViewVC logotype

Contents of /lbbs/src/bbs_main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.80 - (show annotations)
Thu Oct 16 05:24:52 2025 UTC (5 months ago) by sysadm
Branch: MAIN
Changes since 1.79: +6 -1 lines
Content type: text/x-csrc
Fix bug

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

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