summaryrefslogtreecommitdiff
path: root/testing/toxic/main.c
diff options
context:
space:
mode:
authorslvr <slvr@unsafeio.com>2013-08-12 23:08:48 +0100
committerslvr <slvr@unsafeio.com>2013-08-12 23:08:48 +0100
commitbe840b46b961080aee8871a1278d9383affa6f4f (patch)
tree2fb12430cdb2bd4d1aac6b4c87505363d0387c3e /testing/toxic/main.c
parenta56d87cd3f7e20939e7f877a5c6419f28d9b8b24 (diff)
parentf9bfb42504ffb56fdcc028efbe1177fe1d45a26c (diff)
Merge remote-tracking branch 'upstream/master' into timer
Conflicts: core/Messenger.c
Diffstat (limited to 'testing/toxic/main.c')
-rw-r--r--testing/toxic/main.c66
1 files changed, 35 insertions, 31 deletions
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}