summary refs log tree commit diff
path: root/client.c
blob: 68daff6fcbcafbf4d2ac75783990759b6f07523e (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
/* 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 Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include "bounce.h"

enum Need {
	NeedNick = 1 << 0,
	NeedUser = 1 << 1,
	NeedPass = 1 << 2,
	NeedCapEnd = 1 << 3,
};

struct Client {
	bool error;
	struct tls *tls;
	enum Need need;
	bool serverTime;
	char buf[4096];
	size_t len;
};

struct Client *clientAlloc(struct tls *tls) {
	struct Client *client = malloc(sizeof(*client));
	if (!client) err(EX_OSERR, "malloc");

	client->error = false;
	client->tls = tls;
	client->need = NeedNick | NeedUser | (clientPass ? NeedPass : 0);
	client->serverTime = false;
	client->len = 0;

	return client;
}

void clientFree(struct Client *client) {
	tls_close(client->tls);
	tls_free(client->tls);
	free(client);
}

bool clientError(const struct Client *client) {
	return client->error;
}

void clientSend(struct Client *client, const char *ptr, size_t len) {
	if (verbose) fprintf(stderr, "\x1B[34m%.*s\x1B[m", (int)len, ptr);
	while (len) {
		ssize_t ret = tls_write(client->tls, ptr, len);
		// FIXME: Handle non-blocking?
		if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue;
		if (ret < 0) {
			warnx("tls_write: %s", tls_error(client->tls));
			client->error = true;
			return;
		}
		ptr += ret;
		len -= ret;
	}
}

void clientFormat(struct Client *client, const char *format, ...) {
	char buf[513];
	va_list ap;
	va_start(ap, format);
	int len = vsnprintf(buf, sizeof(buf), format, ap);
	va_end(ap);
	assert(len > 0 && (size_t)len < sizeof(buf));
	clientSend(client, buf, len);
}

static void passRequired(struct Client *client) {
	clientFormat(
		client,
		":invalid 464 * :Password incorrect\r\n"
		"ERROR :Password incorrect\r\n"
	);
	client->error = true;
}

typedef void Handler(struct Client *client, struct Message msg);

static void handleNick(struct Client *client, struct Message msg) {
	(void)msg;
	client->need &= ~NeedNick;
	if (!client->need) stateSync(client);
	if (client->need == NeedPass) passRequired(client);
}

static void handleUser(struct Client *client, struct Message msg) {
	(void)msg;
	// TODO: Identify client by username.
	client->need &= ~NeedUser;
	if (!client->need) stateSync(client);
	if (client->need == NeedPass) passRequired(client);
}

static void handlePass(struct Client *client, struct Message msg) {
	if (!msg.params[0] || strcmp(clientPass, msg.params[0])) {
		passRequired(client);
	} else {
		client->need &= ~NeedPass;
		if (!client->need) stateSync(client);
	}
}

static void handleCap(struct Client *client, struct Message msg) {
	if (!msg.params[0]) msg.params[0] = "";

	if (!strcmp(msg.params[0], "END")) {
		if (client->need & NeedCapEnd) {
			client->need &= ~NeedCapEnd;
			if (!client->need) stateSync(client);
		}

	} else if (!strcmp(msg.params[0], "LS")) {
		client->need |= NeedCapEnd;
		clientFormat(client, ":invalid CAP * LS :server-time\r\n");

	} else if (!strcmp(msg.params[0], "REQ") && msg.params[1]) {
		client->need |= NeedCapEnd;
		if (!strcmp(msg.params[1], "server-time")) {
			client->serverTime = true;
			clientFormat(client, ":invalid CAP * ACK :server-time\r\n");
		} else {
			clientFormat(client, ":invalid CAP * NAK :%s\r\n", msg.params[1]);
		}

	} else if (!strcmp(msg.params[0], "LIST")) {
		clientFormat(
			client, ":invalid CAP * LIST :%s\r\n",
			(client->serverTime ? "server-time" : "")
		);

	} else {
		clientFormat(client, ":invalid 410 * :Invalid CAP subcommand\r\n");
	}
}

static const struct {
	const char *cmd;
	Handler *fn;
} Handlers[] = {
	{ "CAP", handleCap },
	{ "NICK", handleNick },
	{ "PASS", handlePass },
	{ "USER", handleUser },
};

static void clientParse(struct Client *client, char *line) {
	struct Message msg = parse(line);
	if (!msg.cmd) {
		// FIXME: Identify client in message.
		warnx("no command");
		client->error = true;
		return;
	}
	for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) {
		if (strcmp(msg.cmd, Handlers[i].cmd)) continue;
		Handlers[i].fn(client, msg);
		break;
	}
}

static bool intercept(const char *line, size_t len) {
	if (len >= 4 && !memcmp(line, "CAP ", 4)) return true;
	// TODO: Intercept PRIVMSG to send to other clients.
	return false;
}

void clientRecv(struct Client *client) {
	ssize_t read = tls_read(
		client->tls,
		&client->buf[client->len], sizeof(client->buf) - client->len
	);
	if (read == TLS_WANT_POLLIN || read == TLS_WANT_POLLOUT) return;
	if (read < 0) warnx("tls_read: %s", tls_error(client->tls));
	if (read < 1) {
		client->error = true;
		return;
	}
	client->len += read;

	char *crlf;
	char *line = client->buf;
	for (;;) {
		crlf = memmem(line, &client->buf[client->len] - line, "\r\n", 2);
		if (!crlf) break;
		if (verbose) {
			fprintf(stderr, "\x1B[33m%.*s\x1B[m\n", (int)(crlf - line), line);
		}
		if (client->need || intercept(line, crlf - line)) {
			crlf[0] = '\0';
			clientParse(client, line);
		} else {
			serverSend(line, crlf + 2 - line);
		}
		line = crlf + 2;
	}
	client->len -= line - client->buf;
	memmove(client->buf, line, client->len);
}