diff --git a/botthread.c b/botthread.c index 7f1059c..c6e3587 100644 --- a/botthread.c +++ b/botthread.c @@ -28,7 +28,6 @@ void *tuveBotThread(void *threadid) { int tid; - char output[512]; tid = (int)threadid; writeLog(0,"Starting Bot Thread: [%d]\n", tid); @@ -43,32 +42,46 @@ tuveChanList_t *tmpChans; for (tmpChans = channels;tmpChans != 0x0;tmpChans = tmpChans->next) { - if (tmpChans->songEnd < time(NULL)) { - writeLog(0,"Need To Find Next Song For: %s\n",tmpChans->channel); + if (tmpChans->songEnd < time(NULL)) { + if (tmpChans->nextUser != 0x0) { + if (tmpChans->count == MAX_COUNT) { + tmpChans->nextUser = tmpChans->nextUser->next; + if (tmpChans->nextUser == 0x0) + tmpChans->nextUser = tmpChans->users; + tmpChans->count = 0x0; + } + else { + sSendData(tmpChans->nextUser->user,"GETSONG\n"); + writeLog(0,"Need To Find Next Song For: %s, From User: %s\n",tmpChans->channel,tmpChans->nextUser->user->userInfo.username); + tmpChans->count++; + } + } + else + writeLog(0,"NO Users In: %s\n",tmpChans->channel); } } + return(0x0); } -void *PrintHello(void *threadid) { - int tid; - tid = (int)threadid; - printf("Hello World! It's me, thread #%d!\n", tid); - pthread_exit(NULL); +int tuveBotSetSong(char *chan,char *playTime,char *file) { + tuveChanList_t *tmpChan; + + char output[1024]; + sprintf(output,"CURPOS:1:%s\n",file); + writeLog(0,"[%s][%s]\n",output,chan); + tuveSendAllInChan(chan,0x0,output); + + tmpChan = findChan(chan); + tmpChan->songTime = atoi(playTime); + tmpChan->songEnd = atoi(playTime) + time(NULL) + 2; + sprintf(tmpChan->curSong,file); + + tmpChan->nextUser = tmpChan->nextUser->next; + if (tmpChan->nextUser == 0x0) + tmpChan->nextUser = tmpChan->users; + + tmpChan->count = 0x0; + + return(0x0); } -/* -int main (int argc, char *argv[]) -{ - pthread_t threads[NUM_THREADS]; - int rc, t; - for(t=0; tuserInfo.username); tuveSendAllInChan(channel,userConnection,output); - sprintf(output,"SEEK:67\n"); + tmpChan = findChan(channel); + + sprintf(output,"CURPOS:%i:%s\n",tmpChan->songTime - (tmpChan->songEnd - (time(NULL) + 2)),tmpChan->curSong); sSendData(userConnection,output); @@ -109,6 +112,16 @@ if (tmpUsers->next != 0x0) tmpUsers->next->prev = tmpUsers->prev; + if (tmpChannel->nextUser == tmpUsers) { + if (tmpUsers->next != 0x0) + tmpChannel->nextUser = tmpUsers->next; + else if (tmpChannel->users != 0x0) + tmpChannel->nextUser = tmpChannel->users; + else + tmpChannel->nextUser = 0x0; + } + tmpChannel->count = 0x0; + free(tmpUsers); printf("User Freed\n"); return(0x0); @@ -151,6 +164,9 @@ tmpChannel->users = tmpUsers; } + tmpChannel->nextUser = tmpUsers; + tmpChannel->count = 0x0; + memset(output,0x0,256); sprintf(output,"JOIN:%s:TUveBOT",tmpChannel->channel); diff --git a/tuve.c b/tuve.c index 038264b..07b55a5 100644 --- a/tuve.c +++ b/tuve.c @@ -137,6 +137,13 @@ writeLog(1,"%s Has Sent CURPOS\n",userConnection->userInfo.username); } } + else if (strcasecmp(cmd,"NEXTSONG") == 0x0) { + tok_last = 0x0; + chan = strtok_r(data,":",&tok_last); + msg = strtok_r(NULL,":",&tok_last); + nick = strtok_r(NULL,"\n",&tok_last); + tuveBotSetSong(chan,msg,nick); + } else { sSendData(userConnection,"invalid command\n"); writeLog(1,"Invalid CMD: [%s]\n",cmd); diff --git a/tuved.h b/tuved.h index 9c1af80..a6b1bc3 100644 --- a/tuved.h +++ b/tuved.h @@ -9,12 +9,14 @@ #include #include #include +#include #define NUM_THREADS 1 #define MYPORT 9999 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold #define LOG_FILE "./tuved.log" #define PING_INTERVAL 20 +#define MAX_COUNT 2 typedef struct tuveUserList { struct tuveUserList *prev; @@ -23,13 +25,16 @@ } tuveUserList_t; typedef struct tuveChanList { - struct tuveChanList *prev; - struct tuveChanList *next; - struct tuveUserList *users; + struct tuveChanList *prev; + struct tuveChanList *next; + struct tuveUserList *users; + struct tuveUserList *nextUser; char topic[128]; char channel[32]; char curSong[256]; int songEnd; + int songTime; + short count; } tuveChanList_t; typedef struct tuveUserChans { @@ -65,6 +70,7 @@ extern int highSock; extern myConnections_t *connections; extern FILE *logFile; +extern tuveChanList_t *channels; /* Socket Functions */ ssize_t sReadSocket(int socketFD,void *buffer,size_t length); @@ -94,7 +100,9 @@ int tuveRemoveFromChanList(char *channel,myConnections_t *userConnection); int tuveSetTopic(myConnections_t *userConnection,char *chan,char *topic); int tuveSendAllInUsersChans(myConnections_t *userConnection,char *output); +tuveChanList_t *findChan(char *); /* Bot Functions */ void *tuveBotThread(void *threadid); int tuveBotCMD(char *chan,char *data); +int tuveBotSetSong(char *chan,char *playTime,char *file);