Merge pull request #577 from FullName/nTox

nTox: Save data on friend operations/exit
This commit is contained in:
irungentoo 2013-09-15 15:58:18 -07:00
commit 7a69f2de28

View File

@ -208,6 +208,9 @@ char *format_message(Tox *m, char *message, int friendnum)
return msg;
}
/* forward declaration */
static int save_data(Tox *m);
void line_eval(Tox *m, char *line)
{
if (line[0] == '/') {
@ -251,7 +254,12 @@ void line_eval(Tox *m, char *line)
break;
default:
sprintf(numstring, "[i] Added friend as %d.", num);
if (num >= 0) {
sprintf(numstring, "[i] Added friend as %d.", num);
save_data(m);
}
else
sprintf(numstring, "[i] Unknown error %i.", num);
break;
}
@ -339,6 +347,7 @@ void line_eval(Tox *m, char *line)
new_lines(numchar);
sprintf(numchar, "[i] added friendnumber %d", num);
new_lines(numchar);
save_data(m);
} else {
sprintf(numchar, "[i] failed to add friend");
new_lines(numchar);
@ -374,8 +383,8 @@ void line_eval(Tox *m, char *line)
tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1));
new_lines(msg);
}
} else if (inpt_command == 'q') { //exit
save_data(m);
endwin();
exit(EXIT_SUCCESS);
} else {
@ -497,55 +506,70 @@ void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t leng
}
}
void load_key(Tox *m, char *path)
{
FILE *data_file = fopen(path, "r");
int size = 0;
static char *data_file_name = NULL;
static int load_data(Tox *m)
{
FILE *data_file = fopen(data_file_name, "r");
int size = 0;
if (data_file) {
//load keys
fseek(data_file, 0, SEEK_END);
size = ftell(data_file);
rewind(data_file);
uint8_t data[size];
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
fputs("[!] could not read data file! exiting...\n", stderr);
goto FILE_ERROR;
fputs("[!] could not read data file!\n", stderr);
return 0;
}
tox_load(m, data, size);
} else {
//else save new keys
int size = tox_size(m);
uint8_t data[size];
tox_save(m, data);
data_file = fopen(path, "w");
if (!data_file) {
perror("[!] load_key");
exit(1);
if (fclose(data_file) < 0) {
perror("[!] fclose failed");
/* we got it open and the expected data read... let it be ok */
/* return 0; */
}
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
fputs("[!] could not write data file! exiting...", stderr);
goto FILE_ERROR;
}
return 1;
}
if (fclose(data_file) < 0)
perror("[!] fclose failed");
return 0;
}
return;
static int save_data(Tox *m)
{
FILE *data_file = fopen(data_file_name, "w");
if (!data_file) {
perror("[!] load_key");
return 0;
}
FILE_ERROR:
int size = tox_size(m);
uint8_t data[size];
tox_save(m, data);
if (fclose(data_file) < 0)
perror("[!] fclose failed");
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
fputs("[!] could not write data file (1)!", stderr);
return 0;
}
exit(1);
if (fclose(data_file) < 0) {
perror("[!] could not write data file (2)");
return 0;
}
}
static int load_data_or_init(Tox *m, char *path)
{
data_file_name = path;
if (load_data(m))
return 1;
if (save_data(m))
return 1;
return 0;
}
void print_help(void)
@ -610,7 +634,7 @@ int main(int argc, char *argv[])
exit(0);
}
load_key(m, filename);
load_data_or_init(m, filename);
tox_callback_friendrequest(m, print_request, NULL);
tox_callback_friendmessage(m, print_message, NULL);
@ -642,9 +666,18 @@ int main(int argc, char *argv[])
nodelay(stdscr, TRUE);
new_lines("[i] change username with /n");
char name[TOX_MAX_NAME_LENGTH];
uint16_t namelen = tox_getselfname(m, name, sizeof(name));
if (namelen > 0) {
char whoami[128 + TOX_MAX_NAME_LENGTH];
snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name);
new_lines(whoami);
}
while (1) {
if (on == 0 && tox_isconnected(m)) {
new_lines("[i] connected to DHT\n[i] define username with /n");
new_lines("[i] connected to DHT");
on = 1;
}