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


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

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