#include #include #include #include #include #include #include #include #include #include //#define DEBUG "epoch" //nick or channel to send debug info to. #define CHUNK 16 int serverConnect(char *serv,char *port) { struct addrinfo hints, *servinfo, *p; int rv; int fd=0; memset(&hints,0,sizeof hints); hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; if((fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { perror("socket"); return fd; } if((rv=getaddrinfo(serv,port,&hints,&servinfo)) != 0) { fprintf(stderr,"error resolving '%s'.\n",serv); return 0; } for(p=servinfo;p;p=p->ai_next) { if (connect(fd,p->ai_addr, p->ai_addrlen) < 0) { perror("connect"); continue; } break; } return p?fd:0; } int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { FILE *fp; fd_set master; fd_set readfs; struct timeval timeout; int fdmax,n,s,i; char *backlog=malloc(CHUNK+1); char *t,*line=0; int blsize=CHUNK; int bllen=0; char buffer[CHUNK];//THIS IS *NOT* NULL TERMINATED. if(!backlog) return 252; FD_ZERO(&master); FD_ZERO(&readfs); FD_SET(fd,&master); fdmax=fd; fp=fdopen(fd,"rw"); memset(backlog,0,CHUNK); memset(buffer,0,CHUNK); if(fd) { int done=0; while(!done) { extra_handler(fd); readfs=master; timeout.tv_sec=0; timeout.tv_usec=1000; if( select(fdmax+1,&readfs,0,0,&timeout) == -1 ) { printf("\n!!!It is crashing here!!!\n\n"); perror("select"); return 1; } if(FD_ISSET(fd,&readfs)) { if((n=recv(fd,buffer,CHUNK,0)) <= 0) {//read CHUNK bytes fprintf(stderr,"recv: %d\n",n); perror("recv"); return 2; } else { buffer[n]=0;//deff right. if(bllen+n >= blsize) {//this is probably off... blsize+=n; t=malloc(blsize); if(!t) { printf("OH FUCK! MALLOC FAILED!\n"); exit(253); } memset(t,0,blsize);//optional? memcpy(t,backlog,blsize-n+1);//??? free(backlog); backlog=t; } memcpy(backlog+bllen,buffer,n); bllen+=n; for(i=0,s=0;i bllen) { //if the ending position is after the size of the backlog... bllen=0;//fuck shifting. :P } else { for(i=s;i<=bllen;i++) {//should work. backlog[i-s]=backlog[i]; } bllen-=s; } } } } } return 0; } //yeah. this is a copy of the previous function. //with a bit of different stuff. didn't want to break anything yet. int runem(int *fd,void (*line_handler)(),void (*extra_handler)()) { FILE *fp; fd_set master; fd_set readfs; struct timeval timeout; int fdmax=0,n,s,i; char *backlog=malloc(CHUNK+1); char *t,*line=0; int blsize=CHUNK; int bllen=0; char buffer[CHUNK];//THIS IS *NOT* NULL TERMINATED. if(!backlog) return 252; FD_ZERO(&master); FD_ZERO(&readfs); for(i=0;fd[i] != -1;i++) { FD_SET(fd,&master); fdmax=fd>fdmax?fd:fdmax; } fp=fdopen(fd,"rw"); memset(backlog,0,CHUNK); memset(buffer,0,CHUNK); if(fd) { int done=0; while(!done) { for(fd=0;fd<=fdmax;fd++) { if(FD_ISSET(fd,&master)) { extra_handler(fd); } } readfs=master; timeout.tv_sec=0; timeout.tv_usec=1000; if( select(fdmax+1,&readfs,0,0,&timeout) == -1 ) { printf("\n!!!It is crashing here!!!\n\n"); perror("select"); return 1; } for(fd=0;fd<=fdmax;fd++) { if(FD_ISSET(fd,&readfs)) { if((n=recv(fd,buffer,CHUNK,0)) <= 0) {//read CHUNK bytes fprintf(stderr,"recv: %d\n",n); perror("recv"); return 2; } else { buffer[n]=0;//deff right. if(bllen+n >= blsize) {//this is probably off... blsize+=n; t=malloc(blsize); if(!t) { printf("OH FUCK! MALLOC FAILED!\n"); exit(253); } memset(t,0,blsize);//optional? memcpy(t,backlog,blsize-n+1);//??? free(backlog); backlog=t; } memcpy(backlog+bllen,buffer,n); bllen+=n; for(i=0,s=0;i bllen) { //if the ending position is after the size of the backlog... bllen=0;//fuck shifting. :P } else { for(i=s;i<=bllen;i++) {//should work. backlog[i-s]=backlog[i]; } bllen-=s; } } } } } } return 0; } //:hack.thebackupbox.net 433 * sysbot :Nickname is already in use. //Need to have it check for this. //and try a nick? //or something... //I don't want to add any string parsing to this function. :/ int ircConnect(char *serv,char *port,char *nick,char *user) { char sendstr[1024]; int fd; fd=serverConnect(serv,port); if(!fd) { return 0; } snprintf(sendstr,sizeof(sendstr)-1,"NICK %s\r\nUSER %s\r\n",nick,user); write(fd,sendstr,strlen(sendstr)); return fd; }