From 511f999dfab4fce8d31af9c388d616ce45a88398 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 18 Jun 2022 10:03:57 -0400 Subject: Flatten extras to top-level directory --- .gitignore | 3 + Makefile | 19 +- configure | 6 + edit.c | 566 ++++++++++++++++++++++++++++ extra/edit/.gitignore | 3 - extra/edit/Makefile | 28 -- extra/edit/configure | 44 --- extra/edit/edit.c | 566 ---------------------------- extra/edit/pounce-edit.1 | 133 ------- extra/edit/xdg.c | 129 ------- extra/notify/.gitignore | 3 - extra/notify/Makefile | 28 -- extra/notify/configure | 44 --- extra/notify/notify.c | 353 ------------------ extra/notify/pounce-notify.1 | 115 ------ extra/palaver/.gitignore | 3 - extra/palaver/Makefile | 30 -- extra/palaver/configure | 45 --- extra/palaver/notify.c | 817 ----------------------------------------- extra/palaver/pounce-palaver.1 | 112 ------ notify.c | 353 ++++++++++++++++++ palaver.c | 817 +++++++++++++++++++++++++++++++++++++++++ pounce-edit.1 | 133 +++++++ pounce-notify.1 | 115 ++++++ pounce-palaver.1 | 112 ++++++ 25 files changed, 2122 insertions(+), 2455 deletions(-) create mode 100644 edit.c delete mode 100644 extra/edit/.gitignore delete mode 100644 extra/edit/Makefile delete mode 100755 extra/edit/configure delete mode 100644 extra/edit/edit.c delete mode 100644 extra/edit/pounce-edit.1 delete mode 100644 extra/edit/xdg.c delete mode 100644 extra/notify/.gitignore delete mode 100644 extra/notify/Makefile delete mode 100755 extra/notify/configure delete mode 100644 extra/notify/notify.c delete mode 100644 extra/notify/pounce-notify.1 delete mode 100644 extra/palaver/.gitignore delete mode 100644 extra/palaver/Makefile delete mode 100755 extra/palaver/configure delete mode 100644 extra/palaver/notify.c delete mode 100644 extra/palaver/pounce-palaver.1 create mode 100644 notify.c create mode 100644 palaver.c create mode 100644 pounce-edit.1 create mode 100644 pounce-notify.1 create mode 100644 pounce-palaver.1 diff --git a/.gitignore b/.gitignore index 882cf40..5249f84 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ calico config.mk localhost.pem pounce +pounce-edit +pounce-notify +pounce-palaver tags diff --git a/Makefile b/Makefile index ac9c045..029d184 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,9 @@ MANDIR ?= ${PREFIX}/man CFLAGS += -std=c11 -Wall -Wextra -Wpedantic LDADD.crypt = -lcrypt +LDADD.libcurl = -lcurl LDADD.libtls = -ltls +LDADD.sqlite3 = -lsqlite3 BINS = calico pounce MANS = ${BINS:=.1} @@ -13,6 +15,9 @@ MANS = ${BINS:=.1} LDLIBS.calico = LDLIBS.pounce = ${LDADD.crypt} ${LDADD.libtls} +LDLIBS.pounce-edit = ${LDADD.libtls} +LDLIBS.pounce-notify = ${LDADD.libtls} +LDLIBS.pounce-palaver = ${LDADD.libcurl} ${LDADD.libtls} ${LDADD.sqlite3} OBJS.calico += dispatch.o @@ -25,15 +30,25 @@ OBJS.pounce += server.o OBJS.pounce += state.o OBJS.pounce += xdg.o -OBJS = ${OBJS.calico} ${OBJS.pounce} +OBJS.pounce-edit = edit.o xdg.o +OBJS.pounce-notify = notify.o +OBJS.pounce-palaver = palaver.o + +OBJS += ${OBJS.calico} +OBJS += ${OBJS.pounce} +OBJS += ${OBJS.pounce-edit} +OBJS += ${OBJS.pounce-notify} +OBJS += ${OBJS.pounce-palaver} dev: tags all all: ${BINS} calico: ${OBJS.calico} - pounce: ${OBJS.pounce} +pounce-edit: ${OBJS.pounce-edit} +pounce-notify: ${OBJS.pounce-notify} +pounce-palaver: ${OBJS.pounce-palaver} ${BINS}: ${CC} ${LDFLAGS} ${OBJS.$@} ${LDLIBS.$@} -o $@ diff --git a/configure b/configure index 1d94084..62a3c1a 100755 --- a/configure +++ b/configure @@ -27,6 +27,12 @@ for opt; do (--prefix=*) echo "PREFIX = ${opt#*=}" ;; (--bindir=*) echo "BINDIR = ${opt#*=}" ;; (--mandir=*) echo "MANDIR = ${opt#*=}" ;; + (--enable-edit) echo 'BINS += pounce-edit' ;; + (--enable-notify) echo 'BINS += pounce-notify' ;; + (--enable-palaver) + echo 'BINS += pounce-palaver' + config libcurl sqlite3 + ;; (*) echo "warning: unsupported option ${opt}" >&2 ;; esac done diff --git a/edit.c b/edit.c new file mode 100644 index 0000000..793e413 --- /dev/null +++ b/edit.c @@ -0,0 +1,566 @@ +/* Copyright (C) 2022 June McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7: + * + * If you modify this Program, or any covered work, by linking or + * combining it with OpenSSL (or a modified version of that library), + * containing parts covered by the terms of the OpenSSL License and the + * original SSLeay license, the licensors of this Program grant you + * additional permission to convey the resulting work. Corresponding + * Source for a non-source form of such a combination shall include the + * source code for the parts of OpenSSL used as well as that of the + * covered work. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *configPath(char *buf, size_t cap, const char *path, int i); +FILE *configOpen(const char *path, const char *mode); + +#define WS "\t " + +#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) + +struct Option { + bool set; + char *name; + char *value; +}; + +struct Config { + size_t cap, len; + struct Option *opts; +}; + +static struct Option configGet(const struct Config *config, const char *name) { + for (size_t i = 0; i < config->len; ++i) { + if (!strcmp(config->opts[i].name, name)) return config->opts[i]; + } + return (struct Option) { .set = false }; +} + +static void +configSet(struct Config *config, const char *name, const char *value) { + for (size_t i = 0; i < config->len; ++i) { + struct Option *opt = &config->opts[i]; + if (strcmp(opt->name, name)) continue; + + opt->set = true; + free(opt->value); + opt->value = NULL; + if (value) { + opt->value = strdup(value); + if (!opt->value) err(EX_OSERR, "strdup"); + } + return; + } + + if (config->len == config->cap) { + config->cap = (config->cap ? config->cap * 2 : 32); + config->opts = realloc( + config->opts, config->cap * sizeof(*config->opts) + ); + if (!config->opts) err(EX_OSERR, "realloc"); + } + + struct Option *opt = &config->opts[config->len++]; + opt->set = true; + opt->name = strdup(name); + if (!opt->name) err(EX_OSERR, "strdup"); + + opt->value = NULL; + if (value) { + opt->value = strdup(value); + if (!opt->value) err(EX_OSERR, "strdup"); + } +} + +static void configUnset(struct Config *config, const char *name) { + for (size_t i = 0; i < config->len; ++i) { + if (strcmp(config->opts[i].name, name)) continue; + config->opts[i].set = false; + break; + } +} + +static void configWrite(const struct Config *config, FILE *file) { + int error = ftruncate(fileno(file), 0); + if (error) err(EX_IOERR, "ftruncate"); + + rewind(file); + fprintf(file, "# written by pounce-edit\n"); + for (size_t i = 0; i < config->len; ++i) { + if (!config->opts[i].set) continue; + fprintf(file, "%s", config->opts[i].name); + if (config->opts[i].value) { + fprintf(file, " = %s", config->opts[i].value); + } + fprintf(file, "\n"); + if (ferror(file)) err(EX_IOERR, "writing configuration"); + } + + error = fflush(file); + if (error) err(EX_IOERR, "writing configuration"); +} + +static void configParse(struct Config *config, const char *path) { + FILE *file = configOpen(path, "r"); + if (!file) exit(EX_NOINPUT); + + ssize_t llen; + size_t cap = 0; + char *buf = NULL; + for (size_t line = 1; 0 < (llen = getline(&buf, &cap, file)); ++line) { + if (buf[llen-1] == '\n') buf[--llen] = '\0'; + + char *name = buf + strspn(buf, WS); + size_t len = strcspn(name, WS "="); + if (!name[0] || name[0] == '#') continue; + + char *equal = &name[len] + strspn(&name[len], WS); + if (*equal && *equal != '=') { + name[len] = '\0'; + errx( + EX_USAGE, "%s:%zu: option `%s' missing equals sign", + path, line, name + ); + } + + char *value = NULL; + if (*equal) { + value = &equal[1] + strspn(&equal[1], WS); + } + + name[len] = '\0'; + configSet(config, name, value); + } + fclose(file); +} + +static bool verbose; +static struct tls *client; + +static void clientWrite(const char *ptr, size_t len) { + while (len) { + ssize_t ret = tls_write(client, ptr, len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); + ptr += ret; + len -= ret; + } +} + +static void format(const char *format, ...) +__attribute__((format(printf, 1, 2))); +static void format(const char *format, ...) { + char buf[1024]; + va_list ap; + va_start(ap, format); + int len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + assert((size_t)len < sizeof(buf)); + if (verbose) fprintf(stderr, "%s", buf); + clientWrite(buf, len); +} + +enum { ParamCap = 2 }; +struct Message { + char *nick; + char *cmd; + char *params[ParamCap]; +}; + +static struct Message parse(char *line) { + if (verbose) fprintf(stderr, "%s\n", line); + struct Message msg = {0}; + if (line[0] == ':') { + char *origin = 1 + strsep(&line, " "); + msg.nick = strsep(&origin, "!"); + } + msg.cmd = strsep(&line, " "); + for (size_t i = 0; line && i < ParamCap; ++i) { + if (line[0] == ':') { + msg.params[i] = &line[1]; + break; + } + msg.params[i] = strsep(&line, " "); + } + return msg; +} + +static void require(const struct Message *msg, bool nick, size_t len) { + if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); + for (size_t i = 0; i < len; ++i) { + if (msg->params[i]) continue; + errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); + } +} + +typedef void Handler(struct Message *msg); + +static void handlePing(struct Message *msg) { + require(msg, false, 1); + format("PONG :%s\r\n", msg->params[0]); +} + +static void handleError(struct Message *msg) { + require(msg, false, 1); + errx(EX_UNAVAILABLE, "%s", msg->params[0]); +} + +static const char *Boolean[] = { + "no-names", "no-sts", "palaver", "sasl-external", "verbose", +}; + +static const char *Integer[] = { + "local-port", "port", "queue-interval", "size", +}; + +// FIXME: local-pass needs to be validated for hash +// FIXME: sasl-plain needs to be validated for colon +static const char *String[] = { + "away", "bind", "blind-req", "client-cert", "client-priv", "host", "join", + "local-ca", "local-cert", "local-host", "local-pass", "local-path", + "local-priv", "mode", "nick", "pass", "quit", "real", "sasl-plain", "save", + "trust", "user", +}; + +// TODO: nick, user aren't safe until pounce can fall back in case +// they're invalid +static const char *Safe[] = { + "away", "join", "local-pass", "mode", "nick", "no-names", "no-sts", + "palaver", "quit", "real", "user", +}; + +static bool allowUnsafe; +static bool safe(const char *name) { + if (allowUnsafe) return true; + for (size_t i = 0; i < ARRAY_LEN(Safe); ++i) { + if (!strcmp(Safe[i], name)) return true; + } + return false; +} + +static bool exists(const char *name) { + for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { + if (!strcmp(Boolean[i], name)) return true; + } + for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { + if (!strcmp(Integer[i], name)) return true; + } + for (size_t i = 0; i < ARRAY_LEN(String); ++i) { + if (!strcmp(String[i], name)) return true; + } + return false; +} + +static const char *validate(const char *name, const char *value) { + for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { + if (strcmp(Boolean[i], name)) continue; + if (!safe(name)) return "cannot be set"; + return (value ? "does not take a value" : NULL); + } + for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { + if (strcmp(Integer[i], name)) continue; + if (!safe(name)) return "cannot be set"; + if (!value) return "requires a value"; + char *end; + size_t n = strtoull(value, &end, 10); + if (!*value || *end) return "must be an integer"; + if (!strcmp(name, "size") && (!n || n & (n-1))) { + return "must be a power of two"; + } + return NULL; + } + for (size_t i = 0; i < ARRAY_LEN(String); ++i) { + if (strcmp(String[i], name)) continue; + if (!safe(name)) return "cannot be set"; + return (value ? NULL : "requires a value"); + } + return "is not an option"; +} + +static FILE *config; +static struct Config over; +static struct Config under; + +static void handlePrivmsg(struct Message *msg) { + require(msg, true, 2); + if (strcmp(msg->nick, msg->params[0])) return; + + char *cmd = strsep(&msg->params[1], " "); + char *name = strsep(&msg->params[1], " "); + char *value = msg->params[1]; + + if (!strcmp(cmd, "get")) { + if (!name) { + format("NOTICE %s :set: ", msg->nick); + for (size_t i = 0; i < over.len; ++i) { + if (!over.opts[i].set) continue; + format("%s\2%s\2", (i ? ", " : ""), over.opts[i].name); + } + format("\r\nNOTICE %s :inherited: ", msg->nick); + for (size_t i = 0; i < under.len; ++i) { + format("%s\2%s\2", (i ? ", " : ""), under.opts[i].name); + } + format("\r\n"); + return; + } + if (!exists(name)) { + format("NOTICE %s :\2%s\2 is not an option\r\n", msg->nick, name); + return; + } + + struct Option opt = configGet(&over, name); + if (!opt.set) opt = configGet(&under, name); + if (opt.set && opt.value) { + format("NOTICE %s :\2%s\2 = %s\r\n", msg->nick, name, opt.value); + } else if (opt.set) { + format("NOTICE %s :\2%s\2 is set\r\n", msg->nick, name); + } else { + format("NOTICE %s :\2%s\2 is unset\r\n", msg->nick, name); + } + + } else if (!strcmp(cmd, "set")) { + if (!name) { + format("NOTICE %s :options: ", msg->nick); + if (allowUnsafe) { + for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { + format("%s\2%s\2", (i ? ", " : ""), Boolean[i]); + } + for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { + format(", \2%s\2", Integer[i]); + } + for (size_t i = 0; i < ARRAY_LEN(String); ++i) { + format(", \2%s\2", String[i]); + } + } else { + for (size_t i = 0; i < ARRAY_LEN(Safe); ++i) { + format("%s\2%s\2", (i ? ", " : ""), Safe[i]); + } + } + format("\r\n"); + return; + } + + const char *error = validate(name, value); + if (error) { + format("NOTICE %s :\2%s\2 %s\r\n", msg->nick, name, error); + return; + } + configSet(&over, name, value); + configWrite(&over, config); + format("NOTICE %s :\2%s\2 set\r\n", msg->nick, name); + + } else if (!strcmp(cmd, "unset")) { + if (!name) { + format("NOTICE %s :set: ", msg->nick); + for (size_t i = 0; i < over.len; ++i) { + if (!over.opts[i].set) continue; + format("%s\2%s\2", (i ? ", " : ""), over.opts[i].name); + } + format("\r\n"); + return; + } + if (!exists(name)) { + format("NOTICE %s :\2%s\2 is not an option\r\n", msg->nick, name); + return; + } + if (!safe(name)) { + format("NOTICE %s :\2%s\2 cannot be unset\r\n", msg->nick, name); + return; + } + + configUnset(&over, name); + configWrite(&over, config); + struct Option opt = configGet(&under, name); + format( + "NOTICE %s :\2%s\2 %s\r\n", + msg->nick, name, (opt.set ? "inherited" : "unset") + ); + + } else if (!strcmp(cmd, "restart")) { + format("QUIT :pounce reloading configuration\r\n"); + } +} + +static const struct { + const char *cmd; + Handler *fn; +} Handlers[] = { + { "ERROR", handleError }, + { "PING", handlePing }, + { "PRIVMSG", handlePrivmsg }, +}; + +static void handle(struct Message *msg) { + if (!msg->cmd) return; + for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { + if (strcmp(msg->cmd, Handlers[i].cmd)) continue; + Handlers[i].fn(msg); + break; + } +} + +static void quit(int sig) { + (void)sig; + format("QUIT\r\n"); + tls_close(client); + _exit(EX_OK); +} + +int main(int argc, char *argv[]) { + bool insecure = false; + const char *cert = NULL; + const char *priv = NULL; + const char *host = NULL; + const char *port = NULL; + const char *pass = NULL; + const char *trust = NULL; + const char *user = "pounce-edit"; + + for (int opt; 0 < (opt = getopt(argc, argv, "!ac:h:k:p:t:u:vw:"));) { + switch (opt) { + break; case '!': insecure = true; + break; case 'a': allowUnsafe = true; + break; case 'c': cert = optarg; + break; case 'h': host = optarg; + break; case 'k': priv = optarg; + break; case 'p': port = optarg; + break; case 't': trust = optarg; + break; case 'u': user = optarg; + break; case 'v': verbose = true; + break; case 'w': pass = optarg; + break; default: return EX_USAGE; + } + } + if (optind == argc) errx(EX_USAGE, "config required"); + + for (int i = optind; i < argc-1; ++i) { + configParse(&under, argv[i]); + } + configParse(&over, argv[argc-1]); + config = configOpen(argv[argc-1], "r+"); + if (!config) exit(EX_NOINPUT); + + if (!host) { + struct Option opt = configGet(&over, "local-host"); + if (!opt.set) opt = configGet(&under, "local-host"); + if (!opt.set || !opt.value) errx(EX_USAGE, "host required"); + host = opt.value; + } + if (!port) { + struct Option opt = configGet(&over, "local-port"); + if (!opt.set) opt = configGet(&under, "local-port"); + if (opt.set && opt.value) { + port = opt.value; + } else { + port = "6697"; + } + } + + client = tls_client(); + if (!client) errx(EX_SOFTWARE, "tls_client"); + + struct tls_config *config = tls_config_new(); + if (!config) errx(EX_SOFTWARE, "tls_config_new"); + + if (insecure) { + tls_config_insecure_noverifycert(config); + tls_config_insecure_noverifyname(config); + } + + int error; + char path[PATH_MAX]; + if (trust) { + tls_config_insecure_noverifyname(config); + for (int i = 0; configPath(path, sizeof(path), trust, i); ++i) { + error = tls_config_set_ca_file(config, path); + if (!error) break; + } + if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); + } + if (cert) { + for (int i = 0; configPath(path, sizeof(path), cert, i); ++i) { + if (priv) { + error = tls_config_set_cert_file(config, path); + } else { + error = tls_config_set_keypair_file(config, path, path); + } + if (!error) break; + } + if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config)); + } + if (priv) { + for (int i = 0; configPath(path, sizeof(path), priv, i); ++i) { + error = tls_config_set_key_file(config, path); + if (!error) break; + } + if (error) errx(EX_NOINPUT, "%s: %s", priv, tls_config_error(config)); + } + + error = tls_configure(client, config); + if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); + tls_config_free(config); + + error = tls_connect(client, host, port); + if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); + + if (pass) format("PASS :%s\r\n", pass); + format( + "CAP REQ :causal.agency/passive\r\n" + "CAP END\r\n" + "NICK *\r\n" + "USER %s 0 * :pounce-edit\r\n", + user + ); + + signal(SIGINT, quit); + signal(SIGTERM, quit); + + size_t len = 0; + char buf[8191 + 512]; + for (;;) { + ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); + if (!ret) errx(EX_PROTOCOL, "server closed connection"); + len += ret; + + char *line = buf; + for (;;) { + char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); + if (!crlf) break; + *crlf = '\0'; + struct Message msg = parse(line); + handle(&msg); + line = crlf + 2; + } + len -= line - buf; + memmove(buf, line, len); + } +} diff --git a/extra/edit/.gitignore b/extra/edit/.gitignore deleted file mode 100644 index 626888d..0000000 --- a/extra/edit/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.o -config.mk -pounce-edit diff --git a/extra/edit/Makefile b/extra/edit/Makefile deleted file mode 100644 index 8bac3a6..0000000 --- a/extra/edit/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -PREFIX ?= /usr/local -BINDIR ?= ${PREFIX}/bin -MANDIR ?= ${PREFIX}/man - -CFLAGS += -std=c11 -Wall -Wextra -Wpedantic -LDADD.libtls = -ltls - --include config.mk - -LDLIBS = ${LDADD.libtls} -OBJS = edit.o xdg.o - -all: pounce-edit - -pounce-edit: ${OBJS} - ${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@ - -clean: - rm -f ${OBJS} pounce-edit - -install: pounce-edit pounce-edit.1 - install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1 - install pounce-edit ${DESTDIR}${BINDIR} - install -m 644 pounce-edit.1 ${DESTDIR}${MANDIR}/man1 - -uninstall: - rm -f ${DESTDIR}${BINDIR}/pounce-edit - rm -f ${DESTDIR}${MANDIR}/man1/pounce-edit.1 diff --git a/extra/edit/configure b/extra/edit/configure deleted file mode 100755 index e180aa5..0000000 --- a/extra/edit/configure +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/sh -set -eu - -cflags() { - echo "CFLAGS += $*" -} -defstr() { - cflags "-D'$1=\"$2\"'" -} -defvar() { - defstr "$1" "$(pkg-config --variable=$3 $2)${4:-}" -} -ldadd() { - lib=$1; shift - echo "LDADD.${lib} = $*" -} -config() { - pkg-config --print-errors "$@" - cflags $(pkg-config --cflags "$@") - for lib; do ldadd $lib $(pkg-config --libs $lib); done -} - -exec >config.mk - -for opt; do - case "${opt}" in - (--prefix=*) echo "PREFIX = ${opt#*=}" ;; - (--bindir=*) echo "BINDIR = ${opt#*=}" ;; - (--mandir=*) echo "MANDIR = ${opt#*=}" ;; - (*) echo "warning: unsupported option ${opt}" >&2 ;; - esac -done - -case "$(uname)" in - (OpenBSD) - ;; - (Linux) - cflags -D_GNU_SOURCE - config libtls - ;; - (*) - config libtls - ;; -esac diff --git a/extra/edit/edit.c b/extra/edit/edit.c deleted file mode 100644 index 793e413..0000000 --- a/extra/edit/edit.c +++ /dev/null @@ -1,566 +0,0 @@ -/* Copyright (C) 2022 June McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * If you modify this Program, or any covered work, by linking or - * combining it with OpenSSL (or a modified version of that library), - * containing parts covered by the terms of the OpenSSL License and the - * original SSLeay license, the licensors of this Program grant you - * additional permission to convey the resulting work. Corresponding - * Source for a non-source form of such a combination shall include the - * source code for the parts of OpenSSL used as well as that of the - * covered work. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -char *configPath(char *buf, size_t cap, const char *path, int i); -FILE *configOpen(const char *path, const char *mode); - -#define WS "\t " - -#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) - -struct Option { - bool set; - char *name; - char *value; -}; - -struct Config { - size_t cap, len; - struct Option *opts; -}; - -static struct Option configGet(const struct Config *config, const char *name) { - for (size_t i = 0; i < config->len; ++i) { - if (!strcmp(config->opts[i].name, name)) return config->opts[i]; - } - return (struct Option) { .set = false }; -} - -static void -configSet(struct Config *config, const char *name, const char *value) { - for (size_t i = 0; i < config->len; ++i) { - struct Option *opt = &config->opts[i]; - if (strcmp(opt->name, name)) continue; - - opt->set = true; - free(opt->value); - opt->value = NULL; - if (value) { - opt->value = strdup(value); - if (!opt->value) err(EX_OSERR, "strdup"); - } - return; - } - - if (config->len == config->cap) { - config->cap = (config->cap ? config->cap * 2 : 32); - config->opts = realloc( - config->opts, config->cap * sizeof(*config->opts) - ); - if (!config->opts) err(EX_OSERR, "realloc"); - } - - struct Option *opt = &config->opts[config->len++]; - opt->set = true; - opt->name = strdup(name); - if (!opt->name) err(EX_OSERR, "strdup"); - - opt->value = NULL; - if (value) { - opt->value = strdup(value); - if (!opt->value) err(EX_OSERR, "strdup"); - } -} - -static void configUnset(struct Config *config, const char *name) { - for (size_t i = 0; i < config->len; ++i) { - if (strcmp(config->opts[i].name, name)) continue; - config->opts[i].set = false; - break; - } -} - -static void configWrite(const struct Config *config, FILE *file) { - int error = ftruncate(fileno(file), 0); - if (error) err(EX_IOERR, "ftruncate"); - - rewind(file); - fprintf(file, "# written by pounce-edit\n"); - for (size_t i = 0; i < config->len; ++i) { - if (!config->opts[i].set) continue; - fprintf(file, "%s", config->opts[i].name); - if (config->opts[i].value) { - fprintf(file, " = %s", config->opts[i].value); - } - fprintf(file, "\n"); - if (ferror(file)) err(EX_IOERR, "writing configuration"); - } - - error = fflush(file); - if (error) err(EX_IOERR, "writing configuration"); -} - -static void configParse(struct Config *config, const char *path) { - FILE *file = configOpen(path, "r"); - if (!file) exit(EX_NOINPUT); - - ssize_t llen; - size_t cap = 0; - char *buf = NULL; - for (size_t line = 1; 0 < (llen = getline(&buf, &cap, file)); ++line) { - if (buf[llen-1] == '\n') buf[--llen] = '\0'; - - char *name = buf + strspn(buf, WS); - size_t len = strcspn(name, WS "="); - if (!name[0] || name[0] == '#') continue; - - char *equal = &name[len] + strspn(&name[len], WS); - if (*equal && *equal != '=') { - name[len] = '\0'; - errx( - EX_USAGE, "%s:%zu: option `%s' missing equals sign", - path, line, name - ); - } - - char *value = NULL; - if (*equal) { - value = &equal[1] + strspn(&equal[1], WS); - } - - name[len] = '\0'; - configSet(config, name, value); - } - fclose(file); -} - -static bool verbose; -static struct tls *client; - -static void clientWrite(const char *ptr, size_t len) { - while (len) { - ssize_t ret = tls_write(client, ptr, len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); - ptr += ret; - len -= ret; - } -} - -static void format(const char *format, ...) -__attribute__((format(printf, 1, 2))); -static void format(const char *format, ...) { - char buf[1024]; - va_list ap; - va_start(ap, format); - int len = vsnprintf(buf, sizeof(buf), format, ap); - va_end(ap); - assert((size_t)len < sizeof(buf)); - if (verbose) fprintf(stderr, "%s", buf); - clientWrite(buf, len); -} - -enum { ParamCap = 2 }; -struct Message { - char *nick; - char *cmd; - char *params[ParamCap]; -}; - -static struct Message parse(char *line) { - if (verbose) fprintf(stderr, "%s\n", line); - struct Message msg = {0}; - if (line[0] == ':') { - char *origin = 1 + strsep(&line, " "); - msg.nick = strsep(&origin, "!"); - } - msg.cmd = strsep(&line, " "); - for (size_t i = 0; line && i < ParamCap; ++i) { - if (line[0] == ':') { - msg.params[i] = &line[1]; - break; - } - msg.params[i] = strsep(&line, " "); - } - return msg; -} - -static void require(const struct Message *msg, bool nick, size_t len) { - if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); - for (size_t i = 0; i < len; ++i) { - if (msg->params[i]) continue; - errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); - } -} - -typedef void Handler(struct Message *msg); - -static void handlePing(struct Message *msg) { - require(msg, false, 1); - format("PONG :%s\r\n", msg->params[0]); -} - -static void handleError(struct Message *msg) { - require(msg, false, 1); - errx(EX_UNAVAILABLE, "%s", msg->params[0]); -} - -static const char *Boolean[] = { - "no-names", "no-sts", "palaver", "sasl-external", "verbose", -}; - -static const char *Integer[] = { - "local-port", "port", "queue-interval", "size", -}; - -// FIXME: local-pass needs to be validated for hash -// FIXME: sasl-plain needs to be validated for colon -static const char *String[] = { - "away", "bind", "blind-req", "client-cert", "client-priv", "host", "join", - "local-ca", "local-cert", "local-host", "local-pass", "local-path", - "local-priv", "mode", "nick", "pass", "quit", "real", "sasl-plain", "save", - "trust", "user", -}; - -// TODO: nick, user aren't safe until pounce can fall back in case -// they're invalid -static const char *Safe[] = { - "away", "join", "local-pass", "mode", "nick", "no-names", "no-sts", - "palaver", "quit", "real", "user", -}; - -static bool allowUnsafe; -static bool safe(const char *name) { - if (allowUnsafe) return true; - for (size_t i = 0; i < ARRAY_LEN(Safe); ++i) { - if (!strcmp(Safe[i], name)) return true; - } - return false; -} - -static bool exists(const char *name) { - for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { - if (!strcmp(Boolean[i], name)) return true; - } - for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { - if (!strcmp(Integer[i], name)) return true; - } - for (size_t i = 0; i < ARRAY_LEN(String); ++i) { - if (!strcmp(String[i], name)) return true; - } - return false; -} - -static const char *validate(const char *name, const char *value) { - for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { - if (strcmp(Boolean[i], name)) continue; - if (!safe(name)) return "cannot be set"; - return (value ? "does not take a value" : NULL); - } - for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { - if (strcmp(Integer[i], name)) continue; - if (!safe(name)) return "cannot be set"; - if (!value) return "requires a value"; - char *end; - size_t n = strtoull(value, &end, 10); - if (!*value || *end) return "must be an integer"; - if (!strcmp(name, "size") && (!n || n & (n-1))) { - return "must be a power of two"; - } - return NULL; - } - for (size_t i = 0; i < ARRAY_LEN(String); ++i) { - if (strcmp(String[i], name)) continue; - if (!safe(name)) return "cannot be set"; - return (value ? NULL : "requires a value"); - } - return "is not an option"; -} - -static FILE *config; -static struct Config over; -static struct Config under; - -static void handlePrivmsg(struct Message *msg) { - require(msg, true, 2); - if (strcmp(msg->nick, msg->params[0])) return; - - char *cmd = strsep(&msg->params[1], " "); - char *name = strsep(&msg->params[1], " "); - char *value = msg->params[1]; - - if (!strcmp(cmd, "get")) { - if (!name) { - format("NOTICE %s :set: ", msg->nick); - for (size_t i = 0; i < over.len; ++i) { - if (!over.opts[i].set) continue; - format("%s\2%s\2", (i ? ", " : ""), over.opts[i].name); - } - format("\r\nNOTICE %s :inherited: ", msg->nick); - for (size_t i = 0; i < under.len; ++i) { - format("%s\2%s\2", (i ? ", " : ""), under.opts[i].name); - } - format("\r\n"); - return; - } - if (!exists(name)) { - format("NOTICE %s :\2%s\2 is not an option\r\n", msg->nick, name); - return; - } - - struct Option opt = configGet(&over, name); - if (!opt.set) opt = configGet(&under, name); - if (opt.set && opt.value) { - format("NOTICE %s :\2%s\2 = %s\r\n", msg->nick, name, opt.value); - } else if (opt.set) { - format("NOTICE %s :\2%s\2 is set\r\n", msg->nick, name); - } else { - format("NOTICE %s :\2%s\2 is unset\r\n", msg->nick, name); - } - - } else if (!strcmp(cmd, "set")) { - if (!name) { - format("NOTICE %s :options: ", msg->nick); - if (allowUnsafe) { - for (size_t i = 0; i < ARRAY_LEN(Boolean); ++i) { - format("%s\2%s\2", (i ? ", " : ""), Boolean[i]); - } - for (size_t i = 0; i < ARRAY_LEN(Integer); ++i) { - format(", \2%s\2", Integer[i]); - } - for (size_t i = 0; i < ARRAY_LEN(String); ++i) { - format(", \2%s\2", String[i]); - } - } else { - for (size_t i = 0; i < ARRAY_LEN(Safe); ++i) { - format("%s\2%s\2", (i ? ", " : ""), Safe[i]); - } - } - format("\r\n"); - return; - } - - const char *error = validate(name, value); - if (error) { - format("NOTICE %s :\2%s\2 %s\r\n", msg->nick, name, error); - return; - } - configSet(&over, name, value); - configWrite(&over, config); - format("NOTICE %s :\2%s\2 set\r\n", msg->nick, name); - - } else if (!strcmp(cmd, "unset")) { - if (!name) { - format("NOTICE %s :set: ", msg->nick); - for (size_t i = 0; i < over.len; ++i) { - if (!over.opts[i].set) continue; - format("%s\2%s\2", (i ? ", " : ""), over.opts[i].name); - } - format("\r\n"); - return; - } - if (!exists(name)) { - format("NOTICE %s :\2%s\2 is not an option\r\n", msg->nick, name); - return; - } - if (!safe(name)) { - format("NOTICE %s :\2%s\2 cannot be unset\r\n", msg->nick, name); - return; - } - - configUnset(&over, name); - configWrite(&over, config); - struct Option opt = configGet(&under, name); - format( - "NOTICE %s :\2%s\2 %s\r\n", - msg->nick, name, (opt.set ? "inherited" : "unset") - ); - - } else if (!strcmp(cmd, "restart")) { - format("QUIT :pounce reloading configuration\r\n"); - } -} - -static const struct { - const char *cmd; - Handler *fn; -} Handlers[] = { - { "ERROR", handleError }, - { "PING", handlePing }, - { "PRIVMSG", handlePrivmsg }, -}; - -static void handle(struct Message *msg) { - if (!msg->cmd) return; - for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { - if (strcmp(msg->cmd, Handlers[i].cmd)) continue; - Handlers[i].fn(msg); - break; - } -} - -static void quit(int sig) { - (void)sig; - format("QUIT\r\n"); - tls_close(client); - _exit(EX_OK); -} - -int main(int argc, char *argv[]) { - bool insecure = false; - const char *cert = NULL; - const char *priv = NULL; - const char *host = NULL; - const char *port = NULL; - const char *pass = NULL; - const char *trust = NULL; - const char *user = "pounce-edit"; - - for (int opt; 0 < (opt = getopt(argc, argv, "!ac:h:k:p:t:u:vw:"));) { - switch (opt) { - break; case '!': insecure = true; - break; case 'a': allowUnsafe = true; - break; case 'c': cert = optarg; - break; case 'h': host = optarg; - break; case 'k': priv = optarg; - break; case 'p': port = optarg; - break; case 't': trust = optarg; - break; case 'u': user = optarg; - break; case 'v': verbose = true; - break; case 'w': pass = optarg; - break; default: return EX_USAGE; - } - } - if (optind == argc) errx(EX_USAGE, "config required"); - - for (int i = optind; i < argc-1; ++i) { - configParse(&under, argv[i]); - } - configParse(&over, argv[argc-1]); - config = configOpen(argv[argc-1], "r+"); - if (!config) exit(EX_NOINPUT); - - if (!host) { - struct Option opt = configGet(&over, "local-host"); - if (!opt.set) opt = configGet(&under, "local-host"); - if (!opt.set || !opt.value) errx(EX_USAGE, "host required"); - host = opt.value; - } - if (!port) { - struct Option opt = configGet(&over, "local-port"); - if (!opt.set) opt = configGet(&under, "local-port"); - if (opt.set && opt.value) { - port = opt.value; - } else { - port = "6697"; - } - } - - client = tls_client(); - if (!client) errx(EX_SOFTWARE, "tls_client"); - - struct tls_config *config = tls_config_new(); - if (!config) errx(EX_SOFTWARE, "tls_config_new"); - - if (insecure) { - tls_config_insecure_noverifycert(config); - tls_config_insecure_noverifyname(config); - } - - int error; - char path[PATH_MAX]; - if (trust) { - tls_config_insecure_noverifyname(config); - for (int i = 0; configPath(path, sizeof(path), trust, i); ++i) { - error = tls_config_set_ca_file(config, path); - if (!error) break; - } - if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); - } - if (cert) { - for (int i = 0; configPath(path, sizeof(path), cert, i); ++i) { - if (priv) { - error = tls_config_set_cert_file(config, path); - } else { - error = tls_config_set_keypair_file(config, path, path); - } - if (!error) break; - } - if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config)); - } - if (priv) { - for (int i = 0; configPath(path, sizeof(path), priv, i); ++i) { - error = tls_config_set_key_file(config, path); - if (!error) break; - } - if (error) errx(EX_NOINPUT, "%s: %s", priv, tls_config_error(config)); - } - - error = tls_configure(client, config); - if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); - tls_config_free(config); - - error = tls_connect(client, host, port); - if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); - - if (pass) format("PASS :%s\r\n", pass); - format( - "CAP REQ :causal.agency/passive\r\n" - "CAP END\r\n" - "NICK *\r\n" - "USER %s 0 * :pounce-edit\r\n", - user - ); - - signal(SIGINT, quit); - signal(SIGTERM, quit); - - size_t len = 0; - char buf[8191 + 512]; - for (;;) { - ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); - if (!ret) errx(EX_PROTOCOL, "server closed connection"); - len += ret; - - char *line = buf; - for (;;) { - char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); - if (!crlf) break; - *crlf = '\0'; - struct Message msg = parse(line); - handle(&msg); - line = crlf + 2; - } - len -= line - buf; - memmove(buf, line, len); - } -} diff --git a/extra/edit/pounce-edit.1 b/extra/edit/pounce-edit.1 deleted file mode 100644 index 36c59aa..0000000 --- a/extra/edit/pounce-edit.1 +++ /dev/null @@ -1,133 +0,0 @@ -.Dd April 3, 2022 -.Dt POUNCE-EDIT 7 -.Os -. -.Sh NAME -.Nm pounce-edit -.Nd pounce configuration over IRC -. -.Sh SYNOPSIS -.Nm -.Op Fl av -.Op Fl c Ar cert -.Op Fl h Ar host -.Op Fl k Ar priv -.Op Fl p Ar port -.Op Fl t Ar trust -.Op Fl u Ar user -.Op Fl w Ar pass -.Ar config ... -. -.Sh DESCRIPTION -The -.Nm -client provides -.Xr pounce 1 -configuration editing -over IRC. -It accepts commands -sent to the user's own nick. -It writes its changes -to the last configuration file -listed on the command line. -Changes do not take effect until -.Xr pounce 1 -is restarted. -Configuration files -are searched for -in the same manner as -.Xr pounce 1 . -. -.Pp -The options are as follows: -.Bl -tag -width Ds -.It Fl a -Allow setting all options, -including those that -may render -.Xr pounce 1 -inoperable. -. -.It Fl c Ar cert -Load the TLS client certificate from -.Ar cert . -If the private key -is in a separate file, -it is loaded with -.Fl k . -. -.It Fl h Ar host -Connect to -.Ar host . -The default is the value of -.Cm local-host -from the configuration files. -. -.It Fl p Ar port -Connect to -.Ar port . -The default is the value of -.Cm local-port -from the configuration files, -or 6697. -. -.It Fl t Ar trust -Trust the self-signed certificate loaded from -.Ar path -and disable server name verification. -. -.It Fl u Ar user -Set the username to -.Ar user . -The default username is -.Dq pounce-edit . -. -.It Fl v -Log IRC protocol to standard error. -. -.It Fl w Ar pass -Log in with the server password -.Ar pass . -.El -. -.Sh COMMANDS -Commands are sent as private messages -to the user's own nick. -Replies are sent as notices. -.Bl -tag -width Ds -.It Ic get Op Ar option -Show the value of an option, -or list all set options. -.It Ic restart -Restart -.Xr pounce 1 . -.It Ic set Ar option Op Ar value -Set an option. -.It Ic unset Ar option -Unset an option. -.El -. -.Sh FILES -See -.Xr pounce 1 . -. -.Sh EXAMPLES -Inherit options from -.Pa tilde.conf , -write changes to -.Pa tilde.edit : -.Bd -literal -offset indent -$ pounce tilde.conf tilde.edit -$ pounce-edit -c local.pem tilde.conf tilde.edit -.Ed -. -.Sh AUTHORS -.An June McEnroe Aq Mt june@causal.agency -. -.Sh BUGS -Send mail to -.Aq Mt list+pounce@causal.agency -or join -.Li #ascii.town -on -.Li irc.tilde.chat . diff --git a/extra/edit/xdg.c b/extra/edit/xdg.c deleted file mode 100644 index b9015b2..0000000 --- a/extra/edit/xdg.c +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright (C) 2019, 2020 June McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * If you modify this Program, or any covered work, by linking or - * combining it with OpenSSL (or a modified version of that library), - * containing parts covered by the terms of the OpenSSL License and the - * original SSLeay license, the licensors of this Program grant you - * additional permission to convey the resulting work. Corresponding - * Source for a non-source form of such a combination shall include the - * source code for the parts of OpenSSL used as well as that of the - * covered work. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define SUBDIR "pounce" - -struct Base { - const char *envHome; - const char *envDirs; - const char *defHome; - const char *defDirs; -}; - -static const struct Base Config = { - .envHome = "XDG_CONFIG_HOME", - .envDirs = "XDG_CONFIG_DIRS", - .defHome = ".config", - .defDirs = "/etc/xdg", -}; - -static const struct Base Data = { - .envHome = "XDG_DATA_HOME", - .envDirs = "XDG_DATA_DIRS", - .defHome = ".local/share", - .defDirs = "/usr/local/share:/usr/share", -}; - -static char *basePath( - struct Base base, char *buf, size_t cap, const char *path, int i -) { - if (path[strspn(path, ".")] == '/') { - if (i > 0) return NULL; - snprintf(buf, cap, "%s", path); - return buf; - } - - if (i > 0) { - const char *dirs = getenv(base.envDirs); - if (!dirs) dirs = base.defDirs; - for (; i > 1; --i) { - dirs += strcspn(dirs, ":"); - dirs += (*dirs == ':'); - } - if (!*dirs) return NULL; - snprintf( - buf, cap, "%.*s/" SUBDIR "/%s", - (int)strcspn(dirs, ":"), dirs, path - ); - return buf; - } - - const char *home = getenv("HOME"); - const char *baseHome = getenv(base.envHome); - if (baseHome) { - snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path); - } else if (home) { - snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path); - } else { - errx(EX_USAGE, "HOME unset"); - } - return buf; -} - -char *configPath(char *buf, size_t cap, const char *path, int i) { - return basePath(Config, buf, cap, path, i); -} - -char *dataPath(char *buf, size_t cap, const char *path, int i) { - return basePath(Data, buf, cap, path, i); -} - -FILE *configOpen(const char *path, const char *mode) { - char buf[PATH_MAX]; - for (int i = 0; configPath(buf, sizeof(buf), path, i); ++i) { - FILE *file = fopen(buf, mode); - if (file) return file; - if (errno != ENOENT) warn("%s", buf); - } - warn("%s", configPath(buf, sizeof(buf), path, 0)); - return NULL; -} - -FILE *dataOpen(const char *path, const char *mode) { - char buf[PATH_MAX]; - for (int i = 0; dataPath(buf, sizeof(buf), path, i); ++i) { - FILE *file = fopen(buf, mode); - if (file) return file; - if (errno != ENOENT) warn("%s", buf); - } - if (mode[0] != 'r') { - int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU); - if (error && errno != EEXIST) warn("%s", buf); - } - FILE *file = fopen(dataPath(buf, sizeof(buf), path, 0), mode); - if (!file) warn("%s", buf); - return file; -} diff --git a/extra/notify/.gitignore b/extra/notify/.gitignore deleted file mode 100644 index acd29ee..0000000 --- a/extra/notify/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.o -config.mk -pounce-notify diff --git a/extra/notify/Makefile b/extra/notify/Makefile deleted file mode 100644 index 5e8ed93..0000000 --- a/extra/notify/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -PREFIX ?= /usr/local -BINDIR ?= ${PREFIX}/bin -MANDIR ?= ${PREFIX}/man - -CFLAGS += -std=c11 -Wall -Wextra -Wpedantic -LDADD.libtls = -ltls - --include config.mk - -LDLIBS = ${LDADD.libtls} -OBJS = notify.o - -all: pounce-notify - -pounce-notify: ${OBJS} - ${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@ - -clean: - rm -f ${OBJS} pounce-notify - -install: pounce-notify pounce-notify.1 - install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1 - install pounce-notify ${DESTDIR}${BINDIR} - install -m 644 pounce-notify.1 ${DESTDIR}${MANDIR}/man1 - -uninstall: - rm -f ${DESTDIR}${BINDIR}/pounce-notify - rm -f ${DESTDIR}${MANDIR}/man1/pounce-notify.1 diff --git a/extra/notify/configure b/extra/notify/configure deleted file mode 100755 index e180aa5..0000000 --- a/extra/notify/configure +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/sh -set -eu - -cflags() { - echo "CFLAGS += $*" -} -defstr() { - cflags "-D'$1=\"$2\"'" -} -defvar() { - defstr "$1" "$(pkg-config --variable=$3 $2)${4:-}" -} -ldadd() { - lib=$1; shift - echo "LDADD.${lib} = $*" -} -config() { - pkg-config --print-errors "$@" - cflags $(pkg-config --cflags "$@") - for lib; do ldadd $lib $(pkg-config --libs $lib); done -} - -exec >config.mk - -for opt; do - case "${opt}" in - (--prefix=*) echo "PREFIX = ${opt#*=}" ;; - (--bindir=*) echo "BINDIR = ${opt#*=}" ;; - (--mandir=*) echo "MANDIR = ${opt#*=}" ;; - (*) echo "warning: unsupported option ${opt}" >&2 ;; - esac -done - -case "$(uname)" in - (OpenBSD) - ;; - (Linux) - cflags -D_GNU_SOURCE - config libtls - ;; - (*) - config libtls - ;; -esac diff --git a/extra/notify/notify.c b/extra/notify/notify.c deleted file mode 100644 index 935ba26..0000000 --- a/extra/notify/notify.c +++ /dev/null @@ -1,353 +0,0 @@ -/* Copyright (C) 2021 C. McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * If you modify this Program, or any covered work, by linking or - * combining it with OpenSSL (or a modified version of that library), - * containing parts covered by the terms of the OpenSSL License and the - * original SSLeay license, the licensors of this Program grant you - * additional permission to convey the resulting work. Corresponding - * Source for a non-source form of such a combination shall include the - * source code for the parts of OpenSSL used as well as that of the - * covered work. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) - -static bool verbose; -static struct tls *client; - -static void clientWrite(const char *ptr, size_t len) { - while (len) { - ssize_t ret = tls_write(client, ptr, len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); - ptr += ret; - len -= ret; - } -} - -static void format(const char *format, ...) { - char buf[1024]; - va_list ap; - va_start(ap, format); - int len = vsnprintf(buf, sizeof(buf), format, ap); - va_end(ap); - assert((size_t)len < sizeof(buf)); - if (verbose) fprintf(stderr, "%s", buf); - clientWrite(buf, len); -} - -enum { ParamCap = 2 }; -struct Message { - char *time; - char *nick; - char *user; - char *host; - char *cmd; - char *params[ParamCap]; -}; - -static struct Message parse(char *line) { - if (verbose) fprintf(stderr, "%s\n", line); - struct Message msg = {0}; - if (line[0] == '@') { - char *tags = 1 + strsep(&line, " "); - while (tags) { - char *tag = strsep(&tags, ";"); - char *key = strsep(&tag, "="); - if (!strcmp(key, "time")) msg.time = tag; - } - } - if (line[0] == ':') { - char *origin = 1 + strsep(&line, " "); - msg.nick = strsep(&origin, "!"); - msg.user = strsep(&origin, "@"); - msg.host = origin; - } - msg.cmd = strsep(&line, " "); - for (size_t i = 0; line && i < ParamCap; ++i) { - if (line[0] == ':') { - msg.params[i] = &line[1]; - break; - } - msg.params[i] = strsep(&line, " "); - } - return msg; -} - -static void require(const struct Message *msg, bool nick, size_t len) { - if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); - for (size_t i = 0; i < len; ++i) { - if (msg->params[i]) continue; - errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); - } -} - -typedef void Handler(struct Message *msg); - -static void handlePing(struct Message *msg) { - require(msg, false, 1); - format("PONG :%s\r\n", msg->params[0]); -} - -static void handleError(struct Message *msg) { - require(msg, false, 1); - errx(EX_UNAVAILABLE, "%s", msg->params[0]); -} - -static char *nick; -static bool away; - -static void handleReplyWelcome(struct Message *msg) { - require(msg, false, 1); - free(nick); - nick = strdup(msg->params[0]); - if (!nick) err(EX_OSERR, "strdup"); - format("USERHOST %s\r\n", nick); -} - -static void handleNick(struct Message *msg) { - require(msg, true, 1); - if (nick && !strcmp(msg->nick, nick)) { - free(nick); - nick = strdup(msg->params[0]); - if (!nick) err(EX_OSERR, "strdup"); - } -} - -static void handleReplyUserHost(struct Message *msg) { - require(msg, false, 2); - while (msg->params[1]) { - char *reply = strsep(&msg->params[1], " "); - char *replyNick = strsep(&reply, "*="); - if (strcmp(replyNick, nick)) continue; - if (reply && !reply[0]) strsep(&msg->params[1], "="); - if (!reply) errx(EX_PROTOCOL, "invalid USERHOST reply"); - away = (reply[0] == '-'); - break; - } -} - -static void handleReplyNowAway(struct Message *msg) { - (void)msg; - away = true; -} - -static void handleReplyUnaway(struct Message *msg) { - (void)msg; - away = false; -} - -static const char *command; - -static void handlePrivmsg(struct Message *msg) { - require(msg, true, 2); - if (!nick || !away) return; - - if (!msg->time) return; - struct tm tm = {0}; - strptime(msg->time, "%FT%T", &tm); - time_t then = timegm(&tm); - if (time(NULL) - then > 60) return; - - bool query = (msg->params[0][0] != '#'); - bool mention = false; - size_t len = strlen(nick); - for ( - char *match = msg->params[1]; - NULL != (match = strstr(match, nick)); - match = &match[len] - ) { - char a = (match > msg->params[1] ? match[-1] : ' '); - char b = (match[len] ? match[len] : ' '); - if (b == '\1') b = ' '; - if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) { - mention = true; - break; - } - match = &match[len]; - } - if (!query && !mention) return; - - pid_t pid = fork(); - if (pid < 0) err(EX_OSERR, "fork"); - if (pid) return; - - setenv("NOTIFY_TIME", msg->time, 1); - setenv("NOTIFY_NICK", msg->nick, 1); - if (msg->user) setenv("NOTIFY_USER", msg->user, 1); - if (msg->host) setenv("NOTIFY_HOST", msg->host, 1); - if (!query) setenv("NOTIFY_CHANNEL", msg->params[0], 1); - setenv("NOTIFY_MESSAGE", msg->params[1], 1); - - const char *shell = getenv("SHELL"); - if (!shell) shell = "/bin/sh"; - execl(shell, "sh", "-c", command, NULL); - err(EX_OSERR, "%s", shell); -} - -static const struct { - const char *cmd; - Handler *fn; -} Handlers[] = { - { "001", handleReplyWelcome }, - { "302", handleReplyUserHost }, - { "305", handleReplyUnaway }, - { "306", handleReplyNowAway }, - { "ERROR", handleError }, - { "NICK", handleNick }, - { "PING", handlePing }, - { "PRIVMSG", handlePrivmsg }, -}; - -static void handle(struct Message *msg) { - if (!msg->cmd) return; - for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { - if (strcmp(msg->cmd, Handlers[i].cmd)) continue; - Handlers[i].fn(msg); - break; - } -} - -static void reap(int sig) { - (void)sig; - int status; - wait(&status); -} - -static void quit(int sig) { - (void)sig; - format("QUIT\r\n"); - tls_close(client); - _exit(EX_OK); -} - -int main(int argc, char *argv[]) { - bool insecure = false; - const char *cert = NULL; - const char *priv = NULL; - const char *host = NULL; - const char *port = "6697"; - const char *pass = NULL; - const char *trust = NULL; - const char *user = "pounce-notify"; - - for (int opt; 0 < (opt = getopt(argc, argv, "!c:k:p:t:u:vw:"));) { - switch (opt) { - break; case '!': insecure = true; - break; case 'c': cert = optarg; - break; case 'k': priv = optarg; - break; case 'p': port = optarg; - break; case 't': trust = optarg; - break; case 'u': user = optarg; - break; case 'v': verbose = true; - break; case 'w': pass = optarg; - break; default: return EX_USAGE; - } - } - if (argc - optind < 1) errx(EX_USAGE, "host required"); - if (argc - optind < 2) errx(EX_USAGE, "command required"); - host = argv[optind++]; - command = argv[optind]; - - setenv("POUNCE_HOST", host, 1); - setenv("POUNCE_PORT", port, 1); - - client = tls_client(); - if (!client) errx(EX_SOFTWARE, "tls_client"); - - struct tls_config *config = tls_config_new(); - if (!config) errx(EX_SOFTWARE, "tls_config_new"); - - if (insecure) { - tls_config_insecure_noverifycert(config); - tls_config_insecure_noverifyname(config); - } - - int error; - if (trust) { - tls_config_insecure_noverifyname(config); - error = tls_config_set_ca_file(config, trust); - if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); - } - if (cert) { - error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert)); - if (error) { - errx( - EX_NOINPUT, "tls_config_set_keypair_file: %s", - tls_config_error(config) - ); - } - } - - error = tls_configure(client, config); - if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); - tls_config_free(config); - - error = tls_connect(client, host, port); - if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); - - if (pass) format("PASS :%s\r\n", pass); - format( - "CAP REQ :causal.agency/passive server-time\r\n" - "CAP END\r\n" - "NICK *\r\n" - "USER %s 0 * :pounce-notify\r\n", - user - ); - - signal(SIGINT, quit); - signal(SIGTERM, quit); - signal(SIGCHLD, reap); - - size_t len = 0; - char buf[8191 + 512]; - for (;;) { - ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); - if (!ret) errx(EX_PROTOCOL, "server closed connection"); - len += ret; - - char *line = buf; - for (;;) { - char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); - if (!crlf) break; - *crlf = '\0'; - struct Message msg = parse(line); - handle(&msg); - line = crlf + 2; - } - len -= line - buf; - memmove(buf, line, len); - } -} diff --git a/extra/notify/pounce-notify.1 b/extra/notify/pounce-notify.1 deleted file mode 100644 index 628bc25..0000000 --- a/extra/notify/pounce-notify.1 +++ /dev/null @@ -1,115 +0,0 @@ -.Dd November 28, 2021 -.Dt POUNCE-NOTIFY 1 -.Os -. -.Sh NAME -.Nm pounce-notify -.Nd notifications for pounce -. -.Sh SYNOPSIS -.Nm -.Op Fl v -.Op Fl c Ar cert -.Op Fl k Ar priv -.Op Fl p Ar port -.Op Fl t Ar trust -.Op Fl u Ar user -.Op Fl w Ar pass -.Ar host -.Ar command -. -.Sh DESCRIPTION -The -.Nm -program connects to an instance of -.Xr pounce 1 -at -.Ar host -and runs -.Ar command -for each private message -or message containing the user's nickname -while the user is marked as away. -The nickname is matched case-sensitively -and must be surrounded by whitespace or punctuation. -Information about the message -is passed to the command -through the environment. -. -.Pp -The arguments are as follows: -.Bl -tag -width Ds -.It Fl c Ar cert -Load the TLS client certificate from -.Ar cert . -If the private key is in a separate file, -it is loaded with -.Fl k . -.It Fl k Ar priv -Load the TLS client private key from -.Ar priv . -.It Fl p Ar port -Connect to -.Ar port . -The default port is 6697. -.It Fl t Ar path -Trust the self-signed certificate loaded -from -.Ar path -and disable server name verification. -.It Fl u Ar user -Set the username to -.Ar user . -The default username is -.Dq pounce-notify . -.It Fl w Ar pass -Log in with the server password -.Ar pass . -.El -. -.Sh ENVIRONMENT -The following variables -are set in the environment of -.Ar command : -.Pp -.Bl -tag -width "NOTIFY_MESSAGE" -compact -.It Ev NOTIFY_CHANNEL -The name of the channel -in which the message was sent. -Unset if the message was private. -.It Ev NOTIFY_HOST -The hostname of the user who sent the message. -.It Ev NOTIFY_MESSAGE -The content of the message. -.It Ev NOTIFY_NICK -The nickname of the user who sent the message. -.It Ev NOTIFY_TIME -The time the message was sent, -in -.Sy server-time -format. -.It Ev NOTIFY_USER -The username of the user who sent the message. -.It Ev POUNCE_HOST -The host -.Nm -is connected to. -.It Ev POUNCE_PORT -The port -.Nm -is connected to. -.El -. -.Sh SEE ALSO -.Xr pounce 1 -. -.Sh AUTHORS -.An June Bug Aq Mt june@causal.agency -. -.Sh BUGS -Send mail to -.Aq Mt list+pounce@causal.agency -or join -.Li #ascii.town -on -.Li irc.tilde.chat . diff --git a/extra/palaver/.gitignore b/extra/palaver/.gitignore deleted file mode 100644 index d92ce2e..0000000 --- a/extra/palaver/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.o -config.mk -pounce-palaver diff --git a/extra/palaver/Makefile b/extra/palaver/Makefile deleted file mode 100644 index fbf10df..0000000 --- a/extra/palaver/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -PREFIX ?= /usr/local -BINDIR ?= ${PREFIX}/bin -MANDIR ?= ${PREFIX}/man - -CFLAGS += -std=c11 -Wall -Wextra -Wpedantic -LDADD.libcurl = -lcurl -LDADD.libtls = -ltls -LDADD.sqlite3 = -lsqlite3 - --include config.mk - -LDLIBS = ${LDADD.libcurl} ${LDADD.libtls} ${LDADD.sqlite3} -OBJS = notify.o - -all: pounce-palaver - -pounce-palaver: ${OBJS} - ${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@ - -clean: - rm -f ${OBJS} pounce-palaver - -install: pounce-palaver pounce-palaver.1 - install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1 - install pounce-palaver ${DESTDIR}${BINDIR} - install -m 644 pounce-palaver.1 ${DESTDIR}${MANDIR}/man1 - -uninstall: - rm -f ${DESTDIR}${BINDIR}/pounce-palaver - rm -f ${DESTDIR}${MANDIR}/man1/pounce-palaver.1 diff --git a/extra/palaver/configure b/extra/palaver/configure deleted file mode 100755 index bce92be..0000000 --- a/extra/palaver/configure +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/sh -set -eu - -cflags() { - echo "CFLAGS += $*" -} -defstr() { - cflags "-D'$1=\"$2\"'" -} -defvar() { - defstr "$1" "$(pkg-config --variable=$3 $2)${4:-}" -} -ldadd() { - lib=$1; shift - echo "LDADD.${lib} = $*" -} -config() { - pkg-config --print-errors "$@" - cflags $(pkg-config --cflags "$@") - for lib; do ldadd $lib $(pkg-config --libs $lib); done -} - -exec >config.mk - -for opt; do - case "${opt}" in - (--prefix=*) echo "PREFIX = ${opt#*=}" ;; - (--bindir=*) echo "BINDIR = ${opt#*=}" ;; - (--mandir=*) echo "MANDIR = ${opt#*=}" ;; - (*) echo "warning: unsupported option ${opt}" >&2 ;; - esac -done - -case "$(uname)" in - (OpenBSD) - config libcurl sqlite3 - ;; - (Linux) - cflags -D_GNU_SOURCE - config libcurl libtls sqlite3 - ;; - (*) - config libcurl libtls sqlite3 - ;; -esac diff --git a/extra/palaver/notify.c b/extra/palaver/notify.c deleted file mode 100644 index 36e4ae1..0000000 --- a/extra/palaver/notify.c +++ /dev/null @@ -1,817 +0,0 @@ -/* Copyright (C) 2019 C. McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Additional permission under GNU GPL version 3 section 7: - * - * If you modify this Program, or any covered work, by linking or - * combining it with OpenSSL (or a modified version of that library), - * containing parts covered by the terms of the OpenSSL License and the - * original SSLeay license, the licensors of this Program grant you - * additional permission to convey the resulting work. Corresponding - * Source for a non-source form of such a combination shall include the - * source code for the parts of OpenSSL used as well as that of the - * covered work. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Why must it return (const unsigned char *)? -#define sqlite3_column_text(...) (const char *)sqlite3_column_text(__VA_ARGS__) - -#define DATABASE_PATH "pounce/palaver.sqlite" - -#define SQL(...) #__VA_ARGS__ -#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) - -static bool verbose; -static char curlError[CURL_ERROR_SIZE]; - -static CURL *curl; -static sqlite3 *db; -static struct tls *client; - -static void dbOpen(const char *path, int flags) { - int error = sqlite3_open_v2(path, &db, flags, NULL); - if (error == SQLITE_CANTOPEN) { - sqlite3_close(db); - db = NULL; - return; - } - if (error) errx(EX_NOINPUT, "%s: %s", path, sqlite3_errmsg(db)); - - sqlite3_busy_timeout(db, 10000); -} - -static void dbFind(char *path) { - if (path) { - dbOpen(path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); - if (db) return; - errx(EX_NOINPUT, "%s: database not found", path); - } - - const char *home = getenv("HOME"); - const char *dataHome = getenv("XDG_DATA_HOME"); - const char *dataDirs = getenv("XDG_DATA_DIRS"); - if (!dataDirs) dataDirs = "/usr/local/share:/usr/share"; - - char buf[PATH_MAX]; - if (dataHome) { - snprintf(buf, sizeof(buf), "%s/" DATABASE_PATH, dataHome); - } else { - if (!home) errx(EX_CONFIG, "HOME unset"); - snprintf(buf, sizeof(buf), "%s/.local/share/" DATABASE_PATH, home); - } - dbOpen(buf, SQLITE_OPEN_READWRITE); - if (db) return; - - char create[PATH_MAX]; - snprintf(create, sizeof(create), "%s", buf); - - while (*dataDirs) { - size_t len = strcspn(dataDirs, ":"); - snprintf(buf, sizeof(buf), "%.*s/" DATABASE_PATH, (int)len, dataDirs); - dbOpen(buf, SQLITE_OPEN_READWRITE); - if (db) return; - dataDirs += len; - if (*dataDirs) dataDirs++; - } - - char *base = strrchr(create, '/'); - *base = '\0'; - int error = mkdir(create, 0700); - if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", create); - *base = '/'; - - dbOpen(create, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); - if (!db) errx(EX_CANTCREAT, "%s: cannot create database", create); -} - -static int dbParam(sqlite3_stmt *stmt, const char *param) { - int index = sqlite3_bind_parameter_index(stmt, param); - if (index) return index; - errx(EX_SOFTWARE, "no such parameter %s: %s", param, sqlite3_sql(stmt)); -} - -static void -dbBindText(sqlite3_stmt *stmt, const char *param, const char *value) { - if (!sqlite3_bind_text(stmt, dbParam(stmt, param), value, -1, NULL)) return; - errx(EX_SOFTWARE, "sqlite3_bind_text: %s", sqlite3_errmsg(db)); -} - -static void -dbBindCopy(sqlite3_stmt *stmt, const char *param, const char *value) { - int error = sqlite3_bind_text( - stmt, dbParam(stmt, param), value, -1, SQLITE_TRANSIENT - ); - if (error) errx(EX_SOFTWARE, "sqlite3_bind_text: %s", sqlite3_errmsg(db)); -} - -static void dbVerbose(sqlite3_stmt *stmt) { - if (!verbose) return; - char *sql = sqlite3_expanded_sql(stmt); - if (sql) fprintf(stderr, "%s\n", sql); - sqlite3_free(sql); -} - -static void dbInit(void) { - const char *sql = SQL( - CREATE TABLE IF NOT EXISTS clients ( - host TEXT NOT NULL, - port INTEGER NOT NULL, - client TEXT NOT NULL, - version TEXT NOT NULL, - UNIQUE (host, port, client) - ); - CREATE TABLE IF NOT EXISTS preferences ( - client TEXT NOT NULL, - key TEXT NOT NULL, - value TEXT NOT NULL - ); - CREATE INDEX IF NOT EXISTS preferencesIndex - ON preferences (client, key); - CREATE TABLE IF NOT EXISTS badges ( - host TEXT NOT NULL, - port TEXT NOT NULL, - count INTEGER NOT NULL, - UNIQUE (host, port) - ); - ); - int error = sqlite3_exec(db, sql, NULL, NULL, NULL); - if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), sql); -} - -static void clientWrite(const char *ptr, size_t len) { - while (len) { - ssize_t ret = tls_write(client, ptr, len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); - ptr += ret; - len -= ret; - } -} - -static void format(const char *format, ...) { - char buf[1024]; - va_list ap; - va_start(ap, format); - int len = vsnprintf(buf, sizeof(buf), format, ap); - va_end(ap); - assert((size_t)len < sizeof(buf)); - if (verbose) fprintf(stderr, "%s", buf); - clientWrite(buf, len); -} - -enum { ParamCap = 4 }; -struct Message { - char *time; - char *nick; - char *cmd; - char *params[ParamCap]; -}; - -static struct Message parse(char *line) { - if (verbose) fprintf(stderr, "%s\n", line); - struct Message msg = {0}; - if (line[0] == '@') { - char *tags = 1 + strsep(&line, " "); - while (tags) { - char *tag = strsep(&tags, ";"); - char *key = strsep(&tag, "="); - if (!strcmp(key, "time")) msg.time = tag; - } - } - if (line[0] == ':') { - char *origin = 1 + strsep(&line, " "); - msg.nick = strsep(&origin, "!"); - } - msg.cmd = strsep(&line, " "); - for (size_t i = 0; line && i < ParamCap; ++i) { - if (line[0] == ':') { - msg.params[i] = &line[1]; - break; - } - msg.params[i] = strsep(&line, " "); - } - return msg; -} - -static void require(const struct Message *msg, bool nick, size_t len) { - if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); - for (size_t i = 0; i < len; ++i) { - if (msg->params[i]) continue; - errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); - } -} - -typedef void Handler(struct Message *msg); - -static void handleCap(struct Message *msg) { - require(msg, false, 3); - if (!strcmp(msg->params[1], "NAK")) { - errx(EX_CONFIG, "pounce palaver option not enabled"); - } -} - -static void handlePing(struct Message *msg) { - require(msg, false, 1); - format("PONG :%s\r\n", msg->params[0]); -} - -static void handleError(struct Message *msg) { - require(msg, false, 1); - errx(EX_UNAVAILABLE, "%s", msg->params[0]); -} - -static char *nick; -static bool away; - -static void handleReplyWelcome(struct Message *msg) { - require(msg, false, 1); - free(nick); - nick = strdup(msg->params[0]); - if (!nick) err(EX_OSERR, "strdup"); - format("USERHOST %s\r\n", nick); -} - -static void handleNick(struct Message *msg) { - require(msg, true, 1); - if (nick && !strcmp(msg->nick, nick)) { - free(nick); - nick = strdup(msg->params[0]); - if (!nick) err(EX_OSERR, "strdup"); - } -} - -static void handleReplyUserHost(struct Message *msg) { - require(msg, false, 2); - while (msg->params[1]) { - char *reply = strsep(&msg->params[1], " "); - char *replyNick = strsep(&reply, "*="); - if (strcmp(replyNick, nick)) continue; - if (reply && !reply[0]) strsep(&msg->params[1], "="); - if (!reply) errx(EX_PROTOCOL, "invalid USERHOST reply"); - away = (reply[0] == '-'); - break; - } -} - -static bool sensitive; - -static void keyword(sqlite3_context *ctx, int n, sqlite3_value *args[]) { - assert(n == 2); - const char *haystack = (const char *)sqlite3_value_text(args[0]); - const char *needle = (const char *)sqlite3_value_text(args[1]); - if (!nick || !haystack || !needle) { - sqlite3_result_null(ctx); - return; - } - - char *copy = NULL; - const char *replace; - if (!strcmp(needle, "{nick}")) { - needle = nick; - } else if (NULL != (replace = strstr(needle, "{nick}"))) { - int n = asprintf( - ©, "%.*s%s%s", - (int)(replace - needle), needle, nick, &replace[6] - ); - if (n < 0) { - sqlite3_result_error_nomem(ctx); - return; - } - needle = copy; - } - - size_t len = strlen(needle); - const char *match = haystack; - sqlite3_result_int(ctx, false); - while (NULL != (match = (sensitive ? strstr : strcasestr)(match, needle))) { - char a = (match > haystack ? match[-1] : ' '); - char b = (match[len] ? match[len] : ' '); - if (b == '\1') b = ' '; - if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) { - sqlite3_result_int(ctx, true); - break; - } - match = &match[len]; - } - free(copy); -} - -enum { - Identify, - Begin, - Set, - End, - Each, - Notify, - Increment, - Reset, - Badge, - QueriesLen, -}; - -static sqlite3_stmt *stmts[QueriesLen]; -static const char *Queries[QueriesLen] = { - [Identify] = SQL( - SELECT 1 FROM clients - WHERE host = :host AND port = :port - AND client = :client AND version = :version; - ), - - [Begin] = SQL( - DELETE FROM preferences WHERE client = :client; - ), - - [Set] = SQL( - INSERT INTO preferences (client, key, value) - VALUES (:client, :key, :value); - ), - - [End] = SQL( - INSERT INTO clients (host, port, client, version) - VALUES (:host, :port, :client, :version) - ON CONFLICT (host, port, client) DO - UPDATE SET version = :version - WHERE host = :host AND port = :port AND client = :client; - ), - - [Each] = SQL( - SELECT pushToken.value, pushEndpoint.value - FROM clients - JOIN preferences AS pushToken USING (client) - JOIN preferences AS pushEndpoint USING (client) - WHERE host = :host AND port = :port - AND pushToken.key = 'PUSH-TOKEN' - AND pushEndpoint.key = 'PUSH-ENDPOINT'; - ), - - [Notify] = SQL( - WITH mentions AS ( - SELECT DISTINCT client - FROM clients - JOIN preferences USING (client) - WHERE host = :host AND port = :port AND ( - (key = 'MENTION-KEYWORD' AND keyword(:message, value)) OR - (key = 'MENTION-CHANNEL' AND value = :channel) OR - (key = 'MENTION-NICK' AND value = :nick) OR - :direct - ) - ), - ignores AS ( - SELECT DISTINCT client - FROM clients - JOIN preferences USING (client) - WHERE host = :host AND port = :port AND ( - (key = 'IGNORE-KEYWORD' AND keyword(:message, value)) OR - (key = 'IGNORE-CHANNEL' AND value = :channel) OR - (key = 'IGNORE-NICK' AND value = :nick) - ) - ), - matches AS (SELECT * FROM mentions EXCEPT SELECT * FROM ignores) - SELECT - pushToken.value, - pushEndpoint.value, - coalesce(showMessagePreview.value, 'true') - FROM clients - JOIN matches USING (client) - JOIN preferences AS pushToken USING (client) - JOIN preferences AS pushEndpoint USING (client) - LEFT JOIN preferences AS showMessagePreview - ON showMessagePreview.client = clients.client - AND showMessagePreview.key = 'SHOW-MESSAGE-PREVIEW' - WHERE pushToken.key = 'PUSH-TOKEN' - AND pushEndpoint.key = 'PUSH-ENDPOINT'; - ), - - [Increment] = SQL( - INSERT INTO badges (host, port, count) - VALUES (:host, :port, 1) - ON CONFLICT (host, port) DO - UPDATE SET count = count + 1 - WHERE host = :host AND port = :port; - ), - - [Reset] = SQL( - DELETE FROM badges WHERE host = :host AND port = :port; - ), - - [Badge] = SQL( - SELECT sum(count) FROM badges; - ), -}; - -static int badgeCount(int op) { - dbVerbose(stmts[op]); - int result = sqlite3_step(stmts[op]); - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[op]); - - dbVerbose(stmts[Badge]); - result = sqlite3_step(stmts[Badge]); - if (result != SQLITE_ROW) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - int badge = sqlite3_column_int(stmts[Badge], 0); - sqlite3_reset(stmts[Badge]); - return badge; -} - -static void palaverIdentify(struct Message *msg) { - require(msg, false, 3); - dbBindText(stmts[Identify], ":client", msg->params[1]); - dbBindText(stmts[Identify], ":version", msg->params[2]); - dbVerbose(stmts[Identify]); - int result = sqlite3_step(stmts[Identify]); - if (result == SQLITE_DONE) { - format("PALAVER REQ\r\n"); - } else if (result != SQLITE_ROW) { - errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - } - sqlite3_reset(stmts[Identify]); -} - -static void palaverBegin(struct Message *msg) { - require(msg, false, 3); - dbBindText(stmts[Begin], ":client", msg->params[1]); - dbVerbose(stmts[Begin]); - int result = sqlite3_step(stmts[Begin]); - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[Begin]); - dbBindCopy(stmts[Set], ":client", msg->params[1]); - dbBindCopy(stmts[End], ":client", msg->params[1]); - dbBindCopy(stmts[End], ":version", msg->params[2]); -} - -static void palaverSet(struct Message *msg) { - require(msg, false, 3); - dbBindText(stmts[Set], ":key", msg->params[1]); - dbBindText(stmts[Set], ":value", msg->params[2]); - dbVerbose(stmts[Set]); - int result = sqlite3_step(stmts[Set]); - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[Set]); -} - -static void palaverEnd(struct Message *msg) { - (void)msg; - dbVerbose(stmts[End]); - int result = sqlite3_step(stmts[End]); - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[End]); -} - -static void handlePalaver(struct Message *msg) { - require(msg, false, 1); - if (!strcmp(msg->params[0], "IDENTIFY")) { - palaverIdentify(msg); - } else if (!strcmp(msg->params[0], "BEGIN")) { - palaverBegin(msg); - } else if (!strcmp(msg->params[0], "SET")) { - palaverSet(msg); - } else if (!strcmp(msg->params[0], "ADD")) { - palaverSet(msg); - } else if (!strcmp(msg->params[0], "END")) { - palaverEnd(msg); - } -} - -static void pushNotify(const char *endpoint, const char *token, char *body) { - CURLcode code = curl_easy_setopt(curl, CURLOPT_URL, endpoint); - if (code) { - warnx("%s: %s", endpoint, curlError); - return; - } - - char auth[256]; - struct curl_slist *list = NULL; - snprintf(auth, sizeof(auth), "Authorization: Bearer %s", token); - list = curl_slist_append(list, "Content-Type: application/json"); - list = curl_slist_append(list, auth); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - - size_t len = strlen(body); - FILE *file = fmemopen(body, len, "r"); - if (!file) err(EX_OSERR, "fmemopen"); - - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)len); - curl_easy_setopt(curl, CURLOPT_READDATA, file); - - if (verbose) fprintf(stderr, "%s\n", body); - code = curl_easy_perform(curl); - if (code) warnx("%s: %s", endpoint, curlError); - - fclose(file); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL); - curl_slist_free_all(list); -} - -static void handleReplyNowAway(struct Message *msg) { - (void)msg; - away = true; -} - -static void handleReplyUnaway(struct Message *msg) { - (void)msg; - if (!away) return; - away = false; - - char json[32]; - snprintf(json, sizeof(json), "{\"badge\":%d}", badgeCount(Reset)); - - int result; - dbVerbose(stmts[Each]); - while (SQLITE_ROW == (result = sqlite3_step(stmts[Each]))) { - int i = 0; - const char *token = sqlite3_column_text(stmts[Each], i++); - const char *endpoint = sqlite3_column_text(stmts[Each], i++); - pushNotify(endpoint, token, json); - } - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[Each]); -} - -static bool noPreview; -static bool noPrivatePreview; - -static void jsonString(FILE *file, const char *str) { - fputc('"', file); - for (const char *ch = str; *ch; ++ch) { - if (iscntrl(*ch) || *ch == '"' || *ch == '\\') { - fprintf(file, "\\u%04x", (unsigned)*ch); - } else { - fputc(*ch, file); - } - } - fputc('"', file); -} - -static char *jsonBody(int badge, struct Message *msg, bool preview) { - bool private = (msg->params[0][0] != '#'); - if (private && noPrivatePreview) preview = false; - if (noPreview) preview = false; - - char *buf; - size_t len; - FILE *file = open_memstream(&buf, &len); - if (!file) err(EX_OSERR, "open_memstream"); - - fprintf(file, "{\"badge\":%d", badge); - fprintf(file, ",\"sender\":"); - jsonString(file, msg->nick); - if (!private) { - fprintf(file, ",\"channel\":"); - jsonString(file, msg->params[0]); - } - if (preview) { - if (!strncmp(msg->params[1], "\1ACTION ", 8)) { - size_t len = strlen(msg->params[1]); - if (msg->params[1][len - 1] == '\1') msg->params[1][len - 1] = '\0'; - fprintf(file, ",\"intent\":\"ACTION\",\"message\":"); - jsonString(file, &msg->params[1][8]); - } else { - fprintf(file, ",\"message\":"); - jsonString(file, msg->params[1]); - } - } else { - fprintf(file, ",\"private\":true"); - } - fprintf(file, "}"); - - int error = fclose(file); - if (error) err(EX_IOERR, "fclose"); - - return buf; -} - -static void handlePrivmsg(struct Message *msg) { - require(msg, true, 2); - if (!away) return; - if (!msg->time) return; - struct tm tm = {0}; - strptime(msg->time, "%FT%T", &tm); - time_t then = timegm(&tm); - if (time(NULL) - then > 60) return; - - dbBindText(stmts[Notify], ":nick", msg->nick); - dbBindText(stmts[Notify], ":channel", msg->params[0]); - dbBindText(stmts[Notify], ":message", msg->params[1]); - dbBindText( - stmts[Notify], ":direct", (!strcmp(msg->params[0], nick) ? "1" : NULL) - ); - dbVerbose(stmts[Notify]); - int result; - int badge = 0; - while (SQLITE_ROW == (result = sqlite3_step(stmts[Notify]))) { - int i = 0; - const char *token = sqlite3_column_text(stmts[Notify], i++); - const char *endpoint = sqlite3_column_text(stmts[Notify], i++); - const char *preview = sqlite3_column_text(stmts[Notify], i++); - - if (!badge) badge = badgeCount(Increment); - char *body = jsonBody(badge, msg, !strcmp(preview, "true")); - pushNotify(endpoint, token, body); - free(body); - } - if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); - sqlite3_reset(stmts[Notify]); -} - -static const struct { - const char *cmd; - Handler *fn; -} Handlers[] = { - { "001", handleReplyWelcome }, - { "302", handleReplyUserHost }, - { "305", handleReplyUnaway }, - { "306", handleReplyNowAway }, - { "CAP", handleCap }, - { "ERROR", handleError }, - { "NICK", handleNick }, - { "NOTICE", handlePrivmsg }, - { "PALAVER", handlePalaver }, - { "PING", handlePing }, - { "PRIVMSG", handlePrivmsg }, -}; - -static void handle(struct Message *msg) { - if (!msg->cmd) return; - for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { - if (strcmp(msg->cmd, Handlers[i].cmd)) continue; - Handlers[i].fn(msg); - break; - } -} - -static void atExit(void) { - if (client) tls_close(client); - curl_easy_cleanup(curl); - for (size_t i = 0; i < QueriesLen; ++i) { - sqlite3_finalize(stmts[i]); - } - sqlite3_close(db); -} - -static void quit(int sig) { - (void)sig; - format("QUIT\r\n"); - atExit(); - _exit(EX_OK); -} - -int main(int argc, char *argv[]) { - bool insecure = false; - char *path = NULL; - const char *cert = NULL; - const char *priv = NULL; - const char *host = NULL; - const char *port = "6697"; - const char *pass = NULL; - const char *trust = NULL; - const char *user = "pounce-palaver"; - - for (int opt; 0 < (opt = getopt(argc, argv, "!NPc:d:k:p:st:u:vw:"));) { - switch (opt) { - break; case '!': insecure = true; - break; case 'N': noPreview = true; - break; case 'P': noPrivatePreview = true; - break; case 'c': cert = optarg; - break; case 'd': path = optarg; - break; case 'k': priv = optarg; - break; case 'p': port = optarg; - break; case 's': sensitive = true; - break; case 't': trust = optarg; - break; case 'u': user = optarg; - break; case 'v': verbose = true; - break; case 'w': pass = optarg; - break; default: return EX_USAGE; - } - } - if (optind == argc) errx(EX_USAGE, "host required"); - host = argv[optind]; - - CURLcode code = curl_global_init(CURL_GLOBAL_ALL); - if (code) errx(EX_OSERR, "curl_global_init: %s", curl_easy_strerror(code)); - - curl = curl_easy_init(); - if (!curl) errx(EX_SOFTWARE, "curl_easy_init"); - - curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlError); - curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); - curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); - curl_easy_setopt(curl, CURLOPT_VERBOSE, (verbose ? 1L : 0L)); - curl_easy_setopt(curl, CURLOPT_POST, 1L); - - dbFind(path); - atexit(atExit); - - dbInit(); - sqlite3_create_function( - db, "keyword", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, - keyword, NULL, NULL - ); - for (size_t i = 0; i < QueriesLen; ++i) { - int error = sqlite3_prepare_v3( - db, Queries[i], -1, SQLITE_PREPARE_PERSISTENT, &stmts[i], NULL - ); - if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), Queries[i]); - if (sqlite3_bind_parameter_index(stmts[i], ":host")) { - dbBindText(stmts[i], ":host", host); - dbBindText(stmts[i], ":port", port); - } - } - - client = tls_client(); - if (!client) errx(EX_SOFTWARE, "tls_client"); - - struct tls_config *config = tls_config_new(); - if (!config) errx(EX_SOFTWARE, "tls_config_new"); - - if (insecure) { - tls_config_insecure_noverifycert(config); - tls_config_insecure_noverifyname(config); - } - - int error; - if (trust) { - tls_config_insecure_noverifyname(config); - error = tls_config_set_ca_file(config, trust); - if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); - } - if (cert) { - error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert)); - if (error) { - errx( - EX_SOFTWARE, "tls_config_set_keypair_file: %s", - tls_config_error(config) - ); - } - } - - error = tls_configure(client, config); - if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); - tls_config_free(config); - - error = tls_connect(client, host, port); - if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); - - if (pass) format("PASS :%s\r\n", pass); - format( - "CAP REQ :server-time palaverapp.com causal.agency/passive\r\n" - "CAP END\r\n" - "NICK *\r\n" - "USER %s 0 * :pounce-palaver\r\n", - user - ); - - signal(SIGINT, quit); - signal(SIGTERM, quit); - - char buf[8191 + 512]; - size_t len = 0; - for (;;) { - ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); - if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); - if (!ret) errx(EX_PROTOCOL, "server closed connection"); - len += ret; - - char *line = buf; - for (;;) { - char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); - if (!crlf) break; - crlf[0] = '\0'; - struct Message msg = parse(line); - handle(&msg); - line = crlf + 2; - } - len -= line - buf; - memmove(buf, line, len); - } -} diff --git a/extra/palaver/pounce-palaver.1 b/extra/palaver/pounce-palaver.1 deleted file mode 100644 index da3937a..0000000 --- a/extra/palaver/pounce-palaver.1 +++ /dev/null @@ -1,112 +0,0 @@ -.Dd November 28, 2021 -.Dt POUNCE-PALAVER 1 -.Os -. -.Sh NAME -.Nm pounce-palaver -.Nd Palaver push notifications for pounce -. -.Sh SYNOPSIS -.Nm -.Op Fl PNsv -.Op Fl c Ar cert -.Op Fl d Ar path -.Op Fl k Ar priv -.Op Fl p Ar port -.Op Fl t Ar trust -.Op Fl u Ar user -.Op Fl w Ar pass -.Ar host -. -.Sh DESCRIPTION -The -.Nm -daemon provides push notifications -for the Palaver IRC app via the -.Xr pounce 1 -IRC bouncer. -The -.Cm palaver -option must be enabled in -.Xr pounce 1 . -. -.Pp -The arguments are as follows: -.Bl -tag -width Ds -.It Fl N -Never send message previews, -regardless of the app preferences. -.It Fl P -Never send message previews -for private messages. -.It Fl c Ar cert -Load the TLS client certificate from -.Ar path . -If the private key is in a separate file, -it is loaded with -.Fl k . -.It Fl d Ar path -Set the path to the database file -used to store notification preferences. -The default path is documented in -.Sx FILES . -.It Fl k Ar priv -Load the TLS client private key from -.Ar path . -.It Fl p Ar port -Connect to -.Ar port . -The default port is 6697. -.It Fl s -Match nick and keywords case-sensitively, -despite the specification. -.It Fl t Ar path -Trust the self-signed certificate loaded from -.Ar path -and disable server name verification. -.It Fl u Ar user -Set the username to -.Ar user . -The default username is -.Dq pounce-palaver . -.It Fl v -Log IRC protocol, SQL and HTTP to standard error. -.It Fl w Ar pass -Log in with the server password -.Ar pass . -.It Ar host -Connect to -.Ar host . -.El -. -.Sh FILES -.Bl -tag -width Ds -.It Pa $XDG_DATA_DIRS/pounce/palaver.sqlite -The database file is searched for first in -.Ev $XDG_DATA_HOME , -usually -.Pa ~/.local/share , -followed by the colon-separated list of paths -.Ev $XDG_DATA_DIRS , -usually -.Pa /usr/local/share:/usr/share . -.It Pa ~/.local/share/pounce/palaver.sqlite -The most likely default path to the database file. -.El -. -.Sh SEE ALSO -.Xr pounce 1 -. -.Sh STANDARDS -.Lk https://github.com/cocodelabs/palaver-irc-capability "Palaver IRC Capability" -. -.Sh AUTHORS -.An June Bug Aq Mt june@causal.agency -. -.Sh BUGS -Send mail to -.Aq Mt list+pounce@causal.agency -or join -.Li #ascii.town -on -.Li irc.tilde.chat . diff --git a/notify.c b/notify.c new file mode 100644 index 0000000..935ba26 --- /dev/null +++ b/notify.c @@ -0,0 +1,353 @@ +/* Copyright (C) 2021 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7: + * + * If you modify this Program, or any covered work, by linking or + * combining it with OpenSSL (or a modified version of that library), + * containing parts covered by the terms of the OpenSSL License and the + * original SSLeay license, the licensors of this Program grant you + * additional permission to convey the resulting work. Corresponding + * Source for a non-source form of such a combination shall include the + * source code for the parts of OpenSSL used as well as that of the + * covered work. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) + +static bool verbose; +static struct tls *client; + +static void clientWrite(const char *ptr, size_t len) { + while (len) { + ssize_t ret = tls_write(client, ptr, len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); + ptr += ret; + len -= ret; + } +} + +static void format(const char *format, ...) { + char buf[1024]; + va_list ap; + va_start(ap, format); + int len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + assert((size_t)len < sizeof(buf)); + if (verbose) fprintf(stderr, "%s", buf); + clientWrite(buf, len); +} + +enum { ParamCap = 2 }; +struct Message { + char *time; + char *nick; + char *user; + char *host; + char *cmd; + char *params[ParamCap]; +}; + +static struct Message parse(char *line) { + if (verbose) fprintf(stderr, "%s\n", line); + struct Message msg = {0}; + if (line[0] == '@') { + char *tags = 1 + strsep(&line, " "); + while (tags) { + char *tag = strsep(&tags, ";"); + char *key = strsep(&tag, "="); + if (!strcmp(key, "time")) msg.time = tag; + } + } + if (line[0] == ':') { + char *origin = 1 + strsep(&line, " "); + msg.nick = strsep(&origin, "!"); + msg.user = strsep(&origin, "@"); + msg.host = origin; + } + msg.cmd = strsep(&line, " "); + for (size_t i = 0; line && i < ParamCap; ++i) { + if (line[0] == ':') { + msg.params[i] = &line[1]; + break; + } + msg.params[i] = strsep(&line, " "); + } + return msg; +} + +static void require(const struct Message *msg, bool nick, size_t len) { + if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); + for (size_t i = 0; i < len; ++i) { + if (msg->params[i]) continue; + errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); + } +} + +typedef void Handler(struct Message *msg); + +static void handlePing(struct Message *msg) { + require(msg, false, 1); + format("PONG :%s\r\n", msg->params[0]); +} + +static void handleError(struct Message *msg) { + require(msg, false, 1); + errx(EX_UNAVAILABLE, "%s", msg->params[0]); +} + +static char *nick; +static bool away; + +static void handleReplyWelcome(struct Message *msg) { + require(msg, false, 1); + free(nick); + nick = strdup(msg->params[0]); + if (!nick) err(EX_OSERR, "strdup"); + format("USERHOST %s\r\n", nick); +} + +static void handleNick(struct Message *msg) { + require(msg, true, 1); + if (nick && !strcmp(msg->nick, nick)) { + free(nick); + nick = strdup(msg->params[0]); + if (!nick) err(EX_OSERR, "strdup"); + } +} + +static void handleReplyUserHost(struct Message *msg) { + require(msg, false, 2); + while (msg->params[1]) { + char *reply = strsep(&msg->params[1], " "); + char *replyNick = strsep(&reply, "*="); + if (strcmp(replyNick, nick)) continue; + if (reply && !reply[0]) strsep(&msg->params[1], "="); + if (!reply) errx(EX_PROTOCOL, "invalid USERHOST reply"); + away = (reply[0] == '-'); + break; + } +} + +static void handleReplyNowAway(struct Message *msg) { + (void)msg; + away = true; +} + +static void handleReplyUnaway(struct Message *msg) { + (void)msg; + away = false; +} + +static const char *command; + +static void handlePrivmsg(struct Message *msg) { + require(msg, true, 2); + if (!nick || !away) return; + + if (!msg->time) return; + struct tm tm = {0}; + strptime(msg->time, "%FT%T", &tm); + time_t then = timegm(&tm); + if (time(NULL) - then > 60) return; + + bool query = (msg->params[0][0] != '#'); + bool mention = false; + size_t len = strlen(nick); + for ( + char *match = msg->params[1]; + NULL != (match = strstr(match, nick)); + match = &match[len] + ) { + char a = (match > msg->params[1] ? match[-1] : ' '); + char b = (match[len] ? match[len] : ' '); + if (b == '\1') b = ' '; + if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) { + mention = true; + break; + } + match = &match[len]; + } + if (!query && !mention) return; + + pid_t pid = fork(); + if (pid < 0) err(EX_OSERR, "fork"); + if (pid) return; + + setenv("NOTIFY_TIME", msg->time, 1); + setenv("NOTIFY_NICK", msg->nick, 1); + if (msg->user) setenv("NOTIFY_USER", msg->user, 1); + if (msg->host) setenv("NOTIFY_HOST", msg->host, 1); + if (!query) setenv("NOTIFY_CHANNEL", msg->params[0], 1); + setenv("NOTIFY_MESSAGE", msg->params[1], 1); + + const char *shell = getenv("SHELL"); + if (!shell) shell = "/bin/sh"; + execl(shell, "sh", "-c", command, NULL); + err(EX_OSERR, "%s", shell); +} + +static const struct { + const char *cmd; + Handler *fn; +} Handlers[] = { + { "001", handleReplyWelcome }, + { "302", handleReplyUserHost }, + { "305", handleReplyUnaway }, + { "306", handleReplyNowAway }, + { "ERROR", handleError }, + { "NICK", handleNick }, + { "PING", handlePing }, + { "PRIVMSG", handlePrivmsg }, +}; + +static void handle(struct Message *msg) { + if (!msg->cmd) return; + for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { + if (strcmp(msg->cmd, Handlers[i].cmd)) continue; + Handlers[i].fn(msg); + break; + } +} + +static void reap(int sig) { + (void)sig; + int status; + wait(&status); +} + +static void quit(int sig) { + (void)sig; + format("QUIT\r\n"); + tls_close(client); + _exit(EX_OK); +} + +int main(int argc, char *argv[]) { + bool insecure = false; + const char *cert = NULL; + const char *priv = NULL; + const char *host = NULL; + const char *port = "6697"; + const char *pass = NULL; + const char *trust = NULL; + const char *user = "pounce-notify"; + + for (int opt; 0 < (opt = getopt(argc, argv, "!c:k:p:t:u:vw:"));) { + switch (opt) { + break; case '!': insecure = true; + break; case 'c': cert = optarg; + break; case 'k': priv = optarg; + break; case 'p': port = optarg; + break; case 't': trust = optarg; + break; case 'u': user = optarg; + break; case 'v': verbose = true; + break; case 'w': pass = optarg; + break; default: return EX_USAGE; + } + } + if (argc - optind < 1) errx(EX_USAGE, "host required"); + if (argc - optind < 2) errx(EX_USAGE, "command required"); + host = argv[optind++]; + command = argv[optind]; + + setenv("POUNCE_HOST", host, 1); + setenv("POUNCE_PORT", port, 1); + + client = tls_client(); + if (!client) errx(EX_SOFTWARE, "tls_client"); + + struct tls_config *config = tls_config_new(); + if (!config) errx(EX_SOFTWARE, "tls_config_new"); + + if (insecure) { + tls_config_insecure_noverifycert(config); + tls_config_insecure_noverifyname(config); + } + + int error; + if (trust) { + tls_config_insecure_noverifyname(config); + error = tls_config_set_ca_file(config, trust); + if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); + } + if (cert) { + error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert)); + if (error) { + errx( + EX_NOINPUT, "tls_config_set_keypair_file: %s", + tls_config_error(config) + ); + } + } + + error = tls_configure(client, config); + if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); + tls_config_free(config); + + error = tls_connect(client, host, port); + if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); + + if (pass) format("PASS :%s\r\n", pass); + format( + "CAP REQ :causal.agency/passive server-time\r\n" + "CAP END\r\n" + "NICK *\r\n" + "USER %s 0 * :pounce-notify\r\n", + user + ); + + signal(SIGINT, quit); + signal(SIGTERM, quit); + signal(SIGCHLD, reap); + + size_t len = 0; + char buf[8191 + 512]; + for (;;) { + ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); + if (!ret) errx(EX_PROTOCOL, "server closed connection"); + len += ret; + + char *line = buf; + for (;;) { + char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); + if (!crlf) break; + *crlf = '\0'; + struct Message msg = parse(line); + handle(&msg); + line = crlf + 2; + } + len -= line - buf; + memmove(buf, line, len); + } +} diff --git a/palaver.c b/palaver.c new file mode 100644 index 0000000..36e4ae1 --- /dev/null +++ b/palaver.c @@ -0,0 +1,817 @@ +/* Copyright (C) 2019 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7: + * + * If you modify this Program, or any covered work, by linking or + * combining it with OpenSSL (or a modified version of that library), + * containing parts covered by the terms of the OpenSSL License and the + * original SSLeay license, the licensors of this Program grant you + * additional permission to convey the resulting work. Corresponding + * Source for a non-source form of such a combination shall include the + * source code for the parts of OpenSSL used as well as that of the + * covered work. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Why must it return (const unsigned char *)? +#define sqlite3_column_text(...) (const char *)sqlite3_column_text(__VA_ARGS__) + +#define DATABASE_PATH "pounce/palaver.sqlite" + +#define SQL(...) #__VA_ARGS__ +#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) + +static bool verbose; +static char curlError[CURL_ERROR_SIZE]; + +static CURL *curl; +static sqlite3 *db; +static struct tls *client; + +static void dbOpen(const char *path, int flags) { + int error = sqlite3_open_v2(path, &db, flags, NULL); + if (error == SQLITE_CANTOPEN) { + sqlite3_close(db); + db = NULL; + return; + } + if (error) errx(EX_NOINPUT, "%s: %s", path, sqlite3_errmsg(db)); + + sqlite3_busy_timeout(db, 10000); +} + +static void dbFind(char *path) { + if (path) { + dbOpen(path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + if (db) return; + errx(EX_NOINPUT, "%s: database not found", path); + } + + const char *home = getenv("HOME"); + const char *dataHome = getenv("XDG_DATA_HOME"); + const char *dataDirs = getenv("XDG_DATA_DIRS"); + if (!dataDirs) dataDirs = "/usr/local/share:/usr/share"; + + char buf[PATH_MAX]; + if (dataHome) { + snprintf(buf, sizeof(buf), "%s/" DATABASE_PATH, dataHome); + } else { + if (!home) errx(EX_CONFIG, "HOME unset"); + snprintf(buf, sizeof(buf), "%s/.local/share/" DATABASE_PATH, home); + } + dbOpen(buf, SQLITE_OPEN_READWRITE); + if (db) return; + + char create[PATH_MAX]; + snprintf(create, sizeof(create), "%s", buf); + + while (*dataDirs) { + size_t len = strcspn(dataDirs, ":"); + snprintf(buf, sizeof(buf), "%.*s/" DATABASE_PATH, (int)len, dataDirs); + dbOpen(buf, SQLITE_OPEN_READWRITE); + if (db) return; + dataDirs += len; + if (*dataDirs) dataDirs++; + } + + char *base = strrchr(create, '/'); + *base = '\0'; + int error = mkdir(create, 0700); + if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", create); + *base = '/'; + + dbOpen(create, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + if (!db) errx(EX_CANTCREAT, "%s: cannot create database", create); +} + +static int dbParam(sqlite3_stmt *stmt, const char *param) { + int index = sqlite3_bind_parameter_index(stmt, param); + if (index) return index; + errx(EX_SOFTWARE, "no such parameter %s: %s", param, sqlite3_sql(stmt)); +} + +static void +dbBindText(sqlite3_stmt *stmt, const char *param, const char *value) { + if (!sqlite3_bind_text(stmt, dbParam(stmt, param), value, -1, NULL)) return; + errx(EX_SOFTWARE, "sqlite3_bind_text: %s", sqlite3_errmsg(db)); +} + +static void +dbBindCopy(sqlite3_stmt *stmt, const char *param, const char *value) { + int error = sqlite3_bind_text( + stmt, dbParam(stmt, param), value, -1, SQLITE_TRANSIENT + ); + if (error) errx(EX_SOFTWARE, "sqlite3_bind_text: %s", sqlite3_errmsg(db)); +} + +static void dbVerbose(sqlite3_stmt *stmt) { + if (!verbose) return; + char *sql = sqlite3_expanded_sql(stmt); + if (sql) fprintf(stderr, "%s\n", sql); + sqlite3_free(sql); +} + +static void dbInit(void) { + const char *sql = SQL( + CREATE TABLE IF NOT EXISTS clients ( + host TEXT NOT NULL, + port INTEGER NOT NULL, + client TEXT NOT NULL, + version TEXT NOT NULL, + UNIQUE (host, port, client) + ); + CREATE TABLE IF NOT EXISTS preferences ( + client TEXT NOT NULL, + key TEXT NOT NULL, + value TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS preferencesIndex + ON preferences (client, key); + CREATE TABLE IF NOT EXISTS badges ( + host TEXT NOT NULL, + port TEXT NOT NULL, + count INTEGER NOT NULL, + UNIQUE (host, port) + ); + ); + int error = sqlite3_exec(db, sql, NULL, NULL, NULL); + if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), sql); +} + +static void clientWrite(const char *ptr, size_t len) { + while (len) { + ssize_t ret = tls_write(client, ptr, len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(client)); + ptr += ret; + len -= ret; + } +} + +static void format(const char *format, ...) { + char buf[1024]; + va_list ap; + va_start(ap, format); + int len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + assert((size_t)len < sizeof(buf)); + if (verbose) fprintf(stderr, "%s", buf); + clientWrite(buf, len); +} + +enum { ParamCap = 4 }; +struct Message { + char *time; + char *nick; + char *cmd; + char *params[ParamCap]; +}; + +static struct Message parse(char *line) { + if (verbose) fprintf(stderr, "%s\n", line); + struct Message msg = {0}; + if (line[0] == '@') { + char *tags = 1 + strsep(&line, " "); + while (tags) { + char *tag = strsep(&tags, ";"); + char *key = strsep(&tag, "="); + if (!strcmp(key, "time")) msg.time = tag; + } + } + if (line[0] == ':') { + char *origin = 1 + strsep(&line, " "); + msg.nick = strsep(&origin, "!"); + } + msg.cmd = strsep(&line, " "); + for (size_t i = 0; line && i < ParamCap; ++i) { + if (line[0] == ':') { + msg.params[i] = &line[1]; + break; + } + msg.params[i] = strsep(&line, " "); + } + return msg; +} + +static void require(const struct Message *msg, bool nick, size_t len) { + if (nick && !msg->nick) errx(EX_PROTOCOL, "%s missing origin", msg->cmd); + for (size_t i = 0; i < len; ++i) { + if (msg->params[i]) continue; + errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i); + } +} + +typedef void Handler(struct Message *msg); + +static void handleCap(struct Message *msg) { + require(msg, false, 3); + if (!strcmp(msg->params[1], "NAK")) { + errx(EX_CONFIG, "pounce palaver option not enabled"); + } +} + +static void handlePing(struct Message *msg) { + require(msg, false, 1); + format("PONG :%s\r\n", msg->params[0]); +} + +static void handleError(struct Message *msg) { + require(msg, false, 1); + errx(EX_UNAVAILABLE, "%s", msg->params[0]); +} + +static char *nick; +static bool away; + +static void handleReplyWelcome(struct Message *msg) { + require(msg, false, 1); + free(nick); + nick = strdup(msg->params[0]); + if (!nick) err(EX_OSERR, "strdup"); + format("USERHOST %s\r\n", nick); +} + +static void handleNick(struct Message *msg) { + require(msg, true, 1); + if (nick && !strcmp(msg->nick, nick)) { + free(nick); + nick = strdup(msg->params[0]); + if (!nick) err(EX_OSERR, "strdup"); + } +} + +static void handleReplyUserHost(struct Message *msg) { + require(msg, false, 2); + while (msg->params[1]) { + char *reply = strsep(&msg->params[1], " "); + char *replyNick = strsep(&reply, "*="); + if (strcmp(replyNick, nick)) continue; + if (reply && !reply[0]) strsep(&msg->params[1], "="); + if (!reply) errx(EX_PROTOCOL, "invalid USERHOST reply"); + away = (reply[0] == '-'); + break; + } +} + +static bool sensitive; + +static void keyword(sqlite3_context *ctx, int n, sqlite3_value *args[]) { + assert(n == 2); + const char *haystack = (const char *)sqlite3_value_text(args[0]); + const char *needle = (const char *)sqlite3_value_text(args[1]); + if (!nick || !haystack || !needle) { + sqlite3_result_null(ctx); + return; + } + + char *copy = NULL; + const char *replace; + if (!strcmp(needle, "{nick}")) { + needle = nick; + } else if (NULL != (replace = strstr(needle, "{nick}"))) { + int n = asprintf( + ©, "%.*s%s%s", + (int)(replace - needle), needle, nick, &replace[6] + ); + if (n < 0) { + sqlite3_result_error_nomem(ctx); + return; + } + needle = copy; + } + + size_t len = strlen(needle); + const char *match = haystack; + sqlite3_result_int(ctx, false); + while (NULL != (match = (sensitive ? strstr : strcasestr)(match, needle))) { + char a = (match > haystack ? match[-1] : ' '); + char b = (match[len] ? match[len] : ' '); + if (b == '\1') b = ' '; + if ((isspace(a) || ispunct(a)) && (isspace(b) || ispunct(b))) { + sqlite3_result_int(ctx, true); + break; + } + match = &match[len]; + } + free(copy); +} + +enum { + Identify, + Begin, + Set, + End, + Each, + Notify, + Increment, + Reset, + Badge, + QueriesLen, +}; + +static sqlite3_stmt *stmts[QueriesLen]; +static const char *Queries[QueriesLen] = { + [Identify] = SQL( + SELECT 1 FROM clients + WHERE host = :host AND port = :port + AND client = :client AND version = :version; + ), + + [Begin] = SQL( + DELETE FROM preferences WHERE client = :client; + ), + + [Set] = SQL( + INSERT INTO preferences (client, key, value) + VALUES (:client, :key, :value); + ), + + [End] = SQL( + INSERT INTO clients (host, port, client, version) + VALUES (:host, :port, :client, :version) + ON CONFLICT (host, port, client) DO + UPDATE SET version = :version + WHERE host = :host AND port = :port AND client = :client; + ), + + [Each] = SQL( + SELECT pushToken.value, pushEndpoint.value + FROM clients + JOIN preferences AS pushToken USING (client) + JOIN preferences AS pushEndpoint USING (client) + WHERE host = :host AND port = :port + AND pushToken.key = 'PUSH-TOKEN' + AND pushEndpoint.key = 'PUSH-ENDPOINT'; + ), + + [Notify] = SQL( + WITH mentions AS ( + SELECT DISTINCT client + FROM clients + JOIN preferences USING (client) + WHERE host = :host AND port = :port AND ( + (key = 'MENTION-KEYWORD' AND keyword(:message, value)) OR + (key = 'MENTION-CHANNEL' AND value = :channel) OR + (key = 'MENTION-NICK' AND value = :nick) OR + :direct + ) + ), + ignores AS ( + SELECT DISTINCT client + FROM clients + JOIN preferences USING (client) + WHERE host = :host AND port = :port AND ( + (key = 'IGNORE-KEYWORD' AND keyword(:message, value)) OR + (key = 'IGNORE-CHANNEL' AND value = :channel) OR + (key = 'IGNORE-NICK' AND value = :nick) + ) + ), + matches AS (SELECT * FROM mentions EXCEPT SELECT * FROM ignores) + SELECT + pushToken.value, + pushEndpoint.value, + coalesce(showMessagePreview.value, 'true') + FROM clients + JOIN matches USING (client) + JOIN preferences AS pushToken USING (client) + JOIN preferences AS pushEndpoint USING (client) + LEFT JOIN preferences AS showMessagePreview + ON showMessagePreview.client = clients.client + AND showMessagePreview.key = 'SHOW-MESSAGE-PREVIEW' + WHERE pushToken.key = 'PUSH-TOKEN' + AND pushEndpoint.key = 'PUSH-ENDPOINT'; + ), + + [Increment] = SQL( + INSERT INTO badges (host, port, count) + VALUES (:host, :port, 1) + ON CONFLICT (host, port) DO + UPDATE SET count = count + 1 + WHERE host = :host AND port = :port; + ), + + [Reset] = SQL( + DELETE FROM badges WHERE host = :host AND port = :port; + ), + + [Badge] = SQL( + SELECT sum(count) FROM badges; + ), +}; + +static int badgeCount(int op) { + dbVerbose(stmts[op]); + int result = sqlite3_step(stmts[op]); + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[op]); + + dbVerbose(stmts[Badge]); + result = sqlite3_step(stmts[Badge]); + if (result != SQLITE_ROW) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + int badge = sqlite3_column_int(stmts[Badge], 0); + sqlite3_reset(stmts[Badge]); + return badge; +} + +static void palaverIdentify(struct Message *msg) { + require(msg, false, 3); + dbBindText(stmts[Identify], ":client", msg->params[1]); + dbBindText(stmts[Identify], ":version", msg->params[2]); + dbVerbose(stmts[Identify]); + int result = sqlite3_step(stmts[Identify]); + if (result == SQLITE_DONE) { + format("PALAVER REQ\r\n"); + } else if (result != SQLITE_ROW) { + errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + } + sqlite3_reset(stmts[Identify]); +} + +static void palaverBegin(struct Message *msg) { + require(msg, false, 3); + dbBindText(stmts[Begin], ":client", msg->params[1]); + dbVerbose(stmts[Begin]); + int result = sqlite3_step(stmts[Begin]); + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[Begin]); + dbBindCopy(stmts[Set], ":client", msg->params[1]); + dbBindCopy(stmts[End], ":client", msg->params[1]); + dbBindCopy(stmts[End], ":version", msg->params[2]); +} + +static void palaverSet(struct Message *msg) { + require(msg, false, 3); + dbBindText(stmts[Set], ":key", msg->params[1]); + dbBindText(stmts[Set], ":value", msg->params[2]); + dbVerbose(stmts[Set]); + int result = sqlite3_step(stmts[Set]); + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[Set]); +} + +static void palaverEnd(struct Message *msg) { + (void)msg; + dbVerbose(stmts[End]); + int result = sqlite3_step(stmts[End]); + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[End]); +} + +static void handlePalaver(struct Message *msg) { + require(msg, false, 1); + if (!strcmp(msg->params[0], "IDENTIFY")) { + palaverIdentify(msg); + } else if (!strcmp(msg->params[0], "BEGIN")) { + palaverBegin(msg); + } else if (!strcmp(msg->params[0], "SET")) { + palaverSet(msg); + } else if (!strcmp(msg->params[0], "ADD")) { + palaverSet(msg); + } else if (!strcmp(msg->params[0], "END")) { + palaverEnd(msg); + } +} + +static void pushNotify(const char *endpoint, const char *token, char *body) { + CURLcode code = curl_easy_setopt(curl, CURLOPT_URL, endpoint); + if (code) { + warnx("%s: %s", endpoint, curlError); + return; + } + + char auth[256]; + struct curl_slist *list = NULL; + snprintf(auth, sizeof(auth), "Authorization: Bearer %s", token); + list = curl_slist_append(list, "Content-Type: application/json"); + list = curl_slist_append(list, auth); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + + size_t len = strlen(body); + FILE *file = fmemopen(body, len, "r"); + if (!file) err(EX_OSERR, "fmemopen"); + + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)len); + curl_easy_setopt(curl, CURLOPT_READDATA, file); + + if (verbose) fprintf(stderr, "%s\n", body); + code = curl_easy_perform(curl); + if (code) warnx("%s: %s", endpoint, curlError); + + fclose(file); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL); + curl_slist_free_all(list); +} + +static void handleReplyNowAway(struct Message *msg) { + (void)msg; + away = true; +} + +static void handleReplyUnaway(struct Message *msg) { + (void)msg; + if (!away) return; + away = false; + + char json[32]; + snprintf(json, sizeof(json), "{\"badge\":%d}", badgeCount(Reset)); + + int result; + dbVerbose(stmts[Each]); + while (SQLITE_ROW == (result = sqlite3_step(stmts[Each]))) { + int i = 0; + const char *token = sqlite3_column_text(stmts[Each], i++); + const char *endpoint = sqlite3_column_text(stmts[Each], i++); + pushNotify(endpoint, token, json); + } + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[Each]); +} + +static bool noPreview; +static bool noPrivatePreview; + +static void jsonString(FILE *file, const char *str) { + fputc('"', file); + for (const char *ch = str; *ch; ++ch) { + if (iscntrl(*ch) || *ch == '"' || *ch == '\\') { + fprintf(file, "\\u%04x", (unsigned)*ch); + } else { + fputc(*ch, file); + } + } + fputc('"', file); +} + +static char *jsonBody(int badge, struct Message *msg, bool preview) { + bool private = (msg->params[0][0] != '#'); + if (private && noPrivatePreview) preview = false; + if (noPreview) preview = false; + + char *buf; + size_t len; + FILE *file = open_memstream(&buf, &len); + if (!file) err(EX_OSERR, "open_memstream"); + + fprintf(file, "{\"badge\":%d", badge); + fprintf(file, ",\"sender\":"); + jsonString(file, msg->nick); + if (!private) { + fprintf(file, ",\"channel\":"); + jsonString(file, msg->params[0]); + } + if (preview) { + if (!strncmp(msg->params[1], "\1ACTION ", 8)) { + size_t len = strlen(msg->params[1]); + if (msg->params[1][len - 1] == '\1') msg->params[1][len - 1] = '\0'; + fprintf(file, ",\"intent\":\"ACTION\",\"message\":"); + jsonString(file, &msg->params[1][8]); + } else { + fprintf(file, ",\"message\":"); + jsonString(file, msg->params[1]); + } + } else { + fprintf(file, ",\"private\":true"); + } + fprintf(file, "}"); + + int error = fclose(file); + if (error) err(EX_IOERR, "fclose"); + + return buf; +} + +static void handlePrivmsg(struct Message *msg) { + require(msg, true, 2); + if (!away) return; + if (!msg->time) return; + struct tm tm = {0}; + strptime(msg->time, "%FT%T", &tm); + time_t then = timegm(&tm); + if (time(NULL) - then > 60) return; + + dbBindText(stmts[Notify], ":nick", msg->nick); + dbBindText(stmts[Notify], ":channel", msg->params[0]); + dbBindText(stmts[Notify], ":message", msg->params[1]); + dbBindText( + stmts[Notify], ":direct", (!strcmp(msg->params[0], nick) ? "1" : NULL) + ); + dbVerbose(stmts[Notify]); + int result; + int badge = 0; + while (SQLITE_ROW == (result = sqlite3_step(stmts[Notify]))) { + int i = 0; + const char *token = sqlite3_column_text(stmts[Notify], i++); + const char *endpoint = sqlite3_column_text(stmts[Notify], i++); + const char *preview = sqlite3_column_text(stmts[Notify], i++); + + if (!badge) badge = badgeCount(Increment); + char *body = jsonBody(badge, msg, !strcmp(preview, "true")); + pushNotify(endpoint, token, body); + free(body); + } + if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + sqlite3_reset(stmts[Notify]); +} + +static const struct { + const char *cmd; + Handler *fn; +} Handlers[] = { + { "001", handleReplyWelcome }, + { "302", handleReplyUserHost }, + { "305", handleReplyUnaway }, + { "306", handleReplyNowAway }, + { "CAP", handleCap }, + { "ERROR", handleError }, + { "NICK", handleNick }, + { "NOTICE", handlePrivmsg }, + { "PALAVER", handlePalaver }, + { "PING", handlePing }, + { "PRIVMSG", handlePrivmsg }, +}; + +static void handle(struct Message *msg) { + if (!msg->cmd) return; + for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) { + if (strcmp(msg->cmd, Handlers[i].cmd)) continue; + Handlers[i].fn(msg); + break; + } +} + +static void atExit(void) { + if (client) tls_close(client); + curl_easy_cleanup(curl); + for (size_t i = 0; i < QueriesLen; ++i) { + sqlite3_finalize(stmts[i]); + } + sqlite3_close(db); +} + +static void quit(int sig) { + (void)sig; + format("QUIT\r\n"); + atExit(); + _exit(EX_OK); +} + +int main(int argc, char *argv[]) { + bool insecure = false; + char *path = NULL; + const char *cert = NULL; + const char *priv = NULL; + const char *host = NULL; + const char *port = "6697"; + const char *pass = NULL; + const char *trust = NULL; + const char *user = "pounce-palaver"; + + for (int opt; 0 < (opt = getopt(argc, argv, "!NPc:d:k:p:st:u:vw:"));) { + switch (opt) { + break; case '!': insecure = true; + break; case 'N': noPreview = true; + break; case 'P': noPrivatePreview = true; + break; case 'c': cert = optarg; + break; case 'd': path = optarg; + break; case 'k': priv = optarg; + break; case 'p': port = optarg; + break; case 's': sensitive = true; + break; case 't': trust = optarg; + break; case 'u': user = optarg; + break; case 'v': verbose = true; + break; case 'w': pass = optarg; + break; default: return EX_USAGE; + } + } + if (optind == argc) errx(EX_USAGE, "host required"); + host = argv[optind]; + + CURLcode code = curl_global_init(CURL_GLOBAL_ALL); + if (code) errx(EX_OSERR, "curl_global_init: %s", curl_easy_strerror(code)); + + curl = curl_easy_init(); + if (!curl) errx(EX_SOFTWARE, "curl_easy_init"); + + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlError); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); + curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); + curl_easy_setopt(curl, CURLOPT_VERBOSE, (verbose ? 1L : 0L)); + curl_easy_setopt(curl, CURLOPT_POST, 1L); + + dbFind(path); + atexit(atExit); + + dbInit(); + sqlite3_create_function( + db, "keyword", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, + keyword, NULL, NULL + ); + for (size_t i = 0; i < QueriesLen; ++i) { + int error = sqlite3_prepare_v3( + db, Queries[i], -1, SQLITE_PREPARE_PERSISTENT, &stmts[i], NULL + ); + if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), Queries[i]); + if (sqlite3_bind_parameter_index(stmts[i], ":host")) { + dbBindText(stmts[i], ":host", host); + dbBindText(stmts[i], ":port", port); + } + } + + client = tls_client(); + if (!client) errx(EX_SOFTWARE, "tls_client"); + + struct tls_config *config = tls_config_new(); + if (!config) errx(EX_SOFTWARE, "tls_config_new"); + + if (insecure) { + tls_config_insecure_noverifycert(config); + tls_config_insecure_noverifyname(config); + } + + int error; + if (trust) { + tls_config_insecure_noverifyname(config); + error = tls_config_set_ca_file(config, trust); + if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); + } + if (cert) { + error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert)); + if (error) { + errx( + EX_SOFTWARE, "tls_config_set_keypair_file: %s", + tls_config_error(config) + ); + } + } + + error = tls_configure(client, config); + if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); + tls_config_free(config); + + error = tls_connect(client, host, port); + if (error) errx(EX_UNAVAILABLE, "tls_connect: %s", tls_error(client)); + + if (pass) format("PASS :%s\r\n", pass); + format( + "CAP REQ :server-time palaverapp.com causal.agency/passive\r\n" + "CAP END\r\n" + "NICK *\r\n" + "USER %s 0 * :pounce-palaver\r\n", + user + ); + + signal(SIGINT, quit); + signal(SIGTERM, quit); + + char buf[8191 + 512]; + size_t len = 0; + for (;;) { + ssize_t ret = tls_read(client, &buf[len], sizeof(buf) - len); + if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; + if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(client)); + if (!ret) errx(EX_PROTOCOL, "server closed connection"); + len += ret; + + char *line = buf; + for (;;) { + char *crlf = memmem(line, &buf[len] - line, "\r\n", 2); + if (!crlf) break; + crlf[0] = '\0'; + struct Message msg = parse(line); + handle(&msg); + line = crlf + 2; + } + len -= line - buf; + memmove(buf, line, len); + } +} diff --git a/pounce-edit.1 b/pounce-edit.1 new file mode 100644 index 0000000..36c59aa --- /dev/null +++ b/pounce-edit.1 @@ -0,0 +1,133 @@ +.Dd April 3, 2022 +.Dt POUNCE-EDIT 7 +.Os +. +.Sh NAME +.Nm pounce-edit +.Nd pounce configuration over IRC +. +.Sh SYNOPSIS +.Nm +.Op Fl av +.Op Fl c Ar cert +.Op Fl h Ar host +.Op Fl k Ar priv +.Op Fl p Ar port +.Op Fl t Ar trust +.Op Fl u Ar user +.Op Fl w Ar pass +.Ar config ... +. +.Sh DESCRIPTION +The +.Nm +client provides +.Xr pounce 1 +configuration editing +over IRC. +It accepts commands +sent to the user's own nick. +It writes its changes +to the last configuration file +listed on the command line. +Changes do not take effect until +.Xr pounce 1 +is restarted. +Configuration files +are searched for +in the same manner as +.Xr pounce 1 . +. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl a +Allow setting all options, +including those that +may render +.Xr pounce 1 +inoperable. +. +.It Fl c Ar cert +Load the TLS client certificate from +.Ar cert . +If the private key +is in a separate file, +it is loaded with +.Fl k . +. +.It Fl h Ar host +Connect to +.Ar host . +The default is the value of +.Cm local-host +from the configuration files. +. +.It Fl p Ar port +Connect to +.Ar port . +The default is the value of +.Cm local-port +from the configuration files, +or 6697. +. +.It Fl t Ar trust +Trust the self-signed certificate loaded from +.Ar path +and disable server name verification. +. +.It Fl u Ar user +Set the username to +.Ar user . +The default username is +.Dq pounce-edit . +. +.It Fl v +Log IRC protocol to standard error. +. +.It Fl w Ar pass +Log in with the server password +.Ar pass . +.El +. +.Sh COMMANDS +Commands are sent as private messages +to the user's own nick. +Replies are sent as notices. +.Bl -tag -width Ds +.It Ic get Op Ar option +Show the value of an option, +or list all set options. +.It Ic restart +Restart +.Xr pounce 1 . +.It Ic set Ar option Op Ar value +Set an option. +.It Ic unset Ar option +Unset an option. +.El +. +.Sh FILES +See +.Xr pounce 1 . +. +.Sh EXAMPLES +Inherit options from +.Pa tilde.conf , +write changes to +.Pa tilde.edit : +.Bd -literal -offset indent +$ pounce tilde.conf tilde.edit +$ pounce-edit -c local.pem tilde.conf tilde.edit +.Ed +. +.Sh AUTHORS +.An June McEnroe Aq Mt june@causal.agency +. +.Sh BUGS +Send mail to +.Aq Mt list+pounce@causal.agency +or join +.Li #ascii.town +on +.Li irc.tilde.chat . diff --git a/pounce-notify.1 b/pounce-notify.1 new file mode 100644 index 0000000..628bc25 --- /dev/null +++ b/pounce-notify.1 @@ -0,0 +1,115 @@ +.Dd November 28, 2021 +.Dt POUNCE-NOTIFY 1 +.Os +. +.Sh NAME +.Nm pounce-notify +.Nd notifications for pounce +. +.Sh SYNOPSIS +.Nm +.Op Fl v +.Op Fl c Ar cert +.Op Fl k Ar priv +.Op Fl p Ar port +.Op Fl t Ar trust +.Op Fl u Ar user +.Op Fl w Ar pass +.Ar host +.Ar command +. +.Sh DESCRIPTION +The +.Nm +program connects to an instance of +.Xr pounce 1 +at +.Ar host +and runs +.Ar command +for each private message +or message containing the user's nickname +while the user is marked as away. +The nickname is matched case-sensitively +and must be surrounded by whitespace or punctuation. +Information about the message +is passed to the command +through the environment. +. +.Pp +The arguments are as follows: +.Bl -tag -width Ds +.It Fl c Ar cert +Load the TLS client certificate from +.Ar cert . +If the private key is in a separate file, +it is loaded with +.Fl k . +.It Fl k Ar priv +Load the TLS client private key from +.Ar priv . +.It Fl p Ar port +Connect to +.Ar port . +The default port is 6697. +.It Fl t Ar path +Trust the self-signed certificate loaded +from +.Ar path +and disable server name verification. +.It Fl u Ar user +Set the username to +.Ar user . +The default username is +.Dq pounce-notify . +.It Fl w Ar pass +Log in with the server password +.Ar pass . +.El +. +.Sh ENVIRONMENT +The following variables +are set in the environment of +.Ar command : +.Pp +.Bl -tag -width "NOTIFY_MESSAGE" -compact +.It Ev NOTIFY_CHANNEL +The name of the channel +in which the message was sent. +Unset if the message was private. +.It Ev NOTIFY_HOST +The hostname of the user who sent the message. +.It Ev NOTIFY_MESSAGE +The content of the message. +.It Ev NOTIFY_NICK +The nickname of the user who sent the message. +.It Ev NOTIFY_TIME +The time the message was sent, +in +.Sy server-time +format. +.It Ev NOTIFY_USER +The username of the user who sent the message. +.It Ev POUNCE_HOST +The host +.Nm +is connected to. +.It Ev POUNCE_PORT +The port +.Nm +is connected to. +.El +. +.Sh SEE ALSO +.Xr pounce 1 +. +.Sh AUTHORS +.An June Bug Aq Mt june@causal.agency +. +.Sh BUGS +Send mail to +.Aq Mt list+pounce@causal.agency +or join +.Li #ascii.town +on +.Li irc.tilde.chat . diff --git a/pounce-palaver.1 b/pounce-palaver.1 new file mode 100644 index 0000000..da3937a --- /dev/null +++ b/pounce-palaver.1 @@ -0,0 +1,112 @@ +.Dd November 28, 2021 +.Dt POUNCE-PALAVER 1 +.Os +. +.Sh NAME +.Nm pounce-palaver +.Nd Palaver push notifications for pounce +. +.Sh SYNOPSIS +.Nm +.Op Fl PNsv +.Op Fl c Ar cert +.Op Fl d Ar path +.Op Fl k Ar priv +.Op Fl p Ar port +.Op Fl t Ar trust +.Op Fl u Ar user +.Op Fl w Ar pass +.Ar host +. +.Sh DESCRIPTION +The +.Nm +daemon provides push notifications +for the Palaver IRC app via the +.Xr pounce 1 +IRC bouncer. +The +.Cm palaver +option must be enabled in +.Xr pounce 1 . +. +.Pp +The arguments are as follows: +.Bl -tag -width Ds +.It Fl N +Never send message previews, +regardless of the app preferences. +.It Fl P +Never send message previews +for private messages. +.It Fl c Ar cert +Load the TLS client certificate from +.Ar path . +If the private key is in a separate file, +it is loaded with +.Fl k . +.It Fl d Ar path +Set the path to the database file +used to store notification preferences. +The default path is documented in +.Sx FILES . +.It Fl k Ar priv +Load the TLS client private key from +.Ar path . +.It Fl p Ar port +Connect to +.Ar port . +The default port is 6697. +.It Fl s +Match nick and keywords case-sensitively, +despite the specification. +.It Fl t Ar path +Trust the self-signed certificate loaded from +.Ar path +and disable server name verification. +.It Fl u Ar user +Set the username to +.Ar user . +The default username is +.Dq pounce-palaver . +.It Fl v +Log IRC protocol, SQL and HTTP to standard error. +.It Fl w Ar pass +Log in with the server password +.Ar pass . +.It Ar host +Connect to +.Ar host . +.El +. +.Sh FILES +.Bl -tag -width Ds +.It Pa $XDG_DATA_DIRS/pounce/palaver.sqlite +The database file is searched for first in +.Ev $XDG_DATA_HOME , +usually +.Pa ~/.local/share , +followed by the colon-separated list of paths +.Ev $XDG_DATA_DIRS , +usually +.Pa /usr/local/share:/usr/share . +.It Pa ~/.local/share/pounce/palaver.sqlite +The most likely default path to the database file. +.El +. +.Sh SEE ALSO +.Xr pounce 1 +. +.Sh STANDARDS +.Lk https://github.com/cocodelabs/palaver-irc-capability "Palaver IRC Capability" +. +.Sh AUTHORS +.An June Bug Aq Mt june@causal.agency +. +.Sh BUGS +Send mail to +.Aq Mt list+pounce@causal.agency +or join +.Li #ascii.town +on +.Li irc.tilde.chat . -- cgit 1.4.1