From 18cbd52c4f57562d1c1da1dad8b174ddb73fa9fc Mon Sep 17 00:00:00 2001 From: pk910 Date: Sun, 14 Aug 2011 19:20:25 +0200 Subject: [PATCH] added mysql db and loading bots from bots table --- ClientSocket.c | 1 + ClientSocket.h | 3 ++- IRCEvents.c | 11 ++++++++++ bot_NeonServ.c | 50 ++++++++++++++++++++++++++++---------------- mysqlConn.c | 56 ++++++++++++++++++++++++++++++++++++++++++-------- mysqlConn.h | 8 +++++++- 6 files changed, 101 insertions(+), 28 deletions(-) diff --git a/ClientSocket.c b/ClientSocket.c index 25d697f..78467a1 100644 --- a/ClientSocket.c +++ b/ClientSocket.c @@ -38,6 +38,7 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User client->flags = 0; client->bufferpos = 0; client->botid = 0; + client->clientid = 0; client->next = sockets->data; sockets->data = client; return client; diff --git a/ClientSocket.h b/ClientSocket.h index 5b145fb..494505b 100644 --- a/ClientSocket.h +++ b/ClientSocket.h @@ -23,7 +23,8 @@ struct ClientSocket { char *pass; struct UserNode *user; - int botid; + int botid : 16; + int clientid : 16; struct ClientSocket *next; }; diff --git a/IRCEvents.c b/IRCEvents.c index 1bbbd4f..d85dff9 100644 --- a/IRCEvents.c +++ b/IRCEvents.c @@ -4,6 +4,7 @@ #include "ChanNode.h" #include "ChanUser.h" #include "ClientSocket.h" +#include "mysqlConn.h" struct binding { void *func; @@ -90,13 +91,23 @@ void unbind_##NAME(FUNCTYPE *func) { \ #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \ int event_##NAME PDECLARATION { \ struct binding *cbind; \ + pre_event(TYPE); \ for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \ FUNCTYPE *func = cbind->func; \ func PLIST; \ } \ + post_event(TYPE); \ return 1; \ } +void pre_event(UNUSED_ARG(int type)) { + +} + +void post_event(UNUSED_ARG(int type)) { + mysql_free(); +} + //EVENTS FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN) diff --git a/bot_NeonServ.c b/bot_NeonServ.c index a2158be..ebed854 100644 --- a/bot_NeonServ.c +++ b/bot_NeonServ.c @@ -6,8 +6,10 @@ #include "ChanNode.h" #include "ChanUser.h" #include "ClientSocket.h" +#include "mysqlConn.h" #define BOTID 1 +#define CLASSNAME "NeonServ" static CMD_BIND(neonserv_cmd_users) { struct ChanUser *chanuser; @@ -24,7 +26,16 @@ static CMD_BIND(neonserv_cmd_modes) { } static void neonserv_bot_ready(struct ClientSocket *client) { - putsock(client, "JOIN #pktest"); + MYSQL_RES *res; + MYSQL_ROW row; + + check_mysql(); + printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d'", client->clientid); + res = mysql_use(); + + while ((row = mysql_fetch_row(res)) != NULL) { + putsock(client, "JOIN %s %s", row[0], row[1]); + } } static void neonserv_trigger_callback(struct ChanNode *chan, char *trigger) { @@ -34,30 +45,32 @@ static void neonserv_trigger_callback(struct ChanNode *chan, char *trigger) { static void start_bots() { struct UserNode *user; struct ClientSocket *client; + MYSQL_RES *res; + MYSQL_ROW row; - user = addUser("TestBot"); - strcpy(user->ident, "test"); - strcpy(user->realname, "testUser!"); - user->flags |= USERFLAG_ISBOT; - client = create_socket("127.0.0.1", 6667, "pktest:pktest123", user); //pktest Hostmask(s): *@127.0.0.1 - client->flags |= SOCKET_FLAG_PREFERRED; - client->botid = BOTID; - connect_socket(client); + printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `whoisbot`, `id` FROM `bots` WHERE `botclass` = '%s' AND `active` = '1'", escape_string(CLASSNAME)); + res = mysql_use(); - user = addUser("TestBot2"); - strcpy(user->ident, "test"); - strcpy(user->realname, "testUser!"); - user->flags |= USERFLAG_ISBOT; - client = create_socket("127.0.0.1", 6667, "pktest:pktest123", user); //pktest Hostmask(s): *@127.0.0.1 - client->botid = BOTID; - connect_socket(client); + while ((row = mysql_fetch_row(res)) != NULL) { + + user = addUser(row[0]); + strcpy(user->ident, row[1]); + strcpy(user->realname, row[2]); + user->flags |= USERFLAG_ISBOT; + client = create_socket(row[3], row[4], row[5], user); + client->flags |= (row[6] == 1 ? SOCKET_FLAG_PREFERRED : 0); + client->botid = BOTID; + client->clientid = row[7]; + connect_socket(client); + + } } void init_NeonServ() { - start_bots(); + check_mysql(); + start_bots(); bind_bot_ready(neonserv_bot_ready); - set_trigger_callback(BOTID, neonserv_trigger_callback); register_command(BOTID, "users", neonserv_cmd_users); @@ -72,3 +85,4 @@ void free_NeonServ() { } #undef BOTID +#undef CLASSNAME diff --git a/mysqlConn.c b/mysqlConn.c index 2dbd608..84933b0 100644 --- a/mysqlConn.c +++ b/mysqlConn.c @@ -6,12 +6,14 @@ struct used_result { struct used_result *next; }; -static MYSQL *mysql_conn = NULL; -static struct used_result *used_results; +struct escaped_string { + char *string; + struct escaped_string *next; +}; -MYSQL *getMySQL() { - return mysql_conn; -} +MYSQL *mysql_conn = NULL; +static struct used_result *used_results; +static struct escaped_string *escaped_strings; void check_mysql() { if(mysql_ping(mysql_conn)) { @@ -33,12 +35,18 @@ MYSQL_RES *mysql_use() { } void mysql_free() { - struct used_result *result, *next; - for(result = used_results; result; result = next) { - next = result->next; + struct used_result *result, *next_result; + for(result = used_results; result; result = next_result) { + next_result = result->next; mysql_free_result(result->result); free(result); } + struct escaped_string *escaped, *next_escaped; + for(escaped = escaped_string; escaped; escaped = next_escaped) { + next_escaped = escaped->next; + free(escaped->string); + free(escaped); + } } void init_mysql() { @@ -52,3 +60,35 @@ void free_mysql() { mysql_close(mysql_conn); } +void show_mysql_error() { + //show mysql_error() + +} + +void printf_mysql_query(const char *text, ...) { + va_list arg_list; + char queryBuf[MYSQLMAXLEN]; + int pos; + queryBuf[0] = '\0'; + va_start(arg_list, text); + pos = vsnprintf(queryBuf, MYSQLMAXLEN - 2, text, arg_list); + va_end(arg_list); + if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2; + queryBuf[pos] = '\0'; + if(mysql_query(mysql_conn, queryBuf)) { + show_mysql_error(); + } +} + +char* escape_string(const char *str) { + struct escaped_string *escapedstr = malloc(sizeof(*escapedstr)); + if (!escapedstr) { + return NULL; + } + char escaped[strlen(str)*2+1]; + mysql_real_escape_string(mysql_conn, escaped, str, strlen(str)); + escapedstr->string = strdup(escaped); + escapedstr->next = escaped_strings; + escaped_strings = escapedstr; + return escapedstr->string; +} diff --git a/mysqlConn.h b/mysqlConn.h index 33438b3..f456471 100644 --- a/mysqlConn.h +++ b/mysqlConn.h @@ -4,11 +4,17 @@ #include "main.h" #include -MYSQL *getMySQL(); +#define MYSQLMAXLEN 1024 + +extern MYSQL *mysql_conn; + void check_mysql(); MYSQL_RES *mysql_use(); void mysql_free(); void init_mysql(); void free_mysql(); +void show_mysql_error(); +void printf_mysql_query(const char *text, ...) PRINTF_LIKE(1, 2); +char* escape_string(const char *str); #endif \ No newline at end of file -- 2.20.1