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.
This commit is contained in:
parent
12e9e51967
commit
18dfa64acc
3 changed files with 264 additions and 1 deletions
216
zynk.c
216
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 "<nick> was last seen <action>
|
||||
* <channel> <timestamp>", 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
|
||||
* "<nick>: message from <sender>: <text>", 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 <nick>" 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 <nick>". 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 <nick> <message>" 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
|
||||
* <nick> 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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue