diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | core/Messenger.c | 31 | ||||
-rw-r--r-- | core/Messenger.h | 4 | ||||
-rw-r--r-- | testing/nTox_win32.c | 2 | ||||
-rw-r--r-- | testing/toxic/CMakeLists.txt | 3 | ||||
-rw-r--r-- | testing/toxic/configdir.c | 136 | ||||
-rw-r--r-- | testing/toxic/configdir.h | 33 | ||||
-rw-r--r-- | testing/toxic/main.c | 31 |
8 files changed, 232 insertions, 11 deletions
@@ -27,3 +27,6 @@ tags | |||
27 | *.exe | 27 | *.exe |
28 | *.out | 28 | *.out |
29 | *.app | 29 | *.app |
30 | |||
31 | #netbeans | ||
32 | nbproject | ||
diff --git a/core/Messenger.c b/core/Messenger.c index bf87a3e4..af4fd797 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -55,6 +55,8 @@ static Friend *friendlist; | |||
55 | static uint32_t numfriends; | 55 | static uint32_t numfriends; |
56 | 56 | ||
57 | 57 | ||
58 | static void set_friend_status(int friendnumber, uint8_t status); | ||
59 | |||
58 | /* 1 if we are online | 60 | /* 1 if we are online |
59 | 0 if we are offline | 61 | 0 if we are offline |
60 | static uint8_t online; */ | 62 | static uint8_t online; */ |
@@ -134,7 +136,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
134 | for (i = 0; i <= numfriends; ++i) { | 136 | for (i = 0; i <= numfriends; ++i) { |
135 | if (friendlist[i].status == NOFRIEND) { | 137 | if (friendlist[i].status == NOFRIEND) { |
136 | DHT_addfriend(client_id); | 138 | DHT_addfriend(client_id); |
137 | friendlist[i].status = FRIEND_ADDED; | 139 | set_friend_status(i, FRIEND_ADDED); |
138 | friendlist[i].crypt_connection_id = -1; | 140 | friendlist[i].crypt_connection_id = -1; |
139 | friendlist[i].friend_request_id = -1; | 141 | friendlist[i].friend_request_id = -1; |
140 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 142 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -165,7 +167,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
165 | for (i = 0; i <= numfriends; ++i) { | 167 | for (i = 0; i <= numfriends; ++i) { |
166 | if(friendlist[i].status == NOFRIEND) { | 168 | if(friendlist[i].status == NOFRIEND) { |
167 | DHT_addfriend(client_id); | 169 | DHT_addfriend(client_id); |
168 | friendlist[i].status = FRIEND_REQUESTED; | 170 | set_friend_status(i, FRIEND_REQUESTED); |
169 | friendlist[i].crypt_connection_id = -1; | 171 | friendlist[i].crypt_connection_id = -1; |
170 | friendlist[i].friend_request_id = -1; | 172 | friendlist[i].friend_request_id = -1; |
171 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 173 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -500,6 +502,21 @@ void m_callback_read_receipt(void (*function)(int, uint32_t)) | |||
500 | read_receipt_isset = 1; | 502 | read_receipt_isset = 1; |
501 | } | 503 | } |
502 | 504 | ||
505 | static void (*friend_statuschange)(int, uint8_t); | ||
506 | static uint8_t friend_statuschange_isset = 0; | ||
507 | void m_callback_friend_status(void (*function)(int, uint8_t)) | ||
508 | { | ||
509 | friend_statuschange = function; | ||
510 | friend_statuschange_isset = 1; | ||
511 | } | ||
512 | |||
513 | static void set_friend_status(int friendnumber, uint8_t status) | ||
514 | { | ||
515 | if (friendlist[friendnumber].status != status && friend_statuschange_isset) | ||
516 | friend_statuschange(friendnumber, status); | ||
517 | friendlist[friendnumber].status = status; | ||
518 | } | ||
519 | |||
503 | #define PORT 33445 | 520 | #define PORT 33445 |
504 | /* run this at startup */ | 521 | /* run this at startup */ |
505 | int initMessenger(void) | 522 | int initMessenger(void) |
@@ -527,9 +544,9 @@ static void doFriends(void) | |||
527 | if (friendlist[i].status == FRIEND_ADDED) { | 544 | if (friendlist[i].status == FRIEND_ADDED) { |
528 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 545 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
529 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ | 546 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ |
530 | friendlist[i].status = FRIEND_REQUESTED; | 547 | set_friend_status(i, FRIEND_REQUESTED); |
531 | else if (fr > 0) | 548 | else if (fr > 0) |
532 | friendlist[i].status = FRIEND_REQUESTED; | 549 | set_friend_status(i, FRIEND_REQUESTED); |
533 | } | 550 | } |
534 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 551 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ |
535 | if (friendlist[i].status == FRIEND_REQUESTED) { | 552 | if (friendlist[i].status == FRIEND_REQUESTED) { |
@@ -545,7 +562,7 @@ static void doFriends(void) | |||
545 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 562 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
546 | break; | 563 | break; |
547 | case 3: /* Connection is established */ | 564 | case 3: /* Connection is established */ |
548 | friendlist[i].status = FRIEND_ONLINE; | 565 | set_friend_status(i, FRIEND_ONLINE); |
549 | break; | 566 | break; |
550 | case 4: | 567 | case 4: |
551 | crypto_kill(friendlist[i].crypt_connection_id); | 568 | crypto_kill(friendlist[i].crypt_connection_id); |
@@ -632,7 +649,7 @@ static void doFriends(void) | |||
632 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 649 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
633 | crypto_kill(friendlist[i].crypt_connection_id); | 650 | crypto_kill(friendlist[i].crypt_connection_id); |
634 | friendlist[i].crypt_connection_id = -1; | 651 | friendlist[i].crypt_connection_id = -1; |
635 | friendlist[i].status = FRIEND_CONFIRMED; | 652 | set_friend_status(i, FRIEND_CONFIRMED); |
636 | } | 653 | } |
637 | break; | 654 | break; |
638 | } | 655 | } |
@@ -653,7 +670,7 @@ static void doInbound(void) | |||
653 | friendlist[friend_id].crypt_connection_id = | 670 | friendlist[friend_id].crypt_connection_id = |
654 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 671 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
655 | 672 | ||
656 | friendlist[friend_id].status = FRIEND_CONFIRMED; | 673 | set_friend_status(friend_id, FRIEND_CONFIRMED); |
657 | } | 674 | } |
658 | } | 675 | } |
659 | } | 676 | } |
diff --git a/core/Messenger.h b/core/Messenger.h index 40d02f46..55c86682 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -211,6 +211,10 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS)); | |||
211 | in that case, you should discard it. */ | 211 | in that case, you should discard it. */ |
212 | void m_callback_read_receipt(void (*function)(int, uint32_t)); | 212 | void m_callback_read_receipt(void (*function)(int, uint32_t)); |
213 | 213 | ||
214 | /* set the callback for friend status changes | ||
215 | function(int friendnumber, uint8_t status) */ | ||
216 | void m_callback_friendstatus(void (*function)(int, uint8_t)); | ||
217 | |||
214 | /* run this at startup | 218 | /* run this at startup |
215 | returns 0 if no connection problems | 219 | returns 0 if no connection problems |
216 | returns -1 if there are problems */ | 220 | returns -1 if there are problems */ |
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index d9d695a5..d2642df1 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c | |||
@@ -416,7 +416,7 @@ int main(int argc, char *argv[]) | |||
416 | m_callback_friendrequest(print_request); | 416 | m_callback_friendrequest(print_request); |
417 | m_callback_friendmessage(print_message); | 417 | m_callback_friendmessage(print_message); |
418 | m_callback_namechange(print_nickchange); | 418 | m_callback_namechange(print_nickchange); |
419 | m_callback_statusmessae(print_statuschange); | 419 | m_callback_statusmessage(print_statuschange); |
420 | char idstring1[PUB_KEY_BYTES][5]; | 420 | char idstring1[PUB_KEY_BYTES][5]; |
421 | char idstring2[PUB_KEY_BYTES][5]; | 421 | char idstring2[PUB_KEY_BYTES][5]; |
422 | int i; | 422 | int i; |
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index 38f02dc6..13b8692d 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt | |||
@@ -10,7 +10,8 @@ add_executable(${exe_name} | |||
10 | main.c | 10 | main.c |
11 | prompt.c | 11 | prompt.c |
12 | friendlist.c | 12 | friendlist.c |
13 | chat.c) | 13 | chat.c |
14 | configdir.c) | ||
14 | 15 | ||
15 | target_link_libraries(${exe_name} | 16 | target_link_libraries(${exe_name} |
16 | curses) | 17 | curses) |
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c new file mode 100644 index 00000000..d91d0804 --- /dev/null +++ b/testing/toxic/configdir.c | |||
@@ -0,0 +1,136 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
3 | * | ||
4 | * This file is part of Tox. | ||
5 | * | ||
6 | * Tox is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * Tox is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | #include <string.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <stdio.h> | ||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | #ifdef WIN32 | ||
29 | #include <shlobj.h> | ||
30 | #include <direct.h> | ||
31 | #endif | ||
32 | |||
33 | #ifdef __APPLE__ | ||
34 | #include <unistd.h> | ||
35 | #include <pwd.h> | ||
36 | #endif | ||
37 | |||
38 | #include "configdir.h" | ||
39 | |||
40 | /* | ||
41 | * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash | ||
42 | */ | ||
43 | char *get_user_config_dir(void) | ||
44 | { | ||
45 | char *user_config_dir; | ||
46 | |||
47 | #ifdef WIN32 | ||
48 | |||
49 | char appdata[MAX_PATH]; | ||
50 | HRESULT result = SHGetFolderPath( | ||
51 | NULL, | ||
52 | CSIDL_APPDATA, | ||
53 | NULL, | ||
54 | SHGFP_TYPE_CURRENT, | ||
55 | appdata | ||
56 | ) | ||
57 | if (!result) return NULL; | ||
58 | |||
59 | user_config_dir = strdup(appdata); | ||
60 | |||
61 | return user_config_dir; | ||
62 | |||
63 | #elif defined __APPLE__ | ||
64 | |||
65 | struct passwd *pass = getpwuid(getuid()); | ||
66 | if (!pass) return NULL; | ||
67 | char *home = pass->pw_dir; | ||
68 | user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1); | ||
69 | |||
70 | if(user_config_dir) { | ||
71 | strcpy(user_config_dir, home); | ||
72 | strcat(user_config_dir, "/Library/Application Support"); | ||
73 | } | ||
74 | return user_config_dir; | ||
75 | |||
76 | #else | ||
77 | |||
78 | if (getenv("XDG_CONFIG_HOME")) { | ||
79 | user_config_dir = strdup(getenv("XDG_CONFIG_HOME")); | ||
80 | } else { | ||
81 | user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); | ||
82 | if (user_config_dir) { | ||
83 | strcpy(user_config_dir, getenv("HOME")); | ||
84 | strcat(user_config_dir, "/.config"); | ||
85 | } | ||
86 | } | ||
87 | return user_config_dir; | ||
88 | |||
89 | #endif | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | * Creates the config directory. | ||
94 | */ | ||
95 | int create_user_config_dir(char *path) | ||
96 | { | ||
97 | |||
98 | int mkdir_err; | ||
99 | |||
100 | #ifdef WIN32 | ||
101 | |||
102 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
103 | strcpy(fullpath, path); | ||
104 | strcat(fullpath, CONFIGDIR); | ||
105 | |||
106 | mkdir_err = _mkdir(fullpath); | ||
107 | struct __stat64 buf; | ||
108 | if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
109 | free(fullpath); | ||
110 | return -1; | ||
111 | } | ||
112 | |||
113 | #else | ||
114 | |||
115 | mkdir_err = mkdir(path, 0700); | ||
116 | struct stat buf; | ||
117 | |||
118 | if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { | ||
119 | return -1; | ||
120 | } | ||
121 | |||
122 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
123 | strcpy(fullpath, path); | ||
124 | strcat(fullpath, CONFIGDIR); | ||
125 | |||
126 | mkdir_err = mkdir(fullpath, 0700); | ||
127 | |||
128 | if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
129 | free(fullpath); | ||
130 | return -1; | ||
131 | } | ||
132 | |||
133 | #endif | ||
134 | |||
135 | return 0; | ||
136 | } | ||
diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h new file mode 100644 index 00000000..fad949cf --- /dev/null +++ b/testing/toxic/configdir.h | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
3 | * | ||
4 | * This file is part of Tox. | ||
5 | * | ||
6 | * Tox is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * Tox is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | #ifdef _win32 | ||
22 | #define CONFIGDIR "\\toxic\\" | ||
23 | #else | ||
24 | #define CONFIGDIR "/toxic/" | ||
25 | #endif | ||
26 | |||
27 | #ifndef S_ISDIR | ||
28 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | ||
29 | #endif | ||
30 | |||
31 | char *get_user_config_dir(void); | ||
32 | |||
33 | int create_user_config_dir(char *path); | ||
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index d5999eb2..2922456e 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -3,14 +3,23 @@ | |||
3 | */ | 3 | */ |
4 | 4 | ||
5 | #include <curses.h> | 5 | #include <curses.h> |
6 | #include <errno.h> | ||
6 | #include <stdio.h> | 7 | #include <stdio.h> |
7 | #include <stdlib.h> | 8 | #include <stdlib.h> |
8 | #include <stdbool.h> | 9 | #include <stdbool.h> |
9 | #include <stdint.h> | 10 | #include <stdint.h> |
10 | 11 | ||
12 | #ifdef _win32 | ||
13 | #include <direct.h> | ||
14 | #else | ||
15 | #include <sys/stat.h> | ||
16 | #include <sys/types.h> | ||
17 | #endif | ||
18 | |||
11 | #include "../../core/Messenger.h" | 19 | #include "../../core/Messenger.h" |
12 | #include "../../core/network.h" | 20 | #include "../../core/network.h" |
13 | 21 | ||
22 | #include "configdir.h" | ||
14 | #include "windows.h" | 23 | #include "windows.h" |
15 | 24 | ||
16 | extern ToxWindow new_prompt(); | 25 | extern ToxWindow new_prompt(); |
@@ -342,9 +351,19 @@ int main(int argc, char *argv[]) | |||
342 | { | 351 | { |
343 | int ch; | 352 | int ch; |
344 | int f_flag = 0; | 353 | int f_flag = 0; |
345 | char *filename = "data"; | 354 | char *user_config_dir = get_user_config_dir(); |
355 | char *filename; | ||
356 | int config_err = create_user_config_dir(user_config_dir); | ||
357 | if(config_err) { | ||
358 | filename = "data"; | ||
359 | } else { | ||
360 | filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); | ||
361 | strcpy(filename, user_config_dir); | ||
362 | strcat(filename, CONFIGDIR); | ||
363 | strcat(filename, "data"); | ||
364 | } | ||
365 | |||
346 | ToxWindow* a; | 366 | ToxWindow* a; |
347 | |||
348 | int i = 0; | 367 | int i = 0; |
349 | for (i = 0; i < argc; ++i) { | 368 | for (i = 0; i < argc; ++i) { |
350 | if (argv[i] == NULL) | 369 | if (argv[i] == NULL) |
@@ -362,6 +381,7 @@ int main(int argc, char *argv[]) | |||
362 | init_term(); | 381 | init_term(); |
363 | init_tox(); | 382 | init_tox(); |
364 | load_data(filename); | 383 | load_data(filename); |
384 | free(filename); | ||
365 | init_windows(); | 385 | init_windows(); |
366 | init_window_status(); | 386 | init_window_status(); |
367 | 387 | ||
@@ -371,6 +391,13 @@ int main(int argc, char *argv[]) | |||
371 | "defaulting to 'data' for a keyfile...\n"); | 391 | "defaulting to 'data' for a keyfile...\n"); |
372 | attroff(COLOR_PAIR(3) | A_BOLD); | 392 | attroff(COLOR_PAIR(3) | A_BOLD); |
373 | } | 393 | } |
394 | |||
395 | if(config_err) { | ||
396 | attron(COLOR_PAIR(3) | A_BOLD); | ||
397 | wprintw(prompt->window, "Unable to determine configuration directory!\n" | ||
398 | "defaulting to 'data' for a keyfile...\n"); | ||
399 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
400 | } | ||
374 | 401 | ||
375 | while(true) { | 402 | while(true) { |
376 | /* Update tox */ | 403 | /* Update tox */ |