From 18dfa64accacc27c2a2def4ce411fe72481f0d41 Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 20 Jul 2026 05:06:50 +0200 Subject: [PATCH] docs: document all undocumented functions, add built-in timezone table to README - Added doc comments (/* ... */) to all 14 undocumented static functions in zynk.c matching the existing documentation style (function name, description, Parameters, Returns sections): * DB ops: seen_update, seen_lookup, tell_add, tell_deliver * String utils: valid_irc_word, normalize_city, collapse_spaces * Command handlers: cmd_reload, cmd_time, cmd_seen, cmd_tell, cmd_greeting_or_chat * Timezone helpers: tz_lookup, tz_format - Added complete reference table of all 45 built-in timezones to README.md with region, UTC offset, and DST support status, so users know which timezones work without tzdata installed. - Updated CHANGELOG.md v0.35.5 entry to reflect these additions. - Clean build, zero warnings. --- CHANGELOG.md | 2 + README.md | 47 ++++++++++- zynk.c | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef68bc2..7e811f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ 2026.07.20 – Fix !time to work without tzdata. - Rewrote `!time` to try the system `localtime_r()` first, then fall back to a built-in timezone table with 45 IANA zones and simplified DST rules when the system can't resolve timezones (e.g. no `tzdata` installed). This means `!time Australia/Sydney` now shows correct AEST/AEDT even on minimal systems. +- Added doc comments to all undocumented functions in zynk.c: `seen_update`, `seen_lookup`, `tell_add`, `tell_deliver`, `valid_irc_word`, `normalize_city`, `collapse_spaces`, `cmd_reload`, `cmd_time`, `cmd_seen`, `cmd_tell`, `cmd_greeting_or_chat`, `tz_lookup`, `tz_format`. +- Added full list of built-in timezones to README.md (all 45 entries from `tz_table`) with region, offset, and DST status. - Version bumped to 0.35.5. 2026.07.20 – Add pi() to !calc. diff --git a/README.md b/README.md index 6158aff..4d43961 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,52 @@ Shows the current date and time. Without arguments, displays the bot's local tim | `!time Asia/Tokyo` | Japan time | | `!time Australia/Sydney` | Australian Eastern time | -Any timezone name from the IANA/Olson timezone database works. Common examples: `UTC`, `US/Eastern`, `US/Central`, `US/Pacific`, `Europe/Berlin`, `Europe/London`, `Europe/Paris`, `Asia/Tokyo`, `Asia/Shanghai`, `Asia/Kolkata`, `Australia/Sydney`, `Pacific/Auckland`. +If the system has `tzdata` installed, any IANA/Olson timezone name works. Otherwise, the bot falls back to a built-in table with the following timezones: + +**Built-in timezones (no tzdata required):** + +| Timezone | Region | Offset | DST | +|---|---|---|---| +| `UTC` / `GMT` | Global | +0 | No | +| `EST` / `CST` / `MST` / `PST` / `HST` | US (fixed) | -5 to -10 | No | +| `US/Eastern` | US Eastern | -5 / -4 | Yes | +| `US/Central` | US Central | -6 / -5 | Yes | +| `US/Mountain` | US Mountain | -7 / -6 | Yes | +| `US/Pacific` | US Pacific | -8 / -7 | Yes | +| `US/Alaska` | US Alaska | -9 / -8 | Yes | +| `US/Honolulu` | US Hawaii | -10 | No | +| `Canada/Toronto` | Canada Eastern | -5 / -4 | Yes | +| `Canada/Vancouver` | Canada Pacific | -8 / -7 | Yes | +| `America/Sao_Paulo` | Brazil | -3 / -2 | Yes | +| `America/Mexico_City` | Mexico | -6 / -5 | Yes | +| `America/Argentina/Buenos_Aires` | Argentina | -3 | No | +| `Europe/London` | UK | 0 / +1 | Yes | +| `Europe/Berlin` | Central Europe | +1 / +2 | Yes | +| `Europe/Paris` | France | +1 / +2 | Yes | +| `Europe/Moscow` | Russia | +3 | No | +| `Europe/Istanbul` | Turkey | +3 | No | +| `Asia/Tokyo` | Japan | +9 | No | +| `Asia/Shanghai` | China | +8 | No | +| `Asia/Kolkata` | India | +5:30 | No | +| `Asia/Dubai` | UAE | +4 | No | +| `Asia/Singapore` | Singapore | +8 | No | +| `Asia/Seoul` | South Korea | +9 | No | +| `Asia/Hong_Kong` | Hong Kong | +8 | No | +| `Asia/Taipei` | Taiwan | +8 | No | +| `Australia/Sydney` | Australia Eastern | +10 / +11 | Yes | +| `Australia/Melbourne` | Australia Eastern | +10 / +11 | Yes | +| `Australia/Brisbane` | Australia Eastern | +10 | No | +| `Australia/Perth` | Australia Western | +8 | No | +| `Australia/Adelaide` | Australia Central | +9:30 / +10:30 | Yes | +| `Australia/Darwin` | Australia Central | +9:30 | No | +| `Pacific/Auckland` | New Zealand | +12 / +13 | Yes | +| `Pacific/Honolulu` | Hawaii | -10 | No | +| `Pacific/Fiji` | Fiji | +12 / +13 | Yes | +| `Africa/Cairo` | Egypt | +2 | No | +| `Africa/Johannesburg` | South Africa | +2 | No | +| `Africa/Lagos` | West Africa | +1 | No | +| `Africa/Nairobi` | East Africa | +3 | No | +| `Atlantic/Reykjavik` | Iceland | +0 | No | #### Calculator (`!calc`) diff --git a/zynk.c b/zynk.c index 9c96e9a..c2a2bfe 100644 --- a/zynk.c +++ b/zynk.c @@ -585,6 +585,21 @@ static void ai_reset(long long id) { /* ---- DB seen/tell operations ---- */ +/* + * seen_update - Record a nick's last-seen timestamp and context. + * + * Upserts a row in the seen table: inserts a new record if the nick has + * never been seen, or updates the existing record with the current UTC + * timestamp, channel, and action. Uses INSERT ... ON CONFLICT DO UPDATE. + * + * Parameters: + * nick - The IRC nick to record. + * channel - The channel where the nick was seen (or "" for DMs/QUIT). + * action - A short description of what the nick did (e.g. "spoke", + * "joined", "left"). Defaults to "spoke" if NULL. + * + * Returns: void. + */ static void seen_update(const char *nick, const char *channel, const char *action) { void *stmt; const char *sql = "INSERT INTO seen(nick,last_seen,last_channel,last_action)" @@ -599,6 +614,20 @@ static void seen_update(const char *nick, const char *channel, const char *actio db_finalize(stmt); } +/* + * seen_lookup - Retrieve last-seen info for a given nick. + * + * Queries the seen table for the most recent timestamp, channel, and + * action recorded for the given nick. Returns a human-readable string + * suitable for sending directly to IRC. + * + * Parameters: + * nick - The IRC nick to look up. + * + * Returns: A newly allocated string like " was last seen + * ", or NULL if the nick was never seen. + * Caller must free() the result. + */ static char *seen_lookup(const char *nick) { void *stmt; const char *sql = "SELECT last_seen, last_channel, last_action FROM seen WHERE nick=?"; @@ -614,6 +643,20 @@ static char *seen_lookup(const char *nick) { return r; } +/* + * tell_add - Queue a message for later delivery to a nick. + * + * Inserts a pending message into the tell table, recording the sender, + * recipient, and message text. The message will be delivered the next + * time the recipient speaks (via tell_deliver). + * + * Parameters: + * from - The IRC nick of the sender. + * to - The IRC nick of the intended recipient. + * msg - The message text to deliver. + * + * Returns: void. + */ static void tell_add(const char *from, const char *to, const char *msg) { void *stmt; const char *sql = "INSERT INTO tell(from_nick,to_nick,message) VALUES(?,?,?)"; @@ -625,6 +668,21 @@ static void tell_add(const char *from, const char *to, const char *msg) { db_finalize(stmt); } +/* + * tell_deliver - Deliver all pending tell messages for a nick. + * + * Queries the tell table for all messages addressed to the given nick, + * sends each one to the specified channel via irc_msg in the format + * ": message from : ", and deletes each row after + * delivery. Messages are delivered in insertion order (lowest ID first). + * + * Parameters: + * s - The IRC session. + * nick - The nick whose pending messages should be delivered. + * channel - The channel to send the delivery messages to. + * + * Returns: void. + */ static void tell_deliver(Session *s, const char *nick, const char *channel) { void *stmt; const char *sql = "SELECT id, from_nick, message FROM tell WHERE to_nick=? ORDER BY id ASC"; @@ -1436,6 +1494,18 @@ static int has_ctl(const char *s) { return 0; } +/* + * valid_irc_word - Check if a string is a single valid IRC word. + * + * A valid IRC word is non-NULL, non-empty, contains no CRLF characters, + * and contains no spaces or tabs. Used to validate IRC nicknames, + * channel names, and command arguments before database operations. + * + * Parameters: + * s - The string to validate. + * + * Returns: 1 if the string is a valid single word, 0 otherwise. + */ static int valid_irc_word(const char *s) { if (!s || !*s || has_crlf(s)) return 0; for (const char *p = s; *p; p++) @@ -1443,6 +1513,24 @@ static int valid_irc_word(const char *s) { return 1; } +/* + * normalize_city - Normalize a user-provided city name for display and URLs. + * + * Produces two output forms: a display form with spaces, and a URL form + * with '+' replacing spaces. Strips non-alphanumeric characters except + * dots, commas, hyphens, and underscores. Collapses multiple spaces into + * single spaces. Defaults to "Berlin" if the input is empty or results + * in an empty string after sanitization. + * + * Parameters: + * in - The raw city name from the user. + * display - Output buffer for the display form (e.g. "New York"). + * dsz - Size of the display buffer. + * url - Output buffer for the URL form (e.g. "New+York"). + * usz - Size of the URL buffer. + * + * Returns: void. + */ static void normalize_city(const char *in, char *display, size_t dsz, char *url, size_t usz) { size_t dl = 0, ul = 0; int pending_space = 0; @@ -1472,6 +1560,18 @@ static void normalize_city(const char *in, char *display, size_t dsz, char *url, } } +/* + * collapse_spaces - Collapse runs of whitespace into single spaces. + * + * Modifies a string in place, replacing each run of one or more spaces + * with a single space. Leading and trailing spaces are also removed. + * No-op if the string is NULL or empty. + * + * Parameters: + * s - The string to modify in place. + * + * Returns: void. + */ static void collapse_spaces(char *s) { char *r = s, *w = s; int space = 0; @@ -2508,6 +2608,23 @@ static int cmd_uptime(Session *s, const char *msg, const char *reply_target, con return 1; } +/* + * cmd_reload - Handle the "!reload" command. + * + * Requires channel op if used in a channel. Rate-limited. Triggers + * reload_do() which serializes bot state, exec's a fresh copy of the + * binary, and restores state. This allows updating the bot without + * disconnecting from IRC. + * + * Parameters: + * s - The IRC session. + * msg - The raw IRC message text. + * reply_target - Channel or nick to send the response to. + * src_nick - The nick of the user who issued the command. + * tgt - The channel or nick the message was sent to. + * + * Returns: 1 if the command was matched, 0 otherwise. + */ static int cmd_reload(Session *s, const char *msg, const char *reply_target, const char *src_nick, const char *tgt) { if (strcmp(msg, "!reload") != 0) return 0; if (tgt[0] == '#' && !chan_is_op(tgt, src_nick)) { @@ -2667,12 +2784,43 @@ static const TzEntry tz_table[] = { {"Atlantic/Reykjavik", 0, 0, 0}, }; +/* + * tz_lookup - Look up a timezone entry by IANA name. + * + * Searches the static tz_table[] array for a matching timezone name + * using case-insensitive comparison. Used as a fallback when the system + * lacks tzdata and cannot resolve IANA timezone names via localtime_r. + * + * Parameters: + * name - The IANA timezone name to look up (e.g. "Europe/Berlin"). + * + * Returns: A pointer to the matching TzEntry, or NULL if not found. + */ static const TzEntry *tz_lookup(const char *name) { for (size_t i = 0; i < sizeof tz_table / sizeof tz_table[0]; i++) if (strcasecmp(tz_table[i].name, name) == 0) return &tz_table[i]; return NULL; } +/* + * tz_format - Format a timestamp with timezone abbreviation. + * + * Converts a Unix epoch timestamp plus UTC offset into a human-readable + * string like "2025-07-20 14:30:00 CEST". Maps internal timezone names + * to their correct abbreviations, handling DST-aware names (e.g. + * "Europe/Berlin" becomes "CEST" in summer, "CET" in winter). Falls + * back to "UTC" for offset 0 if no other abbreviation matches. + * + * Parameters: + * epoch - The Unix epoch timestamp to format. + * offset - The UTC offset in seconds (positive for east of UTC). + * tzname - The IANA timezone name used for abbreviation lookup. + * is_dst - Whether DST is currently in effect for this timezone. + * buf - Output buffer for the formatted string. + * buflen - Size of the output buffer. + * + * Returns: void (result written to buf). + */ static void tz_format(time_t epoch, int offset, const char *tzname, int is_dst, char *buf, size_t buflen) { time_t local = epoch + offset; struct tm tm; @@ -2741,6 +2889,23 @@ static void tz_format(time_t epoch, int offset, const char *tzname, int is_dst, tm.tm_hour, tm.tm_min, tm.tm_sec, abbr); } +/* + * cmd_time - Handle the "!time [timezone]" command. + * + * Without arguments, returns the bot's local time. With a timezone + * argument, first tries the system setenv("TZ")/localtime_r() approach. + * If the system returns UTC for a non-UTC timezone (indicating missing + * tzdata), falls back to the built-in tz_table with hemisphere-aware + * DST rules. Rate-limited. + * + * Parameters: + * s - The IRC session. + * msg - The raw IRC message text. + * reply_target - Channel or nick to send the response to. + * src_nick - The nick of the user who issued the command. + * + * Returns: 1 if the command was matched, 0 otherwise. + */ static int cmd_time(Session *s, const char *msg, const char *reply_target, const char *src_nick) { if (strncmp(msg, "!time", 5) != 0) return 0; if (msg[5] != ' ' && msg[5] != '\0') return 0; @@ -2804,6 +2969,23 @@ static int cmd_time(Session *s, const char *msg, const char *reply_target, const return 1; } +/* + * cmd_seen - Handle the "!seen " command. + * + * Looks up the given nick in the seen database and reports when they + * were last seen, in which channel, and what action they were performing. + * Special-cases: checking yourself returns "You are right here, duh!", + * checking the bot returns "I am right here!", and unknown nicks return + * "I have never seen ". Rate-limited. + * + * Parameters: + * s - The IRC session. + * msg - The raw IRC message text. + * reply_target - Channel or nick to send the response to. + * src_nick - The nick of the user who issued the command. + * + * Returns: 1 if the command was matched, 0 otherwise. + */ static int cmd_seen(Session *s, const char *msg, const char *reply_target, const char *src_nick) { if (strncmp(msg, "!seen", 5) != 0) return 0; if (msg[5] != ' ') return 0; @@ -2839,6 +3021,22 @@ static int cmd_seen(Session *s, const char *msg, const char *reply_target, const return 1; } +/* + * cmd_tell - Handle the "!tell " command. + * + * Stores a pending message in the tell table to be delivered to the + * target nick when they next speak. Validates that the sender is not + * trying to tell themselves something. Confirms with "Ok, I'll tell + * when they're around." Rate-limited. + * + * Parameters: + * s - The IRC session. + * msg - The raw IRC message text. + * reply_target - Channel or nick to send the response to. + * src_nick - The nick of the user who issued the command. + * + * Returns: 1 if the command was matched, 0 otherwise. + */ static int cmd_tell(Session *s, const char *msg, const char *reply_target, const char *src_nick) { if (strncmp(msg, "!tell", 5) != 0) return 0; if (msg[5] != ' ') return 0; @@ -2870,6 +3068,24 @@ static int cmd_tell(Session *s, const char *msg, const char *reply_target, const return 1; } +/* + * cmd_greeting_or_chat - Handle informal greetings and chat messages. + * + * Strips the bot's nick prefix if present (e.g. "zynk: hello") and + * matches against a fixed set of greetings/phrases: "hi", "hey", + * "hello", "bye", "goodbye", "jojo", "whatsup", "waddup", and a few + * German phrases. Responds with a canned reply for each. If no fixed + * phrase matches but the message was addressed by nick, forwards it + * to the AI as a chat question via ai_ask. + * + * Parameters: + * s - The IRC session. + * msg - The raw IRC message text. + * reply_target - Channel or nick to send the response to. + * src_nick - The nick of the user who sent the message. + * + * Returns: void. + */ static void cmd_greeting_or_chat(Session *s, const char *msg, const char *reply_target, const char *src_nick) { int nicklen = strlen(s->nick); const char *cmd = msg;