client->flags = 0;
client->bufferpos = 0;
client->botid = 0;
+ client->clientid = 0;
client->next = sockets->data;
sockets->data = client;
return client;
char *pass;
struct UserNode *user;
- int botid;
+ int botid : 16;
+ int clientid : 16;
struct ClientSocket *next;
};
#include "ChanNode.h"
#include "ChanUser.h"
#include "ClientSocket.h"
+#include "mysqlConn.h"
struct binding {
void *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)
#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;
}
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) {
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);
}
#undef BOTID
+#undef CLASSNAME
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)) {
}
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() {
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;
+}
#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