diff --git a/CHANGELOG.md b/CHANGELOG.md index ad73890..8e4e72e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,18 @@ +2026.07.23 - Full forecast mode, 24h sunrise/moon times, startup version. (0.40.5) + +- Added `full` keyword to `!forecast`: `!forecast frankfurt full` shows per-day detail lines with weather description, sunrise/sunset, and moonrise/moonset in 24-hour format, sent as separate IRC messages. +- Added `to_24h()` helper to convert wttr.in's 12-hour AM/PM astronomy times to 24-hour format. +- Fixed the missing first letter of weather descriptions in full mode (`"value": "` offset was 11 instead of 10). +- Startup message now includes the version: `[zynk 0.40.5] starting up`. +- Added doc comments to `to_24h()`, `find_day_obj()`, and other forecast helper functions. +- Fixed "Not implemented..." fallback to only trigger on `!` followed by a letter (e.g. `!foo`), so bare `!` or `!!!` no longer produce the message. + 2026.07.23 - Raise AI code mode limit, simplify plan mode prompt. (0.40.4) - Increased AI code mode response limit back to 2000 chars (was reduced to 1000 in 0.40.3, but code changes need more room). - Simplified the AI plan mode system prompt further: removed the explicit "tell them you are in plan mode" instruction, keeping it to a short "You are in plan mode. Answer briefly." directive. - Fixed `!gitlog` default count in README.md from 5 to 1 (matching the actual `GITLOG_COUNT` value set in 0.38.2). -- Re-aligned the utilities command table in README.md for cleaner formatting. +- Re-aligned the utility command table in README.md for cleaner formatting. - Minor grammar and punctuation fixes across older changelog entries. 2026.07.23 - Simplify CMake feature loop, adjust AI response limits. (0.40.3) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7848f3..3713340 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -project(zynk VERSION 0.40.4 LANGUAGES C) +project(zynk VERSION 0.40.5 LANGUAGES C) # Prefer C99; the code is compatible with C99/C11 set(CMAKE_C_STANDARD 99) diff --git a/README.md b/README.md index be03679..45ae054 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# zynk - Companion AI Logic Chatbot +# zynk - An IRC bot -A single-file IRC bot in C. Features a key-value store backed by SQLite3, weather lookups via wttr.in, stock quotes, calculator, time, user tracking, and AI-powered replies. +An IRC bot in C. Features a key-value store backed by SQLite3, weather lookups via wttr.in, stock quotes, calculator, time, user tracking, and AI-powered replies. ## Features @@ -60,29 +60,29 @@ cmake --build build Available options (the `!command` shown in parentheses): -| Option | Command | Description | Default | -|-------------------------|------------------|----------------------------------|---------| -| `WITH_ZYNK` | `!zynk` | Key-value store | ON | -| `WITH_PING` | `!ping` | Ping/pong | ON | -| `WITH_VERSION` | `!version` | Version info | ON | -| `WITH_HELP` | `!help` | Help listing | ON | -| `WITH_QUIT` | `!quit` / `!die` | Shut down the bot | ON | -| `WITH_WEATHER` | `!weather` | Weather lookup (requires curl) | ON | -| `WITH_FORECAST` | `!forecast` | 3-day forecast (requires curl) | ON | -| `WITH_STOCK` | `!stock` | Stock quotes (requires curl) | ON | -| `WITH_CALC` | `!calc` | Calculator (requires bc) | ON | -| `WITH_TIME` | `!time` | Time/timezone lookup | ON | -| `WITH_SEEN` | `!seen` | User tracking | ON | -| `WITH_TELL` | `!tell` | Offline messages | ON | -| `WITH_GITLOG` | `!gitlog` | Recent git commits | ON | -| `WITH_CHANGELOG` | `!changelog` | Changelog entries | ON | -| `WITH_AI` | `!ai` | AI questions (requires opencode) | ON | -| `WITH_CODE` | `!code` | AI code changes (ops only) | OFF | -| `WITH_REBUILD` | `!rebuild` | Rebuild & restart (ops only) | OFF | -| `WITH_UPTIME` | `!uptime` | Uptime display | ON | -| `WITH_RELOAD` | `!reload` | Hot reload (ops only) | ON | -| `WITH_RESTART` | `!restart` | Restart (ops only) | ON | -| `WITH_GREETING_OR_CHAT` | `: ` | Greeting/chat with AI fallback | ON | +| Option | Command | Description | Default | +|-------------------------|------------------|----------------------------------|--------------------------| +| `WITH_ZYNK` | `!zynk` | Key-value store | ON | +| `WITH_PING` | `!ping` | Ping/pong | ON | +| `WITH_VERSION` | `!version` | Version info | ON | +| `WITH_HELP` | `!help` | Help listing | ON | +| `WITH_QUIT` | `!quit` / `!die` | Shut down the bot | ON | +| `WITH_WEATHER` | `!weather` | Weather lookup (requires curl) | ON | +| `WITH_FORECAST` | `!forecast` | 3-day forecast (requires curl) | ON | +| `WITH_STOCK` | `!stock` | Stock quotes (requires curl) | ON | +| `WITH_CALC` | `!calc` | Calculator (requires bc) | ON | +| `WITH_TIME` | `!time` | Time/timezone lookup | ON | +| `WITH_SEEN` | `!seen` | User tracking | ON | +| `WITH_TELL` | `!tell` | Offline messages | ON | +| `WITH_GITLOG` | `!gitlog` | Recent git commits | ON | +| `WITH_CHANGELOG` | `!changelog` | Changelog entries | ON | +| `WITH_AI` | `!ai` | AI questions (requires opencode) | ON (can not be disabled) | +| `WITH_CODE` | `!code` | AI code changes (ops only) | OFF | +| `WITH_REBUILD` | `!rebuild` | Rebuild & restart (ops only) | OFF | +| `WITH_UPTIME` | `!uptime` | Uptime display | ON | +| `WITH_RELOAD` | `!reload` | Hot reload (ops only) | ON | +| `WITH_RESTART` | `!restart` | Restart (ops only) | ON | +| `WITH_GREETING_OR_CHAT` | `: ` | Greeting/chat with AI fallback | ON (can not be disabled) | ### Minimal build @@ -153,11 +153,11 @@ rm -rf build ### Weather & Finance -| Command | Description | -|-------------------------------|---------------------------------------------------------| -| `!weather [city]` | Current weather (default: Berlin) | -| `!forecast [city]` | 3-day forecast (default: Berlin) | -| `!stock [,SYMBOL...]` | Stock quotes, up to 3 symbols (e.g. `!stock AAPL,MSFT`) | +| Command | Description | +|-------------------------------|-----------------------------------------------------------------------------------------------------| +| `!weather [city]` | Current weather (default: Berlin) | +| `!forecast [city] [full]` | 3-day forecast; `full` adds weather description, sunrise/sunset, moonrise/moonset (default: Berlin) | +| `!stock [,SYMBOL...]` | Stock quotes, up to 3 symbols (e.g. `!stock AAPL,MSFT`) | ### Utilities diff --git a/cmd_forecast.h b/cmd_forecast.h index bc0c6a1..4b6afe0 100644 --- a/cmd_forecast.h +++ b/cmd_forecast.h @@ -3,6 +3,150 @@ #include "zynk.h" +/* + * parse_forecast_day - Parse a single day object from the wttr.in JSON. + * + * Extracts date, avg temperature, and weather condition from one day + * object within the "weather" array. Used by both fetch_forecast() and + * fetch_forecast_full(). + * + * Parameters: + * p - Pointer to the start of the day JSON object. + * obj_end - Pointer to one past the end of the day object. + * date_out - Buffer to receive the parsed date string (YYYY-MM-DD). + * temp_out - Buffer to receive the average temperature. + * cond_out - Buffer to receive the weather condition string. + * + * Returns: 1 on success, 0 on failure. + */ +static int parse_forecast_day(const char *p, const char *obj_end, + char *date_out, size_t date_sz, + char *temp_out, size_t temp_sz, + char *cond_out, size_t cond_sz) { + date_out[0] = temp_out[0] = cond_out[0] = 0; + const char *db = strstr(p, "\"date\": \""); + if (db && db < obj_end) { db += 9; const char *dq = strchr(db, '"'); if (dq && dq < obj_end) { size_t l = dq - db; if (l >= date_sz) l = date_sz - 1; memcpy(date_out, db, l); date_out[l] = 0; } } + const char *tb = strstr(p, "\"avgtempC\": \""); + if (tb && tb < obj_end) { tb += 13; const char *tq = strchr(tb, '"'); if (tq && tq < obj_end) { size_t l = tq - tb; if (l >= temp_sz) l = temp_sz - 1; memcpy(temp_out, tb, l); temp_out[l] = 0; } } + const char *hb = strstr(p, "\"hourly\""); + if (hb && hb < obj_end) { + hb = strchr(hb, '['); + if (hb && hb < obj_end) { + hb++; + const char *vb = strstr(hb, "\"value\": \""); + if (vb && vb < obj_end) { vb += 9; const char *vq = strchr(vb, '"'); if (vq && vb < obj_end) { size_t l = vq - vb; if (l >= cond_sz) l = cond_sz - 1; memcpy(cond_out, vb, l); cond_out[l] = 0; } } + } + } + return (date_out[0] != 0) ? 1 : 0; +} + +/* + * find_day_obj - Find the i-th day object in the weather JSON array. + * + * Scans the JSON string for the i-th '{' ... '}' block at the top level + * of the "weather" array. Sets obj_end to one past the closing '}'. + * + * Parameters: + * p - Pointer into the JSON string (start of the weather array). + * idx - Zero-based index of the day object to find (0, 1, or 2). + * obj_end - Output pointer set to one past the closing '}' of the + * matched day object. + * + * Returns: Pointer to the opening '{' of the matched day object, or NULL + * if fewer than idx+1 day objects exist. + */ +static const char *find_day_obj(const char *p, int idx, const char **obj_end) { + int count = 0; + while (*p) { + if (*p == '{') { + if (count == idx) { + *obj_end = p + 1; + int depth = 1; + while (**obj_end && depth > 0) { if (**obj_end == '{') depth++; else if (**obj_end == '}') depth--; (*obj_end)++; } + return p; + } + count++; + } + p++; + } + return NULL; +} + +/* + * to_24h - Convert a 12-hour AM/PM time string to 24-hour format. + * + * Parses a time string in "HH:MM AM" or "HH:MM PM" format (as returned + * by wttr.in's astronomy fields) and overwrites the buffer with the + * equivalent 24-hour "HH:MM" string. Handles midnight (12 AM -> 00) + * and noon (12 PM -> 12) correctly. If the string is too short or + * does not end with a recognized AM/PM suffix, the buffer is left + * unchanged. + * + * Parameters: + * buf - Buffer containing the null-terminated 12-hour time string. + * Modified in place on success. + * sz - Size of the buffer. + */ +static void to_24h(char *buf, size_t sz) { + size_t len = strlen(buf); + if (len < 5) return; + char mm[4]; + memcpy(mm, buf + 3, 3); + const char *ampm = buf + len - 2; + if ((ampm[0] == 'A' || ampm[0] == 'a') && (ampm[1] == 'M' || ampm[1] == 'm')) { + int h = (buf[0] - '0') * 10 + (buf[1] - '0'); + if (h == 12) h = 0; + snprintf(buf, sz, "%02d:%s", h, mm); + } else if ((ampm[0] == 'P' || ampm[0] == 'p') && (ampm[1] == 'M' || ampm[1] == 'm')) { + int h = (buf[0] - '0') * 10 + (buf[1] - '0'); + if (h != 12) h += 12; + snprintf(buf, sz, "%02d:%s", h, mm); + } +} + +/* + * extract_astronomy - Extract sunrise/sunset/moonrise/moonset from a day object. + * + * Parameters: + * p, obj_end - The day object boundaries. + * sunrise, sunset - Buffers for sunrise/sunset strings. + * moonrise, moonset - Buffers for moonrise/moonset strings. + */ +static void extract_astronomy(const char *p, const char *obj_end, + char *sunrise, size_t sr_sz, + char *sunset, size_t ss_sz, + char *moonrise, size_t mr_sz, + char *moonset, size_t ms_sz) { + sunrise[0] = sunset[0] = moonrise[0] = moonset[0] = 0; + const char *ab = strstr(p, "\"astronomy\""); + if (!ab || ab >= obj_end) return; + ab = strchr(ab, '['); + if (!ab || ab >= obj_end) return; + ab++; + const char *ae = ab; + int depth = 1; + while (*ae && depth > 0) { if (*ae == '[') depth++; else if (*ae == ']') depth--; ae++; } + const char *fields[][2] = { + { "\"sunrise\": \"", NULL }, { "\"sunset\": \"", NULL }, + { "\"moonrise\": \"", NULL }, { "\"moonset\": \"", NULL }, + }; + size_t sizes[] = { sr_sz, ss_sz, mr_sz, ms_sz }; + char *dsts[] = { sunrise, sunset, moonrise, moonset }; + for (int f = 0; f < 4; f++) { + const char *fb = strstr(ab, fields[f][0]); + if (fb && fb < ae) { + fb += strlen(fields[f][0]); + const char *fq = strchr(fb, '"'); + if (fq && fq < ae) { + size_t l = fq - fb; + if (l >= sizes[f]) l = sizes[f] - 1; + memcpy(dsts[f], fb, l); + dsts[f][l] = 0; + } + } + } +} + /* * fetch_forecast - Fetch and parse a 3-day weather forecast from wttr.in. * @@ -39,25 +183,11 @@ char *fetch_forecast(const char *city_url, const char *city_display) { char line[3][256], fmt_date[3][32]; int ndays = 0; for (int i = 0; i < 3; i++) { - p = strchr(p, '{'); - if (!p) break; - const char *obj_end = p + 1; - int depth = 1; - while (*obj_end && depth > 0) { if (*obj_end == '{') depth++; else if (*obj_end == '}') depth--; obj_end++; } + const char *obj_end = NULL; + const char *day = find_day_obj(p, 0, &obj_end); + if (!day) break; char date[16]="", temp[8]="", cond[64]=""; - char *db = strstr(p, "\"date\": \""); - if (db && db < obj_end) { db += 9; char *dq = strchr(db, '"'); if (dq && dq < obj_end) { size_t l = dq - db; if (l>15) l=15; memcpy(date,db,l); date[l]=0; } } - char *tb = strstr(p, "\"avgtempC\": \""); - if (tb && tb < obj_end) { tb += 13; char *tq = strchr(tb, '"'); if (tq && tq < obj_end) { size_t l = tq - tb; if (l>7) l=7; memcpy(temp,tb,l); temp[l]=0; } } - char *hb = strstr(p, "\"hourly\""); - if (hb && hb < obj_end) { - hb = strchr(hb, '['); - if (hb && hb < obj_end) { - hb++; - char *vb = strstr(hb, "\"value\": \""); - if (vb && vb < obj_end) { vb += 9; char *vq = strchr(vb, '"'); if (vq && vq < obj_end) { size_t l = vq - vb; if (l>63) l=63; memcpy(cond,vb,l); cond[l]=0; } } - } - } + parse_forecast_day(day, obj_end, date, sizeof date, temp, sizeof temp, cond, sizeof cond); struct tm tm = {0}; if (strptime(date, "%Y-%m-%d", &tm)) { mktime(&tm); strftime(fmt_date[ndays], sizeof fmt_date[ndays], "%a %d %b", &tm); } else snprintf(fmt_date[ndays], sizeof fmt_date[ndays], "%s", date); @@ -76,11 +206,107 @@ char *fetch_forecast(const char *city_url, const char *city_display) { } /* - * cmd_forecast - Handle the "!forecast [city]" command. + * fetch_forecast_full - Fetch a detailed 3-day forecast from wttr.in. + * + * Like fetch_forecast(), but returns per-day detail lines including the + * weather description, sunrise/sunset, and moonrise/moonset times. + * Each day is returned as a separate line in a single string, separated + * by newlines. Caller must free() the result. + * + * Parameters: + * city_url - URL-encoded city name for the API request. + * city_display - Human-readable city name for the response. + * + * Returns: A newly allocated multi-line forecast string, or NULL/error. + */ +char *fetch_forecast_full(const char *city_url, const char *city_display) { + (void)city_display; + char url[512]; + snprintf(url, sizeof url, "https://wttr.in/%s?format=j1", city_url); + char *json = fetch_url(url); + if (!json) return NULL; + char *p = strstr(json, "\"weather\""); + if (!p) { + char *err = NULL; + size_t jlen = strlen(json); + if (jlen > 400) jlen = 400; + asprintf(&err, "Forecast parse error (response: %.*s)", (int)jlen, json); + free(json); + return err; + } + p = strchr(p, '['); + if (!p) { free(json); return NULL; } + p++; + char *out = NULL; + size_t out_cap = 0; + size_t out_len = 0; + for (int i = 0; i < 3; i++) { + const char *obj_end = NULL; + const char *day = find_day_obj(p, 0, &obj_end); + if (!day) break; + char date[16]="", temp[8]="", cond[64]=""; + parse_forecast_day(day, obj_end, date, sizeof date, temp, sizeof temp, cond, sizeof cond); + char desc[128]=""; + char *hb = strstr((char*)day, "\"hourly\""); + if (hb && hb < obj_end) { + hb = strchr(hb, '['); + if (hb && hb < obj_end) { + hb++; + char *db = strstr(hb, "\"weatherDesc\""); + if (db && db < obj_end) { + db = strchr(db, '['); + if (db && db < obj_end) { + db++; + char *vb = strstr(db, "\"value\": \""); + if (vb && vb < obj_end) { vb += 10; char *vq = strchr(vb, '"'); if (vq && vq < obj_end) { size_t l = vq - vb; if (l > sizeof desc - 1) l = sizeof desc - 1; memcpy(desc, vb, l); desc[l] = 0; } } + } + } + } + } + char sunrise[32]="", sunset[32]="", moonrise[32]="", moonset[32]=""; + extract_astronomy(day, obj_end, sunrise, sizeof sunrise, sunset, sizeof sunset, moonrise, sizeof moonrise, moonset, sizeof moonset); + to_24h(sunrise, sizeof sunrise); + to_24h(sunset, sizeof sunset); + to_24h(moonrise, sizeof moonrise); + to_24h(moonset, sizeof moonset); + struct tm tm = {0}; + char fmt_date[32]; + if (strptime(date, "%Y-%m-%d", &tm)) { mktime(&tm); strftime(fmt_date, sizeof fmt_date, "%a %d %b", &tm); } + else snprintf(fmt_date, sizeof fmt_date, "%s", date); + char line[512]; + snprintf(line, sizeof line, "%s: %s\u00b0C %s (%s) | Sun %s - %s | Moon %s - %s", + fmt_date, temp, cond, desc, sunrise, sunset, moonrise, moonset); + collapse_spaces(line); + size_t llen = strlen(line); + size_t need = out_len + llen + 2; + if (need > out_cap) { + size_t new_cap = out_cap ? out_cap * 2 : 2048; + if (new_cap < need) new_cap = need; + char *tmp = realloc(out, new_cap); + if (!tmp) { free(out); free(json); return NULL; } + out = tmp; + out_cap = new_cap; + } + if (out_len > 0) { out[out_len++] = '\n'; } + memcpy(out + out_len, line, llen); + out_len += llen; + out[out_len] = 0; + p = (char*)obj_end; + } + free(json); + if (!out || out_len == 0) return NULL; + return out; +} + +/* + * cmd_forecast - Handle the "!forecast [city] [full]" command. * * Fetches a 3-day weather forecast from wttr.in using the JSON API. - * Delegates to fetch_forecast() for parsing. Defaults to Berlin if - * no city is specified. Rate-limited. + * Delegates to fetch_forecast() for the standard summary, or + * fetch_forecast_full() when "full" is passed as an argument. The full + * mode includes weather description, sunrise/sunset, and moonrise/moonset + * for each day, sent as separate IRC messages. Defaults to Berlin if no + * city is specified. Rate-limited. * * Parameters: * s - The IRC session. @@ -95,19 +321,55 @@ int cmd_forecast(Session *s, const char *msg, const char *reply_target, const ch if (msg[9] != ' ' && msg[9] != '\0') return 0; if (rate_limit_check(src_nick, reply_target) < 0) return 1; char city[256], city_url[256]; + int full = 0; const char *rest = msg[9] == ' ' ? msg + 10 : ""; while (*rest == ' ') rest++; - normalize_city(rest, city, sizeof city, city_url, sizeof city_url); - char *body = fetch_forecast(city_url, city); - char resp[1024]; - if (body) { - snprintf(resp, sizeof resp, "%s", body); - free(body); - } else { - snprintf(resp, sizeof resp, "Forecast: could not fetch data"); + char args[512]; + strncpy(args, rest, sizeof args - 1); + args[sizeof args - 1] = 0; + char *full_ptr = strstr(args, "full"); + if (full_ptr) { + size_t plen = full_ptr - args; + if (plen > 0 && args[plen - 1] == ' ') { + full = 1; + args[plen - 1] = 0; + } else if (plen == 0) { + full = 1; + args[0] = 0; + } } - irc_msg(s, reply_target, resp); - log_stamp(); fprintf(stderr, CLR_MAGENTA "FORECAST %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, resp); + while (*args == ' ') { memmove(args, args + 1, strlen(args)); } + size_t alen = strlen(args); + while (alen > 0 && args[alen - 1] == ' ') { args[--alen] = 0; } + normalize_city(args, city, sizeof city, city_url, sizeof city_url); + if (full) { + char *body = fetch_forecast_full(city_url, city); + if (body) { + char *line = strtok(body, "\n"); + while (line) { + irc_msg(s, reply_target, line); + line = strtok(NULL, "\n"); + if (line) { + struct timespec ts_msg = { .tv_sec = 0, .tv_nsec = 200000000 }; + nanosleep(&ts_msg, NULL); + } + } + free(body); + } else { + irc_msg(s, reply_target, "Forecast: could not fetch data"); + } + } else { + char *body = fetch_forecast(city_url, city); + char resp[1024]; + if (body) { + snprintf(resp, sizeof resp, "%s", body); + free(body); + } else { + snprintf(resp, sizeof resp, "Forecast: could not fetch data"); + } + irc_msg(s, reply_target, resp); + } + log_stamp(); fprintf(stderr, CLR_MAGENTA "FORECAST %s from %s (full=%d):" CLR_RESET " %s\n", reply_target, src_nick, full, city); return 1; } diff --git a/irc.h b/irc.h index baadcec..63d23be 100644 --- a/irc.h +++ b/irc.h @@ -390,7 +390,7 @@ static void handle_privmsg(Session *s, const char *params, const char *src_nick) #ifdef HAS_CMD_GREETING_OR_CHAT cmd_greeting_or_chat(s, msg, reply_target, src_nick); #endif - if (msg[0] == '!') + if (msg[0] == '!' && msg[1] && isalpha((unsigned char)msg[1])) irc_msg(s, reply_target, "Not implemented..."); } diff --git a/zynk.c b/zynk.c index 18e503e..ef1845b 100644 --- a/zynk.c +++ b/zynk.c @@ -766,7 +766,7 @@ int main(int argc, char **argv) { char joinbuf[256] = "#zynk"; g_opencode_bin = OPENCODE_BIN; int pos = 0; - log_stamp(); fprintf(stderr, CLR_CYAN "[zynk] starting up" CLR_RESET "\n"); + log_stamp(); fprintf(stderr, CLR_CYAN "[zynk (" VERSION ")] starting up" CLR_RESET "\n"); for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(argv[0]); return 0;