From c049efe694535574770634b5ae40230c59098082 Mon Sep 17 00:00:00 2001 From: epochqwert Date: Fri, 5 Jun 2015 08:01:09 -0500 Subject: cleaned up some stuff in runem(). --- examples/link | Bin 8036 -> 0 bytes examples/link.c | 8 +--- libirc.c | 119 ++++++++++++++++++-------------------------------------- 3 files changed, 38 insertions(+), 89 deletions(-) delete mode 100755 examples/link diff --git a/examples/link b/examples/link deleted file mode 100755 index b40b8a9..0000000 Binary files a/examples/link and /dev/null differ diff --git a/examples/link.c b/examples/link.c index c175595..680550c 100644 --- a/examples/link.c +++ b/examples/link.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include "../irc.h" #define mywrite(a,b) write(a,b,strlen(b)) @@ -12,12 +12,6 @@ void extra_handler(int fd) { return; } -struct user { - char *nick; - char *user; - char *host; -}; - void privmsg_others(int fd,char *msg) { int i; char tmp[512]; diff --git a/libirc.c b/libirc.c index aa86d41..a69e020 100644 --- a/libirc.c +++ b/libirc.c @@ -14,12 +14,13 @@ //#define DEBUG "epoch" //nick or channel to send debug info to. #define CHUNK 4096 -//int main(int argc,char *argv[]) { -// return 0; -//} - #define SILLYLIMIT 1024 +/* how this works: +if server == |/program then open a socketpair and fork and exec program +else try serv as IPv6, as IPv4, resolve as IPv6, resolve as IPv4 +connect to that. +*/ int serverConnect(char *serv,char *port) { int fd=-1; int s[2]; @@ -85,8 +86,7 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) { fd_set master; fd_set readfs; struct timeval timeout; - int fdmax=0,n,s,i; - int fd; + int fdmax=0,n,i; char *backlogs[fdl]; char *t,*line=0; int blsize=CHUNK; @@ -95,94 +95,49 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) { FD_ZERO(&master); FD_ZERO(&readfs); for(i=0;fds[i] != -1;i++) { - //if(!backlogs[i]) return 252;//wtf is this here for? ofc they're not set! FD_SET(fds[i],&master); backlogs[i]=malloc(CHUNK+1); memset(backlogs[i],0,CHUNK); memset(buffers[i],0,CHUNK); fdmax=fds[i]>fdmax?fds[i]:fdmax; } - int done=0; - while(!done) { - for(fd=0;fd<=fdmax;fd++) { - if(FD_ISSET(fd,&master)) { - if(extra_handler) extra_handler(fd); - } - } + for(;;) { 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((j=select(fdmax+1,&readfs,0,0,&timeout)) == -1 ) return perror("select"),1; + for(i=0;fds[i] != -1;i++) if(extra_handler) extra_handler(fds[i]); + if(j == 0) continue;//don't bother to loop over them. + printf("getting there.\n"); for(i=0;fds[i] != -1;i++) { - if(FD_ISSET(fds[i],&readfs)) { - if((n=recv(fds[i],buffers[i],CHUNK,0)) <= 0) {//read CHUNK bytes - if(n) { - fprintf(stderr,"recv: %d\n",n); - perror("recv"); - } else { - fprintf(stderr,"connection closed. fd: %d\n",fds[i]); - } - return 2; + if(!FD_ISSET(fds[i],&readfs)) continue; + if((n=recv(fds[i],buffers[i],CHUNK,0)) <= 0) return (perror("recv"),2); + buffers[i][n]=0;//deff right. + if(bllen+n >= blsize) {//this is probably off... + blsize+=n; + t=malloc(blsize); + if(!t) exit(253); + memcpy(t,backlogs[i],blsize-n+1); + free(backlogs[i]); + backlogs[i]=t; + } + memcpy(backlogs[i]+bllen,buffers[i],n); + bllen+=n; + +//HERE EPOCH + while((t=strstr(backlogs[i],"\r\n"))) { + line=backlogs[i]; + if(!strncmp(line,"PING",4)) { + line[1]='O'; + write(fds[i],line,t-backlogs[i]+2); } else { - buffers[i][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,backlogs[i],blsize-n+1);//??? - free(backlogs[i]); - backlogs[i]=t; - } - memcpy(backlogs[i]+bllen,buffers[i],n); - bllen+=n; - for(j=0,s=0;j bllen) { //if the ending position is after the size of the backlog... - bllen=0;//fuck shifting. :P - } else { - for(j=s;j<=bllen;j++) {//should work. - backlogs[i][j-s]=backlogs[i][j]; - } - bllen-=s; - } + if(!strncmp(line,"ERROR",5)) return 0; + *t=0; + line_handler(fds[i],line); } + bllen-=((t+2)-backlogs[i]); + if(bllen <= 0) bllen=0; + else memmove(backlogs[i],(t+2),bllen); } } } -- cgit v1.2.3