/[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.119 - (show annotations)
Wed Dec 17 03:47:00 2025 UTC (2 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.118: +2 -0 lines
Content type: text/x-csrc
Refine debug log

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * bbs_main
4 * - entry and major procedures of user interactive access
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "article_favor.h"
14 #include "article_view_log.h"
15 #include "bbs.h"
16 #include "bbs_cmd.h"
17 #include "bbs_main.h"
18 #include "bwf.h"
19 #include "common.h"
20 #include "database.h"
21 #include "editor.h"
22 #include "io.h"
23 #include "log.h"
24 #include "login.h"
25 #include "menu.h"
26 #include "screen.h"
27 #include "section_list.h"
28 #include "section_list_display.h"
29 #include "str_process.h"
30 #include "trie_dict.h"
31 #include "user_list.h"
32 #include "user_priv.h"
33 #include <ctype.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
40
41 static void child_proc_sig_usr1_handler(int i)
42 {
43 // Restart log
44 if (log_restart() < 0)
45 {
46 log_error("Restart logging failed\n");
47 }
48 }
49
50 int bbs_info()
51 {
52 prints("\r\n欢迎光临 \033[1;33m%s \033[32m[%s] \033[37m(%s)\033[m\r\n",
53 BBS_name, BBS_server, APP_INFO);
54
55 return iflush();
56 }
57
58 int bbs_welcome(void)
59 {
60 int u_online = 0;
61 int u_anonymous = 0;
62 int u_total = 0;
63 int u_login_count = 0;
64
65 if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
66 {
67 log_error("get_user_online_list_count() error\n");
68 u_online = 0;
69 }
70 u_online += u_anonymous;
71 u_online++; // current user
72 if (BBS_priv.uid == 0)
73 {
74 u_anonymous++;
75 }
76
77 if (get_user_list_count(&u_total) < 0)
78 {
79 log_error("get_user_list_count() error\n");
80 u_total = 0;
81 }
82
83 if (get_user_login_count(&u_login_count) < 0)
84 {
85 log_error("get_user_login_count() error\n");
86 u_login_count = 0;
87 }
88
89 // Display logo
90 display_file(DATA_WELCOME, 2);
91
92 // Display welcome message
93 prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
94 "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
95 "匿名游客[\033[36m%d\033[32m] "
96 "注册用户数[\033[36m%d/%d\033[32m]\r\n"
97 "从 [\033[36m%s\033[32m] 起,累计访问人次: [\033[36m%d\033[32m]\033[m\r\n",
98 BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
99 BBS_max_user_count, BBS_start_dt, u_login_count);
100
101 iflush();
102
103 return 0;
104 }
105
106 int bbs_logout(void)
107 {
108 MYSQL *db;
109
110 db = db_open();
111 if (db == NULL)
112 {
113 return -1;
114 }
115
116 if (user_online_exp(db) < 0)
117 {
118 return -2;
119 }
120
121 mysql_close(db);
122
123 display_file(DATA_GOODBYE, 1);
124
125 log_common("User [%s] (uid=%d) logout, idle for %ld seconds since last input\n",
126 BBS_username, BBS_priv.uid, time(NULL) - BBS_last_access_tm);
127
128 return 0;
129 }
130
131 int bbs_session_cleanup(void)
132 {
133 MYSQL *db;
134
135 db = db_open();
136 if (db == NULL)
137 {
138 return -1;
139 }
140
141 if (user_online_del(db) < 0)
142 {
143 return -2;
144 }
145
146 mysql_close(db);
147
148 return 0;
149 }
150
151 int bbs_center()
152 {
153 int ch;
154 int loop;
155 time_t t_last_action;
156
157 BBS_last_access_tm = t_last_action = time(NULL);
158
159 clearscr();
160
161 show_top("", BBS_name, "");
162 show_active_board();
163 show_bottom("");
164 display_menu(&bbs_menu);
165 iflush();
166
167 for (loop = 1; !SYS_server_exit && loop;)
168 {
169 ch = igetch(100);
170
171 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
172 {
173 BBS_last_access_tm = time(NULL);
174 #ifdef _DEBUG
175 log_error("Debug: BBS_last_access_tm is updated\n");
176 #endif
177 }
178
179 if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
180 {
181 t_last_action = time(NULL);
182
183 show_active_board();
184 show_bottom("");
185 display_menu_cursor(&bbs_menu, 1);
186 iflush();
187 }
188
189 if (user_online_update("MENU") < 0)
190 {
191 log_error("user_online_update(MENU) error\n");
192 }
193
194 switch (ch)
195 {
196 case KEY_NULL: // broken pipe
197 #ifdef _DEBUG
198 log_error("KEY_NULL\n");
199 #endif
200 loop = 0;
201 break;
202 case KEY_TIMEOUT:
203 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
204 {
205 log_error("User input timeout\n");
206 loop = 0;
207 break;
208 }
209 continue;
210 case CR:
211 default:
212 switch (menu_control(&bbs_menu, ch))
213 {
214 case EXITBBS:
215 case EXITMENU:
216 loop = 0;
217 break;
218 case REDRAW:
219 t_last_action = time(NULL);
220 clearscr();
221 show_top("", BBS_name, "");
222 show_active_board();
223 show_bottom("");
224 display_menu(&bbs_menu);
225 break;
226 case NOREDRAW:
227 case UNKNOWN_CMD:
228 default:
229 break;
230 }
231 iflush();
232 }
233 }
234
235 return 0;
236 }
237
238 int bbs_charset_select()
239 {
240 char msg[LINE_BUFFER_LEN];
241 int ch;
242
243 snprintf(msg, sizeof(msg),
244 "\rChoose character set in 5 seconds, (U)UTF-8, (G)GBK [U]: ");
245
246 while (!SYS_server_exit)
247 {
248 ch = press_any_key_ex(msg, 5);
249 switch (toupper(ch))
250 {
251 case KEY_NULL:
252 return -1;
253 case KEY_TIMEOUT:
254 case CR:
255 case 'U':
256 break;
257 case 'G':
258 if (io_conv_init("GBK") < 0)
259 {
260 log_error("io_conv_init(%s) error\n", "GBK");
261 return -1;
262 }
263 break;
264 default:
265 continue;
266 }
267
268 break;
269 }
270
271 snprintf(msg, sizeof(msg),
272 "\r请在5秒内选择宽字符显示规则, (V)变宽, (F)定宽? [V]: ");
273
274 while (!SYS_server_exit)
275 {
276 ch = press_any_key_ex(msg, 5);
277 switch (toupper(ch))
278 {
279 case KEY_NULL:
280 return -1;
281 case KEY_TIMEOUT:
282 case CR:
283 case 'V':
284 UTF8_fixed_width = 0;
285 break;
286 case 'F':
287 UTF8_fixed_width = 1;
288 break;
289 default:
290 continue;
291 }
292
293 break;
294 }
295
296 return 0;
297 }
298
299 int bbs_main()
300 {
301 struct sigaction act = {0};
302 char msg[LINE_BUFFER_LEN];
303
304 // Set signal handler
305 act.sa_handler = SIG_IGN;
306 if (sigaction(SIGHUP, &act, NULL) == -1)
307 {
308 log_error("set signal action of SIGHUP error: %d\n", errno);
309 goto cleanup;
310 }
311 act.sa_handler = SIG_DFL;
312 if (sigaction(SIGCHLD, &act, NULL) == -1)
313 {
314 log_error("set signal action of SIGCHLD error: %d\n", errno);
315 goto cleanup;
316 }
317 act.sa_handler = child_proc_sig_usr1_handler;
318 if (sigaction(SIGUSR1, &act, NULL) == -1)
319 {
320 log_error("set signal action of SIGUSR1 error: %d\n", errno);
321 goto cleanup;
322 }
323
324 // Set data pools in shared memory readonly
325 if (set_trie_dict_shm_readonly() < 0)
326 {
327 goto cleanup;
328 }
329 if (set_article_block_shm_readonly() < 0)
330 {
331 goto cleanup;
332 }
333 #ifdef HAVE_SYSTEM_V
334 if (set_section_list_shm_readonly() < 0)
335 {
336 goto cleanup;
337 }
338 if (set_user_list_pool_shm_readonly() < 0)
339 {
340 goto cleanup;
341 }
342 #endif
343
344 // Load menu in shared memory
345 if (set_menu_shm_readonly(&bbs_menu) < 0)
346 {
347 goto cleanup;
348 }
349
350 // Set default charset
351 if (io_conv_init(BBS_default_charset) < 0)
352 {
353 log_error("io_conv_init(%s) error\n", BBS_default_charset);
354 goto cleanup;
355 }
356
357 set_input_echo(0);
358
359 // Set user charset
360 if (bbs_charset_select() < 0)
361 {
362 goto cleanup;
363 }
364
365 // System info
366 if (bbs_info() < 0)
367 {
368 goto cleanup;
369 }
370
371 // Welcome
372 if (bbs_welcome() < 0)
373 {
374 goto cleanup;
375 }
376
377 // User login
378 if (SSH_v2)
379 {
380 snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
381 press_any_key_ex(msg, 60);
382 }
383 else if (bbs_login() < 0)
384 {
385 goto cleanup;
386 }
387 log_common("User [%s] (uid=%d) login from %s:%d\n",
388 BBS_username, BBS_priv.uid, hostaddr_client, port_client);
389
390 // Check EULA update status
391 if (BBS_update_eula)
392 {
393 user_update_agreement();
394 goto cleanup;
395 }
396
397 // Load section aid locations
398 if (section_aid_locations_load(BBS_priv.uid) < 0)
399 {
400 log_error("article_view_log_load() error\n");
401 goto cleanup;
402 }
403
404 // Load article view log
405 if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
406 {
407 log_error("article_view_log_load() error\n");
408 goto cleanup;
409 }
410
411 // Load article favorite
412 if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
413 {
414 log_error("article_favor_load() error\n");
415 goto cleanup;
416 }
417
418 // Init editor memory pool
419 if (editor_memory_pool_init() < 0)
420 {
421 log_error("editor_memory_pool_init() error\n");
422 goto cleanup;
423 }
424
425 clearscr();
426
427 // BBS Top 10
428 display_file(VAR_BBS_TOP, 1);
429
430 // Main
431 bbs_center();
432
433 // Logout
434 bbs_logout();
435
436 // Save section aid locations
437 if (section_aid_locations_save(BBS_priv.uid) < 0)
438 {
439 log_error("article_view_log_save() error\n");
440 }
441
442 // Save incremental article view log
443 if (article_view_log_save_inc(&BBS_article_view_log) < 0)
444 {
445 log_error("article_view_log_save_inc() error\n");
446 }
447
448 // Save incremental article favorite
449 if (article_favor_save_inc(&BBS_article_favor) < 0)
450 {
451 log_error("article_favor_save_inc() error\n");
452 }
453
454 cleanup:
455 // Cleanup session
456 bbs_session_cleanup();
457
458 // Cleanup iconv
459 io_conv_cleanup();
460
461 // Cleanup editor memory pool
462 editor_memory_pool_cleanup();
463
464 // Unload article view log
465 article_view_log_unload(&BBS_article_view_log);
466
467 // Unload article favor
468 article_favor_unload(&BBS_article_favor);
469
470 // Detach menu in shared memory
471 detach_menu_shm(&bbs_menu);
472 detach_menu_shm(&top10_menu);
473
474 // Detach data pools shm
475 detach_user_list_pool_shm();
476 detach_section_list_shm();
477 detach_article_block_shm();
478 detach_trie_dict_shm();
479
480 // Cleanup BWF
481 bwf_cleanup();
482
483 return 0;
484 }

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