aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorepoch <epoch@hack.thebackupbox.net>2019-05-26 01:10:22 +0000
committerepoch <epoch@hack.thebackupbox.net>2019-05-26 01:10:22 +0000
commit163e3d34ddb7052701f4cefa7d429ffbd1a88534 (patch)
treea43ba627a6ec636785fcd78f5dc8cab1d6426ca2
parentf0e35906a0376e3dd659b58c0cb858f93fd58765 (diff)
downloadlibirc-163e3d34ddb7052701f4cefa7d429ffbd1a88534.tar.gz
libirc-163e3d34ddb7052701f4cefa7d429ffbd1a88534.zip
added some dumb use of globals and made libidc do the hard part
-rw-r--r--libirc.c135
1 files changed, 40 insertions, 95 deletions
diff --git a/libirc.c b/libirc.c
index f98ce9a..e35d732 100644
--- a/libirc.c
+++ b/libirc.c
@@ -1,4 +1,5 @@
#define _GNU_SOURCE //I want memmem out of string.h
+#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
@@ -10,6 +11,9 @@
#include <unistd.h>
#include <sys/select.h>
#include <sys/un.h>
+
+//epoch's lib includes:
+#include <idc.h> //going to use this for mainloop instead of writing one in here.
#include "irc.h"
//#define DEBUG "epoch" //nick or channel to send debug info to.
@@ -17,6 +21,8 @@
#define SILLYLIMIT 1024
+extern struct global libline;
+
/* 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
@@ -94,7 +100,6 @@ int fdlen(int *fds) {
return i+1;
}
-
char *memstr(char *s,char *find,size_t l) {
return memmem(s,l,find,strlen(find));
}
@@ -114,104 +119,44 @@ char *memmem(char *s,char *find,size_t sl,size_t fl) {
}
*/
-int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) {
- int j;
- int hack;
- int fdl=fdlen(fds);
- fd_set master;
- fd_set readfs;
- struct timeval timeout;
- int fdmax=0,n,i;
- char tmp[256];
- char *backlogs[fdl];
- char *t,*line=0;
- int blsize[fdl];
- int bllen[fdl];
- char buffers[fdl][CHUNK];//THIS IS *NOT* NULL TERMINATED.
- if(fds[0] == -1) return 0;
- FD_ZERO(&master);
- FD_ZERO(&readfs);
- for(i=0;fds[i] != -1;i++) {
- FD_SET(fds[i],&master);
- backlogs[i]=malloc(CHUNK);
- memset(backlogs[i],0,CHUNK);
- memset(buffers[i],0,CHUNK);
- blsize[i]=CHUNK;
- bllen[i]=0;
- fdmax=fds[i]>fdmax?fds[i]:fdmax;
- }
- for(;;) {
- readfs=master;
- timeout.tv_sec=0;
- timeout.tv_usec=1000;
- if((j=select(fdmax+1,&readfs,0,0,&timeout)) == -1 ) {
- //let's just ignore it for now. :>
- //return perror("select"),1;
- continue;
- }
- 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.
- for(i=0;fds[i] != -1;i++) {
- if(!FD_ISSET(fds[i],&readfs)) continue;
- if((n=read(fds[i],buffers[i],CHUNK)) <= 0) {
- snprintf(tmp,sizeof(tmp)-1,"fd %d: read",fds[i]);//hopefully this doesn't error and throw off error messages.
- perror(tmp);
- return 2;
- }
- if(bllen[i]+n > blsize[i]) {//this is probably off...
- t=malloc(blsize[i]+n);
- if(!t) exit(253);
- memcpy(t,backlogs[i],blsize[i]);
- blsize[i]+=n;
- free(backlogs[i]);
- backlogs[i]=t;
- }
- memcpy(backlogs[i]+bllen[i],buffers[i],n);
- bllen[i]+=n;
+void (*g_line_handler)(); //kek
+void (*g_extra_handler)(); //kek
- //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((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;
- *t=0;
- if(line_handler) line_handler(fds[i],line);
- }
- bllen[i]-=((t+2)-backlogs[i]);
- if(bllen[i] <= 0) bllen[i]=0;
- else memmove(backlogs[i],(t+2),bllen[i]);
- }
+int g_extra_fd;
-*/
- 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]);
- }
+void irc_handler(struct shit *me,char *line) {
+ fprintf(stderr,"debug: %s\n",line);
+ if(!strncmp(line,"PING ",5)) {
+ //I write back to the me->fd
+ fprintf(stderr,"libirc: GOT A PING. SENDING PONG.\n");
+ dprintf(me->fd,"PONG :%s\r\n",line+5);
+ return;//we probably don't need to let the bot know that it pinged. right?
+ }
+ //I need a way to get the line_handler passed to runem in here.
+ g_line_handler(me->fd,line);
+ //if(g_extra_handler) g_extra_handler(me->fd);///haxxxxxxxx fuck me.
+ //GLOBALS. OFC. THAT IS THE RIGHT WAY TO DO IT. :D :D :D :D :D :D :D :|
+}
+void alarm_handler(int sig) {
+ g_extra_handler(g_extra_fd);//???
+ alarm(1);//lol
+}
+
+//is this really good enough to work? it doesn't have extra_handler use though.
+int runem(int *fds,void (*line_handler)(),void (*extra_handler)()) { //wrap select_everything() so runem() still works
+ g_line_handler=line_handler;
+ g_extra_handler=extra_handler;
+ g_extra_fd=fds[0];
+ int i;
+ //signal(SIGALRM,alarm_handler);
+ //alarm(1);
+ //initialization of this can't be in here. this gets ran after a tail is added.
+ for(i=0;fds[i] != -1;i++) {
+ add_fd(fds[i],irc_handler);
}
- }
- return 0;
+ select_on_everything();
+ return 0;
}
//wrap runem to keep runit around :P