/[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.94 - (show annotations)
Sat Nov 1 10:35:42 2025 UTC (4 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.93: +18 -69 lines
Content type: text/x-csrc
Get online user/guest count from user_list instead of database

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

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