diff options
author | Luke Bratch <luke@bratch.co.uk> | 2019-09-14 20:44:32 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2019-09-14 20:44:32 +0100 |
commit | 4dea4c16313ba3d1575cfa6722d75492c907f551 (patch) | |
tree | cade95813a471e3aa4597a5a35e907e2cd675181 /functions.c | |
parent | e1f41810ac85a0d210062ed33f43938dc4b03be4 (diff) |
Specify multiple nicks using a configuration array instead of multiple individual settings.
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/functions.c b/functions.c index af8d5dd..fe96e0a 100644 --- a/functions.c +++ b/functions.c @@ -1086,31 +1086,51 @@ void cleanexit(SSL *server_ssl, struct client *clients, int sourcefd, struct irc int rehash(struct settings *settings, char *failuremsg) { // TODO - Try to share some/all of this code with the initial main() settings loading - // What is the nick? - char oldircnick[MAXNICKLENGTH]; - strcpy(oldircnick, settings->ircnick); - if (!getconfstr("nick", settings->conffile, settings->ircnick)) { - strcpy(settings->ircnick, oldircnick); - strcpy(failuremsg, "error getting 'nick' from configuration file"); + // What are the configured nick(s)? + char oldircnicks[MAXCONFARR][MAXDATASIZE]; + // Backup the existing configured nicks in case this rehash fails + for (int i = 0; i < MAXCONFARR; i++) { + strcpy(oldircnicks[i], settings->ircnicks[i]); + } + int ret = getconfarr("nicks", settings->conffile, settings->ircnicks); + if (!ret) { + // No nicks read, copy the old ones back + for (int i = 0; i < MAXCONFARR; i++) { + strcpy(settings->ircnicks[i], oldircnicks[i]); + } + strcpy(failuremsg, "error getting any 'nicks' from configuration file"); + return 0; + } else if (ret == -1) { + // Error reading an array line from the configuration file + // Remove any newlines from the string so error printing works nicely + for (size_t i = 0; i < strlen(settings->ircnicks[0]); i++) { + if (settings->ircnicks[0][i] == '\n') { + settings->ircnicks[0][i] = ' '; + } + } + if (!snprintf(failuremsg, MAXDATASIZE, "error getting 'nicks' from configuration file: %s", settings->ircnicks[0])) { + debugprint(DEBUG_CRIT, "Error while preparing nick error response!\n"); + strcpy(failuremsg, "Error while preparing nick error response!"); + } + // Copy the old ones back (after setting failuremsg so we can read the error string from element 0) + for (int i = 0; i < MAXCONFARR; i++) { + strcpy(settings->ircnicks[i], oldircnicks[i]); + } return 0; } - - // What is nick2? - char oldircnick2[MAXNICKLENGTH]; - strcpy(oldircnick2, settings->ircnick2); - if (!getconfstr("nick2", settings->conffile, settings->ircnick2)) { - strcpy(settings->ircnick2, oldircnick2); - // Not configured, set to blank string - settings->ircnick2[0] = '\0'; - } - - // What is nick3? - char oldircnick3[MAXNICKLENGTH]; - strcpy(oldircnick3, settings->ircnick3); - if (!getconfstr("nick3", settings->conffile, settings->ircnick3)) { - strcpy(settings->ircnick3, oldircnick3); - // Not configured, set to blank string - settings->ircnick3[0] = '\0'; + // Make sure nicks aren't too long (since getconfarr() has to use MAXDATASIZE for all string lengths) + for (int i = 0; i < MAXCONFARR; i++) { + if (settings->ircnicks[i][0] && strlen(settings->ircnicks[i]) > MAXNICKLENGTH) { + // A nick is too long, copy the old ones back + for (int i = 0; i < MAXCONFARR; i++) { + strcpy(settings->ircnicks[i], oldircnicks[i]); + } + if (!snprintf(failuremsg, MAXDATASIZE, "error: specified nick '%s' is too long, maximum length is %d.\n", settings->ircnicks[i], MAXNICKLENGTH)) { + debugprint(DEBUG_CRIT, "Error while preparing nick too long response!\n"); + strcpy(failuremsg, "Error while preparing nick too long response!"); + } + return 0; + } } // What is the auto replay mode? |