荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: jjk (prepare for the future), 信区: InstallBBS
标  题: bbsnet改进版,可自由穿梭
发信站: 荔园晨风BBS站 (Sat Jan 12 23:45:51 2002), 转信

【 以下文字转载自 jjk 的信箱 】
【 原文由 jjksam@smth.org 所发表 】
发信人: zdh.bbs@bbs.pse.dhs.org (一辉), 信区: BBSMan_Dev
标  题: bbsnet改进版,可自由穿梭
发信站: THU PSE BBS (Wed Dec  5 02:41:14 2001)
转信站: SMTH!zixia.net!PSEBBS

功能:可以自由输入ip:port,实现自由穿梭。
应用的bbs:PSEBBS、zixiaBBS

在bbsnet.ini中加入一行
自定义站点 自定义站点 127.0.0.1
其实,只判断第二个,站名是"自定义站点",其它两个只是占位,并无作用。

新加功能:
1.跳光标
2.支持166.111.34.155:2300形式
3.支持backspace(不过不能跨:)

/*bbsnet.c*/
// NJU bbsnet, preview version, zhch@dii.nju.edu.cn, 2000.3.23 //
// HIT bbsnet, Changed by Sunner, sun@bbs.hit.edu.cn, 2000.6.11
// PSE bbsnet, Changed by zdh, dh_zheng@hotmail.com,2001.12.04
#include <stdio.h>
#include <termios.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/telnet.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <math.h>
#include <sys/file.h>
#include "bbs.h"  //这个应该是有的吧。
char host1[100][40], host2[100][40], ip[100][40];
char ip_zdh[40];//用来接受用户的ip
static char buf[100]; // output buffer
int port[100], counts= 0;
char str[]= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!
@#$%^&*()";
char datafile[80]= BBSHOME "/etc/bbsnet.ini";
char userid[80]=   "unknown";
init_data() {
    FILE *fp;
    char t[256], *t1, *t2, *t3, *t4;
    fp= fopen(datafile, "r");
    if(fp== NULL) return;
    while(fgets(t, 255, fp)&& counts <= 72) {
        t1= strtok(t, " \t");
        t2= strtok(NULL, " \t\n");
        t3= strtok(NULL, " \t\n");
        t4= strtok(NULL, " \t\n");
        if(t1[0]== '#'|| t1== NULL|| t2== NULL|| t3== NULL) continue;
        strncpy(host1[counts], t1, 16);
        strncpy(host2[counts], t2, 36);
        strncpy(ip[counts], t3, 36);
        port[counts]= t4? atoi(t4): 23;
        counts++;
    }
    fclose(fp);
}
sh(int n) {
    static oldn= -1;
    if(n>= counts) return;
    if(oldn >=0) {
        locate(oldn);
        printf("*[1;32m %c.*[m%s", str[oldn], host2[oldn]);
    }
    oldn= n;
    if (strcmp(host2[n],"自定义站点")==0){  //判断自定义站点
    printf("*[22;3H*[1;37m使用方法: 按回车后输入ip。*[1;33m*[22;32H*[1;37m 站
名: *[1;33m自定义站点              \r\n");
    printf("*[1;37m*[23;3H连往: *[1;33m__________                   *[21;1H"
);
    }
    else{
    printf("*[22;3H*[1;37m单位: *[1;33m%s                   *[22;32H*[1;37m
站名: *[1;33m%s              \r\n", host1[n], host2[n]);
    printf("*[1;37m*[23;3H连往: *[1;33m%s                   *[21;1H", ip[n])
;
    }
    locate(n);
    printf("[%c]*[1;42m%s*[m", str[n], host2[n]);
}
show_all() {
    int n;
    printf("*[H*[2J*[m");
    printf("┏━━━━━━━━━━━━━━━*[1;35m 月  光  宝  盒 *[m━━━
━━━━━━━━━━━━┓\r\n");
    for(n= 1; n< 22; n++)
        printf("┃
                  ┃\r\n");
    printf("┃
 *[1;36m按*[1;33mCtrl+C*[1;36m退出*[m ┃\r\n");
    printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
━━━━━━━┛");
    printf("*[21;3H───*[1;36m般若波羅密*[m───────*[1;36m般-若-波-羅
-蜜~~*[m───────*[1;36m般若波羅密*[m───");
    //printf("*[21;3H*[1;36m般若波羅密般若波羅密般若波羅密般若波羅密般若波羅
密般若波羅密般若波羅密般若波*[m");
    for(n= 0; n< counts; n++) {
        locate(n);
        printf("*[1;32m %c.*[m%s", str[n], host2[n]);
    }
}
locate(int n) {
    int x, y;
    char buf[20];
    if(n>= counts) return;
    y= n%19 + 2;
    x= n /19 * 24+ 4;
    sprintf(buf, "*[%d;%dH", y, x);
    printf(buf);
}
int getch()
{
    int c, d, e;
    static lastc= 0;
    c= getchar();
    if(c== 10&& lastc== 13) c=getchar();
    lastc= c;
    if(c!= 27) return c;
    d= getchar();
    e= getchar();
    if(d== 27) return 27;
    if(e== 'A') return 257;
    if(e== 'B') return 258;
    if(e== 'C') return 259;
    if(e== 'D') return 260;
    return 0;
}
void QuitTime()
{
    reset_tty();
        exit(0);
}
void SetQuitTime()
{
        signal(SIGALRM, QuitTime);
        alarm(60);
}
main_loop() {
    int p= 0;
    int c, n;
L:
    show_all();
    sh(p);
    fflush(stdout);
    while(1) {
        c= getch();
        if(c== 3|| c== 4|| c== 27|| c< 0) break;
        if(c== 257&& p> 0) p--;
        if(c== 258&& p< counts- 1) p++;
        if(c== 259&& p< counts- 19) p+=19;
        if(c== 260&& p>= 19) p-=19;
        if(c== 13|| c== 10) {
            bbsnet(p);
            goto L;
        }
        for(n=0; n< counts; n++) if(str[n]== c) p= n;
        sh(p);
        fflush(stdout);
    }
}
bbsnet(int n) {
    char buf1[40],buf2[39],c,buf3[2];//增加的变量
    int i;//
    int l;//判断是不是port
    int j,m;
    if(n>= counts) return;
    if (strcmp(host2[n],"自定义站点")==0){//如果是自定义站点,等待输入ip或域

    for (i=0;i<25;i++) {buf1[i]='\0';buf2[i]='\0';}
    prints("*[1;32m*[23;3H连往: ");
    refresh();
    j=0;
    l=0;
    for (i=0;i<15;i++){
        c= getch();
        if (c==' '||c=='\015'||c=='\0'||c=='\n') break;
        if (c==':'){
                 l=1;
                 sprintf(buf3,"%c",c);
                 prints("*[0;1m");
                 prints(buf3);
                 prints("*[1;33m");
          refresh();
                 strcpy(ip_zdh,buf1);
              strcpy(ip[n],ip_zdh);
                 for (m=0;m<20;m++) {buf1[m]='\0';buf2[m]='\0';}
                 j=0;
        }
        if ((c>='0'&&c<='9')||(c=='.')||(c>='A'&&c<='Z')||(c>='a'&&c<='z'))
        {
        sprintf(buf3,"%c",c);
        sprintf(buf2,"%s%c",buf1,c);
        sprintf(buf1,"%s",buf2);
        prints(buf3);
        refresh();
        j=j+1;
        }
        if ((c==8)&&(j>=1)){
        for(m=j-1;m<20;m++) {buf1[m]='\0';buf2[m]='\0';}
        sprintf(buf3,"%c",c);
        prints(buf3);
        refresh();
        j--;
        }
        refresh();
        i--;
     }
     if(l==0){
        strcpy(ip_zdh,buf1);
     strcpy(ip[n],ip_zdh);
     }
     else{port[n]=atoi(buf1);}
     }
    printf("*[H*[2J*[1;32mo 连往: %s (%s)\r\n", host2[n], ip[n]);
    printf("%s\r\n\r\n*[m", "o 连不上时请稍候,30 秒后将自动退出");
    fflush(stdout);
    proc(host2[n],ip[n], port[n]);
}
main(int n, char* cmd[]) {
    SetQuitTime();
    get_tty();
    init_tty();
    if(n>= 2) strcpy(datafile, cmd[1]);
    if(n>= 3) strcpy(userid, cmd[2]);
    init_data();
    main_loop();
    printf("*[m");
    reset_tty();
}
syslog(char* s) {
        char buf[512], timestr[16], *thetime, ip[16];
        time_t dtime;
        int fd;
 struct sockaddr_in tmpsin;
 int tmplen = sizeof(struct sockaddr_in);
 getpeername(0, &tmpsin,(int *) &tmplen);
 strncpy( ip, inet_ntoa(tmpsin.sin_addr), 16 );
        time(&dtime);
        thetime = (char*) ctime(&dtime);
        strncpy(timestr, &(thetime[4]), 15);
        timestr[15] = '\0';
        //sprintf(buf,"%s %s@%s %s\n", timestr, userid, ip, s) ;
        snprintf(buf, 512, "%s %-15s %-5d %s\n", timestr, ip, (int)getpid(),
 s) ;
        fd=open(BBSHOME "/reclog/bbsnet.log", O_CREAT | O_APPEND | O_WRONLY
);
 flock( fd, LOCK_EX );
        write( fd, buf, strlen(buf) );
 flock( fd, LOCK_UN );
        close(fd);
}
#define stty(fd, data) tcsetattr( fd, TCSANOW, data )
#define gtty(fd, data) tcgetattr( fd, data )
struct termios tty_state, tty_new ;
get_tty()
{
    if(gtty(1,&tty_state) < 0)
        return 0;
    return 1 ;
}
init_tty()
{
    long vdisable;
    memcpy( &tty_new, &tty_state, sizeof(tty_new)) ;
    tty_new.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ISIG);
    tty_new.c_cflag &= ~CSIZE;
    tty_new.c_cflag |= CS8;
    tty_new.c_cc[ VMIN ] = 1;
    tty_new.c_cc[ VTIME ] = 0;
    if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) >= 0)
      {
      tty_new.c_cc[VSTART] = vdisable;
      tty_new.c_cc[VSTOP]  = vdisable;
      tty_new.c_cc[VLNEXT] = vdisable;
      }
    tcsetattr(1, TCSANOW, &tty_new);
}
reset_tty()
{
    stty(1,&tty_state) ;
}
proc(char *hostname, char *server,int port)
{
        int fd;
        struct sockaddr_in blah;
        struct hostent *he;
        int result;
        unsigned char buf[2048];
        fd_set readfds;
        struct timeval tv;
        signal(SIGALRM, QuitTime);
        alarm(30);
        bzero((char *)&blah,sizeof(blah));
        blah.sin_family=AF_INET;
        blah.sin_addr.s_addr=inet_addr(server);
        blah.sin_port=htons(port);
        fflush(stdout);
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if ((he = gethostbyname(server)) != NULL)
                bcopy(he->h_addr, (char *)&blah.sin_addr, he->h_length);
            else
                if ((blah.sin_addr.s_addr = inet_addr(server)) < 0) return;
            if(connect(fd,(struct sockaddr *)&blah,16)<0) return;
        signal(SIGALRM, SIG_IGN);
        printf("已经连接上主机,按'ctrl+]'快速退出。\n");
  sprintf(buf, "Login %s (%s)", hostname, server);
  syslog(buf);
        while(1)
        {
                tv.tv_sec = 2400;
                tv.tv_usec = 0;
                FD_ZERO(&readfds) ;
                FD_SET(fd,&readfds);
                FD_SET(0,&readfds);
                result = select(fd + 1,&readfds,NULL,NULL,&tv);
                if (result <= 0) break;
                if (FD_ISSET(0,&readfds))
                {
                        result = read(0,buf,2048);
                        if (result<=0) break;
                        if(result==1&&(buf[0]==10||buf[0]==13)){
       buf[0]=13;
       buf[1]=10;
       result=2;
      }
                        //if(buf[0]==29){close(fd);return;}
                        if(buf[0]==29){close(fd);break;}
                        write(fd,buf,result);
                }else{
                        result=read(fd,buf,2048);
                        if (result<=0) break;
                        if(strchr(buf, 255))
                          telnetopt(fd, buf,result);
                        else
                          write(0,buf,result);
                }
        }
  sprintf(buf, "Logout %s (%s)", hostname, server);
  syslog(buf);
}
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++];
                      fflush(stdout);
                      if((d==253)&&(e==3||e==24)) {
                       tmp[0]=255;tmp[1]=251;tmp[2]=e;write(fd,tmp,3);contin
ue;}
                      if((d==251||d==252)&&(e==1||e==3||e==24)) {
                       tmp[0]=255;tmp[1]=253;tmp[2]=e;write(fd,tmp,3);contin
ue;}
                      if(d==251||d==252) {
                       tmp[0]=255;tmp[1]=254;tmp[2]=e;write(fd,tmp,3);contin
ue;}
                      if(d==253||d==254) {
                       tmp[0]=255;tmp[1]=252;tmp[2]=e;write(fd,tmp,3);contin
ue;}
                      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
                      write(0,&c,1);
                }
}
int refresh() {
    write(0, buf, strlen(buf));
    buf[0]= 0;
}
int prints(char* b) {
    strcat(buf, b);
}
--
欢迎访问我的主页:http://zdhice.edu.chinaren.com
生态工业研究中心: http://pse.dhs.org
     模拟水战BBS: http://bbs.pse.dhs.org
                  telnet://bbs.pse.dhs.org
※ 来源:·模拟水战 bbs.pse.dhs.org·[FROM: 166.111.34.163]
--
※ 转载:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.0.146]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店