From 026790e181a4dd26a60bcbada3e546a1c5e50888 Mon Sep 17 00:00:00 2001 From: plutooo Date: Tue, 30 Jul 2013 12:47:40 -0700 Subject: Fixed a bunch of bugs in TOXIC, added friendlist and more. --- testing/toxic/prompt.c | 109 +++++++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 40 deletions(-) (limited to 'testing/toxic/prompt.c') diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 4a59cc7b..22d9eb9e 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -15,6 +15,8 @@ uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX +extern void on_friendadded(int friendnumber); + // XXX: int add_req(uint8_t* public_key) { memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); @@ -40,7 +42,6 @@ static int prompt_buf_pos=0; static void execute(ToxWindow* self, char* cmd) { - // quit/exit: Exit program. if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { endwin(); exit(0); @@ -53,33 +54,32 @@ static void execute(ToxWindow* self, char* cmd) { ip = strchr(cmd, ' '); if(ip == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - ip++; port = strchr(ip, ' '); if(port == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - port[0] = 0; port++; key = strchr(port, ' '); if(key == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - key[0] = 0; key++; if(atoi(port) == 0) { + wprintw(self->window, "Invalid syntax.\n"); return; } - wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key); - dht.port = htons(atoi(port)); int resolved_address = resolve_addr(ip); @@ -91,38 +91,62 @@ static void execute(ToxWindow* self, char* cmd) { DHT_bootstrap(dht, hex_string_to_bin(key)); } else if(!strncmp(cmd, "add ", strlen("add "))) { + uint8_t id_bin[32]; + size_t i; + char xx[3]; + uint32_t x; + char* id; char* msg; int num; id = strchr(cmd, ' '); - if(id == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - id++; msg = strchr(id, ' '); - if(msg == NULL) { + if(msg != NULL) { + msg[0] = 0; + msg++; + } + else msg = ""; + + if(strlen(id) != 2*32) { + wprintw(self->window, "Invalid ID length.\n"); return; } - msg[0] = 0; - msg++; + for(i=0; i<32; i++) { + xx[0] = id[2*i]; + xx[1] = id[2*i+1]; + xx[2] = '\0'; + + if(sscanf(xx, "%02x", &x) != 1) { + wprintw(self->window, "Invalid ID.\n"); + return; + } + + id_bin[i] = x; + } + + num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); - num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Friend added as %d.\n", num); + on_friendadded(num); } else if(!strncmp(cmd, "status ", strlen("status "))) { char* msg; msg = strchr(cmd, ' '); if(msg == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - msg++; + m_set_userstatus((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s.\n", msg); } @@ -133,33 +157,22 @@ static void execute(ToxWindow* self, char* cmd) { if(nick == NULL) { return; } - nick++; + setname((uint8_t*) nick, strlen(nick)+1); wprintw(self->window, "Nickname set to: %s.\n", nick); } else if(!strcmp(cmd, "myid")) { - // XXX: Clean this up - char idstring0[200]; - char idstring1[32][5]; - char idstring2[32][5]; - uint32_t i; - - for(i = 0; i < 32; i++) { - if(self_public_key[i] < 16) - strcpy(idstring1[i], "0"); - else - strcpy(idstring1[i], ""); - - sprintf(idstring2[i], "%hhX", self_public_key[i]); + char id[32*2 + 1] = {0}; + size_t i; + + for(i=0; i<32; i++) { + char xx[3]; + snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); + strcat(id, xx); } - for (i=0; i<32; i++) { - strcat(idstring0, idstring1[i]); - strcat(idstring0, idstring2[i]); - } - - wprintw(self->window, "%s\n", idstring0); + wprintw(self->window, "%s\n", id); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; @@ -167,17 +180,26 @@ static void execute(ToxWindow* self, char* cmd) { id = strchr(cmd, ' '); if(id == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } id++; - + num = atoi(id); if(num >= num_requests) { + wprintw(self->window, "Invalid syntax.\n"); return; } num = m_addfriend_norequest(pending_requests[num]); - wprintw(self->window, "Friend accepted as: %d.\n", num); + + if(num == -1) { + wprintw(self->window, "Failed to add friend.\n"); + } + else { + wprintw(self->window, "Friend accepted as: %d.\n", num); + on_friendadded(num); + } } else if(!strncmp(cmd, "msg ", strlen("msg "))) { char* id; @@ -186,16 +208,16 @@ static void execute(ToxWindow* self, char* cmd) { id = strchr(cmd, ' '); if(id == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - id++; msg = strchr(id, ' '); if(msg == NULL) { + wprintw(self->window, "Invalid syntax.\n"); return; } - msg[0] = 0; msg++; @@ -206,6 +228,9 @@ static void execute(ToxWindow* self, char* cmd) { wprintw(self->window, "Message successfully sent.\n"); } } + else { + wprintw(self->window, "Invalid syntax.\n"); + } } static void prompt_onKey(ToxWindow* self, int key) { @@ -242,9 +267,6 @@ static void prompt_onKey(ToxWindow* self, int key) { static void prompt_onDraw(ToxWindow* self) { int x, y; - mvwin(self->window,0,0); - wresize(self->window, LINES-2, COLS); - getyx(self->window, y, x); (void) x; @@ -265,11 +287,18 @@ static void print_usage(ToxWindow* self) { wprintw(self->window, " connect : Connect to DHT server\n"); wprintw(self->window, " add : Add friend\n"); + wprintw(self->window, " msg : Send message\n"); wprintw(self->window, " status : Set your status\n"); 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, " quit/exit : Exit program\n"); + + + wattron(self->window, A_BOLD); + wprintw(self->window, "Use the TAB key to navigate through the tabs.\n"); + wattroff(self->window, A_BOLD); + wattroff(self->window, COLOR_PAIR(2)); } -- cgit v1.2.3 From 439de6fa3ab50bcb4c401d9175b643b6188786d3 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Wed, 31 Jul 2013 10:36:02 +0200 Subject: Fix space/tab indentation --- testing/DHT_cryptosendfiletest.c | 4 ++-- testing/nTox.c | 16 ++++++++-------- testing/toxic/prompt.c | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'testing/toxic/prompt.c') diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index c7c33531..888dac0f 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c @@ -186,7 +186,7 @@ int main(int argc, char *argv[]) fclose(file2); } } - /* if buffer is empty and the connection timed out. */ + /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(inconnection) == 4) { crypto_kill(inconnection); } @@ -209,7 +209,7 @@ int main(int argc, char *argv[]) fclose(file2); } } - /* if buffer is empty and the connection timed out. */ + /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(connection) == 4) { crypto_kill(connection); } diff --git a/testing/nTox.c b/testing/nTox.c index 5c560079..04ae55cd 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -72,13 +72,13 @@ void print_friendlist() char *format_message(char *message, int friendnum) { - char name[MAX_NAME_LENGTH]; - if(friendnum != -1) { - getname(friendnum, (uint8_t*)name); - } else { - getself_name((uint8_t*)name); - } - char *msg = malloc(100+strlen(message)+strlen(name)+1); + char name[MAX_NAME_LENGTH]; + if(friendnum != -1) { + getname(friendnum, (uint8_t*)name); + } else { + getself_name((uint8_t*)name); + } + char *msg = malloc(100+strlen(message)+strlen(name)+1); time_t rawtime; struct tm * timeinfo; time ( &rawtime ); @@ -129,7 +129,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { new_lines("[i] could not send message"); } else { - new_lines(format_message(message, -1)); + new_lines(format_message(message, -1)); } } else if (line[1] == 'n') { diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 22d9eb9e..0cd10730 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -125,8 +125,8 @@ static void execute(ToxWindow* self, char* cmd) { xx[2] = '\0'; if(sscanf(xx, "%02x", &x) != 1) { - wprintw(self->window, "Invalid ID.\n"); - return; + wprintw(self->window, "Invalid ID.\n"); + return; } id_bin[i] = x; -- cgit v1.2.3 From 2247b7fad552d75eb0e0f4407970aa76f64b8942 Mon Sep 17 00:00:00 2001 From: plutooo Date: Wed, 31 Jul 2013 10:20:03 -0700 Subject: Added chat windows, and some clean up. --- testing/toxic/CMakeLists.txt | 3 +- testing/toxic/chat.c | 134 +++++++++++++++++++++++++++++++++++++++++++ testing/toxic/friendlist.c | 48 +++++++++++++--- testing/toxic/main.c | 45 ++++++++++++--- testing/toxic/prompt.c | 4 +- testing/toxic/windows.h | 6 ++ 6 files changed, 222 insertions(+), 18 deletions(-) create mode 100644 testing/toxic/chat.c (limited to 'testing/toxic/prompt.c') diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index c70babb7..f30d8e9e 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt @@ -6,7 +6,8 @@ set(exe_name toxic) add_executable(${exe_name} main.c prompt.c - friendlist.c) + friendlist.c + chat.c) target_link_libraries(${exe_name} curses) diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c new file mode 100644 index 00000000..53cf3319 --- /dev/null +++ b/testing/toxic/chat.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include + +#include "../../core/Messenger.h" +#include "../../core/network.h" + +#include "windows.h" + +typedef struct { + int friendnum; + + char line[256]; + size_t pos; + + WINDOW* history; + WINDOW* linewin; + +} ChatContext; + +extern void fix_name(uint8_t* name); + + +static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { + ChatContext* ctx = (ChatContext*) self->x; + uint8_t nick[MAX_NAME_LENGTH] = {0}; + + if(ctx->friendnum != num) + return; + + getname(num, (uint8_t*) &nick); + + msg[len-1] = '\0'; + nick[MAX_NAME_LENGTH-1] = '\0'; + + fix_name(msg); + fix_name(nick); + + wprintw(ctx->history, "%s: %s\n", nick, msg); +} + +static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { + ChatContext* ctx = (ChatContext*) self->x; + + if(ctx->friendnum != num) + return; + + nick[len-1] = '\0'; + fix_name(nick); + + wattron(ctx->history, COLOR_PAIR(3)); + wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick); + wattroff(ctx->history, COLOR_PAIR(3)); +} + +static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { + +} + +static void chat_onKey(ToxWindow* self, int key) { + ChatContext* ctx = (ChatContext*) self->x; + + if(isprint(key)) { + + if(ctx->pos != sizeof(ctx->line)-1) { + ctx->line[ctx->pos++] = key; + ctx->line[ctx->pos] = '\0'; + } + } + else if(key == '\n') { + wprintw(ctx->history, "you: %s\n", ctx->line); + + if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { + wprintw(ctx->history, " * Failed to send message.\n"); + } + + ctx->line[0] = '\0'; + ctx->pos = 0; + } +} + +static void chat_onDraw(ToxWindow* self) { + int x, y; + ChatContext* ctx = (ChatContext*) self->x; + + getmaxyx(self->window, y, x); + + (void) x; + if(y < 3) + return; + + wclear(ctx->linewin); + mvwhline(ctx->linewin, 0, 0, '_', COLS); + mvwprintw(ctx->linewin, 1, 0, "%s\n", ctx->line); + + wrefresh(self->window); +} + +static void chat_onInit(ToxWindow* self) { + int x, y; + ChatContext* ctx = (ChatContext*) self->x; + + getmaxyx(self->window, y, x); + + ctx->history = subwin(self->window, y - 4, x, 0, 0); + wprintw(ctx->history, "Here goes chat\n"); + scrollok(ctx->history, 1); + + ctx->linewin = subwin(self->window, 2, x, y - 3, 0); +} + +ToxWindow new_chat(int friendnum) { + ToxWindow ret; + + memset(&ret, 0, sizeof(ret)); + + ret.onKey = &chat_onKey; + ret.onDraw = &chat_onDraw; + ret.onInit = &chat_onInit; + ret.onMessage = &chat_onMessage; + ret.onNickChange = &chat_onNickChange; + ret.onStatusChange = &chat_onStatusChange; + + snprintf(ret.title, sizeof(ret.title), "[chat %d]", friendnum); + + ChatContext* x = calloc(1, sizeof(ChatContext)); + x->friendnum = friendnum; + + ret.x = (void*) x; + + return ret; +} diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index f8b8c840..629adaff 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -12,6 +12,10 @@ #include "windows.h" +extern int add_window(ToxWindow w); +extern int focus_window(int num); +extern ToxWindow new_chat(int friendnum); + #define MAX_FRIENDS_NUM 100 typedef struct { @@ -23,6 +27,7 @@ typedef struct { static friend_t friends[MAX_FRIENDS_NUM]; static int num_friends = 0; +static int num_selected = 0; void fix_name(uint8_t* name) { @@ -42,31 +47,31 @@ void fix_name(uint8_t* name) { *q = 0; } -int friendlist_nickchange(int num, uint8_t* str, uint16_t len) { +void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { if(len >= MAX_NAME_LENGTH || num >= num_friends) - return -1; + return; memcpy((char*) &friends[num].name, (char*) str, len); friends[num].name[len] = 0; fix_name(friends[num].name); - return 0; + return; } -int friendlist_statuschange(int num, uint8_t* str, uint16_t len) { +void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends) - return -1; + return; memcpy((char*) &friends[num].status, (char*) str, len); friends[num].status[len] = 0; fix_name(friends[num].status); - return 0; + return; } -int friendlist_addfriend(int num) { +int friendlist_onFriendAdded(int num) { if(num_friends == MAX_FRIENDS_NUM) return -1; @@ -82,6 +87,17 @@ int friendlist_addfriend(int num) { static void friendlist_onKey(ToxWindow* self, int key) { + if(key == KEY_UP) { + if(num_selected != 0) + num_selected--; + } + else if(key == KEY_DOWN) { + if(num_friends != 0) + num_selected = (num_selected+1) % num_friends; + } + else if(key == '\n') { + focus_window(add_window(new_chat(num_selected))); + } } static void friendlist_onDraw(ToxWindow* self) { @@ -92,11 +108,23 @@ static void friendlist_onDraw(ToxWindow* self) { if(num_friends == 0) { wprintw(self->window, "Empty. Add some friends! :-)\n"); } + else { + wattron(self->window, COLOR_PAIR(2) | A_BOLD); + wprintw(self->window, "Friend list:\n"); + wattroff(self->window, A_BOLD); + + wprintw(self->window, " ENTER: start a chat\n"); + wprintw(self->window, " UP/DOWN: navigate list\n"); + wattroff(self->window, COLOR_PAIR(2)); + } wprintw(self->window, "\n"); for(i=0; iwindow, "[%d] ", friends[i].num); + + if(i == num_selected) wattron(self->window, COLOR_PAIR(3)); + wprintw(self->window, " [%d] ", friends[i].num); + if(i == num_selected) wattroff(self->window, COLOR_PAIR(3)); attron(A_BOLD); wprintw(self->window, "%s ", friends[i].name); @@ -116,9 +144,13 @@ static void friendlist_onInit(ToxWindow* self) { ToxWindow new_friendlist() { ToxWindow ret; + memset(&ret, 0, sizeof(ret)); + ret.onKey = &friendlist_onKey; ret.onDraw = &friendlist_onDraw; ret.onInit = &friendlist_onInit; + ret.onNickChange = &friendlist_onNickChange; + ret.onStatusChange = &friendlist_onStatusChange; strcpy(ret.title, "[friends]"); return ret; diff --git a/testing/toxic/main.c b/testing/toxic/main.c index e1d1ebd0..1c095c54 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -16,9 +16,7 @@ extern ToxWindow new_prompt(); extern ToxWindow new_friendlist(); -extern int friendlist_addfriend(int num); -extern int friendlist_nickchange(int num, uint8_t* str, uint16_t len); -extern int friendlist_statuschange(int num, uint8_t* str, uint16_t len); +extern int friendlist_onFriendAdded(int num); extern int add_req(uint8_t* public_key); // XXX @@ -31,29 +29,52 @@ static ToxWindow* prompt; // CALLBACKS START void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { + size_t i; int n = add_req(public_key); wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); + + for(i=0; iwindow, "\n(message) %d: %s!\n", friendnumber, string); + + for(i=0; iwindow, "\n(nickchange) %d: %s!\n", friendnumber, string); - friendlist_nickchange(friendnumber, string, length); + for(i=0; iwindow, "\n(statuschange) %d: %s!\n", friendnumber, string); - friendlist_statuschange(friendnumber, string, length); + for(i=0; i= w_num || num < 0) + return -1; + + w_active = num; + return 0; } static void init_windows() { diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 22d9eb9e..52e9bde0 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -221,7 +221,7 @@ static void execute(ToxWindow* self, char* cmd) { msg[0] = 0; msg++; - if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { + if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) { wprintw(self->window, "Error occurred while sending message.\n"); } else { @@ -312,6 +312,8 @@ static void prompt_onInit(ToxWindow* self) { ToxWindow new_prompt() { ToxWindow ret; + memset(&ret, 0, sizeof(ret)); + ret.onKey = &prompt_onKey; ret.onDraw = &prompt_onDraw; ret.onInit = &prompt_onInit; diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 2e082ca9..480fd658 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -4,7 +4,13 @@ struct ToxWindow_ { void(*onKey)(ToxWindow*, int); void(*onDraw)(ToxWindow*); void(*onInit)(ToxWindow*); + void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); + void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); + void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); + void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); char title[256]; + void* x; + WINDOW* window; }; -- cgit v1.2.3 From 0815d1110d452f174bc523c11291b8da41e01d4a Mon Sep 17 00:00:00 2001 From: plutooo Date: Wed, 31 Jul 2013 11:20:16 -0700 Subject: toxic: Made everything 1000x more userfriendly. --- testing/toxic/chat.c | 27 ++++++++++++++++++++++++--- testing/toxic/friendlist.c | 37 +++++++++++++++++++++++-------------- testing/toxic/main.c | 25 ++++++++++++++++++++++--- testing/toxic/prompt.c | 7 +++---- testing/toxic/windows.h | 7 +++++++ 5 files changed, 79 insertions(+), 24 deletions(-) (limited to 'testing/toxic/prompt.c') diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 53cf3319..da9ad025 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -1,3 +1,7 @@ +/* + * Toxic -- Tox Curses Client + */ + #include #include #include @@ -38,7 +42,13 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) fix_name(msg); fix_name(nick); - wprintw(ctx->history, "%s: %s\n", nick, msg); + wattron(ctx->history, COLOR_PAIR(4)); + wprintw(ctx->history, "%s: ", nick); + wattroff(ctx->history, COLOR_PAIR(4)); + + wprintw(ctx->history, "%s\n", msg); + + self->blink = true; } static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { @@ -70,15 +80,27 @@ static void chat_onKey(ToxWindow* self, int key) { } } else if(key == '\n') { - wprintw(ctx->history, "you: %s\n", ctx->line); + 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"); + wattroff(ctx->history, COLOR_PAIR(3)); } ctx->line[0] = '\0'; ctx->pos = 0; } + else if(key == 0x107) { + if(ctx->pos != 0) { + ctx->line[--ctx->pos] = '\0'; + } + } + } static void chat_onDraw(ToxWindow* self) { @@ -105,7 +127,6 @@ static void chat_onInit(ToxWindow* self) { getmaxyx(self->window, y, x); ctx->history = subwin(self->window, y - 4, x, 0, 0); - wprintw(ctx->history, "Here goes chat\n"); scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, 2, x, y - 3, 0); diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 629adaff..f9a413f9 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -22,7 +22,7 @@ typedef struct { uint8_t name[MAX_NAME_LENGTH]; uint8_t status[MAX_USERSTATUS_LENGTH]; int num; - + int chatwin; } friend_t; static friend_t friends[MAX_FRIENDS_NUM]; @@ -37,7 +37,7 @@ void fix_name(uint8_t* name) { uint8_t* q = name; while(*p != 0) { - if(isalnum(*p)) { + if(isprint(*p)) { *q++ = *p; } @@ -47,6 +47,16 @@ void fix_name(uint8_t* name) { *q = 0; } +void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) { + + if(num >= num_friends) + return; + + if(friends[num].chatwin == -1) { + friends[num].chatwin = add_window(new_chat(num)); + } +} + void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { if(len >= MAX_NAME_LENGTH || num >= num_friends) @@ -55,8 +65,6 @@ void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t le memcpy((char*) &friends[num].name, (char*) str, len); friends[num].name[len] = 0; fix_name(friends[num].name); - - return; } void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { @@ -67,8 +75,6 @@ void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t memcpy((char*) &friends[num].status, (char*) str, len); friends[num].status[len] = 0; fix_name(friends[num].status); - - return; } int friendlist_onFriendAdded(int num) { @@ -80,6 +86,7 @@ int friendlist_onFriendAdded(int num) { getname(num, friends[num_friends].name); strcpy((char*) friends[num_friends].name, "unknown"); strcpy((char*) friends[num_friends].status, "unknown"); + friends[num_friends].chatwin = -1; num_friends++; return 0; @@ -96,7 +103,12 @@ static void friendlist_onKey(ToxWindow* self, int key) { num_selected = (num_selected+1) % num_friends; } else if(key == '\n') { - focus_window(add_window(new_chat(num_selected))); + + if(friends[num_selected].chatwin != -1) + return; + + friends[num_selected].chatwin = add_window(new_chat(num_selected)); + focus_window(friends[num_selected].chatwin); } } @@ -110,12 +122,8 @@ static void friendlist_onDraw(ToxWindow* self) { } else { wattron(self->window, COLOR_PAIR(2) | A_BOLD); - wprintw(self->window, "Friend list:\n"); - wattroff(self->window, A_BOLD); - - wprintw(self->window, " ENTER: start a chat\n"); - wprintw(self->window, " UP/DOWN: navigate list\n"); - wattroff(self->window, COLOR_PAIR(2)); + wprintw(self->window, "Open chat with.. (up/down keys, enter)\n"); + wattroff(self->window, COLOR_PAIR(2) | A_BOLD); } wprintw(self->window, "\n"); @@ -123,7 +131,7 @@ static void friendlist_onDraw(ToxWindow* self) { for(i=0; iwindow, COLOR_PAIR(3)); - wprintw(self->window, " [%d] ", friends[i].num); + wprintw(self->window, " [#%d] ", friends[i].num); if(i == num_selected) wattroff(self->window, COLOR_PAIR(3)); attron(A_BOLD); @@ -149,6 +157,7 @@ ToxWindow new_friendlist() { ret.onKey = &friendlist_onKey; ret.onDraw = &friendlist_onDraw; ret.onInit = &friendlist_onInit; + ret.onMessage = &friendlist_onMessage; ret.onNickChange = &friendlist_onNickChange; ret.onStatusChange = &friendlist_onStatusChange; strcpy(ret.title, "[friends]"); diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 1c095c54..fffc3f84 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -32,7 +32,14 @@ void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { size_t i; int n = add_req(public_key); - wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); + wprintw(prompt->window, "\nFriend request from:\n"); + + for(i=0; i<32; i++) { + wprintw(prompt->window, "%02x", public_key[i] & 0xff); + } + wprintw(prompt->window, "\n"); + + wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); for(i=0; iwindow); + a->blink = false; a->onDraw(a); draw_bar(); diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 52e9bde0..cd9ef219 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -42,7 +42,7 @@ static int prompt_buf_pos=0; static void execute(ToxWindow* self, char* cmd) { - if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { + if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { endwin(); exit(0); } @@ -282,12 +282,11 @@ static void prompt_onDraw(ToxWindow* self) { static void print_usage(ToxWindow* self) { wattron(self->window, COLOR_PAIR(2) | A_BOLD); - wprintw(self->window, "Usage:\n"); + wprintw(self->window, "Commands:\n"); wattroff(self->window, A_BOLD); wprintw(self->window, " connect : Connect to DHT server\n"); wprintw(self->window, " add : Add friend\n"); - wprintw(self->window, " msg : Send message\n"); wprintw(self->window, " status : Set your status\n"); wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); @@ -296,7 +295,7 @@ static void print_usage(ToxWindow* self) { wattron(self->window, A_BOLD); - wprintw(self->window, "Use the TAB key to navigate through the tabs.\n"); + wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); wattroff(self->window, A_BOLD); wattroff(self->window, COLOR_PAIR(2)); diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 480fd658..dde1430f 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -1,3 +1,9 @@ +/* + * Toxic -- Tox Curses Client + */ + +#include + typedef struct ToxWindow_ ToxWindow; struct ToxWindow_ { @@ -11,6 +17,7 @@ struct ToxWindow_ { char title[256]; void* x; + bool blink; WINDOW* window; }; -- cgit v1.2.3 From 0d5eef4b43f6d936d3e34a2329b7d819e870953c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 31 Jul 2013 15:22:43 -0400 Subject: Backspace key should now work on more terminals in toxic. --- testing/toxic/chat.c | 2 +- testing/toxic/prompt.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'testing/toxic/prompt.c') diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index da9ad025..dceb1d7b 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -95,7 +95,7 @@ static void chat_onKey(ToxWindow* self, int key) { ctx->line[0] = '\0'; ctx->pos = 0; } - else if(key == 0x107) { + else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(ctx->pos != 0) { ctx->line[--ctx->pos] = '\0'; } diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 62470a23..f1b6e689 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -256,7 +256,7 @@ static void prompt_onKey(ToxWindow* self, int key) { } // BACKSPACE key: Remove one character from line. - else if(key == 0x107) { + else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(prompt_buf_pos != 0) { prompt_buf[--prompt_buf_pos] = 0; -- cgit v1.2.3