init
This commit is contained in:
parent
8e86ff746e
commit
ea1dd23c59
5 changed files with 1363 additions and 2 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
calc
|
||||
calc.db
|
||||
*.so
|
||||
*.o
|
||||
|
||||
|
||||
91
AGENTS.md
Normal file
91
AGENTS.md
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
# AGENTS.md — calc
|
||||
|
||||
## 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.
|
||||
|
||||
## Build & Run
|
||||
|
||||
- **Build**: `make` (uses `gcc`, requires `libssl-dev` / `libcrypto` / `libdl`)
|
||||
- **Run**: `./calc -ssl -nick mybot -join "#chan1,#chan2"`
|
||||
- **Clean**: `make clean`
|
||||
- **Install**: `make install`
|
||||
|
||||
## Code Style
|
||||
|
||||
- C99/C11, single file (`calc.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
|
||||
- Dynamic loading of libsqlite3 via `dlopen` + `dlsym`
|
||||
- TLS via OpenSSL (SSL_read/SSL_write, non-blocking)
|
||||
- Prefer `strncpy` + manual NUL, `snprintf`, `asprintf`
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
calc/
|
||||
calc.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
|
||||
|
||||
### SQLite3 Integration (lines 40–228)
|
||||
- Dynamically loads `libsqlite3.so.0` from common paths
|
||||
- Tables: `calc` (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)
|
||||
- `Session` struct holds connection state, buffer, SSL pointer
|
||||
|
||||
### Network I/O (lines 319–379)
|
||||
- `net_connect` — TCP connect with DNS resolution
|
||||
- `net_read` / `net_write` — TLS-aware read/write
|
||||
- `net_send` — formatted send with EINTR/EAGAIN retry
|
||||
|
||||
### AI Query Pipeline (lines 537–612)
|
||||
- Forks a child process per query
|
||||
- Child runs `opencode run --dangerously-skip-permissions` with the question
|
||||
- Output is stripped of formatting and AI disclaimers
|
||||
- Result stored in DB, parent picks it up in the main poll loop
|
||||
|
||||
### IRC Message Handler (lines 667–931)
|
||||
- `irc_handle` parses IRC protocol messages
|
||||
- `irc_feed` buffers line-based input
|
||||
|
||||
### Main Poll Loop (lines 960–1128)
|
||||
- Single `poll()` loop with 100ms timeout
|
||||
- Checks for AI process completion via `waitpid`
|
||||
|
||||
## Important Constants
|
||||
|
||||
- `AI_REPLY_MAX = 400` — max chars per IRC reply from AI
|
||||
- `RATE_LIMIT_WINDOW = 2`, `RATE_LIMIT_MAX = 8` — per-nick rate limiting
|
||||
- `OPENCODE_BIN` — path to opencode binary for AI queries
|
||||
- `DEFAULT_SERVER = "irc.libera.chat"`, `DEF_PORT = 6697`
|
||||
|
||||
## IRC Commands
|
||||
|
||||
| Command | Description |
|
||||
|---|---|
|
||||
| `calc <key>` | Look up a stored value |
|
||||
| `calc <key> = <value>` | Store a value |
|
||||
| `!ping` | Responds with pong |
|
||||
| `!weather [city]` | Current weather (default: Berlin) |
|
||||
| `!forecast [city]` | 3-day forecast |
|
||||
| `!ai <question>` | Ask the AI a question |
|
||||
| `!quit` / `!die` | Shut down the bot |
|
||||
| `<nick>: hi/hello/hey` | Chat greeting |
|
||||
| `<nick>: <question>` | Address the bot by nick to ask AI |
|
||||
|
||||
## Conventions for Contributors
|
||||
|
||||
- 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`
|
||||
- Keep the single-file structure unless there's a strong reason to split
|
||||
24
Makefile
Normal file
24
Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
PREFIX ?= /usr/local
|
||||
BINDIR ?= $(PREFIX)/bin
|
||||
|
||||
CC ?= gcc
|
||||
CFLAGS ?= -Wall -Wextra -pedantic
|
||||
LDLIBS ?= -lssl -lcrypto -ldl
|
||||
|
||||
.PHONY: all clean install uninstall
|
||||
|
||||
all: calc
|
||||
|
||||
calc: calc.c
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
|
||||
|
||||
clean:
|
||||
rm -vf calc
|
||||
|
||||
install: calc
|
||||
mkdir -p $(DESTDIR)$(BINDIR)
|
||||
cp calc $(DESTDIR)$(BINDIR)/calc
|
||||
|
||||
uninstall:
|
||||
rm -vf $(DESTDIR)$(BINDIR)/calc
|
||||
|
||||
103
README.md
103
README.md
|
|
@ -1,3 +1,102 @@
|
|||
# calc
|
||||
# calc — IRC Bot
|
||||
|
||||
an IRC bot
|
||||
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`
|
||||
- **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
|
||||
- **TLS**: Secure connections via OpenSSL
|
||||
- **Rate Limiting**: Per-nick protection against spam
|
||||
- **Single File**: Easy to deploy and modify
|
||||
|
||||
## Requirements
|
||||
|
||||
- `gcc` (or any C99 compiler)
|
||||
- `libssl-dev` (OpenSSL)
|
||||
- `libdl` (dynamic linker)
|
||||
- `libsqlite3` (loaded at runtime via `dlopen`)
|
||||
- `curl` (for weather lookups)
|
||||
- `opencode` (for AI queries — see `calc.c:229` to configure path)
|
||||
|
||||
## Build
|
||||
|
||||
```sh
|
||||
make
|
||||
```
|
||||
|
||||
Set custom compiler or flags:
|
||||
|
||||
```sh
|
||||
make CC=clang CFLAGS="-O2 -Wall -Wextra"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```sh
|
||||
./calc [options]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Description |
|
||||
|---|---|
|
||||
| `-ssl` | Enable TLS (auto-enabled on ports 6697/7000) |
|
||||
| `-noverify` | Skip TLS certificate verification |
|
||||
| `-server <host>` | IRC server (default: `irc.libera.chat`) |
|
||||
| `-port <p>` | Port (default: `6697`) |
|
||||
| `-pass <p>` | Server password |
|
||||
| `-nick <n>` | Nickname (default: `calc`) |
|
||||
| `-join <c>` | Auto-join channels, comma-separated (default: `#calctest`) |
|
||||
| `-h` | Show help |
|
||||
|
||||
### Example
|
||||
|
||||
```sh
|
||||
./calc -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) |
|
||||
| `!ping` | Responds with `pong` |
|
||||
| `!weather [city]` | Current weather (default: Berlin) |
|
||||
| `!forecast [city]` | 3-day forecast (default: Berlin) |
|
||||
| `!ai <question>` | Ask the AI a question |
|
||||
| `!quit` / `!die` | Shut down the bot |
|
||||
| `<nick>: hi` | Greeting response |
|
||||
| `<nick>: <question>` | Ask the bot anything (AI) |
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
make install
|
||||
```
|
||||
|
||||
Installs to `$(PREFIX)/bin` (default: `/usr/local/bin`).
|
||||
|
||||
Override the prefix:
|
||||
|
||||
```sh
|
||||
make install PREFIX=$HOME/.local
|
||||
```
|
||||
|
||||
## Database
|
||||
|
||||
Data is stored in `calc.db` (SQLite3) in the working directory. The bot creates two tables:
|
||||
|
||||
- `calc` — 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.
|
||||
|
||||
## License
|
||||
|
||||
Unlicense — public domain. See `calc.c` for details.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue