summaryrefslogtreecommitdiff
path: root/testing/toxic/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r--testing/toxic/main.c414
1 files changed, 75 insertions, 339 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index e9d285cc..aa025a1a 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -8,6 +8,7 @@
8#include <stdlib.h> 8#include <stdlib.h>
9#include <stdbool.h> 9#include <stdbool.h>
10#include <stdint.h> 10#include <stdint.h>
11#include <signal.h>
11 12
12#ifdef _win32 13#ifdef _win32
13#include <direct.h> 14#include <direct.h>
@@ -21,104 +22,21 @@
21 22
22#include "configdir.h" 23#include "configdir.h"
23#include "windows.h" 24#include "windows.h"
25#include "prompt.h"
26#include "friendlist.h"
24 27
25extern ToxWindow new_prompt();
26extern ToxWindow new_friendlist();
27 28
28extern int friendlist_onFriendAdded(Messenger *m, int num); 29void on_window_resize(int sig)
29extern void disable_chatwin(int f_num);
30extern int add_req(uint8_t *public_key); // XXX
31extern unsigned char *hex_string_to_bin(char hex_string[]);
32
33static int store_data(char*);
34
35/* Holds status of chat windows */
36char WINDOW_STATUS[MAX_WINDOW_SLOTS];
37
38#ifndef TOXICVER
39#define TOXICVER "NOVER" //Use the -D flag to set this
40#endif
41
42static ToxWindow windows[MAX_WINDOW_SLOTS];
43static ToxWindow* prompt;
44
45static Messenger *m;
46static char *DATA_FILE;
47
48int w_num;
49int active_window;
50
51/* CALLBACKS START */
52void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
53{
54 int n = add_req(public_key);
55 wprintw(prompt->window, "\nFriend request from:\n");
56
57 int i;
58 for (i = 0; i < KEY_SIZE_BYTES; ++i) {
59 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
60 }
61
62 wprintw(prompt->window, "\nWith the message: %s\n", data);
63 wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
64
65 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
66 if (windows[i].onFriendRequest != NULL)
67 windows[i].onFriendRequest(&windows[i], public_key, data, length);
68 }
69}
70
71void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
72{
73 int i;
74 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
75 if (windows[i].onMessage != NULL)
76 windows[i].onMessage(&windows[i], m, friendnumber, string, length);
77 }
78}
79
80void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
81{
82 int i;
83 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
84 if (windows[i].onAction != NULL)
85 windows[i].onAction(&windows[i], m, friendnumber, string, length);
86 }
87}
88
89void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
90{
91 wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
92 int i;
93 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
94 if (windows[i].onNickChange != NULL)
95 windows[i].onNickChange(&windows[i], friendnumber, string, length);
96 }
97}
98
99void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
100{
101 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
102 int i;
103 for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
104 if (windows[i].onStatusChange != NULL)
105 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
106 }
107}
108
109void on_friendadded(int friendnumber)
110{ 30{
111 friendlist_onFriendAdded(m, friendnumber); 31 endwin();
112 int st; 32 refresh();
113 if ((st = store_data(DATA_FILE)) != 0) { 33 clear();
114 wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st);
115 }
116} 34}
117/* CALLBACKS END */
118 35
119static void init_term() 36static void init_term()
120{ 37{
121 /* Setup terminal */ 38 /* Setup terminal */
39 signal(SIGWINCH, on_window_resize);
122 initscr(); 40 initscr();
123 cbreak(); 41 cbreak();
124 keypad(stdscr, 1); 42 keypad(stdscr, 1);
@@ -136,10 +54,10 @@ static void init_term()
136 refresh(); 54 refresh();
137} 55}
138 56
139static void init_tox() 57static Messenger *init_tox()
140{ 58{
141 /* Init core */ 59 /* Init core */
142 m = initMessenger(); 60 Messenger *m = initMessenger();
143 61
144 /* Callbacks */ 62 /* Callbacks */
145 m_callback_friendrequest(m, on_request, NULL); 63 m_callback_friendrequest(m, on_request, NULL);
@@ -154,6 +72,7 @@ static void init_tox()
154#else 72#else
155 setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); 73 setname(m, (uint8_t*) "Hipster", sizeof("Hipster"));
156#endif 74#endif
75 return m;
157} 76}
158 77
159#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ 78#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@@ -202,67 +121,7 @@ int init_connection(void)
202 return 0; 121 return 0;
203} 122}
204 123
205void init_window_status() 124static void do_tox(Messenger *m, ToxWindow * prompt)
206{
207 /* Default window values decrement from -2 */
208 int i;
209 for (i = 0; i < N_DEFAULT_WINS; ++i)
210 WINDOW_STATUS[i] = -(i+2);
211
212 int j;
213 for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
214 WINDOW_STATUS[j] = -1;
215}
216
217int add_window(ToxWindow w, int n)
218{
219 if (w_num >= TOXWINDOWS_MAX_NUM)
220 return -1;
221
222 if (LINES < 2)
223 return -1;
224
225 w.window = newwin(LINES - 2, COLS, 0, 0);
226 if (w.window == NULL)
227 return -1;
228
229 windows[n] = w;
230 w.onInit(&w, m);
231 w_num++;
232 return n;
233}
234
235/* Deletes window w and cleans up */
236void del_window(ToxWindow *w, int f_num)
237{
238 delwin(w->window);
239 int i;
240 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
241 if (WINDOW_STATUS[i] == f_num) {
242 WINDOW_STATUS[i] = -1;
243 disable_chatwin(f_num);
244 break;
245 }
246 }
247 clear();
248 refresh();
249}
250
251static void init_windows()
252{
253 w_num = 0;
254 int n_prompt = 0;
255 int n_friendslist = 1;
256 if (add_window(new_prompt(), n_prompt) == -1
257 || add_window(new_friendlist(), n_friendslist) == -1) {
258 fprintf(stderr, "add_window() failed.\n");
259 endwin();
260 exit(1);
261 }
262 prompt = &windows[n_prompt];
263}
264
265static void do_tox()
266{ 125{
267 static int conn_try = 0; 126 static int conn_try = 0;
268 static int conn_err = 0; 127 static int conn_err = 0;
@@ -286,190 +145,69 @@ static void do_tox()
286 doMessenger(m); 145 doMessenger(m);
287} 146}
288 147
289static void populate_friends() 148static void load_data(Messenger *m, char *path)
290{ 149{
291 wprintw(prompt->window, "Populating friends...\n"); 150 FILE *fd;
292 uint32_t i; 151 size_t len;
293 for (i = 0; i < m->numfriends; i++) { 152 uint8_t *buf;
294 wprintw(prompt->window, "Added friend %d\n", i);
295 friendlist_onFriendAdded(m, i);
296 }
297}
298 153
299/* 154 if ((fd = fopen(path, "r")) != NULL) {
300 * Store Messenger data to path 155 fseek(fd, 0, SEEK_END);
301 * Return 0 Messenger stored successfully 156 len = ftell(fd);
302 * Return 1 malloc failed 157 fseek(fd, 0, SEEK_SET);
303 * Return 2 fopen failed
304 * Return 3 fwrite failed
305 */
306static int store_data(char *path)
307{
308 FILE *fd;
309 size_t len;
310 uint8_t *buf;
311 158
159 buf = malloc(len);
160 if (buf == NULL) {
161 fprintf(stderr, "malloc() failed.\n");
162 fclose(fd);
163 endwin();
164 exit(1);
165 }
166 if (fread(buf, len, 1, fd) != 1){
167 fprintf(stderr, "fread() failed.\n");
168 free(buf);
169 fclose(fd);
170 endwin();
171 exit(1);
172 }
173 Messenger_load(m, buf, len);
174 }
175 else {
312 len = Messenger_size(m); 176 len = Messenger_size(m);
313 buf = malloc(len); 177 buf = malloc(len);
314 if (buf == NULL) { 178 if (buf == NULL) {
315 return 1; 179 fprintf(stderr, "malloc() failed.\n");
180 endwin();
181 exit(1);
316 } 182 }
317 Messenger_save(m, buf); 183 Messenger_save(m, buf);
318 184
319 fd = fopen(path, "w"); 185 fd = fopen(path, "w");
320 if (fd == NULL) { 186 if (fd == NULL) {
321 free(buf); 187 fprintf(stderr, "fopen() failed.\n");
322 return 2; 188 free(buf);
323 } 189 endwin();
324 190 exit(1);
325 if (fwrite(buf, len, 1, fd) != 1) {
326 free(buf);
327 fclose(fd);
328 return 3;
329 }
330
331 free(buf);
332 fclose(fd);
333
334 wprintw(prompt->window, "Messenger stored\n");
335
336 return 0;
337}
338
339static void load_data(char *path) {
340 FILE *fd;
341 size_t len;
342 uint8_t *buf;
343
344 if ((fd = fopen(path, "r")) != NULL) {
345 fseek(fd, 0, SEEK_END);
346 len = ftell(fd);
347 fseek(fd, 0, SEEK_SET);
348
349 buf = malloc(len);
350 if (buf == NULL) {
351 fprintf(stderr, "malloc() failed.\n");
352 fclose(fd);
353 endwin();
354 exit(1);
355 }
356 if (fread(buf, len, 1, fd) != 1) {
357 fprintf(stderr, "fread() failed.\n");
358 free(buf);
359 fclose(fd);
360 endwin();
361 exit(1);
362 }
363 if (Messenger_load(m, buf, len) != 0) {
364 fprintf(stderr, "Problem while loading messenger");
365 }
366 free(buf);
367 fclose(fd);
368 } else {
369 int st;
370 if ((st = store_data(path)) != 0) {
371 fprintf(stderr, "storing messenger failed with error code: %d", st);
372 endwin();
373 exit(1);
374 }
375 }
376}
377
378static void draw_bar()
379{
380 static int odd = 0;
381 int blinkrate = 30;
382
383 attron(COLOR_PAIR(4));
384 mvhline(LINES - 2, 0, '_', COLS);
385 attroff(COLOR_PAIR(4));
386
387 move(LINES - 1, 0);
388
389 attron(COLOR_PAIR(4) | A_BOLD);
390 printw(" TOXIC " TOXICVER "|");
391 attroff(COLOR_PAIR(4) | A_BOLD);
392
393 int i;
394 for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
395 if (WINDOW_STATUS[i] != -1) {
396 if (i == active_window)
397 attron(A_BOLD);
398
399 odd = (odd+1) % blinkrate;
400 if (windows[i].blink && (odd < (blinkrate/2)))
401 attron(COLOR_PAIR(3));
402
403 printw(" %s", windows[i].title);
404 if (windows[i].blink && (odd < (blinkrate/2)))
405 attroff(COLOR_PAIR(3));
406
407 if (i == active_window) {
408 attroff(A_BOLD);
409 }
410 } 191 }
411 }
412 refresh();
413}
414 192
415void prepare_window(WINDOW *w) 193 if (fwrite(buf, len, 1, fd) != 1){
416{ 194 fprintf(stderr, "fwrite() failed.\n");
417 mvwin(w, 0, 0); 195 free(buf);
418 wresize(w, LINES-2, COLS); 196 fclose(fd);
419} 197 endwin();
420 198 exit(1);
421/* Shows next window when tab or back-tab is pressed */
422void set_active_window(int ch)
423{
424 int f_inf = 0;
425 int max = MAX_WINDOW_SLOTS-1;
426 if (ch == '\t') {
427 int i = (active_window + 1) % max;
428 while (true) {
429 if (WINDOW_STATUS[i] != -1) {
430 active_window = i;
431 return;
432 }
433 i = (i + 1) % max;
434 if (f_inf++ > max) { // infinite loop check
435 endwin();
436 exit(2);
437 }
438 }
439 }else {
440 int i = active_window - 1;
441 if (i < 0) i = max;
442 while (true) {
443 if (WINDOW_STATUS[i] != -1) {
444 active_window = i;
445 return;
446 }
447 if (--i < 0) i = max;
448 if (f_inf++ > max) {
449 endwin();
450 exit(2);
451 }
452 } 199 }
453 } 200 }
201 free(buf);
202 fclose(fd);
454} 203}
455 204
456int main(int argc, char *argv[]) 205int main(int argc, char *argv[])
457{ 206{
458 int ch;
459 ToxWindow* a;
460 char *user_config_dir = get_user_config_dir(); 207 char *user_config_dir = get_user_config_dir();
461 int config_err = create_user_config_dir(user_config_dir); 208 char *DATA_FILE = NULL;
462 if(config_err) { 209 int config_err = 0;
463 DATA_FILE = "data";
464 } else {
465 DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
466 strcpy(DATA_FILE, user_config_dir);
467 strcat(DATA_FILE, CONFIGDIR);
468 strcat(DATA_FILE, "data");
469 }
470 free(user_config_dir);
471 210
472 /* This is broken */
473 int f_loadfromfile = 1; 211 int f_loadfromfile = 1;
474 int f_flag = 0; 212 int f_flag = 0;
475 int i = 0; 213 int i = 0;
@@ -479,7 +217,7 @@ int main(int argc, char *argv[])
479 else if (argv[i][0] == '-') { 217 else if (argv[i][0] == '-') {
480 if (argv[i][1] == 'f') { 218 if (argv[i][1] == 'f') {
481 if (argv[i + 1] != NULL) 219 if (argv[i + 1] != NULL)
482 DATA_FILE = argv[i + 1]; 220 DATA_FILE = strdup(argv[i + 1]);
483 else 221 else
484 f_flag = -1; 222 f_flag = -1;
485 } else if (argv[i][1] == 'n') { 223 } else if (argv[i][1] == 'n') {
@@ -488,15 +226,27 @@ int main(int argc, char *argv[])
488 } 226 }
489 } 227 }
490 228
229 if (DATA_FILE == NULL ) {
230 config_err = create_user_config_dir(user_config_dir);
231 if (config_err) {
232 DATA_FILE = strdup("data");
233 } else {
234 DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
235 strcpy(DATA_FILE, user_config_dir);
236 strcat(DATA_FILE, CONFIGDIR);
237 strcat(DATA_FILE, "data");
238 }
239 }
240 free(user_config_dir);
241
491 init_term(); 242 init_term();
492 init_tox(); 243 Messenger *m = init_tox();
493 init_windows(); 244 ToxWindow *prompt = init_windows(m);
494 init_window_status(); 245 init_window_status();
495 246
496 if(f_loadfromfile) { 247 if(f_loadfromfile)
497 load_data(DATA_FILE); 248 load_data(m, DATA_FILE);
498 populate_friends(); 249 free(DATA_FILE);
499 }
500 250
501 if (f_flag == -1) { 251 if (f_flag == -1) {
502 attron(COLOR_PAIR(3) | A_BOLD); 252 attron(COLOR_PAIR(3) | A_BOLD);
@@ -513,25 +263,11 @@ int main(int argc, char *argv[])
513 } 263 }
514 while(true) { 264 while(true) {
515 /* Update tox */ 265 /* Update tox */
516 do_tox(); 266 do_tox(m, prompt);
517 267
518 /* Draw */ 268 /* Draw */
519 a = &windows[active_window]; 269 draw_active_window(m);
520 prepare_window(a->window);
521 a->blink = false;
522 draw_bar();
523 a->onDraw(a);
524
525 /* Handle input */
526 ch = getch();
527 if (ch == '\t' || ch == KEY_BTAB)
528 set_active_window(ch);
529 else if (ch != ERR)
530 a->onKey(a, m, ch);
531 } 270 }
532
533 cleanupMessenger(m); 271 cleanupMessenger(m);
534 free(DATA_FILE);
535
536 return 0; 272 return 0;
537} 273}