diff options
-rwxr-xr-x | auto_tests/friends_test.c | 36 | ||||
-rw-r--r-- | auto_tests/messenger_test.c | 50 | ||||
-rw-r--r-- | core/Messenger.c | 516 | ||||
-rw-r--r-- | core/Messenger.h | 126 | ||||
-rw-r--r-- | testing/Messenger_test.c | 79 | ||||
-rw-r--r-- | testing/nTox.c | 71 | ||||
-rw-r--r-- | testing/nTox.h | 6 | ||||
-rw-r--r-- | testing/toxic/chat.c | 36 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 16 | ||||
-rw-r--r-- | testing/toxic/main.c | 47 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 78 | ||||
-rw-r--r-- | testing/toxic/windows.h | 10 |
12 files changed, 562 insertions, 509 deletions
diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c index 0ba42b61..317714d6 100755 --- a/auto_tests/friends_test.c +++ b/auto_tests/friends_test.c | |||
@@ -44,6 +44,8 @@ | |||
44 | #define c_sleep(x) usleep(1000*x) | 44 | #define c_sleep(x) usleep(1000*x) |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | static Messenger *m; | ||
48 | |||
47 | uint8_t *parent_id = NULL; | 49 | uint8_t *parent_id = NULL; |
48 | uint8_t *child_id = NULL; | 50 | uint8_t *child_id = NULL; |
49 | 51 | ||
@@ -60,16 +62,16 @@ void do_tox(void) | |||
60 | dht_on = 0; | 62 | dht_on = 0; |
61 | } | 63 | } |
62 | 64 | ||
63 | doMessenger(); | 65 | doMessenger(m); |
64 | } | 66 | } |
65 | 67 | ||
66 | void parent_confirm_message(int num, uint8_t *data, uint16_t length) | 68 | void parent_confirm_message(Messenger *m, int num, uint8_t *data, uint16_t length) |
67 | { | 69 | { |
68 | puts("OK"); | 70 | puts("OK"); |
69 | request_flags |= SECOND_FLAG; | 71 | request_flags |= SECOND_FLAG; |
70 | } | 72 | } |
71 | 73 | ||
72 | void parent_confirm_status(int num, uint8_t *data, uint16_t length) | 74 | void parent_confirm_status(Messenger *m, int num, uint8_t *data, uint16_t length) |
73 | { | 75 | { |
74 | puts("OK"); | 76 | puts("OK"); |
75 | request_flags |= FIRST_FLAG; | 77 | request_flags |= FIRST_FLAG; |
@@ -84,7 +86,7 @@ int parent_friend_request(void) | |||
84 | fputs("Sending child request.", stdout); | 86 | fputs("Sending child request.", stdout); |
85 | fflush(stdout); | 87 | fflush(stdout); |
86 | 88 | ||
87 | m_addfriend(child_id, (uint8_t *)message, len); | 89 | m_addfriend(m, child_id, (uint8_t *)message, len); |
88 | 90 | ||
89 | /* wait on the status change */ | 91 | /* wait on the status change */ |
90 | for(i = 0; i < WAIT_COUNT; i++) { | 92 | for(i = 0; i < WAIT_COUNT; i++) { |
@@ -110,11 +112,11 @@ void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length) | |||
110 | { | 112 | { |
111 | fputs("OK\nsending status to parent", stdout); | 113 | fputs("OK\nsending status to parent", stdout); |
112 | fflush(stdout); | 114 | fflush(stdout); |
113 | m_addfriend_norequest(public_key); | 115 | m_addfriend_norequest(m, public_key); |
114 | request_flags |= FIRST_FLAG; | 116 | request_flags |= FIRST_FLAG; |
115 | } | 117 | } |
116 | 118 | ||
117 | void child_got_statuschange(int friend_num, uint8_t *string, uint16_t length) | 119 | void child_got_statuschange(Messenger *m, int friend_num, uint8_t *string, uint16_t length) |
118 | { | 120 | { |
119 | request_flags |= SECOND_FLAG; | 121 | request_flags |= SECOND_FLAG; |
120 | } | 122 | } |
@@ -168,12 +170,13 @@ int main(int argc, char *argv[]) | |||
168 | int i = 0; | 170 | int i = 0; |
169 | char *message = "Y-yes Mr. Watson?"; | 171 | char *message = "Y-yes Mr. Watson?"; |
170 | 172 | ||
171 | initMessenger(); | 173 | m = initMessenger(); |
172 | Messenger_save(child_id); | 174 | |
175 | Messenger_save(m, child_id); | ||
173 | msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); | 176 | msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); |
174 | 177 | ||
175 | m_callback_friendrequest(child_got_request); | 178 | m_callback_friendrequest(m, child_got_request); |
176 | m_callback_statusmessage(child_got_statuschange); | 179 | m_callback_statusmessage(m, child_got_statuschange); |
177 | 180 | ||
178 | /* wait on the friend request */ | 181 | /* wait on the friend request */ |
179 | while(!(request_flags & FIRST_FLAG)) | 182 | while(!(request_flags & FIRST_FLAG)) |
@@ -185,10 +188,12 @@ int main(int argc, char *argv[]) | |||
185 | 188 | ||
186 | for(i = 0; i < 6; i++) { | 189 | for(i = 0; i < 6; i++) { |
187 | /* send the message six times, just to be sure */ | 190 | /* send the message six times, just to be sure */ |
188 | m_sendmessage(0, (uint8_t *)message, strlen(message)); | 191 | m_sendmessage(m, 0, (uint8_t *)message, strlen(message)); |
189 | do_tox(); | 192 | do_tox(); |
190 | } | 193 | } |
191 | 194 | ||
195 | cleanupMessenger(m); | ||
196 | |||
192 | return 0; | 197 | return 0; |
193 | } | 198 | } |
194 | 199 | ||
@@ -199,15 +204,16 @@ int main(int argc, char *argv[]) | |||
199 | return -1; | 204 | return -1; |
200 | } | 205 | } |
201 | 206 | ||
207 | m = initMessenger(); | ||
208 | |||
202 | msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); | 209 | msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); |
203 | m_callback_statusmessage(parent_confirm_status); | 210 | m_callback_statusmessage(m, parent_confirm_status); |
204 | m_callback_friendmessage(parent_confirm_message); | 211 | m_callback_friendmessage(m, parent_confirm_message); |
205 | 212 | ||
206 | /* hacky way to give the child time to set up */ | 213 | /* hacky way to give the child time to set up */ |
207 | c_sleep(50); | 214 | c_sleep(50); |
208 | 215 | ||
209 | initMessenger(); | 216 | Messenger_save(m, parent_id); |
210 | Messenger_save(parent_id); | ||
211 | 217 | ||
212 | if(parent_friend_request() == -1) | 218 | if(parent_friend_request() == -1) |
213 | return -1; | 219 | return -1; |
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index af902083..64b44d5f 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c | |||
@@ -34,6 +34,8 @@ unsigned char *bad_id = NULL; | |||
34 | 34 | ||
35 | int friend_id_num = 0; | 35 | int friend_id_num = 0; |
36 | 36 | ||
37 | Messenger *m; | ||
38 | |||
37 | unsigned char * hex_string_to_bin(char hex_string[]) | 39 | unsigned char * hex_string_to_bin(char hex_string[]) |
38 | { | 40 | { |
39 | size_t len = strlen(hex_string); | 41 | size_t len = strlen(hex_string); |
@@ -52,22 +54,22 @@ START_TEST(test_m_sendmesage) | |||
52 | int bad_len = MAX_DATA_SIZE; | 54 | int bad_len = MAX_DATA_SIZE; |
53 | 55 | ||
54 | 56 | ||
55 | ck_assert(m_sendmessage(-1, (uint8_t *)message, good_len) == 0); | 57 | ck_assert(m_sendmessage(m, -1, (uint8_t *)message, good_len) == 0); |
56 | ck_assert(m_sendmessage(REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0); | 58 | ck_assert(m_sendmessage(m, REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0); |
57 | ck_assert(m_sendmessage(17, (uint8_t *)message, good_len) == 0); | 59 | ck_assert(m_sendmessage(m, 17, (uint8_t *)message, good_len) == 0); |
58 | ck_assert(m_sendmessage(friend_id_num, (uint8_t *)message, bad_len) == 0); | 60 | ck_assert(m_sendmessage(m, friend_id_num, (uint8_t *)message, bad_len) == 0); |
59 | } | 61 | } |
60 | END_TEST | 62 | END_TEST |
61 | 63 | ||
62 | START_TEST(test_m_get_userstatus_size) | 64 | START_TEST(test_m_get_userstatus_size) |
63 | { | 65 | { |
64 | int rc = 0; | 66 | int rc = 0; |
65 | ck_assert_msg((m_get_statusmessage_size(-1) == -1), | 67 | ck_assert_msg((m_get_statusmessage_size(m, -1) == -1), |
66 | "m_get_statusmessage_size did NOT catch an argument of -1"); | 68 | "m_get_statusmessage_size did NOT catch an argument of -1"); |
67 | ck_assert_msg((m_get_statusmessage_size(REALLY_BIG_NUMBER) == -1), | 69 | ck_assert_msg((m_get_statusmessage_size(m, REALLY_BIG_NUMBER) == -1), |
68 | "m_get_statusmessage_size did NOT catch the following argument: %d\n", | 70 | "m_get_statusmessage_size did NOT catch the following argument: %d\n", |
69 | REALLY_BIG_NUMBER); | 71 | REALLY_BIG_NUMBER); |
70 | rc = m_get_statusmessage_size(friend_id_num); | 72 | rc = m_get_statusmessage_size(m, friend_id_num); |
71 | 73 | ||
72 | /* this WILL error if the original m_addfriend_norequest() failed */ | 74 | /* this WILL error if the original m_addfriend_norequest() failed */ |
73 | ck_assert_msg((rc > 0 && rc <= MAX_STATUSMESSAGE_LENGTH), | 75 | ck_assert_msg((rc > 0 && rc <= MAX_STATUSMESSAGE_LENGTH), |
@@ -83,11 +85,11 @@ START_TEST(test_m_set_userstatus) | |||
83 | uint16_t good_length = strlen(status); | 85 | uint16_t good_length = strlen(status); |
84 | uint16_t bad_length = REALLY_BIG_NUMBER; | 86 | uint16_t bad_length = REALLY_BIG_NUMBER; |
85 | 87 | ||
86 | if(m_set_statusmessage((uint8_t *)status, bad_length) != -1) | 88 | if(m_set_statusmessage(m, (uint8_t *)status, bad_length) != -1) |
87 | ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n", | 89 | ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n", |
88 | REALLY_BIG_NUMBER); | 90 | REALLY_BIG_NUMBER); |
89 | 91 | ||
90 | if((m_set_statusmessage((uint8_t *)status, good_length)) != 0) | 92 | if((m_set_statusmessage(m, (uint8_t *)status, good_length)) != 0) |
91 | ck_abort_msg("m_set_userstatus did NOT return 0 on the following length: %d\n" | 93 | ck_abort_msg("m_set_userstatus did NOT return 0 on the following length: %d\n" |
92 | "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH); | 94 | "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH); |
93 | } | 95 | } |
@@ -95,9 +97,9 @@ END_TEST | |||
95 | 97 | ||
96 | START_TEST(test_m_friendstatus) | 98 | START_TEST(test_m_friendstatus) |
97 | { | 99 | { |
98 | ck_assert_msg((m_friendstatus(-1) == NOFRIEND), | 100 | ck_assert_msg((m_friendstatus(m, -1) == NOFRIEND), |
99 | "m_friendstatus did NOT catch an argument of -1.\n"); | 101 | "m_friendstatus did NOT catch an argument of -1.\n"); |
100 | ck_assert_msg((m_friendstatus(REALLY_BIG_NUMBER) == NOFRIEND), | 102 | ck_assert_msg((m_friendstatus(m, REALLY_BIG_NUMBER) == NOFRIEND), |
101 | "m_friendstatus did NOT catch an argument of %d.\n", | 103 | "m_friendstatus did NOT catch an argument of %d.\n", |
102 | REALLY_BIG_NUMBER); | 104 | REALLY_BIG_NUMBER); |
103 | } | 105 | } |
@@ -105,9 +107,9 @@ END_TEST | |||
105 | 107 | ||
106 | START_TEST(test_m_delfriend) | 108 | START_TEST(test_m_delfriend) |
107 | { | 109 | { |
108 | ck_assert_msg((m_delfriend(-1) == -1), | 110 | ck_assert_msg((m_delfriend(m, -1) == -1), |
109 | "m_delfriend did NOT catch an argument of -1\n"); | 111 | "m_delfriend did NOT catch an argument of -1\n"); |
110 | ck_assert_msg((m_delfriend(REALLY_BIG_NUMBER) == -1), | 112 | ck_assert_msg((m_delfriend(m, REALLY_BIG_NUMBER) == -1), |
111 | "m_delfriend did NOT catch the following number: %d\n", | 113 | "m_delfriend did NOT catch the following number: %d\n", |
112 | REALLY_BIG_NUMBER); | 114 | REALLY_BIG_NUMBER); |
113 | } | 115 | } |
@@ -124,16 +126,16 @@ START_TEST(test_m_addfriend) | |||
124 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES | 126 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES |
125 | + crypto_box_ZEROBYTES + 100); | 127 | + crypto_box_ZEROBYTES + 100); |
126 | 128 | ||
127 | if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG) | 129 | if(m_addfriend(m, (uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG) |
128 | ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len); | 130 | ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len); |
129 | 131 | ||
130 | /* this will error if the original m_addfriend_norequest() failed */ | 132 | /* this will error if the original m_addfriend_norequest() failed */ |
131 | if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT) | 133 | if(m_addfriend(m, (uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT) |
132 | ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n" | 134 | ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n" |
133 | "(this can be caused by the error of m_addfriend_norequest in" | 135 | "(this can be caused by the error of m_addfriend_norequest in" |
134 | " the beginning of the suite)\n"); | 136 | " the beginning of the suite)\n"); |
135 | 137 | ||
136 | if(m_addfriend((uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE) | 138 | if(m_addfriend(m, (uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE) |
137 | ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); | 139 | ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); |
138 | 140 | ||
139 | /* this should REALLY error */ | 141 | /* this should REALLY error */ |
@@ -152,10 +154,10 @@ START_TEST(test_setname) | |||
152 | int good_length = strlen(good_name); | 154 | int good_length = strlen(good_name); |
153 | int bad_length = REALLY_BIG_NUMBER; | 155 | int bad_length = REALLY_BIG_NUMBER; |
154 | 156 | ||
155 | if(setname((uint8_t *)good_name, bad_length) != -1) | 157 | if(setname(m, (uint8_t *)good_name, bad_length) != -1) |
156 | ck_abort_msg("setname() did NOT error on %d as a length argument!\n", | 158 | ck_abort_msg("setname() did NOT error on %d as a length argument!\n", |
157 | bad_length); | 159 | bad_length); |
158 | if(setname((uint8_t *)good_name, good_length) != 0) | 160 | if(setname(m, (uint8_t *)good_name, good_length) != 0) |
159 | ck_abort_msg("setname() did NOT return 0 on good arguments!\n"); | 161 | ck_abort_msg("setname() did NOT return 0 on good arguments!\n"); |
160 | } | 162 | } |
161 | END_TEST | 163 | END_TEST |
@@ -166,8 +168,8 @@ START_TEST(test_getself_name) | |||
166 | int len = strlen(nickname); | 168 | int len = strlen(nickname); |
167 | char nick_check[len]; | 169 | char nick_check[len]; |
168 | 170 | ||
169 | setname((uint8_t *)nickname, len); | 171 | setname(m, (uint8_t *)nickname, len); |
170 | getself_name((uint8_t *)nick_check); | 172 | getself_name(m, (uint8_t *)nick_check); |
171 | 173 | ||
172 | ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)), | 174 | ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)), |
173 | "getself_name failed to return the known name!\n" | 175 | "getself_name failed to return the known name!\n" |
@@ -256,13 +258,15 @@ int main(int argc, char *argv[]) | |||
256 | good_id_b = hex_string_to_bin(good_id_b_str); | 258 | good_id_b = hex_string_to_bin(good_id_b_str); |
257 | bad_id = hex_string_to_bin(bad_id_str); | 259 | bad_id = hex_string_to_bin(bad_id_str); |
258 | 260 | ||
261 | m = initMessenger(); | ||
262 | |||
259 | /* setup a default friend and friendnum */ | 263 | /* setup a default friend and friendnum */ |
260 | if(m_addfriend_norequest((uint8_t *)friend_id) < 0) | 264 | if(m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) |
261 | fputs("m_addfriend_norequest() failed on a valid ID!\n" | 265 | fputs("m_addfriend_norequest() failed on a valid ID!\n" |
262 | "this was CRITICAL to the test, and the build WILL fail.\n" | 266 | "this was CRITICAL to the test, and the build WILL fail.\n" |
263 | "the tests will continue now...\n\n", stderr); | 267 | "the tests will continue now...\n\n", stderr); |
264 | 268 | ||
265 | if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0) | 269 | if((friend_id_num = getfriend_id(m, (uint8_t *)friend_id)) < 0) |
266 | fputs("getfriend_id() failed on a valid ID!\n" | 270 | fputs("getfriend_id() failed on a valid ID!\n" |
267 | "this was CRITICAL to the test, and the build WILL fail.\n" | 271 | "this was CRITICAL to the test, and the build WILL fail.\n" |
268 | "the tests will continue now...\n\n", stderr); | 272 | "the tests will continue now...\n\n", stderr); |
@@ -276,5 +280,7 @@ int main(int argc, char *argv[]) | |||
276 | free(good_id_b); | 280 | free(good_id_b); |
277 | free(bad_id); | 281 | free(bad_id); |
278 | 282 | ||
283 | cleanupMessenger(m); | ||
284 | |||
279 | return number_failed; | 285 | return number_failed; |
280 | } | 286 | } |
diff --git a/core/Messenger.c b/core/Messenger.c index 92c83b9b..a6189941 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -24,40 +24,8 @@ | |||
24 | #include "Messenger.h" | 24 | #include "Messenger.h" |
25 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 25 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
26 | 26 | ||
27 | typedef struct { | 27 | static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); |
28 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); |
29 | int crypt_connection_id; | ||
30 | uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | ||
31 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ | ||
32 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ | ||
33 | uint8_t name[MAX_NAME_LENGTH]; | ||
34 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ | ||
35 | uint8_t *statusmessage; | ||
36 | uint16_t statusmessage_length; | ||
37 | uint8_t statusmessage_sent; | ||
38 | USERSTATUS userstatus; | ||
39 | uint8_t userstatus_sent; | ||
40 | uint16_t info_size; /* length of the info */ | ||
41 | uint32_t message_id; /* a semi-unique id used in read receipts */ | ||
42 | uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ | ||
43 | } Friend; | ||
44 | |||
45 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
46 | |||
47 | static uint8_t self_name[MAX_NAME_LENGTH]; | ||
48 | static uint16_t self_name_length; | ||
49 | |||
50 | static uint8_t self_statusmessage[MAX_STATUSMESSAGE_LENGTH]; | ||
51 | static uint16_t self_statusmessage_length; | ||
52 | |||
53 | static USERSTATUS self_userstatus; | ||
54 | |||
55 | static Friend *friendlist; | ||
56 | static uint32_t numfriends; | ||
57 | |||
58 | |||
59 | static void set_friend_status(int friendnumber, uint8_t status); | ||
60 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); | ||
61 | 29 | ||
62 | /* 1 if we are online | 30 | /* 1 if we are online |
63 | 0 if we are offline | 31 | 0 if we are offline |
@@ -65,24 +33,24 @@ static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *da | |||
65 | 33 | ||
66 | /* set the size of the friend list to numfriends | 34 | /* set the size of the friend list to numfriends |
67 | return -1 if realloc fails */ | 35 | return -1 if realloc fails */ |
68 | int realloc_friendlist(uint32_t num) { | 36 | int realloc_friendlist(Messenger *m, uint32_t num) { |
69 | Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend)); | 37 | Friend *newfriendlist = realloc(m->friendlist, num*sizeof(Friend)); |
70 | if (newfriendlist == NULL) | 38 | if (newfriendlist == NULL) |
71 | return -1; | 39 | return -1; |
72 | memset(&newfriendlist[num-1], 0, sizeof(Friend)); | 40 | memset(&newfriendlist[num-1], 0, sizeof(Friend)); |
73 | friendlist = newfriendlist; | 41 | m->friendlist = newfriendlist; |
74 | return 0; | 42 | return 0; |
75 | } | 43 | } |
76 | 44 | ||
77 | /* return the friend id associated to that public key. | 45 | /* return the friend id associated to that public key. |
78 | return -1 if no such friend */ | 46 | return -1 if no such friend */ |
79 | int getfriend_id(uint8_t *client_id) | 47 | int getfriend_id(Messenger *m, uint8_t *client_id) |
80 | { | 48 | { |
81 | uint32_t i; | 49 | uint32_t i; |
82 | 50 | ||
83 | for (i = 0; i < numfriends; ++i) { | 51 | for (i = 0; i < m->numfriends; ++i) { |
84 | if (friendlist[i].status > 0) | 52 | if (m->friendlist[i].status > 0) |
85 | if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 53 | if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
86 | return i; | 54 | return i; |
87 | } | 55 | } |
88 | 56 | ||
@@ -93,13 +61,13 @@ int getfriend_id(uint8_t *client_id) | |||
93 | make sure that client_id is of size CLIENT_ID_SIZE. | 61 | make sure that client_id is of size CLIENT_ID_SIZE. |
94 | return 0 if success | 62 | return 0 if success |
95 | return -1 if failure. */ | 63 | return -1 if failure. */ |
96 | int getclient_id(int friend_id, uint8_t *client_id) | 64 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) |
97 | { | 65 | { |
98 | if (friend_id >= numfriends || friend_id < 0) | 66 | if (friend_id >= m->numfriends || friend_id < 0) |
99 | return -1; | 67 | return -1; |
100 | 68 | ||
101 | if (friendlist[friend_id].status > 0) { | 69 | if (m->friendlist[friend_id].status > 0) { |
102 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 70 | memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
103 | return 0; | 71 | return 0; |
104 | } | 72 | } |
105 | 73 | ||
@@ -118,7 +86,7 @@ int getclient_id(int friend_id, uint8_t *client_id) | |||
118 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 86 | * return FAERR_ALREADYSENT if friend request already sent or already a friend |
119 | * return FAERR_UNKNOWN for unknown error | 87 | * return FAERR_UNKNOWN for unknown error |
120 | */ | 88 | */ |
121 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | 89 | int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length) |
122 | { | 90 | { |
123 | if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES | 91 | if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES |
124 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES | 92 | - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES |
@@ -128,57 +96,57 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
128 | return FAERR_NOMESSAGE; | 96 | return FAERR_NOMESSAGE; |
129 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 97 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
130 | return FAERR_OWNKEY; | 98 | return FAERR_OWNKEY; |
131 | if (getfriend_id(client_id) != -1) | 99 | if (getfriend_id(m, client_id) != -1) |
132 | return FAERR_ALREADYSENT; | 100 | return FAERR_ALREADYSENT; |
133 | 101 | ||
134 | /* resize the friend list if necessary */ | 102 | /* resize the friend list if necessary */ |
135 | realloc_friendlist(numfriends + 1); | 103 | realloc_friendlist(m, m->numfriends + 1); |
136 | 104 | ||
137 | uint32_t i; | 105 | uint32_t i; |
138 | for (i = 0; i <= numfriends; ++i) { | 106 | for (i = 0; i <= m->numfriends; ++i) { |
139 | if (friendlist[i].status == NOFRIEND) { | 107 | if (m->friendlist[i].status == NOFRIEND) { |
140 | DHT_addfriend(client_id); | 108 | DHT_addfriend(client_id); |
141 | friendlist[i].status = FRIEND_ADDED; | 109 | m->friendlist[i].status = FRIEND_ADDED; |
142 | friendlist[i].crypt_connection_id = -1; | 110 | m->friendlist[i].crypt_connection_id = -1; |
143 | friendlist[i].friend_request_id = -1; | 111 | m->friendlist[i].friend_request_id = -1; |
144 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 112 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
145 | friendlist[i].statusmessage = calloc(1, 1); | 113 | m->friendlist[i].statusmessage = calloc(1, 1); |
146 | friendlist[i].statusmessage_length = 1; | 114 | m->friendlist[i].statusmessage_length = 1; |
147 | friendlist[i].userstatus = USERSTATUS_NONE; | 115 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
148 | memcpy(friendlist[i].info, data, length); | 116 | memcpy(m->friendlist[i].info, data, length); |
149 | friendlist[i].info_size = length; | 117 | m->friendlist[i].info_size = length; |
150 | friendlist[i].message_id = 0; | 118 | m->friendlist[i].message_id = 0; |
151 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 119 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
152 | 120 | ||
153 | ++numfriends; | 121 | ++ m->numfriends; |
154 | return i; | 122 | return i; |
155 | } | 123 | } |
156 | } | 124 | } |
157 | return FAERR_UNKNOWN; | 125 | return FAERR_UNKNOWN; |
158 | } | 126 | } |
159 | 127 | ||
160 | int m_addfriend_norequest(uint8_t * client_id) | 128 | int m_addfriend_norequest(Messenger *m, uint8_t * client_id) |
161 | { | 129 | { |
162 | if (getfriend_id(client_id) != -1) | 130 | if (getfriend_id(m, client_id) != -1) |
163 | return -1; | 131 | return -1; |
164 | 132 | ||
165 | /* resize the friend list if necessary */ | 133 | /* resize the friend list if necessary */ |
166 | realloc_friendlist(numfriends + 1); | 134 | realloc_friendlist(m, m->numfriends + 1); |
167 | 135 | ||
168 | uint32_t i; | 136 | uint32_t i; |
169 | for (i = 0; i <= numfriends; ++i) { | 137 | for (i = 0; i <= m->numfriends; ++i) { |
170 | if(friendlist[i].status == NOFRIEND) { | 138 | if(m->friendlist[i].status == NOFRIEND) { |
171 | DHT_addfriend(client_id); | 139 | DHT_addfriend(client_id); |
172 | friendlist[i].status = FRIEND_REQUESTED; | 140 | m->friendlist[i].status = FRIEND_REQUESTED; |
173 | friendlist[i].crypt_connection_id = -1; | 141 | m->friendlist[i].crypt_connection_id = -1; |
174 | friendlist[i].friend_request_id = -1; | 142 | m->friendlist[i].friend_request_id = -1; |
175 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 143 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
176 | friendlist[i].statusmessage = calloc(1, 1); | 144 | m->friendlist[i].statusmessage = calloc(1, 1); |
177 | friendlist[i].statusmessage_length = 1; | 145 | m->friendlist[i].statusmessage_length = 1; |
178 | friendlist[i].userstatus = USERSTATUS_NONE; | 146 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
179 | friendlist[i].message_id = 0; | 147 | m->friendlist[i].message_id = 0; |
180 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 148 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
181 | ++numfriends; | 149 | ++ m->numfriends; |
182 | return i; | 150 | return i; |
183 | } | 151 | } |
184 | } | 152 | } |
@@ -188,23 +156,23 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
188 | /* remove a friend | 156 | /* remove a friend |
189 | return 0 if success | 157 | return 0 if success |
190 | return -1 if failure */ | 158 | return -1 if failure */ |
191 | int m_delfriend(int friendnumber) | 159 | int m_delfriend(Messenger *m, int friendnumber) |
192 | { | 160 | { |
193 | if (friendnumber >= numfriends || friendnumber < 0) | 161 | if (friendnumber >= m->numfriends || friendnumber < 0) |
194 | return -1; | 162 | return -1; |
195 | 163 | ||
196 | DHT_delfriend(friendlist[friendnumber].client_id); | 164 | DHT_delfriend(m->friendlist[friendnumber].client_id); |
197 | crypto_kill(friendlist[friendnumber].crypt_connection_id); | 165 | crypto_kill(m->friendlist[friendnumber].crypt_connection_id); |
198 | free(friendlist[friendnumber].statusmessage); | 166 | free(m->friendlist[friendnumber].statusmessage); |
199 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 167 | memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend)); |
200 | uint32_t i; | 168 | uint32_t i; |
201 | 169 | ||
202 | for (i = numfriends; i != 0; --i) { | 170 | for (i = m->numfriends; i != 0; --i) { |
203 | if (friendlist[i-1].status != NOFRIEND) | 171 | if (m->friendlist[i-1].status != NOFRIEND) |
204 | break; | 172 | break; |
205 | } | 173 | } |
206 | numfriends = i; | 174 | m->numfriends = i; |
207 | realloc_friendlist(numfriends + 1); | 175 | realloc_friendlist(m, m->numfriends + 1); |
208 | 176 | ||
209 | return 0; | 177 | return 0; |
210 | } | 178 | } |
@@ -214,31 +182,31 @@ int m_delfriend(int friendnumber) | |||
214 | return FRIEND_REQUESTED if the friend request was sent | 182 | return FRIEND_REQUESTED if the friend request was sent |
215 | return FRIEND_ADDED if the friend was added | 183 | return FRIEND_ADDED if the friend was added |
216 | return NOFRIEND if there is no friend with that number */ | 184 | return NOFRIEND if there is no friend with that number */ |
217 | int m_friendstatus(int friendnumber) | 185 | int m_friendstatus(Messenger *m, int friendnumber) |
218 | { | 186 | { |
219 | if (friendnumber < 0 || friendnumber >= numfriends) | 187 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
220 | return NOFRIEND; | 188 | return NOFRIEND; |
221 | return friendlist[friendnumber].status; | 189 | return m->friendlist[friendnumber].status; |
222 | } | 190 | } |
223 | 191 | ||
224 | /* send a text chat message to an online friend | 192 | /* send a text chat message to an online friend |
225 | return the message id if packet was successfully put into the send queue | 193 | return the message id if packet was successfully put into the send queue |
226 | return 0 if it was not */ | 194 | return 0 if it was not */ |
227 | uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | 195 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) |
228 | { | 196 | { |
229 | if (friendnumber < 0 || friendnumber >= numfriends) | 197 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
230 | return 0; | 198 | return 0; |
231 | uint32_t msgid = ++friendlist[friendnumber].message_id; | 199 | uint32_t msgid = ++m->friendlist[friendnumber].message_id; |
232 | if (msgid == 0) | 200 | if (msgid == 0) |
233 | msgid = 1; /* otherwise, false error */ | 201 | msgid = 1; /* otherwise, false error */ |
234 | if(m_sendmessage_withid(friendnumber, msgid, message, length)) { | 202 | if(m_sendmessage_withid(m, friendnumber, msgid, message, length)) { |
235 | return msgid; | 203 | return msgid; |
236 | } | 204 | } |
237 | 205 | ||
238 | return 0; | 206 | return 0; |
239 | } | 207 | } |
240 | 208 | ||
241 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | 209 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) |
242 | { | 210 | { |
243 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) | 211 | if (length >= (MAX_DATA_SIZE - sizeof(theid))) |
244 | return 0; | 212 | return 0; |
@@ -246,34 +214,34 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message | |||
246 | theid = htonl(theid); | 214 | theid = htonl(theid); |
247 | memcpy(temp, &theid, sizeof(theid)); | 215 | memcpy(temp, &theid, sizeof(theid)); |
248 | memcpy(temp + sizeof(theid), message, length); | 216 | memcpy(temp + sizeof(theid), message, length); |
249 | return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); | 217 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); |
250 | } | 218 | } |
251 | 219 | ||
252 | /* send an action to an online friend | 220 | /* send an action to an online friend |
253 | return 1 if packet was successfully put into the send queue | 221 | return 1 if packet was successfully put into the send queue |
254 | return 0 if it was not */ | 222 | return 0 if it was not */ |
255 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) | 223 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) |
256 | { | 224 | { |
257 | return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length); | 225 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); |
258 | } | 226 | } |
259 | 227 | ||
260 | /* send a name packet to friendnumber | 228 | /* send a name packet to friendnumber |
261 | length is the length with the NULL terminator*/ | 229 | length is the length with the NULL terminator*/ |
262 | static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) | 230 | static int m_sendname(Messenger *m, int friendnumber, uint8_t * name, uint16_t length) |
263 | { | 231 | { |
264 | if(length > MAX_NAME_LENGTH || length == 0) | 232 | if(length > MAX_NAME_LENGTH || length == 0) |
265 | return 0; | 233 | return 0; |
266 | return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length); | 234 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); |
267 | } | 235 | } |
268 | 236 | ||
269 | /* set the name of a friend | 237 | /* set the name of a friend |
270 | return 0 if success | 238 | return 0 if success |
271 | return -1 if failure */ | 239 | return -1 if failure */ |
272 | static int setfriendname(int friendnumber, uint8_t * name) | 240 | static int setfriendname(Messenger *m, int friendnumber, uint8_t * name) |
273 | { | 241 | { |
274 | if (friendnumber >= numfriends || friendnumber < 0) | 242 | if (friendnumber >= m->numfriends || friendnumber < 0) |
275 | return -1; | 243 | return -1; |
276 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 244 | memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
277 | return 0; | 245 | return 0; |
278 | } | 246 | } |
279 | 247 | ||
@@ -283,15 +251,15 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
283 | length is the length of name with the NULL terminator | 251 | length is the length of name with the NULL terminator |
284 | return 0 if success | 252 | return 0 if success |
285 | return -1 if failure */ | 253 | return -1 if failure */ |
286 | int setname(uint8_t * name, uint16_t length) | 254 | int setname(Messenger *m, uint8_t * name, uint16_t length) |
287 | { | 255 | { |
288 | if (length > MAX_NAME_LENGTH || length == 0) | 256 | if (length > MAX_NAME_LENGTH || length == 0) |
289 | return -1; | 257 | return -1; |
290 | memcpy(self_name, name, length); | 258 | memcpy(m->name, name, length); |
291 | self_name_length = length; | 259 | m->name_length = length; |
292 | uint32_t i; | 260 | uint32_t i; |
293 | for (i = 0; i < numfriends; ++i) | 261 | for (i = 0; i < m->numfriends; ++i) |
294 | friendlist[i].name_sent = 0; | 262 | m->friendlist[i].name_sent = 0; |
295 | return 0; | 263 | return 0; |
296 | } | 264 | } |
297 | 265 | ||
@@ -299,10 +267,10 @@ int setname(uint8_t * name, uint16_t length) | |||
299 | put it in name | 267 | put it in name |
300 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 268 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
301 | return the length of the name */ | 269 | return the length of the name */ |
302 | uint16_t getself_name(uint8_t *name) | 270 | uint16_t getself_name(Messenger *m, uint8_t *name) |
303 | { | 271 | { |
304 | memcpy(name, self_name, self_name_length); | 272 | memcpy(name, m->name, m->name_length); |
305 | return self_name_length; | 273 | return m->name_length; |
306 | } | 274 | } |
307 | 275 | ||
308 | /* get name of friendnumber | 276 | /* get name of friendnumber |
@@ -310,293 +278,288 @@ uint16_t getself_name(uint8_t *name) | |||
310 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 278 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
311 | return 0 if success | 279 | return 0 if success |
312 | return -1 if failure */ | 280 | return -1 if failure */ |
313 | int getname(int friendnumber, uint8_t * name) | 281 | int getname(Messenger *m, int friendnumber, uint8_t * name) |
314 | { | 282 | { |
315 | if (friendnumber >= numfriends || friendnumber < 0) | 283 | if (friendnumber >= m->numfriends || friendnumber < 0) |
316 | return -1; | 284 | return -1; |
317 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 285 | memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); |
318 | return 0; | 286 | return 0; |
319 | } | 287 | } |
320 | 288 | ||
321 | int m_set_statusmessage(uint8_t *status, uint16_t length) | 289 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) |
322 | { | 290 | { |
323 | if (length > MAX_STATUSMESSAGE_LENGTH) | 291 | if (length > MAX_STATUSMESSAGE_LENGTH) |
324 | return -1; | 292 | return -1; |
325 | memcpy(self_statusmessage, status, length); | 293 | memcpy(m->statusmessage, status, length); |
326 | self_statusmessage_length = length; | 294 | m->statusmessage_length = length; |
327 | 295 | ||
328 | uint32_t i; | 296 | uint32_t i; |
329 | for (i = 0; i < numfriends; ++i) | 297 | for (i = 0; i < m->numfriends; ++i) |
330 | friendlist[i].statusmessage_sent = 0; | 298 | m->friendlist[i].statusmessage_sent = 0; |
331 | return 0; | 299 | return 0; |
332 | } | 300 | } |
333 | 301 | ||
334 | int m_set_userstatus(USERSTATUS status) | 302 | int m_set_userstatus(Messenger *m, USERSTATUS status) |
335 | { | 303 | { |
336 | if (status >= USERSTATUS_INVALID) { | 304 | if (status >= USERSTATUS_INVALID) { |
337 | return -1; | 305 | return -1; |
338 | } | 306 | } |
339 | self_userstatus = status; | 307 | m->userstatus = status; |
340 | uint32_t i; | 308 | uint32_t i; |
341 | for (i = 0; i < numfriends; ++i) | 309 | for (i = 0; i < m->numfriends; ++i) |
342 | friendlist[i].userstatus_sent = 0; | 310 | m->friendlist[i].userstatus_sent = 0; |
343 | return 0; | 311 | return 0; |
344 | } | 312 | } |
345 | 313 | ||
346 | /* return the size of friendnumber's user status | 314 | /* return the size of friendnumber's user status |
347 | guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ | 315 | guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ |
348 | int m_get_statusmessage_size(int friendnumber) | 316 | int m_get_statusmessage_size(Messenger *m, int friendnumber) |
349 | { | 317 | { |
350 | if (friendnumber >= numfriends || friendnumber < 0) | 318 | if (friendnumber >= m->numfriends || friendnumber < 0) |
351 | return -1; | 319 | return -1; |
352 | return friendlist[friendnumber].statusmessage_length; | 320 | return m->friendlist[friendnumber].statusmessage_length; |
353 | } | 321 | } |
354 | 322 | ||
355 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen | 323 | /* copy the user status of friendnumber into buf, truncating if needed to maxlen |
356 | bytes, use m_get_statusmessage_size to find out how much you need to allocate */ | 324 | bytes, use m_get_statusmessage_size to find out how much you need to allocate */ |
357 | int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen) | 325 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t * buf, uint32_t maxlen) |
358 | { | 326 | { |
359 | if (friendnumber >= numfriends || friendnumber < 0) | 327 | if (friendnumber >= m->numfriends || friendnumber < 0) |
360 | return -1; | 328 | return -1; |
361 | memset(buf, 0, maxlen); | 329 | memset(buf, 0, maxlen); |
362 | memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); | 330 | memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); |
363 | return 0; | 331 | return 0; |
364 | } | 332 | } |
365 | 333 | ||
366 | int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen) | 334 | int m_copy_self_statusmessage(Messenger *m, uint8_t * buf, uint32_t maxlen) |
367 | { | 335 | { |
368 | memset(buf, 0, maxlen); | 336 | memset(buf, 0, maxlen); |
369 | memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); | 337 | memcpy(buf, m->statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); |
370 | return 0; | 338 | return 0; |
371 | } | 339 | } |
372 | 340 | ||
373 | USERSTATUS m_get_userstatus(int friendnumber) | 341 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) |
374 | { | 342 | { |
375 | if (friendnumber >= numfriends || friendnumber < 0) | 343 | if (friendnumber >= m->numfriends || friendnumber < 0) |
376 | return USERSTATUS_INVALID; | 344 | return USERSTATUS_INVALID; |
377 | USERSTATUS status = friendlist[friendnumber].userstatus; | 345 | USERSTATUS status = m->friendlist[friendnumber].userstatus; |
378 | if (status >= USERSTATUS_INVALID) { | 346 | if (status >= USERSTATUS_INVALID) { |
379 | status = USERSTATUS_NONE; | 347 | status = USERSTATUS_NONE; |
380 | } | 348 | } |
381 | return status; | 349 | return status; |
382 | } | 350 | } |
383 | 351 | ||
384 | USERSTATUS m_get_self_userstatus(void) | 352 | USERSTATUS m_get_self_userstatus(Messenger *m) |
385 | { | 353 | { |
386 | return self_userstatus; | 354 | return m->userstatus; |
387 | } | 355 | } |
388 | 356 | ||
389 | static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 357 | static int send_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length) |
390 | { | 358 | { |
391 | return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length); | 359 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); |
392 | } | 360 | } |
393 | 361 | ||
394 | static int send_userstatus(int friendnumber, USERSTATUS status) | 362 | static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status) |
395 | { | 363 | { |
396 | uint8_t stat = status; | 364 | uint8_t stat = status; |
397 | return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); | 365 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); |
398 | } | 366 | } |
399 | 367 | ||
400 | static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) | 368 | static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length) |
401 | { | 369 | { |
402 | if (friendnumber >= numfriends || friendnumber < 0) | 370 | if (friendnumber >= m->numfriends || friendnumber < 0) |
403 | return -1; | 371 | return -1; |
404 | uint8_t *newstatus = calloc(length, 1); | 372 | uint8_t *newstatus = calloc(length, 1); |
405 | memcpy(newstatus, status, length); | 373 | memcpy(newstatus, status, length); |
406 | free(friendlist[friendnumber].statusmessage); | 374 | free(m->friendlist[friendnumber].statusmessage); |
407 | friendlist[friendnumber].statusmessage = newstatus; | 375 | m->friendlist[friendnumber].statusmessage = newstatus; |
408 | friendlist[friendnumber].statusmessage_length = length; | 376 | m->friendlist[friendnumber].statusmessage_length = length; |
409 | return 0; | 377 | return 0; |
410 | } | 378 | } |
411 | 379 | ||
412 | static void set_friend_userstatus(int friendnumber, USERSTATUS status) | 380 | static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status) |
413 | { | 381 | { |
414 | friendlist[friendnumber].userstatus = status; | 382 | m->friendlist[friendnumber].userstatus = status; |
415 | } | 383 | } |
416 | 384 | ||
417 | /* Sets whether we send read receipts for friendnumber. */ | 385 | /* Sets whether we send read receipts for friendnumber. */ |
418 | void m_set_sends_receipts(int friendnumber, int yesno) | 386 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) |
419 | { | 387 | { |
420 | if (yesno != 0 || yesno != 1) | 388 | if (yesno != 0 || yesno != 1) |
421 | return; | 389 | return; |
422 | if (friendnumber >= numfriends || friendnumber < 0) | 390 | if (friendnumber >= m->numfriends || friendnumber < 0) |
423 | return; | 391 | return; |
424 | friendlist[friendnumber].receives_read_receipts = yesno; | 392 | m->friendlist[friendnumber].receives_read_receipts = yesno; |
425 | } | 393 | } |
426 | 394 | ||
427 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 395 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
428 | static uint8_t friend_request_isset = 0; */ | 396 | static uint8_t friend_request_isset = 0; */ |
429 | /* set the function that will be executed when a friend request is received. */ | 397 | /* set the function that will be executed when a friend request is received. */ |
430 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 398 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)) |
431 | { | 399 | { |
432 | callback_friendrequest(function); | 400 | callback_friendrequest(function); |
433 | } | 401 | } |
434 | 402 | ||
435 | static void (*friend_message)(int, uint8_t *, uint16_t); | ||
436 | static uint8_t friend_message_isset = 0; | ||
437 | |||
438 | /* set the function that will be executed when a message from a friend is received. */ | 403 | /* set the function that will be executed when a message from a friend is received. */ |
439 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | 404 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
440 | { | 405 | { |
441 | friend_message = function; | 406 | m->friend_message = function; |
442 | friend_message_isset = 1; | 407 | m->friend_message_isset = 1; |
443 | } | 408 | } |
444 | 409 | ||
445 | static void (*friend_action)(int, uint8_t *, uint16_t); | 410 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
446 | static uint8_t friend_action_isset = 0; | ||
447 | void m_callback_action(void (*function)(int, uint8_t *, uint16_t)) | ||
448 | { | 411 | { |
449 | friend_action = function; | 412 | m->friend_action = function; |
450 | friend_action_isset = 1; | 413 | m->friend_action_isset = 1; |
451 | } | 414 | } |
452 | 415 | ||
453 | static void (*friend_namechange)(int, uint8_t *, uint16_t); | 416 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
454 | static uint8_t friend_namechange_isset = 0; | ||
455 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) | ||
456 | { | 417 | { |
457 | friend_namechange = function; | 418 | m->friend_namechange = function; |
458 | friend_namechange_isset = 1; | 419 | m->friend_namechange_isset = 1; |
459 | } | 420 | } |
460 | 421 | ||
461 | static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t); | 422 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)) |
462 | static uint8_t friend_statusmessagechange_isset = 0; | ||
463 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)) | ||
464 | { | 423 | { |
465 | friend_statusmessagechange = function; | 424 | m->friend_statusmessagechange = function; |
466 | friend_statusmessagechange_isset = 1; | 425 | m->friend_statusmessagechange_isset = 1; |
467 | } | 426 | } |
468 | 427 | ||
469 | static void (*friend_userstatuschange)(int, USERSTATUS); | 428 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)) |
470 | static uint8_t friend_userstatuschange_isset = 0; | ||
471 | void m_callback_userstatus(void (*function)(int, USERSTATUS)) | ||
472 | { | 429 | { |
473 | friend_userstatuschange = function; | 430 | m->friend_userstatuschange = function; |
474 | friend_userstatuschange_isset = 1; | 431 | m->friend_userstatuschange_isset = 1; |
475 | } | 432 | } |
476 | 433 | ||
477 | static void (*read_receipt)(int, uint32_t); | 434 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)) |
478 | static uint8_t read_receipt_isset = 0; | ||
479 | void m_callback_read_receipt(void (*function)(int, uint32_t)) | ||
480 | { | 435 | { |
481 | read_receipt = function; | 436 | m->read_receipt = function; |
482 | read_receipt_isset = 1; | 437 | m->read_receipt_isset = 1; |
483 | } | 438 | } |
484 | 439 | ||
485 | static void (*friend_connectionstatuschange)(int, uint8_t); | 440 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)) |
486 | static uint8_t friend_connectionstatuschange_isset = 0; | ||
487 | void m_callback_connectionstatus(void (*function)(int, uint8_t)) | ||
488 | { | 441 | { |
489 | friend_connectionstatuschange = function; | 442 | m->friend_connectionstatuschange = function; |
490 | friend_connectionstatuschange_isset = 1; | 443 | m->friend_connectionstatuschange_isset = 1; |
491 | } | 444 | } |
492 | 445 | ||
493 | static void check_friend_connectionstatus(int friendnumber, uint8_t status) | 446 | static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) |
494 | { | 447 | { |
495 | if (!friend_connectionstatuschange_isset) | 448 | if (!m->friend_connectionstatuschange_isset) |
496 | return; | 449 | return; |
497 | if (status == NOFRIEND) | 450 | if (status == NOFRIEND) |
498 | return; | 451 | return; |
499 | const uint8_t was_connected = friendlist[friendnumber].status == FRIEND_ONLINE; | 452 | const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE; |
500 | const uint8_t is_connected = status == FRIEND_ONLINE; | 453 | const uint8_t is_connected = status == FRIEND_ONLINE; |
501 | if (is_connected != was_connected) | 454 | if (is_connected != was_connected) |
502 | friend_connectionstatuschange(friendnumber, is_connected); | 455 | m->friend_connectionstatuschange(m, friendnumber, is_connected); |
503 | } | 456 | } |
504 | 457 | ||
505 | static void set_friend_status(int friendnumber, uint8_t status) | 458 | void set_friend_status(Messenger *m, int friendnumber, uint8_t status) |
506 | { | 459 | { |
507 | check_friend_connectionstatus(friendnumber, status); | 460 | check_friend_connectionstatus(m, friendnumber, status); |
508 | friendlist[friendnumber].status = status; | 461 | m->friendlist[friendnumber].status = status; |
509 | } | 462 | } |
510 | 463 | ||
511 | static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) | 464 | int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) |
512 | { | 465 | { |
513 | if (friendnumber < 0 || friendnumber >= numfriends) | 466 | if (friendnumber < 0 || friendnumber >= m->numfriends) |
514 | return 0; | 467 | return 0; |
515 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) | 468 | if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) |
516 | return 0; | 469 | return 0; |
517 | uint8_t packet[length + 1]; | 470 | uint8_t packet[length + 1]; |
518 | packet[0] = packet_id; | 471 | packet[0] = packet_id; |
519 | memcpy(packet + 1, data, length); | 472 | memcpy(packet + 1, data, length); |
520 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1); | 473 | return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); |
521 | } | 474 | } |
522 | 475 | ||
523 | #define PORT 33445 | 476 | #define PORT 33445 |
524 | /* run this at startup */ | 477 | /* run this at startup */ |
525 | int initMessenger(void) | 478 | Messenger * initMessenger(void) |
526 | { | 479 | { |
480 | Messenger *m = calloc(1, sizeof(Messenger)); | ||
481 | if( ! m ) | ||
482 | return 0; | ||
483 | |||
527 | new_keys(); | 484 | new_keys(); |
528 | m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); | 485 | m_set_statusmessage(m, (uint8_t*)"Online", sizeof("Online")); |
529 | initNetCrypto(); | 486 | initNetCrypto(); |
530 | IP ip; | 487 | IP ip; |
531 | ip.i = 0; | 488 | ip.i = 0; |
532 | 489 | ||
533 | if(init_networking(ip,PORT) == -1) | 490 | if(init_networking(ip,PORT) == -1) |
534 | return -1; | 491 | return 0; |
535 | 492 | ||
536 | DHT_init(); | 493 | DHT_init(); |
537 | LosslessUDP_init(); | 494 | LosslessUDP_init(); |
538 | friendreq_init(); | 495 | friendreq_init(); |
539 | LANdiscovery_init(); | 496 | LANdiscovery_init(); |
540 | 497 | ||
541 | return 0; | 498 | return m; |
499 | } | ||
500 | |||
501 | /* run this before closing shop */ | ||
502 | void cleanupMessenger(Messenger *m){ | ||
503 | /* FIXME TODO it seems no one frees friendlist or all the elements status */ | ||
504 | free(m); | ||
542 | } | 505 | } |
543 | 506 | ||
544 | //TODO: make this function not suck. | 507 | //TODO: make this function not suck. |
545 | static void doFriends(void) | 508 | void doFriends(Messenger *m) |
546 | { | 509 | { |
547 | /* TODO: add incoming connections and some other stuff. */ | 510 | /* TODO: add incoming connections and some other stuff. */ |
548 | uint32_t i; | 511 | uint32_t i; |
549 | int len; | 512 | int len; |
550 | uint8_t temp[MAX_DATA_SIZE]; | 513 | uint8_t temp[MAX_DATA_SIZE]; |
551 | for (i = 0; i < numfriends; ++i) { | 514 | for (i = 0; i < m->numfriends; ++i) { |
552 | if (friendlist[i].status == FRIEND_ADDED) { | 515 | if (m->friendlist[i].status == FRIEND_ADDED) { |
553 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 516 | int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size); |
554 | 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 */ | 517 | 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 */ |
555 | set_friend_status(i, FRIEND_REQUESTED); | 518 | set_friend_status(m, i, FRIEND_REQUESTED); |
556 | else if (fr > 0) | 519 | else if (fr > 0) |
557 | set_friend_status(i, FRIEND_REQUESTED); | 520 | set_friend_status(m, i, FRIEND_REQUESTED); |
558 | } | 521 | } |
559 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 522 | if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ |
560 | if (friendlist[i].status == FRIEND_REQUESTED) { | 523 | if (m->friendlist[i].status == FRIEND_REQUESTED) { |
561 | if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ | 524 | if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ |
562 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 525 | send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size); |
563 | friendlist[i].friend_request_id = unix_time(); | 526 | m->friendlist[i].friend_request_id = unix_time(); |
564 | } | 527 | } |
565 | } | 528 | } |
566 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 529 | IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id); |
567 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { | 530 | switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) { |
568 | case 0: | 531 | case 0: |
569 | if (friendip.ip.i > 1) | 532 | if (friendip.ip.i > 1) |
570 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 533 | m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip); |
571 | break; | 534 | break; |
572 | case 3: /* Connection is established */ | 535 | case 3: /* Connection is established */ |
573 | set_friend_status(i, FRIEND_ONLINE); | 536 | set_friend_status(m, i, FRIEND_ONLINE); |
574 | friendlist[i].name_sent = 0; | 537 | m->friendlist[i].name_sent = 0; |
575 | friendlist[i].userstatus_sent = 0; | 538 | m->friendlist[i].userstatus_sent = 0; |
576 | friendlist[i].statusmessage_sent = 0; | 539 | m->friendlist[i].statusmessage_sent = 0; |
577 | break; | 540 | break; |
578 | case 4: | 541 | case 4: |
579 | crypto_kill(friendlist[i].crypt_connection_id); | 542 | crypto_kill(m->friendlist[i].crypt_connection_id); |
580 | friendlist[i].crypt_connection_id = -1; | 543 | m->friendlist[i].crypt_connection_id = -1; |
581 | break; | 544 | break; |
582 | default: | 545 | default: |
583 | break; | 546 | break; |
584 | } | 547 | } |
585 | } | 548 | } |
586 | while (friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ | 549 | while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ |
587 | if (friendlist[i].name_sent == 0) { | 550 | if (m->friendlist[i].name_sent == 0) { |
588 | if (m_sendname(i, self_name, self_name_length)) | 551 | if (m_sendname(m, i, m->name, m->name_length)) |
589 | friendlist[i].name_sent = 1; | 552 | m->friendlist[i].name_sent = 1; |
590 | } | 553 | } |
591 | if (friendlist[i].statusmessage_sent == 0) { | 554 | if (m->friendlist[i].statusmessage_sent == 0) { |
592 | if (send_statusmessage(i, self_statusmessage, self_statusmessage_length)) | 555 | if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length)) |
593 | friendlist[i].statusmessage_sent = 1; | 556 | m->friendlist[i].statusmessage_sent = 1; |
594 | } | 557 | } |
595 | if (friendlist[i].userstatus_sent == 0) { | 558 | if (m->friendlist[i].userstatus_sent == 0) { |
596 | if (send_userstatus(i, self_userstatus)) | 559 | if (send_userstatus(m, i, m->userstatus)) |
597 | friendlist[i].userstatus_sent = 1; | 560 | m->friendlist[i].userstatus_sent = 1; |
598 | } | 561 | } |
599 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 562 | len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp); |
600 | uint8_t packet_id = temp[0]; | 563 | uint8_t packet_id = temp[0]; |
601 | uint8_t* data = temp + 1; | 564 | uint8_t* data = temp + 1; |
602 | int data_length = len - 1; | 565 | int data_length = len - 1; |
@@ -605,10 +568,10 @@ static void doFriends(void) | |||
605 | case PACKET_ID_NICKNAME: { | 568 | case PACKET_ID_NICKNAME: { |
606 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) | 569 | if (data_length >= MAX_NAME_LENGTH || data_length == 0) |
607 | break; | 570 | break; |
608 | if(friend_namechange_isset) | 571 | if(m->friend_namechange_isset) |
609 | friend_namechange(i, data, data_length); | 572 | m->friend_namechange(m, i, data, data_length); |
610 | memcpy(friendlist[i].name, data, data_length); | 573 | memcpy(m->friendlist[i].name, data, data_length); |
611 | friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ | 574 | m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ |
612 | break; | 575 | break; |
613 | } | 576 | } |
614 | case PACKET_ID_STATUSMESSAGE: { | 577 | case PACKET_ID_STATUSMESSAGE: { |
@@ -616,9 +579,9 @@ static void doFriends(void) | |||
616 | break; | 579 | break; |
617 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); | 580 | uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); |
618 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 581 | memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
619 | if (friend_statusmessagechange_isset) | 582 | if (m->friend_statusmessagechange_isset) |
620 | friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 583 | m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
621 | set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); | 584 | set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); |
622 | free(status); | 585 | free(status); |
623 | break; | 586 | break; |
624 | } | 587 | } |
@@ -626,9 +589,9 @@ static void doFriends(void) | |||
626 | if (data_length != 1) | 589 | if (data_length != 1) |
627 | break; | 590 | break; |
628 | USERSTATUS status = data[0]; | 591 | USERSTATUS status = data[0]; |
629 | if (friend_userstatuschange_isset) | 592 | if (m->friend_userstatuschange_isset) |
630 | friend_userstatuschange(i, status); | 593 | m->friend_userstatuschange(m, i, status); |
631 | set_friend_userstatus(i, status); | 594 | set_friend_userstatus(m, i, status); |
632 | break; | 595 | break; |
633 | } | 596 | } |
634 | case PACKET_ID_MESSAGE: { | 597 | case PACKET_ID_MESSAGE: { |
@@ -636,16 +599,16 @@ static void doFriends(void) | |||
636 | uint8_t message_id_length = 4; | 599 | uint8_t message_id_length = 4; |
637 | uint8_t *message = data + message_id_length; | 600 | uint8_t *message = data + message_id_length; |
638 | uint16_t message_length = data_length - message_id_length; | 601 | uint16_t message_length = data_length - message_id_length; |
639 | if (friendlist[i].receives_read_receipts) { | 602 | if (m->friendlist[i].receives_read_receipts) { |
640 | write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length); | 603 | write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); |
641 | } | 604 | } |
642 | if (friend_message_isset) | 605 | if (m->friend_message_isset) |
643 | (*friend_message)(i, message, message_length); | 606 | (*m->friend_message)(m, i, message, message_length); |
644 | break; | 607 | break; |
645 | } | 608 | } |
646 | case PACKET_ID_ACTION: { | 609 | case PACKET_ID_ACTION: { |
647 | if (friend_action_isset) | 610 | if (m->friend_action_isset) |
648 | (*friend_action)(i, data, data_length); | 611 | (*m->friend_action)(m, i, data, data_length); |
649 | break; | 612 | break; |
650 | } | 613 | } |
651 | case PACKET_ID_RECEIPT: { | 614 | case PACKET_ID_RECEIPT: { |
@@ -654,16 +617,16 @@ static void doFriends(void) | |||
654 | break; | 617 | break; |
655 | memcpy(&msgid, data, sizeof(msgid)); | 618 | memcpy(&msgid, data, sizeof(msgid)); |
656 | msgid = ntohl(msgid); | 619 | msgid = ntohl(msgid); |
657 | if (read_receipt_isset) | 620 | if (m->read_receipt_isset) |
658 | (*read_receipt)(i, msgid); | 621 | (*m->read_receipt)(m, i, msgid); |
659 | break; | 622 | break; |
660 | } | 623 | } |
661 | } | 624 | } |
662 | } else { | 625 | } else { |
663 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 626 | if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
664 | crypto_kill(friendlist[i].crypt_connection_id); | 627 | crypto_kill(m->friendlist[i].crypt_connection_id); |
665 | friendlist[i].crypt_connection_id = -1; | 628 | m->friendlist[i].crypt_connection_id = -1; |
666 | set_friend_status(i, FRIEND_CONFIRMED); | 629 | set_friend_status(m, i, FRIEND_CONFIRMED); |
667 | } | 630 | } |
668 | break; | 631 | break; |
669 | } | 632 | } |
@@ -671,20 +634,20 @@ static void doFriends(void) | |||
671 | } | 634 | } |
672 | } | 635 | } |
673 | 636 | ||
674 | static void doInbound(void) | 637 | void doInbound(Messenger *m) |
675 | { | 638 | { |
676 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 639 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
677 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 640 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
678 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 641 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
679 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 642 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
680 | if (inconnection != -1) { | 643 | if (inconnection != -1) { |
681 | int friend_id = getfriend_id(public_key); | 644 | int friend_id = getfriend_id(m, public_key); |
682 | if (friend_id != -1) { | 645 | if (friend_id != -1) { |
683 | crypto_kill(friendlist[friend_id].crypt_connection_id); | 646 | crypto_kill(m->friendlist[friend_id].crypt_connection_id); |
684 | friendlist[friend_id].crypt_connection_id = | 647 | m->friendlist[friend_id].crypt_connection_id = |
685 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 648 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
686 | 649 | ||
687 | set_friend_status(friend_id, FRIEND_CONFIRMED); | 650 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); |
688 | } | 651 | } |
689 | } | 652 | } |
690 | } | 653 | } |
@@ -695,7 +658,7 @@ static void doInbound(void) | |||
695 | static uint64_t last_LANdiscovery; | 658 | static uint64_t last_LANdiscovery; |
696 | 659 | ||
697 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | 660 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ |
698 | static void LANdiscovery(void) | 661 | void LANdiscovery(Messenger *m) |
699 | { | 662 | { |
700 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { | 663 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
701 | send_LANdiscovery(htons(PORT)); | 664 | send_LANdiscovery(htons(PORT)); |
@@ -705,27 +668,27 @@ static void LANdiscovery(void) | |||
705 | 668 | ||
706 | 669 | ||
707 | /* the main loop that needs to be run at least 200 times per second. */ | 670 | /* the main loop that needs to be run at least 200 times per second. */ |
708 | void doMessenger(void) | 671 | void doMessenger(Messenger *m) |
709 | { | 672 | { |
710 | networking_poll(); | 673 | networking_poll(); |
711 | 674 | ||
712 | doDHT(); | 675 | doDHT(); |
713 | doLossless_UDP(); | 676 | doLossless_UDP(); |
714 | doNetCrypto(); | 677 | doNetCrypto(); |
715 | doInbound(); | 678 | doInbound(m); |
716 | doFriends(); | 679 | doFriends(m); |
717 | LANdiscovery(); | 680 | LANdiscovery(m); |
718 | } | 681 | } |
719 | 682 | ||
720 | /* returns the size of the messenger data (for saving) */ | 683 | /* returns the size of the messenger data (for saving) */ |
721 | uint32_t Messenger_size(void) | 684 | uint32_t Messenger_size(Messenger *m) |
722 | { | 685 | { |
723 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 686 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
724 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; | 687 | + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends; |
725 | } | 688 | } |
726 | 689 | ||
727 | /* save the messenger in data of size Messenger_size() */ | 690 | /* save the messenger in data of size Messenger_size() */ |
728 | void Messenger_save(uint8_t *data) | 691 | void Messenger_save(Messenger *m, uint8_t *data) |
729 | { | 692 | { |
730 | save_keys(data); | 693 | save_keys(data); |
731 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 694 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -734,14 +697,14 @@ void Messenger_save(uint8_t *data) | |||
734 | data += sizeof(size); | 697 | data += sizeof(size); |
735 | DHT_save(data); | 698 | DHT_save(data); |
736 | data += size; | 699 | data += size; |
737 | size = sizeof(Friend) * numfriends; | 700 | size = sizeof(Friend) * m->numfriends; |
738 | memcpy(data, &size, sizeof(size)); | 701 | memcpy(data, &size, sizeof(size)); |
739 | data += sizeof(size); | 702 | data += sizeof(size); |
740 | memcpy(data, friendlist, sizeof(Friend) * numfriends); | 703 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); |
741 | } | 704 | } |
742 | 705 | ||
743 | /* load the messenger from data of size length. */ | 706 | /* load the messenger from data of size length. */ |
744 | int Messenger_load(uint8_t * data, uint32_t length) | 707 | int Messenger_load(Messenger *m, uint8_t * data, uint32_t length) |
745 | { | 708 | { |
746 | if (length == ~0) | 709 | if (length == ~0) |
747 | return -1; | 710 | return -1; |
@@ -773,11 +736,12 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
773 | uint32_t i; | 736 | uint32_t i; |
774 | for (i = 0; i < num; ++i) { | 737 | for (i = 0; i < num; ++i) { |
775 | if(temp[i].status != 0) { | 738 | if(temp[i].status != 0) { |
776 | int fnum = m_addfriend_norequest(temp[i].client_id); | 739 | int fnum = m_addfriend_norequest(m, temp[i].client_id); |
777 | setfriendname(fnum, temp[i].name); | 740 | setfriendname(m, fnum, temp[i].name); |
778 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | 741 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ |
779 | } | 742 | } |
780 | } | 743 | } |
781 | free(temp); | 744 | free(temp); |
782 | return 0; | 745 | return 0; |
783 | } | 746 | } |
747 | |||
diff --git a/core/Messenger.h b/core/Messenger.h index 9352cfbb..36e24596 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -73,6 +73,58 @@ typedef enum { | |||
73 | USERSTATUS_INVALID | 73 | USERSTATUS_INVALID |
74 | } USERSTATUS; | 74 | } USERSTATUS; |
75 | 75 | ||
76 | typedef struct { | ||
77 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
78 | int crypt_connection_id; | ||
79 | uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | ||
80 | uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ | ||
81 | uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ | ||
82 | uint8_t name[MAX_NAME_LENGTH]; | ||
83 | uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ | ||
84 | uint8_t *statusmessage; | ||
85 | uint16_t statusmessage_length; | ||
86 | uint8_t statusmessage_sent; | ||
87 | USERSTATUS userstatus; | ||
88 | uint8_t userstatus_sent; | ||
89 | uint16_t info_size; /* length of the info */ | ||
90 | uint32_t message_id; /* a semi-unique id used in read receipts */ | ||
91 | uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ | ||
92 | } Friend; | ||
93 | |||
94 | typedef struct Messenger { | ||
95 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
96 | |||
97 | uint8_t name[MAX_NAME_LENGTH]; | ||
98 | uint16_t name_length; | ||
99 | |||
100 | uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH]; | ||
101 | uint16_t statusmessage_length; | ||
102 | |||
103 | USERSTATUS userstatus; | ||
104 | |||
105 | Friend *friendlist; | ||
106 | uint32_t numfriends; | ||
107 | |||
108 | void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
109 | uint8_t friend_message_isset; | ||
110 | void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
111 | uint8_t friend_action_isset; | ||
112 | void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
113 | uint8_t friend_namechange_isset; | ||
114 | void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t); | ||
115 | uint8_t friend_statusmessagechange_isset; | ||
116 | void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS); | ||
117 | uint8_t friend_userstatuschange_isset; | ||
118 | void (*read_receipt)(struct Messenger *m, int, uint32_t); | ||
119 | uint8_t read_receipt_isset; | ||
120 | void (*friend_statuschange)(struct Messenger *m, int, uint8_t); | ||
121 | uint8_t friend_statuschange_isset; | ||
122 | void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t); | ||
123 | uint8_t friend_connectionstatuschange_isset; | ||
124 | |||
125 | |||
126 | } Messenger; | ||
127 | |||
76 | /* | 128 | /* |
77 | * add a friend | 129 | * add a friend |
78 | * set the data that will be sent along with friend request | 130 | * set the data that will be sent along with friend request |
@@ -85,33 +137,33 @@ typedef enum { | |||
85 | * return -4 if friend request already sent or already a friend | 137 | * return -4 if friend request already sent or already a friend |
86 | * return -5 for unknown error | 138 | * return -5 for unknown error |
87 | */ | 139 | */ |
88 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); | 140 | int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length); |
89 | 141 | ||
90 | 142 | ||
91 | /* add a friend without sending a friendrequest. | 143 | /* add a friend without sending a friendrequest. |
92 | returns the friend number if success | 144 | returns the friend number if success |
93 | return -1 if failure. */ | 145 | return -1 if failure. */ |
94 | int m_addfriend_norequest(uint8_t *client_id); | 146 | int m_addfriend_norequest(Messenger *m, uint8_t *client_id); |
95 | 147 | ||
96 | /* return the friend id associated to that client id. | 148 | /* return the friend id associated to that client id. |
97 | return -1 if no such friend */ | 149 | return -1 if no such friend */ |
98 | int getfriend_id(uint8_t *client_id); | 150 | int getfriend_id(Messenger *m, uint8_t *client_id); |
99 | 151 | ||
100 | /* copies the public key associated to that friend id into client_id buffer. | 152 | /* copies the public key associated to that friend id into client_id buffer. |
101 | make sure that client_id is of size CLIENT_ID_SIZE. | 153 | make sure that client_id is of size CLIENT_ID_SIZE. |
102 | return 0 if success | 154 | return 0 if success |
103 | return -1 if failure */ | 155 | return -1 if failure */ |
104 | int getclient_id(int friend_id, uint8_t *client_id); | 156 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); |
105 | 157 | ||
106 | /* remove a friend */ | 158 | /* remove a friend */ |
107 | int m_delfriend(int friendnumber); | 159 | int m_delfriend(Messenger *m, int friendnumber); |
108 | 160 | ||
109 | /* return 4 if friend is online | 161 | /* return 4 if friend is online |
110 | return 3 if friend is confirmed | 162 | return 3 if friend is confirmed |
111 | return 2 if the friend request was sent | 163 | return 2 if the friend request was sent |
112 | return 1 if the friend was added | 164 | return 1 if the friend was added |
113 | return 0 if there is no friend with that number */ | 165 | return 0 if there is no friend with that number */ |
114 | int m_friendstatus(int friendnumber); | 166 | int m_friendstatus(Messenger *m, int friendnumber); |
115 | 167 | ||
116 | /* send a text chat message to an online friend | 168 | /* send a text chat message to an online friend |
117 | returns the message id if packet was successfully put into the send queue | 169 | returns the message id if packet was successfully put into the send queue |
@@ -120,13 +172,13 @@ int m_friendstatus(int friendnumber); | |||
120 | if one is received. | 172 | if one is received. |
121 | m_sendmessage_withid will send a message with the id of your choosing, | 173 | m_sendmessage_withid will send a message with the id of your choosing, |
122 | however we can generate an id for you by calling plain m_sendmessage. */ | 174 | however we can generate an id for you by calling plain m_sendmessage. */ |
123 | uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); | 175 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); |
124 | uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | 176 | uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); |
125 | 177 | ||
126 | /* send an action to an online friend | 178 | /* send an action to an online friend |
127 | returns 1 if packet was successfully put into the send queue | 179 | returns 1 if packet was successfully put into the send queue |
128 | return 0 if it was not */ | 180 | return 0 if it was not */ |
129 | int m_sendaction(int friendnumber, uint8_t *action, uint32_t length); | 181 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); |
130 | 182 | ||
131 | /* Set our nickname | 183 | /* Set our nickname |
132 | name must be a string of maximum MAX_NAME_LENGTH length. | 184 | name must be a string of maximum MAX_NAME_LENGTH length. |
@@ -134,73 +186,73 @@ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length); | |||
134 | length is the length of name with the NULL terminator | 186 | length is the length of name with the NULL terminator |
135 | return 0 if success | 187 | return 0 if success |
136 | return -1 if failure */ | 188 | return -1 if failure */ |
137 | int setname(uint8_t *name, uint16_t length); | 189 | int setname(Messenger *m, uint8_t *name, uint16_t length); |
138 | 190 | ||
139 | /* get our nickname | 191 | /* get our nickname |
140 | put it in name | 192 | put it in name |
141 | return the length of the name*/ | 193 | return the length of the name*/ |
142 | uint16_t getself_name(uint8_t *name); | 194 | uint16_t getself_name(Messenger *m, uint8_t *name); |
143 | 195 | ||
144 | /* get name of friendnumber | 196 | /* get name of friendnumber |
145 | put it in name | 197 | put it in name |
146 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 198 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
147 | return 0 if success | 199 | return 0 if success |
148 | return -1 if failure */ | 200 | return -1 if failure */ |
149 | int getname(int friendnumber, uint8_t *name); | 201 | int getname(Messenger *m, int friendnumber, uint8_t *name); |
150 | 202 | ||
151 | /* set our user status | 203 | /* set our user status |
152 | you are responsible for freeing status after | 204 | you are responsible for freeing status after |
153 | returns 0 on success, -1 on failure */ | 205 | returns 0 on success, -1 on failure */ |
154 | int m_set_statusmessage(uint8_t *status, uint16_t length); | 206 | int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); |
155 | int m_set_userstatus(USERSTATUS status); | 207 | int m_set_userstatus(Messenger *m, USERSTATUS status); |
156 | 208 | ||
157 | /* return the length of friendnumber's status message, | 209 | /* return the length of friendnumber's status message, |
158 | including null | 210 | including null |
159 | pass it into malloc */ | 211 | pass it into malloc */ |
160 | int m_get_statusmessage_size(int friendnumber); | 212 | int m_get_statusmessage_size(Messenger *m, int friendnumber); |
161 | 213 | ||
162 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | 214 | /* copy friendnumber's status message into buf, truncating if size is over maxlen |
163 | get the size you need to allocate from m_get_statusmessage_size | 215 | get the size you need to allocate from m_get_statusmessage_size |
164 | The self variant will copy our own status message. */ | 216 | The self variant will copy our own status message. */ |
165 | int m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen); | 217 | int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); |
166 | int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen); | 218 | int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); |
167 | 219 | ||
168 | /* Return one of USERSTATUS values. | 220 | /* Return one of USERSTATUS values. |
169 | * Values unknown to your application should be represented as USERSTATUS_NONE. | 221 | * Values unknown to your application should be represented as USERSTATUS_NONE. |
170 | * As above, the self variant will return our own USERSTATUS. | 222 | * As above, the self variant will return our own USERSTATUS. |
171 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | 223 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ |
172 | USERSTATUS m_get_userstatus(int friendnumber); | 224 | USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); |
173 | USERSTATUS m_get_self_userstatus(void); | 225 | USERSTATUS m_get_self_userstatus(Messenger *m); |
174 | 226 | ||
175 | /* Sets whether we send read receipts for friendnumber. | 227 | /* Sets whether we send read receipts for friendnumber. |
176 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | 228 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ |
177 | void m_set_sends_receipts(int friendnumber, int yesno); | 229 | void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); |
178 | 230 | ||
179 | /* set the function that will be executed when a friend request is received. | 231 | /* set the function that will be executed when a friend request is received. |
180 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 232 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
181 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 233 | void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t)); |
182 | 234 | ||
183 | /* set the function that will be executed when a message from a friend is received. | 235 | /* set the function that will be executed when a message from a friend is received. |
184 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 236 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
185 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); | 237 | void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
186 | 238 | ||
187 | /* set the function that will be executed when an action from a friend is received. | 239 | /* set the function that will be executed when an action from a friend is received. |
188 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | 240 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ |
189 | void m_callback_action(void (*function)(int, uint8_t *, uint16_t)); | 241 | void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
190 | 242 | ||
191 | /* set the callback for name changes | 243 | /* set the callback for name changes |
192 | function(int friendnumber, uint8_t *newname, uint16_t length) | 244 | function(int friendnumber, uint8_t *newname, uint16_t length) |
193 | you are not responsible for freeing newname */ | 245 | you are not responsible for freeing newname */ |
194 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); | 246 | void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
195 | 247 | ||
196 | /* set the callback for status message changes | 248 | /* set the callback for status message changes |
197 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 249 | function(int friendnumber, uint8_t *newstatus, uint16_t length) |
198 | you are not responsible for freeing newstatus */ | 250 | you are not responsible for freeing newstatus */ |
199 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); | 251 | void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t)); |
200 | 252 | ||
201 | /* set the callback for status type changes | 253 | /* set the callback for status type changes |
202 | function(int friendnumber, USERSTATUS kind) */ | 254 | function(int friendnumber, USERSTATUS kind) */ |
203 | void m_callback_userstatus(void (*function)(int, USERSTATUS)); | 255 | void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS)); |
204 | 256 | ||
205 | /* set the callback for read receipts | 257 | /* set the callback for read receipts |
206 | function(int friendnumber, uint32_t receipt) | 258 | function(int friendnumber, uint32_t receipt) |
@@ -209,7 +261,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS)); | |||
209 | has been received on the other side. since core doesn't | 261 | has been received on the other side. since core doesn't |
210 | track ids for you, receipt may not correspond to any message | 262 | track ids for you, receipt may not correspond to any message |
211 | in that case, you should discard it. */ | 263 | in that case, you should discard it. */ |
212 | void m_callback_read_receipt(void (*function)(int, uint32_t)); | 264 | void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t)); |
213 | 265 | ||
214 | /* set the callback for connection status changes | 266 | /* set the callback for connection status changes |
215 | function(int friendnumber, uint8_t status) | 267 | function(int friendnumber, uint8_t status) |
@@ -219,26 +271,30 @@ void m_callback_read_receipt(void (*function)(int, uint32_t)); | |||
219 | note that this callback is not called when adding friends, thus the "after | 271 | note that this callback is not called when adding friends, thus the "after |
220 | being previously online" part. it's assumed that when adding friends, | 272 | being previously online" part. it's assumed that when adding friends, |
221 | their connection status is offline. */ | 273 | their connection status is offline. */ |
222 | void m_callback_connectionstatus(void (*function)(int, uint8_t)); | 274 | void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t)); |
223 | 275 | ||
224 | /* run this at startup | 276 | /* run this at startup |
225 | returns 0 if no connection problems | 277 | * returns allocated instance of Messenger on success |
226 | returns -1 if there are problems */ | 278 | * returns 0 if there are problems */ |
227 | int initMessenger(void); | 279 | Messenger * initMessenger(void); |
280 | |||
281 | /* run this before closing shop | ||
282 | * free all datastructures */ | ||
283 | void cleanupMessenger(Messenger *M); | ||
228 | 284 | ||
229 | /* the main loop that needs to be run at least 200 times per second */ | 285 | /* the main loop that needs to be run at least 200 times per second */ |
230 | void doMessenger(void); | 286 | void doMessenger(Messenger *m); |
231 | 287 | ||
232 | /* SAVING AND LOADING FUNCTIONS: */ | 288 | /* SAVING AND LOADING FUNCTIONS: */ |
233 | 289 | ||
234 | /* returns the size of the messenger data (for saving) */ | 290 | /* returns the size of the messenger data (for saving) */ |
235 | uint32_t Messenger_size(void); | 291 | uint32_t Messenger_size(Messenger *m); |
236 | 292 | ||
237 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 293 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ |
238 | void Messenger_save(uint8_t *data); | 294 | void Messenger_save(Messenger *m, uint8_t *data); |
239 | 295 | ||
240 | /* load the messenger from data of size length */ | 296 | /* load the messenger from data of size length */ |
241 | int Messenger_load(uint8_t *data, uint32_t length); | 297 | int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); |
242 | 298 | ||
243 | #ifdef __cplusplus | 299 | #ifdef __cplusplus |
244 | } | 300 | } |
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index a11d374b..51542c6d 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,6 +51,10 @@ | |||
51 | 51 | ||
52 | #endif | 52 | #endif |
53 | 53 | ||
54 | /* FIXME needed as print_request has to match the interface expected by | ||
55 | * networking_requesthandler and so cannot take a Messenger * */ | ||
56 | static Messenger *m; | ||
57 | |||
54 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | 58 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) |
55 | { | 59 | { |
56 | printf("Friend request received from: \n"); | 60 | printf("Friend request received from: \n"); |
@@ -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 | ||
79 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | 83 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length) |
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 | ||
85 | int main(int argc, char *argv[]) | 89 | int 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); |
108 | m_callback_friendmessage(print_message); | 118 | m_callback_friendmessage(m, print_message); |
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..1322067e 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -85,12 +85,12 @@ void new_lines(char *line) | |||
85 | } | 85 | } |
86 | 86 | ||
87 | 87 | ||
88 | void print_friendlist() | 88 | void 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 | ||
110 | char *format_message(char *message, int friendnum) | 110 | char *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 | ||
136 | void line_eval(char *line) | 136 | void 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); |
@@ -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 | ||
352 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | 352 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length) |
353 | { | 353 | { |
354 | new_lines(format_message((char*)string, friendnumber)); | 354 | new_lines(format_message(m, (char*)string, friendnumber)); |
355 | } | 355 | } |
356 | 356 | ||
357 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) | 357 | void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
367 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) | 367 | void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
377 | void load_key(char *path) | 377 | void 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); |
465 | m_callback_friendmessage(print_message); | 471 | m_callback_friendmessage(m, print_message); |
466 | m_callback_namechange(print_nickchange); | 472 | m_callback_namechange(m, print_nickchange); |
467 | m_callback_statusmessage(print_statuschange); | 473 | m_callback_statusmessage(m, print_statuschange); |
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 | ||
45 | void new_lines(char *line); | 45 | void new_lines(char *line); |
46 | void line_eval(char *line); | 46 | void line_eval(Messenger *m, char *line); |
47 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; | 47 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; |
48 | int count_lines(char *string) ; | 48 | int count_lines(char *string) ; |
49 | char *appender(char *str, const char c); | 49 | char *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; | |||
29 | extern void del_window(ToxWindow *w, int f_num); | 29 | extern void del_window(ToxWindow *w, int f_num); |
30 | extern void fix_name(uint8_t *name); | 30 | extern void fix_name(uint8_t *name); |
31 | void print_help(ChatContext *self); | 31 | void print_help(ChatContext *self); |
32 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo); | 32 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo); |
33 | 33 | ||
34 | static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) | 34 | static 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 | ||
64 | static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len) | 64 | static 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 | ||
120 | static void chat_onKey(ToxWindow *self, int key) | 120 | static 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 | ||
181 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) | 181 | void 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 | ||
315 | static void chat_onInit(ToxWindow *self) | 315 | static 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 | ||
345 | ToxWindow new_chat(int friendnum) | 345 | ToxWindow 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 | ||
15 | extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; | 15 | extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; |
16 | extern int add_window(ToxWindow w, int n); | 16 | extern int add_window(ToxWindow w, int n); |
17 | extern ToxWindow new_chat(int friendnum); | 17 | extern ToxWindow new_chat(Messenger *m, int friendnum); |
18 | 18 | ||
19 | extern int active_window; | 19 | extern 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 | ||
45 | void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len) | 45 | void 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 | ||
85 | int friendlist_onFriendAdded(int num) | 85 | int 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 | ||
98 | static void friendlist_onKey(ToxWindow *self, int key) | 98 | static 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 | ||
167 | static void friendlist_onInit(ToxWindow *self) | 167 | static 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..c14dee1f 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -25,7 +25,7 @@ | |||
25 | extern ToxWindow new_prompt(); | 25 | extern ToxWindow new_prompt(); |
26 | extern ToxWindow new_friendlist(); | 26 | extern ToxWindow new_friendlist(); |
27 | 27 | ||
28 | extern int friendlist_onFriendAdded(int num); | 28 | extern int friendlist_onFriendAdded(Messenger *m, 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 | extern unsigned char *hex_string_to_bin(char hex_string[]); |
@@ -40,6 +40,8 @@ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | |||
40 | static ToxWindow windows[MAX_WINDOW_SLOTS]; | 40 | static ToxWindow windows[MAX_WINDOW_SLOTS]; |
41 | static ToxWindow* prompt; | 41 | static ToxWindow* prompt; |
42 | 42 | ||
43 | static Messenger *m; | ||
44 | |||
43 | int w_num; | 45 | int w_num; |
44 | int active_window; | 46 | int active_window; |
45 | 47 | ||
@@ -63,25 +65,25 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) | |||
63 | } | 65 | } |
64 | } | 66 | } |
65 | 67 | ||
66 | void on_message(int friendnumber, uint8_t *string, uint16_t length) | 68 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
75 | void on_action(int friendnumber, uint8_t *string, uint16_t length) | 77 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
84 | void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) | 86 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
94 | void on_statuschange(int friendnumber, uint8_t *string, uint16_t length) | 96 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length) |
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 | ||
104 | void on_friendadded(int friendnumber) | 106 | void 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() | |||
129 | static void init_tox() | 131 | static 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); |
136 | m_callback_friendmessage(on_message); | 138 | m_callback_friendmessage(m, on_message); |
137 | m_callback_namechange(on_nickchange); | 139 | m_callback_namechange(m, on_nickchange); |
138 | m_callback_statusmessage(on_statuschange); | 140 | m_callback_statusmessage(m, on_statuschange); |
139 | m_callback_action(on_action); | 141 | m_callback_action(m, on_action); |
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) */ |
@@ -211,7 +213,7 @@ int add_window(ToxWindow w, int n) | |||
211 | return -1; | 213 | return -1; |
212 | 214 | ||
213 | windows[n] = w; | 215 | windows[n] = w; |
214 | w.onInit(&w); | 216 | w.onInit(&w, m); |
215 | w_num++; | 217 | w_num++; |
216 | return n; | 218 | return n; |
217 | } | 219 | } |
@@ -237,7 +239,7 @@ static void init_windows() | |||
237 | w_num = 0; | 239 | w_num = 0; |
238 | int n_prompt = 0; | 240 | int n_prompt = 0; |
239 | int n_friendslist = 1; | 241 | int n_friendslist = 1; |
240 | if (add_window(new_prompt(), n_prompt) == -1 | 242 | if (add_window(new_prompt(), n_prompt) == -1 |
241 | || add_window(new_friendlist(), n_friendslist) == -1) { | 243 | || add_window(new_friendlist(), n_friendslist) == -1) { |
242 | fprintf(stderr, "add_window() failed.\n"); | 244 | fprintf(stderr, "add_window() failed.\n"); |
243 | endwin(); | 245 | endwin(); |
@@ -257,7 +259,7 @@ static void do_tox() | |||
257 | dht_on = false; | 259 | dht_on = false; |
258 | wprintw(prompt->window, "\nDHT disconnected.\n"); | 260 | wprintw(prompt->window, "\nDHT disconnected.\n"); |
259 | } | 261 | } |
260 | doMessenger(); | 262 | doMessenger(m); |
261 | } | 263 | } |
262 | 264 | ||
263 | static void load_data(char *path) | 265 | static void load_data(char *path) |
@@ -285,17 +287,17 @@ static void load_data(char *path) | |||
285 | endwin(); | 287 | endwin(); |
286 | exit(1); | 288 | exit(1); |
287 | } | 289 | } |
288 | Messenger_load(buf, len); | 290 | Messenger_load(m, buf, len); |
289 | } | 291 | } |
290 | else { | 292 | else { |
291 | len = Messenger_size(); | 293 | len = Messenger_size(m); |
292 | buf = malloc(len); | 294 | buf = malloc(len); |
293 | if (buf == NULL) { | 295 | if (buf == NULL) { |
294 | fprintf(stderr, "malloc() failed.\n"); | 296 | fprintf(stderr, "malloc() failed.\n"); |
295 | endwin(); | 297 | endwin(); |
296 | exit(1); | 298 | exit(1); |
297 | } | 299 | } |
298 | Messenger_save(buf); | 300 | Messenger_save(m, buf); |
299 | 301 | ||
300 | fd = fopen(path, "w"); | 302 | fd = fopen(path, "w"); |
301 | if (fd == NULL) { | 303 | if (fd == NULL) { |
@@ -329,7 +331,7 @@ static void draw_bar() | |||
329 | move(LINES - 1, 0); | 331 | move(LINES - 1, 0); |
330 | 332 | ||
331 | attron(COLOR_PAIR(4) | A_BOLD); | 333 | attron(COLOR_PAIR(4) | A_BOLD); |
332 | printw(" TOXIC " TOXICVER "|"); | 334 | printw(" TOXIC " TOXICVER "|"); |
333 | attroff(COLOR_PAIR(4) | A_BOLD); | 335 | attroff(COLOR_PAIR(4) | A_BOLD); |
334 | 336 | ||
335 | int i; | 337 | int i; |
@@ -473,7 +475,8 @@ int main(int argc, char *argv[]) | |||
473 | if (ch == '\t' || ch == KEY_BTAB) | 475 | if (ch == '\t' || ch == KEY_BTAB) |
474 | set_active_window(ch); | 476 | set_active_window(ch); |
475 | else if (ch != ERR) | 477 | else if (ch != ERR) |
476 | a->onKey(a, ch); | 478 | a->onKey(a, m, ch); |
477 | } | 479 | } |
480 | cleanupMessenger(m); | ||
478 | return 0; | 481 | return 0; |
479 | } | 482 | } |
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}; | |||
20 | static int prompt_buf_pos = 0; | 20 | static int prompt_buf_pos = 0; |
21 | 21 | ||
22 | /* commands */ | 22 | /* commands */ |
23 | void cmd_accept(ToxWindow *, char **); | 23 | void cmd_accept(ToxWindow *, Messenger *m, char **); |
24 | void cmd_add(ToxWindow *, char **); | 24 | void cmd_add(ToxWindow *, Messenger *m, char **); |
25 | void cmd_clear(ToxWindow *, char **); | 25 | void cmd_clear(ToxWindow *, Messenger *m, char **); |
26 | void cmd_connect(ToxWindow *, char **); | 26 | void cmd_connect(ToxWindow *, Messenger *m, char **); |
27 | void cmd_help(ToxWindow *, char **); | 27 | void cmd_help(ToxWindow *, Messenger *m, char **); |
28 | void cmd_msg(ToxWindow *, char **); | 28 | void cmd_msg(ToxWindow *, Messenger *m, char **); |
29 | void cmd_myid(ToxWindow *, char **); | 29 | void cmd_myid(ToxWindow *, Messenger *m, char **); |
30 | void cmd_nick(ToxWindow *, char **); | 30 | void cmd_nick(ToxWindow *, Messenger *m, char **); |
31 | void cmd_quit(ToxWindow *, char **); | 31 | void cmd_quit(ToxWindow *, Messenger *m, char **); |
32 | void cmd_status(ToxWindow *, char **); | 32 | void cmd_status(ToxWindow *, Messenger *m, char **); |
33 | void cmd_statusmsg(ToxWindow *, char **); | 33 | void cmd_statusmsg(ToxWindow *, Messenger *m, char **); |
34 | 34 | ||
35 | #define NUM_COMMANDS 13 | 35 | #define NUM_COMMANDS 13 |
36 | 36 | ||
37 | static struct { | 37 | static 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 | ||
77 | void cmd_accept(ToxWindow *self, char **args) | 77 | void 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 | ||
94 | void cmd_add(ToxWindow *self, char **args) | 94 | void 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 | ||
148 | void cmd_clear(ToxWindow *self, char **args) | 148 | void cmd_clear(ToxWindow *self, Messenger *m, char **args) |
149 | { | 149 | { |
150 | wclear(self->window); | 150 | wclear(self->window); |
151 | } | 151 | } |
152 | 152 | ||
153 | void cmd_connect(ToxWindow *self, char **args) | 153 | void 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 | ||
177 | void cmd_quit(ToxWindow *self, char **args) | 177 | void cmd_quit(ToxWindow *self, Messenger *m, char **args) |
178 | { | 178 | { |
179 | endwin(); | 179 | endwin(); |
180 | exit(0); | 180 | exit(0); |
181 | } | 181 | } |
182 | 182 | ||
183 | void cmd_help(ToxWindow *self, char **args) | 183 | void 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 | ||
208 | void cmd_msg(ToxWindow *self, char **args) | 208 | void 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 | ||
218 | void cmd_myid(ToxWindow *self, char **args) | 218 | void 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 | ||
230 | void cmd_nick(ToxWindow *self, char **args) | 230 | void 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 | ||
237 | void cmd_status(ToxWindow *self, char **args) | 237 | void 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 | ||
273 | void cmd_statusmsg(ToxWindow *self, char **args) | 273 | void 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 | ||
280 | static void execute(ToxWindow *self, char *u_cmd) | 280 | static 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 | ||
359 | static void prompt_onKey(ToxWindow *self, int key) | 359 | static 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 | ||
416 | static void prompt_onInit(ToxWindow *self) | 416 | static 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 @@ | |||
17 | typedef struct ToxWindow_ ToxWindow; | 17 | typedef struct ToxWindow_ ToxWindow; |
18 | 18 | ||
19 | struct ToxWindow_ { | 19 | struct 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; |