/[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.78 by sysadm, Mon Oct 13 00:46:03 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 "screen.h"
30    #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>
37  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
38  #include <unistd.h>  #include <unistd.h>
39    
40  #define ACTIVE_BOARD_HEIGHT     8  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];
   int ret;  
51    
52    set_input_echo (0);          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    bbs_info ();          db = db_open();
62            if (db == NULL)
63            {
64                    return -1;
65            }
66    
67    //Welcome          snprintf(sql, sizeof(sql),
68    bbs_welcome ();                           "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    //Login          snprintf(sql, sizeof(sql),
91    ret = bbs_login ();                           "SELECT COUNT(*) AS cc FROM "
92    if (ret < 0)                           "(SELECT DISTINCT SID FROM user_online "
93      return -1;                           "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
94    clearscr ();                           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    //BBS Top 10          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
114    strcpy (temp, app_home_dir);          if (mysql_query(db, sql) != 0)
115    strcat (temp, "data/bbs_top.txt");          {
116    display_file_ex (temp, 1, 1);                  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    //Main          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");
133    bbs_center ();          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    //Logout          mysql_close(db);
   bbs_exit ();  
152    
153    return 0;          // Count current user before login
154  }          u_online++;
155            u_anonymous++;
156    
157  int          // Display logo
158  bbs_info ()          display_file(DATA_WELCOME, 2);
 {  
   prints ("ӭ \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",  
           BBS_name, BBS_server, app_version);  
159    
160    iflush ();          // Display welcome message
161            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    return 0;          iflush();
170    
171            return 0;
172  }  }
173    
174  int  int bbs_logout(void)
 bbs_exit ()  
175  {  {
176    char temp[256];          MYSQL *db;
177    
178            db = db_open();
179            if (db == NULL)
180            {
181                    return -1;
182            }
183    
184            if (user_online_del(db) < 0)
185            {
186                    return -2;
187            }
188    
189            mysql_close(db);
190    
191    strcpy (temp, app_home_dir);          display_file(DATA_GOODBYE, 1);
   strcat (temp, "data/goodbye.txt");  
   display_file_ex (temp, 1, 0);  
192    
193    sleep (1);          log_common("User logout\n");
194    
195    return 0;          return 0;
196  }  }
197    
198  int  int bbs_center()
 bbs_center ()  
199  {  {
200    int ch, result;          int ch;
201    char key;          time_t t_last_action;
202    time_t t_last_action;  
203    fd_set inputs, testfds;          BBS_last_access_tm = t_last_action = time(NULL);
204    struct timeval timeout;  
205            clearscr();
206    FD_ZERO (&inputs);  
207    FD_SET (0, &inputs);          show_top("", BBS_name, "");
208            show_active_board();
209    t_last_action = time (0);          show_bottom("");
210            display_menu(&bbs_menu);
211    clearscr ();          iflush();
212    
213    show_top ("");          while (!SYS_server_exit)
214    show_active_board ();          {
215    display_menu (&bbs_main_menu);                  ch = igetch(100);
216    show_bottom ("");  
217                    if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
218    while (1)                  {
219      {                          t_last_action = time(NULL);
220        testfds = inputs;  
221        timeout.tv_sec = 0;                          show_active_board();
222        timeout.tv_usec = 100000;                          show_bottom("");
223                            display_menu_cursor(&bbs_menu, 1);
224        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,                          iflush();
225                         (fd_set *) NULL, &timeout);                  }
226    
227        switch (result)                  if (user_online_update("MENU") < 0)
228          {                  {
229          case 0:                          log_error("user_online_update(MENU) error\n");
230            break;                  }
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               ch = igetch ();  
231    
232                switch (ch)                  switch (ch)
233                  {                  {
234                  case KEY_LEFT:                  case KEY_NULL: // broken pipe
235                    ch = 'G';                          return 0;
236                    case KEY_TIMEOUT:
237                            if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
238                            {
239                                    return 0;
240                            }
241                            continue;
242                    case CR:
243                            igetch_reset();
244                  default:                  default:
245                    key = menu_control (&bbs_main_menu, ch);                          switch (menu_control(&bbs_menu, ch))
246                    switch (key)                          {
247                      {                          case EXITBBS:
248                      case 'G':                          case EXITMENU:
249                        return 0;                                  return 0;
250                      }                          case REDRAW:
251                                    t_last_action = time(NULL);
252                                    clearscr();
253                                    show_top("", BBS_name, "");
254                                    show_active_board();
255                                    show_bottom("");
256                                    display_menu(&bbs_menu);
257                                    break;
258                            case NOREDRAW:
259                            case UNKNOWN_CMD:
260                            default:
261                                    break;
262                            }
263                            iflush();
264                  }                  }
265              }  
266            break;                  BBS_last_access_tm = time(NULL);
         }  
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
           show_active_board ();  
267          }          }
     }  
268    
269    return 0;          return 0;
270  }  }
271    
272  int  int bbs_main()
 show_top (char *status)  
273  {  {
274    char buffer[256];          struct sigaction act = {0};
275    
276    str_space (buffer, 20 - strlen (BBS_current_section_name));          // Set signal handler
277            act.sa_handler = SIG_IGN;
278            if (sigaction(SIGHUP, &act, NULL) == -1)
279            {
280                    log_error("set signal action of SIGHUP error: %d\n", errno);
281                    goto cleanup;
282            }
283            act.sa_handler = SIG_DFL;
284            if (sigaction(SIGCHLD, &act, NULL) == -1)
285            {
286                    log_error("set signal action of SIGCHLD error: %d\n", errno);
287                    goto cleanup;
288            }
289    
290    moveto (1, 0);          // Set data pools in shared memory readonly
291    prints ("\033[1;44;33m%-20s \033[37m%20s"          if (set_trie_dict_shm_readonly() < 0)
292            "         %s\033[33m [%s]\033[m",          {
293            status, BBS_name, buffer, BBS_current_section_name);                  goto cleanup;
294    iflush ();          }
295            if (set_article_block_shm_readonly() < 0)
296            {
297                    goto cleanup;
298            }
299            if (set_section_list_shm_readonly() < 0)
300            {
301                    goto cleanup;
302            }
303    
304    return 0;          // Load menu in shared memory
305  }          if (set_menu_shm_readonly(&bbs_menu) < 0)
306            {
307                    goto cleanup;
308            }
309    
310  int          set_input_echo(0);
 show_bottom (char *msg)  
 {  
   char str_time[256], str_time_onine[20], buffer[256];  
   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 ();  
311    
312    return 0;          // System info
313  }          if (bbs_info() < 0)
314            {
315                    goto cleanup;
316            }
317    
318  int          // Welcome
319  show_active_board ()          if (bbs_welcome() < 0)
320  {          {
321    char filename[256], buffer[260];                  goto cleanup;
322    FILE *fin;          }
   int i, j;  
   static long int line;  
323    
324    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          // User login
325            if (SSH_v2)
326            {
327                    prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
328                    iflush();
329                    igetch_t(MAX_DELAY_TIME);
330            }
331            else if (bbs_login() < 0)
332            {
333                    goto cleanup;
334            }
335    
336            // Load article view log
337            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
338            {
339                    log_error("article_view_log_load() error\n");
340                    goto cleanup;
341            }
342    
343            // Load article favorite
344            if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
345            {
346                    log_error("article_favor_load() error\n");
347                    goto cleanup;
348            }
349    
350            // Init editor memory pool
351            if (editor_memory_pool_init() < 0)
352            {
353                    log_error("editor_memory_pool_init() error\n");
354                    goto cleanup;
355            }
356    
357            clearscr();
358    
359            // BBS Top 10
360            display_file(VAR_BBS_TOP, 1);
361    
362    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          // Main
363            bbs_center();
364    
365    moveto (3, 0);          // Logout
366            bbs_logout();
367    
368    if ((fin = fopen (filename, "r")) != NULL)          // Save incremental article view log
369      {          if (article_view_log_save_inc(&BBS_article_view_log) < 0)
       for (j = 0; j < line; j++)  
370          {          {
371            if (fgets (buffer, 255, fin) == NULL)                  log_error("article_view_log_save_inc() error\n");
             {  
               line = 0;  
               rewind (fin);  
               break;  
             }  
372          }          }
373    
374        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          // Save incremental article favorite
375            if (article_favor_save_inc(&BBS_article_favor) < 0)
376          {          {
377            if (fgets (buffer, 255, fin) == NULL)                  log_error("article_favor_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 ();  
378          }          }
       fclose (fin);  
     }  
379    
380    return 0;  cleanup:
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    
393            // Detach data pools shm
394            detach_section_list_shm();
395            detach_article_block_shm();
396            detach_trie_dict_shm();
397    
398            return 0;
399  }  }


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

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