diff --git a/PiHelper/cli.c b/PiHelper/cli.c index 4a0c07e..6683d5b 100644 --- a/PiHelper/cli.c +++ b/PiHelper/cli.c @@ -25,7 +25,6 @@ int main(int argc, char ** argv) { } else { disable = ""; } - write_log(LOG_DEBUG, "Disabling pi-hole for %s seconds", disable); break; case 'e': enable = true; @@ -41,7 +40,6 @@ int main(int argc, char ** argv) { config_path[strlen(cwd)] = '/'; strncpy(&(config_path[strlen(cwd) + 1]), optarg, strlen(optarg)); config_path[full_path_len] = '\0'; - write_log(LOG_DEBUG, "Fixed config_path: %s", config_path); } else { // This is an absolute path, copy as-is config_path = malloc(strlen(optarg) + 1); @@ -49,9 +47,11 @@ int main(int argc, char ** argv) { config_path[strlen(optarg)] = '\0'; } break; + case 'q': + pihelper_set_log_level(PIHELPER_LOG_DISABLED); + break; case 'v': - // TODO: Add log level and set here - //PIHELPER_DEBUG = true; + pihelper_set_log_level(PIHELPER_LOG_DEBUG); break; case 'h': default: @@ -69,6 +69,7 @@ int main(int argc, char ** argv) { FILE * config_file = fopen(config_path, "r+"); if (config_file == NULL) { char * user_input = malloc(2); + // Intentionally using printf here to ensure that this is always printed printf("No Pi-Helper configuration found. Would you like to create it now? [Y/n] "); fgets(user_input, 2, stdin); if (strstr(user_input, "\n") == user_input @@ -79,6 +80,8 @@ int main(int argc, char ** argv) { } else { return 1; } + } else { + write_log(PIHELPER_LOG_DEBUG, "Using config file at: %s", config_path); } pihole_config * config; diff --git a/PiHelper/config.c b/PiHelper/config.c index 29c2fa7..80dec41 100644 --- a/PiHelper/config.c +++ b/PiHelper/config.c @@ -54,7 +54,7 @@ static char * hash_string (char * raw_string) { pihole_config * read_config(char * config_path) { if (access(config_path, F_OK)) { - printf("ERROR: The specified config file doesn't exist\n"); + write_log(PIHELPER_LOG_ERROR, "The specified config file doesn't exist: %s", config_path); return NULL; } @@ -62,8 +62,9 @@ pihole_config * read_config(char * config_path) { FILE * config_file = fopen(config_path, "r"); char host[_POSIX_HOST_NAME_MAX + 7]; fgets(host, _POSIX_HOST_NAME_MAX + 7, config_file); - if (strstr(host, "host=") == NULL) { - printf("Invalid config file\n"); + if (strstr(host, "host=") == NULL || strlen(host) < 7) { + write_log(PIHELPER_LOG_DEBUG, "Config file contains invalid host: %s", host); + write_log(PIHELPER_LOG_ERROR, "Invalid config file"); exit(1); } config->host = calloc(1, strlen(host) - 7); @@ -71,7 +72,8 @@ pihole_config * read_config(char * config_path) { char api_key[74]; fgets(api_key, 74, config_file); if (strstr(api_key, "api-key=") == NULL) { - printf("Invalid config file\n"); + write_log(PIHELPER_LOG_DEBUG, "Config file contains invalid api key: %s", api_key); + write_log(PIHELPER_LOG_ERROR, "Invalid config file"); exit(1); } config->api_key = calloc(1, strlen(api_key) - 8); @@ -83,11 +85,12 @@ pihole_config * read_config(char * config_path) { pihole_config * configure_pihole(char * config_path) { if (access(config_path, F_OK) == 0) { // TODO: Check if file is accessible for read/write (not just if it exists) - printf("WARNING: The config file already exists. Continuing will overwrite any existing configuration.\n"); + write_log(PIHELPER_LOG_WARN, "WARNING: The config file already exists. Continuing will overwrite any existing configuration.\n"); } pihole_config * config = malloc(sizeof(pihole_config)); config->host = calloc(1, _POSIX_HOST_NAME_MAX); config->host[_POSIX_HOST_NAME_MAX] = '\0'; + // Intentionally using printf to ensure this is always printed printf("Enter the hostname or ip address for your pi-hole: "); fgets(config->host, _POSIX_HOST_NAME_MAX, stdin); char * newline = strstr(config->host, "\n"); diff --git a/PiHelper/config.h b/PiHelper/config.h index 4a1889d..ec371a6 100644 --- a/PiHelper/config.h +++ b/PiHelper/config.h @@ -1,6 +1,7 @@ #ifndef PIHELPER_CONFIG #define PIHELPER_CONFIG #include +#include "log.h" static char * DEFAULT_CONFIG_PATH = "/.config/pihelper.conf"; diff --git a/PiHelper/log.c b/PiHelper/log.c index 6776e4e..cdcdece 100644 --- a/PiHelper/log.c +++ b/PiHelper/log.c @@ -5,8 +5,14 @@ #include #include "log.h" +int LOG_LEVEL = 2; // Default to info logs + +void set_log_level(int level) { + LOG_LEVEL = level; +} + void write_log(int level, char * format, ...) { - if (level == LOG_DEBUG) return; + if (level > LOG_LEVEL) return; va_list args; va_start(args, format); int format_len = strlen(format); @@ -14,7 +20,8 @@ void write_log(int level, char * format, ...) { memcpy(new_format, format, format_len); new_format[format_len] = '\n'; new_format[format_len + 1] = '\0'; - vprintf(new_format, args); + FILE *stream = level < PIHELPER_LOG_INFO ? stderr : stdout; + vfprintf(stream, new_format, args); va_end(args); } diff --git a/PiHelper/log.h b/PiHelper/log.h index 3bf12ac..e4af63f 100644 --- a/PiHelper/log.h +++ b/PiHelper/log.h @@ -1,10 +1,13 @@ #ifndef PIHELPER_LOG #define PIHELPER_LOG -static int LOG_ERROR = 0; -static int LOG_WARN = 1; -static int LOG_INFO = 2; -static int LOG_DEBUG = 3; +static int PIHELPER_LOG_DISABLED = -1; +static int PIHELPER_LOG_ERROR = 0; +static int PIHELPER_LOG_WARN = 1; +static int PIHELPER_LOG_INFO = 2; +static int PIHELPER_LOG_DEBUG = 3; + +void set_log_level(int level); void write_log(int level, char * format, ...); #endif diff --git a/PiHelper/network.c b/PiHelper/network.c index d0e3c19..de69387 100644 --- a/PiHelper/network.c +++ b/PiHelper/network.c @@ -17,9 +17,11 @@ static char * HTTP_SCHEME = "http://"; static char * HTTPS_SCHEME = "https://"; int get_status(pihole_config * config) { + write_log(PIHELPER_LOG_DEBUG, "Getting Pi-hole status…"); char * formatted_host = prepend_scheme(config->host); char * response = get(formatted_host); if (response == NULL) { + write_log(PIHELPER_LOG_ERROR, "Failed to retrieve status for Pi-hole at %s\n", config->host); return 1; } else { parse_status(response); @@ -29,6 +31,7 @@ int get_status(pihole_config * config) { } int enable_pihole(pihole_config * config) { + write_log(PIHELPER_LOG_DEBUG, "Enabling Pi-hole…"); char * formatted_host = prepend_scheme(config->host); append_query_parameter(&formatted_host, AUTH_QUERY, config->api_key); append_query_parameter(&formatted_host, ENABLE_QUERY, NULL); @@ -43,6 +46,11 @@ int enable_pihole(pihole_config * config) { } int disable_pihole(pihole_config * config, char * duration) { + if (*duration == '\0') { + write_log(PIHELPER_LOG_DEBUG, "Disabling Pi-hole permanently…"); + } else { + write_log(PIHELPER_LOG_DEBUG, "Disabling Pi-hole for %s seconds…", duration); + } char * formatted_host = prepend_scheme(config->host); append_query_parameter(&formatted_host, AUTH_QUERY, config->api_key); append_query_parameter(&formatted_host, DISABLE_QUERY, duration); @@ -56,8 +64,7 @@ int disable_pihole(pihole_config * config, char * duration) { } } - -/* +/** * Used to handle curl data callbacks */ static size_t receive_data(char *ptr, size_t size, size_t nmemb, void *userdata) { @@ -71,6 +78,9 @@ static size_t receive_data(char *ptr, size_t size, size_t nmemb, void *userdata) return realsize; } +/** + * Used to make a GET request to a given endpoint + */ static char * get(char endpoint[]) { http_response response; response.body = malloc(1); @@ -119,7 +129,7 @@ static void parse_status(char * raw_json) { jobj = json_tokener_parse_ex(tok, raw_json, stringlen); } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); if (jerr != json_tokener_success) { - write_log(LOG_ERROR, "Failed to parse JSON: %s", json_tokener_error_desc(jerr)); + write_log(PIHELPER_LOG_ERROR, "Failed to parse JSON: %s", json_tokener_error_desc(jerr)); return; } json_object *status = json_object_new_object(); @@ -128,7 +138,7 @@ static void parse_status(char * raw_json) { && (status_string = json_object_get_string(status)) != NULL) { printf("Pi-hole status: %s\n", status_string); } else { - write_log(LOG_DEBUG, "Unable to parse response: %s", raw_json); + write_log(PIHELPER_LOG_DEBUG, "Unable to parse response: %s", raw_json); } json_tokener_free(tok); json_object_put(jobj); diff --git a/PiHelper/pihelper.c b/PiHelper/pihelper.c index 67233b7..7d83da4 100644 --- a/PiHelper/pihelper.c +++ b/PiHelper/pihelper.c @@ -1,17 +1,17 @@ -/* - * ===================================================================================== - * - * Filename: pihelper.c - * - * Description: The main PiHelper class - * - * Version: 1.0 - * Created: 12/20/2019 18:24:51 - * Revision: none - * Compiler: gcc - * - * Author: William Brawner (Billy), billy@wbrawner.com - * - * ===================================================================================== - */ +#include "pihelper.h" +int pihelper_get_status(pihole_config * config) { + return get_status(config); +} + +int pihelper_enable_pihole(pihole_config * config) { + return enable_pihole(config); +} + +int pihelper_disable_pihole(pihole_config * config, char * duration) { + return disable_pihole(config, duration); +} + +void pihelper_set_log_level(int level) { + set_log_level(level); +} diff --git a/PiHelper/pihelper.h b/PiHelper/pihelper.h index ffceb67..285faa7 100644 --- a/PiHelper/pihelper.h +++ b/PiHelper/pihelper.h @@ -3,8 +3,16 @@ #include "config.h" #include "log.h" #include "network.h" -const int PIHELPER_OK = 0; -const int PIHELPER_HELP = 1; -const int PIHELPER_INVALID_COMMANDS = 2; +static int PIHELPER_OK = 0; +static int PIHELPER_HELP = 1; +static int PIHELPER_INVALID_COMMANDS = 2; + +void pihelper_set_log_level(int level); + +int pihelper_get_status(pihole_config * config); + +int pihelper_enable_pihole(pihole_config * config); + +int pihelper_disable_pihole(pihole_config * config, char * duration); #endif