From 508c805983afd1a08ff3e303eead5d7fd137f3e0 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 3 Aug 2013 17:13:44 -0400 Subject: [PATCH 1/5] added command to clear prompt screen --- prompt.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/prompt.c b/prompt.c index 1db6088..a47238a 100644 --- a/prompt.c +++ b/prompt.c @@ -251,20 +251,21 @@ static void execute(ToxWindow* self, char* cmd) { wprintw(self->window, "Message successfully sent.\n"); } } + + else if (!strncmp(cmd, "clear", strlen("clear"))) { + wclear(self->window); + } else { wprintw(self->window, "Invalid syntax.\n"); } } static void prompt_onKey(ToxWindow* self, int key) { - // PRINTABLE characters: Add to line. if(isprint(key)) { - if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) { return; } - prompt_buf[prompt_buf_pos++] = key; prompt_buf[prompt_buf_pos] = 0; } @@ -273,14 +274,12 @@ static void prompt_onKey(ToxWindow* self, int key) { else if(key == '\n') { wprintw(self->window, "\n"); execute(self, prompt_buf); - prompt_buf_pos = 0; prompt_buf[0] = 0; } // BACKSPACE key: Remove one character from line. else if(key == 0x107 || key == 0x8 || key == 0x7f) { - if(prompt_buf_pos != 0) { prompt_buf[--prompt_buf_pos] = 0; } @@ -315,6 +314,7 @@ static void print_usage(ToxWindow* self) { wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); wprintw(self->window, " myid : Print your ID\n"); + wprintw(self->window, " clear : Clear the screen\n"); wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " help : Print this message again\n"); From 6ea6af557bfd2392938eafaf824737e24980b1e7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 4 Aug 2013 04:42:17 -0400 Subject: [PATCH 2/5] added command functionality to chat windows and a few minor improvements --- chat.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++--------- prompt.c | 9 +++--- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/chat.c b/chat.c index a90bb2a..72fa5d4 100644 --- a/chat.c +++ b/chat.c @@ -26,6 +26,8 @@ typedef struct { } ChatContext; extern void fix_name(uint8_t* name); +void print_help(ChatContext* self); +void execute(ToxWindow* self, ChatContext* ctx, char* cmd); static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { ChatContext* ctx = (ChatContext*) self->x; @@ -92,24 +94,19 @@ int string_is_empty(char *string) static void chat_onKey(ToxWindow* self, int key) { ChatContext* ctx = (ChatContext*) self->x; - time_t now; - time(&now); - struct tm * timeinfo; - timeinfo = localtime(&now); - + /* PRINTABLE characters: Add to line */ if(isprint(key)) { - if(ctx->pos != sizeof(ctx->line)-1) { ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } + /* RETURN key: Execute command or print line */ else if(key == '\n') { - if(!string_is_empty(ctx->line)) { - /* make sure the string has at least non-space character */ - wattron(ctx->history, COLOR_PAIR(2)); - wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + if (ctx->line[0] == '/') + execute(self, ctx, ctx->line); + else { wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "you: ", ctx->line); wattroff(ctx->history, COLOR_PAIR(1)); @@ -120,18 +117,65 @@ static void chat_onKey(ToxWindow* self, int key) { wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(3)); } - - ctx->line[0] = '\0'; - ctx->pos = 0; } + ctx->line[0] = '\0'; + ctx->pos = 0; } + /* BACKSPACE key: Remove one character from line */ else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(ctx->pos != 0) { ctx->line[--ctx->pos] = '\0'; } } +} +void execute(ToxWindow* self, ChatContext* ctx, char* cmd) +{ + if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { + wclear(self->window); + wclear(ctx->history); + } + else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) + print_help(ctx); + else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { + endwin(); + exit(0); + } + else if (!strncmp(cmd, "/status ", strlen("/status "))) { + char* msg; + msg = strchr(cmd, ' '); + if(msg == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + msg++; + m_set_userstatus((uint8_t*) msg, strlen(msg)+1); + wprintw(ctx->history, "Status set to: %s\n", msg); + } + else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { + char* nick; + nick = strchr(cmd, ' '); + if(nick == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + nick++; + setname((uint8_t*) nick, strlen(nick)+1); + wprintw(ctx->history, "Nickname set to: %s\n", nick); + } + else if(!strcmp(cmd, "/myid")) { + char id[32*2 + 1] = {0}; + int i; + for (i = 0; i < 32; i++) { + char xx[3]; + snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); + strcat(id, xx); + } + wprintw(ctx->history, "Your ID: %s\n", id); + } + else + wprintw(ctx->history, "Invalid command.\n"); } static void chat_onDraw(ToxWindow* self) { @@ -164,6 +208,21 @@ static void chat_onInit(ToxWindow* self) { ctx->linewin = subwin(self->window, 2, x, y - 3, 0); } +void print_help(ChatContext* self) { + wattron(self->history, COLOR_PAIR(2) | A_BOLD); + wprintw(self->history, "\nCommands:\n"); + wattroff(self->history, A_BOLD); + + wprintw(self->history, " /status : Set your status\n"); + wprintw(self->history, " /nick : Set your nickname\n"); + wprintw(self->history, " /myid : Print your ID\n"); + wprintw(self->history, " /clear : Clear the screen\n"); + wprintw(self->history, " /quit or /exit : Exit program\n"); + wprintw(self->history, " /help : Print this message again\n\n"); + + wattroff(self->history, COLOR_PAIR(2)); +} + ToxWindow new_chat(int friendnum) { ToxWindow ret; diff --git a/prompt.c b/prompt.c index a47238a..365aee4 100644 --- a/prompt.c +++ b/prompt.c @@ -148,7 +148,7 @@ static void execute(ToxWindow* self, char* cmd) { wprintw(self->window, "Friend request already sent.\n"); break; case -5: - wprintw(self->window, "[i] Undefined error when adding friend.\n"); + wprintw(self->window, "Undefined error when adding friend.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); @@ -178,12 +178,13 @@ static void execute(ToxWindow* self, char* cmd) { nick = strchr(cmd, ' '); if(nick == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); - wprintw(self->window, "Nickname set to: %s.\n", nick); + wprintw(self->window, "Nickname set to: %s\n", nick); } else if(!strcmp(cmd, "myid")) { char id[32*2 + 1] = {0}; @@ -195,7 +196,7 @@ static void execute(ToxWindow* self, char* cmd) { strcat(id, xx); } - wprintw(self->window, "%s\n", id); + wprintw(self->window, "Your ID: %s\n", id); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; @@ -256,7 +257,7 @@ static void execute(ToxWindow* self, char* cmd) { wclear(self->window); } else { - wprintw(self->window, "Invalid syntax.\n"); + wprintw(self->window, "Invalid command.\n"); } } From 18dcb06920cba20ef204e9e689e376e50320807c Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 4 Aug 2013 05:00:16 -0400 Subject: [PATCH 3/5] didn't mean to delete that --- chat.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/chat.c b/chat.c index 72fa5d4..846bcf2 100644 --- a/chat.c +++ b/chat.c @@ -94,6 +94,11 @@ int string_is_empty(char *string) static void chat_onKey(ToxWindow* self, int key) { ChatContext* ctx = (ChatContext*) self->x; + time_t now; + time(&now); + struct tm * timeinfo; + timeinfo = localtime(&now); + /* PRINTABLE characters: Add to line */ if(isprint(key)) { if(ctx->pos != sizeof(ctx->line)-1) { @@ -107,11 +112,15 @@ static void chat_onKey(ToxWindow* self, int key) { if (ctx->line[0] == '/') execute(self, ctx, ctx->line); else { - wattron(ctx->history, COLOR_PAIR(1)); - wprintw(ctx->history, "you: ", ctx->line); - wattroff(ctx->history, COLOR_PAIR(1)); - wprintw(ctx->history, "%s\n", ctx->line); - + if(!string_is_empty(ctx->line)) { + /* make sure the string has at least non-space character */ + wattron(ctx->history, COLOR_PAIR(2)); + wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattron(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "you: ", ctx->line); + wattroff(ctx->history, COLOR_PAIR(1)); + wprintw(ctx->history, "%s\n", ctx->line); + } if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send message.\n"); From 337d87a3ef02be3cc3bf6525f22022f113c43fbc Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 4 Aug 2013 05:32:53 -0400 Subject: [PATCH 4/5] already a pull request with this addition --- prompt.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/prompt.c b/prompt.c index 365aee4..3fa9d77 100644 --- a/prompt.c +++ b/prompt.c @@ -253,9 +253,6 @@ static void execute(ToxWindow* self, char* cmd) { } } - else if (!strncmp(cmd, "clear", strlen("clear"))) { - wclear(self->window); - } else { wprintw(self->window, "Invalid command.\n"); } @@ -315,7 +312,6 @@ static void print_usage(ToxWindow* self) { wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); wprintw(self->window, " myid : Print your ID\n"); - wprintw(self->window, " clear : Clear the screen\n"); wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " help : Print this message again\n"); From 6f9dea8510db2192ffbf4bbfa4bcd5c8c2d1cae5 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Sun, 4 Aug 2013 03:52:24 -0700 Subject: [PATCH 5/5] Manally merged #314 and #317 --- chat.c | 4 +++- prompt.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/chat.c b/chat.c index 846bcf2..936eb86 100644 --- a/chat.c +++ b/chat.c @@ -51,6 +51,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(4)); wprintw(ctx->history, "%s: ", nick); wattroff(ctx->history, COLOR_PAIR(4)); @@ -116,6 +117,7 @@ static void chat_onKey(ToxWindow* self, int key) { /* make sure the string has at least non-space character */ wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "you: ", ctx->line); wattroff(ctx->history, COLOR_PAIR(1)); @@ -254,6 +256,6 @@ ToxWindow new_chat(int friendnum) { x->friendnum = friendnum; ret.x = (void*) x; - + free(x); return ret; } diff --git a/prompt.c b/prompt.c index 3fa9d77..463b935 100644 --- a/prompt.c +++ b/prompt.c @@ -89,7 +89,9 @@ static void execute(ToxWindow* self, char* cmd) { } dht.ip.i = resolved_address; - DHT_bootstrap(dht, hex_string_to_bin(key)); + unsigned char *binary_string = hex_string_to_bin(key); + DHT_bootstrap(dht, binary_string); + free(binary_string); } else if(!strncmp(cmd, "add ", strlen("add "))) { uint8_t id_bin[32];