ffi-tox 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/ProjectTox-Core/CMakeLists.txt +28 -0
  2. data/ProjectTox-Core/COPYING +674 -0
  3. data/ProjectTox-Core/INSTALL.md +91 -0
  4. data/ProjectTox-Core/README.md +54 -0
  5. data/ProjectTox-Core/core/CMakeLists.txt +17 -0
  6. data/ProjectTox-Core/core/DHT.c +1104 -0
  7. data/ProjectTox-Core/core/DHT.h +111 -0
  8. data/ProjectTox-Core/core/LAN_discovery.c +79 -0
  9. data/ProjectTox-Core/core/LAN_discovery.h +50 -0
  10. data/ProjectTox-Core/core/Lossless_UDP.c +749 -0
  11. data/ProjectTox-Core/core/Lossless_UDP.h +106 -0
  12. data/ProjectTox-Core/core/Messenger.c +581 -0
  13. data/ProjectTox-Core/core/Messenger.h +157 -0
  14. data/ProjectTox-Core/core/friend_requests.c +131 -0
  15. data/ProjectTox-Core/core/friend_requests.h +51 -0
  16. data/ProjectTox-Core/core/net_crypto.c +564 -0
  17. data/ProjectTox-Core/core/net_crypto.h +134 -0
  18. data/ProjectTox-Core/core/network.c +188 -0
  19. data/ProjectTox-Core/core/network.h +134 -0
  20. data/ProjectTox-Core/other/CMakeLists.txt +9 -0
  21. data/ProjectTox-Core/other/DHT_bootstrap.c +139 -0
  22. data/ProjectTox-Core/testing/CMakeLists.txt +18 -0
  23. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +228 -0
  24. data/ProjectTox-Core/testing/DHT_sendfiletest.c +176 -0
  25. data/ProjectTox-Core/testing/DHT_test.c +182 -0
  26. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +214 -0
  27. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +201 -0
  28. data/ProjectTox-Core/testing/Messenger_test.c +145 -0
  29. data/ProjectTox-Core/testing/misc_tools.c +40 -0
  30. data/ProjectTox-Core/testing/misc_tools.h +29 -0
  31. data/ProjectTox-Core/testing/nTox.c +381 -0
  32. data/ProjectTox-Core/testing/nTox.h +50 -0
  33. data/ProjectTox-Core/testing/nTox_win32.c +323 -0
  34. data/ProjectTox-Core/testing/nTox_win32.h +31 -0
  35. data/ProjectTox-Core/testing/rect.py +45 -0
  36. data/ext/ffi-tox/extconf.rb +16 -0
  37. data/interfaces/libtox.i +18 -0
  38. data/lib/ffi-tox/libtox.rb +37 -0
  39. data/lib/ffi-tox.rb +1 -0
  40. metadata +116 -0
@@ -0,0 +1,381 @@
1
+ /* nTox.c
2
+ *
3
+ * Textual frontend for Tox.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
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/>.
21
+ *
22
+ */
23
+ #include "nTox.h"
24
+ #include "misc_tools.h"
25
+
26
+
27
+ #include <stdio.h>
28
+ #include <time.h>
29
+ #ifdef WIN32
30
+ #define c_sleep(x) Sleep(1*x)
31
+ #else
32
+ #include <unistd.h>
33
+ #define c_sleep(x) usleep(1000*x)
34
+ #endif
35
+
36
+ char lines[HISTORY][STRING_LENGTH];
37
+ char line[STRING_LENGTH];
38
+ int x,y;
39
+
40
+ uint8_t pending_requests[256][CLIENT_ID_SIZE];
41
+ uint8_t num_requests;
42
+
43
+ void new_lines(char *line)
44
+ {
45
+ int i;
46
+ for (i = HISTORY-1; i > 0; i--)
47
+ strcpy(lines[i],lines[i-1]);
48
+
49
+ strcpy(lines[0],line);
50
+ do_refresh();
51
+ }
52
+
53
+ void print_friendlist()
54
+ {
55
+ char name[MAX_NAME_LENGTH];
56
+ uint32_t i;
57
+
58
+ new_lines("[i] Friend List:");
59
+ for (i=0; i <= num_requests; i++) {
60
+ char fstring[128];
61
+
62
+ getname(i, (uint8_t*)name);
63
+ if (strlen(name) <= 0) {
64
+ sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i);
65
+ } else {
66
+ sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i);
67
+ }
68
+ new_lines(fstring);
69
+ }
70
+ }
71
+
72
+ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
73
+ {
74
+ if (line[0] == '/') {
75
+ char command[STRING_LENGTH + 2] = "> ";
76
+ strcat(command, line);
77
+ new_lines(command);
78
+ if (line[1] == 'f') { // add friend command: /f ID
79
+ int i;
80
+ char temp_id[128];
81
+ for (i=0; i<128; i++)
82
+ temp_id[i] = line[i+3];
83
+ int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
84
+ char numstring[100];
85
+ sprintf(numstring, "[i] added friend %d", num);
86
+ new_lines(numstring);
87
+ do_refresh();
88
+ }
89
+ else if (line[1] == 'd') {
90
+ doMessenger();
91
+ }
92
+ else if (line[1] == 'm') { //message command: /m friendnumber messsage
93
+ int i;
94
+ size_t len = strlen(line);
95
+ char numstring[len-3];
96
+ char message[len-3];
97
+ for (i=0; i<len; i++) {
98
+ if (line[i+3] != ' ') {
99
+ numstring[i] = line[i+3];
100
+ } else {
101
+ int j;
102
+ for (j=i+1; j<len; j++)
103
+ message[j-i-1] = line[j+3];
104
+ break;
105
+ }
106
+ }
107
+ int num = atoi(numstring);
108
+ if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
109
+ new_lines("[i] could not send message");
110
+ }
111
+ }
112
+ else if (line[1] == 'n') {
113
+ uint8_t name[MAX_NAME_LENGTH];
114
+ int i = 0;
115
+ size_t len = strlen(line);
116
+ for (i=3; i<len; i++) {
117
+ if (line[i] == 0 || line[i] == '\n') break;
118
+ name[i - 3] = line[i];
119
+ }
120
+ name[i - 3] = 0;
121
+ setname(name, i);
122
+ char numstring[100];
123
+ sprintf(numstring, "[i] changed nick to %s", (char*)name);
124
+ new_lines(numstring);
125
+ }
126
+ else if (line[1] == 'l') {
127
+ print_friendlist();
128
+ }
129
+ else if (line[1] == 's') {
130
+ uint8_t status[MAX_USERSTATUS_LENGTH];
131
+ int i = 0;
132
+ size_t len = strlen(line);
133
+ for (i=3; i<len; i++) {
134
+ if (line[i] == 0 || line[i] == '\n') break;
135
+ status[i - 3] = line[i];
136
+ }
137
+ status[i - 3] = 0;
138
+ m_set_userstatus(status, strlen((char*)status));
139
+ char numstring[100];
140
+ sprintf(numstring, "[i] changed status to %s", (char*)status);
141
+ new_lines(numstring);
142
+ }
143
+ else if (line[1] == 'a') {
144
+ uint8_t numf = atoi(line + 3);
145
+ char numchar[100];
146
+ sprintf(numchar, "[i] friend request %u accepted", numf);
147
+ new_lines(numchar);
148
+ int num = m_addfriend_norequest(pending_requests[numf]);
149
+ sprintf(numchar, "[i] added friendnumber %d", num);
150
+ new_lines(numchar);
151
+ do_refresh();
152
+
153
+ }
154
+ else if (line[1] == 'q') { //exit
155
+ endwin();
156
+ exit(EXIT_SUCCESS);
157
+ } else {
158
+ new_lines("[i] invalid command");
159
+ }
160
+ } else {
161
+ new_lines("[i] invalid command");
162
+ //new_lines(line);
163
+ }
164
+ }
165
+
166
+ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
167
+ {
168
+ int i = 0;
169
+ strcpy(output,input);
170
+ size_t len = strlen(output);
171
+ for (i = line_width; i < len; i = i + line_width) {
172
+ while (output[i] != ' ' && i != 0) {
173
+ i--;
174
+ }
175
+ if (i > 0) {
176
+ output[i] = '\n';
177
+ }
178
+ }
179
+ }
180
+
181
+ int count_lines(char *string)
182
+ {
183
+ size_t len = strlen(string);
184
+ int i;
185
+ int count = 1;
186
+ for (i=0; i < len; i++) {
187
+ if (string[i] == '\n')
188
+ count++;
189
+ }
190
+ return count;
191
+ }
192
+
193
+ char *appender(char *str, const char c)
194
+ {
195
+ size_t len = strlen(str);
196
+ if (len < STRING_LENGTH) {
197
+ str[len + 1] = str[len];
198
+ str[len] = c;
199
+ }
200
+ return str;
201
+ }
202
+
203
+ void do_refresh()
204
+ {
205
+ int i;
206
+ int count=0;
207
+ int l;
208
+ char wrap_output[STRING_LENGTH];
209
+ for (i=0; i<HISTORY; i++) {
210
+ wrap(wrap_output, lines[i], x);
211
+ l = count_lines(wrap_output);
212
+ count = count + l;
213
+ if (count < y) {
214
+ move(y-1-count,0);
215
+ printw(wrap_output);
216
+ clrtoeol();
217
+ }
218
+ }
219
+ move(y-1,0);
220
+ clrtoeol();
221
+ printw(">> ");
222
+ printw(line);
223
+ clrtoeol();
224
+ refresh();
225
+ }
226
+
227
+ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
228
+ {
229
+ new_lines("[i] received friend request with message:");
230
+ new_lines((char *)data);
231
+ char numchar[100];
232
+ sprintf(numchar, "[i] accept request with /a %u", num_requests);
233
+ new_lines(numchar);
234
+ memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
235
+ ++num_requests;
236
+ do_refresh();
237
+ }
238
+
239
+ void print_message(int friendnumber, uint8_t * string, uint16_t length)
240
+ {
241
+ char name[MAX_NAME_LENGTH];
242
+ getname(friendnumber, (uint8_t*)name);
243
+ char msg[100+length+strlen(name)+1];
244
+ time_t rawtime;
245
+ struct tm * timeinfo;
246
+ time ( &rawtime );
247
+ timeinfo = localtime ( &rawtime );
248
+ char* temp = asctime(timeinfo);
249
+ size_t len = strlen(temp);
250
+ temp[len-1]='\0';
251
+ sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp
252
+ new_lines(msg);
253
+ }
254
+
255
+ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
256
+ char name[MAX_NAME_LENGTH];
257
+ getname(friendnumber, (uint8_t*)name);
258
+ char msg[100+length];
259
+ sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
260
+ new_lines(msg);
261
+ }
262
+
263
+ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
264
+ char name[MAX_NAME_LENGTH];
265
+ getname(friendnumber, (uint8_t*)name);
266
+ char msg[100+length+strlen(name)+1];
267
+ sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
268
+ new_lines(msg);
269
+ }
270
+
271
+ void load_key(){
272
+ FILE *data_file = NULL;
273
+ if ((data_file = fopen("data","r"))) {
274
+ //load keys
275
+ fseek(data_file, 0, SEEK_END);
276
+ int size = ftell(data_file);
277
+ fseek(data_file, 0, SEEK_SET);
278
+ uint8_t data[size];
279
+ if(fread(data, sizeof(uint8_t), size, data_file) != size){
280
+ printf("[i] could not read data file\n[i] exiting\n");
281
+ exit(1);
282
+ }
283
+ Messenger_load(data, size);
284
+ } else {
285
+ //else save new keys
286
+ int size = Messenger_size();
287
+ uint8_t data[size];
288
+ Messenger_save(data);
289
+ data_file = fopen("data","w");
290
+ if(fwrite(data, sizeof(uint8_t), size, data_file) != size){
291
+ printf("[i] could not write data file\n[i] exiting\n");
292
+ exit(1);
293
+ }
294
+ }
295
+ fclose(data_file);
296
+ }
297
+
298
+ int main(int argc, char *argv[])
299
+ {
300
+ if (argc < 4) {
301
+ printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
302
+ exit(0);
303
+ }
304
+ int c;
305
+ int on = 0;
306
+ initMessenger();
307
+ //if keyfiles exist
308
+ if(argc > 4){
309
+ if(strncmp(argv[4], "nokey", 6) < 0){
310
+ //load_key();
311
+ }
312
+ } else {
313
+ load_key();
314
+ }
315
+ m_callback_friendrequest(print_request);
316
+ m_callback_friendmessage(print_message);
317
+ m_callback_namechange(print_nickchange);
318
+ m_callback_userstatus(print_statuschange);
319
+ char idstring0[200];
320
+ char idstring1[32][5];
321
+ char idstring2[32][5];
322
+ uint32_t i;
323
+ for(i = 0; i < 32; i++)
324
+ {
325
+ if(self_public_key[i] < 16)
326
+ strcpy(idstring1[i],"0");
327
+ else
328
+ strcpy(idstring1[i], "");
329
+ sprintf(idstring2[i], "%hhX",self_public_key[i]);
330
+ }
331
+ strcpy(idstring0,"[i] your ID: ");
332
+ for (i=0; i<32; i++) {
333
+ strcat(idstring0,idstring1[i]);
334
+ strcat(idstring0,idstring2[i]);
335
+ }
336
+ initscr();
337
+ noecho();
338
+ raw();
339
+ getmaxyx(stdscr,y,x);
340
+ new_lines(idstring0);
341
+ new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)");
342
+ new_lines("[i] /l list (list friends), /n nick (to change nickname), /q (to quit)");
343
+ strcpy(line, "");
344
+ IP_Port bootstrap_ip_port;
345
+ bootstrap_ip_port.port = htons(atoi(argv[2]));
346
+ int resolved_address = resolve_addr(argv[1]);
347
+ if (resolved_address != -1)
348
+ bootstrap_ip_port.ip.i = resolved_address;
349
+ else
350
+ exit(1);
351
+
352
+ DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
353
+ nodelay(stdscr, TRUE);
354
+ while(true) {
355
+ if (on == 0 && DHT_isconnected()) {
356
+ new_lines("[i] connected to DHT\n[i] define username with /n");
357
+ on = 1;
358
+ }
359
+
360
+ doMessenger();
361
+ c_sleep(1);
362
+ do_refresh();
363
+
364
+ c = getch();
365
+
366
+ if (c == ERR || c == 27)
367
+ continue;
368
+
369
+ getmaxyx(stdscr, y, x);
370
+ if (c == '\n') {
371
+ line_eval(lines, line);
372
+ strcpy(line, "");
373
+ } else if (c == 127) {
374
+ line[strlen(line) - 1] = '\0';
375
+ } else if (isalnum(c) || ispunct(c) || c == ' ') {
376
+ strcpy(line, appender(line, (char) c));
377
+ }
378
+ }
379
+ endwin();
380
+ return 0;
381
+ }
@@ -0,0 +1,50 @@
1
+ /* nTox.h
2
+ *
3
+ *Textual frontend for Tox.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
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/>.
21
+ *
22
+ */
23
+
24
+ #ifndef NTOX_H
25
+ #define NTOX_H
26
+
27
+ #include <stdio.h>
28
+ #include <stdlib.h>
29
+ #include <string.h>
30
+ #include <ncurses.h>
31
+ #include <curses.h>
32
+ #include <ctype.h>
33
+ #include <sys/socket.h>
34
+ #include <netinet/in.h>
35
+ #include <arpa/inet.h>
36
+ #include <sys/types.h>
37
+ #include <netdb.h>
38
+ #include "../core/Messenger.h"
39
+ #include "../core/network.h"
40
+ #define STRING_LENGTH 256
41
+ #define HISTORY 50
42
+
43
+ void new_lines(char *line);
44
+ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line);
45
+ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
46
+ int count_lines(char *string) ;
47
+ char *appender(char *str, const char c);
48
+ void do_refresh();
49
+
50
+ #endif