mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-27 01:53:27 +01:00
keep chatlog names consistent with friend name changes
This commit is contained in:
parent
e574af7d68
commit
95dfea8d29
@ -37,6 +37,7 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "notify.h"
|
#include "notify.h"
|
||||||
#include "help.h"
|
#include "help.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
#ifdef AUDIO
|
#ifdef AUDIO
|
||||||
#include "audio_call.h"
|
#include "audio_call.h"
|
||||||
@ -334,12 +335,27 @@ static void friendlist_onNickChange(ToxWindow *self, Tox *m, int32_t num, const
|
|||||||
if (len > TOX_MAX_NAME_LENGTH || num >= Friends.max_idx)
|
if (len > TOX_MAX_NAME_LENGTH || num >= Friends.max_idx)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
/* save old name for log renaming */
|
||||||
|
char oldname[TOXIC_MAX_NAME_LENGTH];
|
||||||
|
snprintf(oldname, sizeof(oldname), "%s", Friends.list[num].name);
|
||||||
|
|
||||||
|
/* update name */
|
||||||
char tempname[TOX_MAX_NAME_LENGTH];
|
char tempname[TOX_MAX_NAME_LENGTH];
|
||||||
strcpy(tempname, nick);
|
strcpy(tempname, nick);
|
||||||
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
|
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
|
||||||
tempname[len] = '\0';
|
tempname[len] = '\0';
|
||||||
snprintf(Friends.list[num].name, sizeof(Friends.list[num].name), "%s", tempname);
|
snprintf(Friends.list[num].name, sizeof(Friends.list[num].name), "%s", tempname);
|
||||||
Friends.list[num].namelength = len;
|
Friends.list[num].namelength = len;
|
||||||
|
|
||||||
|
/* get data for chatlog renaming */
|
||||||
|
char newnamecpy[TOXIC_MAX_NAME_LENGTH];
|
||||||
|
char myid[TOX_FRIEND_ADDRESS_SIZE];
|
||||||
|
strcpy(newnamecpy, tempname);
|
||||||
|
tox_get_address(m, (uint8_t *) myid);
|
||||||
|
|
||||||
|
if (strcmp(oldname, newnamecpy) != 0)
|
||||||
|
rename_logfile(oldname, newnamecpy, myid, Friends.list[num].pub_key, Friends.list[num].chatwin);
|
||||||
|
|
||||||
sort_friendlist_index();
|
sort_friendlist_index();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
94
src/log.c
94
src/log.c
@ -35,12 +35,16 @@
|
|||||||
|
|
||||||
extern struct user_settings *user_settings;
|
extern struct user_settings *user_settings;
|
||||||
|
|
||||||
/* Opens log file or creates a new one */
|
/* There are three types of logs: chat logs, groupchat logs, and prompt logs (see LOG_TYPE in log.h)
|
||||||
static int init_logging_session(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype)
|
A prompt log is in the format: LOGDIR/selfkey-home.log
|
||||||
{
|
A chat log is in the format: LOGDIR/selfkey-friendname-otherkey.log
|
||||||
if (selfkey == NULL || (logtype == LOG_CHAT && otherkey == NULL))
|
A groupchat log is in the format: LOGDIR/selfkey-groupname-date[time].log
|
||||||
return -1;
|
|
||||||
|
|
||||||
|
Only the first (KEY_IDENT_DIGITS * 2) numbers of the key are used.
|
||||||
|
|
||||||
|
Returns 0 on success, -1 if the path is too long */
|
||||||
|
static int get_log_path(char *dest, int destsize, char *name, const char *selfkey, const char *otherkey, int logtype)
|
||||||
|
{
|
||||||
if (!valid_nick(name))
|
if (!valid_nick(name))
|
||||||
name = UNKNOWN_NAME;
|
name = UNKNOWN_NAME;
|
||||||
|
|
||||||
@ -76,19 +80,32 @@ static int init_logging_session(char *name, const char *selfkey, const char *oth
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
char log_path[MAX_STR_SIZE];
|
if (path_len >= destsize) {
|
||||||
|
|
||||||
if (path_len >= sizeof(log_path)) {
|
|
||||||
free(user_config_dir);
|
free(user_config_dir);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string_is_empty(set_path))
|
if (!string_is_empty(set_path))
|
||||||
snprintf(log_path, sizeof(log_path), "%s%s-%s%s%s.log", set_path, self_id, name, namedash, other_id);
|
snprintf(dest, destsize, "%s%s-%s%s%s.log", set_path, self_id, name, namedash, other_id);
|
||||||
else
|
else
|
||||||
snprintf(log_path, sizeof(log_path), "%s%s%s-%s%s%s.log", user_config_dir, LOGDIR, self_id, name, namedash, other_id);
|
snprintf(dest, destsize, "%s%s%s-%s%s%s.log", user_config_dir, LOGDIR, self_id, name, namedash, other_id);
|
||||||
|
|
||||||
free(user_config_dir);
|
free(user_config_dir);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Opens log file or creates a new one */
|
||||||
|
static int init_logging_session(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype)
|
||||||
|
{
|
||||||
|
if (selfkey == NULL || (logtype == LOG_CHAT && otherkey == NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
char log_path[MAX_STR_SIZE];
|
||||||
|
|
||||||
|
if (get_log_path(log_path, sizeof(log_path), name, selfkey, otherkey, logtype) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
log->file = fopen(log_path, "a+");
|
log->file = fopen(log_path, "a+");
|
||||||
snprintf(log->path, sizeof(log->path), "%s", log_path);
|
snprintf(log->path, sizeof(log->path), "%s", log_path);
|
||||||
|
|
||||||
@ -132,23 +149,22 @@ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool e
|
|||||||
|
|
||||||
void log_disable(struct chatlog *log)
|
void log_disable(struct chatlog *log)
|
||||||
{
|
{
|
||||||
log->log_on = false;
|
if (log->file != NULL)
|
||||||
|
|
||||||
if (log->file != NULL) {
|
|
||||||
fclose(log->file);
|
fclose(log->file);
|
||||||
log->file = NULL;
|
|
||||||
}
|
memset(log, 0, sizeof(struct chatlog));
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype)
|
void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype)
|
||||||
{
|
{
|
||||||
log->log_on = true;
|
log->log_on = true;
|
||||||
|
|
||||||
if (log->file == NULL) {
|
if (log->file != NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
if (init_logging_session(name, selfkey, otherkey, log, logtype) == -1)
|
if (init_logging_session(name, selfkey, otherkey, log, logtype) == -1)
|
||||||
log_disable(log);
|
log_disable(log);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Loads previous history from chat log */
|
/* Loads previous history from chat log */
|
||||||
void load_chat_history(ToxWindow *self, struct chatlog *log)
|
void load_chat_history(ToxWindow *self, struct chatlog *log)
|
||||||
@ -203,3 +219,47 @@ void load_chat_history(ToxWindow *self, struct chatlog *log)
|
|||||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "");
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "");
|
||||||
free(hstbuf);
|
free(hstbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* renames chatlog file replacing src with dest.
|
||||||
|
Returns 0 on success or if no log exists, -1 on failure. */
|
||||||
|
int rename_logfile(char *src, char *dest, const char *selfkey, const char *otherkey, int winnum)
|
||||||
|
{
|
||||||
|
ToxWindow *toxwin = get_window_ptr(winnum);
|
||||||
|
struct chatlog *log = NULL;
|
||||||
|
bool log_on = false;
|
||||||
|
|
||||||
|
/* disable log if necessary and save its state */
|
||||||
|
if (toxwin != NULL) {
|
||||||
|
log = toxwin->chatwin->log;
|
||||||
|
log_on = log->log_on;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log_on)
|
||||||
|
log_disable(log);
|
||||||
|
|
||||||
|
char newpath[MAX_STR_SIZE];
|
||||||
|
char oldpath[MAX_STR_SIZE];
|
||||||
|
|
||||||
|
if (get_log_path(oldpath, sizeof(oldpath), src, selfkey, otherkey, LOG_CHAT) == -1)
|
||||||
|
goto on_error;
|
||||||
|
|
||||||
|
if (!file_exists(oldpath))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (get_log_path(newpath, sizeof(newpath), dest, selfkey, otherkey, LOG_CHAT) == -1)
|
||||||
|
goto on_error;
|
||||||
|
|
||||||
|
if (rename(oldpath, newpath) != 0)
|
||||||
|
goto on_error;
|
||||||
|
|
||||||
|
if (log_on)
|
||||||
|
log_enable(dest, selfkey, otherkey, log, LOG_CHAT);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
on_error:
|
||||||
|
if (log_on)
|
||||||
|
log_enable(dest, selfkey, otherkey, log, LOG_CHAT);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
@ -48,4 +48,8 @@ void log_disable(struct chatlog *log);
|
|||||||
/* Loads previous history from chat log */
|
/* Loads previous history from chat log */
|
||||||
void load_chat_history(ToxWindow *self, struct chatlog *log);
|
void load_chat_history(ToxWindow *self, struct chatlog *log);
|
||||||
|
|
||||||
|
/* renames chatlog file replacing src with dest.
|
||||||
|
Returns 0 on success or if no log exists, -1 on failure. */
|
||||||
|
int rename_logfile(char *src, char *dest, const char *selfkey, const char *otherkey, int winnum);
|
||||||
|
|
||||||
#endif /* #define LOG_H */
|
#endif /* #define LOG_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user