Newer
Older
tuvebot / src / thread.c
@Charlie Root Charlie Root on 15 Apr 2019 6 KB Working on new build system
/*
 (C) 2007,2008 Christopher Olsen

 $Id: thread.c,v 1.22 2008/05/28 12:03:41 reddawg Exp $
*/
 
#include <stdio.h>
#include <time.h>
#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);
  
  while (1) {
    if (connected == 0) {
      sleep(5);
      if (joinChan != 0) {
        connected = 1;
        if (joinChan != 2)
          botJoinChans();
        }
      }
    else {
      sleep(30);
      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);
    	  
    	  tmpChan = botFindChan(row[0]);
    	  if (tmpChan == 0x0) {
    	    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) {
    	    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 (tmpChan->modes[CHAN_CLASS] != tmpVal) {
    	    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 (tmpChan->modes[CHAN_RANDOM] != tmpVal) {
    	    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 (tmpChan->modes[CHAN_QUEUE] != tmpVal) {
    	    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 (tmpChan->modes[CHAN_EXCLUSIVE] != tmpVal) {
    	    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 (tmpChan->modes[CHAN_TIME] != tmpVal) {
    	    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;
    	    }
    	  
    	  tmpVal = atoi(row[8]);
    	  if (tmpChan->modes[CHAN_NOADS] != tmpVal) {
    	    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 (strcmp(tmpChan->topic,row[7])) {
    	    sWriteSocket("TOPIC %s:%s",row[0],row[7]);
    	    strcpy(tmpChan->topic,row[7]);
    	    }
    	  
    	  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);
    	  }	
        }

      if (res != 0x0)
        mysql_free_result(res);
        
      if (lC == 6) {
        lC = 0;
        sWriteSocket("STATUS");
        }
      lC++;
	  }
    }
  pthread_exit(NULL);
  } /* End tuveCMD_Thread() */