2
0
Fork 0
forked from zynk/zynk

Renamed calc to zynk and refactored all relevant parts.

This commit is contained in:
Johannes Findeisen 2026-07-14 23:52:56 +02:00
commit 2247b9a480
5 changed files with 63 additions and 63 deletions

6
.gitignore vendored
View file

@ -1,7 +1,7 @@
.idea/
calc
calc.db
zynk
zynk.db
*.so
*.o
*.swp

View file

@ -1,19 +1,19 @@
# AGENTS.md — calc
# AGENTS.md — zynk
## Project Overview
`calc` is a single-file IRC bot written in C. It connects to an IRC server, maintains a key-value store via SQLite3 (loaded dynamically at runtime), and responds to commands in channels and private messages.
`zynk` is a single-file IRC bot written in C. It connects to an IRC server, maintains a key-value store via SQLite3 (loaded dynamically at runtime), and responds to commands in channels and private messages.
## Build & Run
- **Build**: `make` (uses `gcc`, requires `libssl-dev` / `libcrypto` / `libdl`)
- **Run**: `./calc -ssl -nick mybot -join "#chan1,#chan2"`
- **Run**: `./zynk -ssl -nick mybot -join "#chan1,#chan2"`
- **Clean**: `make clean`
- **Install**: `make install`
## Code Style
- C99/C11, single file (`calc.c`)
- C99/C11, single file (`zynk.c`)
- No comments in new code unless clarifying a non-obvious edge case
- Error strings use ANSI color macros (`CLR_RED`, `CLR_GREEN`, etc.)
- 2-space indentation, K&R brace style
@ -24,19 +24,19 @@
## Project Structure
```
calc/
calc.c — single-file IRC bot (1129 lines)
zynk/
zynk.c — single-file IRC bot (1129 lines)
Makefile — build system
.gitignore
AGENTS.md — this file
README.md — user-facing docs
```
## Key Modules in calc.c
## Key Modules in zynk.c
### SQLite3 Integration (lines 40–228)
- Dynamically loads `libsqlite3.so.0` from common paths
- Tables: `calc` (key-value store), `ai_pending` (pending AI queries)
- Tables: `zynk` (key-value store), `ai_pending` (pending AI queries)
- Functions: `db_set`, `db_get`, `db_get_raw`, `db_update_lastq`, `db_update_lasta`
### IRC Session (lines 303–317)
@ -72,8 +72,8 @@ calc/
| Command | Description |
|---|---|
| `calc <key>` | Look up a stored value |
| `calc <key> = <value>` | Store a value |
| `zynk <key>` | Look up a stored value |
| `zynk <key> = <value>` | Store a value |
| `!ping` | Responds with pong |
| `!weather [city]` | Current weather (default: Berlin) |
| `!forecast [city]` | 3-day forecast |
@ -87,5 +87,5 @@ calc/
- Do NOT add `#include` that aren't strictly needed
- Do NOT add comments to obvious code
- Match existing style: 2-space indent, no trailing whitespace
- Test with `make && ./calc -h`
- Test with `make && ./zynk -h`
- Keep the single-file structure unless there's a strong reason to split

View file

@ -14,18 +14,18 @@ endif
.PHONY: all clean install uninstall
all: calc
all: zynk
calc: calc.c
calc: zynk.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
clean:
rm -vf calc
rm -vf zynk
install: calc
mkdir -p $(DESTDIR)$(BINDIR)
cp calc $(DESTDIR)$(BINDIR)/calc
cp calc $(DESTDIR)$(BINDIR)/zynk
uninstall:
rm -vf $(DESTDIR)$(BINDIR)/calc
rm -vf $(DESTDIR)$(BINDIR)/zynk

View file

@ -1,10 +1,10 @@
# calc - Companion AI Logic Chatbot
# zynk - Companion AI Logic Chatbot
A single-file IRC bot in C. Features a key-value store backed by SQLite3, weather lookups via wttr.in, and AI-powered replies.
## Features
- **Key-Value Store**: Persist facts, notes, or any data with `calc key = value`
- **Key-Value Store**: Persist facts, notes, or any data with `zynk key = value`
- **Weather**: Current conditions and 3-day forecasts via `wttr.in`
- **AI Queries**: Ask questions via `!ai` or by addressing the bot; powered by `opencode`
- **Chat Greetings**: Responds to `hi`, `hello`, `hey`, `bye`, and more
@ -19,7 +19,7 @@ A single-file IRC bot in C. Features a key-value store backed by SQLite3, weathe
- `libdl` (dynamic linker)
- `libsqlite3` (loaded at runtime via `dlopen`)
- `curl` (for weather lookups)
- `opencode` (for AI queries - see `calc.c:229` to configure path)
- `opencode` (for AI queries)
## Build
@ -36,7 +36,7 @@ make CC=clang CFLAGS="-O2 -Wall -Wextra"
## Usage
```sh
./calc [options]
./zynk [options]
```
### Options
@ -55,15 +55,15 @@ make CC=clang CFLAGS="-O2 -Wall -Wextra"
### Example
```sh
./calc -ssl -nick mybot -join "#mychan,#myotherchan"
./zynk -ssl -nick mybot -join "#mychan,#myotherchan"
```
## IRC Commands
| Command | Description |
|---|---|
| `calc <key>` | Look up a stored value |
| `calc <key> = <value>` | Store a value (key is lowercased) |
| `zynk <key>` | Look up a stored value |
| `zynk <key> = <value>` | Store a value (key is lowercased) |
| `!ping` | Responds with `pong` |
| `!weather [city]` | Current weather (default: Berlin) |
| `!forecast [city]` | 3-day forecast (default: Berlin) |
@ -88,14 +88,14 @@ make install PREFIX=$HOME/.local
## Database
Data is stored in `calc.db` (SQLite3) in the working directory. The bot creates two tables:
Data is stored in `zynk.db` (SQLite3) in the working directory. The bot creates two tables:
- `calc` - key-value pairs with nick and timestamp metadata
- `zynk` - key-value pairs with nick and timestamp metadata
- `ai_pending` - single-row table for managing in-flight AI queries
## Architecture
`calc` uses a single `poll()` loop with a 100ms timeout. AI queries fork a child process that runs the `opencode` CLI; the parent picks up the result on the next poll iteration. SQLite3 is loaded at runtime via `dlopen` to avoid a compile-time dependency.
`zynk` uses a single `poll()` loop with a 100ms timeout. AI queries fork a child process that runs the `opencode` CLI; the parent picks up the result on the next poll iteration. SQLite3 is loaded at runtime via `dlopen` to avoid a compile-time dependency.
## License

View file

@ -157,14 +157,14 @@ static int sqlite_init_lib(void) {
static int db_init(void) {
if (sqlite_init_lib() < 0) { fprintf(stderr, CLR_RED "Cannot load libsqlite3" CLR_RESET "\n"); return -1; }
if (db_open("calc.db", &db_handle) != SQLITE_OK) {
if (db_open("zynk.db", &db_handle) != SQLITE_OK) {
fprintf(stderr, CLR_RED "DB open failed" CLR_RESET "\n"); return -1;
}
const char *sql_calc = "CREATE TABLE IF NOT EXISTS calc ("
const char *sql_zynk = "CREATE TABLE IF NOT EXISTS zynk ("
"key TEXT PRIMARY KEY, value TEXT NOT NULL, "
"nick TEXT NOT NULL, updated_at TEXT DEFAULT (datetime('now')))";
char *err = NULL;
if (db_exec(DB, sql_calc, NULL, NULL, &err) != SQLITE_OK) {
if (db_exec(DB, sql_zynk, NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, CLR_RED "SQL: %s" CLR_RESET "\n", err); db_free(err); return -1;
}
const char *sql_ai = "CREATE TABLE IF NOT EXISTS ai_pending ("
@ -175,7 +175,7 @@ static int db_init(void) {
}
const char *sql_clear_ai = "DELETE FROM ai_pending";
db_exec(DB, sql_clear_ai, NULL, NULL, NULL);
const char *sql_clear = "DELETE FROM calc WHERE key LIKE 'lastq%' OR key LIKE 'lasta%'";
const char *sql_clear = "DELETE FROM zynk WHERE key LIKE 'lastq%' OR key LIKE 'lasta%'";
db_exec(DB, sql_clear, NULL, NULL, NULL);
return 0;
}
@ -186,7 +186,7 @@ static void db_close_cleanup(void) { if (db_handle) { db_close(DB); db_handle =
static int db_set(const char *key, const char *value, const char *nick) {
void *stmt;
const char *sql = "INSERT INTO calc(key,value,nick,updated_at)"
const char *sql = "INSERT INTO zynk(key,value,nick,updated_at)"
" VALUES(?,?,?,datetime('now'))"
" ON CONFLICT(key) DO UPDATE SET value=excluded.value,"
" nick=excluded.nick, updated_at=datetime('now')";
@ -201,7 +201,7 @@ static int db_set(const char *key, const char *value, const char *nick) {
static char *db_get_raw(const char *key) {
void *stmt;
const char *sql = "SELECT value FROM calc WHERE key=?";
const char *sql = "SELECT value FROM zynk WHERE key=?";
if (db_prepare(DB, sql, -1, &stmt, NULL) != SQLITE_OK) return NULL;
db_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (db_step(stmt) != SQLITE_ROW) { db_finalize(stmt); return NULL; }
@ -213,7 +213,7 @@ static char *db_get_raw(const char *key) {
static char *db_get(const char *key) {
void *stmt;
const char *sql = "SELECT value,nick,updated_at FROM calc WHERE key=?";
const char *sql = "SELECT value,nick,updated_at FROM zynk WHERE key=?";
if (db_prepare(DB, sql, -1, &stmt, NULL) != SQLITE_OK) return NULL;
db_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
if (db_step(stmt) != SQLITE_ROW) { db_finalize(stmt); return NULL; }
@ -228,7 +228,7 @@ static char *db_get(const char *key) {
static char *db_get_random(void) {
void *stmt;
const char *sql = "SELECT key,value,nick,updated_at FROM calc"
const char *sql = "SELECT key,value,nick,updated_at FROM zynk"
" WHERE key NOT LIKE 'lastq%' AND key NOT LIKE 'lasta%'"
" ORDER BY RANDOM() LIMIT 1";
if (db_prepare(DB, sql, -1, &stmt, NULL) != SQLITE_OK) return NULL;
@ -252,7 +252,7 @@ static void db_rotate_history(const char *prefix, const char *value) {
old[i] = db_get_raw(kbuf[i]);
}
void *stmt;
const char *sql = "INSERT INTO calc(key,value,nick,updated_at)"
const char *sql = "INSERT INTO zynk(key,value,nick,updated_at)"
" VALUES(?,?,?,datetime('now'))"
" ON CONFLICT(key) DO UPDATE SET value=excluded.value,"
" nick=excluded.nick, updated_at=datetime('now')";
@ -748,7 +748,7 @@ static void ai_child_task(Session *s, long long id) {
s->ssl = NULL;
db_close(DB);
db_handle = NULL;
if (db_open("calc.db", &db_handle) != SQLITE_OK) exit(1);
if (db_open("zynk.db", &db_handle) != SQLITE_OK) exit(1);
char *q = ai_get_question(id);
if (!q) { db_close(DB); exit(1); }
char *history[MAX_HISTORY];
@ -763,13 +763,13 @@ static void ai_child_task(Session *s, long long id) {
int plen;
if (pending_ai_agent) {
plen = snprintf(prefix, sizeof prefix,
"Your name is calc. You are a coding assistant in a project. Execute code changes as requested. "
"Your name is zynk. You are a coding assistant in a project. Execute code changes as requested. "
"Make real file edits and run commands as needed. Answer in English when asked in English, answer in German when asked in German. "
"Max 2000 chars. No disclaimers or AI-sounding phrases. No markdown/formatting. "
"Just make the changes directly and confirm what you did.");
} else {
plen = snprintf(prefix, sizeof prefix,
"Your name is calc. You are in plan mode - you can only answer questions and discuss code, "
"Your name is zynk. You are in plan mode - you can only answer questions and discuss code, "
"but you CANNOT make any changes to files or execute commands. If someone asks you to change or edit code, "
"tell them you are in plan mode and can only answer questions. Suggest they use !code <instruction> to make actual code changes. "
"Answer briefly. Answer in English when asked in English, answer in German when asked in German. Max 800 chars. "
@ -937,11 +937,11 @@ static void irc_pong(Session *s, const char *r) { if (*r == ':') r++; net_send(s
/* ---- IRC command handlers ---- */
static int cmd_calc(Session *s, const char *msg, const char *reply_target, const char *src_nick, const char *tgt) {
if (strncmp(msg, "calc", 4) != 0) return 0;
static int cmd_zynk(Session *s, const char *msg, const char *reply_target, const char *src_nick, const char *tgt) {
if (strncmp(msg, "zynk", 4) != 0) return 0;
if (msg[4] != '\0' && msg[4] != ' ' && msg[4] != '!') return 0;
if (rate_limit_check(src_nick) < 0) return 1;
/* calc!foo without space after ! falls through to other commands */
/* zynk!foo without space after ! falls through to other commands */
if (msg[4] == '!' && msg[5] != '\0' && msg[5] != ' ') return 0;
int force = 0;
const char *rest;
@ -949,15 +949,15 @@ static int cmd_calc(Session *s, const char *msg, const char *reply_target, const
char *rand = db_get_random();
if (rand) {
irc_msg(s, reply_target, rand);
log_stamp(); fprintf(stderr, CLR_MAGENTA "CALC %s from %s:" CLR_RESET " random %s\n", reply_target, src_nick, rand);
log_stamp(); fprintf(stderr, CLR_MAGENTA "ZYNK %s from %s:" CLR_RESET " random %s\n", reply_target, src_nick, rand);
free(rand);
} else {
irc_msg(s, reply_target, "No calcs saved yet.");
irc_msg(s, reply_target, "No zynkss saved yet.");
}
return 1;
} else if (msg[4] == '!') {
if (msg[5] == '\0') {
irc_msg(s, reply_target, "Usage: calc! <key> = <value>");
irc_msg(s, reply_target, "Usage: zynk! <key> = <value>");
return 1;
}
force = 1;
@ -967,7 +967,7 @@ static int cmd_calc(Session *s, const char *msg, const char *reply_target, const
}
while (*rest == ' ') rest++;
if (!*rest) {
irc_msg(s, reply_target, "Usage: calc <key> [= <value>]");
irc_msg(s, reply_target, "Usage: zynk <key> [= <value>]");
return 1;
}
const char *eq = strstr(rest, " = ");
@ -983,14 +983,14 @@ static int cmd_calc(Session *s, const char *msg, const char *reply_target, const
char *existing = db_get_raw(key);
if (existing) {
free(existing);
snprintf(resp, sizeof resp, "calc already exists (use calc! to overwrite)");
snprintf(resp, sizeof resp, "zynk already exists (use zynk! to overwrite)");
irc_msg(s, reply_target, resp);
log_stamp(); fprintf(stderr, CLR_MAGENTA "CALC %s from %s:" CLR_RESET " blocked re-add of %s\n", reply_target, src_nick, key);
log_stamp(); fprintf(stderr, CLR_MAGENTA "ZYNK %s from %s:" CLR_RESET " blocked re-add of %s\n", reply_target, src_nick, key);
return 1;
}
} else {
if (tgt[0] == '#' && !chan_is_op(tgt, src_nick)) {
irc_msg(s, reply_target, "You need op to overwrite a calc");
irc_msg(s, reply_target, "You need op to overwrite a zynk");
return 1;
}
}
@ -1016,7 +1016,7 @@ static int cmd_calc(Session *s, const char *msg, const char *reply_target, const
}
}
irc_msg(s, reply_target, resp);
log_stamp(); fprintf(stderr, CLR_MAGENTA "CALC %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, resp);
log_stamp(); fprintf(stderr, CLR_MAGENTA "ZYNK %s from %s:" CLR_RESET " %s\n", reply_target, src_nick, resp);
return 1;
}
@ -1201,7 +1201,7 @@ static void handle_privmsg(Session *s, const char *params, const char *src_nick)
const char *msg = p;
if (strcasecmp(src_nick, s->nick) == 0) return;
const char *reply_target = (tgt[0] == '#') ? tgt : src_nick;
if (cmd_calc(s, msg, reply_target, src_nick, tgt)) return;
if (cmd_zynk(s, msg, reply_target, src_nick, tgt)) return;
if (cmd_ping(s, msg, reply_target, src_nick)) return;
if (cmd_quit(s, msg, reply_target, src_nick, tgt)) return;
if (cmd_weather(s, msg, reply_target, src_nick)) return;
@ -1403,12 +1403,12 @@ static void usage(const char *prog) {
" -server host IRC server (default: %s)\n"
" -port p Port (default: %d)\n"
" -pass p Server password\n"
" -nick n Nickname (default: calc<PID>)\n"
" -join c Auto-join channels (comma-sep, default: #calc)\n"
" -nick n Nickname (default: zynk<PID>)\n"
" -join c Auto-join channels (comma-sep, default: #zynk)\n"
"\n"
"Calc commands:\n"
" calc foo = bar Store 'bar' under key 'foo'\n"
" calc foo Retrieve value for 'foo'\n",
"Zynk commands:\n"
" zynk foo = bar Store 'bar' under key 'foo'\n"
" zynk foo Retrieve value for 'foo'\n",
prog, DEFAULT_SERVER, DEF_PORT);
}
@ -1457,9 +1457,9 @@ int main(int argc, char **argv) {
char nickbuf[32];
const char *nick = NULL;
char passbuf[256] = "";
char joinbuf[256] = "#calc";
char joinbuf[256] = "#zynk";
int pos = 0;
log_stamp(); fprintf(stderr, CLR_CYAN "[calc] starting up" CLR_RESET "\n");
log_stamp(); fprintf(stderr, CLR_CYAN "[zynk] 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;
@ -1503,7 +1503,7 @@ int main(int argc, char **argv) {
}
if (port <= 0 || port > 65535) port = DEF_PORT;
if ((port == 6697 || port == 7000) && !use_tls) use_tls = 1;
if (!nick) { snprintf(nickbuf, sizeof nickbuf, "calc"); nick = nickbuf; }
if (!nick) { snprintf(nickbuf, sizeof nickbuf, "zynk"); nick = nickbuf; }
if (db_init() < 0) { fprintf(stderr, CLR_RED "DB init failed" CLR_RESET "\n"); return 1; }
Session s;
memset(&s, 0, sizeof s);
@ -1511,7 +1511,7 @@ int main(int argc, char **argv) {
s.port = port;
snprintf(s.nick, sizeof s.nick, "%s", nick);
snprintf(s.user, sizeof s.user, "%s", nick);
snprintf(s.real, sizeof s.real, "calc");
snprintf(s.real, sizeof s.real, "zynk");
snprintf(s.auto_join, sizeof s.auto_join, "%s", joinbuf);
s.fd = -1;
s.use_tls = use_tls;
@ -1553,7 +1553,7 @@ int main(int argc, char **argv) {
if (s.connected && s.fd >= 0) {
int old_fl = fcntl(s.fd, F_GETFL, 0);
fcntl(s.fd, F_SETFL, old_fl & ~O_NONBLOCK);
net_send(&s, "QUIT :calc\r\n");
net_send(&s, "QUIT :zynk\r\n");
struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000000 };
nanosleep(&ts, NULL);
}