File: libuirc.c

package info (click to toggle)
librnd 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,812 kB
  • sloc: ansic: 126,990; sh: 2,602; makefile: 2,145; awk: 7
file content (461 lines) | stat: -rw-r--r-- 10,003 bytes parent folder | download
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
/*
    libuirc - minimal IRC protocol
    Copyright (C) 2020  Tibor 'Igor2' Palinkas

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 31 Milk Street, # 960789 Boston, MA 02196 USA

    Author: libporty (at) igor2.repo.hu
*/


#include "libuirc.h"
#include <librnd/core/compat_misc.h>

#include <stdio.h>

static int uirc_query_alloc(uirc_t *ctx)
{
	int n;
	for(n = 0; n < UIRC_MAX_QUERIES; n++)
		if (ctx->query[n].type == UIRC_UNUSED)
			return n;
	return -1;
}

static int uirc_query_search(uirc_t *ctx, char *name)
{
	int n;
	for(n = 0; n < UIRC_MAX_QUERIES; n++) {
		if ((ctx->query[n].type != UIRC_CHAN) && (ctx->query[n].type != UIRC_PRIV))
			continue;
		if (strcmp(name, ctx->query[n].name) == 0)
			return n;
	}
	return -1;
}


int uirc_connect(uirc_t *ctx, const char *server, int port, char *user)
{
	P_net_init();
	if (P_tcp4_connect(&ctx->sk, server, port, NULL, 0, P_net_nonblock_full) != 0)
		return -1;
	ctx->connecting = 1;
	ctx->alive = 1;
	gds_init(&ctx->ibuf);
	gds_init(&ctx->obuf);
	gds_append_str(&ctx->obuf, "user libuirc a b :");
	gds_append_str(&ctx->obuf, user);
	gds_append_str(&ctx->obuf, "\nnick ");
	gds_append_str(&ctx->obuf, ctx->nick);
	gds_append(&ctx->obuf, '\n');

	ctx->query[0].type = UIRC_SERVER;
	ctx->query[0].name = NULL;

	return 0;
}

void uirc_disconnect(uirc_t *ctx)
{
	P_net_close(ctx->sk);
	ctx->alive = 0;
	ctx->sk = -1;
}


static uirc_event_t uirc_parse_001(uirc_t *ctx, char *arg)
{
	char *end, *nick = arg;

	/* nick may have been truncated */
	end = strpbrk(nick, " \t\r\n");
	if (end != NULL) {
		*end = '\0';
		if (strcmp(nick, ctx->nick) != 0) {
			free(ctx->nick);
			ctx->nick = rnd_strdup(nick);
		}
	}

	if (ctx->on_connect != NULL)
		ctx->on_connect(ctx);

	return UIRC_CONNECT;
}

#define irc_strcasecmp rnd_strcasecmp

static uirc_event_t uirc_parse_join(uirc_t *ctx, char *nick, char *arg)
{
	int q;
/*:libuirc!~libuirc@78-131-56-146.static.hdsnet.hu JOIN :#dev*/

	if (*arg == ':') arg++;

	q = uirc_query_search(ctx, arg);
	if (irc_strcasecmp(nick, ctx->nick) == 0) {
		uirc_event_t res = 0;
		if (q < 0) {
			q = uirc_query_alloc(ctx);
			if (q < 0) {
				gds_append_str(&ctx->obuf, "part ");
				gds_append_str(&ctx->obuf, arg);
				gds_append_str(&ctx->obuf, " :I am on too many channels already.\n");
				return 0;
			}
			ctx->query[q].type = UIRC_CHAN;
			ctx->query[q].name = rnd_strdup(arg);
			ctx->last_new_query = q;
			res |= UIRC_QUERY_BEGIN;
		}

		if (ctx->on_me_join != NULL)
			ctx->on_me_join(ctx, q, arg);
		return res | UIRC_ME_JOIN;
	}

	if ((q >= 0) && (ctx->on_join != NULL)) {
		ctx->on_join(ctx, nick, q, arg);
		return UIRC_JOIN;
	}

	return 0;
}

#define payload(dst, src) \
do { \
	dst = strpbrk(src, " \t"); \
	if (dst != NULL) { \
		*dst++ = '\0'; \
		if (*dst == ':') dst++; \
	} \
} while(0)

static uirc_event_t uirc_parse_part(uirc_t *ctx, char *nick, char *arg)
{
	char *reason;
	int q;

	if (*arg == ':') arg++;
	payload(reason, arg);

	q = uirc_query_search(ctx, arg);
	if (q < 0)
		return 0;

	if (irc_strcasecmp(nick, ctx->nick) == 0) {
		if (ctx->on_me_part != NULL)
			ctx->on_me_part(ctx, q, arg);
		if (ctx->on_query_end != NULL)
			ctx->on_query_end(ctx, q);
		ctx->query[q].type = UIRC_UNUSED;
		free(ctx->query[q].name);
		ctx->query[q].name = NULL;
		return UIRC_ME_PART | UIRC_QUERY_END;
	}

	if (ctx->on_part != NULL) {
		ctx->on_part(ctx, nick, q, arg, reason);
		return UIRC_PART;
	}

	return 0;
}

static uirc_event_t uirc_parse_kick(uirc_t *ctx, char *nick, char *arg)
{
	char *victim, *reason;
	int q;
	uirc_event_t res = 0;

	payload(victim, arg);
	payload(reason, victim);

	q = uirc_query_search(ctx, arg);
	if (q < 0)
		return 0;

	if (irc_strcasecmp(victim, ctx->nick) == 0) {
		if (ctx->on_me_part != NULL)
			ctx->on_me_part(ctx, q, arg);
		if (ctx->on_query_end != NULL)
			ctx->on_query_end(ctx, q);
		ctx->query[q].type = UIRC_UNUSED;
		free(ctx->query[q].name);
		ctx->query[q].name = NULL;
		res |= UIRC_ME_PART | UIRC_QUERY_END;
	}

	if (ctx->on_kick != NULL) {
		ctx->on_kick(ctx, nick, q, arg, victim, reason);
		res |= UIRC_KICK;
	}

	return res;
}

static uirc_event_t uirc_parse_msg(uirc_t *ctx, char *nick, char *arg)
{
	char *text;
	int q;

	if (*arg == ':') arg++;
	payload(text, arg);

	q = uirc_query_search(ctx, arg);

	if (ctx->on_msg != NULL) {
		ctx->on_msg(ctx, nick, q, arg, text);
		return UIRC_MSG;
	}

	return 0;
}

static uirc_event_t uirc_parse_notice(uirc_t *ctx, char *nick, char *arg)
{
	char *text;
	int q;

	if (*arg == ':') arg++;
	payload(text, arg);

	q = uirc_query_search(ctx, arg);

	if (ctx->on_notice != NULL) {
		ctx->on_notice(ctx, nick, q, arg, text);
		return UIRC_NOTICE;
	}

	return 0;
}

static uirc_event_t uirc_parse_topic(uirc_t *ctx, char *nick, char *arg, int numeric)
{
	char *text;
	int q;

	if (*arg == ':') arg++;
	payload(text, arg);

	q = uirc_query_search(ctx, arg);

	if (ctx->on_topic != NULL) {
		if (numeric)
			nick = NULL;
		ctx->on_topic(ctx, nick, q, arg, text);
		return UIRC_TOPIC;
	}

	return 0;
}

static void uirc_new_nick(uirc_t *ctx)
{
	int len = strlen(ctx->nick);
	if (len == 0) {
		free(ctx->nick);
		ctx->nick = rnd_strdup("libuirc");
		len = 7;
	}
	if (len < 9) {
		char *nick = malloc(len+2);
		memcpy(nick, ctx->nick, len);
		nick[len] = (rand() % ('a'-'z')) + 'a';
		nick[len+1] = '\0';
		free(ctx->nick);
		ctx->nick = nick;
	}
	else
		ctx->nick[rand() % len] = (rand() % ('a'-'z')) + 'a';
	gds_append_str(&ctx->obuf, "nick ");
	gds_append_str(&ctx->obuf, ctx->nick);
	gds_append(&ctx->obuf, '\n');
}

static uirc_event_t uirc_parse(uirc_t *ctx, char *line)
{
	char *end, *arg, *cmd, *from = line;
	uirc_event_t res = 0;

#ifdef LIBUIRC_TRACE
fprintf(stderr, "line='%s'\n", line);
#endif

	if (strncmp(line, "PING", 4) == 0) {
		from[1] = 'O';
		uirc_raw(ctx, line);
		return 0;
	}

	cmd = strpbrk(line, " \t\r\n");
	if (cmd == NULL)
		return 0;
	*cmd = '\0';
	cmd++;

	if (strchr(from, '@') == 0) {
		if (ctx->on_got_misc != NULL)
			ctx->on_got_misc(ctx, 0, cmd);
		res |= UIRC_GOT_MISC;
	}

	end = strchr(from, '!');
	if (end != NULL) {
		if (*from == ':')
			from++;
		*end = 0;
	}

	arg = strpbrk(cmd, " \t\r\n");
	if (arg != NULL) {
		*arg = '\0';
		arg++;
	}

	end = strpbrk(arg, "\r\n");
	if (end != NULL)
		*end = '\0';

	if ((ctx->on_rawin != NULL) && (ctx->on_rawin(ctx, from, cmd, arg) != 0))
		return res;

	if (strcmp(cmd, "001") == 0) return res | uirc_parse_001(ctx, arg);
	if (irc_strcasecmp(cmd, "join") == 0) return res | uirc_parse_join(ctx, from, arg);
	if (irc_strcasecmp(cmd, "part") == 0) return res | uirc_parse_part(ctx, from, arg);
	if (irc_strcasecmp(cmd, "kick") == 0) return res | uirc_parse_kick(ctx, from, arg);
	if (irc_strcasecmp(cmd, "privmsg") == 0) return res | uirc_parse_msg(ctx, from, arg);
	if (irc_strcasecmp(cmd, "notice") == 0) return res | uirc_parse_notice(ctx, from, arg);
	if (irc_strcasecmp(cmd, "topic") == 0) return res | uirc_parse_topic(ctx, from, arg, 0);
	if (irc_strcasecmp(cmd, "332") == 0) return res | uirc_parse_topic(ctx, from, arg, 1);
	if (irc_strcasecmp(cmd, "433") == 0) uirc_new_nick(ctx);

	return res;
}

int uirc_get_poll_events(uirc_t *ctx)
{
	return P_POLLIN | ((ctx->obuf.used > 0) ? P_POLLOUT : 0);
}

uirc_event_t uirc_poll(uirc_t *ctx, int *revents)
{
	struct P_pollfd fds[1];
	int new_data = 0;
	char *nl;
	uirc_event_t res = 0;

	if (ctx->sk < 0)
		return 0;

	fds[0].fd = ctx->sk;

	for(;;) {
		if (revents == NULL) {
			fds[0].events = uirc_get_poll_events(ctx);
			if (P_poll(fds, 1, 0) < 1)
				break;
		}
		else
			fds[0].revents = *revents;

		if (fds[0].revents & P_POLLIN) {
			P_size_t len, oused = ctx->ibuf.used;
			gds_enlarge(&ctx->ibuf, ctx->ibuf.used + 600);
			ctx->ibuf.used = oused;
			len = P_net_read(ctx->sk, ctx->ibuf.array + ctx->ibuf.used, 600);
			if (len < 0) {
				if (P_net_inprogress)
					continue;
				uirc_disconnect(ctx);
				return res | UIRC_DISCONNECT;
			}
			if (len == 0) {
				uirc_disconnect(ctx);
				return res | UIRC_DISCONNECT;
			}
			if (len > 0) {
				ctx->ibuf.used += len;
				new_data = 1;
			}
		}
		if ((fds[0].revents & P_POLLOUT) && (ctx->obuf.used > 0)) {
			P_size_t len = P_net_write(ctx->sk, ctx->obuf.array, ctx->obuf.used);
			ctx->connecting = 0;
			if (len < 0) {
				if (P_net_inprogress)
					continue;
				uirc_disconnect(ctx);
				return res | UIRC_DISCONNECT;
			}
			if (len == 0) {
				uirc_disconnect(ctx);
				return res | UIRC_DISCONNECT;
			}
			if (len > 0)
				gds_remove(&ctx->obuf, 0, len);
		}
		if (fds[0].revents & (~(P_POLLIN | P_POLLOUT))) {
			uirc_disconnect(ctx);
			return res | UIRC_DISCONNECT;
		}

		if (revents != NULL)
			break;
	}

	/* call the protocol parse */
	if (!new_data)
		return res;

	for(;;) {
		nl = strchr(ctx->ibuf.array, '\n');
		if (nl == NULL)
			break;
		*nl = '\0';
		res |= uirc_parse(ctx, ctx->ibuf.array);
		gds_remove(&ctx->ibuf, 0, nl - ctx->ibuf.array+1);
	}
	return res;
}


void uirc_join(uirc_t *ctx, const char *chan)
{

}

void uirc_close(uirc_t *ctx, int query)
{

}

void uirc_privmsg(uirc_t *ctx, const char *target, const char *msg)
{
	gds_append_str(&ctx->obuf, "PRIVMSG ");
	gds_append_str(&ctx->obuf, target);
	gds_append_str(&ctx->obuf, " :");
	gds_append_str(&ctx->obuf, (char *)msg);
	gds_append(&ctx->obuf, '\n');
}

void uirc_raw(uirc_t *ctx, const char *msg)
{
	gds_append_str(&ctx->obuf, (char *)msg);
	gds_append(&ctx->obuf, '\n');
}