/[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.3 by sysadm, Mon Mar 21 17:28:01 2005 UTC Revision 1.8 by sysadm, Tue Mar 22 08:59:31 2005 UTC
# Line 30  Line 30 
30  #include <unistd.h>  #include <unistd.h>
31  #include <netdb.h>  #include <netdb.h>
32    
33  #define TIME_OUT        15  #define TIME_OUT                15
34  #define MAX_PROCESS_BAR_LEN 30  #define MAX_PROCESS_BAR_LEN     30
35  #define MAXSTATION  26*2  #define MAXSTATION              26*2
36    #define STATION_PER_LINE        4
37    
38  struct _bbsnet_conf  struct _bbsnet_conf
39  {  {
# Line 81  load_bbsnet_conf (const char *file_confi Line 82  load_bbsnet_conf (const char *file_confi
82        bbsnet_conf[item_count].port = t4 ? atoi (t4) : 23;        bbsnet_conf[item_count].port = t4 ? atoi (t4) : 23;
83    
84        p_menuitem = p_menu->items[item_count] = malloc (sizeof (MENU_ITEM));        p_menuitem = p_menu->items[item_count] = malloc (sizeof (MENU_ITEM));
85        p_menuitem->row = 2 + item_count / 3;        p_menuitem->row = 2 + item_count / STATION_PER_LINE;
86        p_menuitem->col = 5 + item_count % 3 * 20;        p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20;
87        sprintf (p_menuitem->action, "%d", item_count);        sprintf (p_menuitem->action, "%d", item_count);
88        p_menuitem->submenu = 0;        p_menuitem->submenu = 0;
89        p_menuitem->priv = 0;        p_menuitem->priv = 0;
# Line 91  load_bbsnet_conf (const char *file_confi Line 92  load_bbsnet_conf (const char *file_confi
92        p_menuitem->name[0] =        p_menuitem->name[0] =
93          (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);          (item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count);
94        p_menuitem->name[1] = '\0';        p_menuitem->name[1] = '\0';
95        sprintf (p_menuitem->text, "1;36m%c. %s",        sprintf (p_menuitem->text, "%c. %s",
96                 p_menuitem->name[0], bbsnet_conf[item_count].host1);                 p_menuitem->name[0], bbsnet_conf[item_count].host1);
97    
98        item_count++;        item_count++;
# Line 108  load_bbsnet_conf (const char *file_confi Line 109  load_bbsnet_conf (const char *file_confi
109    return 0;    return 0;
110  }  }
111    
 // from Maple-hightman  
 // added by flyriver, 2001.3.3  
 int  
 telnetopt (int fd, char *buf, int max)  
 {  
   unsigned char c, d, e;  
   int pp = 0;  
   unsigned char tmp[30];  
   
   while (pp < max)  
     {  
       c = buf[pp++];  
       if (c == 255)  
         {  
           d = buf[pp++];  
           e = buf[pp++];  
           iflush ();  
           if ((d == 253) && (e == 3 || e == 24))  
             {  
               tmp[0] = 255;  
               tmp[1] = 251;  
               tmp[2] = e;  
               write (fd, tmp, 3);  
               continue;  
             }  
           if ((d == 251 || d == 252) && (e == 1 || e == 3 || e == 24))  
             {  
               tmp[0] = 255;  
               tmp[1] = 253;  
               tmp[2] = e;  
               write (fd, tmp, 3);  
               continue;  
             }  
           if (d == 251 || d == 252)  
             {  
               tmp[0] = 255;  
               tmp[1] = 254;  
               tmp[2] = e;  
               write (fd, tmp, 3);  
               continue;  
             }  
           if (d == 253 || d == 254)  
             {  
               tmp[0] = 255;  
               tmp[1] = 252;  
               tmp[2] = e;  
               write (fd, tmp, 3);  
               continue;  
             }  
           if (d == 250)  
             {  
               while (e != 240 && pp < max)  
                 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;  
 }  
   
112  static void  static void
113  process_bar (int n, int len)  process_bar (int n, int len)
114  {  {
# Line 216  bbsnet_connect (int n) Line 144  bbsnet_connect (int n)
144    int sock, ch, result, len, loop;    int sock, ch, result, len, loop;
145    struct sockaddr_in sin;    struct sockaddr_in sin;
146    char buf[256];    char buf[256];
   time_t now, t_last_action;  
147    fd_set inputs, testfds;    fd_set inputs, testfds;
148    struct timeval timeout;    struct timeval timeout;
149    struct hostent *pHost = NULL;    struct hostent *pHost = NULL;
150    int rc, rv, tos = 020, i;    int rc, rv, tos = 020, i;
151      char remote_addr[256];
152      int remote_port;
153      time_t t_used;
154      struct tm * tm_used;
155    
156    prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",    prints ("\033[1;32m正在测试往 %s (%s) 的连接,请稍候... \033[m\n",
157            bbsnet_conf[n].host1, bbsnet_conf[n].ip);            bbsnet_conf[n].host1, bbsnet_conf[n].ip);
# Line 256  bbsnet_connect (int n) Line 187  bbsnet_connect (int n)
187                   inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));                   inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));
188        return -2;        return -2;
189      }      }
190      
191    bzero (&sin, sizeof (sin));    bzero (&sin, sizeof (sin));
192    sin.sin_family = AF_INET;    sin.sin_family = AF_INET;
   sin.sin_port = htons (bbsnet_conf[n].port);  
193    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];    sin.sin_addr = *(struct in_addr *) pHost->h_addr_list[0];
194      sin.sin_port = htons (bbsnet_conf[n].port);
195    
196      strcpy (remote_addr, inet_ntoa (sin.sin_addr));
197      remote_port = ntohs (sin.sin_port);
198    
199    prints ("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");    prints ("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\n");
200    process_bar (0, MAX_PROCESS_BAR_LEN);    process_bar (0, MAX_PROCESS_BAR_LEN);
# Line 296  bbsnet_connect (int n) Line 229  bbsnet_connect (int n)
229        return -1;        return -1;
230      }      }
231    setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));    setsockopt (sock, IPPROTO_IP, IP_TOS, &tos, sizeof (int));
232    
233    prints ("\033[1;31m连接成功!\033[m\n");    prints ("\033[1;31m连接成功!\033[m\n");
234      log_std ("BBSNET connect to %s:%d\n",
235        remote_addr, remote_port);
236    
237    FD_ZERO (&inputs);    FD_ZERO (&inputs);
238    FD_SET (0, &inputs);    FD_SET (0, &inputs);
239    FD_SET (sock, &inputs);    FD_SET (sock, &inputs);
240    
241    t_last_action = time (0);    BBS_last_access_tm = t_used = time (0);
242    
243    loop = 1;    loop = 1;
244    
245    while (loop)    while (loop)
246      {      {
247        testfds = inputs;        testfds = inputs;
248        timeout.tv_sec = 0;        timeout.tv_sec = TIME_OUT;
249        timeout.tv_usec = 100000;        timeout.tv_usec = 0;
250    
251        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,        result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,
252                         (fd_set *) NULL, &timeout);                         (fd_set *) NULL, &timeout);
# Line 318  bbsnet_connect (int n) Line 254  bbsnet_connect (int n)
254        switch (result)        switch (result)
255          {          {
256          case 0:          case 0:
257              if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)
258                {
259                  loop = 0;
260                }
261            break;            break;
262          case -1:          case -1:
263            log_error ("select() error!\n");            log_error ("select() error!\n");
# Line 344  bbsnet_connect (int n) Line 284  bbsnet_connect (int n)
284                write (1, buf, len);                write (1, buf, len);
285              }              }
286            break;            break;
287          }            BBS_last_access_tm = time (0);
       if (time (0) - t_last_action >= 10)  
         {  
           t_last_action = time (0);  
288          }          }
289      }      }
290    
# Line 356  bbsnet_connect (int n) Line 293  bbsnet_connect (int n)
293        log_error ("Close socket failed\n");        log_error ("Close socket failed\n");
294      }      }
295    
296      t_used = time (0) - t_used;
297      tm_used = gmtime (&t_used);
298    
299      log_std ("BBSNET disconnect, %d days %d hours %d minutes %d seconds used\n",
300        tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec);
301    
302    return 0;    return 0;
303  }  }
304    
# Line 382  bbsnet_refresh () Line 325  bbsnet_refresh ()
325    prints    prints
326      ("╰══════════════════════════════════════╯");      ("╰══════════════════════════════════════╯");
327    moveto (23, 0);    moveto (23, 0);
328    prints    prints (" [\x1b[1;32mCtrl+C\x1b[m]退出");
     (" [\x1b[1;32mCtrl+C\x1b[m]退出 [\x1b[1;32m^\x1b[m]第一个 [\x1b[1;32m$\x1b[m]最后一个\033[0;37m\033[m");  
329    iflush ();    iflush ();
330    
331    return 0;    return 0;
# Line 414  bbsnet_selchange (int new_pos) Line 356  bbsnet_selchange (int new_pos)
356  int  int
357  bbs_net ()  bbs_net ()
358  {  {
359    int ch, result, pos;    int ch, pos, i;
360    char file_config[256];    char file_config[256];
   time_t t_last_action;  
   fd_set inputs, testfds;  
   struct timeval timeout;  
361    
362    strcpy (file_config, app_home_dir);    strcpy (file_config, app_home_dir);
363    strcat (file_config, "conf/bbsnet.conf");    strcat (file_config, "conf/bbsnet.conf");
364    
365    load_bbsnet_conf (file_config);    load_bbsnet_conf (file_config);
366    
367    FD_ZERO (&inputs);    BBS_last_access_tm = time (0);
   FD_SET (0, &inputs);  
   
   t_last_action = time (0);  
368    
369    clearscr ();    clearscr ();
370    bbsnet_refresh ();    bbsnet_refresh ();
# Line 438  bbs_net () Line 374  bbs_net ()
374    
375    while (1)    while (1)
376      {      {
377        testfds = inputs;        ch = igetch ();
378        timeout.tv_sec = 0;        switch (ch)
       timeout.tv_usec = 100000;  
   
       result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,  
                        (fd_set *) NULL, &timeout);  
       switch (result)  
379          {          {
380          case 0:          case KEY_NULL:
381            case Ctrl ('C'):
382              return 0;
383            case KEY_TIMEOUT:
384              if (time (0) - BBS_last_access_tm >= MAX_DELAY_TIME)
385                {
386                  return -1;
387                }
388              continue;
389            case CR:
390              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
391              bbsnet_connect (pos);
392              bbsnet_refresh ();
393              display_current_menu (&bbsnet_menu);
394              bbsnet_selchange (pos);
395            break;            break;
396          case -1:          case KEY_UP:
397            log_error ("select() error!\n");            for (i = 0; i < STATION_PER_LINE; i++)
398                menu_control (&bbsnet_menu, KEY_UP);
399              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
400              bbsnet_selchange (pos);
401              break;
402            case KEY_DOWN:
403              for (i = 0; i < STATION_PER_LINE; i++)
404                menu_control (&bbsnet_menu, KEY_DOWN);
405              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
406              bbsnet_selchange (pos);
407              break;
408            case KEY_LEFT:
409              menu_control (&bbsnet_menu, KEY_UP);
410              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
411              bbsnet_selchange (pos);
412              break;
413            case KEY_RIGHT:
414              menu_control (&bbsnet_menu, KEY_DOWN);
415              pos = bbsnet_menu.p_menu[0]->item_cur_pos;
416              bbsnet_selchange (pos);
417            break;            break;
418          default:          default:
419            if (FD_ISSET (0, &testfds))            menu_control (&bbsnet_menu, ch);
420              {            pos = bbsnet_menu.p_menu[0]->item_cur_pos;
421                ch = igetch ();            bbsnet_selchange (pos);
               switch (ch)  
                 {  
                 case KEY_NULL:  
                 case Ctrl ('C'):  
                   return 0;  
                 case CR:  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_connect (pos);  
                   bbsnet_refresh ();  
                   display_current_menu (&bbsnet_menu);  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_UP:  
                   menu_control (&bbsnet_menu, KEY_UP);  
                   menu_control (&bbsnet_menu, KEY_UP);  
                   menu_control (&bbsnet_menu, KEY_UP);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_DOWN:  
                   menu_control (&bbsnet_menu, KEY_DOWN);  
                   menu_control (&bbsnet_menu, KEY_DOWN);  
                   menu_control (&bbsnet_menu, KEY_DOWN);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_LEFT:  
                   menu_control (&bbsnet_menu, KEY_UP);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case KEY_RIGHT:  
                   menu_control (&bbsnet_menu, KEY_DOWN);  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos;  
                   bbsnet_selchange (pos);  
                   break;  
                 case '^':  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos = 0;  
                   bbsnet_selchange (pos);  
                   break;  
                 case '$':  
                   pos = bbsnet_menu.p_menu[0]->item_cur_pos  
                     = bbsnet_menu.p_menu[0]->item_count - 1;  
                   if (pos < 0)  
                     pos = 0;  
                   bbsnet_selchange (pos);  
                   break;  
                 default:  
                   break;  
                 }  
             }  
422            break;            break;
423          }          }
424        if (time (0) - t_last_action >= 10)        BBS_last_access_tm = time (0);
         {  
           t_last_action = time (0);  
         }  
425      }      }
426    
427    return 0;    return 0;


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

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