/[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.6 by sysadm, Thu Oct 21 17:28:46 2004 UTC Revision 1.68 by sysadm, Sun Jun 8 09:25:24 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 "article_view_log.h"
24    #include "log.h"
25    #include "io.h"
26    #include "screen.h"
27    #include "menu.h"
28    #include "bbs_cmd.h"
29    #include "section_list.h"
30    #include "trie_dict.h"
31    #include <unistd.h>
32    #include <time.h>
33    #include <string.h>
34    #include <stdlib.h>
35    
36  int  int bbs_info()
 bbs_main()  
37  {  {
38    int ret;          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",
39                         BBS_name, BBS_server, APP_NAME_VER);
   bbs_info();  
   
   bbs_welcome();  
   
   ret = bbs_login();  
   if (ret < 0)  
     return -1;  
     
   bbs_exit();  
40    
41    return 0;          return iflush();
42  }  }
43    
44  int  int bbs_welcome(void)
 bbs_info()  
45  {  {
46    printf(          char sql[SQL_BUFFER_LEN];
     "\033[1;37m欢迎光临 \033[33m%s \033[32m[%s]  \033[37m( %s )\r\n",  
     BBS_name, BBS_server, app_version);  
       
   fflush(stdout);  
47    
48    return 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            db = db_open();
58            if (db == NULL)
59            {
60                    return -1;
61            }
62    
63            snprintf(sql, sizeof(sql),
64                             "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            snprintf(sql, sizeof(sql),
87                             "SELECT COUNT(*) AS cc FROM "
88                             "(SELECT DISTINCT SID FROM user_online "
89                             "WHERE UID = 0 AND last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND)) AS t1",
90                             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            snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
110            if (mysql_query(db, sql) != 0)
111            {
112                    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            mysql_close(db);
148    
149            // Count current user before login
150            u_online++;
151            u_anonymous++;
152    
153            // Display logo
154            display_file(DATA_WELCOME, 2);
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] 起,累计访问人次:[\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();
166    
167            return 0;
168    }
169    
170    int bbs_logout(void)
171    {
172            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            display_file(DATA_GOODBYE, 1);
188    
189            log_common("User logout\n");
190    
191            return 0;
192    }
193    
194    int bbs_center()
195    {
196            int ch;
197            time_t t_last_action;
198    
199            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                            t_last_action = time(0);
216    
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:
236                            switch (menu_control(p_bbs_menu, ch))
237                            {
238                            case EXITBBS:
239                                    return 0;
240                            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                    BBS_last_access_tm = time(0);
257            }
258    
259            return 0;
260  }  }
261    
262  int  int bbs_main()
 bbs_exit()  
263  {  {
264    char buffer[1024];          // Set data pools in shared memory readonly
265              if (set_trie_dict_shm_readonly() < 0)
266    sprintf(buffer,          {
267      "\033[0;37m\r\n");                  goto cleanup;
268            }
269    s_send (socket_client, buffer);          if (set_article_block_shm_readonly() < 0)
270              {
271    return 0;                  goto cleanup;
272            }
273            if (set_section_list_shm_readonly() < 0)
274            {
275                    goto cleanup;
276            }
277    
278            // Load menu in shared memory
279            if (set_menu_shm_readonly(p_bbs_menu) < 0)
280            {
281                    goto cleanup;
282            }
283    
284            set_input_echo(0);
285    
286            // System info
287            if (bbs_info() < 0)
288            {
289                    goto cleanup;
290            }
291    
292            // Welcome
293            if (bbs_welcome() < 0)
294            {
295                    goto cleanup;
296            }
297    
298            // 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            // Load article view log
311            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
312            {
313                    log_error("article_view_log_load() error\n");
314                    goto cleanup;
315            }
316    
317            clearscr();
318    
319            // BBS Top 10
320            display_file(VAR_BBS_TOP, 1);
321    
322            // Main
323            bbs_center();
324    
325            // Logout
326            bbs_logout();
327    
328            // Save incremental article view log
329            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
330            {
331                    log_error("article_view_log_save_inc() error\n");
332            }
333    
334    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