summary refs log tree commit diff
path: root/litterbox.c
blob: 181227ab3c4c685954ea4df75e56ce0ed8666f94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <tls.h>
#include <unistd.h>

#include "database.h"

static struct {
	sqlite3_stmt *context;
	sqlite3_stmt *event;
	sqlite3_stmt *events;
} insert;

static void prepare(void) {
	const char *CreateJoins = SQL(
		CREATE TEMPORARY TABLE joins (
			nick TEXT NOT NULL,
			channel TEXT NOT NULL,
			UNIQUE (nick, channel)
		);
	);
	dbExec(CreateJoins);

	const char *InsertContext = SQL(
		INSERT OR IGNORE INTO contexts (network, name, query)
		VALUES (:network, :context, :query);
	);
	dbPersist(&insert.context, InsertContext);

	const char *InsertEvent = SQL(
		INSERT INTO events (time, type, context, name, target, message)
		SELECT
			coalesce(datetime(:time), datetime('now')),
			:type, context, names.name, :target, :message
		FROM contexts, names
		WHERE contexts.network = :network
			AND contexts.name = :context
			AND names.nick = :nick
			AND names.user = :user
			AND names.host = :host;
	);
	dbPersist(&insert.event, InsertEvent);

	const char *InsertEvents = SQL(
		INSERT INTO events (time, type, context, name, target, message)
		SELECT
			coalesce(datetime(:time), datetime('now')),
			:type, context, names.name, :target, :message
		FROM joins, contexts, names
		WHERE joins.nick = :nick
			AND contexts.name = joins.channel
			AND contexts.network = :network
			AND names.nick = :nick
			AND names.user = :user
			AND names.host = :host;
	);
	dbPersist(&insert.events, InsertEvents);
}

static void bindNetwork(const char *network) {
	dbBindTextCopy(insert.context, ":network", network);
	dbBindTextCopy(insert.event, ":network", network);
	dbBindTextCopy(insert.events, ":network", network);
}

static void insertContext(const char *context, bool query) {
	dbBindText(insert.context, ":context", context);
	dbBindInt(insert.context, ":query", query);
	dbRun(insert.context);
}

static void insertEvent(
	const char *time, enum Type type, const char *context,
	const char *nick, const char *user, const char *host,
	const char *target, const char *message
) {
	dbBindText(insert.event, ":time", time);
	dbBindInt(insert.event, ":type", type);
	dbBindText(insert.event, ":context", context);
	dbBindText(insert.event, ":nick", nick);
	dbBindText(insert.event, ":user", user);
	dbBindText(insert.event, ":host", host);
	dbBindText(insert.event, ":target", target);
	dbBindText(insert.event, ":message", message);
	dbRun(insert.event);
}

static void insertEvents(
	const char *time, enum Type type,
	const char *nick, const char *user, const char *host,
	const char *target, const char *message
) {
	dbBindText(insert.events, ":time", time);
	dbBindInt(insert.events, ":type", type);
	dbBindText(insert.events, ":nick", nick);
	dbBindText(insert.events, ":user", user);
	dbBindText(insert.events, ":host", host);
	dbBindText(insert.events, ":target", target);
	dbBindText(insert.events, ":message", message);
	dbRun(insert.events);
}

static void insertName(const char *nick, const char *user, const char *host) {
	static sqlite3_stmt *stmt;
	const char *sql = SQL(
		INSERT OR IGNORE INTO names (nick, user, host)
		VALUES (:nick, :user, :host);
	);
	dbPersist(&stmt, sql);
	dbBindText(stmt, ":nick", nick);
	dbBindText(stmt, ":user", user);
	dbBindText(stmt, ":host", host);
	dbRun(stmt);
}

static void insertJoin(const char *nick, const char *channel) {
	static sqlite3_stmt *stmt;
	const char *sql = SQL(
		INSERT OR IGNORE INTO joins (nick, channel) VALUES (:nick, :channel);
	);
	dbPersist(&stmt, sql);
	dbBindText(stmt, ":nick", nick);
	dbBindText(stmt, ":channel", channel);
	dbRun(stmt);
}

static void deleteJoin(const char *nick, const char *channel) {
	static sqlite3_stmt *stmt;
	const char *sql = SQL(
		DELETE FROM joins WHERE nick = :nick AND channel = :channel;
	);
	dbPersist(&stmt, sql);
	dbBindText(stmt, ":nick", nick);
	dbBindText(stmt, ":channel", channel);
	dbRun(stmt);
}

static void updateJoin(const char *old, const char *new) {
	static sqlite3_stmt *stmt;
	const char *sql = SQL(
		UPDATE joins SET nick = :new WHERE nick = :old;
	);
	dbPersist(&stmt, sql);
	dbBindText(stmt, ":old", old);
	dbBindText(stmt, ":new", new);
	dbRun(stmt);
}

static void clearJoins(const char *nick, const char *channel) {
	static sqlite3_stmt *stmt;
	const char *sql = SQL(
		DELETE FROM joins WHERE nick = :nick OR channel = :channel;
	);
	dbPersist(&stmt, sql);
	dbBindText(stmt, ":nick", nick);
	dbBindText(stmt, ":channel", channel);
	dbRun(stmt);
}

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) printf("%s", buf);
	clientWrite(buf, len);
}

enum { ParamCap = 15 };
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;
		if (!msg.user) msg.user = msg.nick;
		if (!msg.host) msg.host = msg.user;
	}
	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);
	}
}

static const char *join;

static char *self;
static char *chanTypes;
static char *prefixes;

static void set(char **field, const char *value) {
	free(*field);
	*field = strdup(value);
	if (!*field) err(EX_OSERR, "strdup");
}

typedef void Handler(struct Message *msg);

static void handleCap(struct Message *msg) {
	(void)msg;
	format("CAP END\r\n");
}

static void handleReplyWelcome(struct Message *msg) {
	require(msg, false, 1);
	set(&self, msg->params[0]);
	if (join) format("JOIN :%s\r\n", join);
}

static void handleReplyISupport(struct Message *msg) {
	for (size_t i = 0; i < ParamCap; ++i) {
		if (!msg->params[i]) break;
		char *key = strsep(&msg->params[i], "=");
		if (!msg->params[i]) continue;
		if (!strcmp(key, "NETWORK")) {
			bindNetwork(msg->params[i]);
		} else if (!strcmp(key, "CHANTYPES")) {
			set(&chanTypes, msg->params[i]);
		} else if (!strcmp(key, "PREFIX")) {
			strsep(&msg->params[i], ")");
			if (!msg->params[i]) continue;
			set(&prefixes, msg->params[i]);
		}
	}
}

static void handlePrivmsg(struct Message *msg) {
	require(msg, true, 2);

	bool query = true;
	const char *context = msg->params[0];
	if (strchr(chanTypes, context[0])) query = false;
	if (!strcmp(context, self)) context = msg->nick;

	enum Type type = (!strcmp(msg->cmd, "NOTICE") ? Notice : Privmsg);
	char *message = msg->params[1];
	if (!strncmp(message, "\1ACTION ", 8)) {
		message += 8;
		message[strcspn(message, "\1")] = '\0';
		type = Action;
	}

	insertContext(context, query);
	insertName(msg->nick, msg->user, msg->host);
	insertEvent(
		msg->time, type, context,
		msg->nick, msg->user, msg->host, NULL, message
	);
}

static void handleReplyNames(struct Message *msg) {
	require(msg, false, 3);
	char *names = msg->params[3];
	while (names) {
		char *nick = strsep(&names, " ");
		nick += strspn(nick, prefixes);
		insertJoin(nick, msg->params[2]);
	}
}

static void handleJoin(struct Message *msg) {
	require(msg, true, 1);
	insertContext(msg->params[0], false);
	insertName(msg->nick, msg->user, msg->host);
	insertEvent(
		msg->time, Join, msg->params[0],
		msg->nick, msg->user, msg->host, NULL, NULL
	);
	insertJoin(msg->nick, msg->params[0]);
}

static void handlePart(struct Message *msg) {
	require(msg, true, 1);
	insertContext(msg->params[0], false);
	insertName(msg->nick, msg->user, msg->host);
	insertEvent(
		msg->time, Part, msg->params[0],
		msg->nick, msg->user, msg->host, NULL, msg->params[1]
	);
	if (!strcmp(msg->nick, self)) {
		clearJoins(NULL, msg->params[0]);
	} else {
		deleteJoin(msg->nick, msg->params[0]);
	}
}

static void handleKick(struct Message *msg) {
	require(msg, true, 2);
	insertContext(msg->params[0], false);
	insertName(msg->nick, msg->user, msg->host);
	insertEvent(
		msg->time, Kick, msg->params[0],
		msg->nick, msg->user, msg->host,
		msg->params[1], msg->params[2]
	);
	if (!strcmp(msg->params[1], self)) {
		clearJoins(NULL, msg->params[0]);
	} else {
		deleteJoin(msg->params[1], msg->params[0]);
	}
}

static void handleNick(struct Message *msg) {
	require(msg, true, 1);
	if (!strcmp(msg->nick, self)) set(&self, msg->params[0]);
	insertName(msg->nick, msg->user, msg->host);
	insertEvents(
		msg->time, Nick,
		msg->nick, msg->user, msg->host, msg->params[0], NULL
	);
	updateJoin(msg->nick, msg->params[0]);
}

static void handleQuit(struct Message *msg) {
	require(msg, true, 0);
	insertName(msg->nick, msg->user, msg->host);
	insertEvents(
		msg->time, Quit,
		msg->nick, msg->user, msg->host, NULL, msg->params[0]
	);
	clearJoins(msg->nick, NULL);
}

static void handleTopic(struct Message *msg) {
	require(msg, true, 1);
	insertContext(msg->params[0], false);
	insertName(msg->nick, msg->user, msg->host);
	insertEvent(
		msg->time, Topic, msg->params[0],
		msg->nick, msg->user, msg->host, NULL, msg->params[1]
	);
}

static void handlePing(struct Message *msg) {
	require(msg, false, 1);
	format("PONG :%s\r\n", msg->params[0]);
}

static const struct {
	const char *cmd;
	bool transaction;
	Handler *fn;
} Handlers[] = {
	{ "001", false, handleReplyWelcome },
	{ "005", false, handleReplyISupport },
	{ "353", true, handleReplyNames },
	{ "CAP", false, handleCap },
	{ "JOIN", true, handleJoin },
	{ "KICK", true, handleKick },
	{ "NICK", true, handleNick },
	{ "NOTICE", true, handlePrivmsg },
	{ "PART", true, handlePart },
	{ "PING", false, handlePing },
	{ "PRIVMSG", true, handlePrivmsg },
	{ "QUIT", true, handleQuit },
	{ "TOPIC", true, handleTopic },
};

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;
		if (Handlers[i].transaction) {
			dbExec(SQL(BEGIN TRANSACTION;));
			Handlers[i].fn(&msg);
			dbExec(SQL(COMMIT TRANSACTION;));
		} else {
			Handlers[i].fn(&msg);
		}
		break;
	}
}

static void quit(int sig) {
	(void)sig;
	format("QUIT\r\n");
	tls_close(client);
	dbClose();
	_exit(EX_OK);
}

int main(int argc, char *argv[]) {
	char *path = NULL;
	bool init = false;
	bool migrate = false;

	bool insecure = false;
	const char *host = NULL;
	const char *port = "6697";

	const char *nick = "litterbox";
	const char *user = NULL;
	const char *pass = NULL;

	int opt;
	while (0 < (opt = getopt(argc, argv, "!d:h:ij:mn:p:u:vw:"))) {
		switch (opt) {
			break; case '!': insecure = true;
			break; case 'd': path = optarg;
			break; case 'h': host = optarg;
			break; case 'i': init = true;
			break; case 'j': join = optarg;
			break; case 'm': migrate = true;
			break; case 'n': nick = optarg;
			break; case 'p': port = optarg;
			break; case 'u': user = optarg;
			break; case 'v': verbose = true;
			break; case 'w': pass = optarg;
			break; default:  return EX_USAGE;
		}
	}
	if (!user) user = nick;

	int flags = SQLITE_OPEN_READWRITE;
	if (init) flags |= SQLITE_OPEN_CREATE;
	dbFind(path, flags);

	if (init) {
		dbInit();
		return EX_OK;
	}
	if (migrate) {
		dbMigrate();
		return EX_OK;
	}
	if (dbVersion() != DatabaseVersion) {
		errx(EX_CONFIG, "database out of date; migrate with -m");
	}

	if (!host) errx(EX_USAGE, "host required");
	set(&self, "*");
	set(&chanTypes, "#&");
	set(&prefixes, "@+");

	prepare();
	bindNetwork(host);

	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");

	int error = tls_config_set_ciphers(config, "compat");
	if (error) {
		errx(EX_SOFTWARE, "tls_config_set_ciphers: %s", tls_config_error(config));
	}
	if (insecure) {
		tls_config_insecure_noverifycert(config);
		tls_config_insecure_noverifyname(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\r\n");
	format("NICK :%s\r\nUSER %s 0 * :Litterbox\r\n", nick, 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';
			handle(parse(line));
			line = crlf + 2;
		}
		len -= line - buf;
		memmove(buf, line, len);
	}
}