diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | agni.c | 31 | ||||
-rw-r--r-- | cmd/cmd_todo.c | 181 | ||||
-rw-r--r-- | cmd/cmd_todo.h | 3 | ||||
-rw-r--r-- | nbrpc_call.c | 2 | ||||
-rw-r--r-- | nbrpc_event.c | 18 | ||||
-rw-r--r-- | tbl_qcmd.c | 3 |
7 files changed, 232 insertions, 8 deletions
@@ -3,7 +3,7 @@ CC=gcc CFLAGS=-g3 -Wall SOURCES=$(wildcard *.c) OBJECTS=$(SOURCES:.c=.o) -LDFLAGS=-lrt -lm +LDFLAGS=-lrt -lm -lsqlite3 INCLUDE=-I./extlibs/ -I./ all: createcmd createextlib $(OBJECTS) $(PROJECT) @@ -134,6 +134,33 @@ tble_cmd_resp* cllbk_wrapper( void *(*call)(void *), tble_cmd_param *param) } /* +convert irc ident to 3 newly alocated string +*/ +int irciedent_parse(char *ident, rpc_request *req) +{ + int count1, count2; + sds params1 = sdsnew(ident), params2; + sds *tokens1, *tokens2; + + tokens1 = sdssplitlen(params1, sdslen(params1), "!", 1, &count1); + + req->user = alloc_new_str(tokens1[0]); + + params2 = sdsnew(tokens1[1]); + tokens2 = sdssplitlen(params2, sdslen(params2), "@", 1, &count2); + + req->mask = alloc_new_str(tokens2[0]); + req->server = alloc_new_str(tokens2[1]); + + sdsfree(params1); + sdsfree(params2); + sdsfreesplitres(tokens1, count1); + sdsfreesplitres(tokens2, count2); + + return 0; +} + +/* return unique ID, with atomic counter should work in all cases */ _Atomic int _glbl_id=0; @@ -556,9 +583,7 @@ int th_start_client(void *data) } req = rpc_req_new(qcmd->cmd, qcmd->param, qcmd->cid); - req->user = alloc_new_str("U-ser"); - req->mask = alloc_new_str("M-ask"); - req->server = alloc_new_str("S-erver"); + irciedent_parse(qcmd->ircident, req); rpc_req_marsh(req, &nb_req); buf_nb = nb_create(nb_req); diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c index 2b3993b..50218b0 100644 --- a/cmd/cmd_todo.c +++ b/cmd/cmd_todo.c @@ -1,10 +1,105 @@ #include "cmd_todo.h" +static int add_todo(sqlite3 *db, char *user, char *todo) +{ + int rc; + + char sql_add_table[1024]; + snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", user, todo); + printf("%s\n", sql_add_table); + + if ((rc = sqlite3_exec(db, sql_add_table, 0, 0, 0)) != SQLITE_OK) + { + printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db)); + return -1; + } + + return 0; +} + +static int cb_list_todo_table(void *param1, int argc, char **argv, char **cname) +{ + int i; + + sds *out = (sds *)param1; + //sds local_out = sdsempty(); + + for (i=0; i<argc; i++) + { + printf("%s = %s, ", cname[i], argv[i] ? argv[i] : "NULL"); + if (strncmp(cname[i],"id",2) == 0) + { + *out = sdscat(*out, argv[i]); + *out = sdscat(*out, " - "); + } else if (strncmp(cname[i],"todo",4) == 0) + { + *out = sdscat(*out, argv[i]); + *out = sdscat(*out, "\n"); + } + } + + printf("\n"); + + return 0; +} + +static int list_todo(sqlite3 *db, char *user, sds *out) +{ + int rc; + char *err_msg = NULL; + + char sql_list_table[1024]; + snprintf(sql_list_table, 1024, "SELECT * FROM todo WHERE user='%s'", user); + + if ((rc = sqlite3_exec(db, sql_list_table, cb_list_todo_table, out, &err_msg )) != SQLITE_OK) + { + printf("Cant list todo: %s\n", err_msg); + + sqlite3_free(err_msg); + + return -1; + } + + return 0; +} + +static int del_todo(sqlite3 *db, char *user, int id) +{ + int rc; + char *err_msg; + + char sql_del_table[1024]; + snprintf(sql_del_table, 1024, "DELETE FROM todo WHERE user='%s' AND id=%d;", user, id); + + if ((rc = sqlite3_exec(db, sql_del_table, 0, 0, &err_msg )) != SQLITE_OK) + { + printf("Cant del todo: %s\n", err_msg); + + sqlite3_free(err_msg); + + return -1; + } + + return 0; +} + void *cmd_todo(void *data) { char *ret = NULL; char *req_data = NULL; int fret; + int rc; + int i; + + int count; + sds params = sdsempty(); + sds out_result = sdsempty(), out_todo = sdsempty(); + sds *tokens; + + sqlite3 *db=NULL; + sqlite3_stmt *res=NULL; + char *err_msg = NULL; + int table_todo_exists=1; rpc_call_request *req = NULL; rpc_call_response *resp = NULL; @@ -42,8 +137,90 @@ void *cmd_todo(void *data) //---------------------------------------------------------------------------- //main code - PRINT("%s-%s-%s-%s-%s\n", req->method, req->params, req->user, req->mask, req->server); + PRINT("(%s)-(%s)-(%s)-(%s)-(%s)\n", req->method, req->params, req->user, req->mask, req->server); + + if ((rc = sqlite3_open("todo.db", &db)) != SQLITE_OK) + { + printf("Cannot open todo database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + + return 1; + } + + //check if table excists + char *sql_check_table = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='todo'"; + if ((rc = sqlite3_prepare_v2(db, sql_check_table, -1, &res, 0)) != SQLITE_OK) + { + printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + + rc = sqlite3_step(res); + if (rc == SQLITE_ROW) + { + int iret = sqlite3_column_int(res, 0); + //printf("%d\n", iret); + if (iret == 0) + { + table_todo_exists = 0; + } else + { + table_todo_exists = 1; + } + } + + //if table doesnt excists then create new one + if (table_todo_exists == 0) + { + char *sql_create_table = "CREATE TABLE todo(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, todo TEXT)"; + if ((rc = sqlite3_exec(db, sql_create_table, 0, 0, &err_msg)) != SQLITE_OK) + { + printf("Cant create table: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + } + } + + params = sdsnew(req->params); + tokens = sdssplitargs(params, &count); + if (strncmp(tokens[1],"add",3) == 0) + { + PRINT("ADD\n"); + if (count > 2) + { + sds sAdd = sdsempty(); + for (i=2;i<count;i++) + { + sAdd = sdscat(sAdd, tokens[i]); + sAdd = sdscat(sAdd, " "); + } + add_todo(db, req->user, sAdd); + } + } else if (strncmp(tokens[1],"del",3) == 0) + { + PRINT("DEL\n"); + if (count > 2) + { + int id = atoi(tokens[2]); + del_todo(db, req->user, id); + } + + } else if (strncmp(tokens[1],"list",4) == 0) + { + PRINT("LIST\n"); + list_todo(db, req->user, &out_todo); + PRINT("%s\n",out_todo); + out_result = sdscatsds(out_result, out_todo); + out_result[sdslen(out_result)-1] = 0; //dirty hack mate + sdsfree(out_todo); + } else + { + + } + sqlite3_finalize(res); + sqlite3_close(db); //---------------------------------------------------------------------------- //prepare response @@ -51,7 +228,7 @@ void *cmd_todo(void *data) nb_init(nb_resp); PNL(); - resp = rpc_call_resp_new("Success","None",1); + resp = rpc_call_resp_new(out_result,"None",1); resp->user = alloc_new_str(" "); resp->server = alloc_new_str(" "); resp->mask = alloc_new_str(" "); diff --git a/cmd/cmd_todo.h b/cmd/cmd_todo.h index 1e436ba..210529e 100644 --- a/cmd/cmd_todo.h +++ b/cmd/cmd_todo.h @@ -9,9 +9,12 @@ #include <sys/types.h> #include <sys/stat.h> +#include <sqlite3.h> + #include "util.h" #include "debug.h" #include "nbrpc_call.h" +#include "sds.h" void *cmd_todo(void *data); diff --git a/nbrpc_call.c b/nbrpc_call.c index 67242a9..b261cfb 100644 --- a/nbrpc_call.c +++ b/nbrpc_call.c @@ -108,7 +108,7 @@ int rpc_call_req_marsh( rpc_call_request *req, netbyte_store **nb_req) nb_add_u8arr(nb, &nb_user); nb_u8arr_create(&nb_mask, strlen(req->mask), (uint8_t *)req->mask); - nb_add_u8arr(nb, &nb_params); + nb_add_u8arr(nb, &nb_mask); nb_u8arr_create(&nb_server, strlen(req->server), (uint8_t *)req->server); nb_add_u8arr(nb, &nb_server); diff --git a/nbrpc_event.c b/nbrpc_event.c index 85e4aab..d830e5d 100644 --- a/nbrpc_event.c +++ b/nbrpc_event.c @@ -173,6 +173,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } if (0 == nb_val(nb_req, 1, &type)) @@ -185,6 +188,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } if (0 == nb_val(nb_req, 2, &type)) @@ -198,6 +204,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } //user @@ -212,6 +221,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } //mask @@ -226,6 +238,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } //server @@ -240,6 +255,9 @@ int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) { ENL(); } + } else + { + ENL(); } *req = __req; @@ -162,10 +162,11 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server, if (mask != NULL) { PRINT("%s mask\n", mask); - req->mask = alloc_new_str("asd!!"); + req->mask = alloc_new_str(mask); } if (server != NULL) { + PRINT("%s server\n", server); req->server = alloc_new_str(server); } rpc_call_req_marsh(req, &nb_req); |