diff --git a/CHANGELOG.md b/CHANGELOG.md index b827262db..de07fc129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Features: * Support detecting brightness of external displays with DDC/CI (guard behind `--allow-slow-operations`) (Brightness) * Add option `--size-ndigits` and `--size-max-prefix` (#494) * Add option `--processing-timeout` to the timeout when waiting for child processes. +* Public IP module prints the IP location if `--publicip-url` is not set # 1.12.2 diff --git a/src/modules/publicip/publicip.c b/src/modules/publicip/publicip.c index 3b30e5b66..24d65ea3c 100644 --- a/src/modules/publicip/publicip.c +++ b/src/modules/publicip/publicip.c @@ -10,10 +10,17 @@ static FFNetworkingState state; static int status = -1; +static inline void wrapYyjsonFree(yyjson_doc** doc) +{ + assert(doc); + if (*doc) + yyjson_doc_free(*doc); +} + void ffPreparePublicIp(FFPublicIpOptions* options) { if (options->url.length == 0) - status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/ip", NULL); + status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/json", NULL); else { FF_STRBUF_AUTO_DESTROY host = ffStrbufCreateCopy(&options->url); @@ -56,6 +63,22 @@ void ffPrintPublicIp(FFPublicIpOptions* options) if (options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor); + + if (options->url.length == 0) + { + yyjson_doc* __attribute__((__cleanup__(wrapYyjsonFree))) doc = yyjson_read_opts(result.chars, result.length, 0, NULL, NULL); + if (doc) + { + yyjson_val* root = yyjson_doc_get_root(doc); + printf("%s (%s, %s)\n", + yyjson_get_str(yyjson_obj_get(root, "ip")), + yyjson_get_str(yyjson_obj_get(root, "city")), + yyjson_get_str(yyjson_obj_get(root, "country")) + ); + return; + } + } + ffStrbufPutTo(&result, stdout); } else