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


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

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