/[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.115 - (show annotations)
Fri Nov 28 05:05:42 2025 UTC (3 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.114: +3 -3 lines
Content type: text/x-csrc
Force user logout cleanup even if connection is closed before bbs_main() is called.

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 if (user_online_del(db) < 0)
122 {
123 return -3;
124 }
125
126 mysql_close(db);
127
128 display_file(DATA_GOODBYE, 1);
129
130 log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
131
132 return 0;
133 }
134
135 int bbs_center()
136 {
137 int ch;
138 int loop;
139 time_t t_last_action;
140
141 BBS_last_access_tm = t_last_action = time(NULL);
142
143 clearscr();
144
145 show_top("", BBS_name, "");
146 show_active_board();
147 show_bottom("");
148 display_menu(&bbs_menu);
149 iflush();
150
151 for (loop = 1; !SYS_server_exit && loop;)
152 {
153 ch = igetch(100);
154
155 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
156 {
157 BBS_last_access_tm = time(NULL);
158 #ifdef _DEBUG
159 log_error("Debug: BBS_last_access_tm is updated\n");
160 #endif
161 }
162
163 if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
164 {
165 t_last_action = time(NULL);
166
167 show_active_board();
168 show_bottom("");
169 display_menu_cursor(&bbs_menu, 1);
170 iflush();
171 }
172
173 if (user_online_update("MENU") < 0)
174 {
175 log_error("user_online_update(MENU) error\n");
176 }
177
178 switch (ch)
179 {
180 case KEY_NULL: // broken pipe
181 log_error("KEY_NULL\n");
182 loop = 0;
183 break;
184 case KEY_TIMEOUT:
185 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
186 {
187 log_error("User input timeout\n");
188 loop = 0;
189 break;
190 }
191 continue;
192 case CR:
193 default:
194 switch (menu_control(&bbs_menu, ch))
195 {
196 case EXITBBS:
197 case EXITMENU:
198 loop = 0;
199 break;
200 case REDRAW:
201 t_last_action = time(NULL);
202 clearscr();
203 show_top("", BBS_name, "");
204 show_active_board();
205 show_bottom("");
206 display_menu(&bbs_menu);
207 break;
208 case NOREDRAW:
209 case UNKNOWN_CMD:
210 default:
211 break;
212 }
213 iflush();
214 }
215 }
216
217 return 0;
218 }
219
220 int bbs_charset_select()
221 {
222 char msg[LINE_BUFFER_LEN];
223 int ch;
224
225 snprintf(msg, sizeof(msg),
226 "\rChoose character set in 5 seconds, (U)UTF-8, (G)GBK [U]: ");
227
228 while (!SYS_server_exit)
229 {
230 ch = press_any_key_ex(msg, 5);
231 switch (toupper(ch))
232 {
233 case KEY_NULL:
234 return -1;
235 case KEY_TIMEOUT:
236 case CR:
237 case 'U':
238 break;
239 case 'G':
240 if (io_conv_init("GBK") < 0)
241 {
242 log_error("io_conv_init(%s) error\n", "GBK");
243 return -1;
244 }
245 break;
246 default:
247 continue;
248 }
249
250 break;
251 }
252
253 snprintf(msg, sizeof(msg),
254 "\r请在5秒内选择宽字符显示规则, (V)变宽, (F)定宽? [V]: ");
255
256 while (!SYS_server_exit)
257 {
258 ch = press_any_key_ex(msg, 5);
259 switch (toupper(ch))
260 {
261 case KEY_NULL:
262 return -1;
263 case KEY_TIMEOUT:
264 case CR:
265 case 'V':
266 UTF8_fixed_width = 0;
267 break;
268 case 'F':
269 UTF8_fixed_width = 1;
270 break;
271 default:
272 continue;
273 }
274
275 break;
276 }
277
278 return 0;
279 }
280
281 int bbs_main()
282 {
283 struct sigaction act = {0};
284 char msg[LINE_BUFFER_LEN];
285
286 // Set signal handler
287 act.sa_handler = SIG_IGN;
288 if (sigaction(SIGHUP, &act, NULL) == -1)
289 {
290 log_error("set signal action of SIGHUP error: %d\n", errno);
291 goto cleanup;
292 }
293 act.sa_handler = SIG_DFL;
294 if (sigaction(SIGCHLD, &act, NULL) == -1)
295 {
296 log_error("set signal action of SIGCHLD error: %d\n", errno);
297 goto cleanup;
298 }
299 act.sa_handler = child_proc_sig_usr1_handler;
300 if (sigaction(SIGUSR1, &act, NULL) == -1)
301 {
302 log_error("set signal action of SIGUSR1 error: %d\n", errno);
303 goto cleanup;
304 }
305
306 // Set data pools in shared memory readonly
307 if (set_trie_dict_shm_readonly() < 0)
308 {
309 goto cleanup;
310 }
311 if (set_article_block_shm_readonly() < 0)
312 {
313 goto cleanup;
314 }
315 #ifdef HAVE_SYSTEM_V
316 if (set_section_list_shm_readonly() < 0)
317 {
318 goto cleanup;
319 }
320 if (set_user_list_pool_shm_readonly() < 0)
321 {
322 goto cleanup;
323 }
324 #endif
325
326 // Load menu in shared memory
327 if (set_menu_shm_readonly(&bbs_menu) < 0)
328 {
329 goto cleanup;
330 }
331
332 // Set default charset
333 if (io_conv_init(BBS_default_charset) < 0)
334 {
335 log_error("io_conv_init(%s) error\n", BBS_default_charset);
336 goto cleanup;
337 }
338
339 set_input_echo(0);
340
341 // Set user charset
342 if (bbs_charset_select() < 0)
343 {
344 goto cleanup;
345 }
346
347 // System info
348 if (bbs_info() < 0)
349 {
350 goto cleanup;
351 }
352
353 // Welcome
354 if (bbs_welcome() < 0)
355 {
356 goto cleanup;
357 }
358
359 // User login
360 if (SSH_v2)
361 {
362 snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
363 press_any_key_ex(msg, 60);
364 }
365 else if (bbs_login() < 0)
366 {
367 goto cleanup;
368 }
369 log_common("User [%s] login\n", BBS_username);
370
371 // Load section aid locations
372 if (section_aid_locations_load(BBS_priv.uid) < 0)
373 {
374 log_error("article_view_log_load() error\n");
375 goto cleanup;
376 }
377
378 // Load article view log
379 if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
380 {
381 log_error("article_view_log_load() error\n");
382 goto cleanup;
383 }
384
385 // Load article favorite
386 if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
387 {
388 log_error("article_favor_load() error\n");
389 goto cleanup;
390 }
391
392 // Init editor memory pool
393 if (editor_memory_pool_init() < 0)
394 {
395 log_error("editor_memory_pool_init() error\n");
396 goto cleanup;
397 }
398
399 clearscr();
400
401 // BBS Top 10
402 display_file(VAR_BBS_TOP, 1);
403
404 // Main
405 bbs_center();
406
407 // Save section aid locations
408 if (section_aid_locations_save(BBS_priv.uid) < 0)
409 {
410 log_error("article_view_log_save() error\n");
411 }
412
413 // Save incremental article view log
414 if (article_view_log_save_inc(&BBS_article_view_log) < 0)
415 {
416 log_error("article_view_log_save_inc() error\n");
417 }
418
419 // Save incremental article favorite
420 if (article_favor_save_inc(&BBS_article_favor) < 0)
421 {
422 log_error("article_favor_save_inc() error\n");
423 }
424
425 cleanup:
426 // Logout
427 bbs_logout();
428
429 // Cleanup iconv
430 io_conv_cleanup();
431
432 // Cleanup editor memory pool
433 editor_memory_pool_cleanup();
434
435 // Unload article view log
436 article_view_log_unload(&BBS_article_view_log);
437
438 // Unload article favor
439 article_favor_unload(&BBS_article_favor);
440
441 // Detach menu in shared memory
442 detach_menu_shm(&bbs_menu);
443 detach_menu_shm(&top10_menu);
444
445 // Detach data pools shm
446 detach_user_list_pool_shm();
447 detach_section_list_shm();
448 detach_article_block_shm();
449 detach_trie_dict_shm();
450
451 // Cleanup BWF
452 bwf_cleanup();
453
454 return 0;
455 }

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