#ifndef CMD_YT_H #define CMD_YT_H #include "zynk.h" /* * yt_url_encode - Percent-encode a string for safe use in a URL query parameter. * * Encodes all characters except unreserved characters (alphanumerics, '-', '_', * '.', '~') using RFC 3986 percent-encoding. Used to encode the YouTube video * URL before embedding it in the oEmbed API request. * * Parameters: * src - The input string to encode. * dst - Output buffer for the encoded string. * dstsz - Size of the output buffer. * * Returns: void (output is written to dst, NUL-terminated). */ static void yt_url_encode(const char *src, char *dst, size_t dstsz) { static const char hex[] = "0123456789ABCDEF"; size_t di = 0; for (; *src && di + 4 < dstsz; src++) { unsigned char c = (unsigned char)*src; if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { dst[di++] = c; } else { dst[di++] = '%'; dst[di++] = hex[c >> 4]; dst[di++] = hex[c & 0x0F]; } } dst[di] = '\0'; } /* * cmd_yt - Handle the "!yt " command. * * Fetches the title and author of a YouTube video using the YouTube oEmbed * API (https://www.youtube.com/oembed). The video URL is percent-encoded * and passed as a query parameter. The JSON response is parsed for the * "title" and "author_name" fields, and the result is formatted as * "Title — Author" and sent to IRC. Rate-limited. * * Parameters: * s - The IRC session. * msg - The raw message text. * reply_target - Channel or nick to reply to. * src_nick - The nick of the user who sent the command. * * Returns: 1 if matched, 0 otherwise. */ int cmd_yt(Session *s, const char *msg, const char *reply_target, const char *src_nick) { if (strncmp(msg, "!yt", 3) != 0) return 0; if (msg[3] != ' ' && msg[3] != '\0') return 0; if (rate_limit_check(src_nick, reply_target) < 0) return 1; const char *rest = msg[3] == ' ' ? msg + 4 : ""; while (*rest == ' ') rest++; if (!*rest) { irc_reply(s, reply_target, src_nick, "Usage: !yt "); return 1; } char url_buf[512]; int i = 0; while (rest[i] && rest[i] != ' ' && i < 500) { url_buf[i] = rest[i]; i++; } url_buf[i] = '\0'; if (strncmp(url_buf, "http://", 7) != 0 && strncmp(url_buf, "https://", 8) != 0) { irc_reply(s, reply_target, src_nick, "Please provide a valid URL"); return 1; } char encoded[1024]; yt_url_encode(url_buf, encoded, sizeof encoded); char api_url[1536]; snprintf(api_url, sizeof api_url, "https://www.youtube.com/oembed?url=%s&format=json", encoded); char *json = fetch_url(api_url); if (!json) { irc_reply(s, reply_target, src_nick, "YouTube: could not fetch video info"); return 1; } const char *title_s = json_find(json, "\"title\":"); char title[256]; if (title_s && title_s[0] == '"') { title_s++; const char *tq = strchr(title_s, '"'); if (tq) { size_t tlen = tq - title_s; if (tlen > 255) tlen = 255; memcpy(title, title_s, tlen); title[tlen] = '\0'; } else { snprintf(title, sizeof title, "(parse error)"); } } else { snprintf(title, sizeof title, "(no title found)"); } const char *author_s = json_find(json, "\"author_name\":"); char resp[512]; if (author_s && author_s[0] == '"') { author_s++; const char *aq = strchr(author_s, '"'); if (aq) { size_t alen = aq - author_s; if (alen > 127) alen = 127; char author[128]; memcpy(author, author_s, alen); author[alen] = '\0'; snprintf(resp, sizeof resp, "%s — %s", title, author); } else { snprintf(resp, sizeof resp, "%s", title); } } else { snprintf(resp, sizeof resp, "%s", title); } free(json); irc_reply(s, reply_target, src_nick, resp); log_stamp(); fprintf(stderr, CLR_MAGENTA "YT %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, resp); return 1; } #endif