/[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.16 by sysadm, Thu Mar 17 10:48:46 2005 UTC Revision 1.76 by sysadm, Wed Jul 2 04:17:33 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_view_log.h"
18  #include "bbs.h"  #include "bbs.h"
19    #include "bbs_cmd.h"
20    #include "bbs_main.h"
21  #include "common.h"  #include "common.h"
22    #include "database.h"
23    #include "editor.h"
24  #include "io.h"  #include "io.h"
25    #include "log.h"
26    #include "login.h"
27  #include "menu.h"  #include "menu.h"
28    #include "screen.h"
29    #include "section_list.h"
30    #include "trie_dict.h"
31    #include "user_priv.h"
32    #include <errno.h>
33    #include <signal.h>
34    #include <stdlib.h>
35    #include <string.h>
36  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
37  #include <unistd.h>  #include <unistd.h>
38    
39  #define ACTIVE_BOARD_HEIGHT     8  int bbs_info()
40    {
41            prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",
42                       BBS_name, BBS_server, APP_INFO);
43    
44            return iflush();
45    }
46    
47  int  int bbs_welcome(void)
 bbs_main ()  
48  {  {
49    char temp[256];          char sql[SQL_BUFFER_LEN];
   int ret;  
50    
51    set_input_echo (0);          u_int32_t u_online = 0;
52            u_int32_t u_anonymous = 0;
53            u_int32_t u_total = 0;
54            u_int32_t u_login_count = 0;
55    
56            MYSQL *db;
57            MYSQL_RES *rs;
58            MYSQL_ROW row;
59    
60    bbs_info ();          db = db_open();
61            if (db == NULL)
62            {
63                    return -1;
64            }
65    
66    //Welcome          snprintf(sql, sizeof(sql),
67    bbs_welcome ();                           "SELECT COUNT(*) AS cc FROM "
68                             "(SELECT DISTINCT SID FROM user_online "
69                             "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
70                             BBS_user_off_line);
71            if (mysql_query(db, sql) != 0)
72            {
73                    log_error("Query user_online error: %s\n", mysql_error(db));
74                    mysql_close(db);
75                    return -2;
76            }
77            if ((rs = mysql_store_result(db)) == NULL)
78            {
79                    log_error("Get user_online data failed\n");
80                    mysql_close(db);
81                    return -2;
82            }
83            if ((row = mysql_fetch_row(rs)))
84            {
85                    u_online = (u_int32_t)atoi(row[0]);
86            }
87            mysql_free_result(rs);
88    
89    //Login          snprintf(sql, sizeof(sql),
90    ret = bbs_login ();                           "SELECT COUNT(*) AS cc FROM "
91    if (ret < 0)                           "(SELECT DISTINCT SID FROM user_online "
92      return -1;                           "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
93    clearscr ();                           BBS_user_off_line);
94            if (mysql_query(db, sql) != 0)
95            {
96                    log_error("Query user_online error: %s\n", mysql_error(db));
97                    mysql_close(db);
98                    return -2;
99            }
100            if ((rs = mysql_store_result(db)) == NULL)
101            {
102                    log_error("Get user_online data failed\n");
103                    mysql_close(db);
104                    return -2;
105            }
106            if ((row = mysql_fetch_row(rs)))
107            {
108                    u_anonymous = (u_int32_t)atoi(row[0]);
109            }
110            mysql_free_result(rs);
111    
112    //BBS Top 10          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
113    strcpy (temp, app_home_dir);          if (mysql_query(db, sql) != 0)
114    strcat (temp, "data/bbs_top.txt");          {
115    display_file_ex (temp, 1, 1);                  log_error("Query user_list error: %s\n", mysql_error(db));
116                    mysql_close(db);
117                    return -2;
118            }
119            if ((rs = mysql_store_result(db)) == NULL)
120            {
121                    log_error("Get user_list data failed\n");
122                    mysql_close(db);
123                    return -2;
124            }
125            if ((row = mysql_fetch_row(rs)))
126            {
127                    u_total = (u_int32_t)atoi(row[0]);
128            }
129            mysql_free_result(rs);
130    
131    //Main          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");
132    bbs_center ();          if (mysql_query(db, sql) != 0)
133            {
134                    log_error("Query user_login_log error: %s\n", mysql_error(db));
135                    mysql_close(db);
136                    return -2;
137            }
138            if ((rs = mysql_store_result(db)) == NULL)
139            {
140                    log_error("Get user_login_log data failed\n");
141                    mysql_close(db);
142                    return -2;
143            }
144            if ((row = mysql_fetch_row(rs)))
145            {
146                    u_login_count = (u_int32_t)atoi(row[0]);
147            }
148            mysql_free_result(rs);
149    
150    //Logout          mysql_close(db);
   bbs_exit ();  
151    
152    return 0;          // Count current user before login
153  }          u_online++;
154            u_anonymous++;
155    
156  int          // Display logo
157  bbs_info ()          display_file(DATA_WELCOME, 2);
158  {  
159    prints ("ӭ \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          // Display welcome message
160            BBS_name, BBS_server, app_version);          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
161                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
162                       "匿名游客[\033[36m%d\033[32m] "
163                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
164                       "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
165                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
166                       BBS_max_user, BBS_start_dt, u_login_count);
167    
168    iflush ();          iflush();
169    
170    return 0;          return 0;
171  }  }
172    
173  int  int bbs_logout(void)
 bbs_exit ()  
174  {  {
175    char temp[256];          MYSQL *db;
176    
177            db = db_open();
178            if (db == NULL)
179            {
180                    return -1;
181            }
182    
183            if (user_online_del(db) < 0)
184            {
185                    return -2;
186            }
187    
188            mysql_close(db);
189    
190    strcpy (temp, app_home_dir);          display_file(DATA_GOODBYE, 1);
   strcat (temp, "data/goodbye.txt");  
   display_file_ex (temp, 1, 0);  
191    
192    sleep (1);          log_common("User logout\n");
193    
194    return 0;          return 0;
195  }  }
196    
197  int  int bbs_center()
 bbs_center ()  
198  {  {
199    int ch, result;          int ch;
200    char key;          time_t t_last_action;
201    time_t t_last_action;  
202    fd_set inputs, testfds;          BBS_last_access_tm = t_last_action = time(NULL);
203    struct timeval timeout;  
204            clearscr();
205    FD_ZERO (&inputs);  
206    FD_SET (0, &inputs);          show_top("", BBS_name, "");
207            show_active_board();
208    t_last_action = time (0);          show_bottom("");
209            display_menu(&bbs_menu);
210    clearscr ();          iflush();
211    
212    show_top ("");          while (!SYS_server_exit)
213    show_active_board ();          {
214    display_menu (&bbs_main_menu);                  ch = igetch(100);
215    show_bottom ("");  
216                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
217    while (1)                  {
218      {                          t_last_action = time(NULL);
219        testfds = inputs;  
220        timeout.tv_sec = 0;                          show_active_board();
221        timeout.tv_usec = 100000;                          show_bottom("");
222                            display_menu_cursor(&bbs_menu, 1);
223        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,                          iflush();
224                         (fd_set *) NULL, &timeout);                  }
225    
226        switch (result)                  if (user_online_update("MENU") < 0)
227          {                  {
228          case 0:                          log_error("user_online_update(MENU) error\n");
229            break;                  }
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               ch = igetch ();  
230    
231                switch (ch)                  switch (ch)
232                  {                  {
233                  case KEY_LEFT:                  case KEY_NULL: // broken pipe
234                    ch = 'G';                          return 0;
235                    case KEY_TIMEOUT:
236                            if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
237                            {
238                                    return 0;
239                            }
240                            continue;
241                    case CR:
242                            igetch_reset();
243                  default:                  default:
244                    key = menu_control (&bbs_main_menu, ch);                          switch (menu_control(&bbs_menu, ch))
245                    switch (key)                          {
246                      {                          case EXITBBS:
247                      case 'G':                          case EXITMENU:
248                        return 0;                                  return 0;
249                      }                          case REDRAW:
250                                    t_last_action = time(NULL);
251                                    clearscr();
252                                    show_top("", BBS_name, "");
253                                    show_active_board();
254                                    show_bottom("");
255                                    display_menu(&bbs_menu);
256                                    break;
257                            case NOREDRAW:
258                            case UNKNOWN_CMD:
259                            default:
260                                    break;
261                            }
262                            iflush();
263                  }                  }
264              }  
265            break;                  BBS_last_access_tm = time(NULL);
         }  
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
           show_active_board ();  
266          }          }
     }  
267    
268    return 0;          return 0;
269  }  }
270    
271  int  int bbs_main()
 show_top (char *status)  
272  {  {
273    char buffer[256];          struct sigaction act = {0};
274    
275    str_space (buffer, 20 - strlen (BBS_current_section_name));          // Set signal handler
276            act.sa_handler = SIG_IGN;
277    moveto (1, 0);          if (sigaction(SIGHUP, &act, NULL) == -1)
278    prints ("\033[1;44;33m%-20s \033[37m%20s"          {
279            "         %s\033[33m [%s]\033[m",                  log_error("set signal action of SIGHUP error: %d\n", errno);
280            status, BBS_name, buffer, BBS_current_section_name);                  goto cleanup;
281    iflush ();          }
282            act.sa_handler = SIG_DFL;
283            if (sigaction(SIGCHLD, &act, NULL) == -1)
284            {
285                    log_error("set signal action of SIGCHLD error: %d\n", errno);
286                    goto cleanup;
287            }
288    
289    return 0;          // Set data pools in shared memory readonly
290  }          if (set_trie_dict_shm_readonly() < 0)
291            {
292                    goto cleanup;
293            }
294            if (set_article_block_shm_readonly() < 0)
295            {
296                    goto cleanup;
297            }
298            if (set_section_list_shm_readonly() < 0)
299            {
300                    goto cleanup;
301            }
302    
303  int          // Load menu in shared memory
304  show_bottom (char *msg)          if (set_menu_shm_readonly(&bbs_menu) < 0)
305  {          {
306    char str_time[256], str_time_onine[20], buffer[256];                  goto cleanup;
307    time_t time_online;          }
   struct tm *tm_online;  
   
   get_time_str (str_time, 256);  
   str_space (buffer, 33 - strlen (BBS_username));  
   
   time_online = time (0) - BBS_login_tm;  
   tm_online = gmtime (&time_online);  
   
   moveto (screen_lines, 0);  
   prints ("\033[1;44;33m[\033[36m%s\033[33m]"  
           "%sʺ[\033[36m%s\033[33m]"  
           "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",  
           str_time, buffer, BBS_username, tm_online->tm_mday - 1,  
           tm_online->tm_hour, tm_online->tm_min);  
   iflush ();  
308    
309    return 0;          set_input_echo(0);
 }  
310    
311  int          // System info
312  show_active_board ()          if (bbs_info() < 0)
313  {          {
314    char filename[256], buffer[260];                  goto cleanup;
315    FILE *fin;          }
   int i, j;  
   static long int line;  
316    
317    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          // Welcome
318            if (bbs_welcome() < 0)
319            {
320                    goto cleanup;
321            }
322    
323    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          // User login
324            if (SSH_v2)
325            {
326                    prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
327                    iflush();
328                    igetch_t(MAX_DELAY_TIME);
329            }
330            else if (bbs_login() < 0)
331            {
332                    goto cleanup;
333            }
334    
335    moveto (3, 0);          // Load article view log
336            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
337            {
338                    log_error("article_view_log_load() error\n");
339                    goto cleanup;
340            }
341    
342    if ((fin = fopen (filename, "r")) != NULL)          // Init editor memory pool
343      {          if (editor_memory_pool_init() < 0)
       for (j = 0; j < line; j++)  
344          {          {
345            if (fgets (buffer, 255, fin) == NULL)                  log_error("editor_memory_pool_init() error\n");
346              {                  goto cleanup;
               line = 0;  
               rewind (fin);  
               break;  
             }  
347          }          }
348    
349        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          clearscr();
350    
351            // BBS Top 10
352            display_file(VAR_BBS_TOP, 1);
353    
354            // Main
355            bbs_center();
356    
357            // Logout
358            bbs_logout();
359    
360            // Save incremental article view log
361            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
362          {          {
363            if (fgets (buffer, 255, fin) == NULL)                  log_error("article_view_log_save_inc() error\n");
             {  
               line = 0;  
               if (j == 0)  
                 {  
                   rewind (fin);  
                   if (fgets (buffer, 255, fin) == NULL)  
                     {  
                       break;  
                     }  
                 }  
               else  
                 {  
                   break;  
                 }  
             }  
           line++;  
           i = strlen (buffer);  
           if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
364          }          }
       fclose (fin);  
     }  
365    
366    return 0;  cleanup:
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            // Detach menu in shared memory
374            detach_menu_shm(&bbs_menu);
375    
376            // Detach data pools shm
377            detach_section_list_shm();
378            detach_article_block_shm();
379            detach_trie_dict_shm();
380    
381            return 0;
382  }  }


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

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