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