diff --git a/CMakeLists.txt b/CMakeLists.txt index 8275f91df..7bad8d459 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -281,6 +281,7 @@ set(LIBFASTFETCH_SRC src/detection/terminalshell/terminalshell.c src/detection/version/version.c src/detection/vulkan/vulkan.c + src/detection/weather/weather.c src/logo/builtin.c src/logo/image/im6.c src/logo/image/im7.c diff --git a/src/detection/weather/weather.c b/src/detection/weather/weather.c new file mode 100644 index 000000000..94bd6b5b6 --- /dev/null +++ b/src/detection/weather/weather.c @@ -0,0 +1,38 @@ +#include "weather.h" + +static FFNetworkingState state; +static int status = -1; + +void ffPrepareWeather(FFWeatherOptions* options) +{ + if (status != -1) + { + fputs("Error: this module can only be used once due to internal limitations\n", stderr); + exit(1); + } + + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/"); + if (options->location.length) + ffStrbufAppend(&path, &options->location); + ffStrbufAppendS(&path, "?format="); + ffStrbufAppend(&path, &options->outputFormat); + status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n"); +} + +const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result) +{ + if(status == -1) + ffPrepareWeather(options); + + if(status == 0) + return "Failed to connect to 'wttr.in'"; + + ffStrbufEnsureFree(result, 4095); + bool success = ffNetworkingRecvHttpResponse(&state, result, options->timeout); + if (success) ffStrbufSubstrAfterFirstS(result, "\r\n\r\n"); + + if(!success || result->length == 0) + return "Failed to receive the server response"; + + return NULL; +} diff --git a/src/detection/weather/weather.h b/src/detection/weather/weather.h new file mode 100644 index 000000000..c15f1ea3a --- /dev/null +++ b/src/detection/weather/weather.h @@ -0,0 +1,11 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_weather_weather +#define FF_INCLUDED_detection_weather_weather + +#include "common/networking.h" + +void ffPrepareWeather(FFWeatherOptions* options); +const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result); + +#endif diff --git a/src/modules/weather/weather.c b/src/modules/weather/weather.c index 1338f3286..d71dbef74 100644 --- a/src/modules/weather/weather.c +++ b/src/modules/weather/weather.c @@ -1,50 +1,22 @@ #include "common/printing.h" #include "common/jsonconfig.h" -#include "common/networking.h" +#include "detection/weather/weather.h" #include "modules/weather/weather.h" #include "util/stringUtils.h" #define FF_WEATHER_NUM_FORMAT_ARGS 1 -static FFNetworkingState state; -static int status = -1; - -void ffPrepareWeather(FFWeatherOptions* options) -{ - if (status != -1) - { - fputs("Error: " FF_WEATHER_MODULE_NAME " can only be used once due to internal limitations\n", stderr); - exit(1); - } - - FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/"); - if (options->location.length) - ffStrbufAppend(&path, &options->location); - ffStrbufAppendS(&path, "?format="); - ffStrbufAppend(&path, &options->outputFormat); - status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n"); -} - void ffPrintWeather(FFWeatherOptions* options) { - if(status == -1) - ffPrepareWeather(options); + FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); + const char* error = ffDetectWeather(options, &result); - if(status == 0) + if(error) { - ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, "Failed to connect to 'wttr.in'"); + ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, error); return; } - FF_STRBUF_AUTO_DESTROY result = ffStrbufCreateA(4096); - bool success = ffNetworkingRecvHttpResponse(&state, &result, options->timeout); - if (success) ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n"); - - if(!success || result.length == 0) - { - ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, "Failed to receive the server response"); - return; - } if(options->moduleArgs.outputFormat.length == 0) {