1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* nTox_win32.c
*
* Textual frontend for Tox - Windows version
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "nTox_win32.h"
#include "misc_tools.h"
#include <process.h>
uint8_t pending_requests[256][CLIENT_ID_SIZE];
uint8_t num_requests = 0;
uint32_t maxnumfriends;
char line[STRING_LENGTH];
char users_id[200];
void do_header()
{
system("cls");
printf(users_id);
printf("\n---------------------------------");
printf("\n[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status), /n nick (to change nickname), /l (lists friends), /d friendnumber (deletes friend), /q (to quit), /r (reset screen)");
printf("\n---------------------------------");
}
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
{
printf("\n\n[i] received friend request with message\n");
printf((char *)data);
char numchar[100];
sprintf(numchar, "\n\n[i] accept request with /a %u\n\n", num_requests);
printf(numchar);
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
++num_requests;
}
void print_message(int friendnumber, uint8_t * string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1];
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
char* temp = asctime(timeinfo);
size_t len = strlen(temp);
temp[len-1]='\0';
sprintf(msg, "\n[%d] %s <%s> %s\n\n", friendnumber, temp, name, string); // timestamp
printf(msg);
}
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length];
sprintf(msg, "\n\n[i] [%d] %s is now known as %s.\n\n", friendnumber, name, string);
printf(msg);
}
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1];
sprintf(msg, "\n\n[i] [%d] %s's status changed to %s.\n\n", friendnumber, name, string);
printf(msg);
}
void load_key()
{
FILE *data_file = NULL;
data_file = fopen("data","r");
if (data_file) {
fseek(data_file, 0, SEEK_END);
int size = ftell(data_file);
fseek(data_file, 0, SEEK_SET);
uint8_t data[size];
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not read the data file. Exiting.");
exit(1);
}
Messenger_load(data, size);
} else {
int size = Messenger_size();
uint8_t data[size];
Messenger_save(data);
data_file = fopen("data", "w");
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
printf("\n[i] Could not write data to file. Exiting.");
exit(1);
}
}
fclose(data_file);
}
void add_friend()
{
int i;
char temp_id[128];
for (i = 0; i < 128; i++)
temp_id[i] = line[i+3];
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
if (num >= 0) {
char numstring[100];
sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num);
printf(numstring);
++maxnumfriends;
}
else if (num == -1)
printf("\n[i] Message is too long.\n\n");
else if (num == -2)
printf("\n[i] Please add a message to your friend request.\n\n");
else if (num == -3)
printf("\n[i] That appears to be your own ID.\n\n");
else if (num == -4)
printf("\n[i] Friend request already sent.\n\n");
else if (num == -5)
printf("\n[i] Undefined error when adding friend\n\n");
}
void list_friends()
{
int activefriends = 0;
int i;
for (i = 0; i <= maxnumfriends; i++) {
if (m_friendstatus(i) == 4)
activefriends++;
}
printf("\n[i] Friend List | Total: %d\n\n", activefriends);
for (i = 0; i <= 256; i++) {/* TODO: fix this properly*/
char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name);
if (m_friendstatus(i) == 4)
printf("[%d] %s\n\n", i, (uint8_t*)name);
}
}
void delete_friend()
{
size_t len = strlen(line);
char numstring[len-3];
int i;
for (i = 0; i < len; i++) {
if (line[i+3] != ' ')
numstring[i] = line[i+3];
}
int num = atoi(numstring);
m_delfriend(num);
--maxnumfriends;
printf("\n\n");
}
void message_friend()
{
size_t len = strlen(line);
char numstring[len-3];
char message[len-3];
int i;
for (i = 0; i < len; i++) {
if (line[i+3] != ' ')
numstring[i] = line[i+3];
else {
int j;
for (j = (i+1); j < len; j++)
message[j-i-1] = line[j+3];
break;
}
}
int num = atoi(numstring);
if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1)
printf("\n[i] could not send message (they may be offline): %s\n", message);
else
printf("\n");
}
void change_nickname()
{
uint8_t name[MAX_NAME_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
break;
name[i-3] = line[i];
}
name[i-3] = 0;
setname(name, i);
char numstring[100];
sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name);
printf(numstring);
FILE *name_file = NULL;
name_file = fopen("namefile.txt", "w");
fprintf(name_file, "%s", (char*)name);
fclose(name_file);
}
void change_status()
{
uint8_t status[MAX_USERSTATUS_LENGTH];
int i = 0;
size_t len = strlen(line);
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
break;
status[i-3] = line[i];
}
status[i-3] = 0;
m_set_userstatus(status, strlen((char*)status));
char numstring[100];
sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
printf(numstring);
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "w");
fprintf(status_file, "%s", (char*)status);
fclose(status_file);
}
void accept_friend_request()
{
uint8_t numf = atoi(line + 3);
char numchar[100];
sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
printf(numchar);
int num = m_addfriend_norequest(pending_requests[numf]);
sprintf(numchar, "\n[i] added friendnumber %d\n\n", num);
printf(numchar);
++maxnumfriends;
}
void line_eval(char* line)
{
if(line[0] == '/') {
char inpt_command = line[1];
if(inpt_command == 'f') {
add_friend(line);
}
else if (inpt_command == 'r') {
do_header();
printf("\n\n");
}
else if (inpt_command == 'l') {
list_friends(line);
}
else if (inpt_command == 'd') {
delete_friend(line);
}
/* Send message to friend */
else if (inpt_command == 'm') {
message_friend(line);
}
else if (inpt_command == 'n') {
change_nickname(line);
}
else if (inpt_command == 's') {
change_status(line);
}
else if (inpt_command == 'a') {
accept_friend_request(line);
}
/* EXIT */
else if (inpt_command == 'q') {
uint8_t status[MAX_USERSTATUS_LENGTH] = "Offline";
m_set_userstatus(status, strlen((char*)status));
exit(EXIT_SUCCESS);
}
}
}
void get_input()
{
while(1) {
fgets(line, STRING_LENGTH, stdin);
line_eval(line);
strcpy(line, "");
}
}
int main(int argc, char *argv[])
{
if (argc < 4) {
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
exit(0);
}
if (initMessenger() == -1) {
printf("initMessenger failed");
exit(0);
}
if (argc > 4) {
if(strncmp(argv[4], "nokey", 6) < 0) {
}
} else {
load_key();
}
int nameloaded = 0;
int statusloaded = 0;
FILE* name_file = NULL;
name_file = fopen("namefile.txt", "r");
if(name_file) {
uint8_t name[MAX_NAME_LENGTH];
while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
sscanf(line, "%s", (char*)name);
}
setname(name, strlen((char*)name)+1);
nameloaded = 1;
printf("%s\n", name);
}
fclose(name_file);
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "r");
if(status_file) {
uint8_t status[MAX_USERSTATUS_LENGTH];
while (fgets(line, MAX_USERSTATUS_LENGTH, status_file) != NULL) {
sscanf(line, "%s", (char*)status);
}
m_set_userstatus(status, strlen((char*)status)+1);
statusloaded = 1;
printf("%s\n", status);
}
fclose(status_file);
m_callback_friendrequest(print_request);
m_callback_friendmessage(print_message);
m_callback_namechange(print_nickchange);
m_callback_userstatus(print_statuschange);
char idstring1[PUB_KEY_BYTES][5];
char idstring2[PUB_KEY_BYTES][5];
int i;
for(i = 0; i < PUB_KEY_BYTES; i++)
{
if(self_public_key[i] < (PUB_KEY_BYTES/2))
strcpy(idstring1[i],"0");
else
strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",self_public_key[i]);
}
strcpy(users_id,"[i] your ID: ");
int j;
for (j = 0; j < PUB_KEY_BYTES; j++) {
strcat(users_id,idstring1[j]);
strcat(users_id,idstring2[j]);
}
do_header();
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
int c;
int on = 0;
_beginthread(get_input, 0, NULL);
if (nameloaded == 1) {
printf("\nNickname automatically loaded");
printf("\n---------------------------------");
}
if (statusloaded == 1) {
printf("\nStatus automatically loaded");
printf("\n---------------------------------");
}
while(1) {
if (on == 1 && DHT_isconnected() == -1) {
printf("\n---------------------------------");
printf("\n[i] Disconnected from the DHT");
printf("\n---------------------------------\n\n");
on = 0;
}
if (on == 0 && DHT_isconnected()) {
printf("\n[i] Connected to DHT");
printf("\n---------------------------------\n\n");
on = 1;
}
doMessenger();
Sleep(1);
}
return 0;
}
|