diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 140 | ||||
-rw-r--r-- | testing/DHT_sendfiletest.c | 76 | ||||
-rw-r--r-- | testing/DHT_test.c | 132 | ||||
-rw-r--r-- | testing/Lossless_UDP_testclient.c | 100 | ||||
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 83 | ||||
-rw-r--r-- | testing/Messenger_test.c | 67 | ||||
-rw-r--r-- | testing/crypto_speed_test.c | 48 | ||||
-rw-r--r-- | testing/misc_tools.c | 13 | ||||
-rw-r--r-- | testing/misc_tools.h | 76 | ||||
-rw-r--r-- | testing/nTox.c | 245 | ||||
-rw-r--r-- | testing/nTox_win32.c | 185 | ||||
-rw-r--r-- | testing/nTox_win32.h | 6 | ||||
-rw-r--r-- | testing/timer_test.c | 22 | ||||
-rw-r--r-- | testing/toxic/chat.c | 603 | ||||
-rw-r--r-- | testing/toxic/configdir.c | 65 | ||||
-rw-r--r-- | testing/toxic/configdir.h | 2 | ||||
-rw-r--r-- | testing/toxic/dhtstatus.c | 134 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 241 | ||||
-rw-r--r-- | testing/toxic/friendlist.h | 2 | ||||
-rw-r--r-- | testing/toxic/main.c | 317 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 675 | ||||
-rw-r--r-- | testing/toxic/windows.c | 340 | ||||
-rw-r--r-- | testing/toxic/windows.h | 42 |
23 files changed, 1959 insertions, 1655 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index 7ebb72bf..42635b35 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -1,19 +1,19 @@ | |||
1 | /* DHT cryptosendfiletest | 1 | /* DHT cryptosendfiletest |
2 | * | 2 | * |
3 | * This program sends or receives a friend request. | 3 | * This program sends or receives a friend request. |
4 | * | 4 | * |
5 | * it also sends the encrypted data from a file to another client. | 5 | * it also sends the encrypted data from a file to another client. |
6 | * Receives the file data that that client sends us. | 6 | * Receives the file data that that client sends us. |
7 | * | 7 | * |
8 | * NOTE: this program simulates 33% packet loss. | 8 | * NOTE: this program simulates 33% packet loss. |
9 | * | ||
10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c | ||
11 | * | 9 | * |
12 | * | 10 | * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c |
11 | * | ||
12 | * | ||
13 | * Command line arguments are the ip and port of a node (for bootstrapping). | 13 | * Command line arguments are the ip and port of a node (for bootstrapping). |
14 | * | 14 | * |
15 | * Saves all received data to: received.txt | 15 | * Saves all received data to: received.txt |
16 | * | 16 | * |
17 | * EX: ./test 127.0.0.1 33445 filename.txt | 17 | * EX: ./test 127.0.0.1 33445 filename.txt |
18 | * | 18 | * |
19 | * Copyright (C) 2013 Tox project All Rights Reserved. | 19 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -32,9 +32,9 @@ | |||
32 | * | 32 | * |
33 | * You should have received a copy of the GNU General Public License | 33 | * You should have received a copy of the GNU General Public License |
34 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 34 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
35 | * | 35 | * |
36 | */ | 36 | */ |
37 | 37 | ||
38 | #include "../core/network.h" | 38 | #include "../core/network.h" |
39 | #include "../core/DHT.h" | 39 | #include "../core/DHT.h" |
40 | #include "../core/net_crypto.h" | 40 | #include "../core/net_crypto.h" |
@@ -58,7 +58,8 @@ | |||
58 | 58 | ||
59 | void printip(IP_Port ip_port) | 59 | void printip(IP_Port ip_port) |
60 | { | 60 | { |
61 | printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 61 | printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], |
62 | ntohs(ip_port.port)); | ||
62 | } | 63 | } |
63 | 64 | ||
64 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 65 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
@@ -69,68 +70,81 @@ int main(int argc, char *argv[]) | |||
69 | printf("usage %s ip port filename(of file to send)\n", argv[0]); | 70 | printf("usage %s ip port filename(of file to send)\n", argv[0]); |
70 | exit(0); | 71 | exit(0); |
71 | } | 72 | } |
73 | |||
72 | new_keys(); | 74 | new_keys(); |
73 | printf("OUR ID: "); | 75 | printf("OUR ID: "); |
74 | uint32_t i; | 76 | uint32_t i; |
75 | for(i = 0; i < 32; i++) { | 77 | |
76 | if(self_public_key[i] < 16) | 78 | for (i = 0; i < 32; i++) { |
79 | if (self_public_key[i] < 16) | ||
77 | printf("0"); | 80 | printf("0"); |
78 | printf("%hhX",self_public_key[i]); | 81 | |
82 | printf("%hhX", self_public_key[i]); | ||
79 | } | 83 | } |
84 | |||
80 | printf("\n"); | 85 | printf("\n"); |
81 | 86 | ||
82 | memcpy(self_client_id, self_public_key, 32); | 87 | memcpy(self_client_id, self_public_key, 32); |
83 | 88 | ||
84 | char temp_id[128]; | 89 | char temp_id[128]; |
85 | printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n"); | 90 | printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n"); |
86 | scanf("%s", temp_id); | 91 | scanf("%s", temp_id); |
87 | 92 | ||
88 | uint8_t friend_id[32]; | 93 | uint8_t friend_id[32]; |
89 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); | 94 | memcpy(friend_id, hex_string_to_bin(temp_id), 32); |
90 | 95 | ||
91 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ | 96 | /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ |
92 | 97 | ||
93 | 98 | ||
94 | DHT_addfriend(friend_id); | 99 | DHT_addfriend(friend_id); |
95 | IP_Port friend_ip; | 100 | IP_Port friend_ip; |
96 | int connection = -1; | 101 | int connection = -1; |
97 | int inconnection = -1; | 102 | int inconnection = -1; |
98 | 103 | ||
99 | uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES]; | 104 | uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES]; |
100 | int friendrequest = -1; | 105 | int friendrequest = -1; |
101 | uint8_t request_data[512]; | 106 | uint8_t request_data[512]; |
102 | 107 | ||
103 | /* initialize networking | 108 | /* initialize networking |
104 | * bind to ip 0.0.0.0:PORT */ | 109 | * bind to ip 0.0.0.0:PORT */ |
105 | IP ip; | 110 | IP ip; |
106 | ip.i = 0; | 111 | ip.i = 0; |
107 | init_networking(ip, PORT); | 112 | init_networking(ip, PORT); |
108 | initNetCrypto(); | 113 | initNetCrypto(); |
109 | 114 | ||
110 | perror("Initialization"); | 115 | perror("Initialization"); |
111 | IP_Port bootstrap_ip_port; | 116 | IP_Port bootstrap_ip_port; |
112 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 117 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
113 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 118 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
114 | DHT_bootstrap(bootstrap_ip_port); | 119 | DHT_bootstrap(bootstrap_ip_port); |
115 | 120 | ||
116 | IP_Port ip_port; | 121 | IP_Port ip_port; |
117 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 122 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
118 | uint32_t length; | 123 | uint32_t length; |
119 | 124 | ||
120 | uint8_t buffer1[128]; | 125 | uint8_t buffer1[128]; |
121 | int read1 = 0; | 126 | int read1 = 0; |
122 | uint8_t buffer2[128]; | 127 | uint8_t buffer2[128]; |
123 | int read2 = 0; | 128 | int read2 = 0; |
124 | FILE *file1 = fopen(argv[3], "rb"); | 129 | FILE *file1 = fopen(argv[3], "rb"); |
125 | if ( file1==NULL ){printf("Error opening file.\n");return 1;} | 130 | |
131 | if ( file1 == NULL ) { | ||
132 | printf("Error opening file.\n"); | ||
133 | return 1; | ||
134 | } | ||
135 | |||
126 | FILE *file2 = fopen("received.txt", "wb"); | 136 | FILE *file2 = fopen("received.txt", "wb"); |
127 | if ( file2==NULL ){return 1;} | 137 | |
138 | if ( file2 == NULL ) { | ||
139 | return 1; | ||
140 | } | ||
141 | |||
128 | read1 = fread(buffer1, 1, 128, file1); | 142 | read1 = fread(buffer1, 1, 128, file1); |
129 | 143 | ||
130 | while(1) { | 144 | while (1) { |
131 | while(receivepacket(&ip_port, data, &length) != -1) { | 145 | while (receivepacket(&ip_port, data, &length) != -1) { |
132 | if(rand() % 3 != 1) { /* simulate packet loss */ | 146 | if (rand() % 3 != 1) { /* simulate packet loss */ |
133 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { | 147 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { |
134 | /* if packet is not recognized */ | 148 | /* if packet is not recognized */ |
135 | printf("Received unhandled packet with length: %u\n", length); | 149 | printf("Received unhandled packet with length: %u\n", length); |
136 | } else { | 150 | } else { |
@@ -138,82 +152,98 @@ int main(int argc, char *argv[]) | |||
138 | } | 152 | } |
139 | } | 153 | } |
140 | } | 154 | } |
155 | |||
141 | friend_ip = DHT_getfriendip(friend_id); | 156 | friend_ip = DHT_getfriendip(friend_id); |
142 | if(friend_ip.ip.i != 0) { | 157 | |
143 | if(connection == -1 && friendrequest == -1) { | 158 | if (friend_ip.ip.i != 0) { |
159 | if (connection == -1 && friendrequest == -1) { | ||
144 | printf("Sending friend request to peer:"); | 160 | printf("Sending friend request to peer:"); |
145 | printip(friend_ip); | 161 | printip(friend_ip); |
146 | friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); | 162 | friendrequest = send_friendrequest(friend_id, friend_ip, (uint8_t *) "Hello World", 12); |
147 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ | 163 | /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ |
148 | /* connection = new_connection(friend_ip); */ | 164 | /* connection = new_connection(friend_ip); */ |
149 | } | 165 | } |
150 | if(check_friendrequest(friendrequest) == 1) { | 166 | |
167 | if (check_friendrequest(friendrequest) == 1) { | ||
151 | printf("Started connecting to friend:"); | 168 | printf("Started connecting to friend:"); |
152 | connection = crypto_connect(friend_id, friend_ip); | 169 | connection = crypto_connect(friend_id, friend_ip); |
153 | } | 170 | } |
154 | } | 171 | } |
155 | if(inconnection == -1) { | 172 | |
173 | if (inconnection == -1) { | ||
156 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 174 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
157 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 175 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
158 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 176 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
159 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 177 | inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
160 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); | 178 | inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); |
179 | |||
161 | /* inconnection = incoming_connection(); */ | 180 | /* inconnection = incoming_connection(); */ |
162 | if(inconnection != -1) { | 181 | if (inconnection != -1) { |
163 | printf("Someone connected to us:\n"); | 182 | printf("Someone connected to us:\n"); |
164 | /* printip(connection_ip(inconnection)); */ | 183 | /* printip(connection_ip(inconnection)); */ |
165 | } | 184 | } |
166 | } | 185 | } |
167 | if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { | 186 | |
187 | if (handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { | ||
168 | printf("RECEIVED FRIEND REQUEST: %s\n", request_data); | 188 | printf("RECEIVED FRIEND REQUEST: %s\n", request_data); |
169 | } | 189 | } |
170 | 190 | ||
171 | /* if someone connected to us write what he sends to a file | 191 | /* if someone connected to us write what he sends to a file |
172 | * also send him our file. */ | 192 | * also send him our file. */ |
173 | if(inconnection != -1) { | 193 | if (inconnection != -1) { |
174 | if(write_cryptpacket(inconnection, buffer1, read1)) { | 194 | if (write_cryptpacket(inconnection, buffer1, read1)) { |
175 | printf("Wrote data1.\n"); | 195 | printf("Wrote data1.\n"); |
176 | read1 = fread(buffer1, 1, 128, file1); | 196 | read1 = fread(buffer1, 1, 128, file1); |
177 | } | 197 | } |
198 | |||
178 | read2 = read_cryptpacket(inconnection, buffer2); | 199 | read2 = read_cryptpacket(inconnection, buffer2); |
179 | if(read2 != 0) { | 200 | |
201 | if (read2 != 0) { | ||
180 | printf("Received data1.\n"); | 202 | printf("Received data1.\n"); |
181 | if(!fwrite(buffer2, read2, 1, file2)) { | 203 | |
182 | printf("file write error1\n"); | 204 | if (!fwrite(buffer2, read2, 1, file2)) { |
205 | printf("file write error1\n"); | ||
183 | } | 206 | } |
184 | if(read2 < 128) { | 207 | |
208 | if (read2 < 128) { | ||
185 | printf("Closed file1 %u\n", read2); | 209 | printf("Closed file1 %u\n", read2); |
186 | fclose(file2); | 210 | fclose(file2); |
187 | } | 211 | } |
188 | } | 212 | } |
189 | /* if buffer is empty and the connection timed out. */ | 213 | /* if buffer is empty and the connection timed out. */ |
190 | else if(is_cryptoconnected(inconnection) == 4) { | 214 | else if (is_cryptoconnected(inconnection) == 4) { |
191 | crypto_kill(inconnection); | 215 | crypto_kill(inconnection); |
192 | } | 216 | } |
193 | } | 217 | } |
218 | |||
194 | /* if we are connected to a friend send him data from the file. | 219 | /* if we are connected to a friend send him data from the file. |
195 | * also put what he sends us in a file. */ | 220 | * also put what he sends us in a file. */ |
196 | if(is_cryptoconnected(connection) >= 3) { | 221 | if (is_cryptoconnected(connection) >= 3) { |
197 | if(write_cryptpacket(0, buffer1, read1)) { | 222 | if (write_cryptpacket(0, buffer1, read1)) { |
198 | printf("Wrote data2.\n"); | 223 | printf("Wrote data2.\n"); |
199 | read1 = fread(buffer1, 1, 128, file1); | 224 | read1 = fread(buffer1, 1, 128, file1); |
200 | } | 225 | } |
226 | |||
201 | read2 = read_cryptpacket(0, buffer2); | 227 | read2 = read_cryptpacket(0, buffer2); |
202 | if(read2 != 0) { | 228 | |
229 | if (read2 != 0) { | ||
203 | printf("Received data2.\n"); | 230 | printf("Received data2.\n"); |
204 | if(!fwrite(buffer2, read2, 1, file2)) { | 231 | |
205 | printf("file write error2\n"); | 232 | if (!fwrite(buffer2, read2, 1, file2)) { |
233 | printf("file write error2\n"); | ||
206 | } | 234 | } |
207 | if(read2 < 128) { | 235 | |
236 | if (read2 < 128) { | ||
208 | printf("Closed file2 %u\n", read2); | 237 | printf("Closed file2 %u\n", read2); |
209 | fclose(file2); | 238 | fclose(file2); |
210 | } | 239 | } |
211 | } | 240 | } |
212 | /* if buffer is empty and the connection timed out. */ | 241 | /* if buffer is empty and the connection timed out. */ |
213 | else if(is_cryptoconnected(connection) == 4) { | 242 | else if (is_cryptoconnected(connection) == 4) { |
214 | crypto_kill(connection); | 243 | crypto_kill(connection); |
215 | } | 244 | } |
216 | } | 245 | } |
246 | |||
217 | doDHT(); | 247 | doDHT(); |
218 | doLossless_UDP(); | 248 | doLossless_UDP(); |
219 | doNetCrypto(); | 249 | doNetCrypto(); |
@@ -222,7 +252,7 @@ int main(int argc, char *argv[]) | |||
222 | *c_sleep(300); */ | 252 | *c_sleep(300); */ |
223 | c_sleep(1); | 253 | c_sleep(1); |
224 | } | 254 | } |
225 | 255 | ||
226 | shutdown_networking(); | 256 | shutdown_networking(); |
227 | return 0; | 257 | return 0; |
228 | } | 258 | } |
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c index 873a73f8..f839be57 100644 --- a/testing/DHT_sendfiletest.c +++ b/testing/DHT_sendfiletest.c | |||
@@ -1,18 +1,18 @@ | |||
1 | /* DHT sendfiletest | 1 | /* DHT sendfiletest |
2 | * | 2 | * |
3 | * Sends the data from a file to another client. | 3 | * Sends the data from a file to another client. |
4 | * Receives the file data that that client sends us. | 4 | * Receives the file data that that client sends us. |
5 | * | 5 | * |
6 | * NOTE: this program simulates 33% packet loss. | 6 | * NOTE: this program simulates 33% packet loss. |
7 | * | 7 | * |
8 | * Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c | 8 | * Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c |
9 | * | 9 | * |
10 | * Command line arguments are the ip and port of a node (for bootstrapping), the | 10 | * Command line arguments are the ip and port of a node (for bootstrapping), the |
11 | * client_id (32 bytes) of the friend you want to send the data in filename to and | 11 | * client_id (32 bytes) of the friend you want to send the data in filename to and |
12 | * the client_id this node will take. | 12 | * the client_id this node will take. |
13 | * | 13 | * |
14 | * Saves all received data to: received.txt | 14 | * Saves all received data to: received.txt |
15 | * | 15 | * |
16 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg | 16 | * EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg |
17 | * | 17 | * |
18 | * Copyright (C) 2013 Tox project All Rights Reserved. | 18 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -31,9 +31,9 @@ | |||
31 | * | 31 | * |
32 | * You should have received a copy of the GNU General Public License | 32 | * You should have received a copy of the GNU General Public License |
33 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 33 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
34 | * | 34 | * |
35 | */ | 35 | */ |
36 | 36 | ||
37 | #include "../core/network.h" | 37 | #include "../core/network.h" |
38 | #include "../core/DHT.h" | 38 | #include "../core/DHT.h" |
39 | #include "../core/Lossless_UDP.h" | 39 | #include "../core/Lossless_UDP.h" |
@@ -56,56 +56,61 @@ | |||
56 | 56 | ||
57 | void printip(IP_Port ip_port) | 57 | void printip(IP_Port ip_port) |
58 | { | 58 | { |
59 | printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 59 | printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], |
60 | ntohs(ip_port.port)); | ||
60 | } | 61 | } |
61 | 62 | ||
62 | int main(int argc, char *argv[]) | 63 | int main(int argc, char *argv[]) |
63 | { | 64 | { |
64 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | 65 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); |
65 | 66 | ||
66 | if (argc < 6) { | 67 | if (argc < 6) { |
67 | printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]); | 68 | printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]); |
68 | exit(0); | 69 | exit(0); |
69 | } | 70 | } |
71 | |||
70 | DHT_addfriend((uint8_t *)argv[3]); | 72 | DHT_addfriend((uint8_t *)argv[3]); |
71 | IP_Port friend_ip; | 73 | IP_Port friend_ip; |
72 | int connection = -1; | 74 | int connection = -1; |
73 | int inconnection = -1; | 75 | int inconnection = -1; |
74 | 76 | ||
75 | //initialize networking | 77 | //initialize networking |
76 | //bind to ip 0.0.0.0:PORT | 78 | //bind to ip 0.0.0.0:PORT |
77 | IP ip; | 79 | IP ip; |
78 | ip.i = 0; | 80 | ip.i = 0; |
79 | init_networking(ip, PORT); | 81 | init_networking(ip, PORT); |
80 | 82 | ||
81 | memcpy(self_client_id, argv[5], 32); | 83 | memcpy(self_client_id, argv[5], 32); |
82 | 84 | ||
83 | 85 | ||
84 | perror("Initialization"); | 86 | perror("Initialization"); |
85 | IP_Port bootstrap_ip_port; | 87 | IP_Port bootstrap_ip_port; |
86 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 88 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
87 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 89 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
88 | DHT_bootstrap(bootstrap_ip_port); | 90 | DHT_bootstrap(bootstrap_ip_port); |
89 | 91 | ||
90 | IP_Port ip_port; | 92 | IP_Port ip_port; |
91 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 93 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
92 | uint32_t length; | 94 | uint32_t length; |
93 | 95 | ||
94 | uint8_t buffer1[128]; | 96 | uint8_t buffer1[128]; |
95 | int read1 = 0; | 97 | int read1 = 0; |
96 | uint8_t buffer2[128]; | 98 | uint8_t buffer2[128]; |
97 | int read2 = 0; | 99 | int read2 = 0; |
98 | FILE *file1 = fopen(argv[4], "rb"); | 100 | FILE *file1 = fopen(argv[4], "rb"); |
101 | |||
99 | if (file1 == NULL) { | 102 | if (file1 == NULL) { |
100 | printf("Error opening file.\n"); | 103 | printf("Error opening file.\n"); |
101 | return 1; | 104 | return 1; |
102 | } | 105 | } |
106 | |||
103 | FILE *file2 = fopen("received.txt", "wb"); | 107 | FILE *file2 = fopen("received.txt", "wb"); |
108 | |||
104 | if (file2 == NULL) | 109 | if (file2 == NULL) |
105 | return 1; | 110 | return 1; |
106 | 111 | ||
107 | read1 = fread(buffer1, 1, 128, file1); | 112 | read1 = fread(buffer1, 1, 128, file1); |
108 | 113 | ||
109 | while (1) { | 114 | while (1) { |
110 | while (receivepacket(&ip_port, data, &length) != -1) { | 115 | while (receivepacket(&ip_port, data, &length) != -1) { |
111 | if (rand() % 3 != 1) { /* simulate packet loss */ | 116 | if (rand() % 3 != 1) { /* simulate packet loss */ |
@@ -115,7 +120,9 @@ int main(int argc, char *argv[]) | |||
115 | printf("Received handled packet with length: %u\n", length); | 120 | printf("Received handled packet with length: %u\n", length); |
116 | } | 121 | } |
117 | } | 122 | } |
123 | |||
118 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); | 124 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); |
125 | |||
119 | if (friend_ip.ip.i != 0) { | 126 | if (friend_ip.ip.i != 0) { |
120 | if (connection == -1) { | 127 | if (connection == -1) { |
121 | printf("Started connecting to friend:"); | 128 | printf("Started connecting to friend:"); |
@@ -123,13 +130,16 @@ int main(int argc, char *argv[]) | |||
123 | connection = new_connection(friend_ip); | 130 | connection = new_connection(friend_ip); |
124 | } | 131 | } |
125 | } | 132 | } |
133 | |||
126 | if (inconnection == -1) { | 134 | if (inconnection == -1) { |
127 | inconnection = incoming_connection(); | 135 | inconnection = incoming_connection(); |
136 | |||
128 | if (inconnection != -1) { | 137 | if (inconnection != -1) { |
129 | printf("Someone connected to us:"); | 138 | printf("Someone connected to us:"); |
130 | printip(connection_ip(inconnection)); | 139 | printip(connection_ip(inconnection)); |
131 | } | 140 | } |
132 | } | 141 | } |
142 | |||
133 | /* if someone connected to us write what he sends to a file */ | 143 | /* if someone connected to us write what he sends to a file */ |
134 | /* also send him our file. */ | 144 | /* also send him our file. */ |
135 | if (inconnection != -1) { | 145 | if (inconnection != -1) { |
@@ -137,16 +147,21 @@ int main(int argc, char *argv[]) | |||
137 | printf("Wrote data.\n"); | 147 | printf("Wrote data.\n"); |
138 | read1 = fread(buffer1, 1, 128, file1); | 148 | read1 = fread(buffer1, 1, 128, file1); |
139 | } | 149 | } |
150 | |||
140 | read2 = read_packet(inconnection, buffer2); | 151 | read2 = read_packet(inconnection, buffer2); |
152 | |||
141 | if (read2 != 0) { | 153 | if (read2 != 0) { |
142 | printf("Received data.\n"); | 154 | printf("Received data.\n"); |
155 | |||
143 | if (!fwrite(buffer2, read2, 1, file2)) | 156 | if (!fwrite(buffer2, read2, 1, file2)) |
144 | printf("file write error\n"); | 157 | printf("file write error\n"); |
158 | |||
145 | if (read2 < 128) { | 159 | if (read2 < 128) { |
146 | fclose(file2); | 160 | fclose(file2); |
147 | } | 161 | } |
148 | } | 162 | } |
149 | } | 163 | } |
164 | |||
150 | /* if we are connected to a friend send him data from the file. | 165 | /* if we are connected to a friend send him data from the file. |
151 | * also put what he sends us in a file. */ | 166 | * also put what he sends us in a file. */ |
152 | if (is_connected(connection) == 3) { | 167 | if (is_connected(connection) == 3) { |
@@ -154,15 +169,20 @@ int main(int argc, char *argv[]) | |||
154 | printf("Wrote data.\n"); | 169 | printf("Wrote data.\n"); |
155 | read1 = fread(buffer1, 1, 128, file1); | 170 | read1 = fread(buffer1, 1, 128, file1); |
156 | } | 171 | } |
172 | |||
157 | read2 = read_packet(0, buffer2); | 173 | read2 = read_packet(0, buffer2); |
174 | |||
158 | if (read2 != 0) { | 175 | if (read2 != 0) { |
159 | printf("Received data.\n"); | 176 | printf("Received data.\n"); |
160 | if(!fwrite(buffer2, read2, 1, file2)) | 177 | |
161 | printf("file write error\n"); | 178 | if (!fwrite(buffer2, read2, 1, file2)) |
162 | if(read2 < 128) | 179 | printf("file write error\n"); |
180 | |||
181 | if (read2 < 128) | ||
163 | fclose(file2); | 182 | fclose(file2); |
164 | } | 183 | } |
165 | } | 184 | } |
185 | |||
166 | doDHT(); | 186 | doDHT(); |
167 | doLossless_UDP(); | 187 | doLossless_UDP(); |
168 | /* print_clientlist(); | 188 | /* print_clientlist(); |
@@ -170,7 +190,7 @@ int main(int argc, char *argv[]) | |||
170 | * c_sleep(300); */ | 190 | * c_sleep(300); */ |
171 | c_sleep(1); | 191 | c_sleep(1); |
172 | } | 192 | } |
173 | 193 | ||
174 | shutdown_networking(); | 194 | shutdown_networking(); |
175 | return 0; | 195 | return 0; |
176 | } \ No newline at end of file | 196 | } \ No newline at end of file |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index c5b367cf..3883f3a7 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -1,11 +1,11 @@ | |||
1 | /* DHT test | 1 | /* DHT test |
2 | * A file with a main that runs our DHT for testing. | 2 | * A file with a main that runs our DHT for testing. |
3 | * | 3 | * |
4 | * Compile with: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_test.c | 4 | * Compile with: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_test.c |
5 | * | 5 | * |
6 | * Command line arguments are the ip, port and public key of a node. | 6 | * Command line arguments are the ip, port and public key of a node. |
7 | * EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | 7 | * EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
8 | * | 8 | * |
9 | * The test will then ask you for the id (in hex format) of the friend you wish to add | 9 | * The test will then ask you for the id (in hex format) of the friend you wish to add |
10 | * | 10 | * |
11 | * Copyright (C) 2013 Tox project All Rights Reserved. | 11 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -24,9 +24,9 @@ | |||
24 | * | 24 | * |
25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
26 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
27 | * | 27 | * |
28 | */ | 28 | */ |
29 | 29 | ||
30 | //#include "../core/network.h" | 30 | //#include "../core/network.h" |
31 | #include "../core/DHT.c" | 31 | #include "../core/DHT.c" |
32 | #include "../core/friend_requests.c" | 32 | #include "../core/friend_requests.c" |
@@ -53,19 +53,22 @@ void print_clientlist() | |||
53 | uint32_t i, j; | 53 | uint32_t i, j; |
54 | IP_Port p_ip; | 54 | IP_Port p_ip; |
55 | printf("___________________CLOSE________________________________\n"); | 55 | printf("___________________CLOSE________________________________\n"); |
56 | for(i = 0; i < 32; i++) { | 56 | |
57 | for (i = 0; i < 32; i++) { | ||
57 | printf("ClientID: "); | 58 | printf("ClientID: "); |
58 | for(j = 0; j < 32; j++) { | 59 | |
60 | for (j = 0; j < 32; j++) { | ||
59 | printf("%02hhX", close_clientlist[i].client_id[j]); | 61 | printf("%02hhX", close_clientlist[i].client_id[j]); |
60 | } | 62 | } |
63 | |||
61 | p_ip = close_clientlist[i].ip_port; | 64 | p_ip = close_clientlist[i].ip_port; |
62 | printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); | 65 | printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); |
63 | printf("\nTimestamp: %llu",(long long unsigned int) close_clientlist[i].timestamp); | 66 | printf("\nTimestamp: %llu", (long long unsigned int) close_clientlist[i].timestamp); |
64 | printf("\nLast pinged: %llu\n",(long long unsigned int) close_clientlist[i].last_pinged); | 67 | printf("\nLast pinged: %llu\n", (long long unsigned int) close_clientlist[i].last_pinged); |
65 | p_ip = close_clientlist[i].ret_ip_port; | 68 | p_ip = close_clientlist[i].ret_ip_port; |
66 | printf("OUR IP: %u.%u.%u.%u Port: %u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); | 69 | printf("OUR IP: %u.%u.%u.%u Port: %u\n", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); |
67 | printf("Timestamp: %llu\n",(long long unsigned int) close_clientlist[i].ret_timestamp); | 70 | printf("Timestamp: %llu\n", (long long unsigned int) close_clientlist[i].ret_timestamp); |
68 | } | 71 | } |
69 | } | 72 | } |
70 | 73 | ||
71 | void print_friendlist() | 74 | void print_friendlist() |
@@ -73,78 +76,91 @@ void print_friendlist() | |||
73 | uint32_t i, j, k; | 76 | uint32_t i, j, k; |
74 | IP_Port p_ip; | 77 | IP_Port p_ip; |
75 | printf("_________________FRIENDS__________________________________\n"); | 78 | printf("_________________FRIENDS__________________________________\n"); |
76 | for(k = 0; k < num_friends; k++) { | 79 | |
80 | for (k = 0; k < num_friends; k++) { | ||
77 | printf("FRIEND %u\n", k); | 81 | printf("FRIEND %u\n", k); |
78 | printf("ID: "); | 82 | printf("ID: "); |
79 | for(j = 0; j < 32; j++) { | 83 | |
84 | for (j = 0; j < 32; j++) { | ||
80 | printf("%c", friends_list[k].client_id[j]); | 85 | printf("%c", friends_list[k].client_id[j]); |
81 | } | 86 | } |
87 | |||
82 | p_ip = DHT_getfriendip(friends_list[k].client_id); | 88 | p_ip = DHT_getfriendip(friends_list[k].client_id); |
83 | printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); | 89 | printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); |
84 | 90 | ||
85 | printf("\nCLIENTS IN LIST:\n\n"); | 91 | printf("\nCLIENTS IN LIST:\n\n"); |
86 | 92 | ||
87 | for(i = 0; i < 4; i++) { | 93 | for (i = 0; i < 4; i++) { |
88 | printf("ClientID: "); | 94 | printf("ClientID: "); |
89 | for(j = 0; j < 32; j++) { | 95 | |
90 | if(friends_list[k].client_list[i].client_id[j] < 16) | 96 | for (j = 0; j < 32; j++) { |
97 | if (friends_list[k].client_list[i].client_id[j] < 16) | ||
91 | printf("0"); | 98 | printf("0"); |
99 | |||
92 | printf("%hhX", friends_list[k].client_list[i].client_id[j]); | 100 | printf("%hhX", friends_list[k].client_list[i].client_id[j]); |
93 | } | 101 | } |
102 | |||
94 | p_ip = friends_list[k].client_list[i].ip_port; | 103 | p_ip = friends_list[k].client_list[i].ip_port; |
95 | printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); | 104 | printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); |
96 | printf("\nTimestamp: %llu",(long long unsigned int) friends_list[k].client_list[i].timestamp); | 105 | printf("\nTimestamp: %llu", (long long unsigned int) friends_list[k].client_list[i].timestamp); |
97 | printf("\nLast pinged: %llu\n",(long long unsigned int) friends_list[k].client_list[i].last_pinged); | 106 | printf("\nLast pinged: %llu\n", (long long unsigned int) friends_list[k].client_list[i].last_pinged); |
98 | p_ip = friends_list[k].client_list[i].ret_ip_port; | 107 | p_ip = friends_list[k].client_list[i].ret_ip_port; |
99 | printf("ret IP: %u.%u.%u.%u:%u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); | 108 | printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port)); |
100 | printf("Timestamp: %llu\n", (long long unsigned int)friends_list[k].client_list[i].ret_timestamp); | 109 | printf("Timestamp: %llu\n", (long long unsigned int)friends_list[k].client_list[i].ret_timestamp); |
101 | } | 110 | } |
102 | } | 111 | } |
103 | } | 112 | } |
104 | 113 | ||
105 | void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) | 114 | void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) |
106 | { | 115 | { |
107 | uint32_t i; | 116 | uint32_t i; |
108 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | 117 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); |
109 | printf("--------------------BEGIN-----------------------------\n"); | 118 | printf("--------------------BEGIN-----------------------------\n"); |
110 | for(i = 0; i < length; i++) { | 119 | |
111 | if(data[i] < 16) | 120 | for (i = 0; i < length; i++) { |
121 | if (data[i] < 16) | ||
112 | printf("0"); | 122 | printf("0"); |
113 | printf("%hhX",data[i]); | 123 | |
124 | printf("%hhX", data[i]); | ||
114 | } | 125 | } |
126 | |||
115 | printf("\n--------------------END-----------------------------\n\n\n"); | 127 | printf("\n--------------------END-----------------------------\n\n\n"); |
116 | } | 128 | } |
117 | 129 | ||
118 | int main(int argc, char *argv[]) | 130 | int main(int argc, char *argv[]) |
119 | { | 131 | { |
120 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | 132 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); |
121 | 133 | ||
122 | if (argc < 4) { | 134 | if (argc < 4) { |
123 | printf("usage %s ip port public_key\n", argv[0]); | 135 | printf("usage %s ip port public_key\n", argv[0]); |
124 | exit(0); | 136 | exit(0); |
125 | } | 137 | } |
138 | |||
126 | new_keys(); | 139 | new_keys(); |
127 | printf("OUR ID: "); | 140 | printf("OUR ID: "); |
128 | uint32_t i; | 141 | uint32_t i; |
129 | for(i = 0; i < 32; i++) { | 142 | |
130 | if(self_public_key[i] < 16) | 143 | for (i = 0; i < 32; i++) { |
144 | if (self_public_key[i] < 16) | ||
131 | printf("0"); | 145 | printf("0"); |
132 | printf("%hhX",self_public_key[i]); | 146 | |
147 | printf("%hhX", self_public_key[i]); | ||
133 | } | 148 | } |
134 | 149 | ||
135 | char temp_id[128]; | 150 | char temp_id[128]; |
136 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); | 151 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); |
137 | if(scanf("%s", temp_id) != 1) | 152 | |
153 | if (scanf("%s", temp_id) != 1) | ||
138 | exit(0); | 154 | exit(0); |
139 | 155 | ||
140 | DHT_addfriend(hex_string_to_bin(temp_id)); | 156 | DHT_addfriend(hex_string_to_bin(temp_id)); |
141 | 157 | ||
142 | /* initialize networking */ | 158 | /* initialize networking */ |
143 | /* bind to ip 0.0.0.0:PORT */ | 159 | /* bind to ip 0.0.0.0:PORT */ |
144 | IP ip; | 160 | IP ip; |
145 | ip.i = 0; | 161 | ip.i = 0; |
146 | init_networking(ip, PORT); | 162 | init_networking(ip, PORT); |
147 | 163 | ||
148 | 164 | ||
149 | perror("Initialization"); | 165 | perror("Initialization"); |
150 | IP_Port bootstrap_ip_port; | 166 | IP_Port bootstrap_ip_port; |
@@ -155,37 +171,37 @@ int main(int argc, char *argv[]) | |||
155 | * bootstrap_ip_port.ip.c[3] = 1; */ | 171 | * bootstrap_ip_port.ip.c[3] = 1; */ |
156 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 172 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
157 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 173 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
158 | 174 | ||
159 | /* | 175 | /* |
160 | IP_Port ip_port; | 176 | IP_Port ip_port; |
161 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 177 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
162 | uint32_t length; | 178 | uint32_t length; |
163 | */ | 179 | */ |
164 | 180 | ||
165 | DHT_init(); | 181 | DHT_init(); |
166 | friendreq_init(); | 182 | friendreq_init(); |
167 | 183 | ||
168 | while(1) { | 184 | while (1) { |
169 | 185 | ||
170 | doDHT(); | 186 | doDHT(); |
171 | 187 | ||
172 | /* slvrTODO: | 188 | /* slvrTODO: |
173 | while(receivepacket(&ip_port, data, &length) != -1) { | 189 | while(receivepacket(&ip_port, data, &length) != -1) { |
174 | if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { | 190 | if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { |
175 | //unhandled packet | 191 | //unhandled packet |
176 | printpacket(data, length, ip_port); | 192 | printpacket(data, length, ip_port); |
177 | } else { | 193 | } else { |
178 | printf("Received handled packet with length: %u\n", length); | 194 | printf("Received handled packet with length: %u\n", length); |
179 | } | 195 | } |
180 | } | 196 | } |
181 | */ | 197 | */ |
182 | networking_poll(); | 198 | networking_poll(); |
183 | 199 | ||
184 | print_clientlist(); | 200 | print_clientlist(); |
185 | print_friendlist(); | 201 | print_friendlist(); |
186 | c_sleep(300); | 202 | c_sleep(300); |
187 | } | 203 | } |
188 | 204 | ||
189 | shutdown_networking(); | 205 | shutdown_networking(); |
190 | return 0; | 206 | return 0; |
191 | } | 207 | } |
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 7efafa4f..575be2bd 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c | |||
@@ -1,11 +1,11 @@ | |||
1 | /* Lossless_UDP testclient | 1 | /* Lossless_UDP testclient |
2 | * A program that connects and sends a file using our lossless UDP algorithm. | 2 | * A program that connects and sends a file using our lossless UDP algorithm. |
3 | * NOTE: this program simulates a 33% packet loss. | 3 | * NOTE: this program simulates a 33% packet loss. |
4 | * | 4 | * |
5 | * Best used in combination with Lossless_UDP_testserver | 5 | * Best used in combination with Lossless_UDP_testserver |
6 | * | 6 | * |
7 | * Compile with: gcc -O2 -Wall -lsodium -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c | 7 | * Compile with: gcc -O2 -Wall -lsodium -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c |
8 | * | 8 | * |
9 | * Command line arguments are the ip and port to connect and send the file to. | 9 | * Command line arguments are the ip and port to connect and send the file to. |
10 | * EX: ./testclient 127.0.0.1 33445 filename.txt | 10 | * EX: ./testclient 127.0.0.1 33445 filename.txt |
11 | * | 11 | * |
@@ -25,7 +25,7 @@ | |||
25 | * | 25 | * |
26 | * You should have received a copy of the GNU General Public License | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | #include "../core/network.h" | 31 | #include "../core/network.h" |
@@ -49,17 +49,21 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) | |||
49 | uint32_t i; | 49 | uint32_t i; |
50 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | 50 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); |
51 | printf("--------------------BEGIN-----------------------------\n"); | 51 | printf("--------------------BEGIN-----------------------------\n"); |
52 | |||
52 | for (i = 0; i < length; i++) { | 53 | for (i = 0; i < length; i++) { |
53 | if (data[i] < 16) | 54 | if (data[i] < 16) |
54 | printf("0"); | 55 | printf("0"); |
55 | printf("%hhX",data[i]); | 56 | |
57 | printf("%hhX", data[i]); | ||
56 | } | 58 | } |
59 | |||
57 | printf("\n--------------------END-----------------------------\n\n\n"); | 60 | printf("\n--------------------END-----------------------------\n\n\n"); |
58 | } | 61 | } |
59 | 62 | ||
60 | void printip(IP_Port ip_port) | 63 | void printip(IP_Port ip_port) |
61 | { | 64 | { |
62 | printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], ntohs(ip_port.port)); | 65 | printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], |
66 | ntohs(ip_port.port)); | ||
63 | } | 67 | } |
64 | /* | 68 | /* |
65 | void printpackets(Data test) | 69 | void printpackets(Data test) |
@@ -80,9 +84,9 @@ void printconnection(int connection_id) | |||
80 | printf("--------------------BEGIN---------------------\n"); | 84 | printf("--------------------BEGIN---------------------\n"); |
81 | IP_Port ip_port = connections[connection_id].ip_port; | 85 | IP_Port ip_port = connections[connection_id].ip_port; |
82 | printf("IP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 86 | printf("IP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); |
83 | printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status, | 87 | printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status, |
84 | connections[connection_id].inbound, connections[connection_id].SYNC_rate); | 88 | connections[connection_id].inbound, connections[connection_id].SYNC_rate); |
85 | printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate, | 89 | printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate, |
86 | connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv); | 90 | connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv); |
87 | int i; | 91 | int i; |
88 | for(i =0; i < MAX_QUEUE_NUM; i++) | 92 | for(i =0; i < MAX_QUEUE_NUM; i++) |
@@ -97,12 +101,12 @@ void printconnection(int connection_id) | |||
97 | } | 101 | } |
98 | Data sendbuffer[MAX_QUEUE_NUM]; | 102 | Data sendbuffer[MAX_QUEUE_NUM]; |
99 | Data recvbuffer[MAX_QUEUE_NUM]; | 103 | Data recvbuffer[MAX_QUEUE_NUM]; |
100 | printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n", | 104 | printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n", |
101 | connections[connection_id].recv_packetnum, | 105 | connections[connection_id].recv_packetnum, |
102 | connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum, | 106 | connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum, |
103 | connections[connection_id].successful_sent, | 107 | connections[connection_id].successful_sent, |
104 | connections[connection_id].successful_read); | 108 | connections[connection_id].successful_read); |
105 | 109 | ||
106 | printf("req packets: \n"); | 110 | printf("req packets: \n"); |
107 | for(i = 0; i < BUFFER_PACKET_NUM; i++) | 111 | for(i = 0; i < BUFFER_PACKET_NUM; i++) |
108 | { | 112 | { |
@@ -112,7 +116,7 @@ void printconnection(int connection_id) | |||
112 | connections[connection_id].recv_counter, connections[connection_id].send_counter); | 116 | connections[connection_id].recv_counter, connections[connection_id].send_counter); |
113 | 117 | ||
114 | printf("--------------------END---------------------\n"); | 118 | printf("--------------------END---------------------\n"); |
115 | 119 | ||
116 | } | 120 | } |
117 | */ | 121 | */ |
118 | 122 | ||
@@ -120,26 +124,26 @@ void printconnection(int connection_id) | |||
120 | /*run doLossless_UDP(); */ | 124 | /*run doLossless_UDP(); */ |
121 | void Lossless_UDP() | 125 | void Lossless_UDP() |
122 | { | 126 | { |
123 | /* IP_Port ip_port; | 127 | /* IP_Port ip_port; |
124 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 128 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
125 | uint32_t length; | 129 | uint32_t length; |
126 | while (receivepacket(&ip_port, data, &length) != -1) { | 130 | while (receivepacket(&ip_port, data, &length) != -1) { |
127 | printf("packet with length: %u\n", length); */ | 131 | printf("packet with length: %u\n", length); */ |
128 | /* if(rand() % 3 != 1)//add packet loss | 132 | /* if(rand() % 3 != 1)//add packet loss |
129 | { */ | 133 | { */ |
130 | /* | 134 | /* |
131 | if (LosslessUDP_handlepacket(data, length, ip_port)) | 135 | if (LosslessUDP_handlepacket(data, length, ip_port)) |
132 | printpacket(data, length, ip_port); | 136 | printpacket(data, length, ip_port); |
133 | else | 137 | else |
134 | printf("Received handled packet with length: %u\n", length); //printconnection(0); */ | 138 | printf("Received handled packet with length: %u\n", length); //printconnection(0); */ |
135 | 139 | ||
136 | /* } */ | 140 | /* } */ |
137 | /* }*/ | 141 | /* }*/ |
138 | 142 | ||
139 | networking_poll(); | 143 | networking_poll(); |
140 | 144 | ||
141 | doLossless_UDP(); | 145 | doLossless_UDP(); |
142 | 146 | ||
143 | } | 147 | } |
144 | 148 | ||
145 | int main(int argc, char *argv[]) | 149 | int main(int argc, char *argv[]) |
@@ -148,15 +152,16 @@ int main(int argc, char *argv[]) | |||
148 | printf("usage: %s ip port filename\n", argv[0]); | 152 | printf("usage: %s ip port filename\n", argv[0]); |
149 | exit(0); | 153 | exit(0); |
150 | } | 154 | } |
151 | 155 | ||
152 | uint8_t buffer[512]; | 156 | uint8_t buffer[512]; |
153 | int read; | 157 | int read; |
154 | 158 | ||
155 | FILE *file = fopen(argv[3], "rb"); | 159 | FILE *file = fopen(argv[3], "rb"); |
160 | |||
156 | if (file == NULL) | 161 | if (file == NULL) |
157 | return 1; | 162 | return 1; |
158 | 163 | ||
159 | 164 | ||
160 | /* initialize networking */ | 165 | /* initialize networking */ |
161 | /* bind to ip 0.0.0.0:PORT */ | 166 | /* bind to ip 0.0.0.0:PORT */ |
162 | IP ip; | 167 | IP ip; |
@@ -169,36 +174,43 @@ int main(int argc, char *argv[]) | |||
169 | printip(serverip); | 174 | printip(serverip); |
170 | int connection = new_connection(serverip); | 175 | int connection = new_connection(serverip); |
171 | uint64_t timer = current_time(); | 176 | uint64_t timer = current_time(); |
177 | |||
172 | while (1) { | 178 | while (1) { |
173 | /* printconnection(connection); */ | 179 | /* printconnection(connection); */ |
174 | Lossless_UDP(); | 180 | Lossless_UDP(); |
181 | |||
175 | if (is_connected(connection) == 3) { | 182 | if (is_connected(connection) == 3) { |
176 | printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); | 183 | printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); |
177 | break; | 184 | break; |
178 | } | 185 | } |
186 | |||
179 | if (is_connected(connection) == 0) { | 187 | if (is_connected(connection) == 0) { |
180 | printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); | 188 | printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); |
181 | return 1; | 189 | return 1; |
182 | } | 190 | } |
191 | |||
183 | c_sleep(1); | 192 | c_sleep(1); |
184 | } | 193 | } |
194 | |||
185 | timer = current_time(); | 195 | timer = current_time(); |
186 | 196 | ||
187 | LosslessUDP_init(); | 197 | LosslessUDP_init(); |
188 | 198 | ||
189 | /*read first part of file */ | 199 | /*read first part of file */ |
190 | read = fread(buffer, 1, 512, file); | 200 | read = fread(buffer, 1, 512, file); |
191 | 201 | ||
192 | while (1) { | 202 | while (1) { |
193 | /* printconnection(connection); */ | 203 | /* printconnection(connection); */ |
194 | Lossless_UDP(); | 204 | Lossless_UDP(); |
205 | |||
195 | if (is_connected(connection) == 3) { | 206 | if (is_connected(connection) == 3) { |
196 | 207 | ||
197 | if (write_packet(connection, buffer, read)) { | 208 | if (write_packet(connection, buffer, read)) { |
198 | /* printf("Wrote data.\n"); */ | 209 | /* printf("Wrote data.\n"); */ |
199 | read = fread(buffer, 1, 512, file); | 210 | read = fread(buffer, 1, 512, file); |
200 | 211 | ||
201 | } | 212 | } |
213 | |||
202 | /* printf("%u\n", sendqueue(connection)); */ | 214 | /* printf("%u\n", sendqueue(connection)); */ |
203 | if (sendqueue(connection) == 0) { | 215 | if (sendqueue(connection) == 0) { |
204 | if (read == 0) { | 216 | if (read == 0) { |
@@ -206,13 +218,13 @@ int main(int argc, char *argv[]) | |||
206 | break; | 218 | break; |
207 | } | 219 | } |
208 | } | 220 | } |
209 | } | 221 | } else { |
210 | else { | ||
211 | printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); | 222 | printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); |
212 | return 0; | 223 | return 0; |
213 | } | 224 | } |
225 | |||
214 | /* c_sleep(1); */ | 226 | /* c_sleep(1); */ |
215 | } | 227 | } |
216 | 228 | ||
217 | return 0; | 229 | return 0; |
218 | } | 230 | } |
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index bc4fed7a..dec50d7f 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c | |||
@@ -1,11 +1,11 @@ | |||
1 | /* Lossless_UDP testserver | 1 | /* Lossless_UDP testserver |
2 | * A program that waits for a lossless UDP connection and then saves all the data received to a file. | 2 | * A program that waits for a lossless UDP connection and then saves all the data received to a file. |
3 | * NOTE: this program simulates a 33% packet loss. | 3 | * NOTE: this program simulates a 33% packet loss. |
4 | * | 4 | * |
5 | * Best used in combination with Lossless_UDP_testclient | 5 | * Best used in combination with Lossless_UDP_testclient |
6 | * | 6 | * |
7 | * Compile with: gcc -O2 -Wall -lsodium -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c | 7 | * Compile with: gcc -O2 -Wall -lsodium -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c |
8 | * | 8 | * |
9 | * Command line argument is the name of the file to save what we receive to. | 9 | * Command line argument is the name of the file to save what we receive to. |
10 | * EX: ./testserver filename1.txt | 10 | * EX: ./testserver filename1.txt |
11 | * | 11 | * |
@@ -25,7 +25,7 @@ | |||
25 | * | 25 | * |
26 | * You should have received a copy of the GNU General Public License | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 27 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | #include "../core/network.h" | 31 | #include "../core/network.h" |
@@ -50,11 +50,14 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) | |||
50 | uint32_t i; | 50 | uint32_t i; |
51 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | 51 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); |
52 | printf("--------------------BEGIN-----------------------------\n"); | 52 | printf("--------------------BEGIN-----------------------------\n"); |
53 | |||
53 | for (i = 0; i < length; i++) { | 54 | for (i = 0; i < length; i++) { |
54 | if(data[i] < 16) | 55 | if (data[i] < 16) |
55 | printf("0"); | 56 | printf("0"); |
56 | printf("%hhX",data[i]); | 57 | |
58 | printf("%hhX", data[i]); | ||
57 | } | 59 | } |
60 | |||
58 | printf("\n--------------------END-----------------------------\n\n\n"); | 61 | printf("\n--------------------END-----------------------------\n\n\n"); |
59 | } | 62 | } |
60 | 63 | ||
@@ -77,9 +80,9 @@ void printconnection(int connection_id) | |||
77 | printf("--------------------BEGIN---------------------\n"); | 80 | printf("--------------------BEGIN---------------------\n"); |
78 | IP_Port ip_port = connections[connection_id].ip_port; | 81 | IP_Port ip_port = connections[connection_id].ip_port; |
79 | printf("IP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 82 | printf("IP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); |
80 | printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status, | 83 | printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status, |
81 | connections[connection_id].inbound, connections[connection_id].SYNC_rate); | 84 | connections[connection_id].inbound, connections[connection_id].SYNC_rate); |
82 | printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate, | 85 | printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate, |
83 | connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv); | 86 | connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv); |
84 | int i; | 87 | int i; |
85 | for(i =0; i < MAX_QUEUE_NUM; i++) | 88 | for(i =0; i < MAX_QUEUE_NUM; i++) |
@@ -94,12 +97,12 @@ void printconnection(int connection_id) | |||
94 | } | 97 | } |
95 | Data sendbuffer[MAX_QUEUE_NUM]; | 98 | Data sendbuffer[MAX_QUEUE_NUM]; |
96 | Data recvbuffer[MAX_QUEUE_NUM]; | 99 | Data recvbuffer[MAX_QUEUE_NUM]; |
97 | printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n", | 100 | printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n", |
98 | connections[connection_id].recv_packetnum, | 101 | connections[connection_id].recv_packetnum, |
99 | connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum, | 102 | connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum, |
100 | connections[connection_id].successful_sent, | 103 | connections[connection_id].successful_sent, |
101 | connections[connection_id].successful_read); | 104 | connections[connection_id].successful_read); |
102 | 105 | ||
103 | printf("req packets: \n"); | 106 | printf("req packets: \n"); |
104 | for(i = 0; i < BUFFER_PACKET_NUM; i++) | 107 | for(i = 0; i < BUFFER_PACKET_NUM; i++) |
105 | { | 108 | { |
@@ -109,7 +112,7 @@ void printconnection(int connection_id) | |||
109 | connections[connection_id].recv_counter, connections[connection_id].send_counter); | 112 | connections[connection_id].recv_counter, connections[connection_id].send_counter); |
110 | 113 | ||
111 | printf("--------------------END---------------------\n"); | 114 | printf("--------------------END---------------------\n"); |
112 | 115 | ||
113 | } | 116 | } |
114 | */ | 117 | */ |
115 | 118 | ||
@@ -121,20 +124,20 @@ void Lossless_UDP() | |||
121 | // uint8_t data[MAX_UDP_PACKET_SIZE]; | 124 | // uint8_t data[MAX_UDP_PACKET_SIZE]; |
122 | // uint32_t length; | 125 | // uint32_t length; |
123 | // while (receivepacket(&ip_port, data, &length) != -1) { | 126 | // while (receivepacket(&ip_port, data, &length) != -1) { |
124 | //if(rand() % 3 != 1)//add packet loss | 127 | //if(rand() % 3 != 1)//add packet loss |
125 | //{ | 128 | //{ |
126 | // if (LosslessUDP_handlepacket(data, length, ip_port)) { | 129 | // if (LosslessUDP_handlepacket(data, length, ip_port)) { |
127 | // printpacket(data, length, ip_port); | 130 | // printpacket(data, length, ip_port); |
128 | // } else { | 131 | // } else { |
129 | //printconnection(0); | 132 | //printconnection(0); |
130 | // printf("Received handled packet with length: %u\n", length); | 133 | // printf("Received handled packet with length: %u\n", length); |
131 | // } | 134 | // } |
132 | //} | 135 | //} |
133 | // } | 136 | // } |
134 | 137 | ||
135 | networking_poll(); | 138 | networking_poll(); |
136 | 139 | ||
137 | doLossless_UDP(); | 140 | doLossless_UDP(); |
138 | } | 141 | } |
139 | 142 | ||
140 | 143 | ||
@@ -144,61 +147,69 @@ int main(int argc, char *argv[]) | |||
144 | printf("usage: %s filename\n", argv[0]); | 147 | printf("usage: %s filename\n", argv[0]); |
145 | exit(0); | 148 | exit(0); |
146 | } | 149 | } |
147 | 150 | ||
148 | uint8_t buffer[512]; | 151 | uint8_t buffer[512]; |
149 | int read; | 152 | int read; |
150 | 153 | ||
151 | FILE *file = fopen(argv[1], "wb"); | 154 | FILE *file = fopen(argv[1], "wb"); |
155 | |||
152 | if (file == NULL) | 156 | if (file == NULL) |
153 | return 1; | 157 | return 1; |
154 | 158 | ||
155 | 159 | ||
156 | //initialize networking | 160 | //initialize networking |
157 | //bind to ip 0.0.0.0:PORT | 161 | //bind to ip 0.0.0.0:PORT |
158 | IP ip; | 162 | IP ip; |
159 | ip.i = 0; | 163 | ip.i = 0; |
160 | init_networking(ip, PORT); | 164 | init_networking(ip, PORT); |
161 | perror("Initialization"); | 165 | perror("Initialization"); |
162 | 166 | ||
163 | int connection; | 167 | int connection; |
164 | uint64_t timer = current_time(); | 168 | uint64_t timer = current_time(); |
165 | 169 | ||
166 | LosslessUDP_init(); | 170 | LosslessUDP_init(); |
167 | 171 | ||
168 | while (1) { | 172 | while (1) { |
169 | Lossless_UDP(); | 173 | Lossless_UDP(); |
170 | connection = incoming_connection(); | 174 | connection = incoming_connection(); |
171 | if(connection != -1) { | 175 | |
172 | if(is_connected(connection) == 2) { | 176 | if (connection != -1) { |
177 | if (is_connected(connection) == 2) { | ||
173 | printf("Received the connection.\n"); | 178 | printf("Received the connection.\n"); |
174 | 179 | ||
175 | } | 180 | } |
181 | |||
176 | break; | 182 | break; |
177 | } | 183 | } |
184 | |||
178 | c_sleep(1); | 185 | c_sleep(1); |
179 | } | 186 | } |
180 | 187 | ||
181 | timer = current_time(); | 188 | timer = current_time(); |
182 | 189 | ||
183 | while (1) { | 190 | while (1) { |
184 | //printconnection(0); | 191 | //printconnection(0); |
185 | Lossless_UDP(); | 192 | Lossless_UDP(); |
193 | |||
186 | if (is_connected(connection) >= 2) { | 194 | if (is_connected(connection) >= 2) { |
187 | kill_connection_in(connection, 3000000); | 195 | kill_connection_in(connection, 3000000); |
188 | read = read_packet(connection, buffer); | 196 | read = read_packet(connection, buffer); |
197 | |||
189 | if (read != 0) { | 198 | if (read != 0) { |
190 | // printf("Received data.\n"); | 199 | // printf("Received data.\n"); |
191 | if (!fwrite(buffer, read, 1, file)) | 200 | if (!fwrite(buffer, read, 1, file)) |
192 | printf("file write error\n"); | 201 | printf("file write error\n"); |
193 | } | 202 | } |
194 | } | 203 | } |
195 | if(is_connected(connection) == 4) { | 204 | |
205 | if (is_connected(connection) == 4) { | ||
196 | printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); | 206 | printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); |
197 | fclose(file); | 207 | fclose(file); |
198 | return 1; | 208 | return 1; |
199 | } | 209 | } |
210 | |||
200 | c_sleep(1); | 211 | c_sleep(1); |
201 | } | 212 | } |
202 | 213 | ||
203 | return 0; | 214 | return 0; |
204 | } | 215 | } |
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 2e2bedc3..f8685b39 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c | |||
@@ -55,35 +55,37 @@ | |||
55 | * networking_requesthandler and so cannot take a Messenger * */ | 55 | * networking_requesthandler and so cannot take a Messenger * */ |
56 | static Messenger *m; | 56 | static Messenger *m; |
57 | 57 | ||
58 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void* userdata) | 58 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) |
59 | { | 59 | { |
60 | printf("Friend request received from: \n"); | 60 | printf("Friend request received from: \n"); |
61 | printf("ClientID: "); | 61 | printf("ClientID: "); |
62 | uint32_t j; | 62 | uint32_t j; |
63 | for(j = 0; j < 32; j++) | 63 | |
64 | { | 64 | for (j = 0; j < 32; j++) { |
65 | if(public_key[j] < 16) | 65 | if (public_key[j] < 16) |
66 | printf("0"); | 66 | printf("0"); |
67 | |||
67 | printf("%hhX", public_key[j]); | 68 | printf("%hhX", public_key[j]); |
68 | } | 69 | } |
70 | |||
69 | printf("\nOf length: %u with data: %s \n", length, data); | 71 | printf("\nOf length: %u with data: %s \n", length, data); |
70 | 72 | ||
71 | if(length != sizeof("Install Gentoo")) | 73 | if (length != sizeof("Install Gentoo")) { |
72 | { | ||
73 | return; | 74 | return; |
74 | } | 75 | } |
75 | if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 ) | 76 | |
76 | //if the request contained the message of peace the person is obviously a friend so we add him. | 77 | if (memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 ) |
78 | //if the request contained the message of peace the person is obviously a friend so we add him. | ||
77 | { | 79 | { |
78 | printf("Friend request accepted.\n"); | 80 | printf("Friend request accepted.\n"); |
79 | m_addfriend_norequest(m, public_key); | 81 | m_addfriend_norequest(m, public_key); |
80 | } | 82 | } |
81 | } | 83 | } |
82 | 84 | ||
83 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata) | 85 | void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
84 | { | 86 | { |
85 | printf("Message with length %u received from %u: %s \n", length, friendnumber, string); | 87 | printf("Message with length %u received from %u: %s \n", length, friendnumber, string); |
86 | m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6); | 88 | m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6); |
87 | } | 89 | } |
88 | 90 | ||
89 | int main(int argc, char *argv[]) | 91 | int main(int argc, char *argv[]) |
@@ -94,19 +96,24 @@ int main(int argc, char *argv[]) | |||
94 | } | 96 | } |
95 | 97 | ||
96 | m = initMessenger(); | 98 | m = initMessenger(); |
97 | if( !m ){ | 99 | |
100 | if ( !m ) { | ||
98 | fputs("Failed to allocate messenger datastructure\n", stderr); | 101 | fputs("Failed to allocate messenger datastructure\n", stderr); |
99 | exit(0); | 102 | exit(0); |
100 | } | 103 | } |
101 | 104 | ||
102 | if(argc > 3) { | 105 | if (argc > 3) { |
103 | IP_Port bootstrap_ip_port; | 106 | IP_Port bootstrap_ip_port; |
104 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 107 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
105 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); | 108 | bootstrap_ip_port.ip.i = inet_addr(argv[1]); |
106 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 109 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
107 | } else { | 110 | } else { |
108 | FILE *file = fopen(argv[1], "rb"); | 111 | FILE *file = fopen(argv[1], "rb"); |
109 | if ( file==NULL ){return 1;} | 112 | |
113 | if ( file == NULL ) { | ||
114 | return 1; | ||
115 | } | ||
116 | |||
110 | int read; | 117 | int read; |
111 | uint8_t buffer[128000]; | 118 | uint8_t buffer[128000]; |
112 | read = fread(buffer, 1, 128000, file); | 119 | read = fread(buffer, 1, 128000, file); |
@@ -114,6 +121,7 @@ int main(int argc, char *argv[]) | |||
114 | fclose(file); | 121 | fclose(file); |
115 | 122 | ||
116 | } | 123 | } |
124 | |||
117 | m_callback_friendrequest(m, print_request, NULL); | 125 | m_callback_friendrequest(m, print_request, NULL); |
118 | m_callback_friendmessage(m, print_message, NULL); | 126 | m_callback_friendmessage(m, print_message, NULL); |
119 | 127 | ||
@@ -121,39 +129,52 @@ int main(int argc, char *argv[]) | |||
121 | uint32_t i; | 129 | uint32_t i; |
122 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 130 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
123 | getaddress(m, address); | 131 | getaddress(m, address); |
124 | for(i = 0; i < FRIEND_ADDRESS_SIZE; i++) { | 132 | |
125 | if(address[i] < 16) | 133 | for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { |
134 | if (address[i] < 16) | ||
126 | printf("0"); | 135 | printf("0"); |
127 | printf("%hhX",address[i]); | 136 | |
137 | printf("%hhX", address[i]); | ||
128 | } | 138 | } |
129 | 139 | ||
130 | setname(m, (uint8_t *)"Anon", 5); | 140 | setname(m, (uint8_t *)"Anon", 5); |
131 | 141 | ||
132 | char temp_id[128]; | 142 | char temp_id[128]; |
133 | printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); | 143 | printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); |
134 | if(scanf("%s", temp_id) != 1) { | 144 | |
145 | if (scanf("%s", temp_id) != 1) { | ||
135 | return 1; | 146 | return 1; |
136 | } | 147 | } |
137 | int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 148 | |
149 | int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | ||
138 | 150 | ||
139 | perror("Initialization"); | 151 | perror("Initialization"); |
140 | 152 | ||
141 | while(1) { | 153 | while (1) { |
142 | uint8_t name[128]; | 154 | uint8_t name[128]; |
143 | getname(m, num, name); | 155 | getname(m, num, name); |
144 | printf("%s\n", name); | 156 | printf("%s\n", name); |
145 | 157 | ||
146 | m_sendmessage(m, num, (uint8_t*)"Test", 5); | 158 | m_sendmessage(m, num, (uint8_t *)"Test", 5); |
147 | doMessenger(m); | 159 | doMessenger(m); |
148 | c_sleep(30); | 160 | c_sleep(30); |
149 | FILE *file = fopen("Save.bak", "wb"); | 161 | FILE *file = fopen("Save.bak", "wb"); |
150 | if ( file==NULL ){return 1;} | 162 | |
151 | uint8_t * buffer = malloc(Messenger_size(m)); | 163 | if ( file == NULL ) { |
164 | return 1; | ||
165 | } | ||
166 | |||
167 | uint8_t *buffer = malloc(Messenger_size(m)); | ||
152 | Messenger_save(m, buffer); | 168 | Messenger_save(m, buffer); |
153 | size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); | 169 | size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); |
154 | if (write_result < Messenger_size(m)) {return 1;} | 170 | |
171 | if (write_result < Messenger_size(m)) { | ||
172 | return 1; | ||
173 | } | ||
174 | |||
155 | free(buffer); | 175 | free(buffer); |
156 | fclose(file); | 176 | fclose(file); |
157 | } | 177 | } |
178 | |||
158 | cleanupMessenger(m); | 179 | cleanupMessenger(m); |
159 | } | 180 | } |
diff --git a/testing/crypto_speed_test.c b/testing/crypto_speed_test.c index ba40722f..a61b30e8 100644 --- a/testing/crypto_speed_test.c +++ b/testing/crypto_speed_test.c | |||
@@ -10,7 +10,7 @@ double get_time() | |||
10 | LARGE_INTEGER t, f; | 10 | LARGE_INTEGER t, f; |
11 | QueryPerformanceCounter(&t); | 11 | QueryPerformanceCounter(&t); |
12 | QueryPerformanceFrequency(&f); | 12 | QueryPerformanceFrequency(&f); |
13 | return (double)t.QuadPart/(double)f.QuadPart; | 13 | return (double)t.QuadPart / (double)f.QuadPart; |
14 | } | 14 | } |
15 | 15 | ||
16 | #else | 16 | #else |
@@ -23,7 +23,7 @@ double get_time() | |||
23 | struct timeval t; | 23 | struct timeval t; |
24 | struct timezone tzp; | 24 | struct timezone tzp; |
25 | gettimeofday(&t, &tzp); | 25 | gettimeofday(&t, &tzp); |
26 | return t.tv_sec + t.tv_usec*1e-6; | 26 | return t.tv_sec + t.tv_usec * 1e-6; |
27 | } | 27 | } |
28 | 28 | ||
29 | #endif | 29 | #endif |
@@ -35,16 +35,16 @@ double get_time() | |||
35 | void rand_bytes(uint8_t *b, size_t blen) | 35 | void rand_bytes(uint8_t *b, size_t blen) |
36 | { | 36 | { |
37 | size_t i; | 37 | size_t i; |
38 | for (i = 0; i < blen; i++) | 38 | |
39 | { | 39 | for (i = 0; i < blen; i++) { |
40 | b[i] = rand(); | 40 | b[i] = rand(); |
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | int main(int argc, char* argv[]) | 44 | int main(int argc, char *argv[]) |
45 | { | 45 | { |
46 | const int numtrials = 10000; | 46 | const int numtrials = 10000; |
47 | 47 | ||
48 | unsigned char pk1[crypto_box_PUBLICKEYBYTES]; | 48 | unsigned char pk1[crypto_box_PUBLICKEYBYTES]; |
49 | unsigned char sk1[crypto_box_SECRETKEYBYTES]; | 49 | unsigned char sk1[crypto_box_SECRETKEYBYTES]; |
50 | unsigned char pk2[crypto_box_PUBLICKEYBYTES]; | 50 | unsigned char pk2[crypto_box_PUBLICKEYBYTES]; |
@@ -52,7 +52,7 @@ int main(int argc, char* argv[]) | |||
52 | unsigned char k1[crypto_box_BEFORENMBYTES]; | 52 | unsigned char k1[crypto_box_BEFORENMBYTES]; |
53 | unsigned char k2[crypto_box_BEFORENMBYTES]; | 53 | unsigned char k2[crypto_box_BEFORENMBYTES]; |
54 | 54 | ||
55 | unsigned char n[crypto_box_NONCEBYTES]; | 55 | unsigned char n[crypto_box_NONCEBYTES]; |
56 | 56 | ||
57 | unsigned char m[500]; | 57 | unsigned char m[500]; |
58 | unsigned char c[sizeof(m) + ENCRYPTION_PADDING]; | 58 | unsigned char c[sizeof(m) + ENCRYPTION_PADDING]; |
@@ -78,46 +78,50 @@ int main(int argc, char* argv[]) | |||
78 | 78 | ||
79 | printf("starting slow...\n"); | 79 | printf("starting slow...\n"); |
80 | starttime = get_time(); | 80 | starttime = get_time(); |
81 | for (trialno = 0; trialno < numtrials; trialno++) | 81 | |
82 | { | 82 | for (trialno = 0; trialno < numtrials; trialno++) { |
83 | encrypt_data(pk1, sk2, n, m, sizeof(m), c); | 83 | encrypt_data(pk1, sk2, n, m, sizeof(m), c); |
84 | decrypt_data(pk2, sk1, n, c, sizeof(c), m); | 84 | decrypt_data(pk2, sk1, n, c, sizeof(c), m); |
85 | } | 85 | } |
86 | |||
86 | endtime = get_time(); | 87 | endtime = get_time(); |
87 | slow_time = endtime-starttime; | 88 | slow_time = endtime - starttime; |
88 | 89 | ||
89 | printf("starting fast...\n"); | 90 | printf("starting fast...\n"); |
90 | starttime = get_time(); | 91 | starttime = get_time(); |
91 | for (trialno = 0; trialno < numtrials; trialno++) | 92 | |
92 | { | 93 | for (trialno = 0; trialno < numtrials; trialno++) { |
93 | encrypt_data_fast(k1, n, m, sizeof(m), c); | 94 | encrypt_data_fast(k1, n, m, sizeof(m), c); |
94 | decrypt_data_fast(k2, n, c, sizeof(c), m); | 95 | decrypt_data_fast(k2, n, c, sizeof(c), m); |
95 | } | 96 | } |
97 | |||
96 | endtime = get_time(); | 98 | endtime = get_time(); |
97 | fast_time = endtime-starttime; | 99 | fast_time = endtime - starttime; |
98 | 100 | ||
99 | printf("starting keygen...\n"); | 101 | printf("starting keygen...\n"); |
100 | starttime = get_time(); | 102 | starttime = get_time(); |
101 | for (trialno = 0; trialno < numtrials; trialno++) | 103 | |
102 | { | 104 | for (trialno = 0; trialno < numtrials; trialno++) { |
103 | crypto_box_keypair(pk1, sk1); | 105 | crypto_box_keypair(pk1, sk1); |
104 | crypto_box_keypair(pk2, sk2); | 106 | crypto_box_keypair(pk2, sk2); |
105 | } | 107 | } |
108 | |||
106 | endtime = get_time(); | 109 | endtime = get_time(); |
107 | keygen_time = endtime-starttime; | 110 | keygen_time = endtime - starttime; |
108 | 111 | ||
109 | printf("starting precompute...\n"); | 112 | printf("starting precompute...\n"); |
110 | starttime = get_time(); | 113 | starttime = get_time(); |
111 | for (trialno = 0; trialno < numtrials; trialno++) | 114 | |
112 | { | 115 | for (trialno = 0; trialno < numtrials; trialno++) { |
113 | encrypt_precompute(pk1, sk2, k); | 116 | encrypt_precompute(pk1, sk2, k); |
114 | encrypt_precompute(pk2, sk1, k); | 117 | encrypt_precompute(pk2, sk1, k); |
115 | } | 118 | } |
119 | |||
116 | endtime = get_time(); | 120 | endtime = get_time(); |
117 | precompute_time = endtime-starttime; | 121 | precompute_time = endtime - starttime; |
118 | 122 | ||
119 | printf("\n"); | 123 | printf("\n"); |
120 | printf("trials: %i\n", 2*numtrials); | 124 | printf("trials: %i\n", 2 * numtrials); |
121 | printf("\n"); | 125 | printf("\n"); |
122 | printf("slow time: %f sec\n", slow_time); | 126 | printf("slow time: %f sec\n", slow_time); |
123 | printf("fast time: %f sec\n", fast_time); | 127 | printf("fast time: %f sec\n", fast_time); |
@@ -126,8 +130,8 @@ int main(int argc, char* argv[]) | |||
126 | printf("\n"); | 130 | printf("\n"); |
127 | printf("Speed boost: %.1f%%\n", slow_time * 100 / fast_time); | 131 | printf("Speed boost: %.1f%%\n", slow_time * 100 / fast_time); |
128 | printf("\n"); | 132 | printf("\n"); |
129 | printf("slow: %.1f per second\n", 2*numtrials/slow_time); | 133 | printf("slow: %.1f per second\n", 2 * numtrials / slow_time); |
130 | printf("fast: %.1f per second\n", 2*numtrials/fast_time); | 134 | printf("fast: %.1f per second\n", 2 * numtrials / fast_time); |
131 | 135 | ||
132 | return 0; | 136 | return 0; |
133 | } | 137 | } |
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index f3d6b0ae..7268f3cb 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* misc_tools.c | 1 | /* misc_tools.c |
2 | * | 2 | * |
3 | * Miscellaneous functions and data structures for doing random things. | 3 | * Miscellaneous functions and data structures for doing random things. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -18,9 +18,9 @@ | |||
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 | #include "misc_tools.h" | 24 | #include "misc_tools.h" |
25 | 25 | ||
26 | #include <string.h> | 26 | #include <string.h> |
@@ -32,14 +32,15 @@ | |||
32 | #endif // DEBUG | 32 | #endif // DEBUG |
33 | 33 | ||
34 | /* TODO: rewrite */ | 34 | /* TODO: rewrite */ |
35 | unsigned char * hex_string_to_bin(char hex_string[]) | 35 | unsigned char *hex_string_to_bin(char hex_string[]) |
36 | { | 36 | { |
37 | size_t len = strlen(hex_string); | 37 | size_t len = strlen(hex_string); |
38 | unsigned char *val = malloc(len); | 38 | unsigned char *val = malloc(len); |
39 | char *pos = hex_string; | 39 | char *pos = hex_string; |
40 | int i; | 40 | int i; |
41 | for(i = 0; i < len; ++i, pos+=2) | 41 | |
42 | sscanf(pos,"%2hhx",&val[i]); | 42 | for (i = 0; i < len; ++i, pos += 2) |
43 | sscanf(pos, "%2hhx", &val[i]); | ||
43 | 44 | ||
44 | return val; | 45 | return val; |
45 | } | 46 | } |
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index b8c90fa6..1379b31a 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* misc_tools.h | 1 | /* misc_tools.h |
2 | * | 2 | * |
3 | * Miscellaneous functions and data structures for doing random things. | 3 | * Miscellaneous functions and data structures for doing random things. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -18,51 +18,51 @@ | |||
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 MISC_TOOLS_H | 24 | #ifndef MISC_TOOLS_H |
25 | #define MISC_TOOLS_H | 25 | #define MISC_TOOLS_H |
26 | 26 | ||
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | #include <stdint.h> | 28 | #include <stdint.h> |
29 | 29 | ||
30 | unsigned char* hex_string_to_bin(char hex_string[]); | 30 | unsigned char *hex_string_to_bin(char hex_string[]); |
31 | 31 | ||
32 | /*********************Debugging Macros******************** | 32 | /*********************Debugging Macros******************** |
33 | * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging | 33 | * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging |
34 | *********************************************************/ | 34 | *********************************************************/ |
35 | #ifdef DEBUG | 35 | #ifdef DEBUG |
36 | #include <assert.h> | 36 | #include <assert.h> |
37 | #include <stdio.h> | 37 | #include <stdio.h> |
38 | #include <string.h> | 38 | #include <string.h> |
39 | 39 | ||
40 | #define DEBUG_PRINT(str, ...) do { \ | 40 | #define DEBUG_PRINT(str, ...) do { \ |
41 | char msg[1000]; \ | 41 | char msg[1000]; \ |
42 | sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ | 42 | sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ |
43 | fprintf(stderr, msg, __VA_ARGS__); \ | 43 | fprintf(stderr, msg, __VA_ARGS__); \ |
44 | } while (0) | 44 | } while (0) |
45 | 45 | ||
46 | #define WARNING(...) do { \ | 46 | #define WARNING(...) do { \ |
47 | fprintf(stderr, "warning in "); \ | 47 | fprintf(stderr, "warning in "); \ |
48 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 48 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
49 | } while (0) | 49 | } while (0) |
50 | 50 | ||
51 | #define INFO(...) do { \ | 51 | #define INFO(...) do { \ |
52 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 52 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
53 | } while (0) | 53 | } while (0) |
54 | 54 | ||
55 | #undef ERROR | 55 | #undef ERROR |
56 | #define ERROR(exit_status, ...) do { \ | 56 | #define ERROR(exit_status, ...) do { \ |
57 | fprintf(stderr, "error in "); \ | 57 | fprintf(stderr, "error in "); \ |
58 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 58 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
59 | exit(exit_status); \ | 59 | exit(exit_status); \ |
60 | } while (0) | 60 | } while (0) |
61 | #else | 61 | #else |
62 | #define WARNING(...) | 62 | #define WARNING(...) |
63 | #define INFO(...) | 63 | #define INFO(...) |
64 | #undef ERROR | 64 | #undef ERROR |
65 | #define ERROR(...) | 65 | #define ERROR(...) |
66 | #endif // DEBUG | 66 | #endif // DEBUG |
67 | 67 | ||
68 | /************************Linked List*********************** | 68 | /************************Linked List*********************** |
@@ -81,31 +81,31 @@ unsigned char* hex_string_to_bin(char hex_string[]); | |||
81 | #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) | 81 | #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) |
82 | 82 | ||
83 | typedef struct tox_list { | 83 | typedef struct tox_list { |
84 | struct tox_list *prev, *next; | 84 | struct tox_list *prev, *next; |
85 | } tox_list_t; | 85 | } tox_list_t; |
86 | 86 | ||
87 | /* Returns a new tox_list_t. */ | 87 | /* Returns a new tox_list_t. */ |
88 | static inline void tox_list_new(tox_list_t* lst) | 88 | static inline void tox_list_new(tox_list_t *lst) |
89 | { | 89 | { |
90 | lst->prev = lst->next = lst; | 90 | lst->prev = lst->next = lst; |
91 | } | 91 | } |
92 | 92 | ||
93 | /* Inserts a new tox_lst after lst and returns it. */ | 93 | /* Inserts a new tox_lst after lst and returns it. */ |
94 | static inline void tox_list_add(tox_list_t* lst, tox_list_t* new_lst) | 94 | static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) |
95 | { | 95 | { |
96 | tox_list_new(new_lst); | 96 | tox_list_new(new_lst); |
97 | 97 | ||
98 | new_lst->next = lst->next; | 98 | new_lst->next = lst->next; |
99 | new_lst->next->prev = new_lst; | 99 | new_lst->next->prev = new_lst; |
100 | 100 | ||
101 | lst->next = new_lst; | 101 | lst->next = new_lst; |
102 | new_lst->prev = lst; | 102 | new_lst->prev = lst; |
103 | } | 103 | } |
104 | 104 | ||
105 | static inline void tox_list_remove(tox_list_t* lst) | 105 | static inline void tox_list_remove(tox_list_t *lst) |
106 | { | 106 | { |
107 | lst->prev->next = lst->next; | 107 | lst->prev->next = lst->next; |
108 | lst->next->prev = lst->prev; | 108 | lst->next->prev = lst->prev; |
109 | } | 109 | } |
110 | 110 | ||
111 | /****************************Array*************************** | 111 | /****************************Array*************************** |
@@ -124,7 +124,7 @@ static inline void tox_array_init(struct tox_array *arr) | |||
124 | { | 124 | { |
125 | arr->size = 1; | 125 | arr->size = 1; |
126 | arr->length = 0; | 126 | arr->length = 0; |
127 | arr->data = malloc(sizeof(void*)); | 127 | arr->data = malloc(sizeof(void *)); |
128 | } | 128 | } |
129 | 129 | ||
130 | static inline void tox_array_delete(struct tox_array *arr) | 130 | static inline void tox_array_delete(struct tox_array *arr) |
@@ -139,22 +139,24 @@ static inline void tox_array_delete(struct tox_array *arr) | |||
139 | static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) | 139 | static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) |
140 | { | 140 | { |
141 | arr->size = arr->length + extra; | 141 | arr->size = arr->length + extra; |
142 | arr->data = realloc(arr->data, arr->size * sizeof(void*)); | 142 | arr->data = realloc(arr->data, arr->size * sizeof(void *)); |
143 | } | 143 | } |
144 | 144 | ||
145 | static inline void _tox_array_push(struct tox_array *arr, void *new) | 145 | static inline void _tox_array_push(struct tox_array *arr, void *new) |
146 | { | 146 | { |
147 | if (arr->length+1 >= arr->size) | 147 | if (arr->length + 1 >= arr->size) |
148 | tox_array_shrink_to_fit(arr, arr->size); | 148 | tox_array_shrink_to_fit(arr, arr->size); |
149 | |||
149 | arr->data[arr->length++] = new; | 150 | arr->data[arr->length++] = new; |
150 | } | 151 | } |
151 | #define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) | 152 | #define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) |
152 | 153 | ||
153 | static inline void* tox_array_pop(struct tox_array *arr) | 154 | static inline void *tox_array_pop(struct tox_array *arr) |
154 | { | 155 | { |
155 | if (arr->length-1 < arr->size/4) | 156 | if (arr->length - 1 < arr->size / 4) |
156 | tox_array_shrink_to_fit(arr, arr->length*2); | 157 | tox_array_shrink_to_fit(arr, arr->length * 2); |
157 | return arr->data[arr->length--]; | 158 | |
159 | return arr->data[arr->length--]; | ||
158 | } | 160 | } |
159 | 161 | ||
160 | #endif // MISC_TOOLS_H | 162 | #endif // MISC_TOOLS_H |
diff --git a/testing/nTox.c b/testing/nTox.c index ead4fd21..8813f305 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -57,17 +57,18 @@ void get_id(Messenger *m, char *data) | |||
57 | int i = 0; | 57 | int i = 0; |
58 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 58 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
59 | getaddress(m, address); | 59 | getaddress(m, address); |
60 | for(; i < FRIEND_ADDRESS_SIZE; i++) | 60 | |
61 | { | 61 | for (; i < FRIEND_ADDRESS_SIZE; i++) { |
62 | sprintf(data + 2*i + offset, "%02X ", address[i]); | 62 | sprintf(data + 2 * i + offset, "%02X ", address[i]); |
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | void new_lines(char *line) | 66 | void new_lines(char *line) |
67 | { | 67 | { |
68 | int i = 0; | 68 | int i = 0; |
69 | for (i = HISTORY-1; i > 0; i--) | 69 | |
70 | strncpy(lines[i], lines[i-1], STRING_LENGTH - 1); | 70 | for (i = HISTORY - 1; i > 0; i--) |
71 | strncpy(lines[i], lines[i - 1], STRING_LENGTH - 1); | ||
71 | 72 | ||
72 | strncpy(lines[0], line, STRING_LENGTH - 1); | 73 | strncpy(lines[0], line, STRING_LENGTH - 1); |
73 | do_refresh(); | 74 | do_refresh(); |
@@ -79,46 +80,52 @@ void print_friendlist(Messenger *m) | |||
79 | char name[MAX_NAME_LENGTH]; | 80 | char name[MAX_NAME_LENGTH]; |
80 | int i = 0; | 81 | int i = 0; |
81 | new_lines("[i] Friend List:"); | 82 | new_lines("[i] Friend List:"); |
82 | while(getname(m, i, (uint8_t *)name) != -1) { | 83 | |
84 | while (getname(m, i, (uint8_t *)name) != -1) { | ||
83 | /* account for the longest name and the longest "base" string */ | 85 | /* account for the longest name and the longest "base" string */ |
84 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; | 86 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; |
85 | 87 | ||
86 | if (strlen(name) <= 0) { | 88 | if (strlen(name) <= 0) { |
87 | sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); | 89 | sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); |
88 | } else { | 90 | } else { |
89 | sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); | 91 | sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t *)name, i); |
90 | } | 92 | } |
93 | |||
91 | i++; | 94 | i++; |
92 | new_lines(fstring); | 95 | new_lines(fstring); |
93 | } | 96 | } |
94 | 97 | ||
95 | if(i == 0) | 98 | if (i == 0) |
96 | new_lines("\tno friends! D:"); | 99 | new_lines("\tno friends! D:"); |
97 | } | 100 | } |
98 | 101 | ||
99 | char *format_message(Messenger *m, char *message, int friendnum) | 102 | char *format_message(Messenger *m, char *message, int friendnum) |
100 | { | 103 | { |
101 | char name[MAX_NAME_LENGTH]; | 104 | char name[MAX_NAME_LENGTH]; |
105 | |||
102 | if (friendnum != -1) { | 106 | if (friendnum != -1) { |
103 | getname(m, friendnum, (uint8_t*)name); | 107 | getname(m, friendnum, (uint8_t *)name); |
104 | } else { | 108 | } else { |
105 | getself_name(m, (uint8_t*)name, sizeof(name)); | 109 | getself_name(m, (uint8_t *)name, sizeof(name)); |
106 | } | 110 | } |
107 | char *msg = malloc(100+strlen(message)+strlen(name)+1); | 111 | |
112 | char *msg = malloc(100 + strlen(message) + strlen(name) + 1); | ||
108 | 113 | ||
109 | time_t rawtime; | 114 | time_t rawtime; |
110 | struct tm * timeinfo; | 115 | struct tm *timeinfo; |
111 | time ( &rawtime ); | 116 | time ( &rawtime ); |
112 | timeinfo = localtime ( &rawtime ); | 117 | timeinfo = localtime ( &rawtime ); |
113 | char* time = asctime(timeinfo); | 118 | char *time = asctime(timeinfo); |
114 | size_t len = strlen(time); | 119 | size_t len = strlen(time); |
115 | time[len-1] = '\0'; | 120 | time[len - 1] = '\0'; |
121 | |||
116 | if (friendnum != -1) { | 122 | if (friendnum != -1) { |
117 | sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); | 123 | sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); |
118 | } else { | 124 | } else { |
119 | // This message came from ourselves | 125 | // This message came from ourselves |
120 | sprintf(msg, "%s <%s> %s", time, name, message); | 126 | sprintf(msg, "%s <%s> %s", time, name, message); |
121 | } | 127 | } |
128 | |||
122 | return msg; | 129 | return msg; |
123 | } | 130 | } |
124 | 131 | ||
@@ -126,110 +133,127 @@ void line_eval(Messenger *m, char *line) | |||
126 | { | 133 | { |
127 | if (line[0] == '/') { | 134 | if (line[0] == '/') { |
128 | char inpt_command = line[1]; | 135 | char inpt_command = line[1]; |
129 | char prompt[STRING_LENGTH+2] = "> "; | 136 | char prompt[STRING_LENGTH + 2] = "> "; |
130 | int prompt_offset = 3; | 137 | int prompt_offset = 3; |
131 | strcat(prompt, line); | 138 | strcat(prompt, line); |
132 | new_lines(prompt); | 139 | new_lines(prompt); |
140 | |||
133 | if (inpt_command == 'f') { // add friend command: /f ID | 141 | if (inpt_command == 'f') { // add friend command: /f ID |
134 | int i; | 142 | int i; |
135 | char temp_id[128]; | 143 | char temp_id[128]; |
144 | |||
136 | for (i = 0; i < 128; i++) | 145 | for (i = 0; i < 128; i++) |
137 | temp_id[i] = line[i+prompt_offset]; | 146 | temp_id[i] = line[i + prompt_offset]; |
138 | 147 | ||
139 | unsigned char *bin_string = hex_string_to_bin(temp_id); | 148 | unsigned char *bin_string = hex_string_to_bin(temp_id); |
140 | int num = m_addfriend(m, bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 149 | int num = m_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); |
141 | free(bin_string); | 150 | free(bin_string); |
142 | char numstring[100]; | 151 | char numstring[100]; |
152 | |||
143 | switch (num) { | 153 | switch (num) { |
144 | case FAERR_TOOLONG: | 154 | case FAERR_TOOLONG: |
145 | sprintf(numstring, "[i] Message is too long."); | 155 | sprintf(numstring, "[i] Message is too long."); |
146 | break; | 156 | break; |
147 | case FAERR_NOMESSAGE: | 157 | |
148 | sprintf(numstring, "[i] Please add a message to your request."); | 158 | case FAERR_NOMESSAGE: |
149 | break; | 159 | sprintf(numstring, "[i] Please add a message to your request."); |
150 | case FAERR_OWNKEY: | 160 | break; |
151 | sprintf(numstring, "[i] That appears to be your own ID."); | 161 | |
152 | break; | 162 | case FAERR_OWNKEY: |
153 | case FAERR_ALREADYSENT: | 163 | sprintf(numstring, "[i] That appears to be your own ID."); |
154 | sprintf(numstring, "[i] Friend request already sent."); | 164 | break; |
155 | break; | 165 | |
156 | case FAERR_UNKNOWN: | 166 | case FAERR_ALREADYSENT: |
157 | sprintf(numstring, "[i] Undefined error when adding friend."); | 167 | sprintf(numstring, "[i] Friend request already sent."); |
158 | break; | 168 | break; |
159 | default: | 169 | |
160 | sprintf(numstring, "[i] Added friend as %d.", num); | 170 | case FAERR_UNKNOWN: |
161 | break; | 171 | sprintf(numstring, "[i] Undefined error when adding friend."); |
172 | break; | ||
173 | |||
174 | default: | ||
175 | sprintf(numstring, "[i] Added friend as %d.", num); | ||
176 | break; | ||
162 | } | 177 | } |
178 | |||
163 | new_lines(numstring); | 179 | new_lines(numstring); |
164 | do_refresh(); | 180 | do_refresh(); |
165 | } | 181 | } else if (inpt_command == 'd') { |
166 | else if (inpt_command == 'd') { | ||
167 | doMessenger(m); | 182 | doMessenger(m); |
168 | } | 183 | } else if (inpt_command == 'm') { //message command: /m friendnumber messsage |
169 | else if (inpt_command == 'm') { //message command: /m friendnumber messsage | ||
170 | size_t len = strlen(line); | 184 | size_t len = strlen(line); |
171 | if(len < 3) | 185 | |
186 | if (len < 3) | ||
172 | return; | 187 | return; |
173 | 188 | ||
174 | char numstring[len-3]; | 189 | char numstring[len - 3]; |
175 | char message[len-3]; | 190 | char message[len - 3]; |
176 | int i; | 191 | int i; |
192 | |||
177 | for (i = 0; i < len; i++) { | 193 | for (i = 0; i < len; i++) { |
178 | if (line[i+3] != ' ') { | 194 | if (line[i + 3] != ' ') { |
179 | numstring[i] = line[i+3]; | 195 | numstring[i] = line[i + 3]; |
180 | } else { | 196 | } else { |
181 | int j; | 197 | int j; |
182 | for (j = (i+1); j < (len+1); j++) | 198 | |
183 | message[j-i-1] = line[j+3]; | 199 | for (j = (i + 1); j < (len + 1); j++) |
200 | message[j - i - 1] = line[j + 3]; | ||
201 | |||
184 | break; | 202 | break; |
185 | } | 203 | } |
186 | } | 204 | } |
205 | |||
187 | int num = atoi(numstring); | 206 | int num = atoi(numstring); |
188 | if (m_sendmessage(m, num, (uint8_t*) message, strlen(message) + 1) != 1) { | 207 | |
208 | if (m_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { | ||
189 | new_lines("[i] could not send message"); | 209 | new_lines("[i] could not send message"); |
190 | } else { | 210 | } else { |
191 | new_lines(format_message(m, message, -1)); | 211 | new_lines(format_message(m, message, -1)); |
192 | } | 212 | } |
193 | } | 213 | } else if (inpt_command == 'n') { |
194 | else if (inpt_command == 'n') { | ||
195 | uint8_t name[MAX_NAME_LENGTH]; | 214 | uint8_t name[MAX_NAME_LENGTH]; |
196 | int i = 0; | 215 | int i = 0; |
197 | size_t len = strlen(line); | 216 | size_t len = strlen(line); |
217 | |||
198 | for (i = 3; i < len; i++) { | 218 | for (i = 3; i < len; i++) { |
199 | if (line[i] == 0 || line[i] == '\n') break; | 219 | if (line[i] == 0 || line[i] == '\n') break; |
200 | name[i-3] = line[i]; | 220 | |
221 | name[i - 3] = line[i]; | ||
201 | } | 222 | } |
202 | name[i-3] = 0; | 223 | |
224 | name[i - 3] = 0; | ||
203 | setname(m, name, i - 2); | 225 | setname(m, name, i - 2); |
204 | char numstring[100]; | 226 | char numstring[100]; |
205 | sprintf(numstring, "[i] changed nick to %s", (char*)name); | 227 | sprintf(numstring, "[i] changed nick to %s", (char *)name); |
206 | new_lines(numstring); | 228 | new_lines(numstring); |
207 | } | 229 | } else if (inpt_command == 'l') { |
208 | else if (inpt_command == 'l') { | ||
209 | print_friendlist(m); | 230 | print_friendlist(m); |
210 | } | 231 | } else if (inpt_command == 's') { |
211 | else if (inpt_command == 's') { | ||
212 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 232 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; |
213 | int i = 0; | 233 | int i = 0; |
214 | size_t len = strlen(line); | 234 | size_t len = strlen(line); |
235 | |||
215 | for (i = 3; i < len; i++) { | 236 | for (i = 3; i < len; i++) { |
216 | if (line[i] == 0 || line[i] == '\n') break; | 237 | if (line[i] == 0 || line[i] == '\n') break; |
217 | status[i-3] = line[i]; | 238 | |
239 | status[i - 3] = line[i]; | ||
218 | } | 240 | } |
219 | status[i-3] = 0; | 241 | |
220 | m_set_statusmessage(m, status, strlen((char*)status) + 1); | 242 | status[i - 3] = 0; |
243 | m_set_statusmessage(m, status, strlen((char *)status) + 1); | ||
221 | char numstring[100]; | 244 | char numstring[100]; |
222 | sprintf(numstring, "[i] changed status to %s", (char*)status); | 245 | sprintf(numstring, "[i] changed status to %s", (char *)status); |
223 | new_lines(numstring); | 246 | new_lines(numstring); |
224 | } | 247 | } else if (inpt_command == 'a') { |
225 | else if (inpt_command == 'a') { | ||
226 | uint8_t numf = atoi(line + 3); | 248 | uint8_t numf = atoi(line + 3); |
227 | char numchar[100]; | 249 | char numchar[100]; |
250 | |||
228 | if (numf >= num_requests || pending_requests[numf].accepted) { | 251 | if (numf >= num_requests || pending_requests[numf].accepted) { |
229 | sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); | 252 | sprintf(numchar, "[i] you either didn't receive that request or you already accepted it"); |
230 | new_lines(numchar); | 253 | new_lines(numchar); |
231 | } else { | 254 | } else { |
232 | int num = m_addfriend_norequest(m, pending_requests[numf].id); | 255 | int num = m_addfriend_norequest(m, pending_requests[numf].id); |
256 | |||
233 | if (num != -1) { | 257 | if (num != -1) { |
234 | pending_requests[numf].accepted = 1; | 258 | pending_requests[numf].accepted = 1; |
235 | sprintf(numchar, "[i] friend request %u accepted", numf); | 259 | sprintf(numchar, "[i] friend request %u accepted", numf); |
@@ -241,16 +265,15 @@ void line_eval(Messenger *m, char *line) | |||
241 | new_lines(numchar); | 265 | new_lines(numchar); |
242 | } | 266 | } |
243 | } | 267 | } |
268 | |||
244 | do_refresh(); | 269 | do_refresh(); |
270 | } else if (inpt_command == 'h') { //help | ||
271 | new_lines(help); | ||
272 | } else if (inpt_command == 'i') { //info | ||
273 | char idstring[200]; | ||
274 | get_id(m, idstring); | ||
275 | new_lines(idstring); | ||
245 | } | 276 | } |
246 | else if (inpt_command == 'h') { //help | ||
247 | new_lines(help); | ||
248 | } | ||
249 | else if (inpt_command == 'i') { //info | ||
250 | char idstring[200]; | ||
251 | get_id(m, idstring); | ||
252 | new_lines(idstring); | ||
253 | } | ||
254 | 277 | ||
255 | else if (inpt_command == 'q') { //exit | 278 | else if (inpt_command == 'q') { //exit |
256 | endwin(); | 279 | endwin(); |
@@ -266,13 +289,15 @@ void line_eval(Messenger *m, char *line) | |||
266 | 289 | ||
267 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | 290 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) |
268 | { | 291 | { |
269 | strcpy(output,input); | 292 | strcpy(output, input); |
270 | size_t len = strlen(output); | 293 | size_t len = strlen(output); |
271 | int i = 0; | 294 | int i = 0; |
295 | |||
272 | for (i = line_width; i < len; i = i + line_width) { | 296 | for (i = line_width; i < len; i = i + line_width) { |
273 | while (output[i] != ' ' && i != 0) { | 297 | while (output[i] != ' ' && i != 0) { |
274 | i--; | 298 | i--; |
275 | } | 299 | } |
300 | |||
276 | if (i > 0) { | 301 | if (i > 0) { |
277 | output[i] = '\n'; | 302 | output[i] = '\n'; |
278 | } | 303 | } |
@@ -284,40 +309,47 @@ int count_lines(char *string) | |||
284 | size_t len = strlen(string); | 309 | size_t len = strlen(string); |
285 | int count = 1; | 310 | int count = 1; |
286 | int i; | 311 | int i; |
312 | |||
287 | for (i = 0; i < len; i++) { | 313 | for (i = 0; i < len; i++) { |
288 | if (string[i] == '\n') | 314 | if (string[i] == '\n') |
289 | count++; | 315 | count++; |
290 | } | 316 | } |
317 | |||
291 | return count; | 318 | return count; |
292 | } | 319 | } |
293 | 320 | ||
294 | char *appender(char *str, const char c) | 321 | char *appender(char *str, const char c) |
295 | { | 322 | { |
296 | size_t len = strlen(str); | 323 | size_t len = strlen(str); |
324 | |||
297 | if (len < STRING_LENGTH) { | 325 | if (len < STRING_LENGTH) { |
298 | str[len+1] = str[len]; | 326 | str[len + 1] = str[len]; |
299 | str[len] = c; | 327 | str[len] = c; |
300 | } | 328 | } |
329 | |||
301 | return str; | 330 | return str; |
302 | } | 331 | } |
303 | 332 | ||
304 | void do_refresh() | 333 | void do_refresh() |
305 | { | 334 | { |
306 | int count=0; | 335 | int count = 0; |
307 | char wrap_output[STRING_LENGTH]; | 336 | char wrap_output[STRING_LENGTH]; |
308 | int L; | 337 | int L; |
309 | int i; | 338 | int i; |
339 | |||
310 | for (i = 0; i < HISTORY; i++) { | 340 | for (i = 0; i < HISTORY; i++) { |
311 | wrap(wrap_output, lines[i], x); | 341 | wrap(wrap_output, lines[i], x); |
312 | L = count_lines(wrap_output); | 342 | L = count_lines(wrap_output); |
313 | count = count + L; | 343 | count = count + L; |
344 | |||
314 | if (count < y) { | 345 | if (count < y) { |
315 | move(y-1-count, 0); | 346 | move(y - 1 - count, 0); |
316 | printw(wrap_output); | 347 | printw(wrap_output); |
317 | clrtoeol(); | 348 | clrtoeol(); |
318 | } | 349 | } |
319 | } | 350 | } |
320 | move(y-1, 0); | 351 | |
352 | move(y - 1, 0); | ||
321 | clrtoeol(); | 353 | clrtoeol(); |
322 | printw(">> "); | 354 | printw(">> "); |
323 | printw(line); | 355 | printw(line); |
@@ -325,7 +357,7 @@ void do_refresh() | |||
325 | refresh(); | 357 | refresh(); |
326 | } | 358 | } |
327 | 359 | ||
328 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata) | 360 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) |
329 | { | 361 | { |
330 | new_lines("[i] received friend request with message:"); | 362 | new_lines("[i] received friend request with message:"); |
331 | new_lines((char *)data); | 363 | new_lines((char *)data); |
@@ -338,26 +370,28 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* us | |||
338 | do_refresh(); | 370 | do_refresh(); |
339 | } | 371 | } |
340 | 372 | ||
341 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata) | 373 | void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
342 | { | 374 | { |
343 | new_lines(format_message(m, (char*)string, friendnumber)); | 375 | new_lines(format_message(m, (char *)string, friendnumber)); |
344 | } | 376 | } |
345 | 377 | ||
346 | void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 378 | void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
347 | { | 379 | { |
348 | char name[MAX_NAME_LENGTH]; | 380 | char name[MAX_NAME_LENGTH]; |
349 | if(getname(m, friendnumber, (uint8_t*)name) != -1) { | 381 | |
350 | char msg[100+length]; | 382 | if (getname(m, friendnumber, (uint8_t *)name) != -1) { |
383 | char msg[100 + length]; | ||
351 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); | 384 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); |
352 | new_lines(msg); | 385 | new_lines(msg); |
353 | } | 386 | } |
354 | } | 387 | } |
355 | 388 | ||
356 | void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 389 | void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
357 | { | 390 | { |
358 | char name[MAX_NAME_LENGTH]; | 391 | char name[MAX_NAME_LENGTH]; |
359 | if(getname(m, friendnumber, (uint8_t*)name) != -1) { | 392 | |
360 | char msg[100+length+strlen(name)+1]; | 393 | if (getname(m, friendnumber, (uint8_t *)name) != -1) { |
394 | char msg[100 + length + strlen(name) + 1]; | ||
361 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); | 395 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); |
362 | new_lines(msg); | 396 | new_lines(msg); |
363 | } | 397 | } |
@@ -375,10 +409,12 @@ void load_key(Messenger *m, char *path) | |||
375 | rewind(data_file); | 409 | rewind(data_file); |
376 | 410 | ||
377 | uint8_t data[size]; | 411 | uint8_t data[size]; |
378 | if (fread(data, sizeof(uint8_t), size, data_file) != size){ | 412 | |
413 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { | ||
379 | fputs("[!] could not read data file! exiting...\n", stderr); | 414 | fputs("[!] could not read data file! exiting...\n", stderr); |
380 | goto FILE_ERROR; | 415 | goto FILE_ERROR; |
381 | } | 416 | } |
417 | |||
382 | Messenger_load(m, data, size); | 418 | Messenger_load(m, data, size); |
383 | 419 | ||
384 | } else { | 420 | } else { |
@@ -388,24 +424,27 @@ void load_key(Messenger *m, char *path) | |||
388 | Messenger_save(m, data); | 424 | Messenger_save(m, data); |
389 | data_file = fopen(path, "w"); | 425 | data_file = fopen(path, "w"); |
390 | 426 | ||
391 | if(!data_file) { | 427 | if (!data_file) { |
392 | perror("[!] load_key"); | 428 | perror("[!] load_key"); |
393 | exit(1); | 429 | exit(1); |
394 | } | 430 | } |
395 | 431 | ||
396 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ | 432 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size) { |
397 | fputs("[!] could not write data file! exiting...", stderr); | 433 | fputs("[!] could not write data file! exiting...", stderr); |
398 | goto FILE_ERROR; | 434 | goto FILE_ERROR; |
399 | } | 435 | } |
400 | } | 436 | } |
401 | 437 | ||
402 | if(fclose(data_file) < 0) | 438 | if (fclose(data_file) < 0) |
403 | perror("[!] fclose failed"); | 439 | perror("[!] fclose failed"); |
440 | |||
404 | return; | 441 | return; |
405 | 442 | ||
406 | FILE_ERROR: | 443 | FILE_ERROR: |
407 | if(fclose(data_file) < 0) | 444 | |
445 | if (fclose(data_file) < 0) | ||
408 | perror("[!] fclose failed"); | 446 | perror("[!] fclose failed"); |
447 | |||
409 | exit(1); | 448 | exit(1); |
410 | } | 449 | } |
411 | 450 | ||
@@ -431,15 +470,15 @@ int main(int argc, char *argv[]) | |||
431 | exit(0); | 470 | exit(0); |
432 | } | 471 | } |
433 | 472 | ||
434 | for(i = 0; i < argc; i++) { | 473 | for (i = 0; i < argc; i++) { |
435 | if (argv[i] == NULL){ | 474 | if (argv[i] == NULL) { |
436 | break; | 475 | break; |
437 | } else if(argv[i][0] == '-') { | 476 | } else if (argv[i][0] == '-') { |
438 | if(argv[i][1] == 'h') { | 477 | if (argv[i][1] == 'h') { |
439 | print_help(); | 478 | print_help(); |
440 | exit(0); | 479 | exit(0); |
441 | } else if(argv[i][1] == 'f') { | 480 | } else if (argv[i][1] == 'f') { |
442 | if(argv[i + 1] != NULL) | 481 | if (argv[i + 1] != NULL) |
443 | filename = argv[i + 1]; | 482 | filename = argv[i + 1]; |
444 | else { | 483 | else { |
445 | fputs("[!] you passed '-f' without giving an argument!\n", stderr); | 484 | fputs("[!] you passed '-f' without giving an argument!\n", stderr); |
@@ -449,7 +488,8 @@ int main(int argc, char *argv[]) | |||
449 | } | 488 | } |
450 | 489 | ||
451 | m = initMessenger(); | 490 | m = initMessenger(); |
452 | if( !m ){ | 491 | |
492 | if ( !m ) { | ||
453 | fputs("Failed to allocate Messenger datastructure", stderr); | 493 | fputs("Failed to allocate Messenger datastructure", stderr); |
454 | exit(0); | 494 | exit(0); |
455 | } | 495 | } |
@@ -474,6 +514,7 @@ int main(int argc, char *argv[]) | |||
474 | IP_Port bootstrap_ip_port; | 514 | IP_Port bootstrap_ip_port; |
475 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 515 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
476 | int resolved_address = resolve_addr(argv[1]); | 516 | int resolved_address = resolve_addr(argv[1]); |
517 | |||
477 | if (resolved_address != 0) | 518 | if (resolved_address != 0) |
478 | bootstrap_ip_port.ip.i = resolved_address; | 519 | bootstrap_ip_port.ip.i = resolved_address; |
479 | else | 520 | else |
@@ -483,7 +524,8 @@ int main(int argc, char *argv[]) | |||
483 | DHT_bootstrap(bootstrap_ip_port, binary_string); | 524 | DHT_bootstrap(bootstrap_ip_port, binary_string); |
484 | free(binary_string); | 525 | free(binary_string); |
485 | nodelay(stdscr, TRUE); | 526 | nodelay(stdscr, TRUE); |
486 | while(true) { | 527 | |
528 | while (true) { | ||
487 | if (on == 0 && DHT_isconnected()) { | 529 | if (on == 0 && DHT_isconnected()) { |
488 | new_lines("[i] connected to DHT\n[i] define username with /n"); | 530 | new_lines("[i] connected to DHT\n[i] define username with /n"); |
489 | on = 1; | 531 | on = 1; |
@@ -494,19 +536,22 @@ int main(int argc, char *argv[]) | |||
494 | do_refresh(); | 536 | do_refresh(); |
495 | 537 | ||
496 | c = getch(); | 538 | c = getch(); |
539 | |||
497 | if (c == ERR || c == 27) | 540 | if (c == ERR || c == 27) |
498 | continue; | 541 | continue; |
499 | 542 | ||
500 | getmaxyx(stdscr, y, x); | 543 | getmaxyx(stdscr, y, x); |
544 | |||
501 | if (c == '\n') { | 545 | if (c == '\n') { |
502 | line_eval(m, line); | 546 | line_eval(m, line); |
503 | strcpy(line, ""); | 547 | strcpy(line, ""); |
504 | } else if (c == 8 || c == 127) { | 548 | } else if (c == 8 || c == 127) { |
505 | line[strlen(line)-1] = '\0'; | 549 | line[strlen(line) - 1] = '\0'; |
506 | } else if (isalnum(c) || ispunct(c) || c == ' ') { | 550 | } else if (isalnum(c) || ispunct(c) || c == ' ') { |
507 | strcpy(line, appender(line, (char) c)); | 551 | strcpy(line, appender(line, (char) c)); |
508 | } | 552 | } |
509 | } | 553 | } |
554 | |||
510 | cleanupMessenger(m); | 555 | cleanupMessenger(m); |
511 | endwin(); | 556 | endwin(); |
512 | return 0; | 557 | return 0; |
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index 7739c0a6..c26d9d16 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c | |||
@@ -54,7 +54,7 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us | |||
54 | { | 54 | { |
55 | friend_request_received = 1; | 55 | friend_request_received = 1; |
56 | printf("\n\n[i] received friend request with message\n"); | 56 | printf("\n\n[i] received friend request with message\n"); |
57 | printf("'%s'",(char *)data); | 57 | printf("'%s'", (char *)data); |
58 | char numchar[100]; | 58 | char numchar[100]; |
59 | sprintf(numchar, "\n[i] accept request with /a %u\n\n", num_requests); | 59 | sprintf(numchar, "\n[i] accept request with /a %u\n\n", num_requests); |
60 | printf(numchar); | 60 | printf(numchar); |
@@ -63,18 +63,18 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us | |||
63 | ++num_requests; | 63 | ++num_requests; |
64 | } | 64 | } |
65 | 65 | ||
66 | void print_message(Messenger *messenger, int friendnumber, uint8_t * string, uint16_t length, void *userdata) | 66 | void print_message(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
67 | { | 67 | { |
68 | char name[MAX_NAME_LENGTH]; | 68 | char name[MAX_NAME_LENGTH]; |
69 | getname(messenger, friendnumber, (uint8_t*)name); | 69 | getname(messenger, friendnumber, (uint8_t *)name); |
70 | char msg[100+length+strlen(name)+1]; | 70 | char msg[100 + length + strlen(name) + 1]; |
71 | time_t rawtime; | 71 | time_t rawtime; |
72 | struct tm * timeinfo; | 72 | struct tm *timeinfo; |
73 | time (&rawtime); | 73 | time (&rawtime); |
74 | timeinfo = localtime (&rawtime); | 74 | timeinfo = localtime (&rawtime); |
75 | char* temp = asctime(timeinfo); | 75 | char *temp = asctime(timeinfo); |
76 | size_t len = strlen(temp); | 76 | size_t len = strlen(temp); |
77 | temp[len-1]='\0'; | 77 | temp[len - 1] = '\0'; |
78 | sprintf(msg, "\n[%d] %s <%s> %s\n\n", friendnumber, temp, name, string); // timestamp | 78 | sprintf(msg, "\n[%d] %s <%s> %s\n\n", friendnumber, temp, name, string); // timestamp |
79 | printf(msg); | 79 | printf(msg); |
80 | } | 80 | } |
@@ -82,8 +82,8 @@ void print_message(Messenger *messenger, int friendnumber, uint8_t * string, uin | |||
82 | void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 82 | void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
83 | { | 83 | { |
84 | char name[MAX_NAME_LENGTH]; | 84 | char name[MAX_NAME_LENGTH]; |
85 | getname(messenger, friendnumber, (uint8_t*)name); | 85 | getname(messenger, friendnumber, (uint8_t *)name); |
86 | char msg[100+length]; | 86 | char msg[100 + length]; |
87 | sprintf(msg, "\n\n[i] [%d] %s is now known as %s.\n\n", friendnumber, name, string); | 87 | sprintf(msg, "\n\n[i] [%d] %s is now known as %s.\n\n", friendnumber, name, string); |
88 | printf(msg); | 88 | printf(msg); |
89 | } | 89 | } |
@@ -91,8 +91,8 @@ void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, u | |||
91 | void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 91 | void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
92 | { | 92 | { |
93 | char name[MAX_NAME_LENGTH]; | 93 | char name[MAX_NAME_LENGTH]; |
94 | getname(messenger, friendnumber, (uint8_t*)name); | 94 | getname(messenger, friendnumber, (uint8_t *)name); |
95 | char msg[100+length+strlen(name)+1]; | 95 | char msg[100 + length + strlen(name) + 1]; |
96 | sprintf(msg, "\n\n[i] [%d] %s's status changed to %s.\n\n", friendnumber, name, string); | 96 | sprintf(msg, "\n\n[i] [%d] %s's status changed to %s.\n\n", friendnumber, name, string); |
97 | printf(msg); | 97 | printf(msg); |
98 | } | 98 | } |
@@ -100,12 +100,14 @@ void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, | |||
100 | void load_key() | 100 | void load_key() |
101 | { | 101 | { |
102 | FILE *data_file = NULL; | 102 | FILE *data_file = NULL; |
103 | data_file = fopen("data","r"); | 103 | data_file = fopen("data", "r"); |
104 | |||
104 | if (data_file) { | 105 | if (data_file) { |
105 | fseek(data_file, 0, SEEK_END); | 106 | fseek(data_file, 0, SEEK_END); |
106 | int size = ftell(data_file); | 107 | int size = ftell(data_file); |
107 | fseek(data_file, 0, SEEK_SET); | 108 | fseek(data_file, 0, SEEK_SET); |
108 | uint8_t data[size]; | 109 | uint8_t data[size]; |
110 | |||
109 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { | 111 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { |
110 | printf("\n[i] Could not read the data file. Exiting."); | 112 | printf("\n[i] Could not read the data file. Exiting."); |
111 | exit(1); | 113 | exit(1); |
@@ -124,6 +126,7 @@ void load_key() | |||
124 | exit(1); | 126 | exit(1); |
125 | } | 127 | } |
126 | } | 128 | } |
129 | |||
127 | fclose(data_file); | 130 | fclose(data_file); |
128 | } | 131 | } |
129 | 132 | ||
@@ -131,30 +134,29 @@ void add_friend() | |||
131 | { | 134 | { |
132 | int i; | 135 | int i; |
133 | char temp_id[128]; | 136 | char temp_id[128]; |
134 | 137 | ||
135 | for (i = 0; i < 128; i++) | 138 | for (i = 0; i < 128; i++) |
136 | temp_id[i] = line[i+3]; | 139 | temp_id[i] = line[i + 3]; |
137 | 140 | ||
138 | int num = m_addfriend(messenger, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 141 | int num = m_addfriend(messenger, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); |
139 | 142 | ||
140 | if (num >= 0) { | 143 | if (num >= 0) { |
141 | char numstring[100]; | 144 | char numstring[100]; |
142 | sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); | 145 | sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); |
143 | printf(numstring); | 146 | printf(numstring); |
144 | ++maxnumfriends; | 147 | ++maxnumfriends; |
145 | } | 148 | } else if (num == -1) |
146 | else if (num == -1) | ||
147 | printf("\n[i] Message is too long.\n\n"); | 149 | printf("\n[i] Message is too long.\n\n"); |
148 | 150 | ||
149 | else if (num == -2) | 151 | else if (num == -2) |
150 | printf("\n[i] Please add a message to your friend request.\n\n"); | 152 | printf("\n[i] Please add a message to your friend request.\n\n"); |
151 | 153 | ||
152 | else if (num == -3) | 154 | else if (num == -3) |
153 | printf("\n[i] That appears to be your own ID.\n\n"); | 155 | printf("\n[i] That appears to be your own ID.\n\n"); |
154 | 156 | ||
155 | else if (num == -4) | 157 | else if (num == -4) |
156 | printf("\n[i] Friend request already sent.\n\n"); | 158 | printf("\n[i] Friend request already sent.\n\n"); |
157 | 159 | ||
158 | else if (num == -5) | 160 | else if (num == -5) |
159 | printf("\n[i] Undefined error when adding friend\n\n"); | 161 | printf("\n[i] Undefined error when adding friend\n\n"); |
160 | } | 162 | } |
@@ -162,28 +164,29 @@ void add_friend() | |||
162 | void list_friends() | 164 | void list_friends() |
163 | { | 165 | { |
164 | int i; | 166 | int i; |
165 | 167 | ||
166 | printf("\n[i] Friend List"); | 168 | printf("\n[i] Friend List"); |
167 | 169 | ||
168 | printf("----- PENDING -----\n\n"); | 170 | printf("----- PENDING -----\n\n"); |
169 | 171 | ||
170 | for (i = 0; i <= maxnumfriends; i++) { | 172 | for (i = 0; i <= maxnumfriends; i++) { |
171 | char name[MAX_NAME_LENGTH]; | 173 | char name[MAX_NAME_LENGTH]; |
172 | getname(messenger, i, (uint8_t*)name); | 174 | getname(messenger, i, (uint8_t *)name); |
175 | |||
173 | if (m_friendstatus(messenger, i) > 0 && m_friendstatus(messenger, i) < 4) | 176 | if (m_friendstatus(messenger, i) > 0 && m_friendstatus(messenger, i) < 4) |
174 | printf("[%d] %s\n", i, (uint8_t*)name); | 177 | printf("[%d] %s\n", i, (uint8_t *)name); |
175 | } | 178 | } |
176 | 179 | ||
177 | printf("\n"); | 180 | printf("\n"); |
178 | 181 | ||
179 | printf("----- ACTIVE -----\n\n"); | 182 | printf("----- ACTIVE -----\n\n"); |
180 | 183 | ||
181 | for (i = 0; i <= maxnumfriends; i++) { | 184 | for (i = 0; i <= maxnumfriends; i++) { |
182 | char name[MAX_NAME_LENGTH]; | 185 | char name[MAX_NAME_LENGTH]; |
183 | getname(messenger, i, (uint8_t*)name); | 186 | getname(messenger, i, (uint8_t *)name); |
184 | 187 | ||
185 | if (m_friendstatus(messenger, i) == 4) | 188 | if (m_friendstatus(messenger, i) == 4) |
186 | printf("[%d] %s\n", i, (uint8_t*)name); | 189 | printf("[%d] %s\n", i, (uint8_t *)name); |
187 | } | 190 | } |
188 | 191 | ||
189 | printf("\n"); | 192 | printf("\n"); |
@@ -192,14 +195,14 @@ void list_friends() | |||
192 | void delete_friend() | 195 | void delete_friend() |
193 | { | 196 | { |
194 | size_t len = strlen(line); | 197 | size_t len = strlen(line); |
195 | char numstring[len-3]; | 198 | char numstring[len - 3]; |
196 | int i; | 199 | int i; |
197 | 200 | ||
198 | for (i = 0; i < len; i++) { | 201 | for (i = 0; i < len; i++) { |
199 | if (line[i+3] != ' ') | 202 | if (line[i + 3] != ' ') |
200 | numstring[i] = line[i+3]; | 203 | numstring[i] = line[i + 3]; |
201 | } | 204 | } |
202 | 205 | ||
203 | int num = atoi(numstring); | 206 | int num = atoi(numstring); |
204 | m_delfriend(messenger, num); | 207 | m_delfriend(messenger, num); |
205 | --maxnumfriends; | 208 | --maxnumfriends; |
@@ -209,20 +212,20 @@ void delete_friend() | |||
209 | void message_friend() | 212 | void message_friend() |
210 | { | 213 | { |
211 | size_t len = strlen(line); | 214 | size_t len = strlen(line); |
212 | char numstring[len-3]; | 215 | char numstring[len - 3]; |
213 | char message[len-3]; | 216 | char message[len - 3]; |
214 | int i; | 217 | int i; |
215 | 218 | ||
216 | for (i = 0; i < len; i++) { | 219 | for (i = 0; i < len; i++) { |
217 | 220 | ||
218 | if (line[i+3] != ' ') | 221 | if (line[i + 3] != ' ') |
219 | numstring[i] = line[i+3]; | 222 | numstring[i] = line[i + 3]; |
220 | 223 | ||
221 | else { | 224 | else { |
222 | int j; | 225 | int j; |
223 | 226 | ||
224 | for (j = (i+1); j < len; j++) | 227 | for (j = (i + 1); j < len; j++) |
225 | message[j-i-1] = line[j+3]; | 228 | message[j - i - 1] = line[j + 3]; |
226 | 229 | ||
227 | break; | 230 | break; |
228 | } | 231 | } |
@@ -230,9 +233,9 @@ void message_friend() | |||
230 | 233 | ||
231 | int num = atoi(numstring); | 234 | int num = atoi(numstring); |
232 | 235 | ||
233 | if(m_sendmessage(messenger, num, (uint8_t*) message, sizeof(message)) != 1) | 236 | if (m_sendmessage(messenger, num, (uint8_t *) message, sizeof(message)) != 1) |
234 | printf("\n[i] could not send message (they may be offline): %s\n", message); | 237 | printf("\n[i] could not send message (they may be offline): %s\n", message); |
235 | 238 | ||
236 | else | 239 | else |
237 | printf("\n"); | 240 | printf("\n"); |
238 | } | 241 | } |
@@ -242,24 +245,24 @@ void change_nickname() | |||
242 | uint8_t name[MAX_NAME_LENGTH]; | 245 | uint8_t name[MAX_NAME_LENGTH]; |
243 | int i = 0; | 246 | int i = 0; |
244 | size_t len = strlen(line); | 247 | size_t len = strlen(line); |
245 | 248 | ||
246 | for (i = 3; i < len; i++) { | 249 | for (i = 3; i < len; i++) { |
247 | 250 | ||
248 | if (line[i] == 0 || line[i] == '\n') | 251 | if (line[i] == 0 || line[i] == '\n') |
249 | break; | 252 | break; |
250 | 253 | ||
251 | name[i-3] = line[i]; | 254 | name[i - 3] = line[i]; |
252 | } | 255 | } |
253 | 256 | ||
254 | name[i-3] = 0; | 257 | name[i - 3] = 0; |
255 | setname(messenger, name, i); | 258 | setname(messenger, name, i); |
256 | char numstring[100]; | 259 | char numstring[100]; |
257 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); | 260 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char *)name); |
258 | printf(numstring); | 261 | printf(numstring); |
259 | 262 | ||
260 | FILE *name_file = NULL; | 263 | FILE *name_file = NULL; |
261 | name_file = fopen("namefile.txt", "w"); | 264 | name_file = fopen("namefile.txt", "w"); |
262 | fprintf(name_file, "%s", (char*)name); | 265 | fprintf(name_file, "%s", (char *)name); |
263 | fclose(name_file); | 266 | fclose(name_file); |
264 | } | 267 | } |
265 | 268 | ||
@@ -268,24 +271,24 @@ void change_status(int savetofile) | |||
268 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 271 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; |
269 | int i = 0; | 272 | int i = 0; |
270 | size_t len = strlen(line); | 273 | size_t len = strlen(line); |
271 | 274 | ||
272 | for (i = 3; i < len; i++) { | 275 | for (i = 3; i < len; i++) { |
273 | if (line[i] == 0 || line[i] == '\n') | 276 | if (line[i] == 0 || line[i] == '\n') |
274 | break; | 277 | break; |
275 | 278 | ||
276 | status[i-3] = line[i]; | 279 | status[i - 3] = line[i]; |
277 | } | 280 | } |
278 | 281 | ||
279 | status[i-3] = 0; | 282 | status[i - 3] = 0; |
280 | m_set_statusmessage(messenger, status, strlen((char*)status)); | 283 | m_set_statusmessage(messenger, status, strlen((char *)status)); |
281 | char numstring[100]; | 284 | char numstring[100]; |
282 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); | 285 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char *)status); |
283 | printf(numstring); | 286 | printf(numstring); |
284 | 287 | ||
285 | if (savetofile == 1) { | 288 | if (savetofile == 1) { |
286 | FILE* status_file = NULL; | 289 | FILE *status_file = NULL; |
287 | status_file = fopen("statusfile.txt", "w"); | 290 | status_file = fopen("statusfile.txt", "w"); |
288 | fprintf(status_file, "%s", (char*)status); | 291 | fprintf(status_file, "%s", (char *)status); |
289 | fclose(status_file); | 292 | fclose(status_file); |
290 | } | 293 | } |
291 | } | 294 | } |
@@ -295,11 +298,13 @@ void accept_friend_request() | |||
295 | friend_request_received = 0; | 298 | friend_request_received = 0; |
296 | uint8_t numf = atoi(line + 3); | 299 | uint8_t numf = atoi(line + 3); |
297 | char numchar[100]; | 300 | char numchar[100]; |
301 | |||
298 | if (numf >= num_requests || pending_requests[numf].accepted) { | 302 | if (numf >= num_requests || pending_requests[numf].accepted) { |
299 | sprintf(numchar, "\n[i] you either didn't receive that request or you already accepted it"); | 303 | sprintf(numchar, "\n[i] you either didn't receive that request or you already accepted it"); |
300 | printf(numchar); | 304 | printf(numchar); |
301 | } else { | 305 | } else { |
302 | int num = m_addfriend_norequest(messenger, pending_requests[numf].id); | 306 | int num = m_addfriend_norequest(messenger, pending_requests[numf].id); |
307 | |||
303 | if (num != -1) { | 308 | if (num != -1) { |
304 | pending_requests[numf].accepted = 1; | 309 | pending_requests[numf].accepted = 1; |
305 | sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num); | 310 | sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num); |
@@ -312,13 +317,13 @@ void accept_friend_request() | |||
312 | } | 317 | } |
313 | } | 318 | } |
314 | 319 | ||
315 | void line_eval(char* line) | 320 | void line_eval(char *line) |
316 | { | 321 | { |
317 | if(line[0] == '/') { | 322 | if (line[0] == '/') { |
318 | 323 | ||
319 | char inpt_command = line[1]; | 324 | char inpt_command = line[1]; |
320 | 325 | ||
321 | if(inpt_command == 'f') { | 326 | if (inpt_command == 'f') { |
322 | add_friend(); | 327 | add_friend(); |
323 | } | 328 | } |
324 | 329 | ||
@@ -362,7 +367,7 @@ void line_eval(char* line) | |||
362 | 367 | ||
363 | void get_input() | 368 | void get_input() |
364 | { | 369 | { |
365 | while(1) { | 370 | while (1) { |
366 | fgets(line, STRING_LENGTH, stdin); | 371 | fgets(line, STRING_LENGTH, stdin); |
367 | line_eval(line); | 372 | line_eval(line); |
368 | strcpy(line, ""); | 373 | strcpy(line, ""); |
@@ -375,13 +380,16 @@ int main(int argc, char *argv[]) | |||
375 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); | 380 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); |
376 | exit(0); | 381 | exit(0); |
377 | } | 382 | } |
383 | |||
378 | messenger = initMessenger(); | 384 | messenger = initMessenger(); |
385 | |||
379 | if (messenger == 0) { | 386 | if (messenger == 0) { |
380 | printf("initMessenger failed"); | 387 | printf("initMessenger failed"); |
381 | exit(0); | 388 | exit(0); |
382 | } | 389 | } |
390 | |||
383 | if (argc > 4) { | 391 | if (argc > 4) { |
384 | if(strncmp(argv[4], "nokey", 6) < 0) { | 392 | if (strncmp(argv[4], "nokey", 6) < 0) { |
385 | } | 393 | } |
386 | } else { | 394 | } else { |
387 | load_key(); | 395 | load_key(); |
@@ -390,27 +398,33 @@ int main(int argc, char *argv[]) | |||
390 | int nameloaded = 0; | 398 | int nameloaded = 0; |
391 | int statusloaded = 0; | 399 | int statusloaded = 0; |
392 | 400 | ||
393 | FILE* name_file = NULL; | 401 | FILE *name_file = NULL; |
394 | name_file = fopen("namefile.txt", "r"); | 402 | name_file = fopen("namefile.txt", "r"); |
395 | if(name_file) { | 403 | |
404 | if (name_file) { | ||
396 | uint8_t name[MAX_NAME_LENGTH]; | 405 | uint8_t name[MAX_NAME_LENGTH]; |
406 | |||
397 | while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) { | 407 | while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) { |
398 | sscanf(line, "%s", (char*)name); | 408 | sscanf(line, "%s", (char *)name); |
399 | } | 409 | } |
400 | setname(messenger, name, strlen((char*)name)+1); | 410 | |
411 | setname(messenger, name, strlen((char *)name) + 1); | ||
401 | nameloaded = 1; | 412 | nameloaded = 1; |
402 | printf("%s\n", name); | 413 | printf("%s\n", name); |
403 | fclose(name_file); | 414 | fclose(name_file); |
404 | } | 415 | } |
405 | 416 | ||
406 | FILE* status_file = NULL; | 417 | FILE *status_file = NULL; |
407 | status_file = fopen("statusfile.txt", "r"); | 418 | status_file = fopen("statusfile.txt", "r"); |
408 | if(status_file) { | 419 | |
420 | if (status_file) { | ||
409 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 421 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; |
422 | |||
410 | while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) { | 423 | while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) { |
411 | sscanf(line, "%s", (char*)status); | 424 | sscanf(line, "%s", (char *)status); |
412 | } | 425 | } |
413 | m_set_statusmessage(messenger, status, strlen((char*)status)+1); | 426 | |
427 | m_set_statusmessage(messenger, status, strlen((char *)status) + 1); | ||
414 | statusloaded = 1; | 428 | statusloaded = 1; |
415 | printf("%s\n", status); | 429 | printf("%s\n", status); |
416 | fclose(status_file); | 430 | fclose(status_file); |
@@ -423,31 +437,35 @@ int main(int argc, char *argv[]) | |||
423 | char idstring1[PUB_KEY_BYTES][5]; | 437 | char idstring1[PUB_KEY_BYTES][5]; |
424 | char idstring2[PUB_KEY_BYTES][5]; | 438 | char idstring2[PUB_KEY_BYTES][5]; |
425 | int i; | 439 | int i; |
426 | for(i = 0; i < PUB_KEY_BYTES; i++) | 440 | |
427 | { | 441 | for (i = 0; i < PUB_KEY_BYTES; i++) { |
428 | if(self_public_key[i] < (PUB_KEY_BYTES/2)) | 442 | if (self_public_key[i] < (PUB_KEY_BYTES / 2)) |
429 | strcpy(idstring1[i],"0"); | 443 | strcpy(idstring1[i], "0"); |
430 | else | 444 | else |
431 | strcpy(idstring1[i], ""); | 445 | strcpy(idstring1[i], ""); |
432 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | 446 | |
447 | sprintf(idstring2[i], "%hhX", self_public_key[i]); | ||
433 | } | 448 | } |
434 | strcpy(users_id,"[i] your ID: "); | 449 | |
450 | strcpy(users_id, "[i] your ID: "); | ||
435 | int j; | 451 | int j; |
452 | |||
436 | for (j = 0; j < PUB_KEY_BYTES; j++) { | 453 | for (j = 0; j < PUB_KEY_BYTES; j++) { |
437 | strcat(users_id,idstring1[j]); | 454 | strcat(users_id, idstring1[j]); |
438 | strcat(users_id,idstring2[j]); | 455 | strcat(users_id, idstring2[j]); |
439 | } | 456 | } |
440 | 457 | ||
441 | do_header(); | 458 | do_header(); |
442 | 459 | ||
443 | IP_Port bootstrap_ip_port; | 460 | IP_Port bootstrap_ip_port; |
444 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 461 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
445 | int resolved_address = resolve_addr(argv[1]); | 462 | int resolved_address = resolve_addr(argv[1]); |
463 | |||
446 | if (resolved_address != 0) | 464 | if (resolved_address != 0) |
447 | bootstrap_ip_port.ip.i = resolved_address; | 465 | bootstrap_ip_port.ip.i = resolved_address; |
448 | else | 466 | else |
449 | exit(1); | 467 | exit(1); |
450 | 468 | ||
451 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 469 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
452 | 470 | ||
453 | int c; | 471 | int c; |
@@ -465,20 +483,23 @@ int main(int argc, char *argv[]) | |||
465 | printf("\n---------------------------------"); | 483 | printf("\n---------------------------------"); |
466 | } | 484 | } |
467 | 485 | ||
468 | while(1) { | 486 | while (1) { |
469 | if (on == 1 && DHT_isconnected() == -1) { | 487 | if (on == 1 && DHT_isconnected() == -1) { |
470 | printf("\n---------------------------------"); | 488 | printf("\n---------------------------------"); |
471 | printf("\n[i] Disconnected from the DHT"); | 489 | printf("\n[i] Disconnected from the DHT"); |
472 | printf("\n---------------------------------\n\n"); | 490 | printf("\n---------------------------------\n\n"); |
473 | on = 0; | 491 | on = 0; |
474 | } | 492 | } |
493 | |||
475 | if (on == 0 && DHT_isconnected()) { | 494 | if (on == 0 && DHT_isconnected()) { |
476 | printf("\n[i] Connected to DHT"); | 495 | printf("\n[i] Connected to DHT"); |
477 | printf("\n---------------------------------\n\n"); | 496 | printf("\n---------------------------------\n\n"); |
478 | on = 1; | 497 | on = 1; |
479 | } | 498 | } |
499 | |||
480 | doMessenger(messenger); | 500 | doMessenger(messenger); |
481 | Sleep(1); | 501 | Sleep(1); |
482 | } | 502 | } |
503 | |||
483 | return 0; | 504 | return 0; |
484 | } | 505 | } |
diff --git a/testing/nTox_win32.h b/testing/nTox_win32.h index 5dfe81c3..b174edcc 100644 --- a/testing/nTox_win32.h +++ b/testing/nTox_win32.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* nTox_win32.h | 1 | /* nTox_win32.h |
2 | * | 2 | * |
3 | * Textual frontend for Tox - Windows version | 3 | * Textual frontend for Tox - Windows version |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -31,7 +31,7 @@ | |||
31 | 31 | ||
32 | void do_header(); | 32 | void do_header(); |
33 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); | 33 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); |
34 | void print_message(Messenger *messenger, int friendnumber, uint8_t * string, uint16_t length, void *userdata); | 34 | void print_message(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
35 | void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata); | 35 | void print_nickchange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
36 | void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata); | 36 | void print_statuschange(Messenger *messenger, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
37 | void load_key(); | 37 | void load_key(); |
@@ -42,7 +42,7 @@ void message_friend(); | |||
42 | void change_nickname(); | 42 | void change_nickname(); |
43 | void change_status(int savetofile); | 43 | void change_status(int savetofile); |
44 | void accept_friend_request(); | 44 | void accept_friend_request(); |
45 | void line_eval(char* line); | 45 | void line_eval(char *line); |
46 | void get_input(); | 46 | void get_input(); |
47 | 47 | ||
48 | #endif | 48 | #endif |
diff --git a/testing/timer_test.c b/testing/timer_test.c index 63083940..f47d4878 100644 --- a/testing/timer_test.c +++ b/testing/timer_test.c | |||
@@ -3,7 +3,7 @@ | |||
3 | 3 | ||
4 | #ifdef WINDOWS | 4 | #ifdef WINDOWS |
5 | #include <windows.h> | 5 | #include <windows.h> |
6 | #else | 6 | #else |
7 | #include <unistd.h> | 7 | #include <unistd.h> |
8 | #endif | 8 | #endif |
9 | 9 | ||
@@ -16,30 +16,32 @@ void mssleep(int ms) | |||
16 | #endif | 16 | #endif |
17 | } | 17 | } |
18 | 18 | ||
19 | int callback(timer* t, void* arg){ | 19 | int callback(timer *t, void *arg) |
20 | printf("%s\n", (char*)arg); | 20 | { |
21 | printf("%s\n", (char *)arg); | ||
21 | return 1; | 22 | return 1; |
22 | } | 23 | } |
23 | 24 | ||
24 | int repeating(timer* t, void *arg) { | 25 | int repeating(timer *t, void *arg) |
25 | printf("%s\n", (char*)arg); | 26 | { |
27 | printf("%s\n", (char *)arg); | ||
26 | timer_start(t, 3); | 28 | timer_start(t, 3); |
27 | return 0; | 29 | return 0; |
28 | } | 30 | } |
29 | 31 | ||
30 | extern void timer_debug_print(); | 32 | extern void timer_debug_print(); |
31 | 33 | ||
32 | int main(int argc, char** argv) | 34 | int main(int argc, char **argv) |
33 | { | 35 | { |
34 | timer_init(); | 36 | timer_init(); |
35 | timer_debug_print(); | 37 | timer_debug_print(); |
36 | 38 | ||
37 | timer* t = new_timer(); | 39 | timer *t = new_timer(); |
38 | timer_setup(t, &callback, "Long setup method, 4 seconds"); | 40 | timer_setup(t, &callback, "Long setup method, 4 seconds"); |
39 | timer_start(t, 4); | 41 | timer_start(t, 4); |
40 | timer_debug_print(); | 42 | timer_debug_print(); |
41 | 43 | ||
42 | timer_single(&repeating, (void*)"This repeats every 3 seconds", 3); | 44 | timer_single(&repeating, (void *)"This repeats every 3 seconds", 3); |
43 | timer_debug_print(); | 45 | timer_debug_print(); |
44 | 46 | ||
45 | timer_single(&callback, "Short method, 4 seconds", 4); | 47 | timer_single(&callback, "Short method, 4 seconds", 4); |
@@ -53,7 +55,7 @@ int main(int argc, char** argv) | |||
53 | 55 | ||
54 | timer_single(&callback, "10 seconds", 10); | 56 | timer_single(&callback, "10 seconds", 10); |
55 | timer_debug_print(); | 57 | timer_debug_print(); |
56 | 58 | ||
57 | timer_us(&callback, "100000us", 100000); | 59 | timer_us(&callback, "100000us", 100000); |
58 | timer_us(&callback, "13s", 13 * US_PER_SECOND); | 60 | timer_us(&callback, "13s", 13 * US_PER_SECOND); |
59 | 61 | ||
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index d5edf956..ef932cd2 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -19,376 +19,391 @@ | |||
19 | #define CURS_Y_OFFSET 3 | 19 | #define CURS_Y_OFFSET 3 |
20 | 20 | ||
21 | typedef struct { | 21 | typedef struct { |
22 | int friendnum; | 22 | int friendnum; |
23 | char line[MAX_STR_SIZE]; | 23 | char line[MAX_STR_SIZE]; |
24 | size_t pos; | 24 | size_t pos; |
25 | WINDOW* history; | 25 | WINDOW *history; |
26 | WINDOW* linewin; | 26 | WINDOW *linewin; |
27 | } ChatContext; | 27 | } ChatContext; |
28 | 28 | ||
29 | void print_help(ChatContext *self); | 29 | void print_help(ChatContext *self); |
30 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); | 30 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); |
31 | 31 | ||
32 | struct tm *get_time(void) | 32 | struct tm *get_time(void) |
33 | { | 33 | { |
34 | struct tm *timeinfo; | 34 | struct tm *timeinfo; |
35 | time_t now; | 35 | time_t now; |
36 | time(&now); | 36 | time(&now); |
37 | timeinfo = localtime(&now); | 37 | timeinfo = localtime(&now); |
38 | return timeinfo; | 38 | return timeinfo; |
39 | } | 39 | } |
40 | 40 | ||
41 | static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) | 41 | static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) |
42 | { | 42 | { |
43 | ChatContext *ctx = (ChatContext*) self->x; | 43 | ChatContext *ctx = (ChatContext *) self->x; |
44 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 44 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
45 | struct tm *timeinfo = get_time(); | 45 | struct tm *timeinfo = get_time(); |
46 | 46 | ||
47 | if (ctx->friendnum != num) | 47 | if (ctx->friendnum != num) |
48 | return; | 48 | return; |
49 | 49 | ||
50 | getname(m, num, (uint8_t*) &nick); | 50 | getname(m, num, (uint8_t *) &nick); |
51 | msg[len-1] = '\0'; | 51 | msg[len - 1] = '\0'; |
52 | nick[MAX_NAME_LENGTH-1] = '\0'; | 52 | nick[MAX_NAME_LENGTH - 1] = '\0'; |
53 | fix_name(msg); | 53 | fix_name(msg); |
54 | fix_name(nick); | 54 | fix_name(nick); |
55 | 55 | ||
56 | wattron(ctx->history, COLOR_PAIR(2)); | 56 | wattron(ctx->history, COLOR_PAIR(2)); |
57 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 57 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
58 | wattroff(ctx->history, COLOR_PAIR(2)); | 58 | wattroff(ctx->history, COLOR_PAIR(2)); |
59 | wattron(ctx->history, COLOR_PAIR(4)); | 59 | wattron(ctx->history, COLOR_PAIR(4)); |
60 | wprintw(ctx->history, "%s: ", nick); | 60 | wprintw(ctx->history, "%s: ", nick); |
61 | wattroff(ctx->history, COLOR_PAIR(4)); | 61 | wattroff(ctx->history, COLOR_PAIR(4)); |
62 | wprintw(ctx->history, "%s\n", msg); | 62 | wprintw(ctx->history, "%s\n", msg); |
63 | 63 | ||
64 | self->blink = true; | 64 | self->blink = true; |
65 | beep(); | 65 | beep(); |
66 | } | 66 | } |
67 | 67 | ||
68 | static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) | 68 | static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) |
69 | { | 69 | { |
70 | ChatContext *ctx = (ChatContext*) self->x; | 70 | ChatContext *ctx = (ChatContext *) self->x; |
71 | struct tm *timeinfo = get_time(); | 71 | struct tm *timeinfo = get_time(); |
72 | 72 | ||
73 | if (ctx->friendnum != num) | 73 | if (ctx->friendnum != num) |
74 | return; | 74 | return; |
75 | 75 | ||
76 | action[len-1] = '\0'; | 76 | action[len - 1] = '\0'; |
77 | fix_name(action); | 77 | fix_name(action); |
78 | 78 | ||
79 | wattron(ctx->history, COLOR_PAIR(2)); | 79 | wattron(ctx->history, COLOR_PAIR(2)); |
80 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 80 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
81 | wattroff(ctx->history, COLOR_PAIR(2)); | 81 | wattroff(ctx->history, COLOR_PAIR(2)); |
82 | 82 | ||
83 | wattron(ctx->history, COLOR_PAIR(5)); | 83 | wattron(ctx->history, COLOR_PAIR(5)); |
84 | wprintw(ctx->history, "%s\n", action); | 84 | wprintw(ctx->history, "%s\n", action); |
85 | wattroff(ctx->history, COLOR_PAIR(5)); | 85 | wattroff(ctx->history, COLOR_PAIR(5)); |
86 | 86 | ||
87 | self->blink = true; | 87 | self->blink = true; |
88 | beep(); | 88 | beep(); |
89 | } | 89 | } |
90 | 90 | ||
91 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) | 91 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) |
92 | { | 92 | { |
93 | ChatContext *ctx = (ChatContext*) self->x; | 93 | ChatContext *ctx = (ChatContext *) self->x; |
94 | struct tm *timeinfo = get_time(); | 94 | struct tm *timeinfo = get_time(); |
95 | if (ctx->friendnum != num) | 95 | |
96 | return; | 96 | if (ctx->friendnum != num) |
97 | 97 | return; | |
98 | wattron(ctx->history, COLOR_PAIR(2)); | 98 | |
99 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 99 | wattron(ctx->history, COLOR_PAIR(2)); |
100 | wattroff(ctx->history, COLOR_PAIR(2)); | 100 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
101 | 101 | wattroff(ctx->history, COLOR_PAIR(2)); | |
102 | nick[len-1] = '\0'; | 102 | |
103 | fix_name(nick); | 103 | nick[len - 1] = '\0'; |
104 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); | 104 | fix_name(nick); |
105 | 105 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); | |
106 | wattron(ctx->history, COLOR_PAIR(3)); | 106 | |
107 | wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick); | 107 | wattron(ctx->history, COLOR_PAIR(3)); |
108 | wattroff(ctx->history, COLOR_PAIR(3)); | 108 | wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick); |
109 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
109 | } | 110 | } |
110 | 111 | ||
111 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) | 112 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) |
112 | { | 113 | { |
113 | ChatContext *ctx = (ChatContext*) self->x; | 114 | ChatContext *ctx = (ChatContext *) self->x; |
114 | struct tm *timeinfo = get_time(); | 115 | struct tm *timeinfo = get_time(); |
115 | if (ctx->friendnum != num) | 116 | |
116 | return; | 117 | if (ctx->friendnum != num) |
117 | 118 | return; | |
118 | wattron(ctx->history, COLOR_PAIR(2)); | 119 | |
119 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 120 | wattron(ctx->history, COLOR_PAIR(2)); |
120 | wattroff(ctx->history, COLOR_PAIR(2)); | 121 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
121 | 122 | wattroff(ctx->history, COLOR_PAIR(2)); | |
122 | status[len-1] = '\0'; | 123 | |
123 | fix_name(status); | 124 | status[len - 1] = '\0'; |
124 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num); | 125 | fix_name(status); |
125 | 126 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num); | |
126 | wattron(ctx->history, COLOR_PAIR(3)); | 127 | |
127 | wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); | 128 | wattron(ctx->history, COLOR_PAIR(3)); |
128 | wattroff(ctx->history, COLOR_PAIR(3)); | 129 | wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); |
129 | 130 | wattroff(ctx->history, COLOR_PAIR(3)); | |
131 | |||
130 | } | 132 | } |
131 | 133 | ||
132 | /* check that the string has one non-space character */ | 134 | /* check that the string has one non-space character */ |
133 | int string_is_empty(char *string) | 135 | int string_is_empty(char *string) |
134 | { | 136 | { |
135 | int rc = 0; | 137 | int rc = 0; |
136 | char *copy = strdup(string); | 138 | char *copy = strdup(string); |
137 | rc = ((strtok(copy, " ") == NULL) ? 1:0); | 139 | rc = ((strtok(copy, " ") == NULL) ? 1 : 0); |
138 | free(copy); | 140 | free(copy); |
139 | return rc; | 141 | return rc; |
140 | } | 142 | } |
141 | 143 | ||
142 | static void chat_onKey(ToxWindow *self, Messenger *m, int key) | 144 | static void chat_onKey(ToxWindow *self, Messenger *m, int key) |
143 | { | 145 | { |
144 | ChatContext *ctx = (ChatContext*) self->x; | 146 | ChatContext *ctx = (ChatContext *) self->x; |
145 | struct tm *timeinfo = get_time(); | 147 | struct tm *timeinfo = get_time(); |
146 | 148 | ||
147 | int x, y, y2, x2; | 149 | int x, y, y2, x2; |
148 | getyx(self->window, y, x); | 150 | getyx(self->window, y, x); |
149 | getmaxyx(self->window, y2, x2); | 151 | getmaxyx(self->window, y2, x2); |
150 | 152 | ||
151 | /* Add printable chars to buffer and print on input space */ | 153 | /* Add printable chars to buffer and print on input space */ |
152 | if (isprint(key)) { | 154 | if (isprint(key)) { |
153 | if (ctx->pos != sizeof(ctx->line)-1) { | 155 | if (ctx->pos != sizeof(ctx->line) - 1) { |
154 | mvwaddch(self->window, y, x, key); | 156 | mvwaddch(self->window, y, x, key); |
155 | ctx->line[ctx->pos++] = key; | 157 | ctx->line[ctx->pos++] = key; |
156 | ctx->line[ctx->pos] = '\0'; | 158 | ctx->line[ctx->pos] = '\0'; |
159 | } | ||
157 | } | 160 | } |
158 | } | 161 | |
159 | 162 | /* BACKSPACE key: Remove one character from line */ | |
160 | /* BACKSPACE key: Remove one character from line */ | 163 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { |
161 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { | 164 | if (ctx->pos > 0) { |
162 | if (ctx->pos > 0) { | 165 | ctx->line[--ctx->pos] = '\0'; |
163 | ctx->line[--ctx->pos] = '\0'; | 166 | |
164 | if (x == 0) | 167 | if (x == 0) |
165 | mvwdelch(self->window, y-1, x2-1); | 168 | mvwdelch(self->window, y - 1, x2 - 1); |
166 | else | 169 | else |
167 | mvwdelch(self->window, y, x-1); | 170 | mvwdelch(self->window, y, x - 1); |
171 | } | ||
168 | } | 172 | } |
169 | } | ||
170 | |||
171 | /* RETURN key: Execute command or print line */ | ||
172 | else if (key == '\n') { | ||
173 | wclear(ctx->linewin); | ||
174 | wmove(self->window, y2-CURS_Y_OFFSET, 0); | ||
175 | wclrtobot(self->window); | ||
176 | if (ctx->line[0] == '/') | ||
177 | execute(self, ctx, m, ctx->line); | ||
178 | else { | ||
179 | /* make sure the string has at least non-space character */ | ||
180 | if (!string_is_empty(ctx->line)) { | ||
181 | uint8_t selfname[MAX_NAME_LENGTH]; | ||
182 | getself_name(m, selfname, sizeof(selfname)); | ||
183 | fix_name(selfname); | ||
184 | 173 | ||
185 | wattron(ctx->history, COLOR_PAIR(2)); | 174 | /* RETURN key: Execute command or print line */ |
186 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 175 | else if (key == '\n') { |
187 | wattroff(ctx->history, COLOR_PAIR(2)); | 176 | wclear(ctx->linewin); |
188 | wattron(ctx->history, COLOR_PAIR(1)); | 177 | wmove(self->window, y2 - CURS_Y_OFFSET, 0); |
189 | wprintw(ctx->history, "%s: ", selfname); | 178 | wclrtobot(self->window); |
190 | wattroff(ctx->history, COLOR_PAIR(1)); | 179 | |
191 | wprintw(ctx->history, "%s\n", ctx->line); | 180 | if (ctx->line[0] == '/') |
192 | if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) { | 181 | execute(self, ctx, m, ctx->line); |
193 | wattron(ctx->history, COLOR_PAIR(3)); | 182 | else { |
194 | wprintw(ctx->history, " * Failed to send message.\n"); | 183 | /* make sure the string has at least non-space character */ |
195 | wattroff(ctx->history, COLOR_PAIR(3)); | 184 | if (!string_is_empty(ctx->line)) { |
185 | uint8_t selfname[MAX_NAME_LENGTH]; | ||
186 | getself_name(m, selfname, sizeof(selfname)); | ||
187 | fix_name(selfname); | ||
188 | |||
189 | wattron(ctx->history, COLOR_PAIR(2)); | ||
190 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
191 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
192 | wattron(ctx->history, COLOR_PAIR(1)); | ||
193 | wprintw(ctx->history, "%s: ", selfname); | ||
194 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
195 | wprintw(ctx->history, "%s\n", ctx->line); | ||
196 | |||
197 | if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) { | ||
198 | wattron(ctx->history, COLOR_PAIR(3)); | ||
199 | wprintw(ctx->history, " * Failed to send message.\n"); | ||
200 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
201 | } | ||
202 | } | ||
196 | } | 203 | } |
197 | } | 204 | |
205 | ctx->line[0] = '\0'; | ||
206 | ctx->pos = 0; | ||
198 | } | 207 | } |
199 | ctx->line[0] = '\0'; | ||
200 | ctx->pos = 0; | ||
201 | } | ||
202 | } | 208 | } |
203 | 209 | ||
204 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) | 210 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) |
205 | { | 211 | { |
206 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | 212 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { |
207 | wclear(self->window); | 213 | wclear(self->window); |
208 | wclear(ctx->history); | 214 | wclear(ctx->history); |
209 | int x, y; | 215 | int x, y; |
210 | getmaxyx(self->window, y, x); | 216 | getmaxyx(self->window, y, x); |
211 | (void) x; | 217 | (void) x; |
212 | wmove(self->window, y-CURS_Y_OFFSET, 0); | 218 | wmove(self->window, y - CURS_Y_OFFSET, 0); |
213 | } | 219 | } |
214 | |||
215 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | ||
216 | print_help(ctx); | ||
217 | 220 | ||
218 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | 221 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) |
219 | endwin(); | 222 | print_help(ctx); |
220 | exit(0); | ||
221 | } | ||
222 | 223 | ||
223 | else if (!strncmp(cmd, "/me ", strlen("/me "))) { | 224 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { |
224 | struct tm *timeinfo = get_time(); | 225 | endwin(); |
225 | char *action = strchr(cmd, ' '); | 226 | exit(0); |
226 | if (action == NULL) { | ||
227 | wprintw(self->window, "Invalid syntax.\n"); | ||
228 | return; | ||
229 | } | 227 | } |
230 | action++; | ||
231 | 228 | ||
232 | wattron(ctx->history, COLOR_PAIR(2)); | 229 | else if (!strncmp(cmd, "/me ", strlen("/me "))) { |
233 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 230 | struct tm *timeinfo = get_time(); |
234 | wattroff(ctx->history, COLOR_PAIR(2)); | 231 | char *action = strchr(cmd, ' '); |
235 | 232 | ||
236 | uint8_t selfname[MAX_NAME_LENGTH]; | 233 | if (action == NULL) { |
237 | int len = getself_name(m, selfname, sizeof(selfname)); | 234 | wprintw(self->window, "Invalid syntax.\n"); |
238 | char msg[MAX_STR_SIZE-len-4]; | 235 | return; |
239 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); | 236 | } |
240 | 237 | ||
241 | wattron(ctx->history, COLOR_PAIR(5)); | 238 | action++; |
242 | wprintw(ctx->history, msg); | ||
243 | wattroff(ctx->history, COLOR_PAIR(5)); | ||
244 | if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { | ||
245 | wattron(ctx->history, COLOR_PAIR(3)); | ||
246 | wprintw(ctx->history, " * Failed to send action\n"); | ||
247 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
248 | } | ||
249 | } | ||
250 | |||
251 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | ||
252 | char *status = strchr(cmd, ' '); | ||
253 | char *msg; | ||
254 | char *status_text; | ||
255 | if (status == NULL) { | ||
256 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
257 | return; | ||
258 | } | ||
259 | status++; | ||
260 | USERSTATUS status_kind; | ||
261 | if (!strncmp(status, "online", strlen("online"))) { | ||
262 | status_kind = USERSTATUS_NONE; | ||
263 | status_text = "ONLINE"; | ||
264 | } | ||
265 | 239 | ||
266 | else if (!strncmp(status, "away", strlen("away"))) { | 240 | wattron(ctx->history, COLOR_PAIR(2)); |
267 | status_kind = USERSTATUS_AWAY; | 241 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
268 | status_text = "AWAY"; | 242 | wattroff(ctx->history, COLOR_PAIR(2)); |
269 | } | ||
270 | 243 | ||
271 | else if (!strncmp(status, "busy", strlen("busy"))) { | 244 | uint8_t selfname[MAX_NAME_LENGTH]; |
272 | status_kind = USERSTATUS_BUSY; | 245 | int len = getself_name(m, selfname, sizeof(selfname)); |
273 | status_text = "BUSY"; | 246 | char msg[MAX_STR_SIZE - len - 4]; |
274 | } | 247 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); |
275 | 248 | ||
276 | else { | 249 | wattron(ctx->history, COLOR_PAIR(5)); |
277 | wprintw(ctx->history, "Invalid status.\n"); | 250 | wprintw(ctx->history, msg); |
278 | return; | 251 | wattroff(ctx->history, COLOR_PAIR(5)); |
279 | } | ||
280 | 252 | ||
281 | msg = strchr(status, ' '); | 253 | if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { |
282 | if (msg == NULL) { | 254 | wattron(ctx->history, COLOR_PAIR(3)); |
283 | m_set_userstatus(m, status_kind); | 255 | wprintw(ctx->history, " * Failed to send action\n"); |
284 | wprintw(ctx->history, "Status set to: %s\n", status_text); | 256 | wattroff(ctx->history, COLOR_PAIR(3)); |
257 | } | ||
285 | } | 258 | } |
286 | else { | 259 | |
287 | msg++; | 260 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { |
288 | m_set_userstatus(m, status_kind); | 261 | char *status = strchr(cmd, ' '); |
289 | m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1); | 262 | char *msg; |
290 | wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); | 263 | char *status_text; |
264 | |||
265 | if (status == NULL) { | ||
266 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
267 | return; | ||
268 | } | ||
269 | |||
270 | status++; | ||
271 | USERSTATUS status_kind; | ||
272 | |||
273 | if (!strncmp(status, "online", strlen("online"))) { | ||
274 | status_kind = USERSTATUS_NONE; | ||
275 | status_text = "ONLINE"; | ||
276 | } | ||
277 | |||
278 | else if (!strncmp(status, "away", strlen("away"))) { | ||
279 | status_kind = USERSTATUS_AWAY; | ||
280 | status_text = "AWAY"; | ||
281 | } | ||
282 | |||
283 | else if (!strncmp(status, "busy", strlen("busy"))) { | ||
284 | status_kind = USERSTATUS_BUSY; | ||
285 | status_text = "BUSY"; | ||
286 | } | ||
287 | |||
288 | else { | ||
289 | wprintw(ctx->history, "Invalid status.\n"); | ||
290 | return; | ||
291 | } | ||
292 | |||
293 | msg = strchr(status, ' '); | ||
294 | |||
295 | if (msg == NULL) { | ||
296 | m_set_userstatus(m, status_kind); | ||
297 | wprintw(ctx->history, "Status set to: %s\n", status_text); | ||
298 | } else { | ||
299 | msg++; | ||
300 | m_set_userstatus(m, status_kind); | ||
301 | m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); | ||
302 | wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); | ||
303 | } | ||
291 | } | 304 | } |
292 | } | 305 | |
293 | 306 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | |
294 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | 307 | char *nick; |
295 | char *nick; | 308 | nick = strchr(cmd, ' '); |
296 | nick = strchr(cmd, ' '); | 309 | |
297 | if (nick == NULL) { | 310 | if (nick == NULL) { |
298 | wprintw(ctx->history, "Invalid syntax.\n"); | 311 | wprintw(ctx->history, "Invalid syntax.\n"); |
299 | return; | 312 | return; |
313 | } | ||
314 | |||
315 | nick++; | ||
316 | setname(m, (uint8_t *) nick, strlen(nick) + 1); | ||
317 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | ||
300 | } | 318 | } |
301 | 319 | ||
302 | nick++; | 320 | else if (!strcmp(cmd, "/myid")) { |
303 | setname(m, (uint8_t*) nick, strlen(nick)+1); | 321 | char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; |
304 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | 322 | int i; |
305 | } | 323 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
306 | 324 | getaddress(m, address); | |
307 | else if (!strcmp(cmd, "/myid")) { | 325 | |
308 | char id[FRIEND_ADDRESS_SIZE*2+1] = {0}; | 326 | for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { |
309 | int i; | 327 | char xx[3]; |
310 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 328 | snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); |
311 | getaddress(m, address); | 329 | strcat(id, xx); |
312 | for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { | 330 | } |
313 | char xx[3]; | 331 | |
314 | snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); | 332 | wprintw(ctx->history, "%s\n", id); |
315 | strcat(id, xx); | ||
316 | } | 333 | } |
317 | wprintw(ctx->history, "%s\n", id); | ||
318 | } | ||
319 | 334 | ||
320 | else if (strcmp(ctx->line, "/close") == 0) { | 335 | else if (strcmp(ctx->line, "/close") == 0) { |
321 | int f_num = ctx->friendnum; | 336 | int f_num = ctx->friendnum; |
322 | delwin(ctx->linewin); | 337 | delwin(ctx->linewin); |
323 | del_window(self, f_num); | 338 | del_window(self, f_num); |
324 | } | 339 | } |
325 | 340 | ||
326 | else | 341 | else |
327 | wprintw(ctx->history, "Invalid command.\n"); | 342 | wprintw(ctx->history, "Invalid command.\n"); |
328 | } | 343 | } |
329 | 344 | ||
330 | static void chat_onDraw(ToxWindow *self) | 345 | static void chat_onDraw(ToxWindow *self) |
331 | { | 346 | { |
332 | curs_set(1); | 347 | curs_set(1); |
333 | int x, y; | 348 | int x, y; |
334 | getmaxyx(self->window, y, x); | 349 | getmaxyx(self->window, y, x); |
335 | (void) y; | 350 | (void) y; |
336 | ChatContext *ctx = (ChatContext*) self->x; | 351 | ChatContext *ctx = (ChatContext *) self->x; |
337 | mvwhline(ctx->linewin, 0, 0, '_', x); | 352 | mvwhline(ctx->linewin, 0, 0, '_', x); |
338 | wrefresh(self->window); | 353 | wrefresh(self->window); |
339 | } | 354 | } |
340 | 355 | ||
341 | static void chat_onInit(ToxWindow *self, Messenger *m) | 356 | static void chat_onInit(ToxWindow *self, Messenger *m) |
342 | { | 357 | { |
343 | int x, y; | 358 | int x, y; |
344 | ChatContext *ctx = (ChatContext*) self->x; | 359 | ChatContext *ctx = (ChatContext *) self->x; |
345 | getmaxyx(self->window, y, x); | 360 | getmaxyx(self->window, y, x); |
346 | ctx->history = subwin(self->window, y-4, x, 0, 0); | 361 | ctx->history = subwin(self->window, y - 4, x, 0, 0); |
347 | scrollok(ctx->history, 1); | 362 | scrollok(ctx->history, 1); |
348 | ctx->linewin = subwin(self->window, 2, x, y-4, 0); | 363 | ctx->linewin = subwin(self->window, 2, x, y - 4, 0); |
349 | print_help(ctx); | 364 | print_help(ctx); |
350 | wmove(self->window, y-CURS_Y_OFFSET, 0); | 365 | wmove(self->window, y - CURS_Y_OFFSET, 0); |
351 | } | 366 | } |
352 | 367 | ||
353 | void print_help(ChatContext *self) | 368 | void print_help(ChatContext *self) |
354 | { | 369 | { |
355 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | 370 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); |
356 | wprintw(self->history, "Commands:\n"); | 371 | wprintw(self->history, "Commands:\n"); |
357 | wattroff(self->history, A_BOLD); | 372 | wattroff(self->history, A_BOLD); |
358 | 373 | ||
359 | wprintw(self->history, " /status <type> <message> : Set your status\n"); | 374 | wprintw(self->history, " /status <type> <message> : Set your status\n"); |
360 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | 375 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); |
361 | wprintw(self->history, " /me <action> : Do an action\n"); | 376 | wprintw(self->history, " /me <action> : Do an action\n"); |
362 | wprintw(self->history, " /myid : Print your ID\n"); | 377 | wprintw(self->history, " /myid : Print your ID\n"); |
363 | wprintw(self->history, " /clear : Clear the screen\n"); | 378 | wprintw(self->history, " /clear : Clear the screen\n"); |
364 | wprintw(self->history, " /close : Close the current chat window\n"); | 379 | wprintw(self->history, " /close : Close the current chat window\n"); |
365 | wprintw(self->history, " /quit or /exit : Exit program\n"); | 380 | wprintw(self->history, " /quit or /exit : Exit program\n"); |
366 | wprintw(self->history, " /help : Print this message again\n\n"); | 381 | wprintw(self->history, " /help : Print this message again\n\n"); |
367 | 382 | ||
368 | wattroff(self->history, COLOR_PAIR(2)); | 383 | wattroff(self->history, COLOR_PAIR(2)); |
369 | } | 384 | } |
370 | 385 | ||
371 | ToxWindow new_chat(Messenger *m, int friendnum) | 386 | ToxWindow new_chat(Messenger *m, int friendnum) |
372 | { | 387 | { |
373 | ToxWindow ret; | 388 | ToxWindow ret; |
374 | memset(&ret, 0, sizeof(ret)); | 389 | memset(&ret, 0, sizeof(ret)); |
375 | 390 | ||
376 | ret.onKey = &chat_onKey; | 391 | ret.onKey = &chat_onKey; |
377 | ret.onDraw = &chat_onDraw; | 392 | ret.onDraw = &chat_onDraw; |
378 | ret.onInit = &chat_onInit; | 393 | ret.onInit = &chat_onInit; |
379 | ret.onMessage = &chat_onMessage; | 394 | ret.onMessage = &chat_onMessage; |
380 | ret.onNickChange = &chat_onNickChange; | 395 | ret.onNickChange = &chat_onNickChange; |
381 | ret.onStatusChange = &chat_onStatusChange; | 396 | ret.onStatusChange = &chat_onStatusChange; |
382 | ret.onAction = &chat_onAction; | 397 | ret.onAction = &chat_onAction; |
383 | 398 | ||
384 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 399 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
385 | getname(m, friendnum, (uint8_t*) &nick); | 400 | getname(m, friendnum, (uint8_t *) &nick); |
386 | fix_name(nick); | 401 | fix_name(nick); |
387 | 402 | ||
388 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); | 403 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); |
389 | 404 | ||
390 | ChatContext *x = calloc(1, sizeof(ChatContext)); | 405 | ChatContext *x = calloc(1, sizeof(ChatContext)); |
391 | x->friendnum = friendnum; | 406 | x->friendnum = friendnum; |
392 | ret.x = (void*) x; | 407 | ret.x = (void *) x; |
393 | return ret; | 408 | return ret; |
394 | } | 409 | } |
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index 18e211ce..1a62e8ed 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c | |||
@@ -50,6 +50,7 @@ char *get_user_config_dir(void) | |||
50 | BOOL ok; | 50 | BOOL ok; |
51 | 51 | ||
52 | ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); | 52 | ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); |
53 | |||
53 | if (!ok) { | 54 | if (!ok) { |
54 | return NULL; | 55 | return NULL; |
55 | } | 56 | } |
@@ -72,13 +73,16 @@ char *get_user_config_dir(void) | |||
72 | int rc; | 73 | int rc; |
73 | 74 | ||
74 | rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); | 75 | rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); |
76 | |||
75 | if (rc == 0) { | 77 | if (rc == 0) { |
76 | home = pwd.pw_dir; | 78 | home = pwd.pw_dir; |
77 | } else { | 79 | } else { |
78 | home = getenv("HOME"); | 80 | home = getenv("HOME"); |
81 | |||
79 | if (home == NULL) { | 82 | if (home == NULL) { |
80 | return NULL; | 83 | return NULL; |
81 | } | 84 | } |
85 | |||
82 | /* env variables can be tainted */ | 86 | /* env variables can be tainted */ |
83 | snprintf(buf, sizeof(buf), "%s", home); | 87 | snprintf(buf, sizeof(buf), "%s", home); |
84 | home = buf; | 88 | home = buf; |
@@ -87,6 +91,7 @@ char *get_user_config_dir(void) | |||
87 | # if defined(__APPLE__) | 91 | # if defined(__APPLE__) |
88 | len = strlen(home) + strlen("/Library/Application Support") + 1; | 92 | len = strlen(home) + strlen("/Library/Application Support") + 1; |
89 | user_config_dir = malloc(len); | 93 | user_config_dir = malloc(len); |
94 | |||
90 | if (user_config_dir == NULL) { | 95 | if (user_config_dir == NULL) { |
91 | return NULL; | 96 | return NULL; |
92 | } | 97 | } |
@@ -95,6 +100,7 @@ char *get_user_config_dir(void) | |||
95 | # else /* __APPLE__ */ | 100 | # else /* __APPLE__ */ |
96 | len = strlen(home) + strlen("/.config") + 1; | 101 | len = strlen(home) + strlen("/.config") + 1; |
97 | user_config_dir = malloc(len); | 102 | user_config_dir = malloc(len); |
103 | |||
98 | if (user_config_dir == NULL) { | 104 | if (user_config_dir == NULL) { |
99 | return NULL; | 105 | return NULL; |
100 | } | 106 | } |
@@ -111,44 +117,45 @@ char *get_user_config_dir(void) | |||
111 | * Creates the config directory. | 117 | * Creates the config directory. |
112 | */ | 118 | */ |
113 | int create_user_config_dir(char *path) | 119 | int create_user_config_dir(char *path) |
114 | { | 120 | { |
115 | 121 | ||
116 | int mkdir_err; | 122 | int mkdir_err; |
117 | 123 | ||
118 | #ifdef WIN32 | 124 | #ifdef WIN32 |
119 | 125 | ||
120 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | 126 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); |
121 | strcpy(fullpath, path); | 127 | strcpy(fullpath, path); |
122 | strcat(fullpath, CONFIGDIR); | 128 | strcat(fullpath, CONFIGDIR); |
123 | 129 | ||
124 | mkdir_err = _mkdir(fullpath); | 130 | mkdir_err = _mkdir(fullpath); |
125 | struct __stat64 buf; | 131 | struct __stat64 buf; |
126 | if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
127 | free(fullpath); | ||
128 | return -1; | ||
129 | } | ||
130 | 132 | ||
131 | #else | 133 | if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { |
134 | free(fullpath); | ||
135 | return -1; | ||
136 | } | ||
132 | 137 | ||
133 | mkdir_err = mkdir(path, 0700); | 138 | #else |
134 | struct stat buf; | ||
135 | 139 | ||
136 | if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { | 140 | mkdir_err = mkdir(path, 0700); |
137 | return -1; | 141 | struct stat buf; |
138 | } | ||
139 | 142 | ||
140 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | 143 | if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { |
141 | strcpy(fullpath, path); | 144 | return -1; |
142 | strcat(fullpath, CONFIGDIR); | 145 | } |
143 | 146 | ||
144 | mkdir_err = mkdir(fullpath, 0700); | 147 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); |
148 | strcpy(fullpath, path); | ||
149 | strcat(fullpath, CONFIGDIR); | ||
150 | |||
151 | mkdir_err = mkdir(fullpath, 0700); | ||
152 | |||
153 | if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
154 | free(fullpath); | ||
155 | return -1; | ||
156 | } | ||
145 | 157 | ||
146 | if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | 158 | #endif |
147 | free(fullpath); | 159 | free(fullpath); |
148 | return -1; | 160 | return 0; |
149 | } | ||
150 | |||
151 | #endif | ||
152 | free(fullpath); | ||
153 | return 0; | ||
154 | } | 161 | } |
diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h index fad949cf..17d95107 100644 --- a/testing/toxic/configdir.h +++ b/testing/toxic/configdir.h | |||
@@ -23,7 +23,7 @@ | |||
23 | #else | 23 | #else |
24 | #define CONFIGDIR "/toxic/" | 24 | #define CONFIGDIR "/toxic/" |
25 | #endif | 25 | #endif |
26 | 26 | ||
27 | #ifndef S_ISDIR | 27 | #ifndef S_ISDIR |
28 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | 28 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
29 | #endif | 29 | #endif |
diff --git a/testing/toxic/dhtstatus.c b/testing/toxic/dhtstatus.c index e026a173..a11dc616 100644 --- a/testing/toxic/dhtstatus.c +++ b/testing/toxic/dhtstatus.c | |||
@@ -3,87 +3,97 @@ | |||
3 | #include "../../core/network.h" | 3 | #include "../../core/network.h" |
4 | #include "../../core/DHT.h" | 4 | #include "../../core/DHT.h" |
5 | 5 | ||
6 | typedef uint8_t ipbuf[3*4+3+1]; | 6 | typedef uint8_t ipbuf[3 * 4 + 3 + 1]; |
7 | static int num_selected = 0; | 7 | static int num_selected = 0; |
8 | 8 | ||
9 | static void printip(ipbuf buf, IP ip) | 9 | static void printip(ipbuf buf, IP ip) |
10 | { | 10 | { |
11 | sprintf((char*)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]); | 11 | sprintf((char *)buf, "%u.%u.%u.%u", ip.c[0], ip.c[1], ip.c[2], ip.c[3]); |
12 | } | 12 | } |
13 | 13 | ||
14 | static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key) | 14 | static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key) |
15 | { | 15 | { |
16 | switch(key) { | 16 | switch (key) { |
17 | case KEY_UP: | 17 | case KEY_UP: |
18 | case 'k': | 18 | case 'k': |
19 | if (--num_selected < 0) | 19 | if (--num_selected < 0) |
20 | num_selected = CLIENT_ID_SIZE-1; | 20 | num_selected = CLIENT_ID_SIZE - 1; |
21 | break; | 21 | |
22 | 22 | break; | |
23 | case KEY_DOWN: | 23 | |
24 | case 'j': | 24 | case KEY_DOWN: |
25 | num_selected = (num_selected+1) % CLIENT_ID_SIZE; | 25 | case 'j': |
26 | break; | 26 | num_selected = (num_selected + 1) % CLIENT_ID_SIZE; |
27 | 27 | break; | |
28 | case '\n': | 28 | |
29 | break; | 29 | case '\n': |
30 | 30 | break; | |
31 | default: | 31 | |
32 | break; | 32 | default: |
33 | } | 33 | break; |
34 | } | ||
34 | } | 35 | } |
35 | 36 | ||
36 | static void dhtstatus_onDraw(ToxWindow *self) | 37 | static void dhtstatus_onDraw(ToxWindow *self) |
37 | { | 38 | { |
38 | Client_data * close_clientlist = DHT_get_close_list(); | 39 | Client_data *close_clientlist = DHT_get_close_list(); |
39 | curs_set(0); | 40 | curs_set(0); |
40 | werase(self->window); | 41 | werase(self->window); |
41 | 42 | ||
42 | uint64_t now = unix_time(); | 43 | uint64_t now = unix_time(); |
43 | uint32_t i, j; | 44 | uint32_t i, j; |
44 | ipbuf ipbuf; | 45 | ipbuf ipbuf; |
45 | wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now); | 46 | wprintw(self->window, |
46 | for(i = 0; i < 32; i++) { /*Number of nodes in closelist*/ | 47 | "\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", |
47 | Client_data * client = close_clientlist + i; | 48 | now); |
48 | if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); | 49 | |
49 | wprintw(self->window,"[%02i] ", i); | 50 | for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/ |
50 | uint16_t port = ntohs(client->ip_port.port); | 51 | Client_data *client = close_clientlist + i; |
51 | if(port) { | 52 | |
52 | for(j = 0; j < CLIENT_ID_SIZE; j++) | 53 | if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); |
53 | wprintw(self->window, "%02hhx", client->client_id[j]); | 54 | |
54 | 55 | wprintw(self->window, "[%02i] ", i); | |
55 | printip(ipbuf, client->ip_port.ip); | 56 | uint16_t port = ntohs(client->ip_port.port); |
56 | wprintw(self->window, " %15s %5u ", ipbuf, port); | 57 | |
57 | wprintw(self->window, " %3llu ", now - client->timestamp); | 58 | if (port) { |
58 | wprintw(self->window, " %3llu ", now - client->last_pinged); | 59 | for (j = 0; j < CLIENT_ID_SIZE; j++) |
59 | 60 | wprintw(self->window, "%02hhx", client->client_id[j]); | |
60 | port = ntohs(client->ret_ip_port.port); | 61 | |
61 | if(port) { | 62 | printip(ipbuf, client->ip_port.ip); |
62 | printip(ipbuf, client->ret_ip_port.ip); | 63 | wprintw(self->window, " %15s %5u ", ipbuf, port); |
63 | wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp); | 64 | wprintw(self->window, " %3llu ", now - client->timestamp); |
64 | } | 65 | wprintw(self->window, " %3llu ", now - client->last_pinged); |
66 | |||
67 | port = ntohs(client->ret_ip_port.port); | ||
68 | |||
69 | if (port) { | ||
70 | printip(ipbuf, client->ret_ip_port.ip); | ||
71 | wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | wprintw(self->window, "\n"); | ||
76 | |||
77 | if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); | ||
65 | } | 78 | } |
66 | wprintw(self->window, "\n"); | 79 | |
67 | if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); | 80 | wrefresh(self->window); |
68 | } | ||
69 | |||
70 | wrefresh(self->window); | ||
71 | } | 81 | } |
72 | 82 | ||
73 | static void dhtstatus_onInit(ToxWindow *self, Messenger *m) | 83 | static void dhtstatus_onInit(ToxWindow *self, Messenger *m) |
74 | { | 84 | { |
75 | 85 | ||
76 | } | 86 | } |
77 | 87 | ||
78 | ToxWindow new_dhtstatus() | 88 | ToxWindow new_dhtstatus() |
79 | { | 89 | { |
80 | ToxWindow ret; | 90 | ToxWindow ret; |
81 | memset(&ret, 0, sizeof(ret)); | 91 | memset(&ret, 0, sizeof(ret)); |
82 | 92 | ||
83 | ret.onKey = &dhtstatus_onKey; | 93 | ret.onKey = &dhtstatus_onKey; |
84 | ret.onDraw = &dhtstatus_onDraw; | 94 | ret.onDraw = &dhtstatus_onDraw; |
85 | ret.onInit = &dhtstatus_onInit; | 95 | ret.onInit = &dhtstatus_onInit; |
86 | 96 | ||
87 | strcpy(ret.title, "[dht status]"); | 97 | strcpy(ret.title, "[dht status]"); |
88 | return ret; | 98 | return ret; |
89 | } | 99 | } |
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index e7504fbf..c85fd3ae 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c | |||
@@ -13,13 +13,13 @@ | |||
13 | #include "windows.h" | 13 | #include "windows.h" |
14 | #include "friendlist.h" | 14 | #include "friendlist.h" |
15 | 15 | ||
16 | static char * WINDOW_STATUS; | 16 | static char *WINDOW_STATUS; |
17 | 17 | ||
18 | typedef struct { | 18 | typedef struct { |
19 | uint8_t name[MAX_NAME_LENGTH]; | 19 | uint8_t name[MAX_NAME_LENGTH]; |
20 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 20 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; |
21 | int num; | 21 | int num; |
22 | int chatwin; | 22 | int chatwin; |
23 | } friend_t; | 23 | } friend_t; |
24 | 24 | ||
25 | static friend_t friends[MAX_FRIENDS_NUM]; | 25 | static friend_t friends[MAX_FRIENDS_NUM]; |
@@ -28,135 +28,143 @@ static int num_selected = 0; | |||
28 | 28 | ||
29 | void fix_name(uint8_t *name) | 29 | void fix_name(uint8_t *name) |
30 | { | 30 | { |
31 | /* Remove all non alphanumeric characters */ | 31 | /* Remove all non alphanumeric characters */ |
32 | uint8_t *p = name; | 32 | uint8_t *p = name; |
33 | uint8_t *q = name; | 33 | uint8_t *q = name; |
34 | while(*p != 0) { | 34 | |
35 | if (isprint(*p)) | 35 | while (*p != 0) { |
36 | *q++ = *p; | 36 | if (isprint(*p)) |
37 | p++; | 37 | *q++ = *p; |
38 | } | 38 | |
39 | *q = 0; | 39 | p++; |
40 | } | ||
41 | |||
42 | *q = 0; | ||
40 | } | 43 | } |
41 | 44 | ||
42 | void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len) | 45 | void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len) |
43 | { | 46 | { |
44 | if (num >= num_friends) | 47 | if (num >= num_friends) |
45 | return; | 48 | return; |
46 | 49 | ||
47 | if (friends[num].chatwin == -1) { | 50 | if (friends[num].chatwin == -1) { |
48 | friends[num].chatwin = num; | 51 | friends[num].chatwin = num; |
49 | int i; | 52 | int i; |
50 | /* Find first open slot to hold chat window */ | 53 | |
51 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | 54 | /* Find first open slot to hold chat window */ |
52 | if (WINDOW_STATUS[i] == -1) { | 55 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { |
53 | WINDOW_STATUS[i] = num; | 56 | if (WINDOW_STATUS[i] == -1) { |
54 | add_window(m, new_chat(m, num), i); | 57 | WINDOW_STATUS[i] = num; |
55 | break; | 58 | add_window(m, new_chat(m, num), i); |
56 | } | 59 | break; |
60 | } | ||
61 | } | ||
57 | } | 62 | } |
58 | } | ||
59 | } | 63 | } |
60 | 64 | ||
61 | void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) | 65 | void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) |
62 | { | 66 | { |
63 | if (len >= MAX_NAME_LENGTH || num >= num_friends) | 67 | if (len >= MAX_NAME_LENGTH || num >= num_friends) |
64 | return; | 68 | return; |
65 | 69 | ||
66 | memcpy((char*) &friends[num].name, (char*) str, len); | 70 | memcpy((char *) &friends[num].name, (char *) str, len); |
67 | friends[num].name[len] = 0; | 71 | friends[num].name[len] = 0; |
68 | fix_name(friends[num].name); | 72 | fix_name(friends[num].name); |
69 | } | 73 | } |
70 | 74 | ||
71 | void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) | 75 | void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) |
72 | { | 76 | { |
73 | if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends) | 77 | if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends) |
74 | return; | 78 | return; |
75 | 79 | ||
76 | memcpy((char*) &friends[num].status, (char*) str, len); | 80 | memcpy((char *) &friends[num].status, (char *) str, len); |
77 | friends[num].status[len] = 0; | 81 | friends[num].status[len] = 0; |
78 | fix_name(friends[num].status); | 82 | fix_name(friends[num].status); |
79 | } | 83 | } |
80 | 84 | ||
81 | int friendlist_onFriendAdded(Messenger *m, int num) | 85 | int friendlist_onFriendAdded(Messenger *m, int num) |
82 | { | 86 | { |
83 | if (num_friends == MAX_FRIENDS_NUM) | 87 | if (num_friends == MAX_FRIENDS_NUM) |
84 | return -1; | 88 | return -1; |
85 | 89 | ||
86 | friends[num_friends].num = num; | 90 | friends[num_friends].num = num; |
87 | getname(m, num, friends[num_friends].name); | 91 | getname(m, num, friends[num_friends].name); |
88 | strcpy((char*) friends[num_friends].name, "unknown"); | 92 | strcpy((char *) friends[num_friends].name, "unknown"); |
89 | strcpy((char*) friends[num_friends].status, "unknown"); | 93 | strcpy((char *) friends[num_friends].status, "unknown"); |
90 | friends[num_friends++].chatwin = -1; | 94 | friends[num_friends++].chatwin = -1; |
91 | return 0; | 95 | return 0; |
92 | } | 96 | } |
93 | 97 | ||
94 | static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) | 98 | static void friendlist_onKey(ToxWindow *self, Messenger *m, int key) |
95 | { | 99 | { |
96 | if (key == KEY_UP) { | 100 | if (key == KEY_UP) { |
97 | if (--num_selected < 0) | 101 | if (--num_selected < 0) |
98 | num_selected = num_friends-1; | 102 | num_selected = num_friends - 1; |
99 | } | 103 | } else if (key == KEY_DOWN) { |
100 | else if (key == KEY_DOWN) { | 104 | if (num_friends != 0) |
101 | if (num_friends != 0) | 105 | num_selected = (num_selected + 1) % num_friends; |
102 | num_selected = (num_selected+1) % num_friends; | 106 | } else if (key == '\n') { |
103 | } | 107 | /* Jump to chat window if already open */ |
104 | else if (key == '\n') { | 108 | if (friends[num_selected].chatwin != -1) { |
105 | /* Jump to chat window if already open */ | 109 | int i; |
106 | if (friends[num_selected].chatwin != -1) { | 110 | |
107 | int i; | 111 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { |
108 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | 112 | if (WINDOW_STATUS[i] == num_selected) { |
109 | if (WINDOW_STATUS[i] == num_selected) { | 113 | set_active_window(i); |
110 | set_active_window(i); | 114 | break; |
111 | break; | 115 | } |
112 | } | 116 | } |
113 | } | 117 | } else { |
114 | }else { | 118 | int i; |
115 | int i; | 119 | |
116 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | 120 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { |
117 | if (WINDOW_STATUS[i] == -1) { | 121 | if (WINDOW_STATUS[i] == -1) { |
118 | WINDOW_STATUS[i] = num_selected; | 122 | WINDOW_STATUS[i] = num_selected; |
119 | friends[num_selected].chatwin = num_selected; | 123 | friends[num_selected].chatwin = num_selected; |
120 | add_window(m, new_chat(m, num_selected), i); | 124 | add_window(m, new_chat(m, num_selected), i); |
121 | break; | 125 | break; |
126 | } | ||
127 | } | ||
122 | } | 128 | } |
123 | } | ||
124 | } | 129 | } |
125 | } | ||
126 | } | 130 | } |
127 | 131 | ||
128 | static void friendlist_onDraw(ToxWindow *self) | 132 | static void friendlist_onDraw(ToxWindow *self) |
129 | { | 133 | { |
130 | curs_set(0); | 134 | curs_set(0); |
131 | werase(self->window); | 135 | werase(self->window); |
132 | if (num_friends == 0) { | 136 | |
133 | wprintw(self->window, "Empty. Add some friends! :-)\n"); | 137 | if (num_friends == 0) { |
134 | } | 138 | wprintw(self->window, "Empty. Add some friends! :-)\n"); |
135 | else { | 139 | } else { |
136 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); | 140 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); |
137 | wprintw(self->window, "Open chat with.. (up/down keys, enter)\n"); | 141 | wprintw(self->window, "Open chat with.. (up/down keys, enter)\n"); |
138 | wattroff(self->window, COLOR_PAIR(2) | A_BOLD); | 142 | wattroff(self->window, COLOR_PAIR(2) | A_BOLD); |
139 | } | 143 | } |
140 | 144 | ||
141 | wprintw(self->window, "\n"); | 145 | wprintw(self->window, "\n"); |
142 | int i; | 146 | int i; |
143 | for (i = 0; i < num_friends; ++i) { | 147 | |
144 | if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); | 148 | for (i = 0; i < num_friends; ++i) { |
145 | wprintw(self->window, " [#%d] ", friends[i].num); | 149 | if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); |
146 | if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); | 150 | |
147 | 151 | wprintw(self->window, " [#%d] ", friends[i].num); | |
148 | attron(A_BOLD); | 152 | |
149 | wprintw(self->window, "%s ", friends[i].name); | 153 | if (i == num_selected) wattroff(self->window, COLOR_PAIR(3)); |
150 | attroff(A_BOLD); | 154 | |
151 | 155 | attron(A_BOLD); | |
152 | wprintw(self->window, "(%s)\n", friends[i].status); | 156 | wprintw(self->window, "%s ", friends[i].name); |
153 | } | 157 | attroff(A_BOLD); |
154 | wrefresh(self->window); | 158 | |
159 | wprintw(self->window, "(%s)\n", friends[i].status); | ||
160 | } | ||
161 | |||
162 | wrefresh(self->window); | ||
155 | } | 163 | } |
156 | 164 | ||
157 | void disable_chatwin(int f_num) | 165 | void disable_chatwin(int f_num) |
158 | { | 166 | { |
159 | friends[f_num].chatwin = -1; | 167 | friends[f_num].chatwin = -1; |
160 | } | 168 | } |
161 | 169 | ||
162 | static void friendlist_onInit(ToxWindow *self, Messenger *m) | 170 | static void friendlist_onInit(ToxWindow *self, Messenger *m) |
@@ -164,19 +172,20 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m) | |||
164 | 172 | ||
165 | } | 173 | } |
166 | 174 | ||
167 | ToxWindow new_friendlist(char * ws) { | 175 | ToxWindow new_friendlist(char *ws) |
168 | WINDOW_STATUS = ws; | 176 | { |
169 | ToxWindow ret; | 177 | WINDOW_STATUS = ws; |
170 | memset(&ret, 0, sizeof(ret)); | 178 | ToxWindow ret; |
171 | 179 | memset(&ret, 0, sizeof(ret)); | |
172 | ret.onKey = &friendlist_onKey; | 180 | |
173 | ret.onDraw = &friendlist_onDraw; | 181 | ret.onKey = &friendlist_onKey; |
174 | ret.onInit = &friendlist_onInit; | 182 | ret.onDraw = &friendlist_onDraw; |
175 | ret.onMessage = &friendlist_onMessage; | 183 | ret.onInit = &friendlist_onInit; |
176 | ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message | 184 | ret.onMessage = &friendlist_onMessage; |
177 | ret.onNickChange = &friendlist_onNickChange; | 185 | ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message |
178 | ret.onStatusChange = &friendlist_onStatusChange; | 186 | ret.onNickChange = &friendlist_onNickChange; |
179 | 187 | ret.onStatusChange = &friendlist_onStatusChange; | |
180 | strcpy(ret.title, "[friends]"); | 188 | |
181 | return ret; | 189 | strcpy(ret.title, "[friends]"); |
190 | return ret; | ||
182 | } | 191 | } |
diff --git a/testing/toxic/friendlist.h b/testing/toxic/friendlist.h index c3d8dac7..f0c27f53 100644 --- a/testing/toxic/friendlist.h +++ b/testing/toxic/friendlist.h | |||
@@ -4,7 +4,7 @@ | |||
4 | #include "windows.h" | 4 | #include "windows.h" |
5 | #include "chat.h" | 5 | #include "chat.h" |
6 | 6 | ||
7 | ToxWindow new_friendlist(char * ws); | 7 | ToxWindow new_friendlist(char *ws); |
8 | int friendlist_onFriendAdded(Messenger *m, int num); | 8 | int friendlist_onFriendAdded(Messenger *m, int num); |
9 | void disable_chatwin(int f_num); | 9 | void disable_chatwin(int f_num); |
10 | void fix_name(uint8_t *name); | 10 | void fix_name(uint8_t *name); |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 38c20df6..816873b3 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -28,7 +28,7 @@ | |||
28 | /* Export for use in Callbacks */ | 28 | /* Export for use in Callbacks */ |
29 | char *DATA_FILE = NULL; | 29 | char *DATA_FILE = NULL; |
30 | 30 | ||
31 | void on_window_resize(int sig) | 31 | void on_window_resize(int sig) |
32 | { | 32 | { |
33 | endwin(); | 33 | endwin(); |
34 | refresh(); | 34 | refresh(); |
@@ -37,44 +37,45 @@ void on_window_resize(int sig) | |||
37 | 37 | ||
38 | static void init_term() | 38 | static void init_term() |
39 | { | 39 | { |
40 | /* Setup terminal */ | 40 | /* Setup terminal */ |
41 | signal(SIGWINCH, on_window_resize); | 41 | signal(SIGWINCH, on_window_resize); |
42 | initscr(); | 42 | initscr(); |
43 | cbreak(); | 43 | cbreak(); |
44 | keypad(stdscr, 1); | 44 | keypad(stdscr, 1); |
45 | noecho(); | 45 | noecho(); |
46 | timeout(100); | 46 | timeout(100); |
47 | 47 | ||
48 | if (has_colors()) { | 48 | if (has_colors()) { |
49 | start_color(); | 49 | start_color(); |
50 | init_pair(1, COLOR_GREEN, COLOR_BLACK); | 50 | init_pair(1, COLOR_GREEN, COLOR_BLACK); |
51 | init_pair(2, COLOR_CYAN, COLOR_BLACK); | 51 | init_pair(2, COLOR_CYAN, COLOR_BLACK); |
52 | init_pair(3, COLOR_RED, COLOR_BLACK); | 52 | init_pair(3, COLOR_RED, COLOR_BLACK); |
53 | init_pair(4, COLOR_BLUE, COLOR_BLACK); | 53 | init_pair(4, COLOR_BLUE, COLOR_BLACK); |
54 | init_pair(5, COLOR_YELLOW, COLOR_BLACK); | 54 | init_pair(5, COLOR_YELLOW, COLOR_BLACK); |
55 | } | 55 | } |
56 | refresh(); | 56 | |
57 | refresh(); | ||
57 | } | 58 | } |
58 | 59 | ||
59 | static Messenger *init_tox() | 60 | static Messenger *init_tox() |
60 | { | 61 | { |
61 | /* Init core */ | 62 | /* Init core */ |
62 | Messenger *m = initMessenger(); | 63 | Messenger *m = initMessenger(); |
63 | 64 | ||
64 | /* Callbacks */ | 65 | /* Callbacks */ |
65 | m_callback_friendrequest(m, on_request, NULL); | 66 | m_callback_friendrequest(m, on_request, NULL); |
66 | m_callback_friendmessage(m, on_message, NULL); | 67 | m_callback_friendmessage(m, on_message, NULL); |
67 | m_callback_namechange(m, on_nickchange, NULL); | 68 | m_callback_namechange(m, on_nickchange, NULL); |
68 | m_callback_statusmessage(m, on_statuschange, NULL); | 69 | m_callback_statusmessage(m, on_statuschange, NULL); |
69 | m_callback_action(m, on_action, NULL); | 70 | m_callback_action(m, on_action, NULL); |
70 | #ifdef __linux__ | 71 | #ifdef __linux__ |
71 | setname(m, (uint8_t*) "Cool guy", sizeof("Cool guy")); | 72 | setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); |
72 | #elif WIN32 | 73 | #elif WIN32 |
73 | setname(m, (uint8_t*) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); | 74 | setname(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); |
74 | #else | 75 | #else |
75 | setname(m, (uint8_t*) "Hipster", sizeof("Hipster")); | 76 | setname(m, (uint8_t *) "Hipster", sizeof("Hipster")); |
76 | #endif | 77 | #endif |
77 | return m; | 78 | return m; |
78 | } | 79 | } |
79 | 80 | ||
80 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ | 81 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ |
@@ -84,67 +85,75 @@ static Messenger *init_tox() | |||
84 | /* Connects to a random DHT server listed in the DHTservers file */ | 85 | /* Connects to a random DHT server listed in the DHTservers file */ |
85 | int init_connection(void) | 86 | int init_connection(void) |
86 | { | 87 | { |
87 | if (DHT_isconnected()) | 88 | if (DHT_isconnected()) |
88 | return 0; | 89 | return 0; |
90 | |||
91 | FILE *fp = fopen("../../../other/DHTservers", "r"); | ||
92 | |||
93 | if (!fp) | ||
94 | return 1; | ||
95 | |||
96 | char servers[MAXSERVERS][MAXLINE]; | ||
97 | char line[MAXLINE]; | ||
98 | int linecnt = 0; | ||
99 | |||
100 | while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { | ||
101 | if (strlen(line) > MINLINE) | ||
102 | strcpy(servers[linecnt++], line); | ||
103 | } | ||
104 | |||
105 | if (linecnt < 1) { | ||
106 | fclose(fp); | ||
107 | return 2; | ||
108 | } | ||
89 | 109 | ||
90 | FILE *fp = fopen("../../../other/DHTservers", "r"); | ||
91 | if (!fp) | ||
92 | return 1; | ||
93 | |||
94 | char servers[MAXSERVERS][MAXLINE]; | ||
95 | char line[MAXLINE]; | ||
96 | int linecnt = 0; | ||
97 | while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { | ||
98 | if (strlen(line) > MINLINE) | ||
99 | strcpy(servers[linecnt++], line); | ||
100 | } | ||
101 | if (linecnt < 1) { | ||
102 | fclose(fp); | 110 | fclose(fp); |
103 | return 2; | 111 | |
104 | } | 112 | char *server = servers[rand() % linecnt]; |
105 | fclose(fp); | 113 | char *ip = strtok(server, " "); |
106 | 114 | char *port = strtok(NULL, " "); | |
107 | char *server = servers[rand() % linecnt]; | 115 | char *key = strtok(NULL, " "); |
108 | char *ip = strtok(server, " "); | 116 | |
109 | char *port = strtok(NULL, " "); | 117 | if (!ip || !port || !key) |
110 | char *key = strtok(NULL, " "); | 118 | return 3; |
111 | if (!ip || !port || !key) | 119 | |
112 | return 3; | 120 | IP_Port dht; |
113 | 121 | dht.port = htons(atoi(port)); | |
114 | IP_Port dht; | 122 | uint32_t resolved_address = resolve_addr(ip); |
115 | dht.port = htons(atoi(port)); | 123 | |
116 | uint32_t resolved_address = resolve_addr(ip); | 124 | if (resolved_address == 0) |
117 | if (resolved_address == 0) | 125 | return 0; |
126 | |||
127 | dht.ip.i = resolved_address; | ||
128 | unsigned char *binary_string = hex_string_to_bin(key); | ||
129 | DHT_bootstrap(dht, binary_string); | ||
130 | free(binary_string); | ||
118 | return 0; | 131 | return 0; |
119 | dht.ip.i = resolved_address; | ||
120 | unsigned char *binary_string = hex_string_to_bin(key); | ||
121 | DHT_bootstrap(dht, binary_string); | ||
122 | free(binary_string); | ||
123 | return 0; | ||
124 | } | 132 | } |
125 | 133 | ||
126 | static void do_tox(Messenger *m, ToxWindow * prompt) | 134 | static void do_tox(Messenger *m, ToxWindow *prompt) |
127 | { | 135 | { |
128 | static int conn_try = 0; | 136 | static int conn_try = 0; |
129 | static int conn_err = 0; | 137 | static int conn_err = 0; |
130 | static bool dht_on = false; | 138 | static bool dht_on = false; |
131 | if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { | 139 | |
132 | if (!conn_err) { | 140 | if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { |
133 | conn_err = init_connection(); | 141 | if (!conn_err) { |
134 | wprintw(prompt->window, "\nEstablishing connection...\n"); | 142 | conn_err = init_connection(); |
135 | if (conn_err) | 143 | wprintw(prompt->window, "\nEstablishing connection...\n"); |
136 | wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); | 144 | |
145 | if (conn_err) | ||
146 | wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); | ||
147 | } | ||
148 | } else if (!dht_on && DHT_isconnected()) { | ||
149 | dht_on = true; | ||
150 | wprintw(prompt->window, "\nDHT connected.\n"); | ||
151 | } else if (dht_on && !DHT_isconnected()) { | ||
152 | dht_on = false; | ||
153 | wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); | ||
137 | } | 154 | } |
138 | } | 155 | |
139 | else if (!dht_on && DHT_isconnected()) { | 156 | doMessenger(m); |
140 | dht_on = true; | ||
141 | wprintw(prompt->window, "\nDHT connected.\n"); | ||
142 | } | ||
143 | else if (dht_on && !DHT_isconnected()) { | ||
144 | dht_on = false; | ||
145 | wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); | ||
146 | } | ||
147 | doMessenger(m); | ||
148 | } | 157 | } |
149 | 158 | ||
150 | int f_loadfromfile; | 159 | int f_loadfromfile; |
@@ -160,18 +169,22 @@ int store_data(Messenger *m, char *path) | |||
160 | { | 169 | { |
161 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ | 170 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ |
162 | return 0; | 171 | return 0; |
172 | |||
163 | FILE *fd; | 173 | FILE *fd; |
164 | size_t len; | 174 | size_t len; |
165 | uint8_t *buf; | 175 | uint8_t *buf; |
166 | 176 | ||
167 | len = Messenger_size(m); | 177 | len = Messenger_size(m); |
168 | buf = malloc(len); | 178 | buf = malloc(len); |
179 | |||
169 | if (buf == NULL) { | 180 | if (buf == NULL) { |
170 | return 1; | 181 | return 1; |
171 | } | 182 | } |
183 | |||
172 | Messenger_save(m, buf); | 184 | Messenger_save(m, buf); |
173 | 185 | ||
174 | fd = fopen(path, "w"); | 186 | fd = fopen(path, "w"); |
187 | |||
175 | if (fd == NULL) { | 188 | if (fd == NULL) { |
176 | free(buf); | 189 | free(buf); |
177 | return 2; | 190 | return 2; |
@@ -192,6 +205,7 @@ static void load_data(Messenger *m, char *path) | |||
192 | { | 205 | { |
193 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ | 206 | if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ |
194 | return; | 207 | return; |
208 | |||
195 | FILE *fd; | 209 | FILE *fd; |
196 | size_t len; | 210 | size_t len; |
197 | uint8_t *buf; | 211 | uint8_t *buf; |
@@ -202,12 +216,14 @@ static void load_data(Messenger *m, char *path) | |||
202 | fseek(fd, 0, SEEK_SET); | 216 | fseek(fd, 0, SEEK_SET); |
203 | 217 | ||
204 | buf = malloc(len); | 218 | buf = malloc(len); |
219 | |||
205 | if (buf == NULL) { | 220 | if (buf == NULL) { |
206 | fprintf(stderr, "malloc() failed.\n"); | 221 | fprintf(stderr, "malloc() failed.\n"); |
207 | fclose(fd); | 222 | fclose(fd); |
208 | endwin(); | 223 | endwin(); |
209 | exit(1); | 224 | exit(1); |
210 | } | 225 | } |
226 | |||
211 | if (fread(buf, len, 1, fd) != 1) { | 227 | if (fread(buf, len, 1, fd) != 1) { |
212 | fprintf(stderr, "fread() failed.\n"); | 228 | fprintf(stderr, "fread() failed.\n"); |
213 | free(buf); | 229 | free(buf); |
@@ -215,9 +231,11 @@ static void load_data(Messenger *m, char *path) | |||
215 | endwin(); | 231 | endwin(); |
216 | exit(1); | 232 | exit(1); |
217 | } | 233 | } |
234 | |||
218 | Messenger_load(m, buf, len); | 235 | Messenger_load(m, buf, len); |
219 | 236 | ||
220 | uint32_t i; | 237 | uint32_t i; |
238 | |||
221 | for (i = 0; i < m->numfriends; i++) { | 239 | for (i = 0; i < m->numfriends; i++) { |
222 | on_friendadded(m, i); | 240 | on_friendadded(m, i); |
223 | } | 241 | } |
@@ -226,6 +244,7 @@ static void load_data(Messenger *m, char *path) | |||
226 | fclose(fd); | 244 | fclose(fd); |
227 | } else { | 245 | } else { |
228 | int st; | 246 | int st; |
247 | |||
229 | if ((st = store_data(m, path)) != 0) { | 248 | if ((st = store_data(m, path)) != 0) { |
230 | fprintf(stderr, "Store messenger failed with return code: %d\n", st); | 249 | fprintf(stderr, "Store messenger failed with return code: %d\n", st); |
231 | endwin(); | 250 | endwin(); |
@@ -236,70 +255,74 @@ static void load_data(Messenger *m, char *path) | |||
236 | 255 | ||
237 | int main(int argc, char *argv[]) | 256 | int main(int argc, char *argv[]) |
238 | { | 257 | { |
239 | char *user_config_dir = get_user_config_dir(); | 258 | char *user_config_dir = get_user_config_dir(); |
240 | int config_err = 0; | 259 | int config_err = 0; |
241 | 260 | ||
242 | f_loadfromfile = 1; | 261 | f_loadfromfile = 1; |
243 | int f_flag = 0; | 262 | int f_flag = 0; |
244 | int i = 0; | 263 | int i = 0; |
245 | for (i = 0; i < argc; ++i) { | 264 | |
246 | if (argv[i] == NULL) | 265 | for (i = 0; i < argc; ++i) { |
247 | break; | 266 | if (argv[i] == NULL) |
248 | else if (argv[i][0] == '-') { | 267 | break; |
249 | if (argv[i][1] == 'f') { | 268 | else if (argv[i][0] == '-') { |
250 | if (argv[i + 1] != NULL) | 269 | if (argv[i][1] == 'f') { |
251 | DATA_FILE = strdup(argv[i + 1]); | 270 | if (argv[i + 1] != NULL) |
252 | else | 271 | DATA_FILE = strdup(argv[i + 1]); |
253 | f_flag = -1; | 272 | else |
254 | } else if (argv[i][1] == 'n') { | 273 | f_flag = -1; |
255 | f_loadfromfile = 0; | 274 | } else if (argv[i][1] == 'n') { |
256 | } | 275 | f_loadfromfile = 0; |
276 | } | ||
277 | } | ||
278 | } | ||
279 | |||
280 | if (DATA_FILE == NULL ) { | ||
281 | config_err = create_user_config_dir(user_config_dir); | ||
282 | |||
283 | if (config_err) { | ||
284 | DATA_FILE = strdup("data"); | ||
285 | } else { | ||
286 | DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); | ||
287 | strcpy(DATA_FILE, user_config_dir); | ||
288 | strcat(DATA_FILE, CONFIGDIR); | ||
289 | strcat(DATA_FILE, "data"); | ||
290 | } | ||
291 | } | ||
292 | |||
293 | free(user_config_dir); | ||
294 | |||
295 | init_term(); | ||
296 | Messenger *m = init_tox(); | ||
297 | ToxWindow *prompt = init_windows(m); | ||
298 | init_window_status(); | ||
299 | |||
300 | if (f_loadfromfile) | ||
301 | load_data(m, DATA_FILE); | ||
302 | |||
303 | if (f_flag == -1) { | ||
304 | attron(COLOR_PAIR(3) | A_BOLD); | ||
305 | wprintw(prompt->window, "You passed '-f' without giving an argument.\n" | ||
306 | "defaulting to 'data' for a keyfile...\n"); | ||
307 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
257 | } | 308 | } |
258 | } | ||
259 | 309 | ||
260 | if (DATA_FILE == NULL ) { | ||
261 | config_err = create_user_config_dir(user_config_dir); | ||
262 | if (config_err) { | 310 | if (config_err) { |
263 | DATA_FILE = strdup("data"); | 311 | attron(COLOR_PAIR(3) | A_BOLD); |
264 | } else { | 312 | wprintw(prompt->window, "Unable to determine configuration directory.\n" |
265 | DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); | 313 | "defaulting to 'data' for a keyfile...\n"); |
266 | strcpy(DATA_FILE, user_config_dir); | 314 | attroff(COLOR_PAIR(3) | A_BOLD); |
267 | strcat(DATA_FILE, CONFIGDIR); | 315 | } |
268 | strcat(DATA_FILE, "data"); | 316 | |
317 | while (true) { | ||
318 | /* Update tox */ | ||
319 | do_tox(m, prompt); | ||
320 | |||
321 | /* Draw */ | ||
322 | draw_active_window(m); | ||
269 | } | 323 | } |
270 | } | 324 | |
271 | free(user_config_dir); | 325 | cleanupMessenger(m); |
272 | 326 | free(DATA_FILE); | |
273 | init_term(); | 327 | return 0; |
274 | Messenger *m = init_tox(); | ||
275 | ToxWindow *prompt = init_windows(m); | ||
276 | init_window_status(); | ||
277 | |||
278 | if(f_loadfromfile) | ||
279 | load_data(m, DATA_FILE); | ||
280 | |||
281 | if (f_flag == -1) { | ||
282 | attron(COLOR_PAIR(3) | A_BOLD); | ||
283 | wprintw(prompt->window, "You passed '-f' without giving an argument.\n" | ||
284 | "defaulting to 'data' for a keyfile...\n"); | ||
285 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
286 | } | ||
287 | |||
288 | if(config_err) { | ||
289 | attron(COLOR_PAIR(3) | A_BOLD); | ||
290 | wprintw(prompt->window, "Unable to determine configuration directory.\n" | ||
291 | "defaulting to 'data' for a keyfile...\n"); | ||
292 | attroff(COLOR_PAIR(3) | A_BOLD); | ||
293 | } | ||
294 | while(true) { | ||
295 | /* Update tox */ | ||
296 | do_tox(m, prompt); | ||
297 | |||
298 | /* Draw */ | ||
299 | draw_active_window(m); | ||
300 | } | ||
301 | |||
302 | cleanupMessenger(m); | ||
303 | free(DATA_FILE); | ||
304 | return 0; | ||
305 | } | 328 | } |
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 1d0e0144..f6d3e21f 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -14,7 +14,7 @@ | |||
14 | #include "prompt.h" | 14 | #include "prompt.h" |
15 | 15 | ||
16 | uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX | 16 | uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX |
17 | uint8_t num_requests=0; // XXX | 17 | uint8_t num_requests = 0; // XXX |
18 | 18 | ||
19 | static friendAddedFn *on_friendadded_cb; | 19 | static friendAddedFn *on_friendadded_cb; |
20 | static char prompt_buf[MAX_STR_SIZE] = {0}; | 20 | static char prompt_buf[MAX_STR_SIZE] = {0}; |
@@ -36,412 +36,441 @@ void cmd_statusmsg(ToxWindow *, Messenger *m, char **); | |||
36 | #define NUM_COMMANDS 13 | 36 | #define NUM_COMMANDS 13 |
37 | 37 | ||
38 | static struct { | 38 | static struct { |
39 | char *name; | 39 | char *name; |
40 | int numargs; | 40 | int numargs; |
41 | void (*func)(ToxWindow *, Messenger *m, char **); | 41 | void (*func)(ToxWindow *, Messenger *m, char **); |
42 | } commands[] = { | 42 | } commands[] = { |
43 | { "accept", 1, cmd_accept }, | 43 | { "accept", 1, cmd_accept }, |
44 | { "add", 1, cmd_add }, | 44 | { "add", 1, cmd_add }, |
45 | { "clear", 0, cmd_clear }, | 45 | { "clear", 0, cmd_clear }, |
46 | { "connect", 3, cmd_connect }, | 46 | { "connect", 3, cmd_connect }, |
47 | { "exit", 0, cmd_quit }, | 47 | { "exit", 0, cmd_quit }, |
48 | { "help", 0, cmd_help }, | 48 | { "help", 0, cmd_help }, |
49 | { "msg", 2, cmd_msg }, | 49 | { "msg", 2, cmd_msg }, |
50 | { "myid", 0, cmd_myid }, | 50 | { "myid", 0, cmd_myid }, |
51 | { "nick", 1, cmd_nick }, | 51 | { "nick", 1, cmd_nick }, |
52 | { "q", 0, cmd_quit }, | 52 | { "q", 0, cmd_quit }, |
53 | { "quit", 0, cmd_quit }, | 53 | { "quit", 0, cmd_quit }, |
54 | { "status", 2, cmd_status }, | 54 | { "status", 2, cmd_status }, |
55 | { "statusmsg", 1, cmd_statusmsg }, | 55 | { "statusmsg", 1, cmd_statusmsg }, |
56 | }; | 56 | }; |
57 | 57 | ||
58 | // XXX: | 58 | // XXX: |
59 | int add_req(uint8_t *public_key) | 59 | int add_req(uint8_t *public_key) |
60 | { | 60 | { |
61 | memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); | 61 | memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); |
62 | ++num_requests; | 62 | ++num_requests; |
63 | return num_requests-1; | 63 | return num_requests - 1; |
64 | } | 64 | } |
65 | 65 | ||
66 | // XXX: FIX | 66 | // XXX: FIX |
67 | unsigned char *hex_string_to_bin(char hex_string[]) | 67 | unsigned char *hex_string_to_bin(char hex_string[]) |
68 | { | 68 | { |
69 | size_t len = strlen(hex_string); | 69 | size_t len = strlen(hex_string); |
70 | unsigned char *val = malloc(len); | 70 | unsigned char *val = malloc(len); |
71 | char *pos = hex_string; | 71 | char *pos = hex_string; |
72 | int i; | 72 | int i; |
73 | for (i = 0; i < len; ++i, pos+=2) | 73 | |
74 | sscanf(pos,"%2hhx",&val[i]); | 74 | for (i = 0; i < len; ++i, pos += 2) |
75 | return val; | 75 | sscanf(pos, "%2hhx", &val[i]); |
76 | |||
77 | return val; | ||
76 | } | 78 | } |
77 | 79 | ||
78 | void cmd_accept(ToxWindow *self, Messenger *m, char **args) | 80 | void cmd_accept(ToxWindow *self, Messenger *m, char **args) |
79 | { | 81 | { |
80 | int num = atoi(args[1]); | 82 | int num = atoi(args[1]); |
81 | if (num >= num_requests) { | 83 | |
82 | wprintw(self->window, "Invalid syntax.\n"); | 84 | if (num >= num_requests) { |
83 | return; | 85 | wprintw(self->window, "Invalid syntax.\n"); |
84 | } | 86 | return; |
85 | 87 | } | |
86 | num = m_addfriend_norequest(m, pending_requests[num]); | 88 | |
87 | if (num == -1) | 89 | num = m_addfriend_norequest(m, pending_requests[num]); |
88 | wprintw(self->window, "Failed to add friend.\n"); | 90 | |
89 | else { | 91 | if (num == -1) |
90 | wprintw(self->window, "Friend accepted as: %d.\n", num); | 92 | wprintw(self->window, "Failed to add friend.\n"); |
91 | on_friendadded_cb(m, num); | 93 | else { |
92 | } | 94 | wprintw(self->window, "Friend accepted as: %d.\n", num); |
95 | on_friendadded_cb(m, num); | ||
96 | } | ||
93 | } | 97 | } |
94 | 98 | ||
95 | void cmd_add(ToxWindow *self, Messenger *m, char **args) | 99 | void cmd_add(ToxWindow *self, Messenger *m, char **args) |
96 | { | 100 | { |
97 | uint8_t id_bin[FRIEND_ADDRESS_SIZE]; | 101 | uint8_t id_bin[FRIEND_ADDRESS_SIZE]; |
98 | char xx[3]; | 102 | char xx[3]; |
99 | uint32_t x; | 103 | uint32_t x; |
100 | char *id = args[1]; | 104 | char *id = args[1]; |
101 | char *msg = args[2]; | 105 | char *msg = args[2]; |
102 | 106 | ||
103 | if (!id) { | 107 | if (!id) { |
104 | wprintw(self->window, "Invalid command: add expected at least one argument.\n"); | 108 | wprintw(self->window, "Invalid command: add expected at least one argument.\n"); |
105 | return; | 109 | return; |
106 | } | 110 | } |
107 | if (!msg) | 111 | |
108 | msg = ""; | 112 | if (!msg) |
109 | 113 | msg = ""; | |
110 | if (strlen(id) != 2*FRIEND_ADDRESS_SIZE) { | 114 | |
111 | wprintw(self->window, "Invalid ID length.\n"); | 115 | if (strlen(id) != 2 * FRIEND_ADDRESS_SIZE) { |
112 | return; | 116 | wprintw(self->window, "Invalid ID length.\n"); |
113 | } | 117 | return; |
114 | int i; | 118 | } |
115 | for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { | 119 | |
116 | xx[0] = id[2*i]; | 120 | int i; |
117 | xx[1] = id[2*i+1]; | 121 | |
118 | xx[2] = '\0'; | 122 | for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { |
119 | if (sscanf(xx, "%02x", &x) != 1) { | 123 | xx[0] = id[2 * i]; |
120 | wprintw(self->window, "Invalid ID.\n"); | 124 | xx[1] = id[2 * i + 1]; |
121 | return; | 125 | xx[2] = '\0'; |
126 | |||
127 | if (sscanf(xx, "%02x", &x) != 1) { | ||
128 | wprintw(self->window, "Invalid ID.\n"); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | id_bin[i] = x; | ||
133 | } | ||
134 | |||
135 | for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { | ||
136 | id[i] = toupper(id[i]); | ||
137 | } | ||
138 | |||
139 | int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); | ||
140 | |||
141 | switch (num) { | ||
142 | case FAERR_TOOLONG: | ||
143 | wprintw(self->window, "Message is too long.\n"); | ||
144 | break; | ||
145 | |||
146 | case FAERR_NOMESSAGE: | ||
147 | wprintw(self->window, "Please add a message to your request.\n"); | ||
148 | break; | ||
149 | |||
150 | case FAERR_OWNKEY: | ||
151 | wprintw(self->window, "That appears to be your own ID.\n"); | ||
152 | break; | ||
153 | |||
154 | case FAERR_ALREADYSENT: | ||
155 | wprintw(self->window, "Friend request already sent.\n"); | ||
156 | break; | ||
157 | |||
158 | case FAERR_UNKNOWN: | ||
159 | wprintw(self->window, "Undefined error when adding friend.\n"); | ||
160 | break; | ||
161 | |||
162 | case FAERR_BADCHECKSUM: | ||
163 | wprintw(self->window, "Bad checksum in address.\n"); | ||
164 | break; | ||
165 | |||
166 | case FAERR_SETNEWNOSPAM: | ||
167 | wprintw(self->window, "Nospam was different.\n"); | ||
168 | break; | ||
169 | |||
170 | default: | ||
171 | wprintw(self->window, "Friend added as %d.\n", num); | ||
172 | on_friendadded_cb(m, num); | ||
173 | break; | ||
122 | } | 174 | } |
123 | id_bin[i] = x; | ||
124 | } | ||
125 | |||
126 | for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { | ||
127 | id[i] = toupper(id[i]); | ||
128 | } | ||
129 | |||
130 | int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1); | ||
131 | switch (num) { | ||
132 | case FAERR_TOOLONG: | ||
133 | wprintw(self->window, "Message is too long.\n"); | ||
134 | break; | ||
135 | case FAERR_NOMESSAGE: | ||
136 | wprintw(self->window, "Please add a message to your request.\n"); | ||
137 | break; | ||
138 | case FAERR_OWNKEY: | ||
139 | wprintw(self->window, "That appears to be your own ID.\n"); | ||
140 | break; | ||
141 | case FAERR_ALREADYSENT: | ||
142 | wprintw(self->window, "Friend request already sent.\n"); | ||
143 | break; | ||
144 | case FAERR_UNKNOWN: | ||
145 | wprintw(self->window, "Undefined error when adding friend.\n"); | ||
146 | break; | ||
147 | case FAERR_BADCHECKSUM: | ||
148 | wprintw(self->window, "Bad checksum in address.\n"); | ||
149 | break; | ||
150 | case FAERR_SETNEWNOSPAM: | ||
151 | wprintw(self->window, "Nospam was different.\n"); | ||
152 | break; | ||
153 | default: | ||
154 | wprintw(self->window, "Friend added as %d.\n", num); | ||
155 | on_friendadded_cb(m, num); | ||
156 | break; | ||
157 | } | ||
158 | } | 175 | } |
159 | 176 | ||
160 | void cmd_clear(ToxWindow *self, Messenger *m, char **args) | 177 | void cmd_clear(ToxWindow *self, Messenger *m, char **args) |
161 | { | 178 | { |
162 | wclear(self->window); | 179 | wclear(self->window); |
163 | } | 180 | } |
164 | 181 | ||
165 | void cmd_connect(ToxWindow *self, Messenger *m, char **args) | 182 | void cmd_connect(ToxWindow *self, Messenger *m, char **args) |
166 | { | 183 | { |
167 | IP_Port dht; | 184 | IP_Port dht; |
168 | char *ip = args[1]; | 185 | char *ip = args[1]; |
169 | char *port = args[2]; | 186 | char *port = args[2]; |
170 | char *key = args[3]; | 187 | char *key = args[3]; |
171 | 188 | ||
172 | if (atoi(port) == 0) { | 189 | if (atoi(port) == 0) { |
173 | wprintw(self->window, "Invalid syntax.\n"); | 190 | wprintw(self->window, "Invalid syntax.\n"); |
174 | return; | 191 | return; |
175 | } | 192 | } |
176 | 193 | ||
177 | dht.port = htons(atoi(port)); | 194 | dht.port = htons(atoi(port)); |
178 | uint32_t resolved_address = resolve_addr(ip); | 195 | uint32_t resolved_address = resolve_addr(ip); |
179 | if (resolved_address == 0) { | 196 | |
180 | return; | 197 | if (resolved_address == 0) { |
181 | } | 198 | return; |
182 | 199 | } | |
183 | dht.ip.i = resolved_address; | 200 | |
184 | unsigned char *binary_string = hex_string_to_bin(key); | 201 | dht.ip.i = resolved_address; |
185 | DHT_bootstrap(dht, binary_string); | 202 | unsigned char *binary_string = hex_string_to_bin(key); |
186 | free(binary_string); | 203 | DHT_bootstrap(dht, binary_string); |
204 | free(binary_string); | ||
187 | } | 205 | } |
188 | 206 | ||
189 | void cmd_quit(ToxWindow *self, Messenger *m, char **args) | 207 | void cmd_quit(ToxWindow *self, Messenger *m, char **args) |
190 | { | 208 | { |
191 | endwin(); | 209 | endwin(); |
192 | exit(0); | 210 | exit(0); |
193 | } | 211 | } |
194 | 212 | ||
195 | void cmd_help(ToxWindow *self, Messenger *m, char **args) | 213 | void cmd_help(ToxWindow *self, Messenger *m, char **args) |
196 | { | 214 | { |
197 | wclear(self->window); | 215 | wclear(self->window); |
198 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); | 216 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); |
199 | wprintw(self->window, "Commands:\n"); | 217 | wprintw(self->window, "Commands:\n"); |
200 | wattroff(self->window, A_BOLD); | 218 | wattroff(self->window, A_BOLD); |
201 | 219 | ||
202 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); | 220 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); |
203 | wprintw(self->window, " add <id> <message> : Add friend\n"); | 221 | wprintw(self->window, " add <id> <message> : Add friend\n"); |
204 | wprintw(self->window, " status <type> <message> : Set your status\n"); | 222 | wprintw(self->window, " status <type> <message> : Set your status\n"); |
205 | wprintw(self->window, " statusmsg <message> : Set your status\n"); | 223 | wprintw(self->window, " statusmsg <message> : Set your status\n"); |
206 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); | 224 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); |
207 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 225 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
208 | wprintw(self->window, " myid : Print your ID\n"); | 226 | wprintw(self->window, " myid : Print your ID\n"); |
209 | wprintw(self->window, " quit/exit : Exit program\n"); | 227 | wprintw(self->window, " quit/exit : Exit program\n"); |
210 | wprintw(self->window, " help : Print this message again\n"); | 228 | wprintw(self->window, " help : Print this message again\n"); |
211 | wprintw(self->window, " clear : Clear this window\n"); | 229 | wprintw(self->window, " clear : Clear this window\n"); |
212 | 230 | ||
213 | wattron(self->window, A_BOLD); | 231 | wattron(self->window, A_BOLD); |
214 | wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); | 232 | wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); |
215 | wattroff(self->window, A_BOLD); | 233 | wattroff(self->window, A_BOLD); |
216 | 234 | ||
217 | wattroff(self->window, COLOR_PAIR(2)); | 235 | wattroff(self->window, COLOR_PAIR(2)); |
218 | } | 236 | } |
219 | 237 | ||
220 | void cmd_msg(ToxWindow *self, Messenger *m, char **args) | 238 | void cmd_msg(ToxWindow *self, Messenger *m, char **args) |
221 | { | 239 | { |
222 | char *id = args[1]; | 240 | char *id = args[1]; |
223 | char *msg = args[2]; | 241 | char *msg = args[2]; |
224 | if (m_sendmessage(m, atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0) | 242 | |
225 | wprintw(self->window, "Error occurred while sending message.\n"); | 243 | if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0) |
226 | else | 244 | wprintw(self->window, "Error occurred while sending message.\n"); |
227 | wprintw(self->window, "Message successfully sent.\n"); | 245 | else |
246 | wprintw(self->window, "Message successfully sent.\n"); | ||
228 | } | 247 | } |
229 | 248 | ||
230 | void cmd_myid(ToxWindow *self, Messenger *m, char **args) | 249 | void cmd_myid(ToxWindow *self, Messenger *m, char **args) |
231 | { | 250 | { |
232 | char id[FRIEND_ADDRESS_SIZE*2 + 1] = {0}; | 251 | char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; |
233 | size_t i; | 252 | size_t i; |
234 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 253 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
235 | getaddress(m, address); | 254 | getaddress(m, address); |
236 | for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { | 255 | |
237 | char xx[3]; | 256 | for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { |
238 | snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); | 257 | char xx[3]; |
239 | strcat(id, xx); | 258 | snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); |
240 | } | 259 | strcat(id, xx); |
241 | wprintw(self->window, "%s\n", id); | 260 | } |
261 | |||
262 | wprintw(self->window, "%s\n", id); | ||
242 | } | 263 | } |
243 | 264 | ||
244 | void cmd_nick(ToxWindow *self, Messenger *m, char **args) | 265 | void cmd_nick(ToxWindow *self, Messenger *m, char **args) |
245 | { | 266 | { |
246 | char *nick = args[1]; | 267 | char *nick = args[1]; |
247 | setname(m, (uint8_t*) nick, strlen(nick)+1); | 268 | setname(m, (uint8_t *) nick, strlen(nick) + 1); |
248 | wprintw(self->window, "Nickname set to: %s\n", nick); | 269 | wprintw(self->window, "Nickname set to: %s\n", nick); |
249 | } | 270 | } |
250 | 271 | ||
251 | void cmd_status(ToxWindow *self, Messenger *m, char **args) | 272 | void cmd_status(ToxWindow *self, Messenger *m, char **args) |
252 | { | 273 | { |
253 | char *status = args[1]; | 274 | char *status = args[1]; |
254 | char *status_text; | 275 | char *status_text; |
255 | 276 | ||
256 | USERSTATUS status_kind; | 277 | USERSTATUS status_kind; |
257 | if (!strncmp(status, "online", strlen("online"))) { | 278 | |
258 | status_kind = USERSTATUS_NONE; | 279 | if (!strncmp(status, "online", strlen("online"))) { |
259 | status_text = "ONLINE"; | 280 | status_kind = USERSTATUS_NONE; |
260 | } | 281 | status_text = "ONLINE"; |
261 | else if (!strncmp(status, "away", strlen("away"))) { | 282 | } else if (!strncmp(status, "away", strlen("away"))) { |
262 | status_kind = USERSTATUS_AWAY; | 283 | status_kind = USERSTATUS_AWAY; |
263 | status_text = "AWAY"; | 284 | status_text = "AWAY"; |
264 | } | 285 | } else if (!strncmp(status, "busy", strlen("busy"))) { |
265 | else if (!strncmp(status, "busy", strlen("busy"))) { | 286 | status_kind = USERSTATUS_BUSY; |
266 | status_kind = USERSTATUS_BUSY; | 287 | status_text = "BUSY"; |
267 | status_text = "BUSY"; | 288 | } else { |
268 | } | 289 | wprintw(self->window, "Invalid status.\n"); |
269 | else | 290 | return; |
270 | { | 291 | } |
271 | wprintw(self->window, "Invalid status.\n"); | 292 | |
272 | return; | 293 | char *msg = args[2]; |
273 | } | 294 | |
274 | 295 | if (msg == NULL) { | |
275 | char *msg = args[2]; | 296 | m_set_userstatus(m, status_kind); |
276 | if (msg == NULL) { | 297 | wprintw(self->window, "Status set to: %s\n", status_text); |
277 | m_set_userstatus(m, status_kind); | 298 | } else { |
278 | wprintw(self->window, "Status set to: %s\n", status_text); | 299 | m_set_userstatus(m, status_kind); |
279 | } | 300 | m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); |
280 | else { | 301 | wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); |
281 | m_set_userstatus(m, status_kind); | 302 | } |
282 | m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1); | ||
283 | wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); | ||
284 | } | ||
285 | } | 303 | } |
286 | 304 | ||
287 | void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args) | 305 | void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args) |
288 | { | 306 | { |
289 | char *msg = args[1]; | 307 | char *msg = args[1]; |
290 | m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1); | 308 | m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); |
291 | wprintw(self->window, "Status set to: %s\n", msg); | 309 | wprintw(self->window, "Status set to: %s\n", msg); |
292 | } | 310 | } |
293 | 311 | ||
294 | static void execute(ToxWindow *self, Messenger *m, char *u_cmd) | 312 | static void execute(ToxWindow *self, Messenger *m, char *u_cmd) |
295 | { | 313 | { |
296 | int newlines = 0; | 314 | int newlines = 0; |
297 | char cmd[MAX_STR_SIZE] = {0}; | 315 | char cmd[MAX_STR_SIZE] = {0}; |
298 | int i; | 316 | int i; |
299 | for (i = 0; i < strlen(prompt_buf); ++i) { | 317 | |
300 | if (u_cmd[i] == '\n') | 318 | for (i = 0; i < strlen(prompt_buf); ++i) { |
301 | ++newlines; | 319 | if (u_cmd[i] == '\n') |
302 | else | 320 | ++newlines; |
303 | cmd[i - newlines] = u_cmd[i]; | 321 | else |
304 | } | 322 | cmd[i - newlines] = u_cmd[i]; |
305 | |||
306 | int leading_spc = 0; | ||
307 | for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) | ||
308 | leading_spc++; | ||
309 | memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); | ||
310 | |||
311 | int cmd_end = strlen(cmd); | ||
312 | while (cmd_end > 0 && cmd_end--) | ||
313 | if (!isspace(cmd[cmd_end])) | ||
314 | break; | ||
315 | cmd[cmd_end + 1] = '\0'; | ||
316 | |||
317 | /* insert \0 at argument boundaries */ | ||
318 | int numargs = 0; | ||
319 | for (i = 0; i < MAX_STR_SIZE; i++) { | ||
320 | if (cmd[i] == '\"') | ||
321 | while (cmd[++i] != '\"'); /* skip over strings */ | ||
322 | if (cmd[i] == ' ') { | ||
323 | cmd[i] = '\0'; | ||
324 | numargs++; | ||
325 | } | 323 | } |
326 | } | 324 | |
327 | 325 | int leading_spc = 0; | |
328 | /* excessive arguments */ | 326 | |
329 | if (numargs > 3) { | 327 | for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) |
330 | wprintw(self->window, "Invalid command: too many arguments.\n"); | 328 | leading_spc++; |
331 | return; | 329 | |
332 | } | 330 | memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); |
333 | 331 | ||
334 | /* read arguments into array */ | 332 | int cmd_end = strlen(cmd); |
335 | char *cmdargs[5]; | 333 | |
336 | int pos = 0; | 334 | while (cmd_end > 0 && cmd_end--) |
337 | for (i = 0; i < 5; i++) { | 335 | if (!isspace(cmd[cmd_end])) |
338 | cmdargs[i] = cmd + pos; | 336 | break; |
339 | pos += strlen(cmdargs[i]) + 1; | 337 | |
340 | } | 338 | cmd[cmd_end + 1] = '\0'; |
341 | 339 | ||
342 | /* no input */ | 340 | /* insert \0 at argument boundaries */ |
343 | if (strlen(cmdargs[0]) == 0) | 341 | int numargs = 0; |
344 | return; | 342 | |
345 | 343 | for (i = 0; i < MAX_STR_SIZE; i++) { | |
346 | /* match input to command list */ | 344 | if (cmd[i] == '\"') |
347 | for (i = 0; i < NUM_COMMANDS; i++) { | 345 | while (cmd[++i] != '\"'); /* skip over strings */ |
348 | if (!strcmp(cmdargs[0], commands[i].name)) { | 346 | |
349 | /* check for missing arguments */ | 347 | if (cmd[i] == ' ') { |
350 | int j; | 348 | cmd[i] = '\0'; |
351 | for (j = 0; j <= commands[i].numargs; j++) { | 349 | numargs++; |
352 | if (strlen(cmdargs[j]) == 0) { | ||
353 | wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n", | ||
354 | commands[i].name, commands[i].numargs, j - 1); | ||
355 | return; | ||
356 | } | 350 | } |
357 | } | 351 | } |
358 | /* check for excess arguments */ | 352 | |
359 | if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) { | 353 | /* excessive arguments */ |
360 | wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); | 354 | if (numargs > 3) { |
355 | wprintw(self->window, "Invalid command: too many arguments.\n"); | ||
361 | return; | 356 | return; |
362 | } | ||
363 | /* pass arguments to command function */ | ||
364 | (commands[i].func)(self, m, cmdargs); | ||
365 | return; | ||
366 | } | 357 | } |
367 | } | ||
368 | 358 | ||
369 | /* no match */ | 359 | /* read arguments into array */ |
370 | wprintw(self->window, "Invalid command.\n"); | 360 | char *cmdargs[5]; |
361 | int pos = 0; | ||
362 | |||
363 | for (i = 0; i < 5; i++) { | ||
364 | cmdargs[i] = cmd + pos; | ||
365 | pos += strlen(cmdargs[i]) + 1; | ||
366 | } | ||
367 | |||
368 | /* no input */ | ||
369 | if (strlen(cmdargs[0]) == 0) | ||
370 | return; | ||
371 | |||
372 | /* match input to command list */ | ||
373 | for (i = 0; i < NUM_COMMANDS; i++) { | ||
374 | if (!strcmp(cmdargs[0], commands[i].name)) { | ||
375 | /* check for missing arguments */ | ||
376 | int j; | ||
377 | |||
378 | for (j = 0; j <= commands[i].numargs; j++) { | ||
379 | if (strlen(cmdargs[j]) == 0) { | ||
380 | wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n", | ||
381 | commands[i].name, commands[i].numargs, j - 1); | ||
382 | return; | ||
383 | } | ||
384 | } | ||
385 | |||
386 | /* check for excess arguments */ | ||
387 | if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) { | ||
388 | wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name); | ||
389 | return; | ||
390 | } | ||
391 | |||
392 | /* pass arguments to command function */ | ||
393 | (commands[i].func)(self, m, cmdargs); | ||
394 | return; | ||
395 | } | ||
396 | } | ||
397 | |||
398 | /* no match */ | ||
399 | wprintw(self->window, "Invalid command.\n"); | ||
371 | } | 400 | } |
372 | 401 | ||
373 | static void prompt_onKey(ToxWindow *self, Messenger *m, int key) | 402 | static void prompt_onKey(ToxWindow *self, Messenger *m, int key) |
374 | { | 403 | { |
375 | /* Add printable characters to line */ | 404 | /* Add printable characters to line */ |
376 | if (isprint(key)) { | 405 | if (isprint(key)) { |
377 | if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) { | 406 | if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) { |
378 | wprintw(self->window, "\nToo Long.\n"); | 407 | wprintw(self->window, "\nToo Long.\n"); |
379 | prompt_buf_pos = 0; | 408 | prompt_buf_pos = 0; |
380 | prompt_buf[0] = 0; | 409 | prompt_buf[0] = 0; |
381 | } | 410 | } else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) |
382 | else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) | 411 | && (prompt_buf_pos % (COLS - 3) == 0)) { |
383 | && (prompt_buf_pos % (COLS - 3) == 0)) { | 412 | prompt_buf[prompt_buf_pos++] = '\n'; |
384 | prompt_buf[prompt_buf_pos++] = '\n'; | 413 | } else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) |
414 | && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { | ||
415 | prompt_buf[prompt_buf_pos++] = '\n'; | ||
416 | } | ||
417 | |||
418 | prompt_buf[prompt_buf_pos++] = key; | ||
419 | prompt_buf[prompt_buf_pos] = 0; | ||
385 | } | 420 | } |
386 | else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) | 421 | |
387 | && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { | 422 | /* RETURN key: execute command */ |
388 | prompt_buf[prompt_buf_pos++] = '\n'; | 423 | else if (key == '\n') { |
424 | wprintw(self->window, "\n"); | ||
425 | execute(self, m, prompt_buf); | ||
426 | prompt_buf_pos = 0; | ||
427 | prompt_buf[0] = 0; | ||
389 | } | 428 | } |
390 | prompt_buf[prompt_buf_pos++] = key; | 429 | |
391 | prompt_buf[prompt_buf_pos] = 0; | 430 | /* BACKSPACE key: Remove one character from line */ |
392 | } | 431 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { |
393 | 432 | if (prompt_buf_pos != 0) { | |
394 | /* RETURN key: execute command */ | 433 | prompt_buf[--prompt_buf_pos] = 0; |
395 | else if (key == '\n') { | 434 | } |
396 | wprintw(self->window, "\n"); | ||
397 | execute(self, m, prompt_buf); | ||
398 | prompt_buf_pos = 0; | ||
399 | prompt_buf[0] = 0; | ||
400 | } | ||
401 | |||
402 | /* BACKSPACE key: Remove one character from line */ | ||
403 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { | ||
404 | if (prompt_buf_pos != 0) { | ||
405 | prompt_buf[--prompt_buf_pos] = 0; | ||
406 | } | 435 | } |
407 | } | ||
408 | } | 436 | } |
409 | 437 | ||
410 | static void prompt_onDraw(ToxWindow *self) | 438 | static void prompt_onDraw(ToxWindow *self) |
411 | { | 439 | { |
412 | curs_set(1); | 440 | curs_set(1); |
413 | int x, y; | 441 | int x, y; |
414 | getyx(self->window, y, x); | 442 | getyx(self->window, y, x); |
415 | (void) x; | 443 | (void) x; |
416 | int i; | 444 | int i; |
417 | for (i = 0; i < (strlen(prompt_buf)); ++i) { | 445 | |
418 | if ((prompt_buf[i] == '\n') && (y != 0)) | 446 | for (i = 0; i < (strlen(prompt_buf)); ++i) { |
419 | --y; | 447 | if ((prompt_buf[i] == '\n') && (y != 0)) |
420 | } | 448 | --y; |
421 | 449 | } | |
422 | wattron(self->window, COLOR_PAIR(1)); | 450 | |
423 | mvwprintw(self->window, y, 0, "# "); | 451 | wattron(self->window, COLOR_PAIR(1)); |
424 | wattroff(self->window, COLOR_PAIR(1)); | 452 | mvwprintw(self->window, y, 0, "# "); |
425 | mvwprintw(self->window, y, 2, "%s", prompt_buf); | 453 | wattroff(self->window, COLOR_PAIR(1)); |
426 | wclrtoeol(self->window); | 454 | mvwprintw(self->window, y, 2, "%s", prompt_buf); |
427 | wrefresh(self->window); | 455 | wclrtoeol(self->window); |
456 | wrefresh(self->window); | ||
428 | } | 457 | } |
429 | 458 | ||
430 | static void prompt_onInit(ToxWindow *self, Messenger *m) | 459 | static void prompt_onInit(ToxWindow *self, Messenger *m) |
431 | { | 460 | { |
432 | scrollok(self->window, 1); | 461 | scrollok(self->window, 1); |
433 | cmd_help(self, m, NULL); | 462 | cmd_help(self, m, NULL); |
434 | wclrtoeol(self->window); | 463 | wclrtoeol(self->window); |
435 | } | 464 | } |
436 | 465 | ||
437 | ToxWindow new_prompt(friendAddedFn *f) | 466 | ToxWindow new_prompt(friendAddedFn *f) |
438 | { | 467 | { |
439 | on_friendadded_cb = f; | 468 | on_friendadded_cb = f; |
440 | ToxWindow ret; | 469 | ToxWindow ret; |
441 | memset(&ret, 0, sizeof(ret)); | 470 | memset(&ret, 0, sizeof(ret)); |
442 | ret.onKey = &prompt_onKey; | 471 | ret.onKey = &prompt_onKey; |
443 | ret.onDraw = &prompt_onDraw; | 472 | ret.onDraw = &prompt_onDraw; |
444 | ret.onInit = &prompt_onInit; | 473 | ret.onInit = &prompt_onInit; |
445 | strcpy(ret.title, "[prompt]"); | 474 | strcpy(ret.title, "[prompt]"); |
446 | return ret; | 475 | return ret; |
447 | } | 476 | } |
diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index 15caddf7..183c0f9b 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c | |||
@@ -14,232 +14,258 @@ static ToxWindow windows[MAX_WINDOW_SLOTS]; | |||
14 | static Messenger *m; | 14 | static Messenger *m; |
15 | int active_window; | 15 | int active_window; |
16 | 16 | ||
17 | static ToxWindow* prompt; | 17 | static ToxWindow *prompt; |
18 | 18 | ||
19 | /* CALLBACKS START */ | 19 | /* CALLBACKS START */ |
20 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata) | 20 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) |
21 | { | 21 | { |
22 | int n = add_req(public_key); | 22 | int n = add_req(public_key); |
23 | wprintw(prompt->window, "\nFriend request from:\n"); | 23 | wprintw(prompt->window, "\nFriend request from:\n"); |
24 | 24 | ||
25 | int i; | 25 | int i; |
26 | for (i = 0; i < KEY_SIZE_BYTES; ++i) { | ||
27 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); | ||
28 | } | ||
29 | 26 | ||
30 | wprintw(prompt->window, "\nWith the message: %s\n", data); | 27 | for (i = 0; i < KEY_SIZE_BYTES; ++i) { |
31 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); | 28 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); |
29 | } | ||
30 | |||
31 | wprintw(prompt->window, "\nWith the message: %s\n", data); | ||
32 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); | ||
32 | 33 | ||
33 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 34 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
34 | if (windows[i].onFriendRequest != NULL) | 35 | if (windows[i].onFriendRequest != NULL) |
35 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | 36 | windows[i].onFriendRequest(&windows[i], public_key, data, length); |
36 | } | 37 | } |
37 | } | 38 | } |
38 | 39 | ||
39 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 40 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
40 | { | 41 | { |
41 | int i; | 42 | int i; |
42 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 43 | |
43 | if (windows[i].onMessage != NULL) | 44 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
44 | windows[i].onMessage(&windows[i], m, friendnumber, string, length); | 45 | if (windows[i].onMessage != NULL) |
45 | } | 46 | windows[i].onMessage(&windows[i], m, friendnumber, string, length); |
47 | } | ||
46 | } | 48 | } |
47 | 49 | ||
48 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 50 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
49 | { | 51 | { |
50 | int i; | 52 | int i; |
51 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 53 | |
52 | if (windows[i].onAction != NULL) | 54 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
53 | windows[i].onAction(&windows[i], m, friendnumber, string, length); | 55 | if (windows[i].onAction != NULL) |
54 | } | 56 | windows[i].onAction(&windows[i], m, friendnumber, string, length); |
57 | } | ||
55 | } | 58 | } |
56 | 59 | ||
57 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 60 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
58 | { | 61 | { |
59 | wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); | 62 | wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); |
60 | int i; | 63 | int i; |
61 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 64 | |
62 | if (windows[i].onNickChange != NULL) | 65 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
63 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | 66 | if (windows[i].onNickChange != NULL) |
64 | } | 67 | windows[i].onNickChange(&windows[i], friendnumber, string, length); |
68 | } | ||
65 | } | 69 | } |
66 | 70 | ||
67 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata) | 71 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
68 | { | 72 | { |
69 | wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); | 73 | wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); |
70 | int i; | 74 | int i; |
71 | for (i=0; i<MAX_WINDOW_SLOTS; ++i) { | 75 | |
72 | if (windows[i].onStatusChange != NULL) | 76 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
73 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | 77 | if (windows[i].onStatusChange != NULL) |
74 | } | 78 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); |
79 | } | ||
75 | } | 80 | } |
76 | 81 | ||
77 | void on_friendadded(Messenger *m, int friendnumber) | 82 | void on_friendadded(Messenger *m, int friendnumber) |
78 | { | 83 | { |
79 | friendlist_onFriendAdded(m, friendnumber); | 84 | friendlist_onFriendAdded(m, friendnumber); |
80 | if (store_data(m, DATA_FILE) != 0) { | 85 | |
81 | wprintw(prompt->window, "\nCould not store Messenger data\n"); | 86 | if (store_data(m, DATA_FILE) != 0) { |
82 | } | 87 | wprintw(prompt->window, "\nCould not store Messenger data\n"); |
88 | } | ||
83 | } | 89 | } |
84 | /* CALLBACKS END */ | 90 | /* CALLBACKS END */ |
85 | 91 | ||
86 | int add_window(Messenger *m, ToxWindow w, int n) | 92 | int add_window(Messenger *m, ToxWindow w, int n) |
87 | { | 93 | { |
88 | if (w_num >= TOXWINDOWS_MAX_NUM) | 94 | if (w_num >= TOXWINDOWS_MAX_NUM) |
89 | return -1; | 95 | return -1; |
96 | |||
97 | if (LINES < 2) | ||
98 | return -1; | ||
90 | 99 | ||
91 | if (LINES < 2) | 100 | w.window = newwin(LINES - 2, COLS, 0, 0); |
92 | return -1; | ||
93 | 101 | ||
94 | w.window = newwin(LINES - 2, COLS, 0, 0); | 102 | if (w.window == NULL) |
95 | if (w.window == NULL) | 103 | return -1; |
96 | return -1; | ||
97 | 104 | ||
98 | windows[n] = w; | 105 | windows[n] = w; |
99 | w.onInit(&w, m); | 106 | w.onInit(&w, m); |
100 | w_num++; | 107 | w_num++; |
101 | active_window = n; | 108 | active_window = n; |
102 | return n; | 109 | return n; |
103 | } | 110 | } |
104 | 111 | ||
105 | /* Deletes window w and cleans up */ | 112 | /* Deletes window w and cleans up */ |
106 | void del_window(ToxWindow *w, int f_num) | 113 | void del_window(ToxWindow *w, int f_num) |
107 | { | 114 | { |
108 | active_window = 0; // Go to prompt screen | 115 | active_window = 0; // Go to prompt screen |
109 | delwin(w->window); | 116 | delwin(w->window); |
110 | int i; | 117 | int i; |
111 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { | 118 | |
112 | if (WINDOW_STATUS[i] == f_num) { | 119 | for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { |
113 | WINDOW_STATUS[i] = -1; | 120 | if (WINDOW_STATUS[i] == f_num) { |
114 | disable_chatwin(f_num); | 121 | WINDOW_STATUS[i] = -1; |
115 | break; | 122 | disable_chatwin(f_num); |
123 | break; | ||
124 | } | ||
116 | } | 125 | } |
117 | } | 126 | |
118 | clear(); | 127 | clear(); |
119 | refresh(); | 128 | refresh(); |
120 | } | 129 | } |
121 | 130 | ||
122 | /* Shows next window when tab or back-tab is pressed */ | 131 | /* Shows next window when tab or back-tab is pressed */ |
123 | void set_active_window(int ch) | 132 | void set_active_window(int ch) |
124 | { | 133 | { |
125 | int f_inf = 0; | 134 | int f_inf = 0; |
126 | int max = MAX_WINDOW_SLOTS-1; | 135 | int max = MAX_WINDOW_SLOTS - 1; |
127 | if (ch == '\t') { | 136 | |
128 | int i = (active_window + 1) % max; | 137 | if (ch == '\t') { |
129 | while (true) { | 138 | int i = (active_window + 1) % max; |
130 | if (WINDOW_STATUS[i] != -1) { | 139 | |
131 | active_window = i; | 140 | while (true) { |
132 | return; | 141 | if (WINDOW_STATUS[i] != -1) { |
133 | } | 142 | active_window = i; |
134 | i = (i + 1) % max; | 143 | return; |
135 | if (f_inf++ > max) { // infinite loop check | 144 | } |
136 | endwin(); | 145 | |
137 | exit(2); | 146 | i = (i + 1) % max; |
138 | } | 147 | |
139 | } | 148 | if (f_inf++ > max) { // infinite loop check |
140 | }else { | 149 | endwin(); |
141 | int i = active_window - 1; | 150 | exit(2); |
142 | if (i < 0) i = max; | 151 | } |
143 | while (true) { | 152 | } |
144 | if (WINDOW_STATUS[i] != -1) { | 153 | } else { |
145 | active_window = i; | 154 | int i = active_window - 1; |
146 | return; | 155 | |
147 | } | 156 | if (i < 0) i = max; |
148 | if (--i < 0) i = max; | 157 | |
149 | if (f_inf++ > max) { | 158 | while (true) { |
150 | endwin(); | 159 | if (WINDOW_STATUS[i] != -1) { |
151 | exit(2); | 160 | active_window = i; |
152 | } | 161 | return; |
162 | } | ||
163 | |||
164 | if (--i < 0) i = max; | ||
165 | |||
166 | if (f_inf++ > max) { | ||
167 | endwin(); | ||
168 | exit(2); | ||
169 | } | ||
170 | } | ||
153 | } | 171 | } |
154 | } | ||
155 | } | 172 | } |
156 | 173 | ||
157 | void init_window_status() | 174 | void init_window_status() |
158 | { | 175 | { |
159 | /* Default window values decrement from -2 */ | 176 | /* Default window values decrement from -2 */ |
160 | int i; | 177 | int i; |
161 | for (i = 0; i < N_DEFAULT_WINS; ++i) | 178 | |
162 | WINDOW_STATUS[i] = -(i+2); | 179 | for (i = 0; i < N_DEFAULT_WINS; ++i) |
163 | 180 | WINDOW_STATUS[i] = -(i + 2); | |
164 | int j; | 181 | |
165 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) | 182 | int j; |
166 | WINDOW_STATUS[j] = -1; | 183 | |
184 | for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++) | ||
185 | WINDOW_STATUS[j] = -1; | ||
167 | } | 186 | } |
168 | 187 | ||
169 | ToxWindow *init_windows() | 188 | ToxWindow *init_windows() |
170 | { | 189 | { |
171 | w_num = 0; | 190 | w_num = 0; |
172 | int n_prompt = 0; | 191 | int n_prompt = 0; |
173 | int n_friendslist = 1; | 192 | int n_friendslist = 1; |
174 | int n_dhtstatus = 2; | 193 | int n_dhtstatus = 2; |
175 | if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 | 194 | |
176 | || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 | 195 | if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1 |
177 | || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { | 196 | || add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1 |
178 | fprintf(stderr, "add_window() failed.\n"); | 197 | || add_window(m, new_dhtstatus(), n_dhtstatus) == -1) { |
179 | endwin(); | 198 | fprintf(stderr, "add_window() failed.\n"); |
180 | exit(1); | 199 | endwin(); |
181 | } | 200 | exit(1); |
182 | active_window = n_prompt; | 201 | } |
183 | prompt = &windows[n_prompt]; | 202 | |
184 | return prompt; | 203 | active_window = n_prompt; |
204 | prompt = &windows[n_prompt]; | ||
205 | return prompt; | ||
185 | } | 206 | } |
186 | 207 | ||
187 | static void draw_bar() | 208 | static void draw_bar() |
188 | { | 209 | { |
189 | static int odd = 0; | 210 | static int odd = 0; |
190 | int blinkrate = 30; | 211 | int blinkrate = 30; |
212 | |||
213 | attron(COLOR_PAIR(4)); | ||
214 | mvhline(LINES - 2, 0, '_', COLS); | ||
215 | attroff(COLOR_PAIR(4)); | ||
216 | |||
217 | move(LINES - 1, 0); | ||
218 | |||
219 | attron(COLOR_PAIR(4) | A_BOLD); | ||
220 | printw(" TOXIC " TOXICVER "|"); | ||
221 | attroff(COLOR_PAIR(4) | A_BOLD); | ||
191 | 222 | ||
192 | attron(COLOR_PAIR(4)); | 223 | int i; |
193 | mvhline(LINES - 2, 0, '_', COLS); | ||
194 | attroff(COLOR_PAIR(4)); | ||
195 | 224 | ||
196 | move(LINES - 1, 0); | 225 | for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { |
226 | if (WINDOW_STATUS[i] != -1) { | ||
227 | if (i == active_window) | ||
228 | attron(A_BOLD); | ||
197 | 229 | ||
198 | attron(COLOR_PAIR(4) | A_BOLD); | 230 | odd = (odd + 1) % blinkrate; |
199 | printw(" TOXIC " TOXICVER "|"); | ||
200 | attroff(COLOR_PAIR(4) | A_BOLD); | ||
201 | 231 | ||
202 | int i; | 232 | if (windows[i].blink && (odd < (blinkrate / 2))) |
203 | for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { | 233 | attron(COLOR_PAIR(3)); |
204 | if (WINDOW_STATUS[i] != -1) { | ||
205 | if (i == active_window) | ||
206 | attron(A_BOLD); | ||
207 | 234 | ||
208 | odd = (odd+1) % blinkrate; | 235 | printw(" %s", windows[i].title); |
209 | if (windows[i].blink && (odd < (blinkrate/2))) | ||
210 | attron(COLOR_PAIR(3)); | ||
211 | 236 | ||
212 | printw(" %s", windows[i].title); | 237 | if (windows[i].blink && (odd < (blinkrate / 2))) |
213 | if (windows[i].blink && (odd < (blinkrate/2))) | 238 | attroff(COLOR_PAIR(3)); |
214 | attroff(COLOR_PAIR(3)); | ||
215 | 239 | ||
216 | if (i == active_window) { | 240 | if (i == active_window) { |
217 | attroff(A_BOLD); | 241 | attroff(A_BOLD); |
218 | } | 242 | } |
243 | } | ||
219 | } | 244 | } |
220 | } | 245 | |
221 | refresh(); | 246 | refresh(); |
222 | } | 247 | } |
223 | 248 | ||
224 | void prepare_window(WINDOW *w) | 249 | void prepare_window(WINDOW *w) |
225 | { | 250 | { |
226 | mvwin(w, 0, 0); | 251 | mvwin(w, 0, 0); |
227 | wresize(w, LINES-2, COLS); | 252 | wresize(w, LINES - 2, COLS); |
228 | } | 253 | } |
229 | 254 | ||
230 | void draw_active_window(Messenger *m) | 255 | void draw_active_window(Messenger *m) |
231 | { | 256 | { |
232 | 257 | ||
233 | ToxWindow *a = &windows[active_window]; | 258 | ToxWindow *a = &windows[active_window]; |
234 | prepare_window(a->window); | 259 | prepare_window(a->window); |
235 | a->blink = false; | 260 | a->blink = false; |
236 | draw_bar(); | 261 | draw_bar(); |
237 | a->onDraw(a); | 262 | a->onDraw(a); |
238 | 263 | ||
239 | /* Handle input */ | 264 | /* Handle input */ |
240 | int ch = getch(); | 265 | int ch = getch(); |
241 | if (ch == '\t' || ch == KEY_BTAB) | 266 | |
242 | set_active_window(ch); | 267 | if (ch == '\t' || ch == KEY_BTAB) |
243 | else if (ch != ERR) | 268 | set_active_window(ch); |
244 | a->onKey(a, m, ch); | 269 | else if (ch != ERR) |
270 | a->onKey(a, m, ch); | ||
245 | } | 271 | } |
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 9f4d74a2..553a6200 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h | |||
@@ -26,31 +26,31 @@ | |||
26 | typedef struct ToxWindow_ ToxWindow; | 26 | typedef struct ToxWindow_ ToxWindow; |
27 | 27 | ||
28 | struct ToxWindow_ { | 28 | struct ToxWindow_ { |
29 | void(*onKey)(ToxWindow*, Messenger*, int); | 29 | void(*onKey)(ToxWindow *, Messenger *, int); |
30 | void(*onDraw)(ToxWindow*); | 30 | void(*onDraw)(ToxWindow *); |
31 | void(*onInit)(ToxWindow*, Messenger*); | 31 | void(*onInit)(ToxWindow *, Messenger *); |
32 | void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); | 32 | void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); |
33 | void(*onMessage)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t); | 33 | void(*onMessage)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t); |
34 | void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); | 34 | void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); |
35 | void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); | 35 | void(*onStatusChange)(ToxWindow *, int, uint8_t *, uint16_t); |
36 | void(*onAction)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t); | 36 | void(*onAction)(ToxWindow *, Messenger *, int, uint8_t *, uint16_t); |
37 | char title[256]; | 37 | char title[256]; |
38 | 38 | ||
39 | void* x; | 39 | void *x; |
40 | bool blink; | 40 | bool blink; |
41 | 41 | ||
42 | WINDOW* window; | 42 | WINDOW *window; |
43 | }; | 43 | }; |
44 | 44 | ||
45 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata); | 45 | void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); |
46 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 46 | void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
47 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 47 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); |
50 | void on_friendadded(Messenger *m, int friendnumber); | 50 | void on_friendadded(Messenger *m, int friendnumber); |
51 | void init_window_status(); | 51 | void init_window_status(); |
52 | ToxWindow * init_windows(); | 52 | ToxWindow *init_windows(); |
53 | void draw_active_window(Messenger * m); | 53 | void draw_active_window(Messenger *m); |
54 | int add_window(Messenger *m, ToxWindow w, int n); | 54 | int add_window(Messenger *m, ToxWindow w, int n); |
55 | void del_window(ToxWindow *w, int f_num); | 55 | void del_window(ToxWindow *w, int f_num); |
56 | void set_active_window(int ch); | 56 | void set_active_window(int ch); |