summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libirc/examples/link.c12
-rw-r--r--libirc/libirc.c61
-rw-r--r--segfault.c19
3 files changed, 61 insertions, 31 deletions
diff --git a/libirc/examples/link.c b/libirc/examples/link.c
index 0c9e7dd..c175595 100644
--- a/libirc/examples/link.c
+++ b/libirc/examples/link.c
@@ -40,6 +40,7 @@ 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 tmp[512];
struct user *user=malloc(sizeof(struct user));
user->nick=0;
@@ -81,6 +82,8 @@ void line_handler(int fd,char *line) {//this should be built into the libary?
if(!user->user && s) {
if(!strcmp(s,"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);
}
}
@@ -93,7 +96,14 @@ void line_handler(int fd,char *line) {//this should be built into the libary?
if(!strcmp(s,"JOIN")) {
snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has joined %s%c",1,user->nick,t+(*t==':'),1);
privmsg_others(fd,tmp);
- //send a join message to the other end.
+ }
+ if(!strcmp(s,"PART")) {
+ snprintf(tmp,sizeof(tmp)-1,"%cACTION %s has parted %s%c",1,user->nick,t+(*t==':'),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);
+ privmsg_others(fd,tmp);
}
}
free(user);
diff --git a/libirc/libirc.c b/libirc/libirc.c
index fad958d..2394459 100644
--- a/libirc/libirc.c
+++ b/libirc/libirc.c
@@ -37,28 +37,35 @@ int serverConnect(char *serv,char *port) {
return p?fd:0;
}
-//yeah. this is a copy of the previous function.
-//with a bit of different stuff. didn't want to break anything yet.
+int fdlen(int *fds) {
+ int i;
+ for(i=0;fds[i] != -1;i++);
+ return i+1;
+}
+
int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
+ int j;
+ int fdl=fdlen(fds);
fd_set master;
fd_set readfs;
struct timeval timeout;
int fdmax=0,n,s,i;
int fd;
- char *backlog=malloc(CHUNK+1);
+ char *backlogs[fdl];
char *t,*line=0;
int blsize=CHUNK;
int bllen=0;
- char buffer[CHUNK];//THIS IS *NOT* NULL TERMINATED.
- if(!backlog) return 252;
+ char buffers[fdl][CHUNK];//THIS IS *NOT* NULL TERMINATED.
FD_ZERO(&master);
FD_ZERO(&readfs);
for(i=0;fds[i] != -1;i++) {
+ if(!backlogs[i]) return 252;
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;
}
- memset(backlog,0,CHUNK);
- memset(buffer,0,CHUNK);
int done=0;
while(!done) {
for(fd=0;fd<=fdmax;fd++) {
@@ -74,14 +81,14 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
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
+ 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
fprintf(stderr,"recv: %d\n",n);
perror("recv");
return 2;
} else {
- buffer[n]=0;//deff right.
+ buffers[i][n]=0;//deff right.
if(bllen+n >= blsize) {//this is probably off...
blsize+=n;
t=malloc(blsize);
@@ -90,30 +97,30 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
exit(253);
}
memset(t,0,blsize);//optional?
- memcpy(t,backlog,blsize-n+1);//???
- free(backlog);
- backlog=t;
+ memcpy(t,backlogs[i],blsize-n+1);//???
+ free(backlogs[i]);
+ backlogs[i]=t;
}
- memcpy(backlog+bllen,buffer,n);
+ memcpy(backlogs[i]+bllen,buffers[i],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?
+ for(j=0,s=0;j<bllen;j++) {
+ if(backlogs[i][j]=='\n') {
+ line=malloc(j-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.
+ memcpy(line,backlogs[i]+s,j-s+2);
+ line[j-s+1]=0;//gotta null terminate this. line_handler expects it .
+ s=j+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));
+ write(fds[i],t,strlen(t));
#ifdef DEBUG
printf("%s\nPONG %s\n",line,line+6);
- write(fd,"PRIVMSG %s :PONG! w00t!\r\n",DEBUG,28);
+ write(fds[i],"PRIVMSG %s :PONG! w00t!\r\n",DEBUG,28);
#endif
} else if(!strncmp(line,"ERROR",5)) {
#ifdef DEBUG
@@ -121,7 +128,7 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
#endif
return 0;
} else {
- line_handler(fd,line);
+ line_handler(fds[i],line);
}
free(line);
}
@@ -130,8 +137,8 @@ int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
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];
+ for(j=s;j<=bllen;j++) {//should work.
+ backlogs[i][j-s]=backlogs[i][j];
}
bllen-=s;
}
@@ -147,7 +154,7 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) {
int fds[2];
fds[0]=fd;
fds[1]=-1;
- runem(fds,line_handler,extra_handler);
+ return runem(fds,line_handler,extra_handler);
}
//not needed?
diff --git a/segfault.c b/segfault.c
index 8013e0b..a371eb1 100644
--- a/segfault.c
+++ b/segfault.c
@@ -360,7 +360,8 @@ void file_tail(int fd,char *from,char *file,char *args,int opt,struct user *user
privmsg(fd,"#cmd",tmp);
}
fstat(fdd,&st); // <-- is this needed?
- /*for(j=0;j<maxtails;j++) {
+ /*
+ for(j=0;j<maxtails;j++) {
if(tailf[j].fp && tailf[j].file && tailf[j].inode) {
if(tailf[j].inode == st.st_ino) {
if(debug) privmsg(fd,from,"THIS FILE IS ALREADY BEING TAILED ELSEWHERE!");
@@ -379,7 +380,12 @@ void file_tail(int fd,char *from,char *file,char *args,int opt,struct user *user
eofp(tailf[i].fp);
}
if(!from) exit(73);
- if(tailf[i].to) free(tailf[i].to);
+ if(tailf[i].to) {
+ snprintf(tmp,sizeof(tmp),"tailf[%d].to: %s from: %s",i,tailf[i].to,from);
+ privmsg(fd,"#default",tmp);
+ free(tailf[i].to); //commenting this out stopped a buffer overflow. >_> weird.
+ tailf[i].to=0;
+ }
tailf[i].to=strdup(from);//if this properly free()d before being assigned to?
if(!tailf[i].to) {
mywrite(fd,"QUIT :malloc error 3!!!\r\n");
@@ -494,6 +500,7 @@ void c_changetail(int fd,char *from,char *line,struct user *user,...) {
if(tailf[i].file) {
if(!strcmp(tailf[i].file,line) || tailf[i].inode == st.st_ino) {
free(tailf[i].to);
+ tailf[i].to=0;
if(!merp) exit(76);
tailf[i].to=strdup(merp);
if(mode) {
@@ -697,7 +704,9 @@ void c_leetuntail(int fd,char *from,char *line,...) {
}
tailf[i].fp=0;
free(tailf[i].to);
+ tailf[i].to=0;
free(tailf[i].file);
+ tailf[i].file=0;
return;
}
}
@@ -1106,8 +1115,12 @@ void line_handler(int fd,char *line) {//this should be built into the libary?
//:armitage.hacking.allowed.org 353 asdf = #default :@SegFault @FreeArtMan @foobaz @wall @Lamb3_13 @gizmore @blackh0le
strcpy(tmp,"!");
strcat(tmp,s);
+ if(ht_getnode(&alias,tmp) == NULL && ht_getnode(&alias,"!###") != NULL) {
+ strcpy(tmp,"!###");
+ }
if(ht_getnode(&alias,tmp) != NULL) {
- snprintf(tmp,sizeof(tmp),"!%s %s",s,u);
+ strcat(tmp," ");
+ strcat(tmp,u);//lol. fixme later.
user->nick=strdup("epoch");
user->user=strdup("epoch");
user->host=strdup("localhost");