diff --git a/bot.c b/bot.c new file mode 100644 index 0000000..e552a57 --- /dev/null +++ b/bot.c @@ -0,0 +1,71 @@ +#include +#include "tuvebot.h" + +int botJoinChans() { + MYSQL_RES *res = 0x0; + MYSQL_ROW row; + unsigned int i = 0x0; + + res = dbQuery("SELECT channel,topic,modes FROM channels",1); + + for (i = 0;i < mysql_num_rows(res);i++) { + row = mysql_fetch_row(res); + sWriteSocket("JOIN %s\n",row[0]); + if (row[1] != 0x0) { + sleep(1); + sWriteSocket("TOPIC %s:%s\n",row[0],row[1]); + } + if (row[2] != 0x0) { + sleep(1); + sWriteSocket("MSG %s:.tv mode %s\n",row[0],row[2]); + } + } + 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); + } diff --git a/mysql.c b/mysql.c new file mode 100644 index 0000000..5b5052e --- /dev/null +++ b/mysql.c @@ -0,0 +1,58 @@ +#include + +#include "tuvebot.h" + +MYSQL *conn = 0x0; + +//pthread_mutex_t mysqlMutex = PTHREAD_MUTEX_INITIALIZER; + + +int dbInit() { + + conn = mysql_init(NULL); + if (conn == NULL) { + writeLog(0,"mysql_init() failed (probably out of memory)\n."); + return(0x1); + } + + if (!mysql_real_connect(conn,MYSQL_HOST_NAME,MYSQL_USERNAME,MYSQL_PASSWORD,MYSQL_DB_NAME,0,NULL,0)) { + writeLog(0,"mysql_real_connect() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn)); + return(0x1); + } + + if (mysql_options(conn,MYSQL_OPT_RECONNECT,"true") != 0x0) { + writeLog(0,"mysql_options() failed: To Set Reconnect\n"); + } + + //pthread_mutex_init(&mysqlMutex, NULL); + + writeLog(0,"Sucessful connection to the MySQL Database.\n"); + + return(0x0); + } + +MYSQL_RES *dbQuery(const char *query,short type) { + MYSQL_RES *res_set = 0x0; + + //pthread_mutex_lock(&mysqlMutex); + + writeLog(0,"Query: %s\n",query); + + if (mysql_query(conn,query) != 0x0) { + writeLog(0,"mysql_query() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn)); + //pthread_mutex_unlock(&mysqlMutex); + return(0x0); + } + + if (type == 1) { + res_set = mysql_store_result(conn); + if (res_set == 0x0) { + writeLog(0,"mysql_store_result() failed: Error %u (%s)\n.", mysql_errno(conn), mysql_error(conn)); + //pthread_mutex_unlock(&mysqlMutex); + return(0x0); + } + } + + //pthread_mutex_unlock(&mysqlMutex); + return(res_set); + }