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

Diff of /lbbs/src/bbs_main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Tue Oct 19 02:08:35 2004 UTC Revision 1.117 by sysadm, Sun Nov 30 04:43:46 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            bbsd.c  -  description  /*
3                               -------------------   * bbs_main
4      begin                : Mon Oct 18 2004   *   - entry and major procedures of user interactive access
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
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"  #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  int          mysql_close(db);
122  bbs_main()  
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          log_std("bbs_main()\n");          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                            log_error("KEY_NULL\n");
198                            loop = 0;
199                            break;
200                    case KEY_TIMEOUT:
201                            if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
202                            {
203                                    log_error("User input timeout\n");
204                                    loop = 0;
205                                    break;
206                            }
207                            continue;
208                    case CR:
209                    default:
210                            switch (menu_control(&bbs_menu, ch))
211                            {
212                            case EXITBBS:
213                            case EXITMENU:
214                                    loop = 0;
215                                    break;
216                            case REDRAW:
217                                    t_last_action = time(NULL);
218                                    clearscr();
219                                    show_top("", BBS_name, "");
220                                    show_active_board();
221                                    show_bottom("");
222                                    display_menu(&bbs_menu);
223                                    break;
224                            case NOREDRAW:
225                            case UNKNOWN_CMD:
226                            default:
227                                    break;
228                            }
229                            iflush();
230                    }
231            }
232    
233            return 0;
234    }
235    
236    int bbs_charset_select()
237    {
238            char msg[LINE_BUFFER_LEN];
239            int ch;
240    
241            snprintf(msg, sizeof(msg),
242                             "\rChoose character set in 5 seconds, (U)UTF-8, (G)GBK [U]: ");
243    
244            while (!SYS_server_exit)
245            {
246                    ch = press_any_key_ex(msg, 5);
247                    switch (toupper(ch))
248                    {
249                    case KEY_NULL:
250                            return -1;
251                    case KEY_TIMEOUT:
252                    case CR:
253                    case 'U':
254                            break;
255                    case 'G':
256                            if (io_conv_init("GBK") < 0)
257                            {
258                                    log_error("io_conv_init(%s) error\n", "GBK");
259                                    return -1;
260                            }
261                            break;
262                    default:
263                            continue;
264                    }
265    
266                    break;
267            }
268    
269            snprintf(msg, sizeof(msg),
270                             "\r请在5秒内选择宽字符显示规则, (V)变宽, (F)定宽? [V]: ");
271    
272            while (!SYS_server_exit)
273            {
274                    ch = press_any_key_ex(msg, 5);
275                    switch (toupper(ch))
276                    {
277                    case KEY_NULL:
278                            return -1;
279                    case KEY_TIMEOUT:
280                    case CR:
281                    case 'V':
282                            UTF8_fixed_width = 0;
283                            break;
284                    case 'F':
285                            UTF8_fixed_width = 1;
286                            break;
287                    default:
288                            continue;
289                    }
290    
291                    break;
292            }
293    
294            return 0;
295    }
296    
297    int bbs_main()
298    {
299            struct sigaction act = {0};
300            char msg[LINE_BUFFER_LEN];
301    
302            // Set signal handler
303            act.sa_handler = SIG_IGN;
304            if (sigaction(SIGHUP, &act, NULL) == -1)
305            {
306                    log_error("set signal action of SIGHUP error: %d\n", errno);
307                    goto cleanup;
308            }
309            act.sa_handler = SIG_DFL;
310            if (sigaction(SIGCHLD, &act, NULL) == -1)
311            {
312                    log_error("set signal action of SIGCHLD error: %d\n", errno);
313                    goto cleanup;
314            }
315            act.sa_handler = child_proc_sig_usr1_handler;
316            if (sigaction(SIGUSR1, &act, NULL) == -1)
317            {
318                    log_error("set signal action of SIGUSR1 error: %d\n", errno);
319                    goto cleanup;
320            }
321    
322            // Set data pools in shared memory readonly
323            if (set_trie_dict_shm_readonly() < 0)
324            {
325                    goto cleanup;
326            }
327            if (set_article_block_shm_readonly() < 0)
328            {
329                    goto cleanup;
330            }
331    #ifdef HAVE_SYSTEM_V
332            if (set_section_list_shm_readonly() < 0)
333            {
334                    goto cleanup;
335            }
336            if (set_user_list_pool_shm_readonly() < 0)
337            {
338                    goto cleanup;
339            }
340    #endif
341    
342            // Load menu in shared memory
343            if (set_menu_shm_readonly(&bbs_menu) < 0)
344            {
345                    goto cleanup;
346            }
347    
348            // Set default charset
349            if (io_conv_init(BBS_default_charset) < 0)
350            {
351                    log_error("io_conv_init(%s) error\n", BBS_default_charset);
352                    goto cleanup;
353            }
354    
355            set_input_echo(0);
356    
357            // Set user charset
358            if (bbs_charset_select() < 0)
359            {
360                    goto cleanup;
361            }
362    
363            // System info
364            if (bbs_info() < 0)
365            {
366                    goto cleanup;
367            }
368    
369            // Welcome
370            if (bbs_welcome() < 0)
371            {
372                    goto cleanup;
373            }
374    
375            // User login
376            if (SSH_v2)
377            {
378                    snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
379                    press_any_key_ex(msg, 60);
380            }
381            else if (bbs_login() < 0)
382            {
383                    goto cleanup;
384            }
385            log_common("User [%s] (uid=%d) login from %s:%d\n",
386                               BBS_username, BBS_priv.uid, hostaddr_client, port_client);
387    
388            // Load section aid locations
389            if (section_aid_locations_load(BBS_priv.uid) < 0)
390            {
391                    log_error("article_view_log_load() error\n");
392                    goto cleanup;
393            }
394    
395            // Load article view log
396            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
397            {
398                    log_error("article_view_log_load() error\n");
399                    goto cleanup;
400            }
401    
402            // Load article favorite
403            if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
404            {
405                    log_error("article_favor_load() error\n");
406                    goto cleanup;
407            }
408    
409            // Init editor memory pool
410            if (editor_memory_pool_init() < 0)
411            {
412                    log_error("editor_memory_pool_init() error\n");
413                    goto cleanup;
414            }
415    
416            clearscr();
417    
418            // BBS Top 10
419            display_file(VAR_BBS_TOP, 1);
420    
421            // Main
422            bbs_center();
423    
424            // Logout
425            bbs_logout();
426    
427            // Save section aid locations
428            if (section_aid_locations_save(BBS_priv.uid) < 0)
429            {
430                    log_error("article_view_log_save() error\n");
431            }
432    
433            // Save incremental article view log
434            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
435            {
436                    log_error("article_view_log_save_inc() error\n");
437            }
438    
439            // Save incremental article favorite
440            if (article_favor_save_inc(&BBS_article_favor) < 0)
441            {
442                    log_error("article_favor_save_inc() error\n");
443            }
444    
445    cleanup:
446            // Cleanup session
447            bbs_session_cleanup();
448    
449            // Cleanup iconv
450            io_conv_cleanup();
451    
452            // Cleanup editor memory pool
453            editor_memory_pool_cleanup();
454    
455            // Unload article view log
456            article_view_log_unload(&BBS_article_view_log);
457    
458            // Unload article favor
459            article_favor_unload(&BBS_article_favor);
460    
461            // Detach menu in shared memory
462            detach_menu_shm(&bbs_menu);
463            detach_menu_shm(&top10_menu);
464    
465            // Detach data pools shm
466            detach_user_list_pool_shm();
467            detach_section_list_shm();
468            detach_article_block_shm();
469            detach_trie_dict_shm();
470    
471            // Cleanup BWF
472            bwf_cleanup();
473    
474          return 0;          return 0;
475  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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