summaryrefslogtreecommitdiff
path: root/testing/nTox.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-31 10:25:29 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-31 10:25:29 -0400
commitbdd13d1954e0befb1682cf1f6727e9b382450ed8 (patch)
tree61ab2649c5515c1f0bd8b3506fb7fd7be7d15737 /testing/nTox.c
parent55a2e59a64d879440667b9d672a3878fa7fe5d2e (diff)
parent17e64b3ee50605cbc15f195f770dacf31039e121 (diff)
Merge branch 'master' of https://github.com/JFreegman/ProjectTox-Core into JFreegman-master
Conflicts: testing/nTox.c
Diffstat (limited to 'testing/nTox.c')
-rw-r--r--testing/nTox.c164
1 files changed, 86 insertions, 78 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
index 6aef1d7b..29726a3a 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -23,9 +23,9 @@
23#include "nTox.h" 23#include "nTox.h"
24#include "misc_tools.h" 24#include "misc_tools.h"
25 25
26
27#include <stdio.h> 26#include <stdio.h>
28#include <time.h> 27#include <time.h>
28
29#ifdef WIN32 29#ifdef WIN32
30#define c_sleep(x) Sleep(1*x) 30#define c_sleep(x) Sleep(1*x)
31#else 31#else
@@ -35,20 +35,22 @@
35 35
36char lines[HISTORY][STRING_LENGTH]; 36char lines[HISTORY][STRING_LENGTH];
37char line[STRING_LENGTH]; 37char line[STRING_LENGTH];
38
38char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n" 39char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n"
39 "[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"; 40 "[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)";
40int x,y; 41int x, y;
42
41 43
42uint8_t pending_requests[256][CLIENT_ID_SIZE]; 44uint8_t pending_requests[256][CLIENT_ID_SIZE];
43uint8_t num_requests; 45uint8_t num_requests = 0;
44 46
45void new_lines(char *line) 47void new_lines(char *line)
46{ 48{
47 int i; 49 int i;
48 for (i = HISTORY-1; i > 0; i--) 50 for (i = HISTORY-1; i > 0; i--)
49 strcpy(lines[i],lines[i-1]); 51 strcpy(lines[i], lines[i-1]);
50 52
51 strcpy(lines[0],line); 53 strcpy(lines[0], line);
52 do_refresh(); 54 do_refresh();
53} 55}
54 56
@@ -56,10 +58,10 @@ void new_lines(char *line)
56void print_friendlist() 58void print_friendlist()
57{ 59{
58 char name[MAX_NAME_LENGTH]; 60 char name[MAX_NAME_LENGTH];
59 uint32_t i;
60
61 new_lines("[i] Friend List:"); 61 new_lines("[i] Friend List:");
62 for (i=0; i <= num_requests; i++) { 62 uint32_t i;
63 for (i = 0; i <= num_requests; i++) {
64 printf ("num_resusts: %d\n", num_requests);
63 char fstring[128]; 65 char fstring[128];
64 66
65 getname(i, (uint8_t*)name); 67 getname(i, (uint8_t*)name);
@@ -75,19 +77,20 @@ void print_friendlist()
75char *format_message(char *message, int friendnum) 77char *format_message(char *message, int friendnum)
76{ 78{
77 char name[MAX_NAME_LENGTH]; 79 char name[MAX_NAME_LENGTH];
78 if(friendnum != -1) { 80 if (friendnum != -1) {
79 getname(friendnum, (uint8_t*)name); 81 getname(friendnum, (uint8_t*)name);
80 } else { 82 } else {
81 getself_name((uint8_t*)name); 83 getself_name((uint8_t*)name);
82 } 84 }
83 char *msg = malloc(100+strlen(message)+strlen(name)+1); 85 char *msg = malloc(100+strlen(message)+strlen(name)+1);
86
84 time_t rawtime; 87 time_t rawtime;
85 struct tm * timeinfo; 88 struct tm * timeinfo;
86 time ( &rawtime ); 89 time ( &rawtime );
87 timeinfo = localtime ( &rawtime ); 90 timeinfo = localtime ( &rawtime );
88 char* time = asctime(timeinfo); 91 char* time = asctime(timeinfo);
89 size_t len = strlen(time); 92 size_t len = strlen(time);
90 time[len-1]='\0'; 93 time[len-1] = '\0';
91 sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); // timestamp 94 sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); // timestamp
92 return msg; 95 return msg;
93} 96}
@@ -95,13 +98,14 @@ char *format_message(char *message, int friendnum)
95void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) 98void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
96{ 99{
97 if (line[0] == '/') { 100 if (line[0] == '/') {
98 char command[STRING_LENGTH + 2] = "> "; 101 char inpt_command = line[1];
99 strcat(command, line); 102 char prompt[STRING_LENGTH + 2] = "> ";
100 new_lines(command); 103 strcat(prompt, line);
101 if (line[1] == 'f') { // add friend command: /f ID 104 new_lines(prompt);
105 if (inpt_command == 'f') { // add friend command: /f ID
102 int i; 106 int i;
103 char temp_id[128]; 107 char temp_id[128];
104 for (i=0; i<128; i++) 108 for (i = 0; i < 128; i++)
105 temp_id[i] = line[i+3]; 109 temp_id[i] = line[i+3];
106 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 110 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
107 char numstring[100]; 111 char numstring[100];
@@ -109,63 +113,63 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
109 new_lines(numstring); 113 new_lines(numstring);
110 do_refresh(); 114 do_refresh();
111 } 115 }
112 else if (line[1] == 'd') { 116 else if (inpt_command == 'd') {
113 doMessenger(); 117 doMessenger();
114 } 118 }
115 else if (line[1] == 'm') { //message command: /m friendnumber messsage 119 else if (inpt_command == 'm') { //message command: /m friendnumber messsage
116 int i;
117 size_t len = strlen(line); 120 size_t len = strlen(line);
118 char numstring[len-3]; 121 char numstring[len-3];
119 char message[len-3]; 122 char message[len-3];
120 for (i=0; i<len; i++) { 123 int i;
124 for (i = 0; i < len; i++) {
121 if (line[i+3] != ' ') { 125 if (line[i+3] != ' ') {
122 numstring[i] = line[i+3]; 126 numstring[i] = line[i+3];
123 } else { 127 } else {
124 int j; 128 int j;
125 for (j=i+1; j<len; j++) 129 for (j = (i+1); j < len; j++)
126 message[j-i-1] = line[j+3]; 130 message[j-i-1] = line[j+3];
127 break; 131 break;
128 } 132 }
129 } 133 }
130 int num = atoi(numstring); 134 int num = atoi(numstring);
131 if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { 135 if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
132 new_lines("[i] could not send message"); 136 new_lines("[i] could not send message");
133 } else { 137 } else {
134 new_lines(format_message(message, -1)); 138 new_lines(format_message(message, -1));
135 } 139 }
136 } 140 }
137 else if (line[1] == 'n') { 141 else if (inpt_command == 'n') {
138 uint8_t name[MAX_NAME_LENGTH]; 142 uint8_t name[MAX_NAME_LENGTH];
139 int i = 0; 143 int i = 0;
140 size_t len = strlen(line); 144 size_t len = strlen(line);
141 for (i=3; i<len; i++) { 145 for (i = 3; i < len; i++) {
142 if (line[i] == 0 || line[i] == '\n') break; 146 if (line[i] == 0 || line[i] == '\n') break;
143 name[i - 3] = line[i]; 147 name[i-3] = line[i];
144 } 148 }
145 name[i - 3] = 0; 149 name[i-3] = 0;
146 setname(name, i); 150 setname(name, i);
147 char numstring[100]; 151 char numstring[100];
148 sprintf(numstring, "[i] changed nick to %s", (char*)name); 152 sprintf(numstring, "[i] changed nick to %s", (char*)name);
149 new_lines(numstring); 153 new_lines(numstring);
150 } 154 }
151 else if (line[1] == 'l') { 155 else if (inpt_command == 'l') {
152 print_friendlist(); 156 print_friendlist();
153 } 157 }
154 else if (line[1] == 's') { 158 else if (inpt_command == 's') {
155 uint8_t status[MAX_USERSTATUS_LENGTH]; 159 uint8_t status[MAX_USERSTATUS_LENGTH];
156 int i = 0; 160 int i = 0;
157 size_t len = strlen(line); 161 size_t len = strlen(line);
158 for (i=3; i<len; i++) { 162 for (i = 3; i < len; i++) {
159 if (line[i] == 0 || line[i] == '\n') break; 163 if (line[i] == 0 || line[i] == '\n') break;
160 status[i - 3] = line[i]; 164 status[i-3] = line[i];
161 } 165 }
162 status[i - 3] = 0; 166 status[i-3] = 0;
163 m_set_userstatus(status, strlen((char*)status)); 167 m_set_userstatus(status, strlen((char*)status));
164 char numstring[100]; 168 char numstring[100];
165 sprintf(numstring, "[i] changed status to %s", (char*)status); 169 sprintf(numstring, "[i] changed status to %s", (char*)status);
166 new_lines(numstring); 170 new_lines(numstring);
167 } 171 }
168 else if (line[1] == 'a') { 172 else if (inpt_command == 'a') {
169 uint8_t numf = atoi(line + 3); 173 uint8_t numf = atoi(line + 3);
170 char numchar[100]; 174 char numchar[100];
171 sprintf(numchar, "[i] friend request %u accepted", numf); 175 sprintf(numchar, "[i] friend request %u accepted", numf);
@@ -176,34 +180,34 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
176 do_refresh(); 180 do_refresh();
177 181
178 } 182 }
179 183 else if (inpt_command == 'h') { //help
180 else if (line[1] == 'h') { //help 184 new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)");
181 new_lines(help); 185 new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
182 } 186 }
183 187 else if (inpt_command == 'i') { //info
184 else if (line[1] == 'i') { //info
185 char idstring0[200]; 188 char idstring0[200];
186 char idstring1[32][5]; 189 char idstring1[PUB_KEY_BYTES][5];
187 char idstring2[32][5]; 190 char idstring2[PUB_KEY_BYTES][5];
188 uint32_t i; 191 int i;
189 for(i = 0; i < 32; i++) 192 for (i = 0; i < PUB_KEY_BYTES; i++)
190 { 193 {
191 if(self_public_key[i] < 16) 194 if (self_public_key[i] < (PUB_KEY_BYTES/2))
192 strcpy(idstring1[i],"0"); 195 strcpy(idstring1[i],"0");
193 else 196 else
194 strcpy(idstring1[i], ""); 197 strcpy(idstring1[i], "");
195 sprintf(idstring2[i], "%hhX",self_public_key[i]); 198 sprintf(idstring2[i], "%hhX", self_public_key[i]);
196 } 199 }
197 // 200 //
198 strcpy(idstring0,"[i] ID: "); 201 strcpy(idstring0,"[i] ID: ");
199 for (i=0; i<32; i++) { 202 int j;
200 strcat(idstring0,idstring1[i]); 203 for (j = 0; j < PUB_KEY_BYTES; j++) {
201 strcat(idstring0,idstring2[i]); 204 strcat(idstring0,idstring1[j]);
205 strcat(idstring0,idstring2[j]);
202 } 206 }
203 new_lines(idstring0); 207 new_lines(idstring0);
204 } 208 }
205 209
206 else if (line[1] == 'q') { //exit 210 else if (inpt_command == 'q') { //exit
207 endwin(); 211 endwin();
208 exit(EXIT_SUCCESS); 212 exit(EXIT_SUCCESS);
209 } else { 213 } else {
@@ -217,9 +221,9 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
217 221
218void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) 222void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
219{ 223{
220 int i = 0;
221 strcpy(output,input); 224 strcpy(output,input);
222 size_t len = strlen(output); 225 size_t len = strlen(output);
226 int i = 0;
223 for (i = line_width; i < len; i = i + line_width) { 227 for (i = line_width; i < len; i = i + line_width) {
224 while (output[i] != ' ' && i != 0) { 228 while (output[i] != ' ' && i != 0) {
225 i--; 229 i--;
@@ -233,9 +237,9 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
233int count_lines(char *string) 237int count_lines(char *string)
234{ 238{
235 size_t len = strlen(string); 239 size_t len = strlen(string);
236 int i;
237 int count = 1; 240 int count = 1;
238 for (i=0; i < len; i++) { 241 int i;
242 for (i = 0; i < len; i++) {
239 if (string[i] == '\n') 243 if (string[i] == '\n')
240 count++; 244 count++;
241 } 245 }
@@ -246,7 +250,7 @@ char *appender(char *str, const char c)
246{ 250{
247 size_t len = strlen(str); 251 size_t len = strlen(str);
248 if (len < STRING_LENGTH) { 252 if (len < STRING_LENGTH) {
249 str[len + 1] = str[len]; 253 str[len+1] = str[len];
250 str[len] = c; 254 str[len] = c;
251 } 255 }
252 return str; 256 return str;
@@ -254,21 +258,21 @@ char *appender(char *str, const char c)
254 258
255void do_refresh() 259void do_refresh()
256{ 260{
257 int i;
258 int count=0; 261 int count=0;
259 int l;
260 char wrap_output[STRING_LENGTH]; 262 char wrap_output[STRING_LENGTH];
261 for (i=0; i<HISTORY; i++) { 263 int L;
264 int i;
265 for (i = 0; i < HISTORY; i++) {
262 wrap(wrap_output, lines[i], x); 266 wrap(wrap_output, lines[i], x);
263 l = count_lines(wrap_output); 267 L = count_lines(wrap_output);
264 count = count + l; 268 count = count + L;
265 if (count < y) { 269 if (count < y) {
266 move(y-1-count,0); 270 move(y-1-count, 0);
267 printw(wrap_output); 271 printw(wrap_output);
268 clrtoeol(); 272 clrtoeol();
269 } 273 }
270 } 274 }
271 move(y-1,0); 275 move(y-1, 0);
272 clrtoeol(); 276 clrtoeol();
273 printw(">> "); 277 printw(">> ");
274 printw(line); 278 printw(line);
@@ -299,12 +303,13 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
299 timeinfo = localtime ( &rawtime ); 303 timeinfo = localtime ( &rawtime );
300 char* temp = asctime(timeinfo); 304 char* temp = asctime(timeinfo);
301 size_t len = strlen(temp); 305 size_t len = strlen(temp);
302 temp[len-1]='\0'; 306 temp[len-1] = '\0';
303 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp 307 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp
304 new_lines(format_message((char*)string, friendnumber)); 308 new_lines(format_message((char*)string, friendnumber));
305} 309}
306 310
307void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { 311void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
312{
308 char name[MAX_NAME_LENGTH]; 313 char name[MAX_NAME_LENGTH];
309 getname(friendnumber, (uint8_t*)name); 314 getname(friendnumber, (uint8_t*)name);
310 char msg[100+length]; 315 char msg[100+length];
@@ -312,7 +317,8 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
312 new_lines(msg); 317 new_lines(msg);
313} 318}
314 319
315void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { 320void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
321{
316 char name[MAX_NAME_LENGTH]; 322 char name[MAX_NAME_LENGTH];
317 getname(friendnumber, (uint8_t*)name); 323 getname(friendnumber, (uint8_t*)name);
318 char msg[100+length+strlen(name)+1]; 324 char msg[100+length+strlen(name)+1];
@@ -320,15 +326,17 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
320 new_lines(msg); 326 new_lines(msg);
321} 327}
322 328
323void load_key(){ 329void load_key()
330{
324 FILE *data_file = NULL; 331 FILE *data_file = NULL;
325 if ((data_file = fopen("data","r"))) { 332 data_file = fopen("data","r");
333 if (data_file) {
326 //load keys 334 //load keys
327 fseek(data_file, 0, SEEK_END); 335 fseek(data_file, 0, SEEK_END);
328 int size = ftell(data_file); 336 int size = ftell(data_file);
329 fseek(data_file, 0, SEEK_SET); 337 fseek(data_file, 0, SEEK_SET);
330 uint8_t data[size]; 338 uint8_t data[size];
331 if(fread(data, sizeof(uint8_t), size, data_file) != size){ 339 if (fread(data, sizeof(uint8_t), size, data_file) != size){
332 printf("[i] could not read data file\n[i] exiting\n"); 340 printf("[i] could not read data file\n[i] exiting\n");
333 exit(1); 341 exit(1);
334 } 342 }
@@ -339,7 +347,7 @@ void load_key(){
339 uint8_t data[size]; 347 uint8_t data[size];
340 Messenger_save(data); 348 Messenger_save(data);
341 data_file = fopen("data","w"); 349 data_file = fopen("data","w");
342 if(fwrite(data, sizeof(uint8_t), size, data_file) != size){ 350 if (fwrite(data, sizeof(uint8_t), size, data_file) != size){
343 printf("[i] could not write data file\n[i] exiting\n"); 351 printf("[i] could not write data file\n[i] exiting\n");
344 exit(1); 352 exit(1);
345 } 353 }
@@ -369,26 +377,27 @@ int main(int argc, char *argv[])
369 m_callback_namechange(print_nickchange); 377 m_callback_namechange(print_nickchange);
370 m_callback_userstatus(print_statuschange); 378 m_callback_userstatus(print_statuschange);
371 char idstring0[200]; 379 char idstring0[200];
372 char idstring1[32][5]; 380 char idstring1[PUB_KEY_BYTES][5];
373 char idstring2[32][5]; 381 char idstring2[PUB_KEY_BYTES][5];
374 uint32_t i; 382 int i;
375 for(i = 0; i < 32; i++) 383 for(i = 0; i < PUB_KEY_BYTES; i++)
376 { 384 {
377 if(self_public_key[i] < 16) 385 if (self_public_key[i] < (PUB_KEY_BYTES / 2))
378 strcpy(idstring1[i],"0"); 386 strcpy(idstring1[i],"0");
379 else 387 else
380 strcpy(idstring1[i], ""); 388 strcpy(idstring1[i], "");
381 sprintf(idstring2[i], "%hhX",self_public_key[i]); 389 sprintf(idstring2[i], "%hhX",self_public_key[i]);
382 } 390 }
383 strcpy(idstring0,"[i] your ID: "); 391 strcpy(idstring0,"[i] your ID: ");
384 for (i=0; i<32; i++) { 392 int j;
385 strcat(idstring0,idstring1[i]); 393 for (j = 0; j < PUB_KEY_BYTES; j++) {
386 strcat(idstring0,idstring2[i]); 394 strcat(idstring0,idstring1[j]);
395 strcat(idstring0,idstring2[j]);
387 } 396 }
388 initscr(); 397 initscr();
389 noecho(); 398 noecho();
390 raw(); 399 raw();
391 getmaxyx(stdscr,y,x); 400 getmaxyx(stdscr, y, x);
392 new_lines(idstring0); 401 new_lines(idstring0);
393 new_lines(help); 402 new_lines(help);
394 strcpy(line, ""); 403 strcpy(line, "");
@@ -413,7 +422,6 @@ int main(int argc, char *argv[])
413 do_refresh(); 422 do_refresh();
414 423
415 c = getch(); 424 c = getch();
416
417 if (c == ERR || c == 27) 425 if (c == ERR || c == 27)
418 continue; 426 continue;
419 427
@@ -422,7 +430,7 @@ int main(int argc, char *argv[])
422 line_eval(lines, line); 430 line_eval(lines, line);
423 strcpy(line, ""); 431 strcpy(line, "");
424 } else if (c == 127) { 432 } else if (c == 127) {
425 line[strlen(line) - 1] = '\0'; 433 line[strlen(line)-1] = '\0';
426 } else if (isalnum(c) || ispunct(c) || c == ' ') { 434 } else if (isalnum(c) || ispunct(c) || c == ' ') {
427 strcpy(line, appender(line, (char) c)); 435 strcpy(line, appender(line, (char) c));
428 } 436 }