~rcr/rirc

ref: c677d85e88b1397d0eaa9b99ad3acf531f64aa3f rirc/src/utils/utils.c -rw-r--r-- 8.1 KiB
c677d85e — Richard Robbins add -m/--mode flag 6 months 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
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
#include "src/utils/utils.h"

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

static inline int irc_ischanchar(char, int);
static inline int irc_isnickchar(char, int);
static inline int irc_toupper(enum casemapping, int);

int
irc_isnick(const char *str)
{
	if (!irc_isnickchar(*str++, 1))
		return 0;

	while (*str) {
		if (!irc_isnickchar(*str++, 0))
			return 0;
	}

	return 1;
}

int
irc_ischan(const char *str)
{
	if (!irc_ischanchar(*str++, 1))
		return 0;

	while (*str) {
		if (!irc_ischanchar(*str++, 0))
			return 0;
	}

	return 1;
}

int
irc_pinged(enum casemapping cm, const char *mesg, const char *nick)
{
	size_t len = strlen(nick);

	while (*mesg) {

		while (*mesg && *mesg != *nick && !irc_isnickchar(*mesg, 1))
			mesg++;

		if (!irc_strncmp(cm, mesg, nick, len) && !irc_isnickchar(*(mesg + len), 0))
			return 1;

		while (*mesg && *mesg != ' ')
			mesg++;
	}

	return 0;
}

int
irc_strcmp(enum casemapping cm, const char *s1, const char *s2)
{
	/* Case insensitive comparison of strings s1, s2 in accordance
	 * with RFC 2812, section 2.2 */

	int c1, c2;

	for (;;) {

		c1 = irc_toupper(cm, *s1++);
		c2 = irc_toupper(cm, *s2++);

		if ((c1 -= c2))
			return -c1;

		if (c2 == 0)
			break;
	}

	return 0;
}

int
irc_strncmp(enum casemapping cm, const char *s1, const char *s2, size_t n)
{
	/* Case insensitive comparison of strings s1, s2 in accordance
	 * with RFC 2812, section 2.2, up to n characters */

	int c1, c2;

	while (n > 0) {

		c1 = irc_toupper(cm, *s1++);
		c2 = irc_toupper(cm, *s2++);

		if ((c1 -= c2))
			return -c1;

		if (c2 == 0)
			break;

		n--;
	}

	return 0;
}

// TODO: reverse return order
// 0 success, -1 error
int
irc_message_param(struct irc_message *m, char **param)
{
	*param = NULL;

	if (m->params == NULL)
		return 0;

	if (!irc_strtrim(&m->params))
		return 0;

	if (!m->split && m->n_params >= 14) {
		*param = m->params;
		m->params = NULL;
		return 1;
	}

	if (*m->params == ':') {
		*param = m->params + 1;
		m->params = NULL;
		return 1;
	}

	m->n_params++;

	*param = m->params;

	while (*m->params && *m->params != ' ')
		m->params++;

	if (*m->params)
		*m->params++ = 0;

	return 1;
}

int
irc_message_parse(struct irc_message *m, char *buf)
{
	/* RFC 2812, section 2.3.1
	 *
	 * message    =   [ ":" prefix SPACE ] command [ params ] crlf
	 * prefix     =   servername / ( nickname [ [ "!" user ] "@" host ] )
	 * command    =   1*letter / 3digit
	 * params     =   *14( SPACE middle ) [ SPACE ":" trailing ]
	 *            =/  14( SPACE middle ) [ SPACE [ ":" ] trailing ]
	 *
	 * nospcrlfcl =   %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF
	 *                ; any octet except NUL, CR, LF, " " and ":"
	 * middle     =   nospcrlfcl *( ":" / nospcrlfcl )
	 * trailing   =   *( ":" / " " / nospcrlfcl )
	 *
	 * SPACE      =   %x20        ; space character
	 * crlf       =   %x0D %x0A   ; "carriage return" "linefeed"
	 */

	memset(m, 0, sizeof(*m));

	if (!irc_strtrim(&buf))
		return -1;

	if (*buf == ':') {

		/* Prefix:
		 *  =  :name
		 *  =/ :name@host
		 *  =/ :name!user@host
		 */

		buf++;

		m->from = buf;

		while (*buf && *buf != ' '  && *buf != '!' && *buf != '@')
			buf++;

		m->len_from = buf - m->from;

		if (m->len_from == 0)
			return -1;

		if (*buf == '!' || *buf == '@') {
			*buf++ = 0;
			m->host = buf;

			while (*buf && *buf != ' ')
				buf++;

			m->len_host = buf - m->host;
		}

		if (*buf == ' ')
			*buf++ = 0;
	}

	if (!irc_strtrim(&buf))
		return -1;

	m->command = buf;

	while (*buf && *buf != ' ')
		buf++;

	m->len_command = buf - m->command;

	if (*buf == ' ')
		*buf++ = 0;

	if (irc_strtrim(&buf))
		m->params = buf;

	return 0;
}

int
irc_message_split(struct irc_message *m, const char **params, const char **trailing)
{
	/* Split the message params and trailing arg for use in generic handling */

	*params = m->params;
	*trailing = NULL;

	if (!m->params)
		return 0;

	m->split = 1;

	for (char *p = m->params; *p;) {

		while (*p == ' ')
			p++;

		if (*p == 0)
			return 0;

		m->n_params++;

		if (m->n_params >= 15) {
			if (m->params == p) {
				*params = m->params = NULL;
				*trailing = p;
			} else {
				*trailing = (*p) ? p : NULL;
				do {
					if (p == m->params) {
						*params = m->params = NULL;
						return 1;
					}
					p--;
				} while (*p == ' ');
				*(p + 1) = 0;
			}
			return 1;
		}

		if (*p == ':') {
			*p = 0;
			*trailing = (*(p + 1)) ? (p + 1) : NULL;
			do {
				if (p == m->params) {
					*params = m->params = NULL;
					return 1;
				}
				p--;
			} while (*p == ' ');
			*(p + 1) = 0;
			return 1;
		}

		while (*p && *p != ' ')
			p++;
	}

	return 0;
}

char*
irc_strsep(char **str)
{
	char *p;
	char *ret;

	if (str == NULL || (p = *str) == NULL)
		return NULL;

	if ((ret = irc_strtrim(&p)) == NULL)
		return NULL;

	while (*p && *p != ' ')
		p++;

	if (*p) {
		*p = 0;
		*str = (p + 1);
	} else {
		*str = NULL;
	}

	return *ret ? ret : NULL;
}

char*
irc_strtrim(char **str)
{
	char *p;

	if (*str == NULL)
		return NULL;

	for (p = *str; *p && *p == ' '; p++)
		;

	*str = p;

	return *p ? p : NULL;
}

char*
irc_strwrap(unsigned n, char **str, char *end)
{
	/* Greedy word wrap algorithm.
	 *
	 * Given a string bounded by [start, end), return a pointer to the character one
	 * past the maximum printable character for this string segment and advance the string
	 * pointer to the next printable character or the null terminator.
	 *
	 * For example, with 7 text columns and the string "wrap     testing":
	 *
	 *     word_wrap(7, &str, str + strlen(str));
	 *
	 *              split here
	 *                  |
	 *                  v
	 *            .......
	 *           "wrap     testing"
	 *                ^    ^
	 *     returns ___|    |___ str
	 *
	 * A subsequent call to wrap on the remainder, "testing", yields the case
	 * where the whole string fits and str is advanced to the end and returned.
	 *
	 * The caller should check that (str != end) before subsequent calls
	 */

	char *ret, *tmp;

	if (n < 1)
		fatal("insufficient columns: %d", n);

	/* All fits */
	if ((end - *str) <= n)
		return (*str = end);

	/* Find last occuring ' ' character */
	ret = (*str + n);

	while (ret > *str && *ret != ' ')
		ret--;

	/* Nowhere to wrap */
	if (ret == *str)
		return (*str = ret + n);

	/* Discard whitespace between wraps */
	tmp = ret;

	while (ret > *str && *(ret - 1) == ' ')
		ret--;

	while (*tmp == ' ')
		tmp++;

	*str = tmp;

	return ret;
}

static inline int
irc_ischanchar(char c, int first)
{
	/* RFC 2812, section 2.3.1
	 *
	 * channel    =  ( "#" / "+" / ( "!" channelid ) / "&" ) chanstring
	 *               [ ":" chanstring ]
	 * chanstring =  %x01-07 / %x08-09 / %x0B-0C / %x0E-1F / %x21-2B
	 * chanstring =/ %x2D-39 / %x3B-FF
	 *                 ; any octet except NUL, BELL, CR, LF, " ", "," and ":"
	 * channelid  = 5( %x41-5A / digit )   ; 5( A-Z / 0-9 )
	 */

	if (first)
		return (c == '#' || c == '+' || c == '&');

	switch (c) {
		case 0x00: /* NUL */
		case 0x07: /* BEL */
		case 0x0D: /* CR */
		case 0x0A: /* LF */
		case ' ':
		case ',':
		case ':':
			return 0;
		default:
			return 1;
	}
}

static inline int
irc_isnickchar(char c, int first)
{
	/* RFC 2812, section 2.3.1
	 *
	 * nickname   =  ( letter / special ) *8( letter / digit / special / "-" )
	 * letter     =  %x41-5A / %x61-7A       ; A-Z / a-z
	 * digit      =  %x30-39                 ; 0-9
	 * special    =  %x5B-60 / %x7B-7D       ; "[", "]", "\", "`", "_", "^", "{", "|", "}"
	 */

	return ((c >= 0x41 && c <= 0x7D) || (!first && ((c >= 0x30 && c <= 0x39) || c == '-')));
}

static inline int
irc_toupper(enum casemapping cm, int c)
{
	/* RFC 2812, section 2.2
	 *
	 * Because of IRC's Scandinavian origin, the characters {}|^ are
	 * considered to be the lower case equivalents of the characters []\~,
	 * respectively. This is a critical issue when determining the
	 * equivalence of two nicknames or channel names. */

	switch (cm) {
		case CASEMAPPING_RFC1459:
			if (c == '^') return '~';
			/* FALLTHROUGH */
		case CASEMAPPING_STRICT_RFC1459:
			if (c == '{') return '[';
			if (c == '}') return ']';
			if (c == '|') return '\\';
			/* FALLTHROUGH */
		case CASEMAPPING_ASCII:
			return (c >= 'a' && c <= 'z') ? (c + 'A' - 'a') : c;
		default:
			fatal("Unknown CASEMAPPING");
	}
}