summaryrefslogtreecommitdiffstats
path: root/src/detection/weather/weather.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/weather/weather.c
Add the files
Diffstat (limited to 'src/detection/weather/weather.c')
-rw-r--r--src/detection/weather/weather.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/detection/weather/weather.c b/src/detection/weather/weather.c
new file mode 100644
index 0000000..69df642
--- /dev/null
+++ b/src/detection/weather/weather.c
@@ -0,0 +1,62 @@
+#include "weather.h"
+#include "common/networking.h"
+
+#define FF_UNITIALIZED ((const char*) (uintptr_t) -1)
+static FFNetworkingState state;
+static const char* status = FF_UNITIALIZED;
+
+void ffPrepareWeather(FFWeatherOptions* options) {
+ if (status != FF_UNITIALIZED) {
+ fputs("Error: Weather module can only be used once due to internal limitations\n", stderr);
+ exit(1);
+ }
+
+ state.timeout = options->timeout;
+
+ FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/");
+ if (options->location.length) {
+ ffStrbufAppend(&path, &options->location);
+ }
+ ffStrbufAppendS(&path, "?format=");
+ ffStrbufAppend(&path, &options->outputFormat);
+ switch (instance.config.display.tempUnit) {
+ case FF_TEMPERATURE_UNIT_CELSIUS:
+ ffStrbufAppendS(&path, "&m");
+ break;
+ case FF_TEMPERATURE_UNIT_FAHRENHEIT:
+ ffStrbufAppendS(&path, "&u");
+ break;
+ default:
+ break;
+ }
+ status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
+}
+
+const char* ffDetectWeather(FFWeatherOptions* options, FFstrbuf* result) {
+ if (status == FF_UNITIALIZED) {
+ ffPrepareWeather(options);
+ }
+
+ if (status != NULL) {
+ return status;
+ }
+
+ ffStrbufEnsureFree(result, 4095);
+ const char* error = ffNetworkingRecvHttpResponse(&state, result);
+
+ state = (FFNetworkingState) {};
+ status = FF_UNITIALIZED;
+
+ if (error == NULL) {
+ ffStrbufSubstrAfterFirstS(result, "\r\n\r\n");
+ ffStrbufTrimRightSpace(result);
+ } else {
+ return error;
+ }
+
+ if (result->length == 0) {
+ return "Empty server response received";
+ }
+
+ return NULL;
+}