diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-23 10:04:05 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-23 10:04:05 -0700 |
commit | 2b8d86ebe2d193506f0439f095182e5862d545ab (patch) | |
tree | 7b902fcd20eaa969c233600c8797d9dc09f0f9dd | |
parent | 0019fe05e5c5eb81caefb6e7d9743ef500985d7c (diff) | |
parent | ea550d4c869d97c6a1b1dee8defabacd7d9d40e2 (diff) |
Merge pull request #97 from nurupo/master
Some cleanup
-rw-r--r-- | other/DHT_bootstrap.c | 16 | ||||
-rw-r--r-- | testing/nTox.c | 98 |
2 files changed, 54 insertions, 60 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index ac74d901..0bacccd9 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c | |||
@@ -48,10 +48,9 @@ void manage_keys() | |||
48 | if (read_size != KEYS_SIZE) { | 48 | if (read_size != KEYS_SIZE) { |
49 | printf("Error while reading the key file\nExiting.\n"); | 49 | printf("Error while reading the key file\nExiting.\n"); |
50 | exit(1); | 50 | exit(1); |
51 | } else { | ||
52 | printf("Keys loaded successfully\n"); | ||
53 | } | 51 | } |
54 | load_keys(keys); | 52 | load_keys(keys); |
53 | printf("Keys loaded successfully\n"); | ||
55 | } else { | 54 | } else { |
56 | //otherwise save new keys | 55 | //otherwise save new keys |
57 | new_keys(); | 56 | new_keys(); |
@@ -60,9 +59,8 @@ void manage_keys() | |||
60 | if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { | 59 | if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { |
61 | printf("Error while writing the key file.\nExiting.\n"); | 60 | printf("Error while writing the key file.\nExiting.\n"); |
62 | exit(1); | 61 | exit(1); |
63 | } else { | ||
64 | printf("Keys saved successfully\n"); | ||
65 | } | 62 | } |
63 | printf("Keys saved successfully\n"); | ||
66 | } | 64 | } |
67 | 65 | ||
68 | fclose(keys_file); | 66 | fclose(keys_file); |
@@ -80,15 +78,15 @@ int main(int argc, char *argv[]) | |||
80 | printf("%hhX",self_public_key[i]); | 78 | printf("%hhX",self_public_key[i]); |
81 | } | 79 | } |
82 | printf("\n"); | 80 | printf("\n"); |
83 | printf("Port: %u\n", PORT); | 81 | printf("Port: %u\n", PORT); |
84 | //initialize networking | 82 | //initialize networking |
85 | //bind to ip 0.0.0.0:PORT | 83 | //bind to ip 0.0.0.0:PORT |
86 | IP ip; | 84 | IP ip; |
87 | ip.i = 0; | 85 | ip.i = 0; |
88 | init_networking(ip, PORT); | 86 | init_networking(ip, PORT); |
89 | 87 | ||
90 | perror("Initialization"); | 88 | perror("Initialization"); |
91 | 89 | ||
92 | if (argc > 3) { | 90 | if (argc > 3) { |
93 | printf("Trying to bootstrap into the network...\n"); | 91 | printf("Trying to bootstrap into the network...\n"); |
94 | IP_Port bootstrap_info; | 92 | IP_Port bootstrap_info; |
@@ -102,7 +100,7 @@ int main(int argc, char *argv[]) | |||
102 | IP_Port ip_port; | 100 | IP_Port ip_port; |
103 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 101 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
104 | uint32_t length; | 102 | uint32_t length; |
105 | 103 | ||
106 | int is_waiting_for_dht_connection = 1; | 104 | int is_waiting_for_dht_connection = 1; |
107 | while(1) | 105 | while(1) |
108 | { | 106 | { |
@@ -112,7 +110,7 @@ int main(int argc, char *argv[]) | |||
112 | is_waiting_for_dht_connection = 0; | 110 | is_waiting_for_dht_connection = 0; |
113 | } | 111 | } |
114 | doDHT(); | 112 | doDHT(); |
115 | 113 | ||
116 | while(receivepacket(&ip_port, data, &length) != -1) | 114 | while(receivepacket(&ip_port, data, &length) != -1) |
117 | { | 115 | { |
118 | DHT_handlepacket(data, length, ip_port); | 116 | DHT_handlepacket(data, length, ip_port); |
diff --git a/testing/nTox.c b/testing/nTox.c index 8e9a5456..097c73af 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -99,8 +99,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
99 | sprintf(numstring, "[i] changed status to %s", (char*)status); | 99 | sprintf(numstring, "[i] changed status to %s", (char*)status); |
100 | new_lines(numstring); | 100 | new_lines(numstring); |
101 | } else if (line[1] == 'q') { //exit | 101 | } else if (line[1] == 'q') { //exit |
102 | endwin(); | 102 | endwin(); |
103 | exit(EXIT_SUCCESS); | 103 | exit(EXIT_SUCCESS); |
104 | } | 104 | } |
105 | } else { | 105 | } else { |
106 | //new_lines(line); | 106 | //new_lines(line); |
@@ -184,7 +184,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | |||
184 | } | 184 | } |
185 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | 185 | void print_message(int friendnumber, uint8_t * string, uint16_t length) |
186 | { | 186 | { |
187 | char *name = malloc(MAX_NAME_LENGTH); | 187 | char name[MAX_NAME_LENGTH]; |
188 | getname(friendnumber, (uint8_t*)name); | 188 | getname(friendnumber, (uint8_t*)name); |
189 | char msg[100+length+strlen(name)+1]; | 189 | char msg[100+length+strlen(name)+1]; |
190 | time_t rawtime; | 190 | time_t rawtime; |
@@ -195,51 +195,47 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) | |||
195 | int len = strlen(temp); | 195 | int len = strlen(temp); |
196 | temp[len-1]='\0'; | 196 | temp[len-1]='\0'; |
197 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this | 197 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this |
198 | free(name); | ||
199 | new_lines(msg); | 198 | new_lines(msg); |
200 | } | 199 | } |
201 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { | 200 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { |
202 | char *name = malloc(MAX_NAME_LENGTH); | 201 | char name[MAX_NAME_LENGTH]; |
203 | getname(friendnumber, (uint8_t*)name); | 202 | getname(friendnumber, (uint8_t*)name); |
204 | char msg[100+length]; | 203 | char msg[100+length]; |
205 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); | 204 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); |
206 | free(name); | ||
207 | new_lines(msg); | 205 | new_lines(msg); |
208 | } | 206 | } |
209 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { | 207 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { |
210 | char *name = malloc(MAX_NAME_LENGTH); | 208 | char name[MAX_NAME_LENGTH]; |
211 | getname(friendnumber, (uint8_t*)name); | 209 | getname(friendnumber, (uint8_t*)name); |
212 | char msg[100+length+strlen(name)+1]; | 210 | char msg[100+length+strlen(name)+1]; |
213 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); | 211 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); |
214 | free(name); | ||
215 | new_lines(msg); | 212 | new_lines(msg); |
216 | } | 213 | } |
217 | int load_key(){ | 214 | void load_key(){ |
218 | FILE *data_file = NULL; | 215 | FILE *data_file = NULL; |
219 | if ((data_file = fopen("data","r"))) { | 216 | if ((data_file = fopen("data","r"))) { |
220 | //load keys | 217 | //load keys |
221 | fseek(data_file, 0, SEEK_END); | 218 | fseek(data_file, 0, SEEK_END); |
222 | int size = ftell(data_file); | 219 | int size = ftell(data_file); |
223 | fseek(data_file, 0, SEEK_SET); | 220 | fseek(data_file, 0, SEEK_SET); |
224 | uint8_t data[size]; | 221 | uint8_t data[size]; |
225 | if(fread(data, sizeof(uint8_t), size, data_file) != size){ | 222 | if(fread(data, sizeof(uint8_t), size, data_file) != size){ |
226 | printf("Error reading file\n"); | 223 | printf("Error reading data file\nExiting.\n"); |
227 | exit(0); | 224 | exit(1); |
228 | } | 225 | } |
229 | Messenger_load(data, size); | 226 | Messenger_load(data, size); |
230 | } else { | 227 | } else { |
231 | //else save new keys | 228 | //else save new keys |
232 | int size = Messenger_size(); | 229 | int size = Messenger_size(); |
233 | uint8_t data[size]; | 230 | uint8_t data[size]; |
234 | Messenger_save(data); | 231 | Messenger_save(data); |
235 | data_file = fopen("data","w"); | 232 | data_file = fopen("data","w"); |
236 | if(fwrite(data, sizeof(uint8_t), size, data_file) != size){ | 233 | if(fwrite(data, sizeof(uint8_t), size, data_file) != size){ |
237 | printf("Error writing file\n"); | 234 | printf("Error writing data file\nExiting.\n"); |
238 | exit(0); | 235 | exit(1); |
239 | } | 236 | } |
240 | } | 237 | } |
241 | fclose(data_file); | 238 | fclose(data_file); |
242 | return 0; | ||
243 | } | 239 | } |
244 | int main(int argc, char *argv[]) | 240 | int main(int argc, char *argv[]) |
245 | { | 241 | { |
@@ -298,31 +294,31 @@ int main(int argc, char *argv[]) | |||
298 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 294 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
299 | nodelay(stdscr, TRUE); | 295 | nodelay(stdscr, TRUE); |
300 | while(true) { | 296 | while(true) { |
301 | c=getch(); | 297 | |
302 | if (c != ERR) { | 298 | if (on == 0 && DHT_isconnected()) |
303 | if (c != 27) { | ||
304 | getmaxyx(stdscr,y,x); | ||
305 | if (c == '\n') { | ||
306 | line_eval(lines, line); | ||
307 | strcpy(line, ""); | ||
308 | } else if (c == 127) { | ||
309 | line[strlen(line)-1] = '\0'; | ||
310 | } else if (isalnum(c) || ispunct(c) || c == ' ') { | ||
311 | strcpy(line,appender(line, (char) c)); | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | if(on == 0) | ||
316 | { | 299 | { |
317 | if(DHT_isconnected()) | 300 | new_lines("[i] connected to DHT\n[i] define username with /n"); |
318 | { | 301 | on = 1; |
319 | new_lines("[i] connected to DHT\n[i] define username with /n"); | ||
320 | on = 1; | ||
321 | } | ||
322 | } | 302 | } |
303 | |||
323 | doMessenger(); | 304 | doMessenger(); |
324 | c_sleep(1); | 305 | c_sleep(1); |
325 | do_refresh(); | 306 | do_refresh(); |
307 | |||
308 | c = getch(); | ||
309 | |||
310 | if (c == ERR || c == 27) | ||
311 | continue; | ||
312 | |||
313 | getmaxyx(stdscr, y, x); | ||
314 | if (c == '\n') { | ||
315 | line_eval(lines, line); | ||
316 | strcpy(line, ""); | ||
317 | } else if (c == 127) { | ||
318 | line[strlen(line) - 1] = '\0'; | ||
319 | } else if (isalnum(c) || ispunct(c) || c == ' ') { | ||
320 | strcpy(line, appender(line, (char) c)); | ||
321 | } | ||
326 | } | 322 | } |
327 | endwin(); | 323 | endwin(); |
328 | return 0; | 324 | return 0; |