/[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.31 by sysadm, Mon Apr 28 03:30:59 2025 UTC Revision 1.65 by sysadm, Sat Jun 7 08:33:08 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 bbs_main()  int bbs_info()
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_NAME_VER);
39    
40          set_input_echo(0);          return iflush();
41    }
42    
43          bbs_info();  int bbs_welcome(void)
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 *db;
54            MYSQL_RES *rs;
55            MYSQL_ROW row;
56    
57          // Login          db = db_open();
58          ret = bbs_login();          if (db == NULL)
59          if (ret < 0)          {
60                  return -1;                  return -1;
61          log_std("User \"%s\"(%ld) login from %s:%d\n",          }
                         BBS_username, BBS_priv.uid, hostaddr_client, port_client);  
         clearscr();  
62    
63          // BBS Top 10          snprintf(sql, sizeof(sql),
64          strcpy(temp, app_home_dir);                           "SELECT COUNT(*) AS cc FROM "
65          strcat(temp, "data/bbs_top.txt");                           "(SELECT DISTINCT SID FROM user_online "
66          display_file_ex(temp, 1, 1);                           "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          // Main          snprintf(sql, sizeof(sql),
87          bbs_center();                           "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          // Logout          snprintf(sql, sizeof(sql), "SELECT COUNT(UID) AS cc FROM user_list WHERE enable");
110          bbs_exit();          if (mysql_query(db, sql) != 0)
111          log_std("User logout\n");          {
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          return 0;          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  int bbs_info()          mysql_close(db);
148  {  
149          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          // Log max user_online
150                     BBS_name, BBS_server, app_version);          FILE *fin, *fout;
151            if ((fin = fopen(VAR_MAX_USER_ONLINE, "r")) != NULL)
152            {
153                    fscanf(fin, "%d", &max_u_online);
154                    fclose(fin);
155            }
156            if (u_online > max_u_online)
157            {
158                    max_u_online = u_online;
159                    if ((fout = fopen(VAR_MAX_USER_ONLINE, "w")) == NULL)
160                    {
161                            log_error("Open max_user_online.dat failed\n");
162                            return -3;
163                    }
164                    fprintf(fout, "%d\n", max_u_online);
165                    fclose(fout);
166            }
167    
168            // Count current user before login
169            u_online++;
170            u_anonymous++;
171    
172            // Display logo
173            display_file(DATA_WELCOME, 1, 2);
174    
175            // Display welcome message
176            prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
177                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
178                       "匿名游客[\033[36m%d\033[32m] "
179                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
180                       "从 [\033[36m%s\033[32m] 起,最高人数记录:"
181                       "[\033[36m%d\033[32m],累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
182                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
183                       BBS_max_user, BBS_start_dt, max_u_online, u_login_count);
184    
185          iflush();          iflush();
186    
187          return 0;          return 0;
188  }  }
189    
190  int bbs_exit()  int bbs_logout(void)
191  {  {
192          char temp[256];          MYSQL *db;
193    
194          strcpy(temp, app_home_dir);          db = db_open();
195          strcat(temp, "data/goodbye.txt");          if (db == NULL)
196          display_file_ex(temp, 1, 0);          {
197                    return -1;
198            }
199    
200          sleep(1);          if (user_online_del(db) < 0)
201            {
202                    return -2;
203            }
204    
205            mysql_close(db);
206    
207            display_file(DATA_GOODBYE, 1, 1);
208    
209            log_common("User logout\n");
210    
211          return 0;          return 0;
212  }  }
213    
214  int bbs_center()  int bbs_center()
215  {  {
216          int ch, result, redraw;          int ch;
         char temp[256];  
217          time_t t_last_action;          time_t t_last_action;
218    
219          BBS_last_access_tm = t_last_action = time(0);          BBS_last_access_tm = t_last_action = time(0);
220    
221          clearscr();          clearscr();
222    
223          show_top("");          show_top("", BBS_name, "");
224          show_active_board();          show_active_board();
225          show_bottom("");          show_bottom("");
226          display_menu(get_menu(&bbs_menu, "TOPMENU"));          display_menu(p_bbs_menu);
227            iflush();
228    
229          while (!SYS_exit)          while (!SYS_server_exit)
230          {          {
231                  ch = igetch();                  ch = igetch(100);
232    
233                  if (time(0) - t_last_action >= 10)                  if (p_bbs_menu->choose_step == 0 && time(0) - t_last_action >= 10)
234                  {                  {
235                          t_last_action = time(0);                          t_last_action = time(0);
236    
237                          show_active_board();                          show_active_board();
238                          show_bottom("");                          show_bottom("");
239                            display_menu_cursor(p_bbs_menu, 1);
240                            iflush();
241                  }                  }
242    
243                  switch (ch)                  switch (ch)
244                  {                  {
245                  case KEY_NULL:                  case KEY_NULL: // broken pipe
246                          return 0;                          return 0;
247                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
248                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
249                          {                          {
250                                  return -1;                                  return 0;
251                          }                          }
252                          continue;                          continue;
253                    case CR:
254                            igetch_reset();
255                  default:                  default:
256                          redraw = 1;                          switch (menu_control(p_bbs_menu, ch))
                         switch (menu_control(&bbs_menu, ch))  
257                          {                          {
258                          case EXITBBS:                          case EXITBBS:
259                                  return 0;                                  return 0;
260                          case REDRAW:                          case REDRAW:
261                                    t_last_action = time(0);
262                                    clearscr();
263                                    show_top("", BBS_name, "");
264                                    show_active_board();
265                                    show_bottom("");
266                                    display_menu(p_bbs_menu);
267                                  break;                                  break;
268                          case NOREDRAW:                          case NOREDRAW:
269                          case UNKNOWN_CMD:                          case UNKNOWN_CMD:
270                          default:                          default:
                                 redraw = 0;  
271                                  break;                                  break;
272                          }                          }
273                          if (redraw)                          iflush();
                         {  
                                 clearscr();  
                                 show_top("");  
                                 show_active_board();  
                                 show_bottom("");  
                                 display_current_menu(&bbs_menu);  
                         }  
274                  }                  }
275    
276                  BBS_last_access_tm = time(0);                  BBS_last_access_tm = time(0);
277          }          }
278    
279          return 0;          return 0;
280  }  }
281    
282    int bbs_main()
283    {
284            // Set data pools in shared memory readonly
285            if (set_trie_dict_shm_readonly() < 0)
286            {
287                    goto cleanup;
288            }
289            if (set_article_block_shm_readonly() < 0)
290            {
291                    goto cleanup;
292            }
293            if (set_section_list_shm_readonly() < 0)
294            {
295                    goto cleanup;
296            }
297    
298            // Load menu in shared memory
299            if (set_menu_shm_readonly(p_bbs_menu) < 0)
300            {
301                    goto cleanup;
302            }
303    
304            set_input_echo(0);
305    
306            // System info
307            if (bbs_info() < 0)
308            {
309                    goto cleanup;
310            }
311    
312            // Welcome
313            if (bbs_welcome() < 0)
314            {
315                    goto cleanup;
316            }
317    
318            // User login
319            if (SSH_v2)
320            {
321                    prints("\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
322                    iflush();
323                    igetch_t(MAX_DELAY_TIME);
324            }
325            else if (bbs_login() < 0)
326            {
327                    goto cleanup;
328            }
329    
330            // Load article_view_log
331    
332            clearscr();
333    
334            // BBS Top 10
335            display_file(VAR_BBS_TOP, 1, 1);
336    
337            // Main
338            bbs_center();
339    
340            // Logout
341            bbs_logout();
342    
343    cleanup:
344            // Detach menu in shared memory
345            detach_menu_shm(p_bbs_menu);
346            free(p_bbs_menu);
347            p_bbs_menu = NULL;
348    
349            // Detach data pools shm
350            detach_section_list_shm();
351            detach_article_block_shm();
352            detach_trie_dict_shm();
353    
354            return 0;
355    }


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

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