2 a29564dd 2023-08-20 op * Copyright (c) 2023 Omar Polo <op@openbsd.org>
3 d23d2886 2023-07-30 op * Copyright (c) 2003-2004 Daniel Hartmeier
4 d23d2886 2023-07-30 op * All rights reserved.
6 d23d2886 2023-07-30 op * Redistribution and use in source and binary forms, with or without
7 d23d2886 2023-07-30 op * modification, are permitted provided that the following conditions
10 d23d2886 2023-07-30 op * - Redistributions of source code must retain the above copyright
11 d23d2886 2023-07-30 op * notice, this list of conditions and the following disclaimer.
12 d23d2886 2023-07-30 op * - Redistributions in binary form must reproduce the above
13 d23d2886 2023-07-30 op * copyright notice, this list of conditions and the following
14 d23d2886 2023-07-30 op * disclaimer in the documentation and/or other materials provided
15 d23d2886 2023-07-30 op * with the distribution.
17 d23d2886 2023-07-30 op * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 d23d2886 2023-07-30 op * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 d23d2886 2023-07-30 op * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 d23d2886 2023-07-30 op * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 d23d2886 2023-07-30 op * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 d23d2886 2023-07-30 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 d23d2886 2023-07-30 op * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 d23d2886 2023-07-30 op * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 d23d2886 2023-07-30 op * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 d23d2886 2023-07-30 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 d23d2886 2023-07-30 op * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 d23d2886 2023-07-30 op * POSSIBILITY OF SUCH DAMAGE.
32 a3ace556 2023-08-01 op #include <err.h>
33 d23d2886 2023-07-30 op #include <stdio.h>
34 d23d2886 2023-07-30 op #include <stdlib.h>
35 d23d2886 2023-07-30 op #include <string.h>
37 d23d2886 2023-07-30 op #include "icb.h"
38 d23d2886 2023-07-30 op #include "irc.h"
40 c8394ca1 2023-08-01 op extern void icbirc_quit(void);
41 d23d2886 2023-07-30 op extern int sync_write(int, const char *, int);
43 d23d2886 2023-07-30 op static unsigned char icb_args(const char *, unsigned char, char [255][255]);
44 d23d2886 2023-07-30 op static void icb_cmd(const char *, unsigned char, int, int);
45 d23d2886 2023-07-30 op static void icb_ico(int, const char *);
46 d23d2886 2023-07-30 op static void icb_iwl(int, const char *, const char *, long,
47 d23d2886 2023-07-30 op long, const char *, const char *);
48 d23d2886 2023-07-30 op static void icb_send_hw(int, const char *);
50 fbe4e593 2023-08-01 op int icb_logged_in;
52 d23d2886 2023-07-30 op static char icb_protolevel[256];
53 d23d2886 2023-07-30 op static char icb_hostid[256];
54 d23d2886 2023-07-30 op static char icb_serverid[256];
55 d23d2886 2023-07-30 op static char icb_moderator[256];
56 d23d2886 2023-07-30 op enum { imode_none, imode_list, imode_names, imode_whois, imode_who };
57 d23d2886 2023-07-30 op static int imode = imode_none;
58 d23d2886 2023-07-30 op static char icurgroup[256];
59 d23d2886 2023-07-30 op static char igroup[256];
60 d23d2886 2023-07-30 op static char inick[256];
61 d23d2886 2023-07-30 op static char ihostmask[256];
62 d23d2886 2023-07-30 op static unsigned off;
65 d23d2886 2023-07-30 op * A single ICB packet consists of a length byte, a command byte and
66 d23d2886 2023-07-30 op * variable arguments. The length includes command and arguments, but
67 d23d2886 2023-07-30 op * not the length byte itself. Since length is at most 255, the entire
68 d23d2886 2023-07-30 op * packet is at most 256 bytes long.
70 d23d2886 2023-07-30 op * icb_recv() gets passed read(2) chunks and assembles a complete packet
71 d23d2886 2023-07-30 op * (including the length byte) in cmd. Once complete, the packet is
72 d23d2886 2023-07-30 op * passed to icb_cmd() without the length byte. Hence, arguments to
73 d23d2886 2023-07-30 op * icb_cmd() are at most 255 bytes long.
75 d23d2886 2023-07-30 op * icb_cmd() skips the command byte and passes only the variable
76 d23d2886 2023-07-30 op * arguments to icb_args(). Hence, arguments to icb_args() are at most
77 d23d2886 2023-07-30 op * 254 octects long.
79 d23d2886 2023-07-30 op * Variable arguments consist of zero or more strings separated by
80 d23d2886 2023-07-30 op * \001 characters. The strings need not be null-terminated and may
81 d23d2886 2023-07-30 op * be empty. Hence, there can be at most 255 strings and a string can
82 d23d2886 2023-07-30 op * be at most 254 bytes long. icb_args() fills the array argument,
83 d23d2886 2023-07-30 op * null-terminating each argument.
85 d23d2886 2023-07-30 op * This (together with the comments below) should be convincing proof
86 d23d2886 2023-07-30 op * that the char [255][255] as well as the unsigned char variables
87 d23d2886 2023-07-30 op * cannot overflow.
89 d23d2886 2023-07-30 op * Further argument parsing in icb_cmd() and icb_ico() relies on the
90 d23d2886 2023-07-30 op * fact that any argument can be at most 255 bytes long (including
91 d23d2886 2023-07-30 op * null-termination).
93 d23d2886 2023-07-30 op * The icb_send_*() functions may get arbitrarily long arguments from
94 d23d2886 2023-07-30 op * IRC, they may generate packets of at most 256 bytes size. Overlong
95 d23d2886 2023-07-30 op * arguments are truncated, except for open and personal messages,
96 d23d2886 2023-07-30 op * which are split across multiple packets, if needed (generating
97 d23d2886 2023-07-30 op * separate messages on ICB).
99 d23d2886 2023-07-30 op * The ICB protocol definition is not very clear about null-termination
100 d23d2886 2023-07-30 op * of arguments for packets generated by the client. Without any
101 d23d2886 2023-07-30 op * termination, at least one common server implementation shows a
102 d23d2886 2023-07-30 op * buffer re-use bug. Terminating all arguments, however, causes
103 d23d2886 2023-07-30 op * another server implementation to refuse certain commands. The
104 d23d2886 2023-07-30 op * best approach seems to be to null-terminate only the last
105 d23d2886 2023-07-30 op * argument. Where the code below violates that rule, that was done
106 d23d2886 2023-07-30 op * intentionally after testing.
111 d23d2886 2023-07-30 op scan(const char **s, char *d, size_t siz, const char *skip, const char *term)
113 d23d2886 2023-07-30 op while (**s && strchr(skip, **s) != NULL)
115 d23d2886 2023-07-30 op while (**s && strchr(term, **s) == NULL) {
116 d23d2886 2023-07-30 op if (siz > 1) {
127 d23d2886 2023-07-30 op icb_init(void)
129 d23d2886 2023-07-30 op memset(icb_protolevel, 0, sizeof(icb_protolevel));
130 d23d2886 2023-07-30 op memset(icb_hostid, 0, sizeof(icb_hostid));
131 d23d2886 2023-07-30 op memset(icb_serverid, 0, sizeof(icb_serverid));
132 d23d2886 2023-07-30 op memset(icb_moderator, 0, sizeof(icb_moderator));
133 d23d2886 2023-07-30 op imode = imode_none;
134 d23d2886 2023-07-30 op memset(icurgroup, 0, sizeof(icurgroup));
135 d23d2886 2023-07-30 op memset(igroup, 0, sizeof(igroup));
136 d23d2886 2023-07-30 op memset(inick, 0, sizeof(inick));
137 d23d2886 2023-07-30 op memset(ihostmask, 0, sizeof(ihostmask));
142 d23d2886 2023-07-30 op icb_recv(const char *buf, unsigned len, int fd, int server_fd)
144 d23d2886 2023-07-30 op static unsigned char cmd[256];
146 d23d2886 2023-07-30 op while (len > 0) {
147 d23d2886 2023-07-30 op if (off == 0) {
148 d23d2886 2023-07-30 op cmd[off++] = *buf++;
149 d23d2886 2023-07-30 op /* 0 < cmd[0] <= 255 */
152 d23d2886 2023-07-30 op /* off > 0, 0 < cmd[0] <= 255 */
153 d23d2886 2023-07-30 op while (len > 0 && (off - 1) < cmd[0]) {
154 d23d2886 2023-07-30 op cmd[off++] = *buf++;
157 d23d2886 2023-07-30 op /* len == 0 || (off - 1) == cmd[0] */
158 d23d2886 2023-07-30 op if ((off - 1) == cmd[0]) {
159 d23d2886 2023-07-30 op icb_cmd(cmd + 1, off - 1 /* <= 255 */, fd, server_fd);
165 d23d2886 2023-07-30 op static unsigned char
166 d23d2886 2023-07-30 op icb_args(const char *data, unsigned char len, char args[255][255])
168 d23d2886 2023-07-30 op unsigned char i = 0, j = 0, k = 0;
170 d23d2886 2023-07-30 op /* 0 < len < 255 */
171 d23d2886 2023-07-30 op while (i < len) {
172 d23d2886 2023-07-30 op /* 0 <= i, j, k < 255 */
173 d23d2886 2023-07-30 op if (data[i] == '\001') {
174 d23d2886 2023-07-30 op args[j++][k] = 0;
176 d23d2886 2023-07-30 op } else if (data[i] == '\r' || data[i] == '\n')
177 d23d2886 2023-07-30 op args[j][k++] = '?';
179 d23d2886 2023-07-30 op args[j][k++] = data[i];
182 d23d2886 2023-07-30 op /* i, j, k < 255 */
184 d23d2886 2023-07-30 op args[j++][k] = 0;
185 d23d2886 2023-07-30 op /* j <= 255 */
186 d23d2886 2023-07-30 op for (i = j; i < 255; ++i)
187 d23d2886 2023-07-30 op args[i][0] = 0;
192 d23d2886 2023-07-30 op icb_cmd(const char *cmd, unsigned char len, int fd, int server_fd)
194 d23d2886 2023-07-30 op char args[255][255];
195 d23d2886 2023-07-30 op const char *a = args[1];
196 d23d2886 2023-07-30 op unsigned char i, j;
197 d23d2886 2023-07-30 op char s[8192];
199 d23d2886 2023-07-30 op if (len == 0)
202 d23d2886 2023-07-30 op /* 0 < len <= 255 */
203 d23d2886 2023-07-30 op i = icb_args(cmd + 1, len - 1 /* < 255 */, args);
204 d23d2886 2023-07-30 op /* 0 <= i <= 255 */
205 d23d2886 2023-07-30 op switch (cmd[0]) {
206 d23d2886 2023-07-30 op case 'a': /* Login OK */
207 d23d2886 2023-07-30 op irc_send_code(fd, icb_hostid, irc_nick, "001",
208 d23d2886 2023-07-30 op "Welcome to icbirc %s", irc_nick);
209 d23d2886 2023-07-30 op irc_send_code(fd, icb_hostid, irc_nick, "002",
210 d23d2886 2023-07-30 op "Your host is %s running %s protocol %s",
211 d23d2886 2023-07-30 op icb_hostid, icb_serverid, icb_protolevel);
212 d23d2886 2023-07-30 op /* some clients really want to see a MOTD */
213 d23d2886 2023-07-30 op irc_send_code(fd, icb_hostid, irc_nick, "375",
214 d23d2886 2023-07-30 op "ICB server: %s", icb_serverid);
215 d23d2886 2023-07-30 op irc_send_code(fd, icb_hostid, irc_nick, "376",
216 d23d2886 2023-07-30 op "End of MOTD");
217 d23d2886 2023-07-30 op icb_logged_in = 1;
219 d23d2886 2023-07-30 op case 'b': /* Open Message */
220 d23d2886 2023-07-30 op if (!in_irc_channel) {
221 d23d2886 2023-07-30 op irc_send_join(fd, irc_nick, irc_channel);
222 d23d2886 2023-07-30 op icb_send_names(server_fd, irc_channel);
224 d23d2886 2023-07-30 op irc_send_msg(fd, args[0], irc_channel, args[1]);
226 d23d2886 2023-07-30 op case 'c': /* Personal Message */
227 d23d2886 2023-07-30 op irc_send_msg(fd, args[0], irc_nick, args[1]);
229 d23d2886 2023-07-30 op case 'd': /* Status Message */
230 d23d2886 2023-07-30 op if (!strcmp(args[0], "Status") && !strncmp(args[1],
231 d23d2886 2023-07-30 op "You are now in group ", 21)) {
232 d23d2886 2023-07-30 op if (irc_channel[0])
233 d23d2886 2023-07-30 op irc_send_part(fd, irc_nick, irc_channel);
234 d23d2886 2023-07-30 op irc_channel[0] = '#';
236 d23d2886 2023-07-30 op scan(&a, irc_channel + 1, sizeof(irc_channel) - 1,
238 d23d2886 2023-07-30 op irc_send_join(fd, irc_nick, irc_channel);
239 d23d2886 2023-07-30 op icb_send_names(server_fd, irc_channel);
240 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Arrive") ||
241 d23d2886 2023-07-30 op !strcmp(args[0], "Sign-on")) {
242 d23d2886 2023-07-30 op char nick[256], host[256];
244 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
245 d23d2886 2023-07-30 op scan(&a, host, sizeof(host), " (", ")");
246 d23d2886 2023-07-30 op snprintf(s, sizeof(s), "%s!%s", nick, host);
247 d23d2886 2023-07-30 op irc_send_join(fd, s, irc_channel);
248 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Depart")) {
249 d23d2886 2023-07-30 op char nick[256], host[256];
251 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
252 d23d2886 2023-07-30 op scan(&a, host, sizeof(host), " (", ")");
253 d23d2886 2023-07-30 op snprintf(s, sizeof(s), "%s!%s", nick, host);
254 d23d2886 2023-07-30 op irc_send_part(fd, s, irc_channel);
255 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Sign-off")) {
256 d23d2886 2023-07-30 op char nick[256], host[256], reason[256];
258 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
259 d23d2886 2023-07-30 op scan(&a, host, sizeof(host), " (", ")");
260 d23d2886 2023-07-30 op scan(&a, reason, sizeof(reason), " )", "");
261 d23d2886 2023-07-30 op if (strlen(reason) > 0 &&
262 d23d2886 2023-07-30 op reason[strlen(reason) - 1] == '.')
263 d23d2886 2023-07-30 op reason[strlen(reason) - 1] = 0;
264 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s!%s QUIT :%s\r\n",
265 d23d2886 2023-07-30 op nick, host, reason);
266 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
267 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Name")) {
268 d23d2886 2023-07-30 op char old_nick[256], new_nick[256];
270 d23d2886 2023-07-30 op scan(&a, old_nick, sizeof(old_nick), " ", " ");
271 d23d2886 2023-07-30 op if (strncmp(a, " changed nickname to ", 21))
274 d23d2886 2023-07-30 op scan(&a, new_nick, sizeof(new_nick), " ", " ");
275 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s NICK :%s\r\n",
276 d23d2886 2023-07-30 op old_nick, new_nick);
277 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
278 d23d2886 2023-07-30 op if (!strcmp(old_nick, irc_nick))
279 d23d2886 2023-07-30 op strlcpy(irc_nick, new_nick,
280 d23d2886 2023-07-30 op sizeof(irc_nick));
281 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Topic")) {
282 d23d2886 2023-07-30 op char nick[256], topic[256];
284 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
285 d23d2886 2023-07-30 op if (strncmp(a, " changed the topic to \"", 23))
288 d23d2886 2023-07-30 op scan(&a, topic, sizeof(topic), "", "\"");
289 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s TOPIC %s :%s\r\n",
290 d23d2886 2023-07-30 op nick, irc_channel, topic);
291 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
292 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Pass")) {
293 d23d2886 2023-07-30 op char old_mod[256], new_mod[256];
295 d23d2886 2023-07-30 op scan(&a, old_mod, sizeof(old_mod), " ", " ");
296 d23d2886 2023-07-30 op if (!strncmp(a, " has passed moderation to ", 26)) {
298 d23d2886 2023-07-30 op scan(&a, new_mod, sizeof(new_mod), " ", " ");
299 d23d2886 2023-07-30 op snprintf(s, sizeof(s),
300 d23d2886 2023-07-30 op ":%s MODE %s -o+o %s %s\r\n",
301 d23d2886 2023-07-30 op old_mod, irc_channel, old_mod, new_mod);
302 d23d2886 2023-07-30 op } else if (!strcmp(a, " is now mod.")) {
303 d23d2886 2023-07-30 op snprintf(s, sizeof(s),
304 d23d2886 2023-07-30 op ":%s MODE %s +o %s\r\n",
305 d23d2886 2023-07-30 op icb_hostid, irc_channel, old_mod);
308 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
309 d23d2886 2023-07-30 op strlcpy(icb_moderator, new_mod, sizeof(icb_moderator));
310 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "Boot")) {
311 d23d2886 2023-07-30 op char nick[256];
313 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
314 d23d2886 2023-07-30 op if (strcmp(a, " was booted."))
316 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s KICK %s %s :booted\r\n",
317 d23d2886 2023-07-30 op icb_moderator, irc_channel, nick);
318 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
320 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Status Message: %s: %s",
321 d23d2886 2023-07-30 op args[0], args[1]);
323 d23d2886 2023-07-30 op case 'e': /* Error Message */
324 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Error Message: %s", args[0]);
326 d23d2886 2023-07-30 op case 'f': /* Important Message */
327 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Important Message: %s: %s",
328 d23d2886 2023-07-30 op args[0], args[1]);
330 d23d2886 2023-07-30 op case 'g': /* Exit */
331 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Exit");
332 ed972677 2023-08-01 op warnx("server Exit");
333 c8394ca1 2023-08-01 op icbirc_quit();
335 d23d2886 2023-07-30 op case 'i': /* Command Output */
336 d23d2886 2023-07-30 op if (!strcmp(args[0], "co")) {
337 d23d2886 2023-07-30 op for (j = 1; j < i; ++j)
338 d23d2886 2023-07-30 op icb_ico(fd, args[j]);
339 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "wl")) {
340 d23d2886 2023-07-30 op icb_iwl(fd, args[1], args[2], atol(args[3]),
341 d23d2886 2023-07-30 op atol(args[5]), args[6], args[7]);
342 d23d2886 2023-07-30 op } else if (!strcmp(args[0], "wh")) {
343 d23d2886 2023-07-30 op /* display whois header, deprecated */
345 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Command Output: %s: %u args",
346 d23d2886 2023-07-30 op args[0], i - 1);
348 d23d2886 2023-07-30 op case 'j': /* Protocol */
349 d23d2886 2023-07-30 op strlcpy(icb_protolevel, args[0], sizeof(icb_protolevel));
350 d23d2886 2023-07-30 op strlcpy(icb_hostid, args[1], sizeof(icb_hostid));
351 d23d2886 2023-07-30 op strlcpy(icb_serverid, args[2], sizeof(icb_serverid));
353 d23d2886 2023-07-30 op case 'k': /* Beep */
354 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Beep from %s", args[0]);
356 d23d2886 2023-07-30 op case 'l': /* Ping */
357 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Ping '%s'", args[0]);
359 d23d2886 2023-07-30 op case 'm': /* Pong */
360 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Pong '%s'", args[0]);
362 d23d2886 2023-07-30 op case 'n': /* No-op */
363 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB No-op");
366 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB unknown command %d: %u args",
367 d23d2886 2023-07-30 op (int)cmd[0], i);
372 d23d2886 2023-07-30 op icb_iwl(int fd, const char *flags, const char *nick, long idle,
373 d23d2886 2023-07-30 op long signon, const char *ident, const char *host)
375 d23d2886 2023-07-30 op char s[8192];
376 d23d2886 2023-07-30 op int chanop = strchr(flags, 'm') != NULL;
378 d23d2886 2023-07-30 op if (imode == imode_whois && !strcmp(nick, inick)) {
379 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 311 %s %s %s %s * :\r\n",
380 d23d2886 2023-07-30 op icb_hostid, irc_nick, nick, ident, host);
381 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
382 d23d2886 2023-07-30 op if (icurgroup[0]) {
383 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 319 %s %s :%s%s\r\n",
384 d23d2886 2023-07-30 op icb_hostid, irc_nick, nick, chanop ? "@" : "",
386 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
388 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 312 %s %s %s :\r\n",
389 d23d2886 2023-07-30 op icb_hostid, irc_nick, nick, icb_hostid);
390 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
391 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 317 %s %s %ld %ld :seconds idle, "
392 d23d2886 2023-07-30 op "signon time\r\n",
393 d23d2886 2023-07-30 op icb_hostid, irc_nick, nick, idle, signon);
394 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
395 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 318 %s %s :End of /WHOIS list.\r\n",
396 d23d2886 2023-07-30 op icb_hostid, irc_nick, nick);
397 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
398 d23d2886 2023-07-30 op } else if (imode == imode_names && !strcmp(icurgroup, igroup)) {
399 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 353 %s @ %s :%s%s \r\n",
400 d23d2886 2023-07-30 op icb_hostid, irc_nick, icurgroup, chanop ? "@" : "", nick);
401 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
402 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 352 %s %s %s %s %s %s H :5 %s\r\n",
403 d23d2886 2023-07-30 op icb_hostid, irc_nick, icurgroup, nick, host, icb_hostid,
404 d23d2886 2023-07-30 op nick, ident);
405 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
406 d23d2886 2023-07-30 op } else if (imode == imode_who) {
409 d23d2886 2023-07-30 op if (ihostmask[0] == '#')
410 d23d2886 2023-07-30 op match = !strcmp(icurgroup, ihostmask);
412 d23d2886 2023-07-30 op char hostmask[1024];
414 d23d2886 2023-07-30 op snprintf(hostmask, sizeof(hostmask), "%s!%s@%s",
415 d23d2886 2023-07-30 op nick, ident, host);
416 d23d2886 2023-07-30 op match = strstr(hostmask, ihostmask) != NULL;
419 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 352 %s %s %s %s %s %s "
420 d23d2886 2023-07-30 op "H :5 %s\r\n",
421 d23d2886 2023-07-30 op icb_hostid, irc_nick, icurgroup, nick, host,
422 d23d2886 2023-07-30 op icb_hostid, nick, ident);
423 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
427 d23d2886 2023-07-30 op if (chanop && !strcmp(icurgroup, irc_channel))
428 d23d2886 2023-07-30 op strlcpy(icb_moderator, nick, sizeof(icb_moderator));
432 d23d2886 2023-07-30 op icb_ico(int fd, const char *arg)
434 d23d2886 2023-07-30 op char s[8192];
436 d23d2886 2023-07-30 op if (!strncmp(arg, "Group: ", 7)) {
437 d23d2886 2023-07-30 op char group[256];
442 d23d2886 2023-07-30 op group[i++] = '#';
443 d23d2886 2023-07-30 op while (*arg && *arg != ' ')
444 d23d2886 2023-07-30 op group[i++] = *arg++;
445 d23d2886 2023-07-30 op group[i] = 0;
446 d23d2886 2023-07-30 op strlcpy(icurgroup, group, sizeof(icurgroup));
447 d23d2886 2023-07-30 op topic = strstr(arg, "Topic: ");
448 d23d2886 2023-07-30 op if (topic == NULL)
449 d23d2886 2023-07-30 op topic = "(None)";
452 d23d2886 2023-07-30 op if (imode == imode_list) {
453 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 322 %s %s 1 :%s\r\n",
454 d23d2886 2023-07-30 op icb_hostid, irc_nick, group, topic);
455 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
456 d23d2886 2023-07-30 op } else if (imode == imode_names &&
457 d23d2886 2023-07-30 op !strcmp(icurgroup, igroup)) {
458 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 332 %s %s :%s\r\n",
459 d23d2886 2023-07-30 op icb_hostid, irc_nick, icurgroup, topic);
460 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
462 d23d2886 2023-07-30 op } else if (!strncmp(arg, "Total: ", 7)) {
463 d23d2886 2023-07-30 op if (imode == imode_list) {
464 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 323 %s :End of /LIST\r\n",
465 d23d2886 2023-07-30 op icb_hostid, irc_nick);
466 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
467 d23d2886 2023-07-30 op } else if (imode == imode_names) {
468 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 366 %s %s :End of "
469 d23d2886 2023-07-30 op "/NAMES list.\r\n",
470 d23d2886 2023-07-30 op icb_hostid, irc_nick, igroup);
471 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
472 d23d2886 2023-07-30 op } else if (imode == imode_who) {
473 d23d2886 2023-07-30 op snprintf(s, sizeof(s), ":%s 315 %s %s :End of "
474 d23d2886 2023-07-30 op "/WHO list.\r\n",
475 d23d2886 2023-07-30 op icb_hostid, irc_nick, ihostmask);
476 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
478 d23d2886 2023-07-30 op imode = imode_none;
479 d23d2886 2023-07-30 op } else if (strcmp(arg, " "))
480 d23d2886 2023-07-30 op irc_send_notice(fd, "*** Unknown ico: %s", arg);
483 d23d2886 2023-07-30 op #define MAX_MSG_SIZE 246
486 d23d2886 2023-07-30 op icb_send_login(int fd, const char *nick, const char *ident, const char *group)
488 d23d2886 2023-07-30 op char cmd[256];
489 d23d2886 2023-07-30 op unsigned off = 1;
490 d23d2886 2023-07-30 op const char *login_cmd = "login";
492 d23d2886 2023-07-30 op cmd[off++] = 'a';
493 d23d2886 2023-07-30 op while (*ident && off < MAX_MSG_SIZE)
494 d23d2886 2023-07-30 op cmd[off++] = *ident++;
495 d23d2886 2023-07-30 op cmd[off++] = '\001';
496 d23d2886 2023-07-30 op while (*nick && off < MAX_MSG_SIZE)
497 d23d2886 2023-07-30 op cmd[off++] = *nick++;
498 d23d2886 2023-07-30 op cmd[off++] = '\001';
499 d23d2886 2023-07-30 op while (*group && off < MAX_MSG_SIZE)
500 d23d2886 2023-07-30 op cmd[off++] = *group++;
501 d23d2886 2023-07-30 op cmd[off++] = '\001';
502 d23d2886 2023-07-30 op while (*login_cmd)
503 d23d2886 2023-07-30 op cmd[off++] = *login_cmd++;
504 d23d2886 2023-07-30 op cmd[off++] = '\001';
505 d23d2886 2023-07-30 op cmd[off++] = '\001';
506 d23d2886 2023-07-30 op cmd[off++] = '\001';
507 d23d2886 2023-07-30 op cmd[0] = off - 1;
508 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
512 d23d2886 2023-07-30 op icb_send_openmsg(int fd, const char *msg)
514 d23d2886 2023-07-30 op unsigned char cmd[256];
515 d23d2886 2023-07-30 op unsigned off;
517 d23d2886 2023-07-30 op while (*msg) {
519 d23d2886 2023-07-30 op cmd[off++] = 'b';
520 d23d2886 2023-07-30 op while (*msg && off < MAX_MSG_SIZE)
521 d23d2886 2023-07-30 op cmd[off++] = *msg++;
522 d23d2886 2023-07-30 op cmd[off++] = 0;
523 d23d2886 2023-07-30 op cmd[0] = off - 1;
524 d23d2886 2023-07-30 op /* cmd[0] <= MAX_MSG_SIZE */
525 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
530 d23d2886 2023-07-30 op icb_send_privmsg(int fd, const char *nick, const char *msg)
532 d23d2886 2023-07-30 op unsigned char cmd[256];
533 d23d2886 2023-07-30 op unsigned off;
535 d23d2886 2023-07-30 op while (*msg) {
536 d23d2886 2023-07-30 op const char *n = nick;
539 d23d2886 2023-07-30 op cmd[off++] = 'h';
540 d23d2886 2023-07-30 op cmd[off++] = 'm';
541 d23d2886 2023-07-30 op cmd[off++] = '\001';
542 d23d2886 2023-07-30 op while (*n && off < MAX_MSG_SIZE)
543 d23d2886 2023-07-30 op cmd[off++] = *n++;
544 d23d2886 2023-07-30 op cmd[off++] = ' ';
545 d23d2886 2023-07-30 op while (*msg && off < MAX_MSG_SIZE)
546 d23d2886 2023-07-30 op cmd[off++] = *msg++;
547 d23d2886 2023-07-30 op cmd[off++] = 0;
548 d23d2886 2023-07-30 op cmd[0] = off - 1;
549 d23d2886 2023-07-30 op /* cmd[0] <= MAX_MSG_SIZE */
550 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
555 d23d2886 2023-07-30 op icb_send_group(int fd, const char *group)
557 d23d2886 2023-07-30 op char cmd[256];
558 d23d2886 2023-07-30 op unsigned off = 1;
560 d23d2886 2023-07-30 op cmd[off++] = 'h';
561 d23d2886 2023-07-30 op cmd[off++] = 'g';
562 d23d2886 2023-07-30 op cmd[off++] = '\001';
563 d23d2886 2023-07-30 op while (*group && off < MAX_MSG_SIZE)
564 d23d2886 2023-07-30 op cmd[off++] = *group++;
565 d23d2886 2023-07-30 op cmd[off++] = 0;
566 d23d2886 2023-07-30 op cmd[0] = off - 1;
567 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
571 d23d2886 2023-07-30 op icb_send_hw(int fd, const char *arg)
573 d23d2886 2023-07-30 op char cmd[256];
574 d23d2886 2023-07-30 op unsigned off = 1;
576 d23d2886 2023-07-30 op icurgroup[0] = 0;
577 d23d2886 2023-07-30 op cmd[off++] = 'h';
578 d23d2886 2023-07-30 op cmd[off++] = 'w';
579 d23d2886 2023-07-30 op cmd[off++] = '\001';
580 d23d2886 2023-07-30 op while (*arg && off < MAX_MSG_SIZE)
581 d23d2886 2023-07-30 op cmd[off++] = *arg++;
582 d23d2886 2023-07-30 op cmd[off++] = 0;
583 d23d2886 2023-07-30 op cmd[0] = off - 1;
584 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
588 d23d2886 2023-07-30 op icb_send_list(int fd)
590 d23d2886 2023-07-30 op if (imode != imode_none)
592 d23d2886 2023-07-30 op imode = imode_list;
593 d23d2886 2023-07-30 op icb_send_hw(fd, "-g");
597 d23d2886 2023-07-30 op icb_send_names(int fd, const char *group)
599 d23d2886 2023-07-30 op if (imode != imode_none)
601 d23d2886 2023-07-30 op imode = imode_names;
602 d23d2886 2023-07-30 op strlcpy(igroup, group, sizeof(igroup));
603 d23d2886 2023-07-30 op icb_send_hw(fd, "");
607 d23d2886 2023-07-30 op icb_send_whois(int fd, const char *nick)
609 d23d2886 2023-07-30 op if (imode != imode_none)
611 d23d2886 2023-07-30 op imode = imode_whois;
612 d23d2886 2023-07-30 op strlcpy(inick, nick, sizeof(inick));
613 d23d2886 2023-07-30 op icb_send_hw(fd, "");
617 d23d2886 2023-07-30 op icb_send_who(int fd, const char *hostmask)
619 d23d2886 2023-07-30 op if (imode != imode_none)
621 d23d2886 2023-07-30 op imode = imode_who;
622 d23d2886 2023-07-30 op strlcpy(ihostmask, hostmask, sizeof(ihostmask));
623 d23d2886 2023-07-30 op icb_send_hw(fd, "");
627 d23d2886 2023-07-30 op icb_send_pass(int fd, const char *nick)
629 d23d2886 2023-07-30 op char cmd[256];
630 d23d2886 2023-07-30 op unsigned off = 1;
631 d23d2886 2023-07-30 op const char *pass_cmd = "pass";
633 d23d2886 2023-07-30 op cmd[off++] = 'h';
634 d23d2886 2023-07-30 op while (*pass_cmd)
635 d23d2886 2023-07-30 op cmd[off++] = *pass_cmd++;
636 d23d2886 2023-07-30 op cmd[off++] = '\001';
637 d23d2886 2023-07-30 op while (*nick && off < MAX_MSG_SIZE)
638 d23d2886 2023-07-30 op cmd[off++] = *nick++;
639 d23d2886 2023-07-30 op cmd[off++] = 0;
640 d23d2886 2023-07-30 op cmd[0] = off - 1;
641 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
645 d23d2886 2023-07-30 op icb_send_topic(int fd, const char *topic)
647 d23d2886 2023-07-30 op char cmd[256];
648 d23d2886 2023-07-30 op unsigned off = 1;
649 d23d2886 2023-07-30 op const char *topic_cmd = "topic";
651 d23d2886 2023-07-30 op cmd[off++] = 'h';
652 d23d2886 2023-07-30 op while (*topic_cmd)
653 d23d2886 2023-07-30 op cmd[off++] = *topic_cmd++;
654 d23d2886 2023-07-30 op cmd[off++] = '\001';
655 d23d2886 2023-07-30 op while (*topic && off < MAX_MSG_SIZE)
656 d23d2886 2023-07-30 op cmd[off++] = *topic++;
657 d23d2886 2023-07-30 op cmd[off++] = 0;
658 d23d2886 2023-07-30 op cmd[0] = off - 1;
659 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
663 d23d2886 2023-07-30 op icb_send_boot(int fd, const char *nick)
665 d23d2886 2023-07-30 op char cmd[256];
666 d23d2886 2023-07-30 op unsigned off = 1;
667 d23d2886 2023-07-30 op const char *boot_cmd = "boot";
669 d23d2886 2023-07-30 op cmd[off++] = 'h';
670 d23d2886 2023-07-30 op while (*boot_cmd)
671 d23d2886 2023-07-30 op cmd[off++] = *boot_cmd++;
672 d23d2886 2023-07-30 op cmd[off++] = '\001';
673 d23d2886 2023-07-30 op while (*nick && off < MAX_MSG_SIZE)
674 d23d2886 2023-07-30 op cmd[off++] = *nick++;
675 d23d2886 2023-07-30 op cmd[off++] = 0;
676 d23d2886 2023-07-30 op cmd[0] = off - 1;
677 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
681 d23d2886 2023-07-30 op icb_send_name(int fd, const char *nick)
683 d23d2886 2023-07-30 op char cmd[256];
684 d23d2886 2023-07-30 op unsigned off = 1;
685 d23d2886 2023-07-30 op const char *name_cmd = "name";
687 d23d2886 2023-07-30 op cmd[off++] = 'h';
688 d23d2886 2023-07-30 op while (*name_cmd)
689 d23d2886 2023-07-30 op cmd[off++] = *name_cmd++;
690 d23d2886 2023-07-30 op cmd[off++] = '\001';
691 d23d2886 2023-07-30 op while (*nick && off < MAX_MSG_SIZE)
692 d23d2886 2023-07-30 op cmd[off++] = *nick++;
693 d23d2886 2023-07-30 op cmd[off++] = 0;
694 d23d2886 2023-07-30 op cmd[0] = off - 1;
695 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
699 d23d2886 2023-07-30 op icb_send_raw(int fd, const char *data)
701 d23d2886 2023-07-30 op char cmd[256];
702 d23d2886 2023-07-30 op unsigned off = 1;
704 d23d2886 2023-07-30 op while (*data && off < MAX_MSG_SIZE) {
705 d23d2886 2023-07-30 op if (*data == ',')
706 d23d2886 2023-07-30 op cmd[off++] = '\001';
707 d23d2886 2023-07-30 op else if (*data == '\\')
708 d23d2886 2023-07-30 op cmd[off++] = 0;
710 d23d2886 2023-07-30 op cmd[off++] = *data;
713 d23d2886 2023-07-30 op cmd[off++] = 0;
714 d23d2886 2023-07-30 op cmd[0] = off - 1;
715 d23d2886 2023-07-30 op sync_write(fd, cmd, off);
719 d23d2886 2023-07-30 op icb_send_noop(int fd)
721 d23d2886 2023-07-30 op char cmd[256];
722 d23d2886 2023-07-30 op unsigned off = 1;
724 d23d2886 2023-07-30 op cmd[off++] = 'n';
725 d23d2886 2023-07-30 op cmd[off++] = 0;
726 d23d2886 2023-07-30 op cmd[0] = off - 1;
727 d23d2886 2023-07-30 op sync_write(fd, cmd, off);