diff --git a/Makefile b/Makefile index 2806cd1..9d30eb8 100755 --- a/Makefile +++ b/Makefile @@ -22,6 +22,8 @@ main: $(CC) $(CFLAGS) $(SRC)/module.c -o $(OBJ)/module.o $(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o $(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o + $(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o + $(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS) @echo "All Done!" diff --git a/lib/db.h b/lib/db.h index 3f7d82e..56fb2bd 100755 --- a/lib/db.h +++ b/lib/db.h @@ -1,10 +1,15 @@ #ifndef DB_H #define DB_H +#define DB_MAGIC 0xdeadbeef +#define DB_VER 0x10 + struct db_hash { - char key[85]; - char value[4096]; + int key_len; + int value_len; + char *key; + char *value; }; struct db_table @@ -18,8 +23,8 @@ struct db_table int write_db(struct db_table *db, char *fname); struct db_table *read_db(char *fname); -int add_hash(struct db_table *db, char *key, char *value); -int del_hash(struct db_table *db, char *key); +int db_add_hash(struct db_table *db, char *key, char *value); +int db_del_hash(struct db_table *db, char *key); char *get_hash(struct db_table *db, char *key); #endif diff --git a/lib/irc.h b/lib/irc.h index f4c44cb..6867419 100755 --- a/lib/irc.h +++ b/lib/irc.h @@ -9,6 +9,8 @@ #include +#include "db.h" + #ifdef _WIN32 #include #endif @@ -30,6 +32,9 @@ struct irc_conn char port[5]; char real_name[512]; + char db_file[256]; + struct db_table *db; + // I/O Buffers char *out; char *in; diff --git a/src/config.c b/src/config.c index bcd6fbf..22698cc 100755 --- a/src/config.c +++ b/src/config.c @@ -41,6 +41,9 @@ struct irc_conn read_config(struct irc_conn bot, char *file) if (config_lookup_string(cf, "bot.admin", &base)) strlcpy(bot.admin, base, sizeof bot.admin); + if (config_lookup_string(cf, "bot.db", &base)) + strlcpy(bot.db_file, base, sizeof bot.db_file); + autoload = config_lookup(cf, "mods.autoload"); count = config_setting_length(autoload); diff --git a/src/db.c b/src/db.c index cf40b71..43048d2 100755 --- a/src/db.c +++ b/src/db.c @@ -16,10 +16,21 @@ int write_db(struct db_table *db, char *fname) return -1; } - db->db_magic = 0xdeadbeef; - db->db_ver = 0x10; + db->db_magic = DB_MAGIC; + db->db_ver = DB_VER; - fwrite(db, sizeof(struct db_table) + (sizeof(struct db_hash) * db->count), 1, fp); + // write the header + fwrite(db, sizeof(struct db_table), 1, fp); + + // write the hashes + fwrite(db->hashes, sizeof(struct db_hash), db->count, fp); + + // write the keys and values + for (i = 0; i < db->count; i++) + { + fwrite(db->hashes[i].key, sizeof(char), db->hashes[i].key_len, fp); + fwrite(db->hashes[i].value, sizeof(char), db->hashes[i].value_len, fp); + } fclose(fp); @@ -30,6 +41,7 @@ struct db_table *read_db(char *fname) { FILE *fp; struct db_table *db; + int tmp; if ((fp = fopen(fname, "rb")) == NULL) { @@ -41,35 +53,43 @@ struct db_table *read_db(char *fname) fread(db, sizeof(struct db_table), 1, fp); // check the magic value - if (db->db_magic != 0xdeadbeef) + if (db->db_magic != DB_MAGIC) { - printf("Error: %s incompatible or unknown db file format\n", fname); + printf("Error: %s incompatible or unknown db file format: Bad Magic\n", fname); return NULL; } // check the version - if (db->db_ver != 0x10) + if (db->db_ver != DB_VER) { - printf("Error: %s incompatible or unknown db file format\n", fname); + printf("Error: %s incompatible or unknown db file format: Incompatible Version\n", fname); return NULL; } - db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * db->count); - - // skip padding - fseek(fp, 8, SEEK_CUR); + tmp = db->count != 0 ? db->count : sizeof(struct db_hash); + db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * tmp); fread(db->hashes, sizeof(struct db_hash), db->count, fp); + // read the keys and values + for (tmp = 0; tmp < db->count; tmp++) + { + db->hashes[tmp].key = (char *)malloc(sizeof(char) * db->hashes[tmp].key_len); + db->hashes[tmp].value = (char *)malloc(sizeof(char) * db->hashes[tmp].value_len); + + fread(db->hashes[tmp].key, sizeof(char), db->hashes[tmp].key_len, fp); + fread(db->hashes[tmp].value, sizeof(char), db->hashes[tmp].value_len, fp); + } + fclose(fp); return db; } -int add_hash(struct db_table *db, char *key, char *value) +int db_add_hash(struct db_table *db, char *key, char *value) { int i; @@ -83,17 +103,28 @@ int add_hash(struct db_table *db, char *key, char *value) db->hashes = (struct db_hash *)realloc(db->hashes, sizeof(struct db_hash) * (db->count + 1)); - memset(db->hashes[db->count].key, 0, sizeof(db->hashes[db->count].key)); - memset(db->hashes[db->count].value, 0, sizeof(db->hashes[db->count].value)); + // zero out reallocated memory + memset(&db->hashes[db->count], 0, sizeof(struct db_hash)); - strlcpy(db->hashes[db->count].key, key, sizeof(db->hashes[db->count].key)); - strlcpy(db->hashes[db->count].value, value, sizeof(db->hashes[db->count].value)); + db->hashes[db->count].key_len = strlen(key) + 1; + db->hashes[db->count].value_len = strlen(value) + 1; + + printf("key_len: %d, value_len: %d\n", sizeof(char) * db->hashes[db->count].key_len, sizeof(char) * db->hashes[db->count].value_len); + + db->hashes[db->count].key = (char *)malloc(sizeof(char) * db->hashes[db->count].key_len); + db->hashes[db->count].value = (char *)malloc(sizeof(char) * db->hashes[db->count].value_len); + + memset(db->hashes[db->count].key, 0, sizeof(char) * db->hashes[db->count].key_len); + memset(db->hashes[db->count].value, 0, sizeof(char) * db->hashes[db->count].value_len); + + strlcpy(db->hashes[db->count].key, key, sizeof(char) * db->hashes[db->count].key_len); + strlcpy(db->hashes[db->count].value, value, sizeof(char) * db->hashes[db->count].value_len); db->count++; return 0; } -int del_hash(struct db_table *db, char *key) +int db_del_hash(struct db_table *db, char *key) { int i; diff --git a/src/main.c b/src/main.c index 041a4ab..ba166b2 100755 --- a/src/main.c +++ b/src/main.c @@ -9,8 +9,11 @@ #include #include #include +#include + #include "config.h" #include "irc.h" +#include "db.h" #include "util.h" #include "events.h" #include "module.h" @@ -50,6 +53,23 @@ int main() // Read the config bot = read_config(bot, "xbot.cfg"); + // check if the db exists, if not, create it + if (access(bot.db_file, F_OK) == -1) + { + printf("Creating db\n"); + bot.db = (struct db_table *)malloc(sizeof(struct db_table)); + memset(bot.db, 0, sizeof(struct db_table)); + + bot.db->count = 0; + bot.db->hashes = NULL; + + write_db(bot.db, bot.db_file); + } + else + { + bot.db = read_db(bot.db_file); + } + // Connect to the server printf("Connecting to %s...\n", bot.host); diff --git a/test.db b/test.db index 1fd816b..aa5dc16 100755 Binary files a/test.db and b/test.db differ diff --git a/test1.db b/test1.db new file mode 100755 index 0000000..b4cdfc9 Binary files /dev/null and b/test1.db differ diff --git a/test_db.c b/test_db.c index a6289a6..4fbde56 100755 --- a/test_db.c +++ b/test_db.c @@ -2,20 +2,22 @@ #include #include #include +#include -#define FNAME "test.db" +#define FNAME "xbot.db" int main() { struct db_table *db; int i; + /* db = (struct db_table *)malloc(sizeof(struct db_table)); db->count = 0; db->hashes = NULL; - add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua"); - add_hash(db, "lua.scriptcount", "2"); + db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua"); + db_add_hash(db, "lua.scriptcount", "2"); if (write_db(db, FNAME) == -1) { @@ -23,16 +25,36 @@ int main() } free(db); + */ - db = read_db(FNAME); + if (access(FNAME, F_OK) == -1) + { + printf("Creating db\n"); + + db = (struct db_table *)malloc(sizeof(struct db_table)); + db->count = 0; + db->hashes = NULL; + } + else + { + db = read_db(FNAME); + } + + // write some data if db is empty + if (db->count == 0) + { + db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua"); + db_add_hash(db, "lua.scriptcount", "2"); + write_db(db, FNAME); + + return 0; + } for (i = 0; i < db->count; i++) { printf("Key: %s, Value: %s\n", db->hashes[i].key, db->hashes[i].value); } - printf("test: lua.scripts: %s\n", get_hash(db, "lua.scripts")); - free(db); return 0; diff --git a/testdb b/testdb index c6f3e38..6aa9388 100755 Binary files a/testdb and b/testdb differ diff --git a/xbot.cfg b/xbot.cfg index 3c64ecf..9c1a975 100755 --- a/xbot.cfg +++ b/xbot.cfg @@ -6,6 +6,8 @@ bot: nick = "xbot"; user = "xbot"; admin = "ab3800"; + + db = "xbot.db"; }; server: diff --git a/xbot.db b/xbot.db new file mode 100755 index 0000000..c826d12 Binary files /dev/null and b/xbot.db differ