Newer
Older
tuved / botthread.c
@reddawg reddawg on 28 Oct 2007 2 KB Sync
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#include "tuved.h"

char nextSong[256];

int botNextSong();

int tuveBotCMD(char *chan,char *data) {
  char output[512];
  char *cmd     = 0x0;
  char *cmdData = 0x0;
  tuveChanList_t *tmpChan = 0x0;

  if (data == 0x0)
    return(0x1);

  cmd = strtok(data," ");
  cmdData = strtok(NULL,"\n");

  if (strcasecmp(cmd,"echo") == 0x0) {
    sprintf(output,"CHANMSG:TUveBOT:%s:%s\n",chan,cmdData);
    tuveSendAllInChan(chan,0x0,output);
    }
  else if (strcasecmp(cmd,"skip") == 0x0) {
    tmpChan = findChan(cmdData);
    if (tmpChan != 0x0)
      tmpChan->songEnd = 0x0;
    }

  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() {
  tuveChanList_t *tmpChans;

  pthread_mutex_lock(&chanMutex);
  
  for (tmpChans = channels;tmpChans != 0x0;tmpChans = tmpChans->next) {
    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);
      }
    }

  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;

  sprintf(output,"CURPOS:0:%s:%s\n",file,title);
  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,"CHANMSG:@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()