1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:43:01 +01:00

store messenger when friends are added

This commit is contained in:
Michael Rose 2013-08-13 22:07:46 +02:00
parent 2857e6af8f
commit 1158930641

113
main.c
View File

@ -30,6 +30,8 @@ extern void disable_chatwin(int f_num);
extern int add_req(uint8_t *public_key); // XXX extern int add_req(uint8_t *public_key); // XXX
extern unsigned char *hex_string_to_bin(char hex_string[]); extern unsigned char *hex_string_to_bin(char hex_string[]);
static int store_data(char*);
/* Holds status of chat windows */ /* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; char WINDOW_STATUS[MAX_WINDOW_SLOTS];
@ -41,6 +43,7 @@ static ToxWindow windows[MAX_WINDOW_SLOTS];
static ToxWindow* prompt; static ToxWindow* prompt;
static Messenger *m; static Messenger *m;
static char *DATA_FILE;
int w_num; int w_num;
int active_window; int active_window;
@ -105,7 +108,11 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
void on_friendadded(int friendnumber) void on_friendadded(int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
int st;
if ((st = store_data(DATA_FILE)) != 0) {
wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st);
}
} }
/* CALLBACKS END */ /* CALLBACKS END */
@ -279,61 +286,76 @@ static void do_tox()
doMessenger(m); doMessenger(m);
} }
static void load_data(char *path) /*
* Store Messenger data to path
* Return 0 Messenger stored successfully
* Return 1 malloc failed
* Return 2 fopen failed
* Return 3 fwrite failed
*/
static int store_data(char *path)
{ {
FILE *fd; FILE *fd;
size_t len; size_t len;
uint8_t *buf; uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
fclose(fd);
endwin();
exit(1);
}
if (fread(buf, len, 1, fd) != 1){
fprintf(stderr, "fread() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
Messenger_load(m, buf, len);
}
else {
len = Messenger_size(m); len = Messenger_size(m);
buf = malloc(len); buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n"); return 1;
endwin();
exit(1);
} }
Messenger_save(m, buf); Messenger_save(m, buf);
fd = fopen(path, "w"); fd = fopen(path, "w");
if (fd == NULL) { if (fd == NULL) {
fprintf(stderr, "fopen() failed.\n"); return 2;
free(buf);
endwin();
exit(1);
} }
if (fwrite(buf, len, 1, fd) != 1){ if (fwrite(buf, len, 1, fd) != 1) {
fprintf(stderr, "fwrite() failed.\n"); return 3;
free(buf); }
fclose(fd);
endwin(); free(buf);
exit(1); fclose(fd);
return 0;
}
static void load_data(char *path) {
FILE *fd;
size_t len;
uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
fclose(fd);
endwin();
exit(1);
}
if (fread(buf, len, 1, fd) != 1) {
fprintf(stderr, "fread() failed.\n");
free(buf);
fclose(fd);
endwin();
exit(1);
}
Messenger_load(m, buf, len);
free(buf);
fclose(fd);
} else {
int st;
if ((st = store_data(path)) != 0) {
fprintf(stderr, "storing messenger failed with error code: %d", st);
endwin();
exit(1);
}
} }
}
free(buf);
fclose(fd);
} }
static void draw_bar() static void draw_bar()
@ -419,7 +441,6 @@ int main(int argc, char *argv[])
int ch; int ch;
ToxWindow* a; ToxWindow* a;
char *user_config_dir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
char *DATA_FILE;
int config_err = create_user_config_dir(user_config_dir); int config_err = create_user_config_dir(user_config_dir);
if(config_err) { if(config_err) {
DATA_FILE = "data"; DATA_FILE = "data";
@ -457,7 +478,6 @@ int main(int argc, char *argv[])
if(f_loadfromfile) if(f_loadfromfile)
load_data(DATA_FILE); load_data(DATA_FILE);
free(DATA_FILE);
if (f_flag == -1) { if (f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD); attron(COLOR_PAIR(3) | A_BOLD);
@ -490,6 +510,9 @@ int main(int argc, char *argv[])
else if (ch != ERR) else if (ch != ERR)
a->onKey(a, m, ch); a->onKey(a, m, ch);
} }
cleanupMessenger(m); cleanupMessenger(m);
free(DATA_FILE);
return 0; return 0;
} }