/[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.26 by sysadm, Tue Mar 22 08:59:31 2005 UTC Revision 1.60 by sysadm, Thu May 29 13: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 "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 "log.h"
24  #include "io.h"  #include "io.h"
25    #include "screen.h"
26  #include "menu.h"  #include "menu.h"
27  #include "bbs_cmd.h"  #include "bbs_cmd.h"
28    #include "section_list.h"
29    #include "trie_dict.h"
30    #include <unistd.h>
31  #include <time.h>  #include <time.h>
32  #include <string.h>  #include <string.h>
33    #include <stdlib.h>
34    
35  int  int bbs_info()
 bbs_main ()  
36  {  {
37    char temp[256];          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",
38    int ret;                     BBS_name, BBS_server, app_version);
39    
40    set_input_echo (0);          return iflush();
41    }
42    
43    bbs_info ();  int bbs_welcome(MYSQL *db)
44    {
45            char sql[SQL_BUFFER_LEN];
46    
47    //Welcome          u_int32_t u_online = 0;
48    bbs_welcome ();          u_int32_t u_anonymous = 0;
49            u_int32_t u_total = 0;
50            u_int32_t max_u_online = 0;
51            u_int32_t u_login_count = 0;
52    
53            MYSQL_RES *rs;
54            MYSQL_ROW row;
55    
56            snprintf(sql, sizeof(sql),
57                             "SELECT COUNT(*) AS cc FROM "
58                             "(SELECT DISTINCT SID FROM user_online "
59                             "WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
60                             BBS_user_off_line);
61            if (mysql_query(db, sql) != 0)
62            {
63                    log_error("Query user_online error: %s\n", mysql_error(db));
64                    return -2;
65            }
66            if ((rs = mysql_store_result(db)) == NULL)
67            {
68                    log_error("Get user_online data failed\n");
69                    return -2;
70            }
71            if ((row = mysql_fetch_row(rs)))
72            {
73                    u_online = (u_int32_t)atoi(row[0]);
74            }
75            mysql_free_result(rs);
76    
77    //Login          snprintf(sql, sizeof(sql),
78    ret = bbs_login ();                           "SELECT COUNT(*) AS cc FROM "
79    if (ret < 0)                           "(SELECT DISTINCT SID FROM user_online "
80      return -1;                           "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
81    log_std ("User \"%s\"(%ld) login from %s:%d\n",                           BBS_user_off_line);
82      BBS_username, BBS_priv.uid, hostaddr_client, port_client);          if (mysql_query(db, sql) != 0)
83    clearscr ();          {
84                    log_error("Query user_online error: %s\n", mysql_error(db));
85                    return -2;
86            }
87            if ((rs = mysql_store_result(db)) == NULL)
88            {
89                    log_error("Get user_online data failed\n");
90                    return -2;
91            }
92            if ((row = mysql_fetch_row(rs)))
93            {
94                    u_anonymous = (u_int32_t)atoi(row[0]);
95            }
96            mysql_free_result(rs);
97    
98    //BBS Top 10          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
99    strcpy (temp, app_home_dir);          if (mysql_query(db, sql) != 0)
100    strcat (temp, "data/bbs_top.txt");          {
101    display_file_ex (temp, 1, 1);                  log_error("Query user_list error: %s\n", mysql_error(db));
102                    return -2;
103            }
104            if ((rs = mysql_store_result(db)) == NULL)
105            {
106                    log_error("Get user_list data failed\n");
107                    return -2;
108            }
109            if ((row = mysql_fetch_row(rs)))
110            {
111                    u_total = (u_int32_t)atoi(row[0]);
112            }
113            mysql_free_result(rs);
114    
115    //Main          snprintf(sql, sizeof(sql), "SELECT ID FROM user_login_log ORDER BY ID LIMIT 1");
116    bbs_center ();          if (mysql_query(db, sql) != 0)
117            {
118                    log_error("Query user_login_log error: %s\n", mysql_error(db));
119                    return -2;
120            }
121            if ((rs = mysql_store_result(db)) == NULL)
122            {
123                    log_error("Get user_login_log data failed\n");
124                    return -2;
125            }
126            if ((row = mysql_fetch_row(rs)))
127            {
128                    u_login_count = (u_int32_t)atoi(row[0]);
129            }
130            mysql_free_result(rs);
131    
132    //Logout          // Log max user_online
133    bbs_exit ();          FILE *fin, *fout;
134    log_std ("User logout\n");          if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL)
135            {
136                    fscanf(fin, "%d", &max_u_online);
137                    fclose(fin);
138            }
139            if (u_online > max_u_online)
140            {
141                    max_u_online = u_online;
142                    if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL)
143                    {
144                            log_error("Open max_user_online.dat failed\n");
145                            return -3;
146                    }
147                    fprintf(fout, "%d\n", max_u_online);
148                    fclose(fout);
149            }
150    
151            // Count current user before login
152            u_online++;
153            u_anonymous++;
154    
155            // Display logo
156            display_file(DATA_WELCOME, 1, 0);
157    
158            // Display welcome message
159            prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
160                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
161                       "匿名游客[\033[36m%d\033[32m] "
162                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
163                       "从 [\033[36m%s\033[32m] 起,最高人数记录:"
164                       "[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\r\n",
165                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
166                       BBS_max_user, BBS_start_dt, max_u_online, u_login_count);
167    
168    return 0;          return 0;
169  }  }
170    
171  int  int bbs_logout(MYSQL *db)
 bbs_info ()  
172  {  {
173    prints ("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          if (user_online_del(db) < 0)
174            BBS_name, BBS_server, app_version);          {
175                    return -1;
176            }
177    
178    iflush ();          display_file(DATA_GOODBYE, 1, 1);
179    
180    return 0;          log_std("User logout\n");
181    
182            return 0;
183  }  }
184    
185  int  int bbs_center()
 bbs_exit ()  
186  {  {
187    char temp[256];          int ch;
188            time_t t_last_action;
189    
190            BBS_last_access_tm = t_last_action = time(0);
191    
192    strcpy (temp, app_home_dir);          clearscr();
   strcat (temp, "data/goodbye.txt");  
   display_file_ex (temp, 1, 0);  
193    
194    sleep (1);          show_top("", BBS_name, "");
195            show_active_board();
196            show_bottom("");
197            display_menu(p_bbs_menu);
198            iflush();
199    
200    return 0;          while (!SYS_server_exit)
201            {
202                    ch = igetch(100);
203    
204                    if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)
205                    {
206                            t_last_action = time(0);
207    
208                            show_active_board();
209                            show_bottom("");
210                            display_menu_cursor(p_bbs_menu, 1);
211                            iflush();
212                    }
213    
214                    switch (ch)
215                    {
216                    case KEY_NULL: // broken pipe
217                            return 0;
218                    case KEY_TIMEOUT:
219                            if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
220                            {
221                                    return 0;
222                            }
223                            continue;
224                    case CR:
225                            igetch_reset();
226                    default:
227                            switch (menu_control(p_bbs_menu, ch))
228                            {
229                            case EXITBBS:
230                                    return 0;
231                            case REDRAW:
232                                    t_last_action = time(0);
233                                    clearscr();
234                                    show_top("", BBS_name, "");
235                                    show_active_board();
236                                    show_bottom("");
237                                    display_menu(p_bbs_menu);
238                                    break;
239                            case NOREDRAW:
240                            case UNKNOWN_CMD:
241                            default:
242                                    break;
243                            }
244                            iflush();
245                    }
246    
247                    BBS_last_access_tm = time(0);
248            }
249    
250            return 0;
251  }  }
252    
253  int  int bbs_main()
 bbs_center ()  
254  {  {
255    int ch, result, redraw;          MYSQL *db = NULL;
256    char action[MAX_MENUACTION_LENGTH], temp[256];  
257    time_t t_last_action;          // Set data pools in shared memory readonly
258            if (set_trie_dict_shm_readonly() < 0)
259    BBS_last_access_tm = t_last_action = time (0);          {
260                    goto cleanup;
261    clearscr ();          }
262            if (set_article_block_shm_readonly() < 0)
263    show_top ("");          {
264    show_active_board ();                  goto cleanup;
265    show_bottom ("");          }
266    display_menu (get_menu (&bbs_menu, "TOPMENU"));          if (set_section_list_shm_readonly() < 0)
267            {
268    while (1)                  goto cleanup;
269      {          }
270        ch = igetch ();  
271            // Load menu in shared memory
272        if (time (0) - t_last_action >= 10)          if (set_menu_shm_readonly(p_bbs_menu) < 0)
273          {          {
274            t_last_action = time (0);                  goto cleanup;
275            show_active_board ();          }
276            show_bottom ("");  
277          }          set_input_echo(0);
278    
279        switch (ch)          // System info
280          {          if (bbs_info() < 0)
281          case KEY_NULL:          {
282            return 0;                  goto cleanup;
         case KEY_TIMEOUT:  
           if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)  
             {  
               return -1;  
             }  
           continue;  
         default:  
           strcpy (action, menu_control (&bbs_menu, ch));  
           redraw = 1;  
           switch (get_cmd_value (action))  
             {  
             case EXITBBS:  
               return 0;  
             case BBSNET:  
               bbs_net ();  
               break;  
             case LICENSE:  
               strcpy (temp, app_home_dir);  
               strcat (temp, "data/license.txt");  
               display_file_ex (temp, 0, 1);  
               break;  
             case COPYRIGHT:  
               strcpy (temp, app_home_dir);  
               strcat (temp, "data/copyright.txt");  
               display_file_ex (temp, 0, 1);  
               break;  
             case UNKNOWN_CMD:  
             default:  
               redraw = 0;  
               break;  
             }  
           if (redraw)  
             {  
               clearscr ();  
               show_top ("");  
               show_active_board ();  
               show_bottom ("");  
               display_current_menu (&bbs_menu);  
             }  
283          }          }
       BBS_last_access_tm = time (0);  
     }  
284    
285    return 0;          db = db_open();
286            if (db == NULL)
287            {
288                    prints("无法连接数据库\n");
289                    goto cleanup;
290            }
291    
292            // Welcome
293            if (bbs_welcome(db) < 0)
294            {
295                    goto cleanup;
296            }
297    
298            // User login
299            if (bbs_login(db) < 0)
300            {
301                    goto cleanup;
302            }
303            clearscr();
304    
305            // BBS Top 10
306            display_file(VAR_BBS_TOP, 1, 1);
307    
308            // Main
309            bbs_center();
310    
311            // Logout
312            bbs_logout(db);
313    
314    cleanup:
315            if (db != NULL)
316            {
317                    mysql_close(db);
318            }
319    
320            // Detach menu in shared memory
321            detach_menu_shm(p_bbs_menu);
322            free(p_bbs_menu);
323            p_bbs_menu = NULL;
324    
325            // Detach data pools shm
326            detach_section_list_shm();
327            detach_article_block_shm();
328            detach_trie_dict_shm();
329    
330            return 0;
331  }  }


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

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