diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e7e08c..194ab8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +2026.07.25 - Decode Unicode escapes in !yt responses. (0.41.1) + +- Fixed `!yt` displaying raw JSON Unicode escape sequences (e.g. `\u0027` instead of `'`) in video titles and channel names. YouTube's oEmbed API returns non-ASCII characters as `\uXXXX` escapes, which were previously passed through as-is. +- Added `yt_hex_digit()` to convert a hex character to its numeric value. +- Added `yt_codepoint_to_utf8()` to encode a Unicode code point as UTF-8 bytes (handles U+0000 through U+10FFFF, producing 1-4 bytes). +- Added `yt_decode_unicode_escapes()` to scan a string for `\uXXXX` sequences and replace each with the corresponding UTF-8 bytes in-place. Safe for in-place modification because UTF-8 output is always shorter than the 6-byte escape. +- Both title and author fields are now decoded after extraction from the JSON response. +- Added doc comments to all three new helper functions. + 2026.07.25 - YouTube title lookup command, json_find docs. (0.41.0) - Added `!yt ` command: fetches the title and channel name of a YouTube video using the YouTube oEmbed API (`https://www.youtube.com/oembed`). The video URL is percent-encoded via a new `yt_url_encode()` helper before being passed as a query parameter. The JSON response is parsed for `title` and `author_name` fields using the existing `json_find()` helper, and the result is formatted as `"Title — Channel"`. diff --git a/CMakeLists.txt b/CMakeLists.txt index a281ec3..f1c6a05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -project(zynk VERSION 0.41.0 LANGUAGES C) +project(zynk VERSION 0.41.1 LANGUAGES C) # Prefer C99; the code is compatible with C99/C11 set(CMAKE_C_STANDARD 99) diff --git a/cmd_yt.h b/cmd_yt.h index 0acb3bd..815dbf2 100644 --- a/cmd_yt.h +++ b/cmd_yt.h @@ -17,6 +17,70 @@ * * Returns: void (output is written to dst, NUL-terminated). */ +/* + * yt_hex_digit - Convert a hexadecimal character to its numeric value. + * + * Parameters: + * c - A character ('0'-'9', 'a'-'f', or 'A'-'F'). + * + * Returns: The numeric value (0-15), or -1 if not a valid hex digit. + */ +static int yt_hex_digit(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +/* + * yt_codepoint_to_utf8 - Encode a Unicode code point as UTF-8. + * + * Parameters: + * cp - The Unicode code point (U+0000 to U+10FFFF). + * out - Output buffer for the UTF-8 bytes (at least 4 bytes needed). + * + * Returns: The number of UTF-8 bytes written (1-4). + */ +static size_t yt_codepoint_to_utf8(unsigned int cp, char *out) { + if (cp < 0x80) { out[0] = (char)cp; return 1; } + if (cp < 0x800) { out[0] = (char)(0xC0 | (cp >> 6)); out[1] = (char)(0x80 | (cp & 0x3F)); return 2; } + if (cp < 0x10000) { out[0] = (char)(0xE0 | (cp >> 12)); out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); out[2] = (char)(0x80 | (cp & 0x3F)); return 3; } + out[0] = (char)(0xF0 | (cp >> 18)); out[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); + out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); out[3] = (char)(0x80 | (cp & 0x3F)); return 4; +} + +/* + * yt_decode_unicode_escapes - Decode \\uXXXX JSON Unicode escapes in-place. + * + * Scans a string for JSON Unicode escape sequences (e.g. \\u0027 for single + * quote) and replaces each with the corresponding UTF-8 bytes. Safe to use + * in-place because UTF-8 encoding of any code point is never longer than the + * 6-byte \\uXXXX escape. + * + * Parameters: + * s - The string to decode (modified in-place, must be writable). + * + * Returns: void. + */ +static void yt_decode_unicode_escapes(char *s) { + char *r = s, *w = s; + while (*r) { + if (r[0] == '\\' && r[1] == 'u' && yt_hex_digit(r[2]) >= 0 && yt_hex_digit(r[3]) >= 0 && + yt_hex_digit(r[4]) >= 0 && yt_hex_digit(r[5]) >= 0) { + unsigned int cp = ((unsigned int)yt_hex_digit(r[2]) << 12) | ((unsigned int)yt_hex_digit(r[3]) << 8) | + ((unsigned int)yt_hex_digit(r[4]) << 4) | (unsigned int)yt_hex_digit(r[5]); + char utf8[5]; + size_t n = yt_codepoint_to_utf8(cp, utf8); + memcpy(w, utf8, n); + w += n; + r += 6; + } else { + *w++ = *r++; + } + } + *w = '\0'; +} + static void yt_url_encode(const char *src, char *dst, size_t dstsz) { static const char hex[] = "0123456789ABCDEF"; size_t di = 0; @@ -87,6 +151,7 @@ int cmd_yt(Session *s, const char *msg, const char *reply_target, const char *sr if (tlen > 255) tlen = 255; memcpy(title, title_s, tlen); title[tlen] = '\0'; + yt_decode_unicode_escapes(title); } else { snprintf(title, sizeof title, "(parse error)"); } @@ -104,6 +169,7 @@ int cmd_yt(Session *s, const char *msg, const char *reply_target, const char *sr char author[128]; memcpy(author, author_s, alen); author[alen] = '\0'; + yt_decode_unicode_escapes(author); snprintf(resp, sizeof resp, "%s — %s", title, author); } else { snprintf(resp, sizeof resp, "%s", title);