diff --git a/bridge.c b/bridge.c new file mode 100644 index 0000000..8e52341 --- /dev/null +++ b/bridge.c @@ -0,0 +1,378 @@ +/* + bot.c (C) 2007 Christopher Olsen + + $Id$ +*/ + +#include +#include "tuvebot.h" + +botChanList_t *chans = 0x0; + +pthread_mutex_t chanMutex = PTHREAD_MUTEX_INITIALIZER; + +botChanList_t *botFindChan(char *channel) { + botChanList_t *tmpChan = 0x0; + + pthread_mutex_lock(&chanMutex); + + for (tmpChan = chans;tmpChan != 0x0;tmpChan = tmpChan->next) { + if (!strcasecmp(tmpChan->channel,channel)) { + pthread_mutex_unlock(&chanMutex); + return(tmpChan); + } + } + + pthread_mutex_unlock(&chanMutex); + return(0x0); + } + +botChanList_t *botAddChan(char *channel) { + botChanList_t *tmpChan = 0x0; + + tmpChan = botFindChan(channel); + + if (tmpChan != 0x0) + return(tmpChan); + + writeLog(2,"ADDING CHANNEL: %s\n",channel); + + tmpChan = (botChanList_t *)malloc(sizeof(botChanList_t)); + + strcpy(tmpChan->channel,channel); + tmpChan->next = 0x0; + tmpChan->prev = 0x0; + tmpChan->updated = 0x0; + bzero(tmpChan->modes,CHAN_MODES); + + pthread_mutex_lock(&chanMutex); + + if (chans == 0x0) + chans = tmpChan; + else { + tmpChan->next = chans; + chans->prev = tmpChan; + chans = tmpChan; + } + + pthread_mutex_unlock(&chanMutex); + + return(tmpChan); + } + +int botJoinChans() { + 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,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); + + sWriteSocket("JOIN %s",row[0]); + sleep(2); + + pthread_mutex_lock(&chanMutex); + + /* 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 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 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 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; + + if (row[8] != 0x0) { + 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); + } + joinChan = 0; + return(0x0); + } + +int botSetMode(char *data) { + botChanList_t *tmpChan = 0x0; + char *chan = 0x0; + char *mode = 0x0; + char *md_tok = 0x0; + int tmpVal = 0x0; + char qryStr[512]; + + chan = strtok_r(data,":",&md_tok); + + tmpChan = botFindChan(chan); + + if (tmpChan == 0x0) + return(-1); + + if (tmpChan->updated == 0x0) + return(0x0); + + mode = strtok_r(NULL,":",&md_tok); + + switch (mode[0]) { + case 'A': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_RATING] != tmpVal) { + tmpChan->modes[CHAN_RATING] = tmpVal; + sprintf(qryStr,"UPDATE channels SET rating = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'C': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_CLASS] != tmpVal) { + tmpChan->modes[CHAN_CLASS] = tmpVal; + sprintf(qryStr,"UPDATE channels SET classification = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'R': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_RANDOM] != tmpVal) { + tmpChan->modes[CHAN_RANDOM] = tmpVal; + sprintf(qryStr,"UPDATE channels SET random = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'Q': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_QUEUE] != tmpVal) { + tmpChan->modes[CHAN_QUEUE] = tmpVal; + sprintf(qryStr,"UPDATE channels SET queue = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'X': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_NOADS] != tmpVal) { + tmpChan->modes[CHAN_NOADS] = tmpVal; + sprintf(qryStr,"UPDATE channels SET noads = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'E': + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpVal != 0x0) + tmpVal = 1; + if (tmpChan->modes[CHAN_EXCLUSIVE] != tmpVal) { + tmpChan->modes[CHAN_EXCLUSIVE] = tmpVal; + sprintf(qryStr,"UPDATE channels SET exclusive = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + case 'T': + tmpVal = atoi(strtok_r(NULL,":",&md_tok)); + if (tmpVal != 0x0) + tmpVal = atoi(strtok_r(NULL,"\n",&md_tok)); + if (tmpChan->modes[CHAN_TIME] != tmpVal) { + tmpChan->modes[CHAN_TIME] = tmpVal; + sprintf(qryStr,"UPDATE channels SET time = %i WHERE channel = '%s'",tmpVal,tmpChan->channel); + dbQuery(qryStr,0); + } + break; + default: + writeLog(0,"Invalid Mode: %c\n",mode[0]); + } + return(0x0); + } + +int botSetCurSong(char *data) { + char output[128]; + char *chan = 0x0; + char *song = 0x0; + + if (data == 0x0) + return(-1); + + chan = strtok(data,":"); + strtok(NULL,":"); + strtok(NULL,":"); + strtok(NULL,":"); + strtok(NULL,":"); + song = strtok(NULL,":"); + + if (song == 0x0) + return(-1); + + sprintf(output,"UPDATE channels SET nowplaying = %s WHERE channel = '%s'",song,chan); + dbQuery(output,0); + + writeLog(0,"Test: %s %s\n",chan,song); + return(0x0); + } + +int botStoreTopic(char *data) { + char *chan = 0x0; + char *topic = 0x0; + char qStr[512]; + + if (data == 0x0) + return(0x1); + + chan = strtok(data,":"); + topic = strtok(NULL,"\n"); + + if (chan == 0x0) + return(0x1); + + sprintf(qStr,"UPDATE channels SET topic = \"%s\" WHERE channel = '%s'",topic,chan); + dbQuery(qStr,0); + + return(0x0); + } + +int botDoLogin(char *cmdData) { + char *nick = 0x0; + char *ident = 0x0; + char *str_tok = 0x0; + char qryStr[256]; + MYSQL_RES *res = 0x0; + MYSQL_ROW row; + unsigned int i = 0x0; + + nick = strtok_r(cmdData,":",&str_tok); + ident = strtok_r(NULL,"\n",&str_tok); + + sprintf(qryStr,"SELECT channel FROM active a INNER JOIN channels c ON a.uid = c.oid WHERE a.userid = %s",ident); + res = dbQuery(qryStr,1); + + for (i = 0x0;i < mysql_num_rows(res);i++) { + row = mysql_fetch_row(res); + sWriteSocket("MSG %s:.tv mode +O %s",row[0],nick); + } + + if (res != 0x0) + mysql_free_result(res); + + return(0x0); + } + +int botDoJoin(char *cmdData) { + char *chan = 0x0; + char *nick = 0x0; + char *ident = 0x0; + char *str_tok = 0x0; + char qryStr[256]; + MYSQL_RES *res = 0x0; + + if (cmdData == 0x0) + return(-1); + + chan = strtok_r(cmdData,":",&str_tok); + nick = strtok_r(NULL,":",&str_tok); + ident = strtok_r(NULL,":",&str_tok); + + while (ident != 0x0) { + sprintf(qryStr,"SELECT cid FROM active a INNER JOIN channels c ON a.uid = c.oid WHERE userid = %s AND channel like '%s'",ident,chan); + res = dbQuery(qryStr,1); + if (res != 0x0) { + if (mysql_num_rows(res) > 0) + sWriteSocket("MSG %s:.tv mode +O %s",chan,nick); + mysql_free_result(res); + } + nick = strtok_r(NULL,":",&str_tok); + ident = strtok_r(NULL,":",&str_tok); + } + return(0x0); + } + +int botDoEmote(char cmdData) { +/* + char *nick = 0x0; + char *where = 0x0; + char *msg = 0x0; + + if (cmdData != 0x0) { + nick = strtok(cmdData,":"); + where = strtok(NULL,":"); + msg = strtok(NULL,"\n"); + } +*/ + return(0x0); + }