safe random nick on reboot
This commit is contained in:
parent
e00f9c6293
commit
815f79bc0b
|
|
@ -78,44 +78,6 @@ static void dump_settings() {
|
||||||
|
|
||||||
// Public
|
// Public
|
||||||
|
|
||||||
void settings_load() {
|
|
||||||
g_settings = DEFAULTS;
|
|
||||||
|
|
||||||
snprintf(g_settings.nick, sizeof(g_settings.nick),
|
|
||||||
"%s%04u", NICK_DEFAULT_PREFIX, (unsigned)(esp_random() % 10000));
|
|
||||||
|
|
||||||
if (!LittleFS.begin(true)) {
|
|
||||||
Serial.println("[settings] LittleFS mount failed, using defaults");
|
|
||||||
dump_settings();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File f = LittleFS.open("/config.txt", "r");
|
|
||||||
if (!f) {
|
|
||||||
Serial.println("[settings] /config.txt not found, using defaults");
|
|
||||||
dump_settings();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char line[128];
|
|
||||||
while (f.available()) {
|
|
||||||
int n = f.readBytesUntil('\n', line, sizeof(line) - 1);
|
|
||||||
line[n] = '\0';
|
|
||||||
char* l = trim(line);
|
|
||||||
if (*l == '\0' || *l == '#') continue; // blank line or comment
|
|
||||||
|
|
||||||
char* eq = strchr(l, '=');
|
|
||||||
if (!eq) continue; // no '=' skip
|
|
||||||
*eq = '\0';
|
|
||||||
char* key = trim(l);
|
|
||||||
char* val = trim(eq + 1);
|
|
||||||
apply_kv(key, val);
|
|
||||||
}
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
dump_settings();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rewrite_config_line(const char* key, const char* val) {
|
static void rewrite_config_line(const char* key, const char* val) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
size_t keylen = strlen(key);
|
size_t keylen = strlen(key);
|
||||||
|
|
@ -155,6 +117,52 @@ static void rewrite_config_line(const char* key, const char* val) {
|
||||||
Serial.printf("[settings] %s saved: %s\n", key, val);
|
Serial.printf("[settings] %s saved: %s\n", key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void settings_load() {
|
||||||
|
g_settings = DEFAULTS;
|
||||||
|
|
||||||
|
snprintf(g_settings.nick, sizeof(g_settings.nick),
|
||||||
|
"%s%04u", NICK_DEFAULT_PREFIX, (unsigned)(esp_random() % 10000));
|
||||||
|
char nick_check[CHAT_MAX_NICK_LEN];
|
||||||
|
strcpy(nick_check, g_settings.nick);
|
||||||
|
|
||||||
|
if (!LittleFS.begin(true))
|
||||||
|
{
|
||||||
|
Serial.println("[settings] LittleFS mount failed, using defaults");
|
||||||
|
dump_settings();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File f = LittleFS.open("/config.txt", "r");
|
||||||
|
if (!f) {
|
||||||
|
Serial.println("[settings] /config.txt not found, using defaults");
|
||||||
|
dump_settings();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char line[128];
|
||||||
|
while (f.available()) {
|
||||||
|
int n = f.readBytesUntil('\n', line, sizeof(line) - 1);
|
||||||
|
line[n] = '\0';
|
||||||
|
char* l = trim(line);
|
||||||
|
if (*l == '\0' || *l == '#') continue; // blank line or comment
|
||||||
|
|
||||||
|
char* eq = strchr(l, '=');
|
||||||
|
if (!eq) continue; // no '=' skip
|
||||||
|
*eq = '\0';
|
||||||
|
char* key = trim(l);
|
||||||
|
char* val = trim(eq + 1);
|
||||||
|
apply_kv(key, val);
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
if(!strcmp(g_settings.nick, nick_check)) { // safe the random nick
|
||||||
|
rewrite_config_line("nick", g_settings.nick);
|
||||||
|
}
|
||||||
|
|
||||||
|
dump_settings();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void settings_set_nick(const char* nick) {
|
void settings_set_nick(const char* nick) {
|
||||||
strncpy(g_settings.nick, nick, CHAT_MAX_NICK_LEN - 1);
|
strncpy(g_settings.nick, nick, CHAT_MAX_NICK_LEN - 1);
|
||||||
g_settings.nick[CHAT_MAX_NICK_LEN - 1] = '\0';
|
g_settings.nick[CHAT_MAX_NICK_LEN - 1] = '\0';
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue