/[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.101 - (show annotations)
Thu Nov 6 10:58:03 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.100: +3 -0 lines
Content type: text/x-csrc
Update debugging code

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

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