diff --git a/Makefile b/Makefile index 1f902ec..6458689 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ REMOVE = rm -f #Objects -OBJS = main.o socket.o +OBJS = bot.o mysql.o main.o socket.o LIBRARIES = -lthr -L/usr/local/lib/mysql -lmysqlclient CFLAGS = -Wall -W -ggdb diff --git a/main.c b/main.c index 795c513..698cf82 100644 --- a/main.c +++ b/main.c @@ -22,8 +22,11 @@ int main(int argc,char **argv) { int ch; int doFork = 0x1; + int readSocks = 0x0; fd_set readset; + fd_set faultset; char data[1024]; + struct timeval timeout = {30,0}; /* Get our start time */ startTime = time(NULL); @@ -62,20 +65,35 @@ exit(1); writeLog(0,"TUveBOT Starting: %i\n",startTime); + + dbInit(); srandom(time(NULL)); - sStartSocket(); + sConnect(SERVER_HOST,SERVER_PORT); - sWriteSocket("IDENT TUveBOT:1234567\n"); while (1) { sGetConnections(&readset); + sGetConnections(&faultset); + readSocks = select(highSock+1,&readset,0x0,&faultset,&timeout); + + if (readSocks < 0) { + perror("select failed"); + exit(0x1); + } + + if (FD_ISSET(mySocket,&faultset) != 0x0) { + writeLog(0,"Socket Died!\n"); + sConnect(SERVER_HOST,SERVER_PORT); + } + if (FD_ISSET(mySocket,&readset) != 0x0) { memset(&data,0x0,1024); if (sReadSocket(&data,1024) == -1) { writeLog(0,"Socket Died!\n"); - exit(1); + sConnect(SERVER_HOST,SERVER_PORT); + //exit(1); } else { botProcessData(data); @@ -132,19 +150,29 @@ char *cmdData = 0x0; char *str_tok = 0x0; + cmd = strtok_r(data,":",&str_tok); cmdData = strtok_r(NULL,"\n",&str_tok); + if (cmd == 0x0) + return(0x0); + if (strstr(cmd,"PING")) { sWriteSocket("PONG!\n"); } else if (!strcmp(cmd,"IDENT")) { - sWriteSocket("JOIN #general\n"); sWriteSocket("MSG TUveD:.tv login reddawg temp123\n"); + botJoinChans(); } else if (!strcmp(cmd,"MSG")) { botProcessMsg(cmdData); } + else if (!strcmp(cmd,"TOPIC")) { + botStoreTopic(cmdData); + } + else if (!strcmp(cmd,"CURPOS")) { + botSetCurSong(cmdData); + } else { writeLog(0,"Unknown Command: [%s]\n",cmd); } diff --git a/socket.c b/socket.c index 62cc4cd..30bbba5 100644 --- a/socket.c +++ b/socket.c @@ -18,19 +18,17 @@ int mySocket = 0x0; -int sStartSocket() { +int sConnect(const char *host,int port) { struct sockaddr_in rmAddr; // remote address information struct hostent *hp; + if (mySocket != 0x0) + close(mySocket); + if ((mySocket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } - - return(sConnect(SERVER_HOST,SERVER_PORT); - } - -int sConnect(const char *host,int port) { hp = gethostbyname(host); @@ -40,10 +38,15 @@ rmAddr.sin_port = htons(port); // short, network byte order memset(rmAddr.sin_zero, '\0', sizeof rmAddr.sin_zero); - connect(mySocket,&rmAddr,sizeof(rmAddr)); + while (connect(mySocket,&rmAddr,sizeof(rmAddr)) != 0x0) { + writeLog(1,"Error Connecting.\n"); + sleep(5); + } writeLog(2,"my Socket: [%i]\n",mySocket); + sWriteSocket("IDENT TUveBOT:1234567\n"); + return(0x0); } @@ -51,6 +54,8 @@ ssize_t recLen = 0x0; recLen = read(mySocket,buffer,length); + if (recLen == 0x0) + return(-1); return(recLen); } diff --git a/tuvebot.h b/tuvebot.h index 5732ddb..3488fc5 100644 --- a/tuvebot.h +++ b/tuvebot.h @@ -29,7 +29,7 @@ #define MAX_USER_LEN 32 #define MAX_BANS 30 #define VERSION "v0.40" -#define MYSQL_HOST_NAME "www.ubixonline.com" +#define MYSQL_HOST_NAME "ubuse.ubixos.com" #define MYSQL_USERNAME "tuve" #define MYSQL_PASSWORD "5558585" #define MYSQL_DB_NAME "tuve" @@ -108,7 +108,6 @@ extern time_t startTime; /* Socket Functions */ -int sStartSocket(); int sConnect(const char *host,int port); ssize_t sReadSocket(void *buffer,size_t length); int sGetConnections(fd_set *readset); @@ -146,14 +145,10 @@ int tuveBotSetSong(char *chan,char *playTime,char *file,char *title); int tuveBotNoSong(char *chan); -/* Nick Functions */ -int tuveVerifyNick(char *nick); -myConnections_t *findNick(const char *); -int tuveWhois(myConnections_t *userConnection,char *nick); - -/* Server Functions */ -int tuveStatus(myConnections_t *userConnection); - /* Database Functions */ int dbInit(); MYSQL_RES *dbQuery(const char *query); + +/* Bot Functions */ +int botJoinChans(); +int botStoreTopic(char *data);