Added new !yt command (cmd_yt.h) that fetches the title and channel name of a YouTube video using the YouTube oEmbed API. 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'. - New file: cmd_yt.h with yt_url_encode() and cmd_yt() command handler - CMakeLists.txt: added WITH_YT feature toggle (ON by default) - zynk.c: added conditional #include for cmd_yt.h - irc.h: added cmd_yt() dispatch in handle_privmsg() - cmd_help.h: added !yt <url> to the help listing - zynk.c: added doc comments to json_find() function - cmd_yt.h: added doc comments to yt_url_encode() and cmd_yt() - CHANGELOG.md: added 0.41.0 entry documenting all changes - README.md: updated feature summary, features list, feature toggles table, minimal build command, and IRC commands utilities table
120 lines
3.8 KiB
C
120 lines
3.8 KiB
C
#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 <url>" 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 <youtube-url>");
|
|
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
|