/*
 bot.c (C) 2007 Christopher Olsen
 $Id$
*/
#include <stdio.h>
#include "tuvebot.h"
int botJoinChans() {
  MYSQL_RES *res          = 0x0;
  MYSQL_ROW  row;
  unsigned int  i = 0x0;
  int      tmpVal = 0;
  res = dbQuery("SELECT channel,rating,classification,random,queue,exclusive,time,topic FROM channels",1);
  
  if (mysql_num_rows(res) > 0) {
	for (i = 0;i < mysql_num_rows(res);i++) {
	  row = mysql_fetch_row(res);
	  sWriteSocket("JOIN %s",row[0]);
	  sleep(2);
	  /* 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]);
	  
	  /* 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]);
	  /* Set Random */
	  if (row[3][0] == '1')
	    sWriteSocket("MSG %s:.tv mode +R",row[0]);
	  else
		sWriteSocket("MSG %s:.tv mode -R",row[0]);
	  /* Set Queue */
	  if (row[4][0] == '1')
	    sWriteSocket("MSG %s:.tv mode +Q",row[0]);
	  else
		sWriteSocket("MSG %s:.tv mode -Q",row[0]);
	  /* Set Exclusive */
	  if (row[5][0] == '1')
	    sWriteSocket("MSG %s:.tv mode +E",row[0]);
	  else
		sWriteSocket("MSG %s:.tv mode -E",row[0]);
	  /* 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]);
	  
	  sWriteSocket("TOPIC %s:%s",row[0],row[7]);
	  }
    }
  return(0x0);
  }
int botSetCurSong(char *data) {
  char output[128];
  char *chan = 0x0;
  char *song = 0x0;
  writeLog(0,"TESTING");
  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); 
  }