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) {
printf("Error while reading the key file\nExiting.\n");
exit(1);
} else {
printf("Keys loaded successfully\n");
}
load_keys(keys);
printf("Keys loaded successfully\n");
} else {
//otherwise save new keys
new_keys();
@ -60,9 +59,8 @@ void manage_keys()
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
printf("Error while writing the key file.\nExiting.\n");
exit(1);
} else {
printf("Keys saved successfully\n");
}
printf("Keys saved successfully\n");
}
fclose(keys_file);
@ -80,15 +78,15 @@ int main(int argc, char *argv[])
printf("%hhX",self_public_key[i]);
}
printf("\n");
printf("Port: %u\n", PORT);
printf("Port: %u\n", PORT);
//initialize networking
//bind to ip 0.0.0.0:PORT
IP ip;
ip.i = 0;
init_networking(ip, PORT);
perror("Initialization");
if (argc > 3) {
printf("Trying to bootstrap into the network...\n");
IP_Port bootstrap_info;
@ -102,7 +100,7 @@ int main(int argc, char *argv[])
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
int is_waiting_for_dht_connection = 1;
while(1)
{
@ -112,7 +110,7 @@ int main(int argc, char *argv[])
is_waiting_for_dht_connection = 0;
}
doDHT();
while(receivepacket(&ip_port, data, &length) != -1)
{
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);
new_lines(numstring);
} else if (line[1] == 'q') { //exit
endwin();
exit(EXIT_SUCCESS);
endwin();
exit(EXIT_SUCCESS);
}
} else {
//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)
{
char *name = malloc(MAX_NAME_LENGTH);
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
char msg[100+length+strlen(name)+1];
time_t rawtime;
@ -195,51 +195,47 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
int len = strlen(temp);
temp[len-1]='\0';
sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this
free(name);
new_lines(msg);
}
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);
char msg[100+length];
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
free(name);
new_lines(msg);
}
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);
char msg[100+length+strlen(name)+1];
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
free(name);
new_lines(msg);
}
int load_key(){
void load_key(){
FILE *data_file = NULL;
if ((data_file = fopen("data","r"))) {
//load keys
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("Error reading file\n");
exit(0);
}
Messenger_load(data, size);
} else {
//else save new keys
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("Error writing file\n");
exit(0);
}
}
//load keys
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("Error reading data file\nExiting.\n");
exit(1);
}
Messenger_load(data, size);
} else {
//else save new keys
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("Error writing data file\nExiting.\n");
exit(1);
}
}
fclose(data_file);
return 0;
}
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]));
nodelay(stdscr, TRUE);
while(true) {
c=getch();
if (c != ERR) {
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 (on == 0 && DHT_isconnected())
{
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();
c_sleep(1);
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();
return 0;