diff options
author | epochqwert <epoch@53flpnlls43fcguy.onion> | 2015-03-08 06:04:41 -0500 |
---|---|---|
committer | epochqwert <epoch@53flpnlls43fcguy.onion> | 2015-03-08 06:04:41 -0500 |
commit | ca97dc3913735ac0aed03974ada43aede82bec02 (patch) | |
tree | 4fa24825a91d5a7d0d8fe400f07cb0420bc3c4d9 /libirc | |
parent | 04e1a88e14844fa692c6bb6b484a2a2d9207f194 (diff) | |
download | segfault-ca97dc3913735ac0aed03974ada43aede82bec02.tar.gz segfault-ca97dc3913735ac0aed03974ada43aede82bec02.zip |
added a linkbot example to libirc, bunch of random shit in segfault I guess. I don't remember. I should commit more.
Diffstat (limited to 'libirc')
-rw-r--r-- | libirc/examples/link.c | 124 | ||||
-rw-r--r-- | libirc/libirc.c | 158 |
2 files changed, 198 insertions, 84 deletions
diff --git a/libirc/examples/link.c b/libirc/examples/link.c new file mode 100644 index 0000000..5c13e94 --- /dev/null +++ b/libirc/examples/link.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <irc.h> + +#define mywrite(a,b) write(a,b,strlen(b)) + +int *fds; +char **chans; + +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]; + for(i=0;fds[i] != -1;i++) { + if(fds[i] != fd) { + snprintf(tmp,sizeof(tmp)-1,"PRIVMSG %s :%s\r\n",chans[fdtoi(fds[i])],msg); + write(fds[i],tmp,strlen(tmp)); + } + } +} + +void message_handler(int fd,char *from,struct user *user,char *line) { + int i; + char tmp[512]; + if(!strcmp(from,chans[fdtoi(fd)])) {//don't want to be forwarding PMs. :P + snprintf(tmp,sizeof(tmp)-1,"<%s> %s",user->nick,line); + privmsg_others(fd,tmp); + } +} + +void line_handler(int fd,char *line) {//this should be built into the libary? + char *s=line,*t=0,*u=0; + 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")) { + snprintf(tmp,sizeof(tmp)-1,"JOIN %s\r\n",chans[fdtoi(fd)]); + mywrite(fd,tmp); + } + } + if(s && t && u) { + if(!strcmp(s,"PRIVMSG")) { + message_handler(fd,*t=='#'?t:user->nick,user,++u); + } + } + if(s && user->nick && t) { + if(!strcmp(s,"JOIN")) { + snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has joined %s%c",1,user->nick,t+1,1); + privmsg_others(fd,tmp); + //send a join message to the other end. + } + } + free(user); +} + +int fdtoi(int fd) { + int i; + for(i=0;fds[i] != -1;i++) { + if(fds[i] == fd) return i; + } + return -1; +} + +int main(int argc,char *argv[]) { + fds=malloc(sizeof(int) * (argc+3) / 3); + chans=malloc(sizeof(char *) * (argc+3) / 3); + int i=0; + printf("%d\n",argc); + for(i=0;((i*3)+3)<argc;i++) { + printf("%d server: %s port: %s channel: %s\n",i,argv[(i*3)+1],argv[(i*3)+2],argv[(i*3)+3]); + fds[i]=serverConnect(argv[(i*3)+1],argv[(i*3)+2]); + chans[i]=strdup(argv[(i*3)+3]); + mywrite(fds[i],"NICK link\r\nUSER a b c :d\r\n"); + } + fds[i]=-1; + //heh. you can write your own code for picking a different nick per server. fuck you. + runem(fds,line_handler,extra_handler); +} diff --git a/libirc/libirc.c b/libirc/libirc.c index d2bff54..7968f76 100644 --- a/libirc/libirc.c +++ b/libirc/libirc.c @@ -132,15 +132,14 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { return 0; } -#if 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; +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; char *backlog=malloc(CHUNK+1); char *t,*line=0; int blsize=CHUNK; @@ -149,92 +148,89 @@ int runem(int *fd,void (*line_handler)(),void (*extra_handler)()) { 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; + for(i=0;fds[i] != -1;i++) { + FD_SET(fds[i],&master); + fdmax=fds[i]>fdmax?fds[i]: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); - } + 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; + } + readfs=master; + timeout.tv_sec=0; + timeout.tv_usec=1000; + if( select(fdmax+1,&readfs,0,0,&timeout) == -1 ) { + printf("\n!!!It is rashing 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); } - memcpy(backlog+bllen,buffer,n); - bllen+=n; - for(i=0,s=0;i<bllen;i++) { - if(backlog[i]=='\n') { - line=malloc(i-s+3);//on linux it crashes without the +1 +3? weird. when did I do that? - if(!line) { - printf("ANOTHER malloc error!\n"); - exit(254); - } - memcpy(line,backlog+s,i-s+2); - line[i-s+1]=0;//gotta null terminate this. line_handler expects it . - s=i+1;//the character after the newline. - if(!strncmp(line,"PING",4)) { - t=malloc(strlen(line)); - strcpy(t,"PONG "); - strcat(t,line+6); - write(fd,t,strlen(t)); - //fprintf(fp,"PONG %s",line+6);//a whole FILE * and fdopen JUST for this??? oy... - //fflush(fp); + 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;i++) { + if(backlog[i]=='\n') { + line=malloc(i-s+3);//on linux it crashes without the +1 +3? weird. when did I do that? + if(!line) { + printf("ANOTHER malloc error!\n"); + exit(254); + } + memcpy(line,backlog+s,i-s+2); + line[i-s+1]=0;//gotta null terminate this. line_handler expects it . + s=i+1;//the character after the newline. + if(!strncmp(line,"PING",4)) { + t=malloc(strlen(line)); + strcpy(t,"PONG "); + strcat(t,line+6); + write(fd,t,strlen(t)); + //fprintf(fp,"PONG %s",line+6);//a whole FILE * and fdopen JUST for this??? oy... + //fflush(fp); #ifdef DEBUG - printf("%s\nPONG %s\n",line,line+6); - write(fd,"PRIVMSG %s :PONG! w00t!\r\n",DEBUG,28); + printf("%s\nPONG %s\n",line,line+6); + write(fd,"PRIVMSG %s :PONG! w00t!\r\n",DEBUG,28); #endif - } else if(!strncmp(line,"ERROR",5)) { + } else if(!strncmp(line,"ERROR",5)) { #ifdef DEBUG - printf("error: %s\n",line); + printf("error: %s\n",line); #endif - return 0; - } else { - line_handler(fd,line); - } - free(line); + return 0; + } else { + line_handler(fd,line); } + free(line); } - //left shift the backlog so the last thing we got to is at the start - if(s > 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; + } + //left shift the backlog so the last thing we got to is at the start + if(s > 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; } } } @@ -242,13 +238,8 @@ int runem(int *fd,void (*line_handler)(),void (*extra_handler)()) { } return 0; } -#endif -//: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. :/ +//not needed? int ircConnect(char *serv,char *port,char *nick,char *user) { char sendstr[1024]; int fd; @@ -260,4 +251,3 @@ int ircConnect(char *serv,char *port,char *nick,char *user) { write(fd,sendstr,strlen(sendstr)); return fd; } - |