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


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

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