From c46d01b5afd2da4779efb2700469037eca6122be Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sat, 11 May 2019 15:01:22 +0100 Subject: Implement a configuration file reader, an example configuration file, and start reading nick/username/realname from it instead of being statically defined. --- config.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 config.c (limited to 'config.c') diff --git a/config.c b/config.c new file mode 100644 index 0000000..ca6b158 --- /dev/null +++ b/config.c @@ -0,0 +1,103 @@ +#include "config.h" + +// Check to see if a line is the configuration option specified by +// checking to see if str starts with conf followed by a space, a tab, +// or an equals sign. +// Returns the length of the name of the requested option if found, or +// zero if not. +long int isconf(char *str, char *conf) { + char *ret; + char substr[MAXCHAR]; + long int len = strlen(conf); + + // Ensure the next character is an equals sign, a space, or a tab + if (str[len] != '=' && str[len] != ' ' && str[len] != ' ') { + return 0; + } + + strncpy(substr, str, len); + substr[len] = '\0'; + + + if (!(ret = strstr(substr, conf)) == 0) { + return len; + } + + return 0; +} + +// Extracts the value of a configuration option from a string. Expects +// to receive the string and the number of characters long that the +// configuration option name is. +void getconf(char *str, long int start) { + long int len = strlen(str); + long int pos; + char conf[MAXCHAR]; + + for (long int i = start; i < len; i++) { + if (str[i] == '=' || str[i] == ' ' || str[i] == ' ') { + continue; + } else { + pos = i; + break; + } + } + + strncpy(conf, str + pos, len - pos - 1); // Copy remainder to new string and lop off the newline + conf[len - pos - 1] = '\0'; // Null terminate + + // Check for start and end quotes + if (conf[0] != '"' || conf[strlen(conf) - 1] != '"') { + printf("Error reading configuration option, not enclosed in double quotes!\n"); + exit(1); + } + + strncpy(str, conf + 1, strlen(conf) - 2); // Copy result back to original string without quotes + str[strlen(conf) - 2] = '\0'; // Null terminate +} + +int readnames(char *nick, char *username, char *realname) { + FILE *fp; + char str[MAXCHAR]; + char* filename = "blabouncer.conf"; + + // Set all strings to zero-length to begin + nick[0] = '\0'; + username[0] = '\0'; + realname[0] = '\0'; + + fp = fopen(filename, "r"); + + if (fp == NULL) { + printf("error: could not open configuration file '%s.'", filename); + return 1; + } + while (fgets(str, MAXCHAR, fp) != NULL) { + long int len; + if ((len = isconf(str, "nick"))) { + getconf(str, len); + strncpy(nick, str, strlen(str)); + nick[strlen(str)] = '\0'; + printf("Nick is: '%s', length '%ld'.\n", nick, strlen(nick)); + } else if ((len = isconf(str, "username"))) { + getconf(str, len); + strncpy(username, str, strlen(str)); + username[strlen(str)] = '\0'; + printf("Username is: '%s', length '%ld'.\n", username, strlen(username)); + } else if ((len = isconf(str, "realname"))) { + getconf(str, len); + strncpy(realname, str, strlen(str)); + realname[strlen(str)] = '\0'; + printf("Real name is: '%s', length '%ld'.\n", realname, strlen(realname)); + } + } + + // Ensure we have everything required + if (!strlen(nick) || !strlen(username) || !strlen(realname)) { + printf("Failed to read nick and/or username and/or real name from configuration file.\n"); + exit(1); + } + + fclose(fp); + return 1; +} -- cgit v1.2.3