summaryrefslogtreecommitdiffstats
path: root/src/modules/publicip
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/modules/publicip
Add the files
Diffstat (limited to 'src/modules/publicip')
-rw-r--r--src/modules/publicip/option.h13
-rw-r--r--src/modules/publicip/publicip.c125
-rw-r--r--src/modules/publicip/publicip.h13
3 files changed, 151 insertions, 0 deletions
diff --git a/src/modules/publicip/option.h b/src/modules/publicip/option.h
new file mode 100644
index 0000000..aca7236
--- /dev/null
+++ b/src/modules/publicip/option.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include "common/option.h"
+
+typedef struct FFPublicIPOptions {
+ FFModuleArgs moduleArgs;
+
+ FFstrbuf url;
+ uint32_t timeout;
+ bool ipv6;
+} FFPublicIPOptions;
+
+static_assert(sizeof(FFPublicIPOptions) <= FF_OPTION_MAX_SIZE, "FFPublicIPOptions size exceeds maximum allowed size");
diff --git a/src/modules/publicip/publicip.c b/src/modules/publicip/publicip.c
new file mode 100644
index 0000000..5521733
--- /dev/null
+++ b/src/modules/publicip/publicip.c
@@ -0,0 +1,125 @@
+#include "common/printing.h"
+#include "common/jsonconfig.h"
+#include "common/strutil.h"
+#include "modules/publicip/publicip.h"
+#include "detection/publicip/publicip.h"
+
+#define FF_PUBLICIP_DISPLAY_NAME "Public IP"
+
+bool ffPrintPublicIp(FFPublicIPOptions* options) {
+ FFPublicIpResult result;
+ ffStrbufInit(&result.ip);
+ ffStrbufInit(&result.location);
+ const char* error = ffDetectPublicIp(options, &result);
+
+ if (error) {
+ ffPrintError(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
+ return false;
+ }
+
+ if (options->moduleArgs.outputFormat.length == 0) {
+ ffPrintLogoAndKey(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
+ if (result.location.length) {
+ printf("%s (%s)\n", result.ip.chars, result.location.chars);
+ } else {
+ ffStrbufPutTo(&result.ip, stdout);
+ }
+ } else {
+ FF_PRINT_FORMAT_CHECKED(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
+ FF_ARG(result.ip, "ip"),
+ FF_ARG(result.location, "location"),
+ }));
+ }
+
+ ffStrbufDestroy(&result.ip);
+ ffStrbufDestroy(&result.location);
+
+ return true;
+}
+
+void ffParsePublicIpJsonObject(FFPublicIPOptions* options, yyjson_val* module) {
+ yyjson_val *key, *val;
+ size_t idx, max;
+ yyjson_obj_foreach (module, idx, max, key, val) {
+ if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
+ continue;
+ }
+
+ if (unsafe_yyjson_equals_str(key, "url")) {
+ ffStrbufSetJsonVal(&options->url, val);
+ continue;
+ }
+
+ if (unsafe_yyjson_equals_str(key, "timeout")) {
+ options->timeout = (uint32_t) yyjson_get_uint(val);
+ continue;
+ }
+
+ if (unsafe_yyjson_equals_str(key, "ipv6")) {
+ options->ipv6 = yyjson_get_bool(val);
+ continue;
+ }
+
+ ffPrintError(FF_PUBLICIP_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
+ }
+}
+
+void ffGeneratePublicIpJsonConfig(FFPublicIPOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
+ ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
+
+ yyjson_mut_obj_add_strbuf(doc, module, "url", &options->url);
+
+ yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout);
+
+ yyjson_mut_obj_add_bool(doc, module, "ipv6", options->ipv6);
+}
+
+bool ffGeneratePublicIpJsonResult(FFPublicIPOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
+ FFPublicIpResult result;
+ ffStrbufInit(&result.ip);
+ ffStrbufInit(&result.location);
+ const char* error = ffDetectPublicIp(options, &result);
+
+ if (error) {
+ yyjson_mut_obj_add_str(doc, module, "error", error);
+ return false;
+ }
+
+ yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
+ yyjson_mut_obj_add_strbuf(doc, obj, "ip", &result.ip);
+ yyjson_mut_obj_add_strbuf(doc, obj, "location", &result.location);
+
+ ffStrbufDestroy(&result.ip);
+ ffStrbufDestroy(&result.location);
+
+ return true;
+}
+
+void ffInitPublicIpOptions(FFPublicIPOptions* options) {
+ ffOptionInitModuleArg(&options->moduleArgs, "󰩠");
+
+ ffStrbufInit(&options->url);
+ options->timeout = 0;
+ options->ipv6 = false;
+}
+
+void ffDestroyPublicIpOptions(FFPublicIPOptions* options) {
+ ffOptionDestroyModuleArg(&options->moduleArgs);
+
+ ffStrbufDestroy(&options->url);
+}
+
+FFModuleBaseInfo ffPublicIPModuleInfo = {
+ .name = FF_PUBLICIP_MODULE_NAME,
+ .description = "Print your public IP address and related information",
+ .initOptions = (void*) ffInitPublicIpOptions,
+ .destroyOptions = (void*) ffDestroyPublicIpOptions,
+ .parseJsonObject = (void*) ffParsePublicIpJsonObject,
+ .printModule = (void*) ffPrintPublicIp,
+ .generateJsonResult = (void*) ffGeneratePublicIpJsonResult,
+ .generateJsonConfig = (void*) ffGeneratePublicIpJsonConfig,
+ .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
+ { "Public IP address", "ip" },
+ { "Location", "location" },
+ }))
+};
diff --git a/src/modules/publicip/publicip.h b/src/modules/publicip/publicip.h
new file mode 100644
index 0000000..92768cb
--- /dev/null
+++ b/src/modules/publicip/publicip.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include "option.h"
+
+#define FF_PUBLICIP_MODULE_NAME "PublicIp"
+
+void ffPreparePublicIp(FFPublicIPOptions* options);
+
+bool ffPrintPublicIp(FFPublicIPOptions* options);
+void ffInitPublicIpOptions(FFPublicIPOptions* options);
+void ffDestroyPublicIpOptions(FFPublicIPOptions* options);
+
+extern FFModuleBaseInfo ffPublicIPModuleInfo;