summaryrefslogtreecommitdiff
path: root/testing/toxic
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic')
-rw-r--r--testing/toxic/CMakeLists.txt15
-rw-r--r--testing/toxic/chat.c155
-rw-r--r--testing/toxic/friendlist.c166
-rw-r--r--testing/toxic/main.c103
-rw-r--r--testing/toxic/prompt.c118
-rw-r--r--testing/toxic/windows.h13
6 files changed, 518 insertions, 52 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt
new file mode 100644
index 00000000..f30d8e9e
--- /dev/null
+++ b/testing/toxic/CMakeLists.txt
@@ -0,0 +1,15 @@
1cmake_minimum_required(VERSION 2.6.0)
2project(toxic C)
3
4set(exe_name toxic)
5
6add_executable(${exe_name}
7 main.c
8 prompt.c
9 friendlist.c
10 chat.c)
11
12target_link_libraries(${exe_name}
13 curses)
14
15linkCoreLibraries(${exe_name})
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
new file mode 100644
index 00000000..dceb1d7b
--- /dev/null
+++ b/testing/toxic/chat.c
@@ -0,0 +1,155 @@
1/*
2 * Toxic -- Tox Curses Client
3 */
4
5#include <curses.h>
6#include <stdlib.h>
7#include <string.h>
8#include <stdint.h>
9#include <ctype.h>
10
11#include "../../core/Messenger.h"
12#include "../../core/network.h"
13
14#include "windows.h"
15
16typedef struct {
17 int friendnum;
18
19 char line[256];
20 size_t pos;
21
22 WINDOW* history;
23 WINDOW* linewin;
24
25} ChatContext;
26
27extern void fix_name(uint8_t* name);
28
29
30static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
31 ChatContext* ctx = (ChatContext*) self->x;
32 uint8_t nick[MAX_NAME_LENGTH] = {0};
33
34 if(ctx->friendnum != num)
35 return;
36
37 getname(num, (uint8_t*) &nick);
38
39 msg[len-1] = '\0';
40 nick[MAX_NAME_LENGTH-1] = '\0';
41
42 fix_name(msg);
43 fix_name(nick);
44
45 wattron(ctx->history, COLOR_PAIR(4));
46 wprintw(ctx->history, "%s: ", nick);
47 wattroff(ctx->history, COLOR_PAIR(4));
48
49 wprintw(ctx->history, "%s\n", msg);
50
51 self->blink = true;
52}
53
54static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
55 ChatContext* ctx = (ChatContext*) self->x;
56
57 if(ctx->friendnum != num)
58 return;
59
60 nick[len-1] = '\0';
61 fix_name(nick);
62
63 wattron(ctx->history, COLOR_PAIR(3));
64 wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick);
65 wattroff(ctx->history, COLOR_PAIR(3));
66}
67
68static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {
69
70}
71
72static void chat_onKey(ToxWindow* self, int key) {
73 ChatContext* ctx = (ChatContext*) self->x;
74
75 if(isprint(key)) {
76
77 if(ctx->pos != sizeof(ctx->line)-1) {
78 ctx->line[ctx->pos++] = key;
79 ctx->line[ctx->pos] = '\0';
80 }
81 }
82 else if(key == '\n') {
83 wattron(ctx->history, COLOR_PAIR(1));
84 wprintw(ctx->history, "you: ", ctx->line);
85 wattroff(ctx->history, COLOR_PAIR(1));
86
87 wprintw(ctx->history, "%s\n", ctx->line);
88
89 if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
90 wattron(ctx->history, COLOR_PAIR(3));
91 wprintw(ctx->history, " * Failed to send message.\n");
92 wattroff(ctx->history, COLOR_PAIR(3));
93 }
94
95 ctx->line[0] = '\0';
96 ctx->pos = 0;
97 }
98 else if(key == 0x107 || key == 0x8 || key == 0x7f) {
99 if(ctx->pos != 0) {
100 ctx->line[--ctx->pos] = '\0';
101 }
102 }
103
104}
105
106static void chat_onDraw(ToxWindow* self) {
107 int x, y;
108 ChatContext* ctx = (ChatContext*) self->x;
109
110 getmaxyx(self->window, y, x);
111
112 (void) x;
113 if(y < 3)
114 return;
115
116 wclear(ctx->linewin);
117 mvwhline(ctx->linewin, 0, 0, '_', COLS);
118 mvwprintw(ctx->linewin, 1, 0, "%s\n", ctx->line);
119
120 wrefresh(self->window);
121}
122
123static void chat_onInit(ToxWindow* self) {
124 int x, y;
125 ChatContext* ctx = (ChatContext*) self->x;
126
127 getmaxyx(self->window, y, x);
128
129 ctx->history = subwin(self->window, y - 4, x, 0, 0);
130 scrollok(ctx->history, 1);
131
132 ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
133}
134
135ToxWindow new_chat(int friendnum) {
136 ToxWindow ret;
137
138 memset(&ret, 0, sizeof(ret));
139
140 ret.onKey = &chat_onKey;
141 ret.onDraw = &chat_onDraw;
142 ret.onInit = &chat_onInit;
143 ret.onMessage = &chat_onMessage;
144 ret.onNickChange = &chat_onNickChange;
145 ret.onStatusChange = &chat_onStatusChange;
146
147 snprintf(ret.title, sizeof(ret.title), "[chat %d]", friendnum);
148
149 ChatContext* x = calloc(1, sizeof(ChatContext));
150 x->friendnum = friendnum;
151
152 ret.x = (void*) x;
153
154 return ret;
155}
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
new file mode 100644
index 00000000..f9a413f9
--- /dev/null
+++ b/testing/toxic/friendlist.c
@@ -0,0 +1,166 @@
1/*
2 * Toxic -- Tox Curses Client
3 */
4
5#include <curses.h>
6#include <string.h>
7#include <stdint.h>
8#include <ctype.h>
9
10#include "../../core/Messenger.h"
11#include "../../core/network.h"
12
13#include "windows.h"
14
15extern int add_window(ToxWindow w);
16extern int focus_window(int num);
17extern ToxWindow new_chat(int friendnum);
18
19#define MAX_FRIENDS_NUM 100
20
21typedef struct {
22 uint8_t name[MAX_NAME_LENGTH];
23 uint8_t status[MAX_USERSTATUS_LENGTH];
24 int num;
25 int chatwin;
26} friend_t;
27
28static friend_t friends[MAX_FRIENDS_NUM];
29static int num_friends = 0;
30static int num_selected = 0;
31
32
33void fix_name(uint8_t* name) {
34
35 // Remove all non alphanumeric characters.
36 uint8_t* p = name;
37 uint8_t* q = name;
38
39 while(*p != 0) {
40 if(isprint(*p)) {
41 *q++ = *p;
42 }
43
44 p++;
45 }
46
47 *q = 0;
48}
49
50void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
51
52 if(num >= num_friends)
53 return;
54
55 if(friends[num].chatwin == -1) {
56 friends[num].chatwin = add_window(new_chat(num));
57 }
58}
59
60void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
61
62 if(len >= MAX_NAME_LENGTH || num >= num_friends)
63 return;
64
65 memcpy((char*) &friends[num].name, (char*) str, len);
66 friends[num].name[len] = 0;
67 fix_name(friends[num].name);
68}
69
70void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
71
72 if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
73 return;
74
75 memcpy((char*) &friends[num].status, (char*) str, len);
76 friends[num].status[len] = 0;
77 fix_name(friends[num].status);
78}
79
80int friendlist_onFriendAdded(int num) {
81
82 if(num_friends == MAX_FRIENDS_NUM)
83 return -1;
84
85 friends[num_friends].num = num;
86 getname(num, friends[num_friends].name);
87 strcpy((char*) friends[num_friends].name, "unknown");
88 strcpy((char*) friends[num_friends].status, "unknown");
89 friends[num_friends].chatwin = -1;
90
91 num_friends++;
92 return 0;
93}
94
95static void friendlist_onKey(ToxWindow* self, int key) {
96
97 if(key == KEY_UP) {
98 if(num_selected != 0)
99 num_selected--;
100 }
101 else if(key == KEY_DOWN) {
102 if(num_friends != 0)
103 num_selected = (num_selected+1) % num_friends;
104 }
105 else if(key == '\n') {
106
107 if(friends[num_selected].chatwin != -1)
108 return;
109
110 friends[num_selected].chatwin = add_window(new_chat(num_selected));
111 focus_window(friends[num_selected].chatwin);
112 }
113}
114
115static void friendlist_onDraw(ToxWindow* self) {
116 size_t i;
117
118 wclear(self->window);
119
120 if(num_friends == 0) {
121 wprintw(self->window, "Empty. Add some friends! :-)\n");
122 }
123 else {
124 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
125 wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
126 wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
127 }
128
129 wprintw(self->window, "\n");
130
131 for(i=0; i<num_friends; i++) {
132
133 if(i == num_selected) wattron(self->window, COLOR_PAIR(3));
134 wprintw(self->window, " [#%d] ", friends[i].num);
135 if(i == num_selected) wattroff(self->window, COLOR_PAIR(3));
136
137 attron(A_BOLD);
138 wprintw(self->window, "%s ", friends[i].name);
139 attroff(A_BOLD);
140
141 wprintw(self->window, "(%s)\n", friends[i].status);
142 }
143
144 wrefresh(self->window);
145}
146
147static void friendlist_onInit(ToxWindow* self) {
148
149}
150
151
152ToxWindow new_friendlist() {
153 ToxWindow ret;
154
155 memset(&ret, 0, sizeof(ret));
156
157 ret.onKey = &friendlist_onKey;
158 ret.onDraw = &friendlist_onDraw;
159 ret.onInit = &friendlist_onInit;
160 ret.onMessage = &friendlist_onMessage;
161 ret.onNickChange = &friendlist_onNickChange;
162 ret.onStatusChange = &friendlist_onStatusChange;
163 strcpy(ret.title, "[friends]");
164
165 return ret;
166}
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 0aad6777..fffc3f84 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -14,6 +14,10 @@
14#include "windows.h" 14#include "windows.h"
15 15
16extern ToxWindow new_prompt(); 16extern ToxWindow new_prompt();
17extern ToxWindow new_friendlist();
18
19extern int friendlist_onFriendAdded(int num);
20
17extern int add_req(uint8_t* public_key); // XXX 21extern int add_req(uint8_t* public_key); // XXX
18 22
19#define TOXWINDOWS_MAX_NUM 32 23#define TOXWINDOWS_MAX_NUM 32
@@ -25,21 +29,59 @@ static ToxWindow* prompt;
25 29
26// CALLBACKS START 30// CALLBACKS START
27void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { 31void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) {
32 size_t i;
28 int n = add_req(public_key); 33 int n = add_req(public_key);
29 34
30 wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); 35 wprintw(prompt->window, "\nFriend request from:\n");
36
37 for(i=0; i<32; i++) {
38 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
39 }
40 wprintw(prompt->window, "\n");
41
42 wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n);
43
44 for(i=0; i<w_num; i++) {
45 if(windows[i].onFriendRequest != NULL)
46 windows[i].onFriendRequest(&windows[i], public_key, data, length);
47 }
31} 48}
32 49
33void on_message(int friendnumber, uint8_t* string, uint16_t length) { 50void on_message(int friendnumber, uint8_t* string, uint16_t length) {
51 size_t i;
52
34 wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); 53 wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string);
54
55 for(i=0; i<w_num; i++) {
56 if(windows[i].onMessage != NULL)
57 windows[i].onMessage(&windows[i], friendnumber, string, length);
58 }
35} 59}
36 60
37void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { 61void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
38 wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); 62 size_t i;
63
64 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
65
66 for(i=0; i<w_num; i++) {
67 if(windows[i].onNickChange != NULL)
68 windows[i].onNickChange(&windows[i], friendnumber, string, length);
69 }
39} 70}
40 71
41void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { 72void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
42 wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); 73 size_t i;
74
75 wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
76
77 for(i=0; i<w_num; i++) {
78 if(windows[i].onStatusChange != NULL)
79 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
80 }
81}
82
83void on_friendadded(int friendnumber) {
84 friendlist_onFriendAdded(friendnumber);
43} 85}
44// CALLBACKS END 86// CALLBACKS END
45 87
@@ -73,7 +115,7 @@ static void init_tox() {
73 m_callback_userstatus(on_statuschange); 115 m_callback_userstatus(on_statuschange);
74} 116}
75 117
76static int add_window(ToxWindow w) { 118int add_window(ToxWindow w) {
77 if(w_num == TOXWINDOWS_MAX_NUM) 119 if(w_num == TOXWINDOWS_MAX_NUM)
78 return -1; 120 return -1;
79 121
@@ -88,14 +130,22 @@ static int add_window(ToxWindow w) {
88 windows[w_num++] = w; 130 windows[w_num++] = w;
89 w.onInit(&w); 131 w.onInit(&w);
90 132
91 return w_num; 133 return w_num - 1;
134}
135
136int focus_window(int num) {
137 if(num >= w_num || num < 0)
138 return -1;
139
140 w_active = num;
141 return 0;
92} 142}
93 143
94static void init_windows() { 144static void init_windows() {
95 w_num = 0; 145 w_num = 0;
96 w_active = 0; 146 w_active = 0;
97 147
98 if(add_window(new_prompt()) == -1) { 148 if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) {
99 fprintf(stderr, "add_window() failed.\n"); 149 fprintf(stderr, "add_window() failed.\n");
100 150
101 endwin(); 151 endwin();
@@ -134,14 +184,18 @@ static void load_data() {
134 184
135 if(buf == NULL) { 185 if(buf == NULL) {
136 fprintf(stderr, "malloc() failed.\n"); 186 fprintf(stderr, "malloc() failed.\n");
187
137 fclose(fd); 188 fclose(fd);
189 endwin();
138 exit(1); 190 exit(1);
139 } 191 }
140 192
141 if(fread(buf, len, 1, fd) != 1){ 193 if(fread(buf, len, 1, fd) != 1){
142 fprintf(stderr, "fread() failed.\n"); 194 fprintf(stderr, "fread() failed.\n");
195
143 free(buf); 196 free(buf);
144 fclose(fd); 197 fclose(fd);
198 endwin();
145 exit(1); 199 exit(1);
146 } 200 }
147 201
@@ -153,6 +207,7 @@ static void load_data() {
153 207
154 if(buf == NULL) { 208 if(buf == NULL) {
155 fprintf(stderr, "malloc() failed.\n"); 209 fprintf(stderr, "malloc() failed.\n");
210 endwin();
156 exit(1); 211 exit(1);
157 } 212 }
158 213
@@ -161,14 +216,18 @@ static void load_data() {
161 fd = fopen("data", "w"); 216 fd = fopen("data", "w");
162 if(fd == NULL) { 217 if(fd == NULL) {
163 fprintf(stderr, "fopen() failed.\n"); 218 fprintf(stderr, "fopen() failed.\n");
219
164 free(buf); 220 free(buf);
221 endwin();
165 exit(1); 222 exit(1);
166 } 223 }
167 224
168 if(fwrite(buf, len, 1, fd) != 1){ 225 if(fwrite(buf, len, 1, fd) != 1){
169 fprintf(stderr, "fwrite() failed.\n"); 226 fprintf(stderr, "fwrite() failed.\n");
227
170 free(buf); 228 free(buf);
171 fclose(fd); 229 fclose(fd);
230 endwin();
172 exit(1); 231 exit(1);
173 } 232 }
174 } 233 }
@@ -178,6 +237,7 @@ static void load_data() {
178} 237}
179 238
180static void draw_bar() { 239static void draw_bar() {
240 static int odd = 0;
181 size_t i; 241 size_t i;
182 242
183 attron(COLOR_PAIR(4)); 243 attron(COLOR_PAIR(4));
@@ -186,12 +246,26 @@ static void draw_bar() {
186 246
187 move(LINES - 1, 0); 247 move(LINES - 1, 0);
188 248
249 attron(COLOR_PAIR(4) | A_BOLD);
250 printw(" TOXIC 1.0 |");
251 attroff(COLOR_PAIR(4) | A_BOLD);
252
189 for(i=0; i<w_num; i++) { 253 for(i=0; i<w_num; i++) {
190 if(i == w_active) { 254 if(i == w_active) {
191 attron(A_BOLD); 255 attron(A_BOLD);
192 } 256 }
193 257
194 printw(" %s ", windows[i].title); 258 odd = (odd+1) % 2;
259
260 if(windows[i].blink && odd) {
261 attron(COLOR_PAIR(3));
262 }
263
264 printw(" %s", windows[i].title);
265
266 if(windows[i].blink && odd) {
267 attron(COLOR_PAIR(3));
268 }
195 269
196 if(i == w_active) { 270 if(i == w_active) {
197 attroff(A_BOLD); 271 attroff(A_BOLD);
@@ -201,6 +275,11 @@ static void draw_bar() {
201 refresh(); 275 refresh();
202} 276}
203 277
278void prepare_window(WINDOW* w) {
279 mvwin(w, 0, 0);
280 wresize(w, LINES-2, COLS);
281}
282
204int main(int argc, char* argv[]) { 283int main(int argc, char* argv[]) {
205 int ch; 284 int ch;
206 ToxWindow* a; 285 ToxWindow* a;
@@ -211,14 +290,22 @@ int main(int argc, char* argv[]) {
211 init_windows(); 290 init_windows();
212 291
213 while(true) { 292 while(true) {
293 // Update tox.
214 do_tox(); 294 do_tox();
215 295
296 // Draw.
216 a = &windows[w_active]; 297 a = &windows[w_active];
298 prepare_window(a->window);
299 a->blink = false;
217 a->onDraw(a); 300 a->onDraw(a);
218 draw_bar(); 301 draw_bar();
219 302
303 // Handle input.
220 ch = getch(); 304 ch = getch();
221 if(ch != ERR) { 305 if(ch == '\t') {
306 w_active = (w_active + 1) % w_num;
307 }
308 else if(ch != ERR) {
222 a->onKey(a, ch); 309 a->onKey(a, ch);
223 } 310 }
224 311
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 4a59cc7b..f1b6e689 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -15,6 +15,8 @@
15uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX 15uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX
16uint8_t num_requests=0; // XXX 16uint8_t num_requests=0; // XXX
17 17
18extern void on_friendadded(int friendnumber);
19
18// XXX: 20// XXX:
19int add_req(uint8_t* public_key) { 21int add_req(uint8_t* public_key) {
20 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); 22 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
@@ -40,8 +42,7 @@ static int prompt_buf_pos=0;
40 42
41static void execute(ToxWindow* self, char* cmd) { 43static void execute(ToxWindow* self, char* cmd) {
42 44
43 // quit/exit: Exit program. 45 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
44 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
45 endwin(); 46 endwin();
46 exit(0); 47 exit(0);
47 } 48 }
@@ -53,33 +54,32 @@ static void execute(ToxWindow* self, char* cmd) {
53 54
54 ip = strchr(cmd, ' '); 55 ip = strchr(cmd, ' ');
55 if(ip == NULL) { 56 if(ip == NULL) {
57 wprintw(self->window, "Invalid syntax.\n");
56 return; 58 return;
57 } 59 }
58
59 ip++; 60 ip++;
60 61
61 port = strchr(ip, ' '); 62 port = strchr(ip, ' ');
62 if(port == NULL) { 63 if(port == NULL) {
64 wprintw(self->window, "Invalid syntax.\n");
63 return; 65 return;
64 } 66 }
65
66 port[0] = 0; 67 port[0] = 0;
67 port++; 68 port++;
68 69
69 key = strchr(port, ' '); 70 key = strchr(port, ' ');
70 if(key == NULL) { 71 if(key == NULL) {
72 wprintw(self->window, "Invalid syntax.\n");
71 return; 73 return;
72 } 74 }
73
74 key[0] = 0; 75 key[0] = 0;
75 key++; 76 key++;
76 77
77 if(atoi(port) == 0) { 78 if(atoi(port) == 0) {
79 wprintw(self->window, "Invalid syntax.\n");
78 return; 80 return;
79 } 81 }
80 82
81 wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);
82
83 dht.port = htons(atoi(port)); 83 dht.port = htons(atoi(port));
84 84
85 int resolved_address = resolve_addr(ip); 85 int resolved_address = resolve_addr(ip);
@@ -91,38 +91,62 @@ static void execute(ToxWindow* self, char* cmd) {
91 DHT_bootstrap(dht, hex_string_to_bin(key)); 91 DHT_bootstrap(dht, hex_string_to_bin(key));
92 } 92 }
93 else if(!strncmp(cmd, "add ", strlen("add "))) { 93 else if(!strncmp(cmd, "add ", strlen("add "))) {
94 uint8_t id_bin[32];
95 size_t i;
96 char xx[3];
97 uint32_t x;
98
94 char* id; 99 char* id;
95 char* msg; 100 char* msg;
96 int num; 101 int num;
97 102
98 id = strchr(cmd, ' '); 103 id = strchr(cmd, ' ');
99
100 if(id == NULL) { 104 if(id == NULL) {
105 wprintw(self->window, "Invalid syntax.\n");
101 return; 106 return;
102 } 107 }
103
104 id++; 108 id++;
105 109
106 msg = strchr(id, ' '); 110 msg = strchr(id, ' ');
107 if(msg == NULL) { 111 if(msg != NULL) {
112 msg[0] = 0;
113 msg++;
114 }
115 else msg = "";
116
117 if(strlen(id) != 2*32) {
118 wprintw(self->window, "Invalid ID length.\n");
108 return; 119 return;
109 } 120 }
110 121
111 msg[0] = 0; 122 for(i=0; i<32; i++) {
112 msg++; 123 xx[0] = id[2*i];
124 xx[1] = id[2*i+1];
125 xx[2] = '\0';
126
127 if(sscanf(xx, "%02x", &x) != 1) {
128 wprintw(self->window, "Invalid ID.\n");
129 return;
130 }
131
132 id_bin[i] = x;
133 }
134
135 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
113 136
114 num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
115 wprintw(self->window, "Friend added as %d.\n", num); 137 wprintw(self->window, "Friend added as %d.\n", num);
138 on_friendadded(num);
116 } 139 }
117 else if(!strncmp(cmd, "status ", strlen("status "))) { 140 else if(!strncmp(cmd, "status ", strlen("status "))) {
118 char* msg; 141 char* msg;
119 142
120 msg = strchr(cmd, ' '); 143 msg = strchr(cmd, ' ');
121 if(msg == NULL) { 144 if(msg == NULL) {
145 wprintw(self->window, "Invalid syntax.\n");
122 return; 146 return;
123 } 147 }
124
125 msg++; 148 msg++;
149
126 m_set_userstatus((uint8_t*) msg, strlen(msg)+1); 150 m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
127 wprintw(self->window, "Status set to: %s.\n", msg); 151 wprintw(self->window, "Status set to: %s.\n", msg);
128 } 152 }
@@ -133,33 +157,22 @@ static void execute(ToxWindow* self, char* cmd) {
133 if(nick == NULL) { 157 if(nick == NULL) {
134 return; 158 return;
135 } 159 }
136
137 nick++; 160 nick++;
161
138 setname((uint8_t*) nick, strlen(nick)+1); 162 setname((uint8_t*) nick, strlen(nick)+1);
139 wprintw(self->window, "Nickname set to: %s.\n", nick); 163 wprintw(self->window, "Nickname set to: %s.\n", nick);
140 } 164 }
141 else if(!strcmp(cmd, "myid")) { 165 else if(!strcmp(cmd, "myid")) {
142 // XXX: Clean this up 166 char id[32*2 + 1] = {0};
143 char idstring0[200]; 167 size_t i;
144 char idstring1[32][5]; 168
145 char idstring2[32][5]; 169 for(i=0; i<32; i++) {
146 uint32_t i; 170 char xx[3];
147 171 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
148 for(i = 0; i < 32; i++) { 172 strcat(id, xx);
149 if(self_public_key[i] < 16)
150 strcpy(idstring1[i], "0");
151 else
152 strcpy(idstring1[i], "");
153
154 sprintf(idstring2[i], "%hhX", self_public_key[i]);
155 } 173 }
156 174
157 for (i=0; i<32; i++) { 175 wprintw(self->window, "%s\n", id);
158 strcat(idstring0, idstring1[i]);
159 strcat(idstring0, idstring2[i]);
160 }
161
162 wprintw(self->window, "%s\n", idstring0);
163 } 176 }
164 else if(!strncmp(cmd, "accept ", strlen("accept "))) { 177 else if(!strncmp(cmd, "accept ", strlen("accept "))) {
165 char* id; 178 char* id;
@@ -167,17 +180,26 @@ static void execute(ToxWindow* self, char* cmd) {
167 180
168 id = strchr(cmd, ' '); 181 id = strchr(cmd, ' ');
169 if(id == NULL) { 182 if(id == NULL) {
183 wprintw(self->window, "Invalid syntax.\n");
170 return; 184 return;
171 } 185 }
172 id++; 186 id++;
173 187
174 num = atoi(id); 188 num = atoi(id);
175 if(num >= num_requests) { 189 if(num >= num_requests) {
190 wprintw(self->window, "Invalid syntax.\n");
176 return; 191 return;
177 } 192 }
178 193
179 num = m_addfriend_norequest(pending_requests[num]); 194 num = m_addfriend_norequest(pending_requests[num]);
180 wprintw(self->window, "Friend accepted as: %d.\n", num); 195
196 if(num == -1) {
197 wprintw(self->window, "Failed to add friend.\n");
198 }
199 else {
200 wprintw(self->window, "Friend accepted as: %d.\n", num);
201 on_friendadded(num);
202 }
181 } 203 }
182 else if(!strncmp(cmd, "msg ", strlen("msg "))) { 204 else if(!strncmp(cmd, "msg ", strlen("msg "))) {
183 char* id; 205 char* id;
@@ -186,26 +208,29 @@ static void execute(ToxWindow* self, char* cmd) {
186 id = strchr(cmd, ' '); 208 id = strchr(cmd, ' ');
187 209
188 if(id == NULL) { 210 if(id == NULL) {
211 wprintw(self->window, "Invalid syntax.\n");
189 return; 212 return;
190 } 213 }
191
192 id++; 214 id++;
193 215
194 msg = strchr(id, ' '); 216 msg = strchr(id, ' ');
195 if(msg == NULL) { 217 if(msg == NULL) {
218 wprintw(self->window, "Invalid syntax.\n");
196 return; 219 return;
197 } 220 }
198
199 msg[0] = 0; 221 msg[0] = 0;
200 msg++; 222 msg++;
201 223
202 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { 224 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
203 wprintw(self->window, "Error occurred while sending message.\n"); 225 wprintw(self->window, "Error occurred while sending message.\n");
204 } 226 }
205 else { 227 else {
206 wprintw(self->window, "Message successfully sent.\n"); 228 wprintw(self->window, "Message successfully sent.\n");
207 } 229 }
208 } 230 }
231 else {
232 wprintw(self->window, "Invalid syntax.\n");
233 }
209} 234}
210 235
211static void prompt_onKey(ToxWindow* self, int key) { 236static void prompt_onKey(ToxWindow* self, int key) {
@@ -231,7 +256,7 @@ static void prompt_onKey(ToxWindow* self, int key) {
231 } 256 }
232 257
233 // BACKSPACE key: Remove one character from line. 258 // BACKSPACE key: Remove one character from line.
234 else if(key == 0x107) { 259 else if(key == 0x107 || key == 0x8 || key == 0x7f) {
235 260
236 if(prompt_buf_pos != 0) { 261 if(prompt_buf_pos != 0) {
237 prompt_buf[--prompt_buf_pos] = 0; 262 prompt_buf[--prompt_buf_pos] = 0;
@@ -242,9 +267,6 @@ static void prompt_onKey(ToxWindow* self, int key) {
242static void prompt_onDraw(ToxWindow* self) { 267static void prompt_onDraw(ToxWindow* self) {
243 int x, y; 268 int x, y;
244 269
245 mvwin(self->window,0,0);
246 wresize(self->window, LINES-2, COLS);
247
248 getyx(self->window, y, x); 270 getyx(self->window, y, x);
249 (void) x; 271 (void) x;
250 272
@@ -260,7 +282,7 @@ static void prompt_onDraw(ToxWindow* self) {
260 282
261static void print_usage(ToxWindow* self) { 283static void print_usage(ToxWindow* self) {
262 wattron(self->window, COLOR_PAIR(2) | A_BOLD); 284 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
263 wprintw(self->window, "Usage:\n"); 285 wprintw(self->window, "Commands:\n");
264 wattroff(self->window, A_BOLD); 286 wattroff(self->window, A_BOLD);
265 287
266 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); 288 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
@@ -270,6 +292,12 @@ static void print_usage(ToxWindow* self) {
270 wprintw(self->window, " accept <number> : Accept friend request\n"); 292 wprintw(self->window, " accept <number> : Accept friend request\n");
271 wprintw(self->window, " myid : Print your ID\n"); 293 wprintw(self->window, " myid : Print your ID\n");
272 wprintw(self->window, " quit/exit : Exit program\n"); 294 wprintw(self->window, " quit/exit : Exit program\n");
295
296
297 wattron(self->window, A_BOLD);
298 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
299 wattroff(self->window, A_BOLD);
300
273 wattroff(self->window, COLOR_PAIR(2)); 301 wattroff(self->window, COLOR_PAIR(2));
274} 302}
275 303
@@ -283,6 +311,8 @@ static void prompt_onInit(ToxWindow* self) {
283ToxWindow new_prompt() { 311ToxWindow new_prompt() {
284 ToxWindow ret; 312 ToxWindow ret;
285 313
314 memset(&ret, 0, sizeof(ret));
315
286 ret.onKey = &prompt_onKey; 316 ret.onKey = &prompt_onKey;
287 ret.onDraw = &prompt_onDraw; 317 ret.onDraw = &prompt_onDraw;
288 ret.onInit = &prompt_onInit; 318 ret.onInit = &prompt_onInit;
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h
index 2e082ca9..dde1430f 100644
--- a/testing/toxic/windows.h
+++ b/testing/toxic/windows.h
@@ -1,10 +1,23 @@
1/*
2 * Toxic -- Tox Curses Client
3 */
4
5#include <stdbool.h>
6
1typedef struct ToxWindow_ ToxWindow; 7typedef struct ToxWindow_ ToxWindow;
2 8
3struct ToxWindow_ { 9struct ToxWindow_ {
4 void(*onKey)(ToxWindow*, int); 10 void(*onKey)(ToxWindow*, int);
5 void(*onDraw)(ToxWindow*); 11 void(*onDraw)(ToxWindow*);
6 void(*onInit)(ToxWindow*); 12 void(*onInit)(ToxWindow*);
13 void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t);
14 void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t);
15 void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
16 void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t);
7 char title[256]; 17 char title[256];
8 18
19 void* x;
20 bool blink;
21
9 WINDOW* window; 22 WINDOW* window;
10}; 23};