void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Input required."); return; } char nick[MAX_STR_SIZE]; int len = 0; if (argv[1][0] == '\"') { /* remove opening and closing quotes */ snprintf(nick, sizeof(nick), "%s", &argv[1][1]); len = strlen(nick) - 1; nick[len] = '\0'; } else { snprintf(nick, sizeof(nick), "%s", argv[1]); len = strlen(nick); } if (!valid_nick(nick)) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); return; } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); nick[len] = '\0'; tox_set_name(m, (uint8_t *) nick, (uint16_t) len); prompt_update_nick(prompt, nick); store_data(m, DATA_FILE); }
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; /* check arguments */ if (argc < 1) { errmsg = "Invalid name."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } char *nick = argv[1]; int len = strlen(nick); if (nick[0] == '\"') { ++nick; len -= 2; nick[len] = '\0'; } if (!valid_nick(nick)) { errmsg = "Invalid name."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); nick[len] = '\0'; tox_set_name(m, (uint8_t *) nick, (uint16_t) len); prompt_update_nick(prompt, nick); store_data(m, DATA_FILE); }
/* There are three types of logs: chat logs, groupchat logs, and prompt logs (see LOG_TYPE in log.h) A prompt log is in the format: LOGDIR/selfkey-home.log A chat log is in the format: LOGDIR/selfkey-friendname-otherkey.log A groupchat log is in the format: LOGDIR/selfkey-groupname-date[time].log 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)) name = UNKNOWN_NAME; const char *namedash = logtype == LOG_PROMPT ? "" : "-"; const char *set_path = user_settings->chatlogs_path; char *user_config_dir = get_user_config_dir(); int path_len = strlen(name) + strlen(".log") + strlen("-") + strlen(namedash); path_len += strlen(set_path) ? *set_path : strlen(user_config_dir) + strlen(LOGDIR); /* first 6 digits of selfkey */ char self_id[32]; path_len += KEY_IDENT_DIGITS * 2; sprintf(&self_id[0], "%02X", selfkey[0] & 0xff); sprintf(&self_id[2], "%02X", selfkey[1] & 0xff); sprintf(&self_id[4], "%02X", selfkey[2] & 0xff); self_id[KEY_IDENT_DIGITS * 2] = '\0'; char other_id[32] = {0}; switch (logtype) { case LOG_CHAT: path_len += KEY_IDENT_DIGITS * 2; sprintf(&other_id[0], "%02X", otherkey[0] & 0xff); sprintf(&other_id[2], "%02X", otherkey[1] & 0xff); sprintf(&other_id[4], "%02X", otherkey[2] & 0xff); other_id[KEY_IDENT_DIGITS * 2] = '\0'; break; case LOG_GROUP: strftime(other_id, sizeof(other_id), "%Y-%m-%d[%H:%M:%S]", get_time()); path_len += strlen(other_id); break; } if (path_len >= destsize) { free(user_config_dir); return -1; } if (!string_is_empty(set_path)) snprintf(dest, destsize, "%s%s-%s%s%s.log", set_path, self_id, name, namedash, other_id); else snprintf(dest, destsize, "%s%s%s-%s%s%s.log", user_config_dir, LOGDIR, self_id, name, namedash, other_id); free(user_config_dir); return 0; }
/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) { if (!log->log_on) return; if (!valid_nick(name)) name = UNKNOWN_NAME; char *user_config_dir = get_user_config_dir(); int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name); /* use first 4 digits of key as log ident. If no key use a timestamp */ uint8_t ident[32]; if (key != NULL) { path_len += (KEY_IDENT_DIGITS * 2 + 5); sprintf(&ident[0], "%02X", key[0] & 0xff); sprintf(&ident[2], "%02X", key[1] & 0xff); ident[KEY_IDENT_DIGITS * 2 + 1] = '\0'; } else { strftime(ident, sizeof(ident), "%Y-%m-%d[%H:%M:%S]", get_time()); path_len += strlen(ident) + 1; } if (path_len > MAX_STR_SIZE) { log->log_on = false; free(user_config_dir); return; } uint8_t log_path[MAX_STR_SIZE]; snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); free(user_config_dir); log->file = fopen(log_path, "a"); if (log->file == NULL) { log->log_on = false; return; } fprintf(log->file, "\n*** NEW SESSION ***\n\n"); }