diff --git a/Makefile b/Makefile index 4073bed..0435758 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,11 @@ REMOVE = rm -f #Objects -OBJS = server.o nick.o botthread.o main.o socket.o tuve.o channel.o +OBJS = mysql.o server.o nick.o botthread.o main.o socket.o tuve.o channel.o -LIBRARIES = -lthr +LIBRARIES = -lthr -L/usr/local/lib/mysql -lmysqlclient CFLAGS = -Wall -W -ggdb +INCLUDES = -I/usr/local/include # Link The Binary $(BINARY) : $(OBJS) diff --git a/botthread.c b/botthread.c index 4a24356..b9dfa7a 100644 --- a/botthread.c +++ b/botthread.c @@ -10,29 +10,47 @@ int tuveBotCMD(myConnections_t *userConnection,char *chan,char *data) { char output[512]; - char *cmd = 0x0; - char *cmdData = 0x0; + char *cmd = 0x0; + char *cmdData = 0x0; + char *modeString = 0x0; + char *tmp = 0x0; + char *tmp2 = 0x0; + char *tok_last = 0x0; tuveChanList_t *tmpChan = 0x0; + MYSQL_RES *res = 0x0; + MYSQL_ROW row; + short dM = 0x0; + unsigned int i = 0x0; if (data == 0x0) return(0x1); - cmd = strtok(data," "); - cmdData = strtok(NULL,"\n"); + cmd = strtok_r(data," ",&tok_last); + cmdData = strtok_r(NULL,"\n",&tok_last); if (strcasecmp(cmd,"echo") == 0x0) { sprintf(output,"MSG:@TUveBOT:%s:%s\n",chan,cmdData); tuveSendAllInChan(chan,0x0,output); } else if (strcasecmp(cmd,"skip") == 0x0) { - tmpChan = findChan(cmdData); + if (cmdData != 0x0) + tmpChan = findChan(cmdData); + else + tmpChan = findChan(chan); if (tmpChan != 0x0) { tmpChan->songEnd = 0x0; tmpChan->count = MAX_COUNT; } } else if (strcasecmp(cmd,"login") == 0x0) { - if (!strcmp(cmdData,"b00b5")) { + tok_last = 0x0; + tmp = strtok_r(cmdData," ",&tok_last); + tmp2 = strtok_r(NULL,"\n",&tok_last); + if ((tmp != 0x0) & (tmp2 != 0x0)) { + sprintf(output,"SELECT username FROM users WHERE username = '%s' AND pass = '%s'",tmp,tmp2); + res = dbQuery(output); + + if (mysql_num_rows(res) == 1) { sprintf(output,"MSG:@TUveBOT:%s:You have successfully logged in.\n",chan); sSendData(userConnection,output); sprintf(output,"EMOTE:%s:STATUS:Is now an Evil Overlord!\n",userConnection->userInfo.username); @@ -44,8 +62,121 @@ sSendData(userConnection,output); tuveDelUserChans(userConnection,"This Mother Fucker Tried To Login With The Wrong Password!"); userConnection->userInfo.status = -1; + } } } + else if (strcasecmp(cmd,"mode") == 0x0) { + tmpChan = findChan(chan); + + if (userConnection->userInfo.mode != 1) { + sSendData(userConnection,"MSG:@TUveBOT:STATUS:You do not have permission to set channel mode\n"); + if (tmpChan->modes[CHAN_TIME] == 1) { + sprintf(output,"MSG:TUveD:%s:This channel has a time limit of %i seconds for songs.\n",tmpChan->channel,tmpChan->maxTime); + sSendData(userConnection,output); + sprintf(output,"MODE:%s:T:1:%i\n",tmpChan->channel,tmpChan->maxTime); + sSendData(userConnection,output); + } + else { + sprintf(output,"MODE:%s:T:0\n",tmpChan->channel); + sSendData(userConnection,output); + } + + if (tmpChan->modes[CHAN_QUEUE] == 1) + sprintf(output,"MODE:%s:Q:1\n",tmpChan->channel); + else + sprintf(output,"MODE:%s:Q:0\n",tmpChan->channel); + sSendData(userConnection,output); + + if (tmpChan->modes[CHAN_RANDOM] == 1) + sprintf(output,"MODE:%s:R:1\n",tmpChan->channel); + else + sprintf(output,"MODE:%s:R:0\n",tmpChan->channel); + sSendData(userConnection,output); + + return(0x0); + } + if (cmdData != 0x0) { + tok_last = 0x0; + modeString = strtok_r(cmdData," ",&tok_last); + + if (modeString[0] == '-') + dM = 0; + else if (modeString[0] == '+') + dM = 1; + else { + sSendData(userConnection,"MSG:@TUveBOT:STATUS:Invalid Mode String\n"); + return(0x0); + } + + + for (i = 1;i < strlen(modeString);i++) { + switch(modeString[i]) { + case 'r': + case 'R': + tmpChan->modes[CHAN_RANDOM] = dM; + sprintf(output,"MSG:@TUveBOT:%s:%s Sets Mode %cRandom\n",chan,userConnection->userInfo.username,modeString[0]); + tuveSendAllInChan(chan,0x0,output); + sprintf(output,"MODE:%s:R:%i\n",tmpChan->channel,dM); + tuveSendAllInChan(chan,0x0,output); + break; + case 'q': + case 'Q': + tmpChan->modes[CHAN_QUEUE] = dM; + sprintf(output,"MSG:@TUveBOT:%s:%s Sets Mode %cQueue\n",chan,userConnection->userInfo.username,modeString[0]); + tuveSendAllInChan(chan,0x0,output); + sprintf(output,"MODE:%s:Q:%i\n",tmpChan->channel,dM); + tuveSendAllInChan(chan,0x0,output); + break; + case 't': + case 'T': + if (dM == 1) { + tmp = strtok_r(NULL," ",&tok_last); + if (tmp != 0x0) { + tmpChan->modes[CHAN_TIME] = dM; + tmpChan->maxTime = atoi(tmp); + sprintf(output,"MSG:@TUveBOT:%s:%s Sets Mode +Time Limit %s\n",chan,userConnection->userInfo.username,tmp); + tuveSendAllInChan(chan,0x0,output); + sprintf(output,"MODE:%s:T:%i:%s\n",tmpChan->channel,dM,tmp); + tuveSendAllInChan(chan,0x0,output); + } + } + else { + tmpChan->modes[CHAN_TIME] = dM; + sprintf(output,"MSG:@TUveBOT:%s:%s Sets Mode %cTime Limit\n",chan,userConnection->userInfo.username,modeString[0]); + tuveSendAllInChan(chan,0x0,output); + sprintf(output,"MODE:%s:T:%i\n",tmpChan->channel,dM); + tuveSendAllInChan(chan,0x0,output); + } + break; + default: + sSendData(userConnection,"MSG:@TUveBOT:STATUS:Invalid Channel Mode\n"); + break; + } + } + + + } + } + else if (strcasecmp(cmd,"random") == 0x0) { + if (userConnection->userInfo.mode != 1) { + sSendData(userConnection,"MSG:@TUveBOT:STATUS:You do not have permission to play a random song.\n"); + return(0x0); + } + + + res = dbQuery("SELECT artist,title,file,length FROM videos"); + if (mysql_num_rows(res) == 0x0) { + sprintf(output,"MSG:@TUveBOT:%s:Sorry I'm Have SQL Troubles Today.\n",chan); + tuveSendAllInChan(chan,0x0,output); + } + else { + mysql_data_seek(res,(random() % mysql_num_rows(res))); + row = mysql_fetch_row(res); + sprintf(output,"%s's Random Pick -> %s - %s",userConnection->userInfo.username,row[0],row[1]); + tuveBotSetSong(chan,row[3],row[2],output); + } + + } return(0x0); } @@ -63,8 +194,12 @@ } int botNextSong() { + char output[1024]; tuveChanList_t *tmpChans; tuveUserList_t *tmpUser; + MYSQL_RES *res = 0x0; + MYSQL_ROW row; + pthread_mutex_lock(&chanMutex); @@ -72,6 +207,8 @@ if (tmpChans->songEnd < time(NULL)) { tmpUser = tmpChans->nextUser; findUser: + if (tmpChans->modes[CHAN_QUEUE] == 0x0) + goto getRandom; if (tmpChans->nextUser != 0x0) { if (tmpChans->count == MAX_COUNT) { tmpChans->nextUser = tmpChans->nextUser->next; @@ -92,6 +229,21 @@ tmpChans->count = MAX_COUNT; if (tmpUser != tmpChans->nextUser) goto findUser; + getRandom: + if (tmpChans->modes[CHAN_RANDOM] == 0x1) { + res = dbQuery("SELECT artist,title,file,length FROM videos"); + if (mysql_num_rows(res) == 0x0) { + sprintf(output,"MSG:@TUveBOT:%s:Sorry I'm Have SQL Troubles Today.\n",tmpChans->channel); + tuveSendAllInChan(tmpChans->channel,0x0,output); + } + else { + mysql_data_seek(res,(random() % mysql_num_rows(res))); + row = mysql_fetch_row(res); + sprintf(output,"TUveBot's Pick -> %s - %s",row[0],row[1]); + tuveBotSetSong(tmpChans->channel,row[3],row[2],output); + } + } + } } } @@ -135,7 +287,16 @@ songCount++; playCount += atoi(playTime); - sprintf(output,"CURPOS:0:%s:%s\n",file,title); + tmpChan = findChan(chan); + + if (tmpChan->modes[CHAN_TIME] == 0x1) { + if (atoi(playTime) > tmpChan->maxTime) { + return(0x0); + } + } + + + sprintf(output,"CURPOS:0:%s:%s:%s\n",file,title,playTime); writeLog(0,"[%s][%s]\n",output,chan); tuveSendAllInChan(chan,0x0,output); diff --git a/channel.c b/channel.c index 2f1a57e..f515ab1 100644 --- a/channel.c +++ b/channel.c @@ -85,7 +85,7 @@ tmpChan = findChan(channel); assert(tmpChan); - sprintf(output,"CURPOS:%i:%s:%s\n",tmpChan->songTime - (tmpChan->songEnd - (time(NULL) + 2)),tmpChan->curSong,tmpChan->songTitle); + sprintf(output,"CURPOS:%i:%s:%s:%i\n",tmpChan->songTime - (tmpChan->songEnd - (time(NULL) + 2)),tmpChan->curSong,tmpChan->songTitle,tmpChan->songTime); sSendData(userConnection,output); min = tmpChan->songTime / 60; @@ -190,7 +190,7 @@ for (;tmpUsers != 0x0;tmpUsers = tmpUsers->next) { if (!strcasecmp(tmpUsers->user->userInfo.username,userConnection->userInfo.username)) { - printf("Found and removed user: %s\n",tmpUsers->user->userInfo.username); + writeLog(3,"Found and removed user: %s\n",tmpUsers->user->userInfo.username); if (tmpUsers->prev != 0x0) tmpUsers->prev->next = tmpUsers->next; @@ -213,7 +213,7 @@ tmpChannel->userCount--; free(tmpUsers); - printf("User Freed\n"); + writeLog(3,"User Freed\n"); pthread_mutex_unlock(&chanMutex); return(0x0); } @@ -304,6 +304,30 @@ sprintf(output,"MSG:TUveD:%s:Topic was set by %s %i seconds ago.\n",tmpChannel->channel,tmpChannel->topicSetBy,time(NULL) - tmpChannel->topicSet); sSendData(userConnection,output); + if (tmpChannel->modes[CHAN_TIME] == 1) { + sprintf(output,"MSG:TUveD:%s:This channel has a time limit of %i seconds for songs.\n",tmpChannel->channel,tmpChannel->maxTime); + sSendData(userConnection,output); + sprintf(output,"MODE:%s:T:1:%i\n",tmpChannel->channel,tmpChannel->maxTime); + sSendData(userConnection,output); + } + else { + sprintf(output,"MODE:%s:T:0\n",tmpChannel->channel); + sSendData(userConnection,output); + } + + if (tmpChannel->modes[CHAN_QUEUE] == 1) + sprintf(output,"MODE:%s:Q:1\n",tmpChannel->channel); + else + sprintf(output,"MODE:%s:Q:0\n",tmpChannel->channel); + sSendData(userConnection,output); + + if (tmpChannel->modes[CHAN_RANDOM] == 1) + sprintf(output,"MODE:%s:R:1\n",tmpChannel->channel); + else + sprintf(output,"MODE:%s:R:0\n",tmpChannel->channel); + sSendData(userConnection,output); + + writeLog(2,"Added user: %s to Channel: %s\n",userConnection->userInfo.username,channel); pthread_mutex_unlock(&chanMutex); @@ -344,6 +368,7 @@ channels->next = 0x0; channels->prev = 0x0; channels->users = 0x0; + channels->modes[CHAN_QUEUE] = 0x1; sprintf(channels->channel,channel); sprintf(channels->topic,"No Topic Set"); sprintf(channels->topicSetBy,"TUveD"); @@ -366,6 +391,7 @@ tmpChannel->next = channels; tmpChannel->prev = 0x0; tmpChannel->users = 0x0; + tmpChannel->modes[CHAN_QUEUE] = 0x1; channels->prev = tmpChannel; channels = tmpChannel; writeLog(2,"ADDED CHAN2\n"); diff --git a/main.c b/main.c index 506a498..bdf595a 100644 --- a/main.c +++ b/main.c @@ -70,6 +70,10 @@ writeLog(0,"Podz Daemon Starting: %i\n",startTime); + dbInit(); + + srandom(time(NULL)); + pthread_mutex_init(&chanMutex, NULL); pthread_t threads[NUM_THREADS]; diff --git a/tuved.h b/tuved.h index e1f66d0..7dcab1c 100644 --- a/tuved.h +++ b/tuved.h @@ -11,6 +11,7 @@ #include #include #include +#include #define NUM_THREADS 1 #define MYPORT 9999 // the port users will be connecting to @@ -24,6 +25,13 @@ #define MAX_USER_LEN 32 #define MAX_BANS 30 #define VERSION "v0.40" +#define MYSQL_HOST_NAME "www.ubixonline.com" +#define MYSQL_USERNAME "tuve" +#define MYSQL_PASSWORD "5558585" +#define MYSQL_DB_NAME "tuve" +#define CHAN_RANDOM 0 +#define CHAN_QUEUE 1 +#define CHAN_TIME 2 typedef struct tuveUserList { struct tuveUserList *prev; @@ -37,6 +45,7 @@ struct tuveUserList *users; struct tuveUserList *nextUser; long long bans[MAX_BANS]; + char modes[10]; time_t topicSet; char topicSetBy[MAX_USER_LEN]; char topic[MAX_TOPIC_LEN]; @@ -47,6 +56,7 @@ int songTime; int userCount; short count; + short maxTime; } tuveChanList_t; typedef struct tuveUserChans { @@ -146,3 +156,7 @@ /* Server Functions */ int tuveStatus(myConnections_t *userConnection); + +/* Database Functions */ +int dbInit(); +MYSQL_RES *dbQuery(const char *query);