From afb3657fa2cbc6bd9e5901d6c5590b09d15c2d89 Mon Sep 17 00:00:00 2001
From: FreeArtMan <dos21h@gmail.com>
Date: Wed, 22 Nov 2017 21:42:17 +0000
Subject: Added todo file size max 1MB and 64 todo's per user

---
 agni.c         | 42 +++++++++++++++++++++---------------------
 cmd/cmd_lua.c  | 26 ++++++++++++--------------
 cmd/cmd_todo.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 tbl_qcmd.c     | 38 +++++++++++++++++++-------------------
 util.c         | 10 ++++++++++
 util.h         |  3 +++
 6 files changed, 104 insertions(+), 59 deletions(-)

diff --git a/agni.c b/agni.c
index 520ef45..e4be388 100644
--- a/agni.c
+++ b/agni.c
@@ -78,9 +78,9 @@ INPUT(string)->OUTPUT(string)
 */
 tble_cmd_resp* cllbk_wrapper( void *(*call)(void *), tble_cmd_param *param)
 {
-	void *data_out = NULL;
-	tble_cmd_resp *resp = NULL;
-	char *data=NULL;
+	void          *data_out = NULL;
+	tble_cmd_resp *resp     = NULL;
+	char          *data     = NULL;
 
 	if (call == NULL)
 	{
@@ -223,11 +223,11 @@ int th_start_client(void *data)
 	int fret=-1;
 
 	server_cfg *cfg = data;
-	mq_ntf_mdt *mq = cfg->mq;
+	mq_ntf_mdt *mq  = cfg->mq;
 	struct mq_attr out_attr, *ptr_out_attr=&out_attr;
 	struct mq_attr in_attr, *ptr_in_attr=&in_attr;
-	char *out_buf = NULL;
-	char *in_buf = NULL;
+	char *out_buf   = NULL;
+	char *in_buf    = NULL;
 
 	//network creation var
 	int cret = -1;
@@ -237,13 +237,13 @@ int th_start_client(void *data)
 	char cmd_buf[TH_CONN_BUF_SZ];
 
 	//irc parsting
-	irc_buf *ib = NULL;
+	irc_buf *ib     = NULL;
 	irc_token *itok = NULL;
-	char *irc_line = NULL;
+	char *irc_line  = NULL;
 
 	//table to match response/request
 	tble_qcmd *qcmd = NULL;
-	tbl_qcmd *qtbl = NULL;
+	tbl_qcmd *qtbl  = NULL;
 
 	//create response table
 	qtbl = tbl_qcmd_c(100);
@@ -254,9 +254,9 @@ int th_start_client(void *data)
 
 	//////////////////////////////////////////////////////////////////////////////
 	//nbrpc
-	char *buf_nb = NULL;
-	rpc_request  *req=NULL;
-	rpc_response *resp=NULL;
+	char          *buf_nb = NULL;
+	rpc_request   *req    = NULL;
+	rpc_response  *resp   = NULL;
 	netbyte_store *nb_req, *nb_resp;
 
 	nb_resp = malloc(sizeof(netbyte_store));
@@ -697,10 +697,10 @@ int th_event_manager(void *data)
 {
 	event_handler_cfg *cfg = data;
 	atomic_fetch_add(&cfg->running,1);
-	mq_ntf_mdt *mq=NULL,*mq_cur=NULL;
-	int i_mq=0; //to iter trought mqueue
+	mq_ntf_mdt *mq= NULL,*mq_cur=NULL;
+	int   i_mq    = 0; //to iter trought mqueue
 	char *out_buf = NULL;
-	char *in_buf = NULL;
+	char *in_buf  = NULL;
 	int run;
 	int mq_event;
 
@@ -782,9 +782,9 @@ int th_event_manager(void *data)
 
 	//////////////////////////////////////////////////////////////////////////////
 	//nbrpc prepare variables
-	rpc_request  *req=NULL;
-	rpc_response *resp=NULL;
-	netbyte_store *nb_req=NULL, *nb_resp=NULL;
+	rpc_request  *req     = NULL;
+	rpc_response *resp    = NULL;
+	netbyte_store *nb_req = NULL, *nb_resp=NULL;
 	char *buf_nb = NULL;
 
 	nb_resp = malloc(sizeof(netbyte_store)); //not freed
@@ -929,9 +929,9 @@ int main(int argc, char **argv)
 	int i,j;
 
 	int cnt_servers,cnt_running;
-	server_cfg *cfg_list;
+	server_cfg        *cfg_list;
 	event_handler_cfg *evhnd_cfg;
-	mq_ntf_mdt *mq_array;
+	mq_ntf_mdt        *mq_array;
 
 	/*set atomic variables to init value*/
 	atomic_store(&_glbl_id, 0);
@@ -968,7 +968,7 @@ int main(int argc, char **argv)
 	/* For each configuration create listener */
 
 	for (i=0;i<cnt_servers;i++)
-	{
+	{	
 		/*initialise server config*/
 		server_cfg *srvc = &cfg_list[i];
 		//irc_server_conf *isrvc = &server_list[i];
diff --git a/cmd/cmd_lua.c b/cmd/cmd_lua.c
index 105cb47..820620f 100644
--- a/cmd/cmd_lua.c
+++ b/cmd/cmd_lua.c
@@ -127,6 +127,7 @@ int lua_excute(const char *fname, rpc_call_request *req, rpc_call_response **res
   {
     int t;
     int top = lua_gettop(L);
+    int len = lua_rawlen(L,1);
 
     if (top == 1)
     {
@@ -134,23 +135,26 @@ int lua_excute(const char *fname, rpc_call_request *req, rpc_call_response **res
       if (t == LUA_TTABLE)
       {
         int j;
-        int len = lua_rawlen(L,1);
+        
         const char *r_result;
         const char *r_error;
         int r_id;
         size_t sz;
 
+
         check_table_key_string(L, "result", &r_result, &sz);
-        //resp->result = alloc_new_str(result);
-        printf("RESULT:%s\n", r_result);
+
+        //printf("RESULT:%s\n", r_result);
         check_table_key_integer(L, "id", &r_id);
-        //resp->id = result_i;
-        printf("ID:%d\n", r_id);
+
+        //printf("ID:%d\n", r_id);
         check_table_key_string(L, "error",  &r_error, &sz);
-        //resp->error = alloc_new_str(result);
-        printf("ERROR:%s\n", r_error);
 
-        *resp = rpc_call_resp_new(alloc_new_str(r_result), alloc_new_str(r_error), 1);
+        //printf("ERROR:%s\n", r_error);
+
+        if (r_result == NULL) r_result = "!";
+        if (r_error == NULL) r_error = "!";
+        *resp = rpc_call_resp_new(alloc_new_str(r_result), alloc_new_str(r_error), r_id);
         (*resp)->user = alloc_new_str(" ");
         (*resp)->server = alloc_new_str(" ");
         (*resp)->mask = alloc_new_str(" ");
@@ -215,13 +219,9 @@ void *cmd_lua(void *data)
 
   //----------------------------------------------------------------------------
   //main code
-
-
   params = sdsnew(req->params);
   tokens = sdssplitargs(params, &count);
 
-
-
   if (-1 == lua_excute(tokens[1], req, &resp))
   {
     resp = rpc_call_resp_new(out_result,"None",1);
@@ -238,10 +238,8 @@ void *cmd_lua(void *data)
   nb_resp = malloc(sizeof(netbyte_store));
   nb_init(nb_resp);
 
-  PNL();
   rpc_call_resp_marsh(resp, &nb_resp);
 
-  PNL();
   nb_buf = (char *)nb_create(nb_resp);
   if (nb_buf)
   {
diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c
index e90aeb7..8cd712d 100644
--- a/cmd/cmd_todo.c
+++ b/cmd/cmd_todo.c
@@ -3,14 +3,44 @@
 //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);
@@ -156,14 +186,19 @@ void *cmd_todo(void *data)
   //----------------------------------------------------------------------------
   //main code
 
-  PRINT("(%s)-(%s)-(%s)-(%s)-(%s)\n", req->method, req->params, req->user, req->mask, req->server);
+  //check if database filesize is not bigger then 1MB, consider it for now as abuse woop woop
+  if (file_size("todo.db")>1024*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 1;
+    return ret;
   }
 
   //check if table excists
@@ -172,14 +207,13 @@ void *cmd_todo(void *data)
   {
     printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db));
     sqlite3_close(db);
-    return 1;
+    return ret;
   }
 
   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;
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
index ca8830f..b360e8b 100644
--- a/tbl_qcmd.c
+++ b/tbl_qcmd.c
@@ -83,9 +83,9 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd)
 
 int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server, void **resp)
 {
-	int ret=-1;
-	void *clbk_data=NULL;
-	char *char_iter=NULL;
+	int   ret       = -1;
+	void *clbk_data = NULL;
+	char *char_iter = NULL;
 
 	int i;
 	tble_exec *el=NULL;
@@ -141,10 +141,10 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server,
 				ret_resp = el->callback(clbk_data); //no params for now
 			} else if (el->type == TBL_T_RPC)
 			{
-				rpc_call_request *req = NULL;
+				rpc_call_request  *req = NULL;
 				rpc_call_response *resp = NULL;
-				netbyte_store *nb_req=NULL, *nb_resp=NULL;
-				char *buf_nb = NULL, *resp_buf=NULL;
+				netbyte_store     *nb_req=NULL, *nb_resp=NULL;
+				char              *buf_nb = NULL, *resp_buf=NULL;
 
 				nb_resp = malloc(sizeof(netbyte_store));
 				nb_req = malloc(sizeof(netbyte_store));
@@ -247,7 +247,7 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server,
 
 tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd)
 {
-	tble_exec *ret = NULL;
+	tble_exec *ret      = NULL;
 	tble_exec *cur_exec = NULL;
 	int i;
 
@@ -392,8 +392,8 @@ tbl_qcmd* tbl_qcmd_c(int size)
 	MVAR_ALLOC_STRC(ret,tbl_qcmd,{
 		return NULL;});
 
-	ret->id = uniq_id();
-	ret->size = 0;
+	ret->id       = uniq_id();
+	ret->size     = 0;
 	ret->max_size = size;
 
 	//alloc array of (tble_qcmd*) pointers
@@ -494,9 +494,9 @@ int tbl_qcmd_chk(tbl_qcmd *tbl)
 //delete but dont free from list? well lets free
 int tbl_qcmd_del_by_idx(tbl_qcmd *tbl, int idx)
 {
-	int ret=-1;
-	tble_qcmd *replace_cmd=NULL;
-	tble_qcmd *remove_cmd=NULL;
+	int        ret         = -1;
+	tble_qcmd *replace_cmd = NULL;
+	tble_qcmd *remove_cmd  = NULL;
 
 	if (tbl == NULL || idx < 0)
 	{
@@ -540,7 +540,7 @@ int tbl_qcmd_del_by_id(tbl_qcmd *tbl, int id)
 {
 	int i;
 	tble_qcmd *replace_cmd = NULL;
-	tble_qcmd *remove_cmd = NULL;
+	tble_qcmd *remove_cmd  = NULL;
 
 
 	if (tbl == NULL)
@@ -685,12 +685,12 @@ tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd)
 		PERM();
 		return NULL;});
 
-	ret->id = uniq_id();
-	ret->qid = cmd->id;
+	ret->id       = uniq_id();
+	ret->qid      = cmd->id;
 	ret->ircident = alloc_new_str(cmd->ircident);
-	ret->cmd = alloc_new_str(cmd->cmd);
-	ret->param = alloc_new_str(cmd->param);
-	ret->out_q = cmd->out_q;
+	ret->cmd      = alloc_new_str(cmd->cmd);
+	ret->param    = alloc_new_str(cmd->param);
+	ret->out_q    = cmd->out_q;
 
 	return ret;
 }
@@ -745,7 +745,7 @@ tble_cmd_resp* tbl_cmd_resp_c(tble_cmd_param *param)
 		return NULL;
 	});
 
-	ret->id = uniq_id();
+	ret->id  = uniq_id();
 	ret->qid = param->qid;
 
 	return ret;
diff --git a/util.c b/util.c
index b0d8efe..d167193 100644
--- a/util.c
+++ b/util.c
@@ -48,4 +48,14 @@ char *alloc_new_str_s(char *str, size_t size)
 char *alloc_new_str(char *str)
 {
 	return alloc_new_str_s(str, strlen(str));
+}
+
+off_t file_size(const char *fname)
+{
+	struct stat st;
+	if ( !stat( fname, &st ) )
+	{
+		return st.st_size; 
+	}
+	return 0; //hehe if error or file is 0, same woop woop
 }
\ No newline at end of file
diff --git a/util.h b/util.h
index ef50d2b..bd9e54c 100644
--- a/util.h
+++ b/util.h
@@ -5,9 +5,12 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdatomic.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 int uniq_id();
 char *alloc_new_str_s(char *str, size_t size);
 char *alloc_new_str(char *str);
+off_t file_size(const char *fname);
 
 #endif
\ No newline at end of file
-- 
cgit v1.2.3