#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "tuved.h"
char nextSong[256];
int botNextSong();
int tuveBotCMD(myConnections_t *userConnection,char *chan,char *data) {
char output[512];
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_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) {
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) {
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);
tuveSendAllInUsersChans(userConnection,output);
userConnection->userInfo.mode = 1;
}
else {
sprintf(output,"MSG:@TUveBOT:%s:You have failed login. Your connection will now be terminated!\n",chan);
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);
}
void *tuveBotThread(void *threadid) {
int tid;
tid = (int)threadid;
writeLog(0,"Starting Bot Thread: [%d]\n", tid);
while (1) {
sleep(5);
botNextSong();
}
pthread_exit(NULL);
}
int botNextSong() {
char output[1024];
tuveChanList_t *tmpChans;
tuveUserList_t *tmpUser;
MYSQL_RES *res = 0x0;
MYSQL_ROW row;
pthread_mutex_lock(&chanMutex);
for (tmpChans = channels;tmpChans != 0x0;tmpChans = tmpChans->next) {
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;
if (tmpChans->nextUser == 0x0)
tmpChans->nextUser = tmpChans->users;
tmpChans->count = 0x0;
if (tmpChans->nextUser != 0x0)
goto gotUser;
}
else {
gotUser:
if (tmpChans->nextUser->user->userInfo.queue == 0x1) {
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 {
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);
}
}
}
}
}
else
writeLog(0,"NO Users In: %s\n",tmpChans->channel);
}
}
pthread_mutex_unlock(&chanMutex);
return(0x0);
}
int tuveBotNoSong(char *chan) {
tuveChanList_t *tmpChan;
tmpChan = findChan(chan);
if (tmpChan == 0x0) {
writeLog(0,"ERROR: tuveBotNoSong tmpChan Failed\n");
return(0x1);
}
tmpChan->nextUser = tmpChan->nextUser->next;
if (tmpChan->nextUser == 0x0)
tmpChan->nextUser = tmpChan->users;
if (tmpChan->nextUser != 0x0)
sSendData(tmpChan->nextUser->user,"GETSONG\n");
return(0x0);
}
int
tuveBotSetSong(char *chan,char *playTime,char *file,char *title) {
tuveChanList_t *tmpChan;
char output[1024];
int min, sec;
songCount++;
playCount += atoi(playTime);
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);
min = strtol(playTime, (char **)NULL, 10) / 60;
sec = strtol(playTime, (char **)NULL, 10) % 60;
sprintf(output,"MSG:@TUveBOT:%s:Playing %s (%d:%.2d)\n",chan,title,min,sec);
tuveSendAllInChan(chan,0x0,output);
tmpChan = findChan(chan);
tmpChan->songTime = atoi(playTime);
tmpChan->songEnd = atoi(playTime) + time(NULL) + 2;
sprintf(tmpChan->curSong,file);
sprintf(tmpChan->songTitle,title);
tmpChan->nextUser = tmpChan->nextUser->next;
if (tmpChan->nextUser == 0x0)
tmpChan->nextUser = tmpChan->users;
tmpChan->count = 0x0;
return(0x0);
} // tuveBotSetSong()