summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
Diffstat (limited to 'testing')
-rw-r--r--testing/Messenger_test.c81
-rw-r--r--testing/nTox.c73
-rw-r--r--testing/nTox.h6
-rw-r--r--testing/toxic/chat.c36
-rw-r--r--testing/toxic/friendlist.c16
-rw-r--r--testing/toxic/main.c66
-rw-r--r--testing/toxic/prompt.c78
-rw-r--r--testing/toxic/windows.h10
8 files changed, 194 insertions, 172 deletions
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index a11d374b..fa5d6890 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -1,21 +1,21 @@
1/* Messenger test 1/* Messenger test
2 * 2 *
3 * This program adds a friend and accepts all friend requests with the proper message. 3 * This program adds a friend and accepts all friend requests with the proper message.
4 * 4 *
5 * It tries sending a message to the added friend. 5 * It tries sending a message to the added friend.
6 * 6 *
7 * If it receives a message from a friend it replies back. 7 * If it receives a message from a friend it replies back.
8 * 8 *
9 * 9 *
10 * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c 10 * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c
11 * 11 *
12 * 12 *
13 * Command line arguments are the ip, port and public_key of a node (for bootstrapping). 13 * Command line arguments are the ip, port and public_key of a node (for bootstrapping).
14 * 14 *
15 * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C 15 * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
16 * 16 *
17 * Or the argument can be the path to the save file. 17 * Or the argument can be the path to the save file.
18 * 18 *
19 * EX: ./test Save.bak 19 * EX: ./test Save.bak
20 * 20 *
21 * Copyright (C) 2013 Tox project All Rights Reserved. 21 * Copyright (C) 2013 Tox project All Rights Reserved.
@@ -34,7 +34,7 @@
34 * 34 *
35 * You should have received a copy of the GNU General Public License 35 * You should have received a copy of the GNU General Public License
36 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 36 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
37 * 37 *
38 */ 38 */
39 39
40#include "../core/Messenger.h" 40#include "../core/Messenger.h"
@@ -51,7 +51,11 @@
51 51
52#endif 52#endif
53 53
54void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) 54/* FIXME needed as print_request has to match the interface expected by
55 * networking_requesthandler and so cannot take a Messenger * */
56static Messenger *m;
57
58void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void* userdata)
55{ 59{
56 printf("Friend request received from: \n"); 60 printf("Friend request received from: \n");
57 printf("ClientID: "); 61 printf("ClientID: ");
@@ -63,7 +67,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
63 printf("%hhX", public_key[j]); 67 printf("%hhX", public_key[j]);
64 } 68 }
65 printf("\nOf length: %u with data: %s \n", length, data); 69 printf("\nOf length: %u with data: %s \n", length, data);
66 70
67 if(length != sizeof("Install Gentoo")) 71 if(length != sizeof("Install Gentoo"))
68 { 72 {
69 return; 73 return;
@@ -72,14 +76,14 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
72 //if the request contained the message of peace the person is obviously a friend so we add him. 76 //if the request contained the message of peace the person is obviously a friend so we add him.
73 { 77 {
74 printf("Friend request accepted.\n"); 78 printf("Friend request accepted.\n");
75 m_addfriend_norequest(public_key); 79 m_addfriend_norequest(m, public_key);
76 } 80 }
77} 81}
78 82
79void print_message(int friendnumber, uint8_t * string, uint16_t length) 83void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
80{ 84{
81 printf("Message with length %u received from %u: %s \n", length, friendnumber, string); 85 printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
82 m_sendmessage(friendnumber, (uint8_t*)"Test1", 6); 86 m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6);
83} 87}
84 88
85int main(int argc, char *argv[]) 89int main(int argc, char *argv[])
@@ -88,7 +92,13 @@ int main(int argc, char *argv[])
88 printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); 92 printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
89 exit(0); 93 exit(0);
90 } 94 }
91 initMessenger(); 95
96 m = initMessenger();
97 if( !m ){
98 fputs("Failed to allocate messenger datastructure\n", stderr);
99 exit(0);
100 }
101
92 if(argc > 3) { 102 if(argc > 3) {
93 IP_Port bootstrap_ip_port; 103 IP_Port bootstrap_ip_port;
94 bootstrap_ip_port.port = htons(atoi(argv[2])); 104 bootstrap_ip_port.port = htons(atoi(argv[2]));
@@ -100,13 +110,13 @@ int main(int argc, char *argv[])
100 int read; 110 int read;
101 uint8_t buffer[128000]; 111 uint8_t buffer[128000];
102 read = fread(buffer, 1, 128000, file); 112 read = fread(buffer, 1, 128000, file);
103 printf("Messenger loaded: %i\n", Messenger_load(buffer, read)); 113 printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read));
104 fclose(file); 114 fclose(file);
105 115
106 } 116 }
107 m_callback_friendrequest(print_request); 117 m_callback_friendrequest(m, print_request, NULL);
108 m_callback_friendmessage(print_message); 118 m_callback_friendmessage(m, print_message, NULL);
109 119
110 printf("OUR ID: "); 120 printf("OUR ID: ");
111 uint32_t i; 121 uint32_t i;
112 for(i = 0; i < 32; i++) { 122 for(i = 0; i < 32; i++) {
@@ -114,33 +124,34 @@ int main(int argc, char *argv[])
114 printf("0"); 124 printf("0");
115 printf("%hhX",self_public_key[i]); 125 printf("%hhX",self_public_key[i]);
116 } 126 }
117 127
118 setname((uint8_t *)"Anon", 5); 128 setname(m, (uint8_t *)"Anon", 5);
119 129
120 char temp_id[128]; 130 char temp_id[128];
121 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); 131 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
122 if(scanf("%s", temp_id) != 1) { 132 if(scanf("%s", temp_id) != 1) {
123 return 1; 133 return 1;
124 } 134 }
125 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 135 int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
126 136
127 perror("Initialization"); 137 perror("Initialization");
128 138
129 while(1) { 139 while(1) {
130 uint8_t name[128]; 140 uint8_t name[128];
131 getname(num, name); 141 getname(m, num, name);
132 printf("%s\n", name); 142 printf("%s\n", name);
133 143
134 m_sendmessage(num, (uint8_t*)"Test", 5); 144 m_sendmessage(m, num, (uint8_t*)"Test", 5);
135 doMessenger(); 145 doMessenger(m);
136 c_sleep(30); 146 c_sleep(30);
137 FILE *file = fopen("Save.bak", "wb"); 147 FILE *file = fopen("Save.bak", "wb");
138 if ( file==NULL ){return 1;} 148 if ( file==NULL ){return 1;}
139 uint8_t * buffer = malloc(Messenger_size()); 149 uint8_t * buffer = malloc(Messenger_size(m));
140 Messenger_save(buffer); 150 Messenger_save(m, buffer);
141 size_t write_result = fwrite(buffer, 1, Messenger_size(), file); 151 size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
142 if (write_result < Messenger_size()) {return 1;} 152 if (write_result < Messenger_size(m)) {return 1;}
143 free(buffer); 153 free(buffer);
144 fclose(file); 154 fclose(file);
145 } 155 }
156 cleanupMessenger(m);
146} 157}
diff --git a/testing/nTox.c b/testing/nTox.c
index ee4d7de4..59d1cbf6 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -85,12 +85,12 @@ void new_lines(char *line)
85} 85}
86 86
87 87
88void print_friendlist() 88void print_friendlist(Messenger *m)
89{ 89{
90 char name[MAX_NAME_LENGTH]; 90 char name[MAX_NAME_LENGTH];
91 int i = 0; 91 int i = 0;
92 new_lines("[i] Friend List:"); 92 new_lines("[i] Friend List:");
93 while(getname(i, (uint8_t *)name) != -1) { 93 while(getname(m, i, (uint8_t *)name) != -1) {
94 /* account for the longest name and the longest "base" string */ 94 /* account for the longest name and the longest "base" string */
95 char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; 95 char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
96 96
@@ -107,13 +107,13 @@ void print_friendlist()
107 new_lines("\tno friends! D:"); 107 new_lines("\tno friends! D:");
108} 108}
109 109
110char *format_message(char *message, int friendnum) 110char *format_message(Messenger *m, char *message, int friendnum)
111{ 111{
112 char name[MAX_NAME_LENGTH]; 112 char name[MAX_NAME_LENGTH];
113 if (friendnum != -1) { 113 if (friendnum != -1) {
114 getname(friendnum, (uint8_t*)name); 114 getname(m, friendnum, (uint8_t*)name);
115 } else { 115 } else {
116 getself_name((uint8_t*)name); 116 getself_name(m, (uint8_t*)name);
117 } 117 }
118 char *msg = malloc(100+strlen(message)+strlen(name)+1); 118 char *msg = malloc(100+strlen(message)+strlen(name)+1);
119 119
@@ -133,7 +133,7 @@ char *format_message(char *message, int friendnum)
133 return msg; 133 return msg;
134} 134}
135 135
136void line_eval(char *line) 136void line_eval(Messenger *m, char *line)
137{ 137{
138 if (line[0] == '/') { 138 if (line[0] == '/') {
139 char inpt_command = line[1]; 139 char inpt_command = line[1];
@@ -148,7 +148,7 @@ void line_eval(char *line)
148 temp_id[i] = line[i+prompt_offset]; 148 temp_id[i] = line[i+prompt_offset];
149 149
150 unsigned char *bin_string = hex_string_to_bin(temp_id); 150 unsigned char *bin_string = hex_string_to_bin(temp_id);
151 int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 151 int num = m_addfriend(m, bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
152 free(bin_string); 152 free(bin_string);
153 char numstring[100]; 153 char numstring[100];
154 switch (num) { 154 switch (num) {
@@ -175,7 +175,7 @@ void line_eval(char *line)
175 do_refresh(); 175 do_refresh();
176 } 176 }
177 else if (inpt_command == 'd') { 177 else if (inpt_command == 'd') {
178 doMessenger(); 178 doMessenger(m);
179 } 179 }
180 else if (inpt_command == 'm') { //message command: /m friendnumber messsage 180 else if (inpt_command == 'm') { //message command: /m friendnumber messsage
181 size_t len = strlen(line); 181 size_t len = strlen(line);
@@ -196,10 +196,10 @@ void line_eval(char *line)
196 } 196 }
197 } 197 }
198 int num = atoi(numstring); 198 int num = atoi(numstring);
199 if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) { 199 if (m_sendmessage(m, num, (uint8_t*) message, strlen(message) + 1) != 1) {
200 new_lines("[i] could not send message"); 200 new_lines("[i] could not send message");
201 } else { 201 } else {
202 new_lines(format_message(message, -1)); 202 new_lines(format_message(m, message, -1));
203 } 203 }
204 } 204 }
205 else if (inpt_command == 'n') { 205 else if (inpt_command == 'n') {
@@ -211,13 +211,13 @@ void line_eval(char *line)
211 name[i-3] = line[i]; 211 name[i-3] = line[i];
212 } 212 }
213 name[i-3] = 0; 213 name[i-3] = 0;
214 setname(name, i - 2); 214 setname(m, name, i - 2);
215 char numstring[100]; 215 char numstring[100];
216 sprintf(numstring, "[i] changed nick to %s", (char*)name); 216 sprintf(numstring, "[i] changed nick to %s", (char*)name);
217 new_lines(numstring); 217 new_lines(numstring);
218 } 218 }
219 else if (inpt_command == 'l') { 219 else if (inpt_command == 'l') {
220 print_friendlist(); 220 print_friendlist(m);
221 } 221 }
222 else if (inpt_command == 's') { 222 else if (inpt_command == 's') {
223 uint8_t status[MAX_STATUSMESSAGE_LENGTH]; 223 uint8_t status[MAX_STATUSMESSAGE_LENGTH];
@@ -228,7 +228,7 @@ void line_eval(char *line)
228 status[i-3] = line[i]; 228 status[i-3] = line[i];
229 } 229 }
230 status[i-3] = 0; 230 status[i-3] = 0;
231 m_set_statusmessage(status, strlen((char*)status) + 1); 231 m_set_statusmessage(m, status, strlen((char*)status) + 1);
232 char numstring[100]; 232 char numstring[100];
233 sprintf(numstring, "[i] changed status to %s", (char*)status); 233 sprintf(numstring, "[i] changed status to %s", (char*)status);
234 new_lines(numstring); 234 new_lines(numstring);
@@ -240,7 +240,7 @@ void line_eval(char *line)
240 sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); 240 sprintf(numchar,"[i] you either didn't receive that request or you already accepted it");
241 new_lines(numchar); 241 new_lines(numchar);
242 } else { 242 } else {
243 int num = m_addfriend_norequest(pending_requests[numf].id); 243 int num = m_addfriend_norequest(m, pending_requests[numf].id);
244 if (num != -1) { 244 if (num != -1) {
245 pending_requests[numf].accepted = 1; 245 pending_requests[numf].accepted = 1;
246 sprintf(numchar, "[i] friend request %u accepted", numf); 246 sprintf(numchar, "[i] friend request %u accepted", numf);
@@ -336,7 +336,7 @@ void do_refresh()
336 refresh(); 336 refresh();
337} 337}
338 338
339void print_request(uint8_t *public_key, uint8_t *data, uint16_t length) 339void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
340{ 340{
341 new_lines("[i] received friend request with message:"); 341 new_lines("[i] received friend request with message:");
342 new_lines((char *)data); 342 new_lines((char *)data);
@@ -349,32 +349,32 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
349 do_refresh(); 349 do_refresh();
350} 350}
351 351
352void print_message(int friendnumber, uint8_t * string, uint16_t length) 352void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
353{ 353{
354 new_lines(format_message((char*)string, friendnumber)); 354 new_lines(format_message(m, (char*)string, friendnumber));
355} 355}
356 356
357void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) 357void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
358{ 358{
359 char name[MAX_NAME_LENGTH]; 359 char name[MAX_NAME_LENGTH];
360 if(getname(friendnumber, (uint8_t*)name) != -1) { 360 if(getname(m, friendnumber, (uint8_t*)name) != -1) {
361 char msg[100+length]; 361 char msg[100+length];
362 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); 362 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
363 new_lines(msg); 363 new_lines(msg);
364 } 364 }
365} 365}
366 366
367void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) 367void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
368{ 368{
369 char name[MAX_NAME_LENGTH]; 369 char name[MAX_NAME_LENGTH];
370 if(getname(friendnumber, (uint8_t*)name) != -1) { 370 if(getname(m, friendnumber, (uint8_t*)name) != -1) {
371 char msg[100+length+strlen(name)+1]; 371 char msg[100+length+strlen(name)+1];
372 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); 372 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
373 new_lines(msg); 373 new_lines(msg);
374 } 374 }
375} 375}
376 376
377void load_key(char *path) 377void load_key(Messenger *m, char *path)
378{ 378{
379 FILE *data_file = fopen(path, "r"); 379 FILE *data_file = fopen(path, "r");
380 int size = 0; 380 int size = 0;
@@ -390,13 +390,13 @@ void load_key(char *path)
390 fputs("[!] could not read data file! exiting...\n", stderr); 390 fputs("[!] could not read data file! exiting...\n", stderr);
391 goto FILE_ERROR; 391 goto FILE_ERROR;
392 } 392 }
393 Messenger_load(data, size); 393 Messenger_load(m, data, size);
394 394
395 } else { 395 } else {
396 //else save new keys 396 //else save new keys
397 int size = Messenger_size(); 397 int size = Messenger_size(m);
398 uint8_t data[size]; 398 uint8_t data[size];
399 Messenger_save(data); 399 Messenger_save(m, data);
400 data_file = fopen(path, "w"); 400 data_file = fopen(path, "w");
401 401
402 if(!data_file) { 402 if(!data_file) {
@@ -435,6 +435,7 @@ int main(int argc, char *argv[])
435 int i = 0; 435 int i = 0;
436 char *filename = "data"; 436 char *filename = "data";
437 char idstring[200] = {0}; 437 char idstring[200] = {0};
438 Messenger *m;
438 439
439 if (argc < 4) { 440 if (argc < 4) {
440 printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); 441 printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
@@ -458,13 +459,18 @@ int main(int argc, char *argv[])
458 } 459 }
459 } 460 }
460 461
461 initMessenger(); 462 m = initMessenger();
462 load_key(filename); 463 if( !m ){
464 fputs("Failed to allocate Messenger datastructure", stderr);
465 exit(0);
466 }
467
468 load_key(m, filename);
463 469
464 m_callback_friendrequest(print_request); 470 m_callback_friendrequest(m, print_request, NULL);
465 m_callback_friendmessage(print_message); 471 m_callback_friendmessage(m, print_message, NULL);
466 m_callback_namechange(print_nickchange); 472 m_callback_namechange(m, print_nickchange, NULL);
467 m_callback_statusmessage(print_statuschange); 473 m_callback_statusmessage(m, print_statuschange, NULL);
468 474
469 initscr(); 475 initscr();
470 noecho(); 476 noecho();
@@ -494,7 +500,7 @@ int main(int argc, char *argv[])
494 on = 1; 500 on = 1;
495 } 501 }
496 502
497 doMessenger(); 503 doMessenger(m);
498 c_sleep(1); 504 c_sleep(1);
499 do_refresh(); 505 do_refresh();
500 506
@@ -504,7 +510,7 @@ int main(int argc, char *argv[])
504 510
505 getmaxyx(stdscr, y, x); 511 getmaxyx(stdscr, y, x);
506 if (c == '\n') { 512 if (c == '\n') {
507 line_eval(line); 513 line_eval(m, line);
508 strcpy(line, ""); 514 strcpy(line, "");
509 } else if (c == 8 || c == 127) { 515 } else if (c == 8 || c == 127) {
510 line[strlen(line)-1] = '\0'; 516 line[strlen(line)-1] = '\0';
@@ -512,6 +518,7 @@ int main(int argc, char *argv[])
512 strcpy(line, appender(line, (char) c)); 518 strcpy(line, appender(line, (char) c));
513 } 519 }
514 } 520 }
521 cleanupMessenger(m);
515 endwin(); 522 endwin();
516 return 0; 523 return 0;
517} 524}
diff --git a/testing/nTox.h b/testing/nTox.h
index 47c73513..fdd88fb4 100644
--- a/testing/nTox.h
+++ b/testing/nTox.h
@@ -1,5 +1,5 @@
1/* nTox.h 1/* nTox.h
2 * 2 *
3 *Textual frontend for Tox. 3 *Textual frontend for Tox.
4 * 4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 5 * Copyright (C) 2013 Tox project All Rights Reserved.
@@ -18,7 +18,7 @@
18 * 18 *
19 * You should have received a copy of the GNU General Public License 19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23 23
24#ifndef NTOX_H 24#ifndef NTOX_H
@@ -43,7 +43,7 @@
43#define PUB_KEY_BYTES 32 43#define PUB_KEY_BYTES 32
44 44
45void new_lines(char *line); 45void new_lines(char *line);
46void line_eval(char *line); 46void line_eval(Messenger *m, char *line);
47void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; 47void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
48int count_lines(char *string) ; 48int count_lines(char *string) ;
49char *appender(char *str, const char c); 49char *appender(char *str, const char c);
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index e1897230..112b20b7 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -29,9 +29,9 @@ extern int active_window;
29extern void del_window(ToxWindow *w, int f_num); 29extern void del_window(ToxWindow *w, int f_num);
30extern void fix_name(uint8_t *name); 30extern void fix_name(uint8_t *name);
31void print_help(ChatContext *self); 31void print_help(ChatContext *self);
32void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo); 32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo);
33 33
34static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) 34static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
35{ 35{
36 ChatContext *ctx = (ChatContext*) self->x; 36 ChatContext *ctx = (ChatContext*) self->x;
37 uint8_t nick[MAX_NAME_LENGTH] = {0}; 37 uint8_t nick[MAX_NAME_LENGTH] = {0};
@@ -43,7 +43,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
43 if (ctx->friendnum != num) 43 if (ctx->friendnum != num)
44 return; 44 return;
45 45
46 getname(num, (uint8_t*) &nick); 46 getname(m, num, (uint8_t*) &nick);
47 msg[len-1] = '\0'; 47 msg[len-1] = '\0';
48 nick[MAX_NAME_LENGTH-1] = '\0'; 48 nick[MAX_NAME_LENGTH-1] = '\0';
49 fix_name(msg); 49 fix_name(msg);
@@ -61,7 +61,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
61 beep(); 61 beep();
62} 62}
63 63
64static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len) 64static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
65{ 65{
66 ChatContext *ctx = (ChatContext*) self->x; 66 ChatContext *ctx = (ChatContext*) self->x;
67 time_t now; 67 time_t now;
@@ -117,7 +117,7 @@ int string_is_empty(char *string)
117 return rc; 117 return rc;
118} 118}
119 119
120static void chat_onKey(ToxWindow *self, int key) 120static void chat_onKey(ToxWindow *self, Messenger *m, int key)
121{ 121{
122 ChatContext *ctx = (ChatContext*) self->x; 122 ChatContext *ctx = (ChatContext*) self->x;
123 time_t now; 123 time_t now;
@@ -155,7 +155,7 @@ static void chat_onKey(ToxWindow *self, int key)
155 wmove(self->window, y2-CURS_Y_OFFSET, 0); 155 wmove(self->window, y2-CURS_Y_OFFSET, 0);
156 wclrtobot(self->window); 156 wclrtobot(self->window);
157 if (ctx->line[0] == '/') 157 if (ctx->line[0] == '/')
158 execute(self, ctx, ctx->line, timeinfo); 158 execute(self, ctx, m, ctx->line, timeinfo);
159 else { 159 else {
160 if (!string_is_empty(ctx->line)) { 160 if (!string_is_empty(ctx->line)) {
161 /* make sure the string has at least non-space character */ 161 /* make sure the string has at least non-space character */
@@ -167,7 +167,7 @@ static void chat_onKey(ToxWindow *self, int key)
167 wattroff(ctx->history, COLOR_PAIR(1)); 167 wattroff(ctx->history, COLOR_PAIR(1));
168 wprintw(ctx->history, "%s\n", ctx->line); 168 wprintw(ctx->history, "%s\n", ctx->line);
169 } 169 }
170 if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) { 170 if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
171 wattron(ctx->history, COLOR_PAIR(3)); 171 wattron(ctx->history, COLOR_PAIR(3));
172 wprintw(ctx->history, " * Failed to send message.\n"); 172 wprintw(ctx->history, " * Failed to send message.\n");
173 wattroff(ctx->history, COLOR_PAIR(3)); 173 wattroff(ctx->history, COLOR_PAIR(3));
@@ -178,7 +178,7 @@ static void chat_onKey(ToxWindow *self, int key)
178 } 178 }
179} 179}
180 180
181void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) 181void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo)
182{ 182{
183 if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { 183 if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
184 wclear(self->window); 184 wclear(self->window);
@@ -210,14 +210,14 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
210 wattroff(ctx->history, COLOR_PAIR(2)); 210 wattroff(ctx->history, COLOR_PAIR(2));
211 211
212 uint8_t selfname[MAX_NAME_LENGTH]; 212 uint8_t selfname[MAX_NAME_LENGTH];
213 int len = getself_name(selfname); 213 int len = getself_name(m, selfname);
214 char msg[MAX_STR_SIZE-len-4]; 214 char msg[MAX_STR_SIZE-len-4];
215 snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); 215 snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
216 216
217 wattron(ctx->history, COLOR_PAIR(1)); 217 wattron(ctx->history, COLOR_PAIR(1));
218 wprintw(ctx->history, msg); 218 wprintw(ctx->history, msg);
219 wattroff(ctx->history, COLOR_PAIR(1)); 219 wattroff(ctx->history, COLOR_PAIR(1));
220 if (m_sendaction(ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { 220 if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) {
221 wattron(ctx->history, COLOR_PAIR(3)); 221 wattron(ctx->history, COLOR_PAIR(3));
222 wprintw(ctx->history, " * Failed to send action\n"); 222 wprintw(ctx->history, " * Failed to send action\n");
223 wattroff(ctx->history, COLOR_PAIR(3)); 223 wattroff(ctx->history, COLOR_PAIR(3));
@@ -256,13 +256,13 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
256 256
257 msg = strchr(status, ' '); 257 msg = strchr(status, ' ');
258 if (msg == NULL) { 258 if (msg == NULL) {
259 m_set_userstatus(status_kind); 259 m_set_userstatus(m, status_kind);
260 wprintw(ctx->history, "Status set to: %s\n", status_text); 260 wprintw(ctx->history, "Status set to: %s\n", status_text);
261 } 261 }
262 else { 262 else {
263 msg++; 263 msg++;
264 m_set_userstatus(status_kind); 264 m_set_userstatus(m, status_kind);
265 m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); 265 m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1);
266 wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); 266 wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
267 } 267 }
268 } 268 }
@@ -275,7 +275,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
275 return; 275 return;
276 } 276 }
277 nick++; 277 nick++;
278 setname((uint8_t*) nick, strlen(nick)+1); 278 setname(m, (uint8_t*) nick, strlen(nick)+1);
279 wprintw(ctx->history, "Nickname set to: %s\n", nick); 279 wprintw(ctx->history, "Nickname set to: %s\n", nick);
280 } 280 }
281 281
@@ -312,7 +312,7 @@ static void chat_onDraw(ToxWindow *self)
312 wrefresh(self->window); 312 wrefresh(self->window);
313} 313}
314 314
315static void chat_onInit(ToxWindow *self) 315static void chat_onInit(ToxWindow *self, Messenger *m)
316{ 316{
317 int x, y; 317 int x, y;
318 ChatContext *ctx = (ChatContext*) self->x; 318 ChatContext *ctx = (ChatContext*) self->x;
@@ -329,7 +329,7 @@ void print_help(ChatContext *self)
329 wattron(self->history, COLOR_PAIR(2) | A_BOLD); 329 wattron(self->history, COLOR_PAIR(2) | A_BOLD);
330 wprintw(self->history, "Commands:\n"); 330 wprintw(self->history, "Commands:\n");
331 wattroff(self->history, A_BOLD); 331 wattroff(self->history, A_BOLD);
332 332
333 wprintw(self->history, " /status <type> <message> : Set your status\n"); 333 wprintw(self->history, " /status <type> <message> : Set your status\n");
334 wprintw(self->history, " /nick <nickname> : Set your nickname\n"); 334 wprintw(self->history, " /nick <nickname> : Set your nickname\n");
335 wprintw(self->history, " /me <action> : Do an action\n"); 335 wprintw(self->history, " /me <action> : Do an action\n");
@@ -342,7 +342,7 @@ void print_help(ChatContext *self)
342 wattroff(self->history, COLOR_PAIR(2)); 342 wattroff(self->history, COLOR_PAIR(2));
343} 343}
344 344
345ToxWindow new_chat(int friendnum) 345ToxWindow new_chat(Messenger *m, int friendnum)
346{ 346{
347 ToxWindow ret; 347 ToxWindow ret;
348 memset(&ret, 0, sizeof(ret)); 348 memset(&ret, 0, sizeof(ret));
@@ -356,7 +356,7 @@ ToxWindow new_chat(int friendnum)
356 ret.onAction = &chat_onAction; 356 ret.onAction = &chat_onAction;
357 357
358 uint8_t nick[MAX_NAME_LENGTH] = {0}; 358 uint8_t nick[MAX_NAME_LENGTH] = {0};
359 getname(friendnum, (uint8_t*) &nick); 359 getname(m, friendnum, (uint8_t*) &nick);
360 fix_name(nick); 360 fix_name(nick);
361 361
362 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); 362 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index f2aa1cf4..56061cf9 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -14,7 +14,7 @@
14 14
15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; 15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
16extern int add_window(ToxWindow w, int n); 16extern int add_window(ToxWindow w, int n);
17extern ToxWindow new_chat(int friendnum); 17extern ToxWindow new_chat(Messenger *m, int friendnum);
18 18
19extern int active_window; 19extern int active_window;
20 20
@@ -42,7 +42,7 @@ void fix_name(uint8_t *name)
42 *q = 0; 42 *q = 0;
43} 43}
44 44
45void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len) 45void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
46{ 46{
47 if (num >= num_friends) 47 if (num >= num_friends)
48 return; 48 return;
@@ -54,7 +54,7 @@ void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len)
54 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { 54 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
55 if (WINDOW_STATUS[i] == -1) { 55 if (WINDOW_STATUS[i] == -1) {
56 WINDOW_STATUS[i] = num; 56 WINDOW_STATUS[i] = num;
57 add_window(new_chat(num), i); 57 add_window(new_chat(m, num), i);
58 active_window = i; 58 active_window = i;
59 break; 59 break;
60 } 60 }
@@ -82,20 +82,20 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t
82 fix_name(friends[num].status); 82 fix_name(friends[num].status);
83} 83}
84 84
85int friendlist_onFriendAdded(int num) 85int friendlist_onFriendAdded(Messenger *m, int num)
86{ 86{
87 if (num_friends == MAX_FRIENDS_NUM) 87 if (num_friends == MAX_FRIENDS_NUM)
88 return -1; 88 return -1;
89 89
90 friends[num_friends].num = num; 90 friends[num_friends].num = num;
91 getname(num, friends[num_friends].name); 91 getname(m, num, friends[num_friends].name);
92 strcpy((char*) friends[num_friends].name, "unknown"); 92 strcpy((char*) friends[num_friends].name, "unknown");
93 strcpy((char*) friends[num_friends].status, "unknown"); 93 strcpy((char*) friends[num_friends].status, "unknown");
94 friends[num_friends++].chatwin = -1; 94 friends[num_friends++].chatwin = -1;
95 return 0; 95 return 0;
96} 96}
97 97
98static void friendlist_onKey(ToxWindow *self, int key) 98static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
99{ 99{
100 if (key == KEY_UP) { 100 if (key == KEY_UP) {
101 if (--num_selected < 0) 101 if (--num_selected < 0)
@@ -121,7 +121,7 @@ static void friendlist_onKey(ToxWindow *self, int key)
121 if (WINDOW_STATUS[i] == -1) { 121 if (WINDOW_STATUS[i] == -1) {
122 WINDOW_STATUS[i] = num_selected; 122 WINDOW_STATUS[i] = num_selected;
123 friends[num_selected].chatwin = num_selected; 123 friends[num_selected].chatwin = num_selected;
124 add_window(new_chat(num_selected), i); 124 add_window(new_chat(m, num_selected), i);
125 active_window = i; 125 active_window = i;
126 break; 126 break;
127 } 127 }
@@ -164,7 +164,7 @@ void disable_chatwin(int f_num)
164 friends[f_num].chatwin = -1; 164 friends[f_num].chatwin = -1;
165} 165}
166 166
167static void friendlist_onInit(ToxWindow *self) 167static void friendlist_onInit(ToxWindow *self, Messenger *m)
168{ 168{
169 169
170} 170}
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 752453f2..397f9391 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -25,7 +25,7 @@
25extern ToxWindow new_prompt(); 25extern ToxWindow new_prompt();
26extern ToxWindow new_friendlist(); 26extern ToxWindow new_friendlist();
27 27
28extern int friendlist_onFriendAdded(int num); 28extern int friendlist_onFriendAdded(Messenger *m, int num);
29extern void disable_chatwin(int f_num); 29extern void disable_chatwin(int f_num);
30extern int add_req(uint8_t *public_key); // XXX 30extern int add_req(uint8_t *public_key); // XXX
31extern unsigned char *hex_string_to_bin(char hex_string[]); 31extern unsigned char *hex_string_to_bin(char hex_string[]);
@@ -40,11 +40,13 @@ char WINDOW_STATUS[MAX_WINDOW_SLOTS];
40static ToxWindow windows[MAX_WINDOW_SLOTS]; 40static ToxWindow windows[MAX_WINDOW_SLOTS];
41static ToxWindow* prompt; 41static ToxWindow* prompt;
42 42
43static Messenger *m;
44
43int w_num; 45int w_num;
44int active_window; 46int active_window;
45 47
46/* CALLBACKS START */ 48/* CALLBACKS START */
47void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) 49void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
48{ 50{
49 int n = add_req(public_key); 51 int n = add_req(public_key);
50 wprintw(prompt->window, "\nFriend request from:\n"); 52 wprintw(prompt->window, "\nFriend request from:\n");
@@ -63,25 +65,25 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
63 } 65 }
64} 66}
65 67
66void on_message(int friendnumber, uint8_t *string, uint16_t length) 68void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
67{ 69{
68 int i; 70 int i;
69 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { 71 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
70 if (windows[i].onMessage != NULL) 72 if (windows[i].onMessage != NULL)
71 windows[i].onMessage(&windows[i], friendnumber, string, length); 73 windows[i].onMessage(&windows[i], m, friendnumber, string, length);
72 } 74 }
73} 75}
74 76
75void on_action(int friendnumber, uint8_t *string, uint16_t length) 77void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
76{ 78{
77 int i; 79 int i;
78 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { 80 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
79 if (windows[i].onAction != NULL) 81 if (windows[i].onAction != NULL)
80 windows[i].onAction(&windows[i], friendnumber, string, length); 82 windows[i].onAction(&windows[i], m, friendnumber, string, length);
81 } 83 }
82} 84}
83 85
84void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) 86void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
85{ 87{
86 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); 88 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
87 int i; 89 int i;
@@ -91,7 +93,7 @@ void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
91 } 93 }
92} 94}
93 95
94void on_statuschange(int friendnumber, uint8_t *string, uint16_t length) 96void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
95{ 97{
96 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); 98 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
97 int i; 99 int i;
@@ -103,7 +105,7 @@ void on_statuschange(int friendnumber, uint8_t *string, uint16_t length)
103 105
104void on_friendadded(int friendnumber) 106void on_friendadded(int friendnumber)
105{ 107{
106 friendlist_onFriendAdded(friendnumber); 108 friendlist_onFriendAdded(m, friendnumber);
107} 109}
108/* CALLBACKS END */ 110/* CALLBACKS END */
109 111
@@ -129,14 +131,14 @@ static void init_term()
129static void init_tox() 131static void init_tox()
130{ 132{
131 /* Init core */ 133 /* Init core */
132 initMessenger(); 134 m = initMessenger();
133 135
134 /* Callbacks */ 136 /* Callbacks */
135 m_callback_friendrequest(on_request); 137 m_callback_friendrequest(m, on_request, NULL);
136 m_callback_friendmessage(on_message); 138 m_callback_friendmessage(m, on_message, NULL);
137 m_callback_namechange(on_nickchange); 139 m_callback_namechange(m, on_nickchange, NULL);
138 m_callback_statusmessage(on_statuschange); 140 m_callback_statusmessage(m, on_statuschange, NULL);
139 m_callback_action(on_action); 141 m_callback_action(m, on_action, NULL);
140} 142}
141 143
142#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ 144#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@@ -157,8 +159,7 @@ int init_connection(void)
157 char line[MAXLINE]; 159 char line[MAXLINE];
158 int linecnt = 0; 160 int linecnt = 0;
159 while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { 161 while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
160 int len = strlen(line); 162 if (strlen(line) > MINLINE)
161 if (len > MINLINE && len < MAXLINE)
162 strcpy(servers[linecnt++], line); 163 strcpy(servers[linecnt++], line);
163 } 164 }
164 if (linecnt < 1) { 165 if (linecnt < 1) {
@@ -211,7 +212,7 @@ int add_window(ToxWindow w, int n)
211 return -1; 212 return -1;
212 213
213 windows[n] = w; 214 windows[n] = w;
214 w.onInit(&w); 215 w.onInit(&w, m);
215 w_num++; 216 w_num++;
216 return n; 217 return n;
217} 218}
@@ -237,7 +238,7 @@ static void init_windows()
237 w_num = 0; 238 w_num = 0;
238 int n_prompt = 0; 239 int n_prompt = 0;
239 int n_friendslist = 1; 240 int n_friendslist = 1;
240 if (add_window(new_prompt(), n_prompt) == -1 241 if (add_window(new_prompt(), n_prompt) == -1
241 || add_window(new_friendlist(), n_friendslist) == -1) { 242 || add_window(new_friendlist(), n_friendslist) == -1) {
242 fprintf(stderr, "add_window() failed.\n"); 243 fprintf(stderr, "add_window() failed.\n");
243 endwin(); 244 endwin();
@@ -248,16 +249,22 @@ static void init_windows()
248 249
249static void do_tox() 250static void do_tox()
250{ 251{
252 static int conn_try = 0;
251 static bool dht_on = false; 253 static bool dht_on = false;
252 if (!dht_on && DHT_isconnected()) { 254 if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
255 init_connection();
256 wprintw(prompt->window, "\nEstablishing connection...\n");
257 }
258 else if (!dht_on && DHT_isconnected()) {
253 dht_on = true; 259 dht_on = true;
254 wprintw(prompt->window, "\nDHT connected.\n"); 260 wprintw(prompt->window, "\nDHT connected.\n");
255 } 261 }
256 else if (dht_on && !DHT_isconnected()) { 262 else if (dht_on && !DHT_isconnected()) {
257 dht_on = false; 263 dht_on = false;
258 wprintw(prompt->window, "\nDHT disconnected.\n"); 264 wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
265 init_connection();
259 } 266 }
260 doMessenger(); 267 doMessenger(m);
261} 268}
262 269
263static void load_data(char *path) 270static void load_data(char *path)
@@ -285,17 +292,17 @@ static void load_data(char *path)
285 endwin(); 292 endwin();
286 exit(1); 293 exit(1);
287 } 294 }
288 Messenger_load(buf, len); 295 Messenger_load(m, buf, len);
289 } 296 }
290 else { 297 else {
291 len = Messenger_size(); 298 len = Messenger_size(m);
292 buf = malloc(len); 299 buf = malloc(len);
293 if (buf == NULL) { 300 if (buf == NULL) {
294 fprintf(stderr, "malloc() failed.\n"); 301 fprintf(stderr, "malloc() failed.\n");
295 endwin(); 302 endwin();
296 exit(1); 303 exit(1);
297 } 304 }
298 Messenger_save(buf); 305 Messenger_save(m, buf);
299 306
300 fd = fopen(path, "w"); 307 fd = fopen(path, "w");
301 if (fd == NULL) { 308 if (fd == NULL) {
@@ -329,7 +336,7 @@ static void draw_bar()
329 move(LINES - 1, 0); 336 move(LINES - 1, 0);
330 337
331 attron(COLOR_PAIR(4) | A_BOLD); 338 attron(COLOR_PAIR(4) | A_BOLD);
332 printw(" TOXIC " TOXICVER "|"); 339 printw(" TOXIC " TOXICVER "|");
333 attroff(COLOR_PAIR(4) | A_BOLD); 340 attroff(COLOR_PAIR(4) | A_BOLD);
334 341
335 int i; 342 int i;
@@ -440,10 +447,6 @@ int main(int argc, char *argv[])
440 load_data(DATA_FILE); 447 load_data(DATA_FILE);
441 free(DATA_FILE); 448 free(DATA_FILE);
442 449
443 int connected = init_connection();
444 if (connected != 0)
445 wprintw(prompt->window, "Auto-connect failed (error code %d)\n", connected);
446
447 if (f_flag == -1) { 450 if (f_flag == -1) {
448 attron(COLOR_PAIR(3) | A_BOLD); 451 attron(COLOR_PAIR(3) | A_BOLD);
449 wprintw(prompt->window, "You passed '-f' without giving an argument.\n" 452 wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
@@ -473,7 +476,8 @@ int main(int argc, char *argv[])
473 if (ch == '\t' || ch == KEY_BTAB) 476 if (ch == '\t' || ch == KEY_BTAB)
474 set_active_window(ch); 477 set_active_window(ch);
475 else if (ch != ERR) 478 else if (ch != ERR)
476 a->onKey(a, ch); 479 a->onKey(a, m, ch);
477 } 480 }
481 cleanupMessenger(m);
478 return 0; 482 return 0;
479} 483}
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index ab44e960..67f80fef 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -20,24 +20,24 @@ static char prompt_buf[MAX_STR_SIZE] = {0};
20static int prompt_buf_pos = 0; 20static int prompt_buf_pos = 0;
21 21
22/* commands */ 22/* commands */
23void cmd_accept(ToxWindow *, char **); 23void cmd_accept(ToxWindow *, Messenger *m, char **);
24void cmd_add(ToxWindow *, char **); 24void cmd_add(ToxWindow *, Messenger *m, char **);
25void cmd_clear(ToxWindow *, char **); 25void cmd_clear(ToxWindow *, Messenger *m, char **);
26void cmd_connect(ToxWindow *, char **); 26void cmd_connect(ToxWindow *, Messenger *m, char **);
27void cmd_help(ToxWindow *, char **); 27void cmd_help(ToxWindow *, Messenger *m, char **);
28void cmd_msg(ToxWindow *, char **); 28void cmd_msg(ToxWindow *, Messenger *m, char **);
29void cmd_myid(ToxWindow *, char **); 29void cmd_myid(ToxWindow *, Messenger *m, char **);
30void cmd_nick(ToxWindow *, char **); 30void cmd_nick(ToxWindow *, Messenger *m, char **);
31void cmd_quit(ToxWindow *, char **); 31void cmd_quit(ToxWindow *, Messenger *m, char **);
32void cmd_status(ToxWindow *, char **); 32void cmd_status(ToxWindow *, Messenger *m, char **);
33void cmd_statusmsg(ToxWindow *, char **); 33void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
34 34
35#define NUM_COMMANDS 13 35#define NUM_COMMANDS 13
36 36
37static struct { 37static struct {
38 char *name; 38 char *name;
39 int numargs; 39 int numargs;
40 void (*func)(ToxWindow *, char **); 40 void (*func)(ToxWindow *, Messenger *m, char **);
41} commands[] = { 41} commands[] = {
42 { "accept", 1, cmd_accept }, 42 { "accept", 1, cmd_accept },
43 { "add", 1, cmd_add }, 43 { "add", 1, cmd_add },
@@ -74,7 +74,7 @@ unsigned char *hex_string_to_bin(char hex_string[])
74 return val; 74 return val;
75} 75}
76 76
77void cmd_accept(ToxWindow *self, char **args) 77void cmd_accept(ToxWindow *self, Messenger *m, char **args)
78{ 78{
79 int num = atoi(args[1]); 79 int num = atoi(args[1]);
80 if (num >= num_requests) { 80 if (num >= num_requests) {
@@ -82,7 +82,7 @@ void cmd_accept(ToxWindow *self, char **args)
82 return; 82 return;
83 } 83 }
84 84
85 num = m_addfriend_norequest(pending_requests[num]); 85 num = m_addfriend_norequest(m, pending_requests[num]);
86 if (num == -1) 86 if (num == -1)
87 wprintw(self->window, "Failed to add friend.\n"); 87 wprintw(self->window, "Failed to add friend.\n");
88 else { 88 else {
@@ -91,7 +91,7 @@ void cmd_accept(ToxWindow *self, char **args)
91 } 91 }
92} 92}
93 93
94void cmd_add(ToxWindow *self, char **args) 94void cmd_add(ToxWindow *self, Messenger *m, char **args)
95{ 95{
96 uint8_t id_bin[KEY_SIZE_BYTES]; 96 uint8_t id_bin[KEY_SIZE_BYTES];
97 char xx[3]; 97 char xx[3];
@@ -121,7 +121,7 @@ void cmd_add(ToxWindow *self, char **args)
121 } 121 }
122 id_bin[i] = x; 122 id_bin[i] = x;
123 } 123 }
124 int num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); 124 int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1);
125 switch (num) { 125 switch (num) {
126 case FAERR_TOOLONG: 126 case FAERR_TOOLONG:
127 wprintw(self->window, "Message is too long.\n"); 127 wprintw(self->window, "Message is too long.\n");
@@ -145,12 +145,12 @@ void cmd_add(ToxWindow *self, char **args)
145 } 145 }
146} 146}
147 147
148void cmd_clear(ToxWindow *self, char **args) 148void cmd_clear(ToxWindow *self, Messenger *m, char **args)
149{ 149{
150 wclear(self->window); 150 wclear(self->window);
151} 151}
152 152
153void cmd_connect(ToxWindow *self, char **args) 153void cmd_connect(ToxWindow *self, Messenger *m, char **args)
154{ 154{
155 IP_Port dht; 155 IP_Port dht;
156 char *ip = args[1]; 156 char *ip = args[1];
@@ -174,13 +174,13 @@ void cmd_connect(ToxWindow *self, char **args)
174 free(binary_string); 174 free(binary_string);
175} 175}
176 176
177void cmd_quit(ToxWindow *self, char **args) 177void cmd_quit(ToxWindow *self, Messenger *m, char **args)
178{ 178{
179 endwin(); 179 endwin();
180 exit(0); 180 exit(0);
181} 181}
182 182
183void cmd_help(ToxWindow *self, char **args) 183void cmd_help(ToxWindow *self, Messenger *m, char **args)
184{ 184{
185 wclear(self->window); 185 wclear(self->window);
186 wattron(self->window, COLOR_PAIR(2) | A_BOLD); 186 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
@@ -197,7 +197,7 @@ void cmd_help(ToxWindow *self, char **args)
197 wprintw(self->window, " quit/exit : Exit program\n"); 197 wprintw(self->window, " quit/exit : Exit program\n");
198 wprintw(self->window, " help : Print this message again\n"); 198 wprintw(self->window, " help : Print this message again\n");
199 wprintw(self->window, " clear : Clear this window\n"); 199 wprintw(self->window, " clear : Clear this window\n");
200 200
201 wattron(self->window, A_BOLD); 201 wattron(self->window, A_BOLD);
202 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); 202 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
203 wattroff(self->window, A_BOLD); 203 wattroff(self->window, A_BOLD);
@@ -205,17 +205,17 @@ void cmd_help(ToxWindow *self, char **args)
205 wattroff(self->window, COLOR_PAIR(2)); 205 wattroff(self->window, COLOR_PAIR(2));
206} 206}
207 207
208void cmd_msg(ToxWindow *self, char **args) 208void cmd_msg(ToxWindow *self, Messenger *m, char **args)
209{ 209{
210 char *id = args[1]; 210 char *id = args[1];
211 char *msg = args[2]; 211 char *msg = args[2];
212 if (m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0) 212 if (m_sendmessage(m, atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0)
213 wprintw(self->window, "Error occurred while sending message.\n"); 213 wprintw(self->window, "Error occurred while sending message.\n");
214 else 214 else
215 wprintw(self->window, "Message successfully sent.\n"); 215 wprintw(self->window, "Message successfully sent.\n");
216} 216}
217 217
218void cmd_myid(ToxWindow *self, char **args) 218void cmd_myid(ToxWindow *self, Messenger *m, char **args)
219{ 219{
220 char id[KEY_SIZE_BYTES*2 + 1] = {0}; 220 char id[KEY_SIZE_BYTES*2 + 1] = {0};
221 size_t i; 221 size_t i;
@@ -227,14 +227,14 @@ void cmd_myid(ToxWindow *self, char **args)
227 wprintw(self->window, "Your ID: %s\n", id); 227 wprintw(self->window, "Your ID: %s\n", id);
228} 228}
229 229
230void cmd_nick(ToxWindow *self, char **args) 230void cmd_nick(ToxWindow *self, Messenger *m, char **args)
231{ 231{
232 char *nick = args[1]; 232 char *nick = args[1];
233 setname((uint8_t*) nick, strlen(nick)+1); 233 setname(m, (uint8_t*) nick, strlen(nick)+1);
234 wprintw(self->window, "Nickname set to: %s\n", nick); 234 wprintw(self->window, "Nickname set to: %s\n", nick);
235} 235}
236 236
237void cmd_status(ToxWindow *self, char **args) 237void cmd_status(ToxWindow *self, Messenger *m, char **args)
238{ 238{
239 char *status = args[1]; 239 char *status = args[1];
240 char *status_text; 240 char *status_text;
@@ -260,24 +260,24 @@ void cmd_status(ToxWindow *self, char **args)
260 260
261 char *msg = args[2]; 261 char *msg = args[2];
262 if (msg == NULL) { 262 if (msg == NULL) {
263 m_set_userstatus(status_kind); 263 m_set_userstatus(m, status_kind);
264 wprintw(self->window, "Status set to: %s\n", status_text); 264 wprintw(self->window, "Status set to: %s\n", status_text);
265 } 265 }
266 else { 266 else {
267 m_set_userstatus(status_kind); 267 m_set_userstatus(m, status_kind);
268 m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); 268 m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
269 wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); 269 wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
270 } 270 }
271} 271}
272 272
273void cmd_statusmsg(ToxWindow *self, char **args) 273void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args)
274{ 274{
275 char *msg = args[1]; 275 char *msg = args[1];
276 m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); 276 m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
277 wprintw(self->window, "Status set to: %s\n", msg); 277 wprintw(self->window, "Status set to: %s\n", msg);
278} 278}
279 279
280static void execute(ToxWindow *self, char *u_cmd) 280static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
281{ 281{
282 int newlines = 0; 282 int newlines = 0;
283 char cmd[MAX_STR_SIZE] = {0}; 283 char cmd[MAX_STR_SIZE] = {0};
@@ -341,13 +341,13 @@ static void execute(ToxWindow *self, char *u_cmd)
341 return; 341 return;
342 } 342 }
343 } 343 }
344 /* check for excess arguments */ 344 /* check for excess arguments */
345 if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) { 345 if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
346 wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); 346 wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
347 return; 347 return;
348 } 348 }
349 /* pass arguments to command function */ 349 /* pass arguments to command function */
350 (commands[i].func)(self, cmdargs); 350 (commands[i].func)(self, m, cmdargs);
351 return; 351 return;
352 } 352 }
353 } 353 }
@@ -356,7 +356,7 @@ static void execute(ToxWindow *self, char *u_cmd)
356 wprintw(self->window, "Invalid command.\n"); 356 wprintw(self->window, "Invalid command.\n");
357} 357}
358 358
359static void prompt_onKey(ToxWindow *self, int key) 359static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
360{ 360{
361 /* Add printable characters to line */ 361 /* Add printable characters to line */
362 if (isprint(key)) { 362 if (isprint(key)) {
@@ -380,7 +380,7 @@ static void prompt_onKey(ToxWindow *self, int key)
380 /* RETURN key: execute command */ 380 /* RETURN key: execute command */
381 else if (key == '\n') { 381 else if (key == '\n') {
382 wprintw(self->window, "\n"); 382 wprintw(self->window, "\n");
383 execute(self, prompt_buf); 383 execute(self, m, prompt_buf);
384 prompt_buf_pos = 0; 384 prompt_buf_pos = 0;
385 prompt_buf[0] = 0; 385 prompt_buf[0] = 0;
386 } 386 }
@@ -413,10 +413,10 @@ static void prompt_onDraw(ToxWindow *self)
413 wrefresh(self->window); 413 wrefresh(self->window);
414} 414}
415 415
416static void prompt_onInit(ToxWindow *self) 416static void prompt_onInit(ToxWindow *self, Messenger *m)
417{ 417{
418 scrollok(self->window, 1); 418 scrollok(self->window, 1);
419 cmd_help(self, NULL); 419 cmd_help(self, m, NULL);
420 wclrtoeol(self->window); 420 wclrtoeol(self->window);
421} 421}
422 422
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h
index c6925ce1..648243d0 100644
--- a/testing/toxic/windows.h
+++ b/testing/toxic/windows.h
@@ -9,7 +9,7 @@
9#define KEY_SIZE_BYTES 32 9#define KEY_SIZE_BYTES 32
10 10
11/* number of permanent default windows */ 11/* number of permanent default windows */
12#define N_DEFAULT_WINS 2 12#define N_DEFAULT_WINS 2
13 13
14/* maximum window slots for WINDOW_STATUS array */ 14/* maximum window slots for WINDOW_STATUS array */
15#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM 15#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM
@@ -17,14 +17,14 @@
17typedef struct ToxWindow_ ToxWindow; 17typedef struct ToxWindow_ ToxWindow;
18 18
19struct ToxWindow_ { 19struct ToxWindow_ {
20 void(*onKey)(ToxWindow*, int); 20 void(*onKey)(ToxWindow*, Messenger*, int);
21 void(*onDraw)(ToxWindow*); 21 void(*onDraw)(ToxWindow*);
22 void(*onInit)(ToxWindow*); 22 void(*onInit)(ToxWindow*, Messenger*);
23 void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); 23 void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t);
24 void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); 24 void(*onMessage)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t);
25 void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); 25 void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
26 void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); 26 void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t);
27 void(*onAction)(ToxWindow*, int, uint8_t*, uint16_t); 27 void(*onAction)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t);
28 char title[256]; 28 char title[256];
29 29
30 void* x; 30 void* x;