Some cleanup

This commit is contained in:
Maxim Biro 2013-07-23 12:11:07 -04:00
parent 23f13cd95d
commit ea550d4c86
2 changed files with 54 additions and 60 deletions

View File

@ -48,10 +48,9 @@ void manage_keys()
if (read_size != KEYS_SIZE) { if (read_size != KEYS_SIZE) {
printf("Error while reading the key file\nExiting.\n"); printf("Error while reading the key file\nExiting.\n");
exit(1); exit(1);
} else {
printf("Keys loaded successfully\n");
} }
load_keys(keys); load_keys(keys);
printf("Keys loaded successfully\n");
} else { } else {
//otherwise save new keys //otherwise save new keys
new_keys(); new_keys();
@ -60,9 +59,8 @@ void manage_keys()
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) { if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
printf("Error while writing the key file.\nExiting.\n"); printf("Error while writing the key file.\nExiting.\n");
exit(1); exit(1);
} else {
printf("Keys saved successfully\n");
} }
printf("Keys saved successfully\n");
} }
fclose(keys_file); fclose(keys_file);
@ -80,15 +78,15 @@ int main(int argc, char *argv[])
printf("%hhX",self_public_key[i]); printf("%hhX",self_public_key[i]);
} }
printf("\n"); printf("\n");
printf("Port: %u\n", PORT); printf("Port: %u\n", PORT);
//initialize networking //initialize networking
//bind to ip 0.0.0.0:PORT //bind to ip 0.0.0.0:PORT
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
perror("Initialization"); perror("Initialization");
if (argc > 3) { if (argc > 3) {
printf("Trying to bootstrap into the network...\n"); printf("Trying to bootstrap into the network...\n");
IP_Port bootstrap_info; IP_Port bootstrap_info;
@ -102,7 +100,7 @@ int main(int argc, char *argv[])
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
int is_waiting_for_dht_connection = 1; int is_waiting_for_dht_connection = 1;
while(1) while(1)
{ {
@ -112,7 +110,7 @@ int main(int argc, char *argv[])
is_waiting_for_dht_connection = 0; is_waiting_for_dht_connection = 0;
} }
doDHT(); doDHT();
while(receivepacket(&ip_port, data, &length) != -1) while(receivepacket(&ip_port, data, &length) != -1)
{ {
DHT_handlepacket(data, length, ip_port); DHT_handlepacket(data, length, ip_port);

View File

@ -99,8 +99,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
sprintf(numstring, "[i] changed status to %s", (char*)status); sprintf(numstring, "[i] changed status to %s", (char*)status);
new_lines(numstring); new_lines(numstring);
} else if (line[1] == 'q') { //exit } else if (line[1] == 'q') { //exit
endwin(); endwin();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} else { } else {
//new_lines(line); //new_lines(line);
@ -184,7 +184,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
} }
void print_message(int friendnumber, uint8_t * string, uint16_t length) void print_message(int friendnumber, uint8_t * string, uint16_t length)
{ {
char *name = malloc(MAX_NAME_LENGTH); char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1]; char msg[100+length+strlen(name)+1];
time_t rawtime; time_t rawtime;
@ -195,51 +195,47 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
int len = strlen(temp); int len = strlen(temp);
temp[len-1]='\0'; temp[len-1]='\0';
sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this
free(name);
new_lines(msg); new_lines(msg);
} }
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
char *name = malloc(MAX_NAME_LENGTH); char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
char msg[100+length]; char msg[100+length];
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
free(name);
new_lines(msg); new_lines(msg);
} }
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
char *name = malloc(MAX_NAME_LENGTH); char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name); getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1]; char msg[100+length+strlen(name)+1];
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
free(name);
new_lines(msg); new_lines(msg);
} }
int load_key(){ void load_key(){
FILE *data_file = NULL; FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) { if ((data_file = fopen("data","r"))) {
//load keys //load keys
fseek(data_file, 0, SEEK_END); fseek(data_file, 0, SEEK_END);
int size = ftell(data_file); int size = ftell(data_file);
fseek(data_file, 0, SEEK_SET); fseek(data_file, 0, SEEK_SET);
uint8_t data[size]; uint8_t data[size];
if(fread(data, sizeof(uint8_t), size, data_file) != size){ if(fread(data, sizeof(uint8_t), size, data_file) != size){
printf("Error reading file\n"); printf("Error reading data file\nExiting.\n");
exit(0); exit(1);
} }
Messenger_load(data, size); Messenger_load(data, size);
} else { } else {
//else save new keys //else save new keys
int size = Messenger_size(); int size = Messenger_size();
uint8_t data[size]; uint8_t data[size];
Messenger_save(data); Messenger_save(data);
data_file = fopen("data","w"); data_file = fopen("data","w");
if(fwrite(data, sizeof(uint8_t), size, data_file) != size){ if(fwrite(data, sizeof(uint8_t), size, data_file) != size){
printf("Error writing file\n"); printf("Error writing data file\nExiting.\n");
exit(0); exit(1);
} }
} }
fclose(data_file); fclose(data_file);
return 0;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -298,31 +294,31 @@ int main(int argc, char *argv[])
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
nodelay(stdscr, TRUE); nodelay(stdscr, TRUE);
while(true) { while(true) {
c=getch();
if (c != ERR) { if (on == 0 && DHT_isconnected())
if (c != 27) {
getmaxyx(stdscr,y,x);
if (c == '\n') {
line_eval(lines, line);
strcpy(line, "");
} else if (c == 127) {
line[strlen(line)-1] = '\0';
} else if (isalnum(c) || ispunct(c) || c == ' ') {
strcpy(line,appender(line, (char) c));
}
}
}
if(on == 0)
{ {
if(DHT_isconnected()) new_lines("[i] connected to DHT\n[i] define username with /n");
{ on = 1;
new_lines("[i] connected to DHT\n[i] define username with /n");
on = 1;
}
} }
doMessenger(); doMessenger();
c_sleep(1); c_sleep(1);
do_refresh(); do_refresh();
c = getch();
if (c == ERR || c == 27)
continue;
getmaxyx(stdscr, y, x);
if (c == '\n') {
line_eval(lines, line);
strcpy(line, "");
} else if (c == 127) {
line[strlen(line) - 1] = '\0';
} else if (isalnum(c) || ispunct(c) || c == ' ') {
strcpy(line, appender(line, (char) c));
}
} }
endwin(); endwin();
return 0; return 0;