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


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

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