~rcr/rirc

ref: 9f6f56529796701b2b87d4f66f8942c3394aec76 rirc/src/components/server.c -rw-r--r-- 8.6 KiB
9f6f5652 — Richard Robbins Merge branch 'dev' into static_analysis 3 years ago
                                                                                
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
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "src/components/server.h"
#include "src/state.h"
#include "src/utils/utils.h"

#define HANDLED_005 \
	X(CASEMAPPING)  \
	X(CHANMODES)    \
	X(MODES)        \
	X(PREFIX)

struct opt
{
	char *arg;
	char *val;
};

static int parse_opt(struct opt*, char**);
static int server_cmp(const struct server*, const char*, const char*);

#define X(cmd) static int server_set_##cmd(struct server*, char*);
HANDLED_005
#undef X

struct server*
server(const char *host, const char *port, const char *pass, const char *user, const char *real)
{
	struct server *s;

	if ((s = calloc(1, sizeof(*s))) == NULL)
		fatal("calloc: %s", strerror(errno));

	s->host = strdup(host);
	s->port = strdup(port);
	s->pass = pass ? strdup(pass) : NULL;
	s->username = strdup(user);
	s->realname = strdup(real);

	s->mode_str.type = MODE_STR_USERMODE;

	mode_cfg(&(s->mode_cfg), NULL, MODE_CFG_DEFAULTS);

	// FIXME: channel()
	s->channel = new_channel(host, s, CHANNEL_T_SERVER);

	// move this to the state_new_server
	channel_set_current(s->channel);

	if ((s->connection = connection(s, host, port)) == NULL) {
		server_free(s);
		return NULL;
	}

	return s;
}

struct server*
server_list_get(struct server_list *sl, const char *host, const char *port)
{
	struct server *tmp;

	if ((tmp = sl->head) == NULL)
		return NULL;

	if (!server_cmp(sl->head, host, port))
		return sl->head;

	while ((tmp = tmp->next) != sl->head) {

		if (!server_cmp(tmp, host, port))
			return tmp;
	}

	return NULL;
}

struct server*
server_list_add(struct server_list *sl, struct server *s)
{
	struct server *tmp;

	if ((tmp = server_list_get(sl, s->host, s->port)) != NULL)
		return s;

	if (sl->head == NULL) {
		sl->head = s->next = s;
		sl->tail = s->prev = s;
	} else {
		s->next = sl->tail->next;
		s->prev = sl->tail;
		sl->head->prev = s;
		sl->tail->next = s;
		sl->tail = s;
	}

	return NULL;
}

struct server*
server_list_del(struct server_list *sl, struct server *s)
{
	struct server *tmp_h,
	              *tmp_t;

	if (sl->head == s && sl->tail == s) {
		/* Removing last server */
		sl->head = NULL;
		sl->tail = NULL;
	} else if ((tmp_h = sl->head) == s) {
		/* Removing head */
		sl->head = sl->tail->next = sl->head->next;
		sl->head->prev = sl->tail;
	} else if ((tmp_t = sl->tail) == s) {
		/* Removing tail */
		sl->tail = sl->head->prev = sl->tail->prev;
		sl->tail->next = sl->head;
	} else {
		/* Removing some server (head, tail) */
		while ((tmp_h = tmp_h->next) != s) {
			if (tmp_h == tmp_t)
				return NULL;
		}
		s->next->prev = s->prev;
		s->prev->next = s->next;
	}

	s->next = NULL;
	s->prev = NULL;

	return s;
}

void
server_reset(struct server *s)
{
	mode_reset(&(s->usermodes), &(s->mode_str));
	s->ping = 0;
	s->quitting = 0;
	s->nicks.next = 0;
}

void
server_free(struct server *s)
{
	free((void *)s->host);
	free((void *)s->port);
	free((void *)s->pass);
	free((void *)s->username);
	free((void *)s->realname);
	free((void *)s->nick);
	free((void *)s->nicks.base);
	free((void *)s->nicks.set);
	free(s);
}

void
server_set_004(struct server *s, char *str)
{
	/* <server_name> <version> <user_modes> <chan_modes> */

	struct channel *c = s->channel;

	char *server_name, /* Not used */
	     *version,     /* Not used */
	     *user_modes,  /* Configure server usermodes */
	     *chan_modes;  /* Configure server chanmodes */

	if (!(server_name = getarg(&str, " ")))
		newline(c, 0, "-!!-", "invalid numeric 004: server_name is null");

	if (!(version = getarg(&str, " ")))
		newline(c, 0, "-!!-", "invalid numeric 004: version is null");

	if (!(user_modes = getarg(&str, " ")))
		newline(c, 0, "-!!-", "invalid numeric 004: user_modes is null");

	if (!(chan_modes = getarg(&str, " ")))
		newline(c, 0, "-!!-", "invalid numeric 004: chan_modes is null");

	if (user_modes) {

		DEBUG_MSG("Setting numeric 004 user_modes: %s", user_modes);

		if (mode_cfg(&(s->mode_cfg), user_modes, MODE_CFG_USERMODES) != MODE_ERR_NONE)
			newlinef(c, 0, "-!!-", "invalid numeric 004 user_modes: %s", user_modes);
	}

	if (chan_modes) {

		DEBUG_MSG("Setting numeric 004 chan_modes: %s", chan_modes);

		if (mode_cfg(&(s->mode_cfg), chan_modes, MODE_CFG_CHANMODES) != MODE_ERR_NONE)
			newlinef(c, 0, "-!!-", "invalid numeric 004 chan_modes: %s", chan_modes);
	}
}

void
server_set_005(struct server *s, char *str)
{
	/* Iterate over options parsed from str and set for server s */

	struct opt opt;

	while (parse_opt(&opt, &str)) {
		#define X(cmd) \
		if (!strcmp(opt.arg, #cmd) && server_set_##cmd(s, opt.val)) \
			newlinef(s->channel, 0, "-!!-", "invalid %s: %s", #cmd, opt.val);
		HANDLED_005
		#undef X
	}
}

int
server_set_nicks(struct server *s, const char *nicks)
{
	char *p1, *p2, *base;
	size_t n = 0;

	p2 = base = strdup(nicks);

	do {
		n++;

		p1 = p2;
		p2 = strchr(p2, ',');

		if (p2)
			*p2++ = 0;

		if (!irc_isnick(p1)) {
			free(base);
			return -1;
		}
	} while (p2);

	free((void *)s->nicks.base);
	free((void *)s->nicks.set);

	s->nicks.next = 0;
	s->nicks.size = n;
	s->nicks.base = base;

	if ((s->nicks.set = malloc(sizeof(*s->nicks.set) * n)) == NULL)
		fatal("malloc: %s", strerror(errno));

	for (const char **set = s->nicks.set; n; n--, set++) {
		*set = base;
		base = strchr(base, 0) + 1;
	}

	return 0;
}

static int
server_cmp(const struct server *s, const char *host, const char *port)
{
	int cmp;

	if ((cmp = strcmp(s->host, host)))
		return cmp;

	if ((cmp = strcmp(s->port, port)))
		return cmp;

	return 0;
}

static int
parse_opt(struct opt *opt, char **str)
{
	/* Parse a single argument from numeric 005 (ISUPPORT)
	 *
	 * docs/ISUPPORT.txt, section 2
	 *
	 * ":" servername SP "005" SP nickname SP 1*13( token SP ) ":are supported by this server"
	 *
	 * token     =  *1"-" parameter / parameter *1( "=" value )
	 * parameter =  1*20letter
	 * value     =  *letpun
	 * letter    =  ALPHA / DIGIT
	 * punct     =  %d33-47 / %d58-64 / %d91-96 / %d123-126
	 * letpun    =  letter / punct
	 */

	/* FIXME: (see docs)
	 *
	 * '-PARAMETER' is valid and negates a previously set parameter to its default
	 * 'PARAMETER', 'PARAMTER=' are equivalent
	 *
	 * The parameter's value may contain sequences of the form "\xHH", where
	 * HH is a two-digit hexadecimal number.  Each such sequence is
	 * considered identical to the equivalent octet after parsing of the
	 * reply into separate tokens has occurred.
	 *
	 * [Example: X=A\x20B defines one token, "X", with the value "A B",
	 * rather than two tokens "X" and "B".]
	 * [Note: The literal string "\x" must therefore be encoded as
	 * "\x5Cx".]
	 *
	 * If the server has not advertised a CHARSET parameter, it MUST not use
	 * such sequences with a value outside those permitted by the above ABNF
	 * grammar, with the exception of "\x20";  if it has advertised CHARSET,
	 * then it may in addition send any printable character defined in that
	 * encoding. Characters in multibyte encodings such as UTF-8 should be
	 * sent as a series of \x sequences.
	 */

	char *t, *p = *str;

	opt->arg = NULL;
	opt->val = NULL;

	if (!skip_sp(&p))
		return 0;

	if (!isalnum(*p))
		return 0;

	opt->arg = p;

	if ((t = strchr(p, ' '))) {
		*t++ = 0;
		*str = t;
	} else {
		*str = strchr(p, 0);
	}

	if ((p = strchr(opt->arg, '='))) {
		*p++ = 0;

		if (*p)
			opt->val = p;
	}

	return 1;
}

static int
server_set_CASEMAPPING(struct server *s, char *val)
{
	/* TODO: sets a function pointer to be used for
	 * nick/chan string cmps specific to this server */
	(void)(s);
	(void)(val);
	return 0;
}

static int
server_set_CHANMODES(struct server *s, char *val)
{
	DEBUG_MSG("Setting numeric 005 CHANMODES: %s", val);

	return (mode_cfg(&(s->mode_cfg), val, MODE_CFG_SUBTYPES) != MODE_ERR_NONE);
}

static int
server_set_MODES(struct server *s, char *val)
{
	DEBUG_MSG("Setting numeric 005 MODES: %s", val);

	return (mode_cfg(&(s->mode_cfg), val, MODE_CFG_MODES) != MODE_ERR_NONE);
}

static int
server_set_PREFIX(struct server *s, char *val)
{
	DEBUG_MSG("Setting numeric 005 PREFIX: %s", val);

	return (mode_cfg(&(s->mode_cfg), val, MODE_CFG_PREFIX) != MODE_ERR_NONE);
}

void
server_nick_set(struct server *s, const char *nick)
{
	DEBUG_MSG("Setting server nick: %s", nick);

	if (s->nick)
		free((void *)s->nick);

	s->nick = strdup(nick);
}

void
server_nicks_next(struct server *s)
{
	if (s->nicks.size && s->nicks.next < s->nicks.size) {
		server_nick_set(s, s->nicks.set[s->nicks.next++]);
	} else {
		/* Default to random nick, length 9 (RFC2912, section 1.2.1) */

		char nick_cset[] = "0123456789ABCDEF";
		char nick_rand[] = "rirc*****";

		for (char *p = strchr(nick_rand, '*'); p && *p; p++) {
			/* coverity[dont_call] Acceptable use of insecure rand() function */
			*p = nick_cset[rand() % strlen(nick_cset)];
		}

		server_nick_set(s, nick_rand);
	}
}