diff --git a/bot.c b/bot.c index cb0092d..8e52341 100644 --- a/bot.c +++ b/bot.c @@ -61,95 +61,117 @@ } int botJoinChans() { - MYSQL_RES *res = 0x0; + MYSQL_RES *res = 0x0; + MYSQL_RES *res2 = 0x0; MYSQL_ROW row; unsigned int i = 0x0; + unsigned int x = 0x0; int tmpVal = 0; botChanList_t *tmpChan = 0x0; + char qryStr[256]; - res = dbQuery("SELECT channel,rating,classification,random,queue,exclusive,time,noads,topic FROM channels",1); + res = dbQuery("SELECT channel,rating,classification,random,queue,exclusive,time,noads,topic,cid FROM channels",1); + + joinChan = 2; if (mysql_num_rows(res) > 0) { - for (i = 0;i < mysql_num_rows(res);i++) { - row = mysql_fetch_row(res); - tmpChan = botAddChan(row[0]); - if (tmpChan == 0x0) - return(-1); + for (i = 0;i < mysql_num_rows(res);i++) { + + row = mysql_fetch_row(res); + tmpChan = botAddChan(row[0]); + + if (tmpChan == 0x0) + return(-1); - sWriteSocket("JOIN %s",row[0]); - sleep(2); + sWriteSocket("JOIN %s",row[0]); + sleep(2); - pthread_mutex_lock(&chanMutex); + pthread_mutex_lock(&chanMutex); - /* Op Myself */ - sWriteSocket("MSG %s:.tv MODE +O TUveBOT",row[0]); + /* Op Myself */ + sWriteSocket("MSG %s:.tv MODE +O TUveBOT",row[0]); - /* Set Access Level */ - tmpVal = atoi(row[1]); - if (tmpVal > 0) - sWriteSocket("MSG %s:.tv mode +A %i",row[0],tmpVal); - else - sWriteSocket("MSG %s:.tv mode -A",row[0]); - tmpChan->modes[CHAN_RATING] = tmpVal; + /* Set Access Level */ + tmpVal = atoi(row[1]); + if (tmpVal > 0) + sWriteSocket("MSG %s:.tv mode +A %i",row[0],tmpVal); + else + sWriteSocket("MSG %s:.tv mode -A",row[0]); + tmpChan->modes[CHAN_RATING] = tmpVal; - /* Set Classification */ - tmpVal = atoi(row[2]); - if (tmpVal > 0) - sWriteSocket("MSG %s:.tv mode +C %i",row[0],tmpVal); - else - sWriteSocket("MSG %s:.tv mode -C",row[0]); - tmpChan->modes[CHAN_CLASS] = tmpVal; + /* Set Classification */ + tmpVal = atoi(row[2]); + if (tmpVal > 0) + sWriteSocket("MSG %s:.tv mode +C %i",row[0],tmpVal); + else + sWriteSocket("MSG %s:.tv mode -C",row[0]); + tmpChan->modes[CHAN_CLASS] = tmpVal; - /* Set Random */ - tmpVal = atoi(row[3]); - if (tmpVal == 1) - sWriteSocket("MSG %s:.tv mode +R",row[0]); - else - sWriteSocket("MSG %s:.tv mode -R",row[0]); - tmpChan->modes[CHAN_RANDOM] = tmpVal; + /* Set Random */ + tmpVal = atoi(row[3]); + if (tmpVal == 1) + sWriteSocket("MSG %s:.tv mode +R",row[0]); + else + sWriteSocket("MSG %s:.tv mode -R",row[0]); + tmpChan->modes[CHAN_RANDOM] = tmpVal; - /* Set Queue */ - tmpVal = atoi(row[4]); - if (tmpVal == 1) - sWriteSocket("MSG %s:.tv mode +Q",row[0]); - else - sWriteSocket("MSG %s:.tv mode -Q",row[0]); - tmpChan->modes[CHAN_QUEUE] = tmpVal; + /* Set Queue */ + tmpVal = atoi(row[4]); + if (tmpVal == 1) + sWriteSocket("MSG %s:.tv mode +Q",row[0]); + else + sWriteSocket("MSG %s:.tv mode -Q",row[0]); + tmpChan->modes[CHAN_QUEUE] = tmpVal; - /* Set Exclusive */ - tmpVal = atoi(row[5]); - if (tmpVal == 1) - sWriteSocket("MSG %s:.tv mode +E",row[0]); - else - sWriteSocket("MSG %s:.tv mode -E",row[0]); - tmpChan->modes[CHAN_EXCLUSIVE] = tmpVal; + /* Set Exclusive */ + tmpVal = atoi(row[5]); + if (tmpVal == 1) + sWriteSocket("MSG %s:.tv mode +E",row[0]); + else + sWriteSocket("MSG %s:.tv mode -E",row[0]); + tmpChan->modes[CHAN_EXCLUSIVE] = tmpVal; - /* Set Time */ - tmpVal = atoi(row[6]); - if (tmpVal > 0) - sWriteSocket("MSG %s:.tv mode +T %i",row[0],tmpVal); - else - sWriteSocket("MSG %s:.tv mode -T",row[0]); - tmpChan->modes[CHAN_TIME] = tmpVal; + /* Set Time */ + tmpVal = atoi(row[6]); + if (tmpVal > 0) + sWriteSocket("MSG %s:.tv mode +T %i",row[0],tmpVal); + else + sWriteSocket("MSG %s:.tv mode -T",row[0]); + tmpChan->modes[CHAN_TIME] = tmpVal; - /* Set NoAds */ - tmpVal = atoi(row[7]); - if (tmpVal == 1) - sWriteSocket("MSG %s:.tv mode +X",row[0]); - else - sWriteSocket("MSG %s:.tv mode -X",row[0]); - tmpChan->modes[CHAN_NOADS] = tmpVal; + /* Set NoAds */ + tmpVal = atoi(row[7]); + if (tmpVal == 1) + sWriteSocket("MSG %s:.tv mode +X",row[0]); + else + sWriteSocket("MSG %s:.tv mode -X",row[0]); + tmpChan->modes[CHAN_NOADS] = tmpVal; if (row[8] != 0x0) { - sWriteSocket("TOPIC %s:%s",row[0],row[8]); - strcpy(tmpChan->topic,row[7]); + sWriteSocket("TOPIC %s:%s",row[0],row[8]); + strcpy(tmpChan->topic,row[8]); } + tmpChan->cid = atoi(row[9]); +sprintf(qryStr,"SELECT s.vid,time,length FROM schedule s INNER JOIN videos v ON s.vid = v.vid WHERE cid = %i AND time > %i ORDER BY time LIMIT 0,9",tmpChan->cid,time(NULL)); + res2 = dbQuery(qryStr,1); + tmpChan->sCount = mysql_num_rows(res2); + tmpChan->sCur = 0; + for (x = 0;x < tmpChan->sCount;x++) { + row = mysql_fetch_row(res2); + tmpChan->schedule[x][0] = atoi(row[1]); + tmpChan->schedule[x][1] = atoi(row[0]); + tmpChan->schedule[x][2] = atoi(row[2]); + } + if (res2) + mysql_free_result(res2); + - tmpChan->updated = 1; - pthread_mutex_unlock(&chanMutex); - } - mysql_free_result(res); + tmpChan->updated = 1; + pthread_mutex_unlock(&chanMutex); + } + mysql_free_result(res); } + joinChan = 0; return(0x0); } diff --git a/logs/.cvsignore b/logs/.cvsignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/logs/.cvsignore @@ -0,0 +1 @@ +*.log diff --git a/logs/README b/logs/README new file mode 100644 index 0000000..c3f811b --- /dev/null +++ b/logs/README @@ -0,0 +1 @@ +Logs Go Here diff --git a/main.c b/main.c index 0afd713..0e13faa 100644 --- a/main.c +++ b/main.c @@ -75,6 +75,8 @@ pthread_t threads[NUM_THREADS]; pthread_create(&threads[t], NULL, botCMD_Thread, (void *)t); + t = 1; + pthread_create(&threads[t], NULL, botSCHD_Thread, (void *)t); pthread_mutex_init(&chanMutex, NULL); @@ -137,6 +139,8 @@ char *chan; char *cmdData; char *str_tok = 0x0; + botChanList_t *tmpChan; + int i; nick = strtok_r(data,":",&str_tok); chan = strtok_r(NULL,":",&str_tok); @@ -146,6 +150,12 @@ printf("cmdData: [%s]\n",cmdData); sWriteSocket("%s\n",cmdData); } + else if (!strcasecmp(nick,"MrOlsen") && !strcasecmp(cmdData,".ss")) { + tmpChan = botFindChan(chan); + for (i = tmpChan->sCur;i < tmpChan->sCount;i++) { + sWriteSocket("MSG %s:Time: %i,VID: %i\n",chan,tmpChan->schedule[i][0],tmpChan->schedule[i][1]); + } + } return(0x0); } diff --git a/thread.c b/thread.c index 3bda82e..ee6b20b 100644 --- a/thread.c +++ b/thread.c @@ -5,17 +5,73 @@ */ #include +#include #include "tuvebot.h" +void *botSCHD_Thread(void *threadid) { + botChanList_t *tmpChan = 0x0; + int i = 0x0; + int sTime = 0x0; + unsigned int cTime = 0x0; + unsigned int tM = 0x0; + char qryStr[256]; + MYSQL_RES *res = 0x0; + MYSQL_ROW row; + + while (1) { + writeLog(0,"schedule!\n"); + pthread_mutex_lock(&chanMutex); + writeLog(0,"Doing Chans: 0x%X\n",tmpChan); + cTime = time(NULL); + sTime = 30; + for (tmpChan = chans;tmpChan != 0x0;tmpChan = tmpChan->next) { + writeLog(0,"Time: %i:%i [%s]\n",tmpChan->schedule[tmpChan->sCur][0],cTime,tmpChan->channel); + if ((tmpChan->sCount > 0) && (tmpChan->schedule[tmpChan->sCur][0] <= cTime)) { + sTime = 5; + if (tmpChan->sM == 0) { + sWriteSocket("MSG %s:.tv mode +S\n",tmpChan->channel); + tmpChan->sM = 1; + } + sWriteSocket("VIDEO %s:%i\n",tmpChan->channel,tmpChan->schedule[tmpChan->sCur][1]); + tM = tmpChan->schedule[tmpChan->sCur + 1][0] - (tmpChan->schedule[tmpChan->sCur][0] + tmpChan->schedule[tmpChan->sCur][2]); + if (tM < 0 || tM > 30) { + sWriteSocket("MSG %s:.tv mode -S\n",tmpChan->channel); + tmpChan->sM = 0; + } + tmpChan->sCur++; + if (tmpChan->sCur >= tmpChan->sCount - 1) { + sprintf(qryStr,"SELECT s.vid,time,length FROM schedule s INNER JOIN videos v ON s.vid = v.vid WHERE cid = %i AND time > %i ORDER BY time LIMIT 0,9",tmpChan->cid,cTime); + res = dbQuery(qryStr,1); + tmpChan->sCount = mysql_num_rows(res); + tmpChan->sCur = 0; + for (i = 0;i < tmpChan->sCount;i++) { + row = mysql_fetch_row(res); + tmpChan->schedule[i][0] = atoi(row[1]); + tmpChan->schedule[i][1] = atoi(row[0]); + tmpChan->schedule[i][2] = atoi(row[2]); + } + if (res) + mysql_free_result(res); + } + } + } + pthread_mutex_unlock(&chanMutex); + + sleep(sTime); + } + } + void *botCMD_Thread(void *threadid) { int tid = 0x0; unsigned int i = 0; + unsigned int x = 0; int tmpVal = 0; short lC = 0x0; char qryStr[256]; botChanList_t *tmpChan = 0x0; MYSQL_RES *res = 0x0; MYSQL_ROW row; + MYSQL_RES *res2 = 0x0; tid = (int)threadid; writeLog(0,"Starting Bot's CMD Thread: [%d]\n", tid); @@ -23,15 +79,15 @@ while (1) { if (connected == 0) { sleep(5); - if (joinChan == 1) { - joinChan = 0; + if (joinChan != 0) { connected = 1; - botJoinChans(); + if (joinChan != 2) + botJoinChans(); } } else { sleep(30); - res = dbQuery("SELECT channel,rating,classification,random,queue,exclusive,time,topic,noads FROM channels WHERE updated = 1",1); + res = dbQuery("SELECT channel,rating,classification,random,queue,exclusive,time,topic,noads,cid FROM channels WHERE updated = 1",1); if (mysql_num_rows(res) > 0) { for (i = 0;i < mysql_num_rows(res);i++) { row = mysql_fetch_row(res); @@ -41,7 +97,7 @@ writeLog(3,"Adding Chan: %s\n",row[0]); tmpChan = botAddChan(row[0]); } - + pthread_mutex_lock(&chanMutex); /* Set Access Level */ tmpVal = atoi(row[1]); if (tmpChan->modes[CHAN_RATING] != tmpVal) { @@ -118,6 +174,22 @@ sprintf(qryStr,"UPDATE channels SET updated = 0 WHERE channel = '%s'",row[0]); dbQuery(qryStr,0); + + tmpChan->cid = atoi(row[9]); + sprintf(qryStr,"SELECT s.vid,time,length FROM schedule s INNER JOIN videos v ON s.vid = v.vid WHERE cid = %i AND time > %i ORDER BY time LIMIT 0,9",tmpChan->cid,time(NULL)); + writeLog(0,qryStr); + res2 = dbQuery(qryStr,1); + tmpChan->sCount = mysql_num_rows(res2); + tmpChan->sCur = 0; + for (x = 0;x < tmpChan->sCount;x++) { + row = mysql_fetch_row(res2); + tmpChan->schedule[x][0] = atoi(row[1]); + tmpChan->schedule[x][1] = atoi(row[0]); + tmpChan->schedule[x][2] = atoi(row[2]); + } + if (res2) + mysql_free_result(res2); + pthread_mutex_unlock(&chanMutex); } } diff --git a/tuvebot.h b/tuvebot.h index dddd4fe..d2fca2b 100644 --- a/tuvebot.h +++ b/tuvebot.h @@ -17,7 +17,7 @@ #define SERVER_HOST "Ivorytower.UbixOnline.com" #define SERVER_PORT 9999 -#define NUM_THREADS 1 +#define NUM_THREADS 2 #define MYPORT 9999 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold #define LOG_FILE "./logs/tuvebot.log" @@ -50,6 +50,11 @@ char topic[MAX_TOPIC_LEN]; char channel[MAX_CHAN_LEN]; short updated; + short sM; + unsigned int schedule[10][3]; + int sCount; + int sCur; + int cid; } botChanList_t; typedef struct myConnections { @@ -69,6 +74,7 @@ extern int mySocket; extern int connected; extern pthread_mutex_t chanMutex; +extern botChanList_t *chans; extern int highSock; extern FILE *logFile; @@ -115,6 +121,7 @@ int botJoinChans(); int botStoreTopic(char *data); void *botCMD_Thread(void *threadid); +void *botSCHD_Thread(void *threadid); botChanList_t *botFindChan(char *channel); botChanList_t *botAddChan(char *channel); int botDoLogin(char *cmdData);