/* create a new note */ int db_new(char* search) { /* search by name */ sql_result *result; result = db_get("SELECT * FROM nodau WHERE name = '%s'",search); if (result) { /* there's already a note with that name, so error and return */ if (result->num_rows) { printf("There is already a note called '%s'\n",search); db_result_free(result); return 1; } /* free the search result */ db_result_free(result); } /* create the new entry */ db_insert(search,"new entry"); if (error_msg) printf("%s\n",error_msg); /* open for editing */ return db_edit(search); }
/* encrypt an existing note, or create a new encrypted note */ int db_encrypt(char* search) { /* search by name */ sql_result *result; char* crypt; int r; result = db_get("SELECT * FROM nodau WHERE name = '%s'",search); /* there's already a note with that name */ if (result->num_rows) { char* name; char* text; /* get the data */ name = result->data[COLUMN(0,COL_NAME)]; text = result->data[COLUMN(0,COL_TEXT)]; crypt = result->data[COLUMN(0,COL_CRYPT)]; /* encrypt it if it's not already */ if (!strcmp(crypt,"false")) { crypt = crypt_get_key(); r = db_update(name,text); } else { printf("Note '%s' is already encrypted\n",search); } db_result_free(result); return r; } /* free the search result */ db_result_free(result); /* create the new entry */ db_insert(search,"new entry"); if (error_msg) fprintf(stderr,"%s\n",error_msg); crypt = crypt_get_key(); /* open for editing */ return db_edit(search); }
void edit(http_request_t req, socket_t * clientSocket,db_t * db) { ptrdiff_t nameLeng=(strpbrk(req.uri,"0123456789")-1)-(req.uri+9); char *name=malloc(sizeof(char)*nameLeng+1); memcpy(name,req.uri+9,nameLeng); name[nameLeng]='\0'; db_edit(req,db); char buf[1000]; sprintf(buf,"<html>" "<head>" "<title>Page Title</title>" "</head>" "<body>" "<p align=\"center\">EDIT SUCCESS</p>" "<p align=\"center\"><a href=\"/db/%s\" align=\"center\" >To %s</a></p>" "</body>" "</html>",name,name); socket_write_string(clientSocket,buf); }
int main(int argc, char** argv) { char* args; /* no option, print usage */ if (argc < 2) { usage(); return 0; } config_load(); /* connect to the db or error */ if (db_connect()) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } /* compile the arguments */ args = get_args(argc,argv); /* if listing notes */ if (strcmp(argv[1],"list") == 0) { db_list(args); }else{ /* if null argument print usage */ if (args == NULL || argc <3) { usage(); /* if creating a new note */ }else if (strcmp(argv[1],"new") == 0 || strcmp(argv[1],"create") == 0) { db_new(args); /* if opening/editing an existing note */ }else if (strcmp(argv[1],"open") == 0 || strcmp(argv[1],"edit") == 0) { db_edit(args); /* append to a note if data is on stdin */ }else if (strcmp(argv[1],"append") == 0) { if (isatty(STDIN_FILENO)) { db_edit(args); }else{ db_append(args); } /* encrypt a new or existing note */ }else if (strcmp(argv[1],"encrypt") == 0) { db_encrypt(args); /* decrypt an existing note */ }else if (strcmp(argv[1],"decrypt") == 0) { db_decrypt(args); /* display an existing note */ }else if (strcmp(argv[1],"show") == 0) { db_show(args); /* if deleting note/s */ }else if (strcmp(argv[1],"del") == 0) { db_del(args); /* unknown option, print usage */ }else{ usage(); } } /* free args if we can */ if (args != NULL) free(args); /* close the database */ sqlite3_close(db); /* save config */ config_save(); return 0; }