summary refs log tree commit diff
path: root/state.c
blob: 01829a4e938be95e8d37f02b699886cb6123486c (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
/* 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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#include "bounce.h"

static struct {
	char *origin;
	char *welcome;
	char *yourHost;
	char *created;
	char *myInfo[4];
} intro;

static struct {
	char *nick;
	char *origin;
} self;

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

struct Channel {
	char *name;
	char *topic;
};

static struct {
	struct Channel *ptr;
	size_t cap, len;
} chans;

static void chanAdd(const char *name) {
	if (chans.len == chans.cap) {
		chans.cap = (chans.cap ? chans.cap * 2 : 8);
		chans.ptr = realloc(chans.ptr, sizeof(*chans.ptr) * chans.cap);
		if (!chans.ptr) err(EX_OSERR, "realloc");
	}
	struct Channel *chan = &chans.ptr[chans.len++];
	chan->name = strdup(name);
	if (!chan->name) err(EX_OSERR, "strdup");
	chan->topic = NULL;
}

static void chanTopic(const char *name, const char *topic) {
	for (size_t i = 0; i < chans.len; ++i) {
		if (strcmp(chans.ptr[i].name, name)) continue;
		free(chans.ptr[i].topic);
		chans.ptr[i].topic = strdup(topic);
		if (!chans.ptr[i].topic) err(EX_OSERR, "strdup");
		break;
	}
}

static void chanRemove(const char *name) {
	for (size_t i = 0; i < chans.len; ++i) {
		if (strcmp(chans.ptr[i].name, name)) continue;
		free(chans.ptr[i].name);
		free(chans.ptr[i].topic);
		chans.ptr[i] = chans.ptr[--chans.len];
		break;
	}
}

static struct {
	char **tokens;
	size_t cap, len;
} support;

static void supportAdd(const char *token) {
	if (support.len == support.cap) {
		support.cap = (support.cap ? support.cap * 2 : 8);
		support.tokens = realloc(support.tokens, sizeof(char *) * support.cap);
		if (!support.tokens) err(EX_OSERR, "realloc");
	}
	support.tokens[support.len] = strdup(token);
	if (!support.tokens[support.len]) err(EX_OSERR, "strdup");
	support.len++;
}

bool stateReady(void) {
	return self.nick
		&& intro.origin
		&& intro.welcome
		&& intro.yourHost
		&& intro.created
		&& intro.myInfo[0];
}

const char *stateSelf(void) {
	if (self.origin) return self.origin;
	if (self.nick) return self.nick;
	return "*";
}

typedef void Handler(struct Message *msg);

static void handleCap(struct Message *msg) {
	bool ack = msg->params[1] && !strcmp(msg->params[1], "ACK");
	bool sasl = msg->params[2] && !strncmp(msg->params[2], "sasl", 4);
	if (!ack || !sasl) errx(EX_CONFIG, "server does not support SASL");
	serverFormat("AUTHENTICATE PLAIN\r\n");
}

static void handleAuthenticate(struct Message *msg) {
	(void)msg;
	serverAuth();
}

static void handleReplyWelcome(struct Message *msg) {
	if (!msg->params[1]) errx(EX_PROTOCOL, "RPL_WELCOME without message");
	set(&intro.origin, msg->origin);
	set(&self.nick, msg->params[0]);
	set(&intro.welcome, msg->params[1]);
}

static void handleReplyYourHost(struct Message *msg) {
	if (!msg->params[1]) errx(EX_PROTOCOL, "RPL_YOURHOST without message");
	set(&intro.yourHost, msg->params[1]);
}

static void handleReplyCreated(struct Message *msg) {
	if (!msg->params[1]) errx(EX_PROTOCOL, "RPL_CREATED without message");
	set(&intro.created, msg->params[1]);
}

static void handleReplyMyInfo(struct Message *msg) {
	if (!msg->params[4]) errx(EX_PROTOCOL, "RPL_MYINFO without 4 parameters");
	set(&intro.myInfo[0], msg->params[1]);
	set(&intro.myInfo[1], msg->params[2]);
	set(&intro.myInfo[2], msg->params[3]);
	set(&intro.myInfo[3], msg->params[4]);
}

static void handleReplyISupport(struct Message *msg) {
	for (size_t i = 1; i < ParamCap; ++i) {
		if (!msg->params[i] || strchr(msg->params[i], ' ')) break;
		supportAdd(msg->params[i]);
	}
}

static void handleErrorNicknameInUse(struct Message *msg) {
	if (self.nick) return;
	if (!msg->params[1]) errx(EX_PROTOCOL, "ERR_NICKNAMEINUSE without nick");
	serverFormat("NICK %s_\r\n", msg->params[1]);
}

static bool fromSelf(const struct Message *msg) {
	if (!self.nick) return false;
	size_t len = strlen(self.nick);
	if (strlen(msg->origin) < len) return false;
	if (strncmp(msg->origin, self.nick, len)) return false;
	if (msg->origin[len] != '!') return false;
	if (!self.origin || strcmp(self.origin, msg->origin)) {
		set(&self.origin, msg->origin);
	}
	return true;
}

static void handleNick(struct Message *msg) {
	if (!msg->origin) errx(EX_PROTOCOL, "NICK without origin");
	if (!msg->params[0]) errx(EX_PROTOCOL, "NICK without nick");
	if (fromSelf(msg)) set(&self.nick, msg->params[0]);
}

static void handleJoin(struct Message *msg) {
	if (!msg->origin) errx(EX_PROTOCOL, "JOIN without origin");
	if (!msg->params[0]) errx(EX_PROTOCOL, "JOIN without channel");
	if (fromSelf(msg)) chanAdd(msg->params[0]);
}

static void handlePart(struct Message *msg) {
	if (!msg->origin) errx(EX_PROTOCOL, "PART without origin");
	if (!msg->params[0]) errx(EX_PROTOCOL, "PART without channel");
	if (fromSelf(msg)) chanRemove(msg->params[0]);
}

static void handleKick(struct Message *msg) {
	if (!msg->params[0]) errx(EX_PROTOCOL, "KICK without channel");
	if (!msg->params[1]) errx(EX_PROTOCOL, "KICK without nick");
	if (self.nick && !strcmp(msg->params[1], self.nick)) {
		chanRemove(msg->params[0]);
	}
}

static void handleTopic(struct Message *msg) {
	if (!msg->params[0]) errx(EX_PROTOCOL, "TOPIC without channel");
	if (!msg->params[1]) errx(EX_PROTOCOL, "TOPIC without topic");
	chanTopic(msg->params[0], msg->params[1]);
}

static void handleReplyTopic(struct Message *msg) {
	if (!msg->params[1]) errx(EX_PROTOCOL, "RPL_TOPIC without channel");
	if (!msg->params[2]) errx(EX_PROTOCOL, "RPL_TOPIC without topic");
	chanTopic(msg->params[1], msg->params[2]);
}

static void handleError(struct Message *msg) {
	errx(EX_UNAVAILABLE, "%s", msg->params[0]);
}

static const struct {
	const char *cmd;
	Handler *fn;
} Handlers[] = {
	{ "001", handleReplyWelcome },
	{ "002", handleReplyYourHost },
	{ "003", handleReplyCreated },
	{ "004", handleReplyMyInfo },
	{ "005", handleReplyISupport },
	{ "332", handleReplyTopic },
	{ "433", handleErrorNicknameInUse },
	{ "AUTHENTICATE", handleAuthenticate },
	{ "CAP", handleCap },
	{ "ERROR", handleError },
	{ "JOIN", handleJoin },
	{ "KICK", handleKick },
	{ "NICK", handleNick },
	{ "PART", handlePart },
	{ "TOPIC", handleTopic },
};

void stateParse(char *line) {
	struct Message msg = parse(line);
	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;
	}
}

void stateSync(struct Client *client) {
	assert(stateReady());

	clientFormat(
		client,
		":%s NOTICE %s :"
		"pounce is AGPLv3 fwee softwawe ^w^  code is avaiwable fwom %s\r\n",
		Origin, self.nick, SourceURL
	);

	clientFormat(
		client,
		":%s 001 %s :%s\r\n"
		":%s 002 %s :%s\r\n"
		":%s 003 %s :%s\r\n",
		intro.origin, self.nick, intro.welcome,
		intro.origin, self.nick, intro.yourHost,
		intro.origin, self.nick, intro.created
	);
	clientFormat(
		client, ":%s 004 %s %s %s %s %s\r\n",
		intro.origin, self.nick,
		intro.myInfo[0], intro.myInfo[1], intro.myInfo[2], intro.myInfo[3]
	);

	size_t i;
	for (i = 0; support.len - i >= 13; i += 13) {
		clientFormat(
			client,
			":%s 005 %s"
			" %s %s %s %s %s %s %s %s %s %s %s %s %s"
			" :are supported by this server\r\n",
			intro.origin, self.nick,
			support.tokens[i + 0], support.tokens[i + 1],
			support.tokens[i + 2], support.tokens[i + 3],
			support.tokens[i + 4], support.tokens[i + 5],
			support.tokens[i + 6], support.tokens[i + 7],
			support.tokens[i + 8], support.tokens[i + 9],
			support.tokens[i + 10], support.tokens[i + 11],
			support.tokens[i + 12]
		);
	}
	if (i < support.len) {
		clientFormat(client, ":%s 005 %s", intro.origin, self.nick);
		for (; i < support.len; ++i) {
			clientFormat(client, " %s", support.tokens[i]);
		}
		clientFormat(client, " :are supported by this server\r\n");
	}

	if (chans.len) assert(self.origin);
	for (size_t i = 0; i < chans.len; ++i) {
		const struct Channel *chan = &chans.ptr[i];
		clientFormat(client, ":%s JOIN %s\r\n", self.origin, chan->name);
		if (chan->topic) {
			clientFormat(
				client, ":%s 332 %s %s :%s\r\n",
				Origin, self.nick, chan->name, chan->topic
			);
		}
		if (stateJoinNames) serverFormat("NAMES %s\r\n", chan->name);
	}
}