/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.4 by sysadm, Mon Mar 21 17:52:09 2005 UTC Revision 1.29 by sysadm, Sat May 10 11:09:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            bbs_net.c  -  description                                                    bbs_net.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.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
19    #include "log.h"
20  #include "io.h"  #include "io.h"
21    #include "screen.h"
22  #include "menu.h"  #include "menu.h"
 #include "tcplib.h"  
23  #include <stdio.h>  #include <stdio.h>
24  #include <stdarg.h>  #include <stdarg.h>
25    #include <errno.h>
26    #include <string.h>
27    #include <stdlib.h>
28    #include <fcntl.h>
29    #include <time.h>
30    #include <unistd.h>
31    #include <netdb.h>
32    #include <sys/select.h>
33  #include <sys/ioctl.h>  #include <sys/ioctl.h>
34  #include <sys/socket.h>  #include <sys/socket.h>
35  #include <netinet/in.h>  #include <netinet/in.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <time.h>  
 #include <unistd.h>  
 #include <netdb.h>  
37    
38  #define TIME_OUT                15  #define MENU_CONF_DELIM " \t\r\n"
39  #define MAX_PROCESS_BAR_LEN     30  
40  #define MAXSTATION              26*2  #define MAX_PROCESS_BAR_LEN 30
41  #define STATION_PER_LINE        4  #define MAXSTATION 26 * 2
42    #define STATION_PER_LINE 4
43    
44  struct _bbsnet_conf  struct _bbsnet_conf
45  {  {
46    char host1[20];          char host1[20];
47    char host2[40];          char host2[40];
48    char ip[40];          char ip[40];
49    int port;          in_port_t port;
50  } bbsnet_conf[MAXSTATION];  } bbsnet_conf[MAXSTATION];
51    
52  MENU_SET bbsnet_menu;  MENU_SET bbsnet_menu;
53    
54  int  int load_bbsnet_conf(const char *file_config)
 load_bbsnet_conf (const char *file_config)  
55  {  {
56    FILE *fp;          FILE *fp;
57    MENU *p_menu;          MENU *p_menu;
58    MENU_ITEM *p_menuitem;          MENU_ITEM *p_menuitem;
59    char t[256], *t1, *t2, *t3, *t4;          char t[256], *t1, *t2, *t3, *t4, *saveptr;
60    int item_count = 0;          int item_count = 0;
   
   fp = fopen (file_config, "r");  
   if (fp == NULL)  
     return -1;  
   
   p_menu = bbsnet_menu.p_menu[0] = malloc (sizeof (MENU));  
   strcpy (p_menu->name, "BBSNET");  
   p_menu->title.show = 0;  
   p_menu->screen.show = 0;  
   
   while (fgets (t, 255, fp) && item_count < MAXSTATION)  
     {  
       t1 = strtok (t, " \t");  
       t2 = strtok (NULL, " \t\n");  
       t3 = strtok (NULL, " \t\n");  
       t4 = strtok (NULL, " \t\n");  
   
       if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL  
           || t3 == NULL)  
         continue;  
       strncpy (bbsnet_conf[item_count].host1, t2, 18);  
       bbsnet_conf[item_count].host1[18] = 0;  
       strncpy (bbsnet_conf[item_count].host2, t1, 36);  
       bbsnet_conf[item_count].host2[36] = 0;  
       strncpy (bbsnet_conf[item_count].ip, t3, 36);  
       bbsnet_conf[item_count].ip[36] = 0;  
       bbsnet_conf[item_count].port = t4 ? atoi (t4) : 23;  
   
       p_menuitem = p_menu->items[item_count] = malloc (sizeof (MENU_ITEM));  
       p_menuitem->row = 2 + item_count / STATION_PER_LINE;  
       p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;  
       sprintf (p_menuitem->action, "%d", item_count);  
       p_menuitem->submenu = 0;  
       p_menuitem->priv = 0;  
       p_menuitem->level = 0;  
       p_menuitem->display = 0;  
       p_menuitem->name[0] =  
         (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);  
       p_menuitem->name[1] = '\0';  
       sprintf (p_menuitem->text, "1;36m%c. %s",  
                p_menuitem->name[0], bbsnet_conf[item_count].host1);  
   
       item_count++;  
     }  
   fclose (fp);  
   
   p_menu->item_count = item_count;  
   p_menu->item_cur_pos = 0;  
   
   bbsnet_menu.menu_count = 1;  
   bbsnet_menu.menu_select_depth = 0;  
   bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0];  
61    
62    return 0;          fp = fopen(file_config, "r");
63  }          if (fp == NULL)
64            {
65                    return -1;
66            }
67    
68  // from Maple-hightman          p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU));
69  // added by flyriver, 2001.3.3          strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1);
70  int          p_menu->name[sizeof(p_menu->name) - 1] = '\0';
71  telnetopt (int fd, char *buf, int max)          p_menu->title.show = 0;
72  {          p_menu->screen.show = 0;
73    unsigned char c, d, e;  
74    int pp = 0;          while (fgets(t, 255, fp) && item_count < MAXSTATION)
75    unsigned char tmp[30];          {
76                    t1 = strtok_r(t, MENU_CONF_DELIM, &saveptr);
77    while (pp < max)                  t2 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
78      {                  t3 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
79        c = buf[pp++];                  t4 = strtok_r(NULL, MENU_CONF_DELIM, &saveptr);
80        if (c == 255)  
81          {                  if (t1 == NULL || t2 == NULL || t3 == NULL || t4 == NULL || t[0] == '#' || t[0] == '*')
82            d = buf[pp++];                  {
83            e = buf[pp++];                          continue;
84            iflush ();                  }
85            if ((d == 253) && (e == 3 || e == 24))  
86              {                  strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1);
87                tmp[0] = 255;                  bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0';
88                tmp[1] = 251;                  strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1);
89                tmp[2] = e;                  bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0';
90                write (fd, tmp, 3);                  strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1);
91                continue;                  bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0';
92              }                  bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23);
93            if ((d == 251 || d == 252) && (e == 1 || e == 3 || e == 24))  
94              {                  p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM));
95                tmp[0] = 255;                  p_menuitem->row = 2 + item_count / STATION_PER_LINE;
96                tmp[1] = 253;                  p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;
97                tmp[2] = e;                  snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count);
98                write (fd, tmp, 3);                  p_menuitem->submenu = 0;
99                continue;                  p_menuitem->priv = 0;
100              }                  p_menuitem->level = 0;
101            if (d == 251 || d == 252)                  p_menuitem->display = 0;
102              {                  p_menuitem->name[0] =
103                tmp[0] = 255;                          (char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);
104                tmp[1] = 254;                  p_menuitem->name[1] = '\0';
105                tmp[2] = e;                  snprintf(p_menuitem->text, sizeof(p_menuitem->text), "%c. %s",
106                write (fd, tmp, 3);                                   p_menuitem->name[0], bbsnet_conf[item_count].host1);
107                continue;  
108              }                  item_count++;
109            if (d == 253 || d == 254)          }
110              {          fclose(fp);
111                tmp[0] = 255;  
112                tmp[1] = 252;          p_menu->item_count = item_count;
113                tmp[2] = e;          p_menu->item_cur_pos = 0;
114                write (fd, tmp, 3);  
115                continue;          bbsnet_menu.menu_count = 1;
116              }          bbsnet_menu.menu_select_depth = 0;
117            if (d == 250)          bbsnet_menu.p_menu_select[0] = bbsnet_menu.p_menu[0];
118              {  
119                while (e != 240 && pp < max)          return 0;
                 e = buf[pp++];  
               tmp[0] = 255;  
               tmp[1] = 250;  
               tmp[2] = 24;  
               tmp[3] = 0;  
               tmp[4] = 65;  
               tmp[5] = 78;  
               tmp[6] = 83;  
               tmp[7] = 73;  
               tmp[8] = 255;  
               tmp[9] = 240;  
               write (fd, tmp, 10);  
             }  
         }  
       else  
         outc (c);  
     }  
   iflush ();  
   return 0;  
120  }  }
121    
122  static void  static void process_bar(int n, int len)
 process_bar (int n, int len)  
123  {  {
124    char buf[256];          char buf[LINE_BUFFER_LEN];
125    char buf2[256];          char buf2[LINE_BUFFER_LEN];
126    char *ptr;  
127    char *ptr2;          if (len > LINE_BUFFER_LEN)
128    char *ptr3;          {
129                    len = LINE_BUFFER_LEN - 1;
130    moveto (4, 0);          }
131    prints ("┌───────────────┐\n");          if (n < 0)
132    sprintf (buf2, "            %3d%%              ", n * 100 / len);          {
133    ptr = buf;                  n = 0;
134    ptr2 = buf2;          }
135    ptr3 = buf + n;          else if (n > len)
136    while (ptr != ptr3)          {
137      *ptr++ = *ptr2++;                  n = len;
138    *ptr++ = '\x1b';          }
139    *ptr++ = '[';  
140    *ptr++ = '4';          moveto(4, 0);
141    *ptr++ = '4';          prints(" ------------------------------ \r\n");
142    *ptr++ = 'm';          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
143    while (*ptr2 != '\0')          strncpy(buf2, buf, (size_t) n);
144      *ptr++ = *ptr2++;          buf2[n] = '\0';
145    *ptr++ = '\0';          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
146    prints ("│\033[46m%s\033[m│\n", buf);          prints(" ------------------------------ \r\n");
147    prints ("└───────────────┘\n");          iflush();
148  }  }
149    
150  int  int bbsnet_connect(int n)
 bbsnet_connect (int n)  
151  {  {
152    int sock, ch, result, len, loop;          int sock, flags, ret, loop, error;
153    struct sockaddr_in sin;          ssize_t len;
154    char buf[256];          struct sockaddr_in sin;
155    time_t now, t_last_action;          char buf[LINE_BUFFER_LEN];
156    fd_set inputs, testfds;          fd_set read_fds;
157    struct timeval timeout;          fd_set write_fds;
158    struct hostent *pHost = NULL;          struct timeval timeout;
159    int rc, rv, tos = 020, i;          struct hostent *p_host = NULL;
160            int tos = 020, i;
161    prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",          char remote_addr[IP_ADDR_LEN];
162            bbsnet_conf[n].host1, bbsnet_conf[n].ip);          int remote_port;
163    prints ("\033[1;32m如果在 %d 秒内无法连上,穿梭程序将放弃连接。\033[m\n",          time_t t_used;
164            TIME_OUT);          struct tm *tm_used;
165            int ch;
166    pHost = gethostbyname (bbsnet_conf[n].ip);  
167            clearscr();
168    if (pHost == NULL)  
169      {          moveto(0, 0);
170        prints ("\033[1;31m查找主机名失败!\033[m\n");          prints("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\r\n",
171        press_any_key ();                     bbsnet_conf[n].host1, bbsnet_conf[n].ip);
172        return -1;          iflush();
173      }  
174            p_host = gethostbyname(bbsnet_conf[n].ip);
175    sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);  
176            if (p_host == NULL)
177    if (sock < 0)          {
178      {                  prints("\033[1;31m查找主机名失败!\033[m\r\n");
179        prints ("\033[1;31m无法创建socket!\033[m\n");                  press_any_key();
180        press_any_key ();                  return -1;
181        return -1;          }
182      }  
183            sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
184    sin.sin_family = AF_INET;  
185    sin.sin_addr.s_addr =          if (sock < 0)
186      (strlen (hostaddr_server) > 0 ? inet_addr (hostaddr_server) : INADDR_ANY);          {
187    sin.sin_port = 0;                  prints("\033[1;31m无法创建socket!\033[m\r\n");
188                    press_any_key();
189    if (bind (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)                  return -1;
190      {          }
191        log_error ("Bind address %s:%u failed\n",  
192                   inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));          sin.sin_family = AF_INET;
193        return -2;          sin.sin_addr.s_addr =
194      }                  (strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY);
195            sin.sin_port = 0;
196    bzero (&sin, sizeof (sin));  
197    sin.sin_family = AF_INET;          if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
198    sin.sin_port = htons (bbsnet_conf[n].port);          {
199    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];                  log_error("Bind address %s:%u failed\n",
200                                      inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
201                    return -2;
202    prints ("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");          }
203    process_bar (0, MAX_PROCESS_BAR_LEN);  
204    for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          bzero(&sin, sizeof(sin));
205      {          sin.sin_family = AF_INET;
206        if (i == 0)          sin.sin_addr = *(struct in_addr *)p_host->h_addr_list[0];
207          rv =          sin.sin_port = htons(bbsnet_conf[n].port);
208            NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin),  
209                               500, 1);          strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1);
210        else          remote_addr[sizeof(remote_addr) - 1] = '\0';
211          rv =          remote_port = ntohs(sin.sin_port);
212            NonBlockConnectEx (sock, (struct sockaddr *) &sin, sizeof (sin),  
213                               500, 0);          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
214        if (rv == ERR_TCPLIB_TIMEOUT)          process_bar(0, MAX_PROCESS_BAR_LEN);
215          {  
216            process_bar (i + 1, MAX_PROCESS_BAR_LEN);          // Set socket as non-blocking
217            continue;          flags = fcntl(sock, F_GETFL, 0);
218          }          fcntl(sock, F_SETFL, flags | O_NONBLOCK);
219        else if (rv == 0)  
220          break;          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
221        else          {
222          {                  if (errno != EINPROGRESS)
           prints ("\033[1;31m连接失败!\033[m\n");  
           press_any_key ();  
           return -1;  
         }  
     }  
   if (i == MAX_PROCESS_BAR_LEN)  
     {  
       prints ("\033[1;31m连接超时!\033[m\n");  
       press_any_key ();  
       return -1;  
     }  
   setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));  
   prints ("\033[1;31m连接成功!\033[m\n");  
   
   FD_ZERO (&inputs);  
   FD_SET (0, &inputs);  
   FD_SET (sock, &inputs);  
   
   t_last_action = time (0);  
   
   loop = 1;  
   
   while (loop)  
     {  
       testfds = inputs;  
       timeout.tv_sec = 0;  
       timeout.tv_usec = 100000;  
   
       result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,  
                        (fd_set *) NULL, &timeout);  
   
       switch (result)  
         {  
         case 0:  
           break;  
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               len = read (0, buf, 255);  
               if (len == 0)  
223                  {                  {
224                    loop = 0;                          prints("\033[1;31m连接失败!\033[m\r\n");
225                    break;                          press_any_key();
226                            return -1;
227                  }                  }
228                write (sock, buf, len);          }
229              }  
230            if (FD_ISSET (sock, &testfds))          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
231              {          {
232                len = read (sock, buf, 255);                  ch = igetch(0); // 0.1 second
233                if (len == 0)                  if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit)
234                    {
235                            return 0;
236                    }
237    
238                    FD_ZERO(&read_fds);
239                    FD_SET(sock, &read_fds);
240    
241                    FD_ZERO(&write_fds);
242                    FD_SET(sock, &write_fds);
243    
244                    timeout.tv_sec = 0;
245                    timeout.tv_usec = 400 * 1000; // 0.4 second
246            
247                    ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
248    
249                    if (ret == 0) // Timeout
250                    {
251                            process_bar(i + 1, MAX_PROCESS_BAR_LEN);
252                    }
253                    else if (ret < 0)
254                    {
255                            if (errno != EINTR)
256                            {
257                                    log_error("select() error (%d) !\n", errno);
258                                    return -1;
259                            }
260                    }
261                    // ret > 0
262                    else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds))
263                    {
264                            len = sizeof(error);
265                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
266                            {
267                                    log_error("getsockopt() error (%d) !\n", error);
268                                    return -1;
269                            }
270    
271                            break; // connected
272                    }
273            }
274            if (i == MAX_PROCESS_BAR_LEN)
275            {
276                    prints("\033[1;31m连接超时!\033[m\r\n");
277                    press_any_key();
278                    return -1;
279            }
280    
281            fcntl(sock, F_SETFL, flags); /* restore file status flags */
282            setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));
283    
284            prints("\033[1;31m连接成功!\033[m\r\n");
285            log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
286    
287            BBS_last_access_tm = t_used = time(0);
288    
289            loop = 1;
290    
291            while (loop && !SYS_server_exit)
292            {
293                    FD_ZERO(&read_fds);
294                    FD_SET(STDIN_FILENO, &read_fds);
295                    FD_SET(sock, &read_fds);
296            
297                    timeout.tv_sec = 0;
298                    timeout.tv_usec = 100 * 1000; // 0.1 second
299    
300                    ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
301    
302                    if (ret == 0) // timeout
303                  {                  {
304                    loop = 0;                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
305                    break;                          {
306                                    loop = 0;
307                            }
308                    }
309                    else if (ret < 0)
310                    {
311                            if (errno != EINTR)
312                            {
313                                    log_error("select() error (%d) !\n", errno);
314                                    loop = 0;
315                            }
316                    }
317                    else if (ret > 0)
318                    {
319                            if (FD_ISSET(STDIN_FILENO, &read_fds))
320                            {
321                                    len = read(STDIN_FILENO, buf, sizeof(buf));
322                                    if (len == 0)
323                                    {
324                                            loop = 0;
325                                    }
326                                    write(sock, buf, (size_t)len);
327    
328                                    BBS_last_access_tm = time(0);
329                            }
330                            if (FD_ISSET(sock, &read_fds))
331                            {
332                                    len = read(sock, buf, sizeof(buf));
333                                    if (len == 0)
334                                    {
335                                            loop = 0;
336                                    }
337                                    write(STDOUT_FILENO, buf, (size_t)len);
338                            }
339                  }                  }
               write (1, buf, len);  
             }  
           break;  
340          }          }
341        if (time (0) - t_last_action >= 10)  
342            if (close(sock) == -1)
343          {          {
344            t_last_action = time (0);                  log_error("Close socket failed\n");
345          }          }
     }  
346    
347    if (close (sock) == -1)          t_used = time(0) - t_used;
348      {          tm_used = gmtime(&t_used);
349        log_error ("Close socket failed\n");  
350      }          log_std("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
351                            tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min,
352                            tm_used->tm_sec);
353    
354    return 0;          return 0;
355  }  }
356    
357  static int  static int
358  bbsnet_refresh ()  bbsnet_refresh()
359  {  {
360    int i;          clearscr();
361            moveto(1, 0);
362            prints(" ----------------------------------------------------------------------------- ");
363            for (int i = 2; i < 19; i++)
364            {
365                    moveto(i, 0);
366                    prints("|");
367                    moveto(i, 79);
368                    prints("|");
369            }
370            moveto(19, 0);
371            prints("|-----------------------------------------------------------------------------|");
372            moveto(22, 0);
373            prints(" ----------------------------------------------------------------------------- ");
374            moveto(23, 0);
375            prints(" [\x1b[1;32mCtrl+C\x1b[m]退出");
376    
377    clearscr ();          iflush();
   moveto (1, 0);  
   prints  
     ("╭══════════════════════════════════════╮");  
   for (i = 2; i < 19; i++)  
     {  
       moveto (i, 0);  
       prints ("║");  
       moveto (i, 79);  
       prints ("║");  
     }  
   moveto (19, 0);  
   prints  
     ("║——————————————————————————————————————║");  
   moveto (22, 0);  
   prints  
     ("╰══════════════════════════════════════╯");  
   moveto (23, 0);  
   prints  
     (" [\x1b[1;32mCtrl+C\x1b[m]退出");  
   iflush ();  
378    
379    return 0;          return 0;
380  }  }
381    
382  int  int bbsnet_selchange(int new_pos)
 bbsnet_selchange (int new_pos)  
383  {  {
384    moveto (20, 0);          moveto(20, 0);
385    clrtoeol ();          clrtoeol();
386    prints ("║\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",          prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m  站名:\x1b[1;33m%s\x1b[m",
387            bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);                     bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1);
388    moveto (20, 79);          moveto(20, 79);
389    prints ("║");          prints("|");
390    moveto (21, 0);          moveto(21, 0);
391    clrtoeol ();          clrtoeol();
392    prints ("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);          prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip);
393    if (bbsnet_conf[new_pos].port != 23)          if (bbsnet_conf[new_pos].port != 23)
394      prints ("  %d", bbsnet_conf[new_pos].port);          {
395    prints ("\x1b[m");                  prints("  %d", bbsnet_conf[new_pos].port);
396    moveto (21, 79);          }
397    prints ("║");          prints("\x1b[m");
398    iflush ();          moveto(21, 79);
399            prints("|");
400            iflush();
401    
402    return 0;          return 0;
403  }  }
404    
405  int  int bbs_net()
 bbs_net ()  
406  {  {
407    int ch, result, pos, i;          int ch, pos, i;
408    char file_config[256];  
409    time_t t_last_action;          load_bbsnet_conf(CONF_BBSNET);
410    fd_set inputs, testfds;  
411    struct timeval timeout;          BBS_last_access_tm = time(0);
412    
413    strcpy (file_config, app_home_dir);          clearscr();
414    strcat (file_config, "conf/bbsnet.conf");          bbsnet_refresh();
415            pos = bbsnet_menu.p_menu[0]->item_cur_pos;
416    load_bbsnet_conf (file_config);          display_menu(get_menu(&bbsnet_menu, "BBSNET"));
417            bbsnet_selchange(pos);
418    FD_ZERO (&inputs);  
419    FD_SET (0, &inputs);          while (!SYS_server_exit)
420            {
421    t_last_action = time (0);                  ch = igetch(0);
422                    switch (ch)
   clearscr ();  
   bbsnet_refresh ();  
   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
   display_menu (get_menu (&bbsnet_menu, "BBSNET"));  
   bbsnet_selchange (pos);  
   
   while (1)  
     {  
       testfds = inputs;  
       timeout.tv_sec = 0;  
       timeout.tv_usec = 100000;  
   
       result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,  
                        (fd_set *) NULL, &timeout);  
       switch (result)  
         {  
         case 0:  
           break;  
         case -1:  
           log_error ("select() error!\n");  
           break;  
         default:  
           if (FD_ISSET (0, &testfds))  
             {  
               ch = igetch ();  
               switch (ch)  
423                  {                  {
424                  case KEY_NULL:                  case KEY_NULL:
425                  case Ctrl ('C'):                          return -1;
426                    return 0;                  case Ctrl('C'):
427                            return 0;
428                    case KEY_TIMEOUT:
429                            if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
430                            {
431                                    return 0;
432                            }
433                            continue;
434                  case CR:                  case CR:
435                    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
436                    bbsnet_connect (pos);                          bbsnet_connect(pos);
437                    bbsnet_refresh ();                          bbsnet_refresh();
438                    display_current_menu (&bbsnet_menu);                          display_current_menu(&bbsnet_menu);
439                    bbsnet_selchange (pos);                          bbsnet_selchange(pos);
440                    break;                          break;
441                  case KEY_UP:                  case KEY_UP:
442                    for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
443                      menu_control (&bbsnet_menu, KEY_UP);                          {
444                    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                                  menu_control(&bbsnet_menu, KEY_UP);
445                    bbsnet_selchange (pos);                          }
446                    break;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
447                            bbsnet_selchange(pos);
448                            break;
449                  case KEY_DOWN:                  case KEY_DOWN:
450                    for (i = 0; i < STATION_PER_LINE; i++)                          for (i = 0; i < STATION_PER_LINE; i++)
451                      menu_control (&bbsnet_menu, KEY_DOWN);                          {
452                    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                                  menu_control(&bbsnet_menu, KEY_DOWN);
453                    bbsnet_selchange (pos);                          }
454                    break;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
455                            bbsnet_selchange(pos);
456                            break;
457                  case KEY_LEFT:                  case KEY_LEFT:
458                    menu_control (&bbsnet_menu, KEY_UP);                          menu_control(&bbsnet_menu, KEY_UP);
459                    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
460                    bbsnet_selchange (pos);                          bbsnet_selchange(pos);
461                    break;                          break;
462                  case KEY_RIGHT:                  case KEY_RIGHT:
463                    menu_control (&bbsnet_menu, KEY_DOWN);                          menu_control(&bbsnet_menu, KEY_DOWN);
464                    pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
465                    bbsnet_selchange (pos);                          bbsnet_selchange(pos);
466                    break;                          break;
467                  default:                  default:
468                    break;                          menu_control(&bbsnet_menu, ch);
469                            pos = bbsnet_menu.p_menu[0]->item_cur_pos;
470                            bbsnet_selchange(pos);
471                            break;
472                  }                  }
473              }                  BBS_last_access_tm = time(0);
           break;  
         }  
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
474          }          }
     }  
475    
476    return 0;          return 0;
477  }  }


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

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