Added !command and fixed the !forecast command.
This commit is contained in:
parent
0b6df45fb8
commit
02d73e0af6
1 changed files with 113 additions and 10 deletions
123
zynk.c
123
zynk.c
|
|
@ -29,7 +29,7 @@ int asprintf(char **, const char *, ...);
|
|||
|
||||
/* ---- constants ---- */
|
||||
|
||||
#define VERSION "0.32.2"
|
||||
#define VERSION "0.33.0"
|
||||
#define DEFAULT_SERVER "irc.libera.chat"
|
||||
#define OPENCODE_BIN "/usr/bin/opencode"
|
||||
#define CLR_RESET "\033[0m"
|
||||
|
|
@ -46,7 +46,7 @@ enum {
|
|||
DEF_PORT = 6697,
|
||||
POLL_MS = 100,
|
||||
AI_REPLY_MAX = 2048,
|
||||
AI_TIMEOUT = 240,
|
||||
AI_TIMEOUT = 600,
|
||||
MAX_CHANS = 32,
|
||||
MAX_CHAN_OPS = 64,
|
||||
RATE_LIMIT_WINDOW = 10,
|
||||
|
|
@ -740,11 +740,11 @@ static char *run_curl(const char *url) {
|
|||
close(fd[1]);
|
||||
int dn = open("/dev/null", O_WRONLY);
|
||||
if (dn >= 0) { dup2(dn, STDERR_FILENO); close(dn); }
|
||||
execlp("curl", "curl", "-s", "--max-time", "10", url, (char *)NULL);
|
||||
execlp("curl", "curl", "-s", "--max-time", "10", "-A", "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0", url, (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
close(fd[1]);
|
||||
char buf[2048], tmp[1024];
|
||||
char buf[32768], tmp[4096];
|
||||
size_t len = 0;
|
||||
ssize_t n;
|
||||
while ((n = read(fd[0], tmp, sizeof tmp)) > 0) {
|
||||
|
|
@ -769,9 +769,18 @@ static char *fetch_forecast(const char *city_url, const char *city_display) {
|
|||
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\":[");
|
||||
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 += 11;
|
||||
p++;
|
||||
char line[3][256], fmt_date[3][32];
|
||||
int ndays = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
|
|
@ -785,10 +794,14 @@ static char *fetch_forecast(const char *city_url, const char *city_display) {
|
|||
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\":[");
|
||||
char *hb = strstr(p, "\"hourly\"");
|
||||
if (hb && hb < obj_end) {
|
||||
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; } }
|
||||
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)) strftime(fmt_date[ndays], sizeof fmt_date[ndays], "%a %d %b", &tm);
|
||||
|
|
@ -1181,7 +1194,7 @@ static int cmd_help(Session *s, const char *msg, const char *reply_target, const
|
|||
if (rate_limit_check(src_nick) < 0) return 1;
|
||||
char buf[512];
|
||||
snprintf(buf, sizeof buf,
|
||||
"Commands: !help, !version, !ping, !weather [city], !forecast [city], !ai <question>, !code <change> (ops), !quit/!die (ops)");
|
||||
"Commands: !help, !version, !ping, !weather [city], !forecast [city], !stock <SYMBOL>[,SYMBOL...], !ai <question>, !code <change> (ops), !quit/!die (ops)");
|
||||
irc_msg(s, reply_target, buf);
|
||||
log_stamp(); fprintf(stderr, CLR_CYAN "HELP from %s in %s" CLR_RESET "\n", src_nick, reply_target);
|
||||
return 1;
|
||||
|
|
@ -1244,6 +1257,95 @@ static int cmd_forecast(Session *s, const char *msg, const char *reply_target, c
|
|||
return 1;
|
||||
}
|
||||
|
||||
static const char *json_find(const char *haystack, const char *key) {
|
||||
const char *p = strstr(haystack, key);
|
||||
if (!p) return NULL;
|
||||
p += strlen(key);
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
if (*p == ':') { p++; while (*p == ' ' || *p == '\t') p++; }
|
||||
while (*p == '[' || *p == ' ') p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
static int cmd_stock(Session *s, const char *msg, const char *reply_target, const char *src_nick) {
|
||||
if (strncmp(msg, "!stock", 6) != 0) return 0;
|
||||
if (msg[6] != ' ' && msg[6] != '\0') return 0;
|
||||
if (rate_limit_check(src_nick) < 0) return 1;
|
||||
const char *rest = msg[6] == ' ' ? msg + 7 : "";
|
||||
while (*rest == ' ') rest++;
|
||||
if (!*rest) {
|
||||
irc_msg(s, reply_target, "Usage: !stock <SYMBOL>[,SYMBOL...] (max 3, e.g. !stock AAPL or !stock AAPL,MSFT,GOOGL)");
|
||||
return 1;
|
||||
}
|
||||
char buf[2048];
|
||||
buf[0] = 0;
|
||||
int count = 0;
|
||||
char symbols[3][16];
|
||||
const char *p = rest;
|
||||
while (*p && count < 3) {
|
||||
while (*p == ' ' || *p == ',') p++;
|
||||
if (!*p) break;
|
||||
int i = 0;
|
||||
while (*p && *p != ' ' && *p != ',' && i < 15) symbols[count][i++] = toupper((unsigned char)*p++);
|
||||
symbols[count][i] = 0;
|
||||
count++;
|
||||
}
|
||||
for (int si = 0; si < count; si++) {
|
||||
char url[512];
|
||||
snprintf(url, sizeof url, "https://query1.finance.yahoo.com/v8/finance/chart/%s?interval=1d&range=1d", symbols[si]);
|
||||
char *json = fetch_url(url);
|
||||
if (!json) {
|
||||
char tmp[128];
|
||||
snprintf(tmp, sizeof tmp, "%s: fetch error", symbols[si]);
|
||||
if (buf[0]) strncat(buf, " | ", sizeof buf - strlen(buf) - 1);
|
||||
strncat(buf, tmp, sizeof buf - strlen(buf) - 1);
|
||||
continue;
|
||||
}
|
||||
const char *rp_s = json_find(json, "\"regularMarketPrice\":");
|
||||
const char *pc_s = json_find(json, "\"chartPreviousClose\":");
|
||||
const char *hi_s = json_find(json, "\"high\":");
|
||||
const char *lo_s = json_find(json, "\"low\":");
|
||||
const char *vo_s = json_find(json, "\"volume\":");
|
||||
const char *nm_s = json_find(json, "\"shortName\":");
|
||||
if (!rp_s || !pc_s || !hi_s || !lo_s || !vo_s) {
|
||||
char tmp[480];
|
||||
size_t jlen = strlen(json);
|
||||
if (jlen > 400) jlen = 400;
|
||||
snprintf(tmp, sizeof tmp, "%s: parse error (response: %.*s)", symbols[si], (int)jlen, json);
|
||||
if (buf[0]) strncat(buf, " | ", sizeof buf - strlen(buf) - 1);
|
||||
strncat(buf, tmp, sizeof buf - strlen(buf) - 1);
|
||||
free(json); continue;
|
||||
}
|
||||
double price = strtod(rp_s, NULL);
|
||||
double prev_close = strtod(pc_s, NULL);
|
||||
double high = strtod(hi_s, NULL);
|
||||
double low = strtod(lo_s, NULL);
|
||||
long volume = strtol(vo_s, NULL, 10);
|
||||
double change = price - prev_close;
|
||||
double pct = (prev_close != 0) ? (change / prev_close) * 100.0 : 0.0;
|
||||
char name[32] = "";
|
||||
if (nm_s && nm_s[0] == '"') {
|
||||
nm_s++;
|
||||
const char *nq = strchr(nm_s, '"');
|
||||
if (nq) { size_t nl = nq - nm_s; if (nl > 31) nl = 31; memcpy(name, nm_s, nl); name[nl] = 0; }
|
||||
}
|
||||
char tmp[384];
|
||||
if (name[0])
|
||||
snprintf(tmp, sizeof tmp, "%s (%s): $%.2f %+.2f (%+.2f%%) H:%.2f L:%.2f Vol:%ldM",
|
||||
symbols[si], name, price, change, pct, high, low, volume / 1000000);
|
||||
else
|
||||
snprintf(tmp, sizeof tmp, "%s: $%.2f %+.2f (%+.2f%%) H:%.2f L:%.2f Vol:%ldM",
|
||||
symbols[si], price, change, pct, high, low, volume / 1000000);
|
||||
if (buf[0]) strncat(buf, " | ", sizeof buf - strlen(buf) - 1);
|
||||
strncat(buf, tmp, sizeof buf - strlen(buf) - 1);
|
||||
free(json);
|
||||
}
|
||||
if (!buf[0]) snprintf(buf, sizeof buf, "Stock: could not fetch data");
|
||||
irc_msg(s, reply_target, buf);
|
||||
log_stamp(); fprintf(stderr, CLR_MAGENTA "STOCK %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cmd_ai(Session *s, const char *msg, const char *reply_target, const char *src_nick) {
|
||||
if (strncmp(msg, "!ai", 3) != 0) return 0;
|
||||
if (msg[3] != ' ' && msg[3] != '\0') return 0;
|
||||
|
|
@ -1348,6 +1450,7 @@ static void handle_privmsg(Session *s, const char *params, const char *src_nick)
|
|||
if (cmd_quit(s, msg, reply_target, src_nick, tgt)) return;
|
||||
if (cmd_weather(s, msg, reply_target, src_nick)) return;
|
||||
if (cmd_forecast(s, msg, reply_target, src_nick)) return;
|
||||
if (cmd_stock(s, msg, reply_target, src_nick)) return;
|
||||
if (cmd_ai(s, msg, reply_target, src_nick)) return;
|
||||
if (cmd_code(s, msg, reply_target, src_nick, tgt)) return;
|
||||
cmd_greeting_or_chat(s, msg, reply_target, src_nick);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue