#include "cmd_todo.h" //https://github.com/littlstar/b64.c //https://www.google.nl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwiMu9_F5ZrXAhVLOMAKHZ6NDQYQFghDMAM&url=https%3A%2F%2Fopensource.apple.com%2Fsource%2FQuickTimeStreamingServer%2FQuickTimeStreamingServer-452%2FCommonUtilitiesLib%2Fbase64.c&usg=AOvVaw3tk0M33ne4ru28Bn_R1KI3 #define TODO_MAX_PER_USER 64 static int add_todo(sqlite3 *db, char *user, char *todo) { int rc; char sql_add_table[3*256]; char *b64_user, *b64_todo; sqlite3_stmt *res=NULL; b64_user = b64_encode(user, strlen(user)); b64_todo = b64_encode(todo, strlen(todo)); //check how many todo stuff is for single user snprintf(sql_add_table, 1024, "SELECT COUNT(*) FROM todo WHERE user='%s';", b64_user, b64_todo); printf("%s\n", sql_add_table); if ((rc = sqlite3_prepare_v2(db, sql_add_table, -1, &res, 0)) != SQLITE_OK) { printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db)); free(b64_user); free(b64_todo); return -1; } rc = sqlite3_step(res); if (rc == SQLITE_ROW) { int iret = sqlite3_column_int(res, 0); PRINT("%d\n",iret); if (iret >= TODO_MAX_PER_USER) { free(b64_user); free(b64_todo); return -1; } } sqlite3_finalize(res); //insert new value snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", b64_user, b64_todo); printf("%s\n", sql_add_table); free(b64_user); free(b64_todo); 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; i1024*1024) { printf("File should be limited to just 1MB pal\n"); return ret; } if ((rc = sqlite3_open("todo.db", &db)) != SQLITE_OK) { printf("Cannot open todo database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return ret; } //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 ret; } rc = sqlite3_step(res); if (rc == SQLITE_ROW) { int iret = sqlite3_column_int(res, 0); 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;iuser, sAdd); } } else if (strncmp(tokens[1],"del",3) == 0) { PRINT("DEL\n"); if (count > 2) { int id = atoi(tokens[2]); PRINT("%d\n",id); 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 if (strncmp(tokens[1],"total",5) == 0) { PRINT("LIST\n"); list_total(db, &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); } sqlite3_finalize(res); sqlite3_close(db); //---------------------------------------------------------------------------- //prepare response nb_resp = malloc(sizeof(netbyte_store)); nb_init(nb_resp); PNL(); resp = rpc_call_resp_new(out_result,"None",1); resp->user = alloc_new_str(" "); resp->server = alloc_new_str(" "); resp->mask = alloc_new_str(" "); PNL(); rpc_call_resp_marsh(resp, &nb_resp); PNL(); nb_buf = (char *)nb_create(nb_resp); if (nb_buf) { ret = nb_buf; } nb_free(nb_resp); rpc_call_resp_free(resp); return ret; }