diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-11 07:40:07 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-11 07:40:07 -0700 |
commit | c0d5fe9b5b27981f1d1c040366dc945ce7cf1624 (patch) | |
tree | 500ef538191c44574f7e607869cba829de3f196c /testing/toxic | |
parent | 3a2d453165ea99deb1898992cfb9a7fa2913ec13 (diff) | |
parent | 2c2d608e29a02dc7b73ba3f18c90b8ecc2f00fa8 (diff) |
Merge pull request #423 from JFreegman/master
Auto-connect toxic to DHT on startup
Diffstat (limited to 'testing/toxic')
-rw-r--r-- | testing/toxic/chat.c | 2 | ||||
-rw-r--r-- | testing/toxic/configdir.c | 4 | ||||
-rw-r--r-- | testing/toxic/main.c | 93 |
3 files changed, 76 insertions, 23 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 33f51f29..e1897230 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -150,7 +150,7 @@ static void chat_onKey(ToxWindow *self, int key) | |||
150 | } | 150 | } |
151 | 151 | ||
152 | /* RETURN key: Execute command or print line */ | 152 | /* RETURN key: Execute command or print line */ |
153 | if (key == '\n') { | 153 | else if (key == '\n') { |
154 | wclear(ctx->linewin); | 154 | wclear(ctx->linewin); |
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); |
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index d91d0804..6cbb06bc 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c | |||
@@ -129,8 +129,8 @@ int create_user_config_dir(char *path) | |||
129 | free(fullpath); | 129 | free(fullpath); |
130 | return -1; | 130 | return -1; |
131 | } | 131 | } |
132 | 132 | ||
133 | #endif | 133 | #endif |
134 | 134 | free(fullpath); | |
135 | return 0; | 135 | return 0; |
136 | } | 136 | } |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index bb5faf83..d7d375a3 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -28,6 +28,7 @@ extern ToxWindow new_friendlist(); | |||
28 | extern int friendlist_onFriendAdded(int num); | 28 | extern int friendlist_onFriendAdded(int num); |
29 | extern void disable_chatwin(int f_num); | 29 | extern void disable_chatwin(int f_num); |
30 | extern int add_req(uint8_t *public_key); // XXX | 30 | extern int add_req(uint8_t *public_key); // XXX |
31 | extern unsigned char *hex_string_to_bin(char hex_string[]); | ||
31 | 32 | ||
32 | /* Holds status of chat windows */ | 33 | /* Holds status of chat windows */ |
33 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | 34 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; |
@@ -138,6 +139,53 @@ static void init_tox() | |||
138 | m_callback_action(on_action); | 139 | m_callback_action(on_action); |
139 | } | 140 | } |
140 | 141 | ||
142 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ | ||
143 | #define MINLINE 70 | ||
144 | #define MAXSERVERS 50 | ||
145 | |||
146 | /* Connects to a random DHT server listed in the DHTservers file */ | ||
147 | int init_connection(void) | ||
148 | { | ||
149 | if (DHT_isconnected()) | ||
150 | return 0; | ||
151 | |||
152 | FILE *fp = fopen("../../../other/DHTservers", "r"); | ||
153 | if (!fp) | ||
154 | return 1; | ||
155 | |||
156 | char servers[MAXSERVERS][MAXLINE]; | ||
157 | char line[MAXLINE]; | ||
158 | int linecnt = 0; | ||
159 | while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { | ||
160 | int len = strlen(line); | ||
161 | if (len > MINLINE && len < MAXLINE) | ||
162 | strcpy(servers[linecnt++], line); | ||
163 | } | ||
164 | if (linecnt < 1) { | ||
165 | fclose(fp); | ||
166 | return 2; | ||
167 | } | ||
168 | fclose(fp); | ||
169 | |||
170 | char *server = servers[rand() % linecnt]; | ||
171 | char *ip = strtok(server, " "); | ||
172 | char *port = strtok(NULL, " "); | ||
173 | char *key = strtok(NULL, " "); | ||
174 | if (!ip || !port || !key) | ||
175 | return 3; | ||
176 | |||
177 | IP_Port dht; | ||
178 | dht.port = htons(atoi(port)); | ||
179 | uint32_t resolved_address = resolve_addr(ip); | ||
180 | if (resolved_address == 0) | ||
181 | return 4; | ||
182 | dht.ip.i = resolved_address; | ||
183 | unsigned char *binary_string = hex_string_to_bin(key); | ||
184 | DHT_bootstrap(dht, binary_string); | ||
185 | free(binary_string); | ||
186 | return 0; | ||
187 | } | ||
188 | |||
141 | void init_window_status() | 189 | void init_window_status() |
142 | { | 190 | { |
143 | /* Default window values decrement from -2 */ | 191 | /* Default window values decrement from -2 */ |
@@ -203,11 +251,11 @@ static void do_tox() | |||
203 | static bool dht_on = false; | 251 | static bool dht_on = false; |
204 | if (!dht_on && DHT_isconnected()) { | 252 | if (!dht_on && DHT_isconnected()) { |
205 | dht_on = true; | 253 | dht_on = true; |
206 | wprintw(prompt->window, "\nDHT connected!\n"); | 254 | wprintw(prompt->window, "\nDHT connected.\n"); |
207 | } | 255 | } |
208 | else if (dht_on && !DHT_isconnected()) { | 256 | else if (dht_on && !DHT_isconnected()) { |
209 | dht_on = false; | 257 | dht_on = false; |
210 | wprintw(prompt->window, "\nDHT disconnected!\n"); | 258 | wprintw(prompt->window, "\nDHT disconnected.\n"); |
211 | } | 259 | } |
212 | doMessenger(); | 260 | doMessenger(); |
213 | } | 261 | } |
@@ -350,21 +398,22 @@ void set_active_window(int ch) | |||
350 | int main(int argc, char *argv[]) | 398 | int main(int argc, char *argv[]) |
351 | { | 399 | { |
352 | int ch; | 400 | int ch; |
353 | int f_flag = 0; | 401 | ToxWindow* a; |
354 | char *user_config_dir = get_user_config_dir(); | 402 | char *user_config_dir = get_user_config_dir(); |
355 | char *filename; | 403 | char *DATA_FILE; |
356 | int config_err = create_user_config_dir(user_config_dir); | 404 | int config_err = create_user_config_dir(user_config_dir); |
357 | uint8_t loadfromfile = 1; | ||
358 | if(config_err) { | 405 | if(config_err) { |
359 | filename = "data"; | 406 | DATA_FILE = "data"; |
360 | } else { | 407 | } else { |
361 | filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); | 408 | DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); |
362 | strcpy(filename, user_config_dir); | 409 | strcpy(DATA_FILE, user_config_dir); |
363 | strcat(filename, CONFIGDIR); | 410 | strcat(DATA_FILE, CONFIGDIR); |
364 | strcat(filename, "data"); | 411 | strcat(DATA_FILE, "data"); |
365 | } | 412 | } |
366 | 413 | ||
367 | ToxWindow* a; | 414 | /* This is broken */ |
415 | int f_loadfromfile = 1; | ||
416 | int f_flag = 0; | ||
368 | int i = 0; | 417 | int i = 0; |
369 | for (i = 0; i < argc; ++i) { | 418 | for (i = 0; i < argc; ++i) { |
370 | if (argv[i] == NULL) | 419 | if (argv[i] == NULL) |
@@ -372,37 +421,41 @@ int main(int argc, char *argv[]) | |||
372 | else if (argv[i][0] == '-') { | 421 | else if (argv[i][0] == '-') { |
373 | if (argv[i][1] == 'f') { | 422 | if (argv[i][1] == 'f') { |
374 | if (argv[i + 1] != NULL) | 423 | if (argv[i + 1] != NULL) |
375 | filename = argv[i + 1]; | 424 | DATA_FILE = argv[i + 1]; |
376 | else | 425 | else |
377 | f_flag = -1; | 426 | f_flag = -1; |
378 | } else if (argv[i][1] == 'n') { | 427 | } else if (argv[i][1] == 'n') { |
379 | loadfromfile = 0; | 428 | f_loadfromfile = 0; |
380 | } | 429 | } |
381 | } | 430 | } |
382 | } | 431 | } |
383 | 432 | ||
384 | init_term(); | 433 | init_term(); |
385 | init_tox(); | 434 | init_tox(); |
386 | if(loadfromfile) | ||
387 | load_data(filename); | ||
388 | free(filename); | ||
389 | init_windows(); | 435 | init_windows(); |
390 | init_window_status(); | 436 | init_window_status(); |
391 | 437 | ||
438 | if(f_loadfromfile) | ||
439 | load_data(DATA_FILE); | ||
440 | free(DATA_FILE); | ||
441 | |||
442 | int connected = init_connection(); | ||
443 | if (connected != 0) | ||
444 | wprintw(prompt->window, "Auto-connect failed (error code %d)\n", connected); | ||
445 | |||
392 | if (f_flag == -1) { | 446 | if (f_flag == -1) { |
393 | attron(COLOR_PAIR(3) | A_BOLD); | 447 | attron(COLOR_PAIR(3) | A_BOLD); |
394 | wprintw(prompt->window, "You passed '-f' without giving an argument!\n" | 448 | wprintw(prompt->window, "You passed '-f' without giving an argument.\n" |
395 | "defaulting to 'data' for a keyfile...\n"); | 449 | "defaulting to 'data' for a keyfile...\n"); |
396 | attroff(COLOR_PAIR(3) | A_BOLD); | 450 | attroff(COLOR_PAIR(3) | A_BOLD); |
397 | } | 451 | } |
398 | 452 | ||
399 | if(config_err) { | 453 | if(config_err) { |
400 | attron(COLOR_PAIR(3) | A_BOLD); | 454 | attron(COLOR_PAIR(3) | A_BOLD); |
401 | wprintw(prompt->window, "Unable to determine configuration directory!\n" | 455 | wprintw(prompt->window, "Unable to determine configuration directory.\n" |
402 | "defaulting to 'data' for a keyfile...\n"); | 456 | "defaulting to 'data' for a keyfile...\n"); |
403 | attroff(COLOR_PAIR(3) | A_BOLD); | 457 | attroff(COLOR_PAIR(3) | A_BOLD); |
404 | } | 458 | } |
405 | |||
406 | while(true) { | 459 | while(true) { |
407 | /* Update tox */ | 460 | /* Update tox */ |
408 | do_tox(); | 461 | do_tox(); |