1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
#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
#define TODO_MAX_DB_FILE_SIZE (1024*1024)
#define TODO_DB_NAME "todo.db"
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((unsigned char *)user, strlen(user));
b64_todo = b64_encode((unsigned char *)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);
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; 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)
{
char *b64_todo = (char *)b64_decode(argv[i], strlen(argv[i]));
*out = sdscat(*out, b64_todo);
free(b64_todo);
*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];
char *b64_user;
b64_user = b64_encode((unsigned char *)user, strlen(user));
snprintf(sql_list_table, 1024, "SELECT * FROM todo WHERE user='%s'", b64_user);
printf("%s\n",sql_list_table);
free(b64_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 list_total(sqlite3 *db, sds *out)
{
int rc;
sqlite3_stmt *res=NULL;
char sql_list_total[1024];
snprintf(sql_list_total, 1024, "SELECT COUNT(*) FROM todo;");
printf("%s\n",sql_list_total);
if ((rc = sqlite3_prepare_v2(db, sql_list_total, -1, &res, 0)) != SQLITE_OK)
{
printf("Cannot prepare statment: %s\n", sqlite3_errmsg(db));
return -1;
}
rc = sqlite3_step(res);
if (rc == SQLITE_ROW)
{
const char *cret = (char *)sqlite3_column_text(res, 0);
*out = sdscat(*out, cret);
}
sqlite3_finalize(res);
return 0;
}
static int del_todo(sqlite3 *db, char *user, int id)
{
int rc;
char *err_msg;
char sql_del_table[1024];
char *b64_user;
b64_user = b64_encode((unsigned char *)user, strlen(user));
snprintf(sql_del_table, 1024, "DELETE FROM todo WHERE user='%s' AND id=%d;", b64_user, id);
free(b64_user);
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;
netbyte_store *nb_req = NULL, *nb_resp=NULL;
char *nb_buf = NULL;
PRINT("TODO\n");
req_data = (char *)data;
if (!req_data)
{
PERM();
return NULL;
}
//----------------------------------------------------------------------------
//prepare request
nb_req = malloc(sizeof(netbyte_store));
nb_init(nb_req);
nb_load(nb_req, (unsigned char *)req_data);
if (nb_req == NULL)
{
ERROR("implement response\n");
return NULL;
}
fret = rpc_call_req_unmarsh(nb_req, &req);
if (fret != 0)
{
ERROR("Invalid request format\n");
return NULL;
}
//----------------------------------------------------------------------------
//main code
//check if database filesize is not bigger then 1MB, consider it for now as abuse woop woop
if (file_size(TODO_DB_NAME)>TODO_MAX_DB_FILE_SIZE)
{
printf("File should be limited to just 1MB pal\n");
return ret;
}
if ((rc = sqlite3_open(TODO_DB_NAME, &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;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 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;
}
|