/[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.17 by sysadm, Sat Mar 19 08:23:05 2005 UTC Revision 1.96 by sysadm, Sun Nov 2 08:13:50 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 "article_favor.h"
18    #include "article_view_log.h"
19  #include "bbs.h"  #include "bbs.h"
20    #include "bbs_cmd.h"
21    #include "bbs_main.h"
22  #include "common.h"  #include "common.h"
23    #include "database.h"
24    #include "editor.h"
25  #include "io.h"  #include "io.h"
26    #include "log.h"
27    #include "login.h"
28  #include "menu.h"  #include "menu.h"
29    #include "screen.h"
30    #include "section_list.h"
31    #include "section_list_display.h"
32    #include "trie_dict.h"
33    #include "user_list.h"
34    #include "user_priv.h"
35    #include <errno.h>
36    #include <signal.h>
37    #include <stdlib.h>
38    #include <string.h>
39  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
40  #include <unistd.h>  #include <unistd.h>
41    
42  #define ACTIVE_BOARD_HEIGHT     8  int bbs_info()
   
 int  
 bbs_main ()  
43  {  {
44    char temp[256];          prints("欢迎光临 \033[1;33m%s \033[32m[%s]  \033[37m( %s )\033[m\r\n",
45    int ret;                     BBS_name, BBS_server, APP_INFO);
46    
47    set_input_echo (0);          return iflush();
48    }
49    
50    bbs_info ();  int bbs_welcome(void)
51    {
52            int u_online = 0;
53            int u_anonymous = 0;
54            int u_total = 0;
55            int u_login_count = 0;
56    
57    //Welcome          if (get_user_online_list_count(&u_online, &u_anonymous) < 0)
58    bbs_welcome ();          {
59                    log_error("get_user_online_list_count() error\n");
60                    u_online = 0;
61            }
62            u_online += u_anonymous;
63            u_online++; // current user
64            if (BBS_priv.uid == 0)
65            {
66                    u_anonymous++;
67            }
68    
69    //Login          if (get_user_list_count(&u_total) < 0)
70    ret = bbs_login ();          {
71    if (ret < 0)                  log_error("get_user_list_count() error\n");
72      return -1;                  u_total = 0;
73    clearscr ();          }
74    
75            if (get_user_login_count(&u_login_count) < 0)
76            {
77                    log_error("get_user_login_count() error\n");
78                    u_login_count = 0;
79            }
80    
81    //BBS Top 10          // Display logo
82    strcpy (temp, app_home_dir);          display_file(DATA_WELCOME, 2);
   strcat (temp, "data/bbs_top.txt");  
   display_file_ex (temp, 1, 1);  
83    
84    //Main          // Display welcome message
85    bbs_center ();          prints("\r\033[1;35m欢迎光临\033[33m 【 %s 】 \033[35mBBS\r\n"
86                       "\033[32m目前上站人数 [\033[36m%d/%d\033[32m] "
87                       "匿名游客[\033[36m%d\033[32m] "
88                       "注册用户数[\033[36m%d/%d\033[32m]\r\n"
89                       "从 [\033[36m%s\033[32m] 起,累计访问人次:[\033[36m%d\033[32m]\033[m\r\n",
90                       BBS_name, u_online, BBS_max_client, u_anonymous, u_total,
91                       BBS_max_user_count, BBS_start_dt, u_login_count);
92    
93    //Logout          iflush();
   bbs_exit ();  
94    
95    return 0;          return 0;
96  }  }
97    
98  int  int bbs_logout(void)
 bbs_info ()  
99  {  {
100    prints ("ӭ \033[1;33m%s \033[32m[%s]  \033[37m( %s )\r\n",          MYSQL *db;
           BBS_name, BBS_server, app_version);  
101    
102    iflush ();          db = db_open();
103            if (db == NULL)
104            {
105                    return -1;
106            }
107    
108    return 0;          if (user_online_exp(db) < 0)
109  }          {
110                    return -2;
111            }
112    
113  int          if (user_online_del(db) < 0)
114  bbs_exit ()          {
115  {                  return -3;
116    char temp[256];          }
117    
118    strcpy (temp, app_home_dir);          mysql_close(db);
   strcat (temp, "data/goodbye.txt");  
   display_file_ex (temp, 1, 0);  
119    
120    sleep (1);          display_file(DATA_GOODBYE, 1);
121    
122    return 0;          log_common("User [%s] logout, idle for %ld seconds since last input\n", BBS_username, time(NULL) - BBS_last_access_tm);
123    
124            return 0;
125  }  }
126    
127  int  int bbs_center()
 bbs_center ()  
128  {  {
129    int ch, result;          int ch;
130    char key;          time_t t_last_action;
131    time_t t_last_action;  
132    fd_set inputs, testfds;          BBS_last_access_tm = t_last_action = time(NULL);
   struct timeval timeout;  
133    
134    FD_ZERO (&inputs);          clearscr();
   FD_SET (0, &inputs);  
135    
136    t_last_action = time (0);          show_top("", BBS_name, "");
137            show_active_board();
138            show_bottom("");
139            display_menu(&bbs_menu);
140            iflush();
141    
142    clearscr ();          while (!SYS_server_exit)
143            {
144                    ch = igetch(100);
145    
146    show_top ("");                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
147    show_active_board ();                  {
148    display_menu (&bbs_menu);                          BBS_last_access_tm = time(NULL);
149    show_bottom ("");                  }
150    
151    while (1)                  if (bbs_menu.choose_step == 0 && time(NULL) - t_last_action >= 10)
152      {                  {
153        testfds = inputs;                          t_last_action = time(NULL);
       timeout.tv_sec = 0;  
       timeout.tv_usec = 100000;  
154    
155        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,                          show_active_board();
156                         (fd_set *) NULL, &timeout);                          show_bottom("");
157                            display_menu_cursor(&bbs_menu, 1);
158                            iflush();
159                    }
160    
161        switch (result)                  if (user_online_update("MENU") < 0)
162          {                  {
163          case 0:                          log_error("user_online_update(MENU) error\n");
164            break;                  }
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               ch = igetch ();  
165    
166                switch (ch)                  switch (ch)
167                  {                  {
168                  case KEY_LEFT:                  case KEY_NULL: // broken pipe
169                    ch = 'G';                          log_error("KEY_NULL\n");
170                            return 0;
171                    case KEY_TIMEOUT:
172                            if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME)
173                            {
174                                    log_error("User input timeout\n");
175                                    return 0;
176                            }
177                            continue;
178                    case CR:
179                  default:                  default:
180                    key = menu_control (&bbs_menu, ch);                          switch (menu_control(&bbs_menu, ch))
181                    switch (key)                          {
182                      {                          case EXITBBS:
183                      case 'G':                          case EXITMENU:
184                        return 0;                                  return 0;
185                      }                          case REDRAW:
186                                    t_last_action = time(NULL);
187                                    clearscr();
188                                    show_top("", BBS_name, "");
189                                    show_active_board();
190                                    show_bottom("");
191                                    display_menu(&bbs_menu);
192                                    break;
193                            case NOREDRAW:
194                            case UNKNOWN_CMD:
195                            default:
196                                    break;
197                            }
198                            iflush();
199                  }                  }
             }  
           break;  
200          }          }
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
           show_active_board ();  
         }  
     }  
201    
202    return 0;          return 0;
203  }  }
204    
205  int  int bbs_charset_select()
 show_top (char *status)  
206  {  {
207    char buffer[256];          char msg[LINE_BUFFER_LEN];
208            int ch;
209    
210    str_space (buffer, 20 - strlen (BBS_current_section_name));          snprintf(msg, sizeof(msg),
211                             "\rChoose character set in 5 seconds [UTF-8, GBK]: [U/g]");
212    
213    moveto (1, 0);          while (!SYS_server_exit)
214    prints ("\033[1;44;33m%-20s \033[37m%20s"          {
215            "         %s\033[33m [%s]\033[m",                  ch = press_any_key_ex(msg, 5);
216            status, BBS_name, buffer, BBS_current_section_name);                  switch (ch)
217    iflush ();                  {
218                    case KEY_NULL:
219                            return -1;
220                    case KEY_TIMEOUT:
221                    case CR:
222                    case 'u':
223                    case 'U':
224                            return 0;
225                    case 'g':
226                    case 'G':
227                            if (io_conv_init("GBK") < 0)
228                            {
229                                    log_error("io_conv_init(%s) error\n", "GBK");
230                                    return -1;
231                            }
232                            return 0;
233                    default:
234                            continue;
235                    }
236            }
237    
238    return 0;          return 0;
239  }  }
240    
241  int  int bbs_main()
 show_bottom (char *msg)  
242  {  {
243    char str_time[256], str_time_onine[20], buffer[256];          struct sigaction act = {0};
244    time_t time_online;          char msg[LINE_BUFFER_LEN];
   struct tm *tm_online;  
245    
246    get_time_str (str_time, 256);          // Set signal handler
247    str_space (buffer, 33 - strlen (BBS_username));          act.sa_handler = SIG_IGN;
248            if (sigaction(SIGHUP, &act, NULL) == -1)
249            {
250                    log_error("set signal action of SIGHUP error: %d\n", errno);
251                    goto cleanup;
252            }
253            act.sa_handler = SIG_DFL;
254            if (sigaction(SIGCHLD, &act, NULL) == -1)
255            {
256                    log_error("set signal action of SIGCHLD error: %d\n", errno);
257                    goto cleanup;
258            }
259    
260    time_online = time (0) - BBS_login_tm;          // Set data pools in shared memory readonly
261    tm_online = gmtime (&time_online);          if (set_trie_dict_shm_readonly() < 0)
262            {
263                    goto cleanup;
264            }
265            if (set_article_block_shm_readonly() < 0)
266            {
267                    goto cleanup;
268            }
269            if (set_section_list_shm_readonly() < 0)
270            {
271                    goto cleanup;
272            }
273            if (set_user_list_pool_shm_readonly() < 0)
274            {
275                    goto cleanup;
276            }
277    
278    moveto (screen_lines, 0);          // Load menu in shared memory
279    prints ("\033[1;44;33m[\033[36m%s\033[33m]"          if (set_menu_shm_readonly(&bbs_menu) < 0)
280            "%sʺ[\033[36m%s\033[33m]"          {
281            "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                  goto cleanup;
282            str_time, buffer, BBS_username, tm_online->tm_mday - 1,          }
           tm_online->tm_hour, tm_online->tm_min);  
   iflush ();  
283    
284    return 0;          // Set default charset
285  }          if (io_conv_init(BBS_DEFAULT_CHARSET) < 0)
286            {
287                    log_error("io_conv_init(%s) error\n", BBS_DEFAULT_CHARSET);
288                    goto cleanup;
289            }
290    
291  int          set_input_echo(0);
 show_active_board ()  
 {  
   char filename[256], buffer[260];  
   FILE *fin;  
   int i, j;  
   static long int line;  
292    
293    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          // Set user charset
294            bbs_charset_select();
295    
296    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          // System info
297            if (bbs_info() < 0)
298            {
299                    goto cleanup;
300            }
301    
302    moveto (3, 0);          // Welcome
303            if (bbs_welcome() < 0)
304            {
305                    goto cleanup;
306            }
307    
308    if ((fin = fopen (filename, "r")) != NULL)          // User login
309      {          if (SSH_v2)
310        for (j = 0; j < line; j++)          {
311                    snprintf(msg, sizeof(msg), "\033[1m%s 欢迎使用ssh方式访问 \033[1;33m按任意键继续...\033[m", BBS_username);
312                    press_any_key_ex(msg, 60);
313            }
314            else if (bbs_login() < 0)
315          {          {
316            if (fgets (buffer, 255, fin) == NULL)                  goto cleanup;
             {  
               line = 0;  
               rewind (fin);  
               break;  
             }  
317          }          }
318            log_common("User [%s] login\n", BBS_username);
319    
320        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          // Load section aid locations
321            if (section_aid_locations_load(BBS_priv.uid) < 0)
322          {          {
323            if (fgets (buffer, 255, fin) == NULL)                  log_error("article_view_log_load() error\n");
324              {                  goto cleanup;
               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 ();  
325          }          }
326        fclose (fin);          
327      }          // Load article view log
328            if (article_view_log_load(BBS_priv.uid, &BBS_article_view_log, 0) < 0)
329            {
330                    log_error("article_view_log_load() error\n");
331                    goto cleanup;
332            }
333    
334            // Load article favorite
335            if (article_favor_load(BBS_priv.uid, &BBS_article_favor, 0) < 0)
336            {
337                    log_error("article_favor_load() error\n");
338                    goto cleanup;
339            }
340    
341            // Init editor memory pool
342            if (editor_memory_pool_init() < 0)
343            {
344                    log_error("editor_memory_pool_init() error\n");
345                    goto cleanup;
346            }
347    
348            clearscr();
349    
350            // BBS Top 10
351            display_file(VAR_BBS_TOP, 1);
352    
353            // Main
354            bbs_center();
355    
356            // Logout
357            bbs_logout();
358    
359            // Save section aid locations
360            if (section_aid_locations_save(BBS_priv.uid) < 0)
361            {
362                    log_error("article_view_log_save() error\n");
363            }
364    
365            // Save incremental article view log
366            if (article_view_log_save_inc(&BBS_article_view_log) < 0)
367            {
368                    log_error("article_view_log_save_inc() error\n");
369            }
370    
371            // Save incremental article favorite
372            if (article_favor_save_inc(&BBS_article_favor) < 0)
373            {
374                    log_error("article_favor_save_inc() error\n");
375            }
376    
377    cleanup:
378            // Cleanup iconv
379            io_conv_cleanup();
380    
381            // Cleanup editor memory pool
382            editor_memory_pool_cleanup();
383    
384            // Unload article view log
385            article_view_log_unload(&BBS_article_view_log);
386    
387            // Unload article favor
388            article_favor_unload(&BBS_article_favor);
389    
390            // Detach menu in shared memory
391            detach_menu_shm(&bbs_menu);
392            detach_menu_shm(&top10_menu);
393    
394            // Detach data pools shm
395            detach_user_list_pool_shm();
396            detach_section_list_shm();
397            detach_article_block_shm();
398            detach_trie_dict_shm();
399    
400    return 0;          return 0;
401  }  }


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

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