Blame


1 d23d2886 2023-07-30 op /*
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.
5 d23d2886 2023-07-30 op *
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
8 d23d2886 2023-07-30 op * are met:
9 d23d2886 2023-07-30 op *
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.
16 d23d2886 2023-07-30 op *
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.
29 d23d2886 2023-07-30 op *
30 d23d2886 2023-07-30 op */
31 d23d2886 2023-07-30 op
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>
36 fe009782 2023-08-01 op
37 d23d2886 2023-07-30 op #include "icb.h"
38 d23d2886 2023-07-30 op #include "irc.h"
39 d23d2886 2023-07-30 op
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);
42 d23d2886 2023-07-30 op
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 *);
49 d23d2886 2023-07-30 op
50 fbe4e593 2023-08-01 op int icb_logged_in;
51 d23d2886 2023-07-30 op
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;
63 d23d2886 2023-07-30 op
64 d23d2886 2023-07-30 op /*
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.
69 d23d2886 2023-07-30 op *
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.
74 d23d2886 2023-07-30 op *
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.
78 d23d2886 2023-07-30 op *
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.
84 d23d2886 2023-07-30 op *
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.
88 d23d2886 2023-07-30 op *
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).
92 d23d2886 2023-07-30 op *
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).
98 d23d2886 2023-07-30 op *
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.
107 d23d2886 2023-07-30 op *
108 d23d2886 2023-07-30 op */
109 d23d2886 2023-07-30 op
110 d23d2886 2023-07-30 op void
111 d23d2886 2023-07-30 op scan(const char **s, char *d, size_t siz, const char *skip, const char *term)
112 d23d2886 2023-07-30 op {
113 d23d2886 2023-07-30 op while (**s && strchr(skip, **s) != NULL)
114 d23d2886 2023-07-30 op (*s)++;
115 d23d2886 2023-07-30 op while (**s && strchr(term, **s) == NULL) {
116 d23d2886 2023-07-30 op if (siz > 1) {
117 d23d2886 2023-07-30 op *d++ = **s;
118 d23d2886 2023-07-30 op siz--;
119 d23d2886 2023-07-30 op }
120 d23d2886 2023-07-30 op (*s)++;
121 d23d2886 2023-07-30 op }
122 d23d2886 2023-07-30 op if (siz > 0)
123 7a8dd0b7 2023-09-04 op *d = '\0';
124 d23d2886 2023-07-30 op }
125 d23d2886 2023-07-30 op
126 d23d2886 2023-07-30 op void
127 d23d2886 2023-07-30 op icb_init(void)
128 d23d2886 2023-07-30 op {
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));
138 d23d2886 2023-07-30 op off = 0;
139 d23d2886 2023-07-30 op }
140 d23d2886 2023-07-30 op
141 d23d2886 2023-07-30 op void
142 d23d2886 2023-07-30 op icb_recv(const char *buf, unsigned len, int fd, int server_fd)
143 d23d2886 2023-07-30 op {
144 d23d2886 2023-07-30 op static unsigned char cmd[256];
145 d23d2886 2023-07-30 op
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 */
150 d23d2886 2023-07-30 op len--;
151 d23d2886 2023-07-30 op }
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++;
155 d23d2886 2023-07-30 op len--;
156 d23d2886 2023-07-30 op }
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);
160 d23d2886 2023-07-30 op off = 0;
161 d23d2886 2023-07-30 op }
162 d23d2886 2023-07-30 op }
163 d23d2886 2023-07-30 op }
164 d23d2886 2023-07-30 op
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])
167 d23d2886 2023-07-30 op {
168 d23d2886 2023-07-30 op unsigned char i = 0, j = 0, k = 0;
169 d23d2886 2023-07-30 op
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;
175 d23d2886 2023-07-30 op 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++] = '?';
178 d23d2886 2023-07-30 op else
179 d23d2886 2023-07-30 op args[j][k++] = data[i];
180 d23d2886 2023-07-30 op i++;
181 d23d2886 2023-07-30 op }
182 d23d2886 2023-07-30 op /* i, j, k < 255 */
183 d23d2886 2023-07-30 op if (k > 0)
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;
188 d23d2886 2023-07-30 op return (j);
189 d23d2886 2023-07-30 op }
190 d23d2886 2023-07-30 op
191 d23d2886 2023-07-30 op static void
192 d23d2886 2023-07-30 op icb_cmd(const char *cmd, unsigned char len, int fd, int server_fd)
193 d23d2886 2023-07-30 op {
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];
198 d23d2886 2023-07-30 op
199 d23d2886 2023-07-30 op if (len == 0)
200 d23d2886 2023-07-30 op return;
201 d23d2886 2023-07-30 op
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;
218 d23d2886 2023-07-30 op break;
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);
223 d23d2886 2023-07-30 op }
224 d23d2886 2023-07-30 op irc_send_msg(fd, args[0], irc_channel, args[1]);
225 d23d2886 2023-07-30 op break;
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]);
228 d23d2886 2023-07-30 op break;
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] = '#';
235 d23d2886 2023-07-30 op a += 21;
236 d23d2886 2023-07-30 op scan(&a, irc_channel + 1, sizeof(irc_channel) - 1,
237 d23d2886 2023-07-30 op " ", " ");
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];
243 d23d2886 2023-07-30 op
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];
250 d23d2886 2023-07-30 op
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];
257 d23d2886 2023-07-30 op
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];
269 d23d2886 2023-07-30 op
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))
272 d23d2886 2023-07-30 op return;
273 d23d2886 2023-07-30 op a += 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];
283 d23d2886 2023-07-30 op
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))
286 d23d2886 2023-07-30 op return;
287 d23d2886 2023-07-30 op a += 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];
294 d23d2886 2023-07-30 op
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)) {
297 d23d2886 2023-07-30 op a += 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);
306 d23d2886 2023-07-30 op } else
307 d23d2886 2023-07-30 op return;
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];
312 d23d2886 2023-07-30 op
313 d23d2886 2023-07-30 op scan(&a, nick, sizeof(nick), " ", " ");
314 d23d2886 2023-07-30 op if (strcmp(a, " was booted."))
315 d23d2886 2023-07-30 op return;
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));
319 d23d2886 2023-07-30 op } else
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]);
322 d23d2886 2023-07-30 op break;
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]);
325 d23d2886 2023-07-30 op break;
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]);
329 d23d2886 2023-07-30 op break;
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();
334 d23d2886 2023-07-30 op break;
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 */
344 d23d2886 2023-07-30 op } else
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);
347 d23d2886 2023-07-30 op break;
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));
352 d23d2886 2023-07-30 op break;
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]);
355 d23d2886 2023-07-30 op break;
356 d23d2886 2023-07-30 op case 'l': /* Ping */
357 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Ping '%s'", args[0]);
358 d23d2886 2023-07-30 op break;
359 d23d2886 2023-07-30 op case 'm': /* Pong */
360 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB Pong '%s'", args[0]);
361 d23d2886 2023-07-30 op break;
362 d23d2886 2023-07-30 op case 'n': /* No-op */
363 d23d2886 2023-07-30 op irc_send_notice(fd, "ICB No-op");
364 d23d2886 2023-07-30 op break;
365 d23d2886 2023-07-30 op default:
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);
368 d23d2886 2023-07-30 op }
369 d23d2886 2023-07-30 op }
370 d23d2886 2023-07-30 op
371 d23d2886 2023-07-30 op static void
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)
374 d23d2886 2023-07-30 op {
375 d23d2886 2023-07-30 op char s[8192];
376 d23d2886 2023-07-30 op int chanop = strchr(flags, 'm') != NULL;
377 d23d2886 2023-07-30 op
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 ? "@" : "",
385 d23d2886 2023-07-30 op icurgroup);
386 d23d2886 2023-07-30 op sync_write(fd, s, strlen(s));
387 d23d2886 2023-07-30 op }
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) {
407 d23d2886 2023-07-30 op int match;
408 d23d2886 2023-07-30 op
409 d23d2886 2023-07-30 op if (ihostmask[0] == '#')
410 d23d2886 2023-07-30 op match = !strcmp(icurgroup, ihostmask);
411 d23d2886 2023-07-30 op else {
412 d23d2886 2023-07-30 op char hostmask[1024];
413 d23d2886 2023-07-30 op
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;
417 d23d2886 2023-07-30 op }
418 d23d2886 2023-07-30 op if (match) {
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));
424 d23d2886 2023-07-30 op }
425 d23d2886 2023-07-30 op }
426 d23d2886 2023-07-30 op
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));
429 d23d2886 2023-07-30 op }
430 d23d2886 2023-07-30 op
431 d23d2886 2023-07-30 op static void
432 d23d2886 2023-07-30 op icb_ico(int fd, const char *arg)
433 d23d2886 2023-07-30 op {
434 d23d2886 2023-07-30 op char s[8192];
435 d23d2886 2023-07-30 op
436 d23d2886 2023-07-30 op if (!strncmp(arg, "Group: ", 7)) {
437 d23d2886 2023-07-30 op char group[256];
438 d23d2886 2023-07-30 op int i = 0;
439 d23d2886 2023-07-30 op char *topic;
440 d23d2886 2023-07-30 op
441 d23d2886 2023-07-30 op arg += 7;
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)";
450 d23d2886 2023-07-30 op else
451 d23d2886 2023-07-30 op topic += 7;
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));
461 d23d2886 2023-07-30 op }
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));
477 d23d2886 2023-07-30 op }
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);
481 d23d2886 2023-07-30 op }
482 d23d2886 2023-07-30 op
483 d23d2886 2023-07-30 op #define MAX_MSG_SIZE 246
484 d23d2886 2023-07-30 op
485 d23d2886 2023-07-30 op void
486 d23d2886 2023-07-30 op icb_send_login(int fd, const char *nick, const char *ident, const char *group)
487 d23d2886 2023-07-30 op {
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";
491 d23d2886 2023-07-30 op
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);
509 d23d2886 2023-07-30 op }
510 d23d2886 2023-07-30 op
511 d23d2886 2023-07-30 op void
512 d23d2886 2023-07-30 op icb_send_openmsg(int fd, const char *msg)
513 d23d2886 2023-07-30 op {
514 d23d2886 2023-07-30 op unsigned char cmd[256];
515 d23d2886 2023-07-30 op unsigned off;
516 d23d2886 2023-07-30 op
517 d23d2886 2023-07-30 op while (*msg) {
518 d23d2886 2023-07-30 op off = 1;
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);
526 d23d2886 2023-07-30 op }
527 d23d2886 2023-07-30 op }
528 d23d2886 2023-07-30 op
529 d23d2886 2023-07-30 op void
530 d23d2886 2023-07-30 op icb_send_privmsg(int fd, const char *nick, const char *msg)
531 d23d2886 2023-07-30 op {
532 d23d2886 2023-07-30 op unsigned char cmd[256];
533 d23d2886 2023-07-30 op unsigned off;
534 d23d2886 2023-07-30 op
535 d23d2886 2023-07-30 op while (*msg) {
536 d23d2886 2023-07-30 op const char *n = nick;
537 d23d2886 2023-07-30 op
538 d23d2886 2023-07-30 op off = 1;
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);
551 d23d2886 2023-07-30 op }
552 d23d2886 2023-07-30 op }
553 d23d2886 2023-07-30 op
554 d23d2886 2023-07-30 op void
555 d23d2886 2023-07-30 op icb_send_group(int fd, const char *group)
556 d23d2886 2023-07-30 op {
557 d23d2886 2023-07-30 op char cmd[256];
558 d23d2886 2023-07-30 op unsigned off = 1;
559 d23d2886 2023-07-30 op
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);
568 d23d2886 2023-07-30 op }
569 d23d2886 2023-07-30 op
570 d23d2886 2023-07-30 op static void
571 d23d2886 2023-07-30 op icb_send_hw(int fd, const char *arg)
572 d23d2886 2023-07-30 op {
573 d23d2886 2023-07-30 op char cmd[256];
574 d23d2886 2023-07-30 op unsigned off = 1;
575 d23d2886 2023-07-30 op
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);
585 d23d2886 2023-07-30 op }
586 d23d2886 2023-07-30 op
587 d23d2886 2023-07-30 op void
588 d23d2886 2023-07-30 op icb_send_list(int fd)
589 d23d2886 2023-07-30 op {
590 d23d2886 2023-07-30 op if (imode != imode_none)
591 d23d2886 2023-07-30 op return;
592 d23d2886 2023-07-30 op imode = imode_list;
593 d23d2886 2023-07-30 op icb_send_hw(fd, "-g");
594 d23d2886 2023-07-30 op }
595 d23d2886 2023-07-30 op
596 d23d2886 2023-07-30 op void
597 d23d2886 2023-07-30 op icb_send_names(int fd, const char *group)
598 d23d2886 2023-07-30 op {
599 d23d2886 2023-07-30 op if (imode != imode_none)
600 d23d2886 2023-07-30 op return;
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, "");
604 d23d2886 2023-07-30 op }
605 d23d2886 2023-07-30 op
606 d23d2886 2023-07-30 op void
607 d23d2886 2023-07-30 op icb_send_whois(int fd, const char *nick)
608 d23d2886 2023-07-30 op {
609 d23d2886 2023-07-30 op if (imode != imode_none)
610 d23d2886 2023-07-30 op return;
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, "");
614 d23d2886 2023-07-30 op }
615 d23d2886 2023-07-30 op
616 d23d2886 2023-07-30 op void
617 d23d2886 2023-07-30 op icb_send_who(int fd, const char *hostmask)
618 d23d2886 2023-07-30 op {
619 d23d2886 2023-07-30 op if (imode != imode_none)
620 d23d2886 2023-07-30 op return;
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, "");
624 d23d2886 2023-07-30 op }
625 d23d2886 2023-07-30 op
626 d23d2886 2023-07-30 op void
627 d23d2886 2023-07-30 op icb_send_pass(int fd, const char *nick)
628 d23d2886 2023-07-30 op {
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";
632 d23d2886 2023-07-30 op
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);
642 d23d2886 2023-07-30 op }
643 d23d2886 2023-07-30 op
644 d23d2886 2023-07-30 op void
645 d23d2886 2023-07-30 op icb_send_topic(int fd, const char *topic)
646 d23d2886 2023-07-30 op {
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";
650 d23d2886 2023-07-30 op
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);
660 d23d2886 2023-07-30 op }
661 d23d2886 2023-07-30 op
662 d23d2886 2023-07-30 op void
663 d23d2886 2023-07-30 op icb_send_boot(int fd, const char *nick)
664 d23d2886 2023-07-30 op {
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";
668 d23d2886 2023-07-30 op
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);
678 d23d2886 2023-07-30 op }
679 d23d2886 2023-07-30 op
680 d23d2886 2023-07-30 op void
681 d23d2886 2023-07-30 op icb_send_name(int fd, const char *nick)
682 d23d2886 2023-07-30 op {
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";
686 d23d2886 2023-07-30 op
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);
696 d23d2886 2023-07-30 op }
697 d23d2886 2023-07-30 op
698 d23d2886 2023-07-30 op void
699 d23d2886 2023-07-30 op icb_send_raw(int fd, const char *data)
700 d23d2886 2023-07-30 op {
701 d23d2886 2023-07-30 op char cmd[256];
702 d23d2886 2023-07-30 op unsigned off = 1;
703 d23d2886 2023-07-30 op
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;
709 d23d2886 2023-07-30 op else
710 d23d2886 2023-07-30 op cmd[off++] = *data;
711 d23d2886 2023-07-30 op data++;
712 d23d2886 2023-07-30 op }
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);
716 d23d2886 2023-07-30 op }
717 d23d2886 2023-07-30 op
718 d23d2886 2023-07-30 op void
719 d23d2886 2023-07-30 op icb_send_noop(int fd)
720 d23d2886 2023-07-30 op {
721 d23d2886 2023-07-30 op char cmd[256];
722 d23d2886 2023-07-30 op unsigned off = 1;
723 d23d2886 2023-07-30 op
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);
728 d23d2886 2023-07-30 op }