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


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

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