~rcr/rirc

ref: 7ca2a99a605b7e0b88fa85bc43a8fadc13695ba8 rirc/src/handlers/irc_ctcp.c -rw-r--r-- 12.5 KiB
7ca2a99a — Richard Robbins refactor order of includes 1 year, 7 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "src/handlers/irc_ctcp.h"

#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>

#include "src/components/channel.h"
#include "src/handlers/irc_ctcp.gperf.out"
#include "src/io.h"
#include "src/state.h"
#include "src/utils/utils.h"

#define failf(S, ...) \
	do { server_error((S), __VA_ARGS__); \
	     return 1; \
	} while (0)

#define sendf(S, ...) \
	do { int ret; \
	     if ((ret = io_sendf((S)->connection, __VA_ARGS__))) \
	         failf((S), "Send fail: %s", io_err(ret)); \
	} while (0)

static int
parse_ctcp(struct server *s, const char *from, char **args, const char **cmd)
{
	char *message = *args;
	char *command;
	char *p;

	if (!from)
		failf(s, "Received CTCP from unknown sender");

	if (*message != '\001')
		failf(s, "Received malformed CTCP from %s", from);

	if ((p = strchr(message + 1, '\001')))
		*p = 0;

	*message++ = 0;

	if (!(command = strsep(&message)))
		failf(s, "Received empty CTCP from %s", from);

	for (p = command; *p; p++)
		*p = toupper(*p);

	*cmd = command;
	*args = strtrim(&message);

	return 0;
}

int
ctcp_request(struct server *s, const char *from, const char *targ, char *message)
{
	const char *command;
	const struct ctcp_handler *ctcp;
	int ret;

	if ((ret = parse_ctcp(s, from, &message, &command)) != 0)
		return ret;

	if (!(ctcp = ctcp_handler_lookup(command, strlen(command))))
		failf(s, "Received unsupported CTCP request '%s' from %s", command, from);

	return ctcp->f_request(s, from, targ, message);
}

int
ctcp_response(struct server *s, const char *from, const char *targ, char *message)
{
	const char *command;
	const struct ctcp_handler *ctcp;
	int ret;

	if ((ret = parse_ctcp(s, from, &message, &command)) != 0)
		return ret;

	if (!(ctcp = ctcp_handler_lookup(command, strlen(command))) || !ctcp->f_response)
		failf(s, "Received unsupported CTCP response '%s' from %s", command, from);

	return ctcp->f_response(s, from, targ, message);
}

static int
ctcp_request_action(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Formatting
	 * Request:  ACTION <text>
	 * Response: -- no response --
	 *
	 * This extended formatting message shows that <text> should be displayed as
	 * a third-person action or emote. If <text> is empty, clients SHOULD still
	 * include a single space after
	 */

	struct channel *c;

	if (!targ)
		failf(s, "CTCP ACTION: target is NULL");

	if (!irc_strcmp(s->casemapping, targ, s->nick)) {
		if ((c = channel_list_get(&s->clist, from, s->casemapping)) == NULL) {
			c = channel(from, CHANNEL_T_PRIVATE);
			c->activity = ACTIVITY_PINGED;
			c->server = s;
			channel_list_add(&s->clist, c);
		}
	} else if ((c = channel_list_get(&s->clist, targ, s->casemapping)) == NULL) {
		failf(s, "CTCP ACTION: target '%s' not found", targ);
	}

	if (strtrim(&m))
		newlinef(c, 0, "*", "%s %s", from, m);
	else
		newlinef(c, 0, "*", "%s", from);

	return 0;
}

static int
ctcp_request_clientinfo(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  CLIENTINFO
	 * Response: CLIENTINFO <args>
	 *
	 * This extended query returns a list of the CTCP messages that this client
	 * supports and implements, delimited by a single ASCII space.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP CLIENTINFO from %s (%s)", from, m);
	else
		server_info(s, "CTCP CLIENTINFO from %s", from);

	sendf(s, "NOTICE %s :\001CLIENTINFO ACTION CLIENTINFO PING SOURCE TIME VERSION\001", from);

	return 0;
}

static int
ctcp_request_finger(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  FINGER
	 * Response: FINGER <info>
	 *
	 * This metadata query returns miscellaneous info about the user, typically
	 * the same information that’s held in their realname field. However, some
	 * implementations return the client name and version instead.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP FINGER from %s (%s)", from, m);
	else
		server_info(s, "CTCP FINGER from %s", from);

	sendf(s, "NOTICE %s :\001FINGER rirc v"VERSION" ("__DATE__")\001", from);

	return 0;
}

static int
ctcp_request_ping(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  PING <info>
	 * Response: PING <info>
	 *
	 * This extended query confirms reachability and latency to the target
	 * client. When receiving a CTCP PING, the reply MUST contain exactly
	 * the same parameters as the original query.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP PING from %s (%s)", from, m);
	else
		server_info(s, "CTCP PING from %s", from);

	if (m)
		sendf(s, "NOTICE %s :\001PING %s\001", from, m);
	else
		sendf(s, "NOTICE %s :\001PING\001", from);

	return 0;
}

static int
ctcp_request_source(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  SOURCE
	 * Response: SOURCE <info>
	 *
	 * This metadata query returns the location of the source code for the client.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP SOURCE from %s (%s)", from, m);
	else
		server_info(s, "CTCP SOURCE from %s", from);

	sendf(s, "NOTICE %s :\001SOURCE rcr.io/rirc\001", from);

	return 0;
}

static int
ctcp_request_time(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  TIME
	 * Response: TIME <timestring>
	 *
	 * This extended query returns the client’s local time in an unspecified
	 * human-readable format. In practice, both the format output by ctime()
	 * and the format described in Section 3.3 of RFC5322 are common. Earlier
	 * specifications recommended prefixing the time string with a colon,
	 * but this is no longer recommended.
	 */

	/* ISO 8601 */
	char buf[sizeof("1970-01-01T00:00:00")];
	struct tm tm;
	time_t t = 0;

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP TIME from %s (%s)", from, m);
	else
		server_info(s, "CTCP TIME from %s", from);

#ifdef TESTING
	if (gmtime_r(&t, &tm) == NULL)
		failf(s, "CTCP TIME: gmtime_r error: %s", strerror(errno));
#else
	t = time(NULL);

	if (localtime_r(&t, &tm) == NULL)
		failf(s, "CTCP TIME: localtime_r error: %s", strerror(errno));
#endif

	if ((strftime(buf, sizeof(buf), "%FT%T", &tm)) == 0)
		failf(s, "CTCP TIME: strftime error");

	sendf(s, "NOTICE %s :\001TIME %s\001", from, buf);

	return 0;
}

static int
ctcp_request_userinfo(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  USERINFO
	 * Response: USERINFO <info>
	 *
	 * This metadata query returns miscellaneous info about the user, typically
	 * the same information that’s held in their realname field. However, some
	 * implementations return <nickname> (<realname>) instead.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP USERINFO from %s (%s)", from, m);
	else
		server_info(s, "CTCP USERINFO from %s", from);

	sendf(s, "NOTICE %s :\001USERINFO %s (%s)\001", from, s->nick, s->realname);

	return 0;
}

static int
ctcp_request_version(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  VERSION
	 * Response: VERSION <verstring>
	 *
	 * This metadata query returns the name and version of the client software in
	 * use. There is no specified format for the version string.
	 */

	UNUSED(targ);

	if (strtrim(&m))
		server_info(s, "CTCP VERSION from %s (%s)", from, m);
	else
		server_info(s, "CTCP VERSION from %s", from);

	sendf(s, "NOTICE %s :\001VERSION rirc v"VERSION" ("__DATE__")\001", from);

	return 0;
}

static int
ctcp_response_clientinfo(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  CLIENTINFO
	 * Response: CLIENTINFO <args>
	 *
	 * This extended query returns a list of the CTCP messages that this client
	 * supports and implements, delimited by a single ASCII space.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP CLIENTINFO response from %s: empty message", from);

	server_info(s, "CTCP CLIENTINFO response from %s: %s", from, m);

	return 0;
}

static int
ctcp_response_finger(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  FINGER
	 * Response: FINGER <info>
	 *
	 * This metadata query returns miscellaneous info about the user, typically
	 * the same information that’s held in their realname field. However, some
	 * implementations return the client name and version instead.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP FINGER response from %s: empty message", from);

	server_info(s, "CTCP FINGER response from %s: %s", from, m);

	return 0;
}

static int
ctcp_response_ping(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  PING <info>
	 * Response: PING <info>
	 *
	 * This extended query confirms reachability and latency to the target
	 * client. When receiving a CTCP PING, the reply MUST contain exactly
	 * the same parameters as the original query.
	 */

	const char *sec;
	const char *usec;
	long long unsigned res;
	long long unsigned res_sec;
	long long unsigned res_usec;
	long unsigned t1_sec;
	long unsigned t1_usec;
	long unsigned t2_sec;
	long unsigned t2_usec;

	UNUSED(targ);

	if (!(sec = strsep(&m)))
		failf(s, "CTCP PING response from %s: sec is NULL", from);

	if (!(usec = strsep(&m)))
		failf(s, "CTCP PING response from %s: usec is NULL", from);

	for (const char *p = sec; *p; p++) {
		if (!(isdigit(*p)))
			failf(s, "CTCP PING response from %s: sec is invalid", from);
	}

	for (const char *p = usec; *p; p++) {
		if (!(isdigit(*p)))
			failf(s, "CTCP PING response from %s: usec is invalid", from);
	}

#ifdef TESTING
	t2_sec = 123;
	t2_usec = 456789;
#else
	struct timeval t;

	(void) gettimeofday(&t, NULL);

	t2_sec = (long unsigned) t.tv_sec;
	t2_usec = (long unsigned) t.tv_usec;
#endif

	errno = 0;

	t1_sec = strtoul(sec, NULL, 10);
	t1_usec = strtoul(usec, NULL, 10);

	if (errno)
		failf(s, "CTCP PING response from %s: failed to parse timestamp", from);

	if ((t1_usec > 999999) || (t1_sec > t2_sec) || (t1_sec == t2_sec && t1_usec > t2_usec))
		failf(s, "CTCP PING response from %s: invalid timestamp", from);

	res = (t2_usec + (1000000 * t2_sec)) - (t1_usec + (1000000 * t1_sec));
	res_sec = res / 1000000;
	res_usec = res % 1000000;

	server_info(s, "CTCP PING response from %s: %llu.%llus", from, res_sec, res_usec);

	return 0;
}

static int
ctcp_response_source(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  SOURCE
	 * Response: SOURCE <info>
	 *
	 * This metadata query returns the location of the source code for the client.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP SOURCE response from %s: empty message", from);

	server_info(s, "CTCP SOURCE response from %s: %s", from, m);

	return 0;
}

static int
ctcp_response_time(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Extended Query
	 * Request:  TIME
	 * Response: TIME <timestring>
	 *
	 * This extended query returns the client’s local time in an unspecified
	 * human-readable format. In practice, both the format output by ctime()
	 * and the format described in Section 3.3 of RFC5322 are common. Earlier
	 * specifications recommended prefixing the time string with a colon,
	 * but this is no longer recommended.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP TIME response from %s: empty message", from);

	server_info(s, "CTCP TIME response from %s: %s", from, m);

	return 0;
}

static int
ctcp_response_userinfo(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  USERINFO
	 * Response: USERINFO <info>
	 *
	 * This metadata query returns miscellaneous info about the user, typically
	 * the same information that’s held in their realname field. However, some
	 * implementations return <nickname> (<realname>) instead.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP USERINFO response from %s: empty message", from);

	server_info(s, "CTCP USERINFO response from %s: %s", from, m);

	return 0;
}

static int
ctcp_response_version(struct server *s, const char *from, const char *targ, char *m)
{
	/* Type:     Metadata Query
	 * Request:  VERSION
	 * Response: VERSION <verstring>
	 *
	 * This metadata query returns the name and version of the client software in
	 * use. There is no specified format for the version string.
	 */

	UNUSED(targ);

	if (!strtrim(&m))
		failf(s, "CTCP VERSION response from %s: empty message", from);

	server_info(s, "CTCP VERSION response from %s: %s", from, m);

	return 0;
}

#undef failf
#undef sendf