From 617c641f21c4be1e14d463dec89821daa112b58d Mon Sep 17 00:00:00 2001 From: epochqwert Date: Mon, 5 Oct 2015 00:37:02 -0500 Subject: fixed up some of runem that hadn't been made for multiple fds. update to link example bot to use line_cutter --- examples/link.c | 63 ++++++++------------------------- libirc.c | 106 ++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 99 insertions(+), 70 deletions(-) diff --git a/examples/link.c b/examples/link.c index 680550c..3d5a0cc 100644 --- a/examples/link.c +++ b/examples/link.c @@ -33,70 +33,36 @@ void message_handler(int fd,char *from,struct user *user,char *line) { } void line_handler(int fd,char *line) {//this should be built into the libary? - char *s=line,*t=0,*u=0; char *temp; + char **a; char tmp[512]; struct user *user=malloc(sizeof(struct user)); - user->nick=0; - user->user=0; - user->host=0; - if(strchr(line,'\r')) *strchr(line,'\r')=0; - if(strchr(line,'\n')) *strchr(line,'\n')=0; printf("line: '%s'\n",line); - if(line[0]==':') { - if((user->nick=strchr(line,':'))) { - *(user->nick)=0; - (user->nick)++; - } - } - if(user->nick) { - if((s=strchr((user->nick),' '))) { - *s=0; - s++; - if((t=strchr(s,' '))) { - *t=0; - t++; - if((u=strchr(t,' '))) {//: - *u=0; - u++; - } - } - } - if(((user->user)=strchr((user->nick),'!'))) { - *(user->user)=0; - (user->user)++; - if(((user->host)=strchr((user->user),'@'))) { - *(user->host)=0; - (user->host)++; - } - } else { - user->host=user->nick; - } - } - if(!user->user && s) { - if(!strcmp(s,"004")) { + a=line_cutter(fd,line,user); + if(!user->user && a[0]) { + if(!strcmp(a[0],"004")) { snprintf(tmp,sizeof(tmp)-1,"JOIN %s\r\n",chans[fdtoi(fd)]); temp=strchr(chans[fdtoi(fd)],' '); if(temp) *temp=0; mywrite(fd,tmp); } } - if(s && t && u) { - if(!strcmp(s,"PRIVMSG")) { - message_handler(fd,*t=='#'?t:user->nick,user,++u); + if(a[0] && a[1] && a[2]) { + if(!strcmp(a[0],"PRIVMSG")) { + message_handler(fd,*a[1]=='#'?a[1]:user->nick,user,a[2]); } } - if(s && user->nick && t) { - if(!strcmp(s,"JOIN")) { - snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has joined %s%c",1,user->nick,t+(*t==':'),1); + if(a[0] && user->nick && a[1]) { + if(!strcmp(a[0],"JOIN")) { + snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has joined %s%c",1,user->nick,a[1]+(*a[1]==':'),1); privmsg_others(fd,tmp); } - if(!strcmp(s,"PART")) { - snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has parted %s%c",1,user->nick,t+(*t==':'),1); + if(!strcmp(a[0],"PART")) { + snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has parted %s%c",1,user->nick,a[1]+(*a[1]==':'),1); privmsg_others(fd,tmp); } - if(!strcmp(s,"QUIT")) { - snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has quited %s%c",1,user->nick,t+(*t==':'),1); + if(!strcmp(a[0],"QUIT")) { + snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has quited %s%c",1,user->nick,a[1]+(*a[1]==':'),1); privmsg_others(fd,tmp); } } @@ -119,6 +85,7 @@ int main(int argc,char *argv[]) { for(i=0;((i*3)+3)h_addr_list);(he->h_addr_list)++) { inet_ntop(hints.ai_family,*(he->h_addr_list),buf,SILLYLIMIT); - if(!getaddrinfo(buf,port,&hints,&servinfo)) - for(p=servinfo;p;p=p->ai_next) - if(connect(fd,p->ai_addr, p->ai_addrlen) < 0) continue; else return fd; + if(!getaddrinfo(buf,port,&hints,&servinfo)) { + for(p=servinfo;p;p=p->ai_next) { + if(connect(fd,p->ai_addr, p->ai_addrlen) < 0) { + printf("libirc: serverConnect: trying something else...\n"); + continue; + } else { + printf("libirc: serverConnect: returning something! %d\n",fd); + return fd; + } + } + } } } printf("I tried as hard as I could and couldn't connect to %s:%s\n",serv,port); @@ -80,8 +93,29 @@ int fdlen(int *fds) { return i+1; } + +char *memstr(char *s,char *find,size_t l) { + return memmem(s,l,find,strlen(find)); +} + +/* already in string.h! +char *memmem(char *s,char *find,size_t sl,size_t fl) { + size_t i,j,fl; + if(!s) return 0; + if(!find) return 0; + for(i=0;ifdmax?fds[i]:fdmax; } for(;;) { @@ -112,31 +148,56 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) { for(i=0;fds[i] != -1;i++) { 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(bllen[i]+n > blsize[i]) {//this is probably off... + t=malloc(blsize[i]+n); if(!t) exit(253); - memcpy(t,backlogs[i],blsize-n+1); + memcpy(t,backlogs[i],blsize[i]); + blsize[i]+=n; free(backlogs[i]); backlogs[i]=t; } - memcpy(backlogs[i]+bllen,buffers[i],n); - bllen+=n; - while((t=strstr(backlogs[i],"\r\n"))) { + memcpy(backlogs[i]+bllen[i],buffers[i],n); + bllen[i]+=n; + + //manually loop? +//works for IRC that uses \r\n. need to make it work for just \n too? +/* + while((t=memstr(backlogs[i],"\r\n",bllen[i]))) {//no. backlogs aren't nulled. line=backlogs[i]; - if(!strncmp(line,"PING",4)) { + if((t-backlogs[i]) >=4 && !strncmp(line,"PING",4)) { line[1]='O'; write(fds[i],line,t-backlogs[i]+2); } else { - if(!strncmp(line,"ERROR",5)) return 0; + if(!strncmp(line,"ERROR",5)) return 0; *t=0; - line_handler(fds[i],line); + if(line_handler) line_handler(fds[i],line); } - bllen-=((t+2)-backlogs[i]); - if(bllen <= 0) bllen=0; - else memmove(backlogs[i],(t+2),bllen); + bllen[i]-=((t+2)-backlogs[i]); + if(bllen[i] <= 0) bllen[i]=0; + else memmove(backlogs[i],(t+2),bllen[i]); } +*/ + while((t=memchr(backlogs[i],'\n',bllen[i]))) {//no. backlogs aren't nulled. + line=backlogs[i]; + if(*(t-1) == '\r') { + t--; + hack=2; + } else { + hack=1; + } + if((t-backlogs[i]) >=4 && !strncmp(line,"PING",4)) { + line[1]='O'; + write(fds[i],line,t-backlogs[i]+hack); + } else { + if(!strncmp(line,"ERROR",5)) return 0; + *t=0; + if(line_handler) line_handler(fds[i],line); + } + bllen[i]-=((t+hack)-backlogs[i]); + if(bllen[i] <= 0) bllen[i]=0; + else memmove(backlogs[i],(t+hack),bllen[i]); + } + } } return 0; @@ -168,6 +229,7 @@ int ircConnect(char *serv,char *port,char *nick,char *user) { char **line_cutter(int fd,char *line,struct user *user) { int i; char **a=malloc(sizeof(char *) * 256);//heh. + if(!a) exit(54); memset(a,0,sizeof(char *) * 256); if(!user) return 0; user->nick=0; -- cgit v1.2.3