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


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

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