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


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

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