added mysql db and loading bots from bots table
authorpk910 <philipp@zoelle1.de>
Sun, 14 Aug 2011 17:20:25 +0000 (19:20 +0200)
committerpk910 <philipp@zoelle1.de>
Sun, 14 Aug 2011 17:20:25 +0000 (19:20 +0200)
ClientSocket.c
ClientSocket.h
IRCEvents.c
bot_NeonServ.c
mysqlConn.c
mysqlConn.h

index 25d697f8e80a6b1b7ad52ab4840076121c3aaa96..78467a1c94b5d1d725d216ed4f57e261d6a4706e 100644 (file)
@@ -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;
index 5b145fb4336a340bd67aa6fb85d942ab4adfc869..494505b15451d9530f0d75184232e029c309123e 100644 (file)
@@ -23,7 +23,8 @@ struct ClientSocket {
     char *pass;
     struct UserNode *user;
        
-       int botid;
+       int botid : 16;
+    int clientid : 16;
     
     struct ClientSocket *next;
 };
index 1bbbd4f9d790d470a2a7ab7d1ff2aaad4534d7fd..d85dff9b9027c49736d02c66d6fe1d2ec3a41dae 100644 (file)
@@ -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)
index a2158be4c9f264e10311bc7b8a9bd998cd6b0b63..ebed8547fdd063eb78e5d151452dd372d71d42bc 100644 (file)
@@ -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
index 2dbd608bef84fb281bac9f80e17e2e3ef6b411ba..84933b0bd65a271e066738c4e3a2e7d62e6f6a8c 100644 (file)
@@ -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;
+}
index 33438b3d66ca553154e7dde6313c9b9cb0fe8c21..f456471b6444ccab62186a3da059455286bec282 100644 (file)
@@ -4,11 +4,17 @@
 #include "main.h"
 #include <mysql.h>
 
-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