summary refs log tree commit diff
path: root/bin/pingbot.c
blob: 41876567ef11b9f49f48ff620782f1eff6b2e4fc (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
/* Copyright (C) 2019  June 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 <err.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/event.h>
#include <sys/socket.h>
#include <sysexits.h>
#include <unistd.h>

typedef unsigned uint;

__attribute__((format(printf, 2, 3)))
static int pdprintf(int fd, const char *format, ...) {
	va_list ap;
	va_start(ap, format);
	vprintf(format, ap);
	va_end(ap);
	va_start(ap, format);
	int len = vdprintf(fd, format, ap);
	va_end(ap);
	return len;
}

int main(int argc, char *argv[]) {
	int error;

	const char *host = NULL;
	const char *port = "6667";
	const char *nick = "pingbot";
	const char *join = NULL;
	uint timeout = 120;
	uint delay = 30;

	int opt;
	while (0 < (opt = getopt(argc, argv, "d:j:n:p:t:"))) {
		switch (opt) {
			break; case 'd': delay = strtoul(optarg, NULL, 0);
			break; case 'j': join = optarg;
			break; case 'n': nick = optarg;
			break; case 'p': port = optarg;
			break; case 't': timeout = strtoul(optarg, NULL, 0);
			break; default:  return EX_USAGE;
		}
	}
	if (optind < argc) host = argv[optind];
	if (!host || !timeout) return EX_USAGE;

	struct addrinfo *head;
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_protocol = IPPROTO_TCP,
	};
	error = getaddrinfo(host, port, &hints, &head);
	if (error) errx(EX_NOHOST, "getaddrinfo: %s", gai_strerror(error));

	int client = -1;
	struct addrinfo *ai;
	for (ai = head; ai; ai = ai->ai_next) {
		client = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
		if (client < 0) err(EX_OSERR, "socket");

		error = connect(client, ai->ai_addr, ai->ai_addrlen);
		if (!error) break;

		close(client);
		client = -1;
	}
	if (client < 0) err(EX_UNAVAILABLE, "connect");

	error = pdprintf(client, "NICK %1$s\r\nUSER %1$s 0 * :%1$s\r\n", nick);
	if (error < 0) err(EX_IOERR, "pdprintf");

	int kq = kqueue();
	if (kq < 0) err(EX_OSERR, "kqueue");

	struct kevent event;
	EV_SET(&event, client, EVFILT_READ, EV_ADD, 0, 0, 0);
	int nevents = kevent(kq, &event, 1, NULL, 0, NULL);
	if (nevents < 0) err(EX_OSERR, "kevent");

	char buf[4096];
	size_t len = 0;
	bool pong = true;
	for (;;) {
		nevents = kevent(kq, NULL, 0, &event, 1, NULL);
		if (nevents < 0) err(EX_IOERR, "kevent");

		if (event.filter == EVFILT_TIMER) {
			char *ping = event.udata;
			if (pong) {
				error = pdprintf(client, "PONG %s\r\n", ping);
				if (error < 0) err(EX_IOERR, "pdprintf");
			}
			pong = true;
			free(ping);
			continue;
		}

		ssize_t rlen = read(client, &buf[len], sizeof(buf) - len);
		if (rlen < 0) err(EX_IOERR, "read");
		len += (size_t)rlen;

		char *crlf;
		char *line = buf;
		while (NULL != (crlf = strnstr(line, "\r\n", &buf[len] - line))) {
			crlf[0] = '\0';
			printf("%s\n", line);

			if (line[0] == ':') strsep(&line, " ");
			if (!line) errx(EX_PROTOCOL, "unexpected eol");
			char *cmd = strsep(&line, " ");

			if (!strcmp(cmd, "001") && join) {
				error = pdprintf(client, "JOIN :%s\r\n", join);
				if (error < 0) err(EX_IOERR, "pdprintf");

			} else if (!strcmp(cmd, "PING")) {
				EV_SET(
					&event, 0, EVFILT_TIMER, EV_ADD | EV_ONESHOT,
					NOTE_SECONDS, timeout - 1, strdup(line)
				);
				nevents = kevent(kq, &event, 1, NULL, 0, NULL);
				if (nevents < 0) err(EX_OSERR, "kevent");

			} else if (!strcmp(cmd, "PRIVMSG") && strcasestr(line, nick)) {
				pong = false;

			} else if (!strcmp(cmd, "ERROR")) {
				close(client);
				sleep(delay);

				client = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
				if (client < 0) err(EX_OSERR, "socket");

				error = connect(client, ai->ai_addr, ai->ai_addrlen);
				if (error) err(EX_UNAVAILABLE, "connect");

				error = pdprintf(
					client, "NICK %1$s\r\nUSER %1$s 0 * :%1$s\r\n", nick
				);
				if (error < 0) err(EX_IOERR, "pdprintf");

				EV_SET(&event, client, EVFILT_READ, EV_ADD, 0, 0, 0);
				int nevents = kevent(kq, &event, 1, NULL, 0, NULL);
				if (nevents < 0) err(EX_OSERR, "kevent");
			}

			line = &crlf[2];
		}

		len -= line - buf;
		memmove(buf, line, len);
	}
}