2
0
Fork 0
forked from zynk/zynk
zynk/cmd_forecast.h
hanez 739e3622b5 Commit links for !gitlog, full changelog link for !changelog, CMake, multi-file refactor
- !gitlog now shows a link to each commit in the repository, using the
  git remote URL converted from SSH/SCP to HTTPS
- !changelog now shows a 'Full changelog: <url>' link to CHANGELOG.md
  in the repository as the last message after all entries
- Removed 'full' keyword from !gitlog and !changelog (previously showed
  full commit messages and complete changelog entries)
- Switched build system from Make to CMake with feature toggles
  (-DWITH_<FEATURE>=ON/OFF)
- Refactored single-file codebase into dedicated header files: each
  command now lives in its own cmd_*.h file, with separate modules for
  database (db.h), IRC protocol (irc.h), networking (net.h), TLS (tls.h),
  and AI (ai.h)
- Updated README.md to reflect the simplified !gitlog and !changelog
  syntax and the new CMake build instructions

(0.40.0)
2026-07-23 01:46:37 +02:00

114 lines
4.5 KiB
C

#ifndef CMD_FORECAST_H
#define CMD_FORECAST_H
#include "zynk.h"
/*
* fetch_forecast - Fetch and parse a 3-day weather forecast from wttr.in.
*
* Fetches the JSON forecast (format=j1) from wttr.in for the given city,
* then manually parses the "weather" array to extract date, average
* temperature, and weather condition for up to 3 days. Dates are formatted
* as "Mon DD Mon" (e.g. "Mon 19 Jul"). Returns a single-line summary
* string like "Forecast for Berlin: Mon 19 Jul: 22°C Partly cloudy / ...".
*
* Parameters:
* city_url - URL-encoded city name for the API request.
* city_display - Human-readable city name for the response.
*
* Returns: A newly allocated forecast string, or NULL/ error message on
* failure. Caller must free() the result.
*/
char *fetch_forecast(const char *city_url, const char *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 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++; }
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; } }
}
}
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);
snprintf(line[ndays], sizeof line[ndays], "%s: %s\u00b0C %s", fmt_date[ndays], temp, cond);
ndays++;
p = (char*)obj_end;
}
free(json);
if (!ndays) return NULL;
char *out = NULL;
if (ndays == 1) { if (asprintf(&out, "Forecast for %s: %s", city_display, line[0]) < 0) out = NULL; }
else if (ndays == 2) { if (asprintf(&out, "Forecast for %s: %s / %s", city_display, line[0], line[1]) < 0) out = NULL; }
else { if (asprintf(&out, "Forecast for %s: %s / %s / %s", city_display, line[0], line[1], line[2]) < 0) out = NULL; }
if (out) collapse_spaces(out);
return out;
}
/*
* cmd_forecast - Handle the "!forecast [city]" 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.
*
* 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_forecast(Session *s, const char *msg, const char *reply_target, const char *src_nick) {
if (strncmp(msg, "!forecast", 9) != 0) return 0;
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];
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");
}
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);
return 1;
}
#endif