1 | /*
|
---|
2 | * Copyright (c) 2009, 2010, 2013, 2014 Mike Belopuhov
|
---|
3 | *
|
---|
4 | * Permission to use, copy, modify, and distribute this software for any
|
---|
5 | * purpose with or without fee is hereby granted, provided that the above
|
---|
6 | * copyright notice and this permission notice appear in all copies.
|
---|
7 | *
|
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <sys/types.h>
|
---|
18 | #include <sys/queue.h>
|
---|
19 | #include <netdb.h>
|
---|
20 | #include <stdarg.h>
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <string.h>
|
---|
24 | #include <syslog.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <event.h>
|
---|
28 | #include <vis.h>
|
---|
29 |
|
---|
30 | #include "icb.h"
|
---|
31 | #include "icbd.h"
|
---|
32 |
|
---|
33 | struct icb_grplist groups;
|
---|
34 |
|
---|
35 | extern int creategroups;
|
---|
36 | extern char srvname[NI_MAXHOST];
|
---|
37 |
|
---|
38 | void icb_command(struct icb_session *, char *, char *);
|
---|
39 | void icb_groupmsg(struct icb_session *, char *);
|
---|
40 | int icb_login(struct icb_session *, char *, char *, char *);
|
---|
41 | int icb_dowho(struct icb_session *, struct icb_group *);
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * icb_init: initialize server name and a list of groups
|
---|
45 | */
|
---|
46 | void
|
---|
47 | icb_init(void)
|
---|
48 | {
|
---|
49 | LIST_INIT(&groups);
|
---|
50 |
|
---|
51 | if (strlen(srvname) == 0)
|
---|
52 | (void)gethostname(srvname, sizeof srvname);
|
---|
53 | /*
|
---|
54 | * NI_MAXHOST is usually greater than what we
|
---|
55 | * can actually send, hence truncation:
|
---|
56 | */
|
---|
57 | if (strlen(srvname) > 200)
|
---|
58 | srvname[200] = '\0';
|
---|
59 | }
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * icb_start: called upon accepting a new connection, greets new client
|
---|
63 | */
|
---|
64 | void
|
---|
65 | icb_start(struct icb_session *is)
|
---|
66 | {
|
---|
67 | icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
|
---|
68 | ICB_M_SEP, "icbd");
|
---|
69 | SETF(is->flags, ICB_SF_PROTOSENT);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * icb_input: main input processing routine
|
---|
74 | */
|
---|
75 | int
|
---|
76 | icb_input(struct icb_session *is)
|
---|
77 | {
|
---|
78 | char *msg = is->buffer;
|
---|
79 | int msglen = is->length;
|
---|
80 | unsigned char type;
|
---|
81 | char *wptr = NULL;
|
---|
82 | int res = 0;
|
---|
83 |
|
---|
84 | is->last = getmonotime();
|
---|
85 | type = msg[0];
|
---|
86 | msg++;
|
---|
87 | if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
|
---|
88 | icb_error(is, "Not logged in");
|
---|
89 | return (0);
|
---|
90 | }
|
---|
91 | switch (type) {
|
---|
92 | case ICB_M_LOGIN: {
|
---|
93 | char client[ICB_MAXNICKLEN];
|
---|
94 | char nick[ICB_MAXNICKLEN];
|
---|
95 | char group[ICB_MAXGRPLEN];
|
---|
96 | char cmd[ICB_MAXCMDLEN];
|
---|
97 |
|
---|
98 | memset(client, 0, sizeof client);
|
---|
99 | if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN,
|
---|
100 | ICB_M_SEP, 1) < 0) {
|
---|
101 | icb_error(is, "Invalid client");
|
---|
102 | icbd_drop(is, NULL);
|
---|
103 | return (1);
|
---|
104 | }
|
---|
105 | memset(nick, 0, sizeof nick);
|
---|
106 | if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN,
|
---|
107 | ICB_M_SEP, 1) <= 0) {
|
---|
108 | icb_error(is, "Invalid nick");
|
---|
109 | icbd_drop(is, NULL);
|
---|
110 | return (1);
|
---|
111 | }
|
---|
112 | memset(group, 0, sizeof group);
|
---|
113 | if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN,
|
---|
114 | ICB_M_SEP, 1) < 0) {
|
---|
115 | icb_error(is, "Invalid login group");
|
---|
116 | icbd_drop(is, NULL);
|
---|
117 | return (1);
|
---|
118 | }
|
---|
119 | memset(cmd, 0, sizeof cmd);
|
---|
120 | if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
|
---|
121 | ICB_M_SEP, 1) < 0) {
|
---|
122 | icb_error(is, "Invalid command");
|
---|
123 | icbd_drop(is, NULL);
|
---|
124 | return (1);
|
---|
125 | }
|
---|
126 | if (strlen(cmd) > 0 && cmd[0] == 'w') {
|
---|
127 | icb_error(is, "Command not implemented");
|
---|
128 | icbd_drop(is, NULL);
|
---|
129 | return (1);
|
---|
130 | }
|
---|
131 | if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
|
---|
132 | icb_error(is, "Malformed login packet");
|
---|
133 | icbd_drop(is, NULL);
|
---|
134 | return (1);
|
---|
135 | }
|
---|
136 | res = icb_login(is, group, nick, client);
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | case ICB_M_OPEN: {
|
---|
140 | icb_groupmsg(is, msg);
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | case ICB_M_COMMAND: {
|
---|
144 | char cmd[ICB_MAXCMDLEN];
|
---|
145 |
|
---|
146 | memset(cmd, 0, sizeof cmd);
|
---|
147 | if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
|
---|
148 | ICB_M_SEP, 1) <= 0) {
|
---|
149 | icb_error(is, "Invalid command");
|
---|
150 | icbd_drop(is, NULL);
|
---|
151 | return (1);
|
---|
152 | }
|
---|
153 | msg = wptr;
|
---|
154 | icb_command(is, cmd, msg);
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | case ICB_M_PONG: {
|
---|
158 | icb_sendfmt(is, "%c", ICB_M_PING);
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case ICB_M_PROTO:
|
---|
162 | case ICB_M_NOOP:
|
---|
163 | /* ignore */
|
---|
164 | break;
|
---|
165 | default:
|
---|
166 | /* everything else is not valid */
|
---|
167 | icb_error(is, "Undefined message type %u", type);
|
---|
168 | }
|
---|
169 | return (res);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * icb_login: handles login ('a') packets
|
---|
174 | */
|
---|
175 | int
|
---|
176 | icb_login(struct icb_session *is, char *grp, char *nick, char *client)
|
---|
177 | {
|
---|
178 | const char *defgrp = "1";
|
---|
179 | struct icb_group *ig;
|
---|
180 | struct icb_session *s;
|
---|
181 | char group[ICB_MAXGRPLEN];
|
---|
182 |
|
---|
183 | if (!nick || strlen(nick) == 0 ||
|
---|
184 | icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
|
---|
185 | icb_error(is, "Invalid nick");
|
---|
186 | icbd_drop(is, NULL);
|
---|
187 | return (1);
|
---|
188 | }
|
---|
189 | if (!grp || strlen(grp) == 0)
|
---|
190 | strlcpy(group, defgrp, ICB_MAXGRPLEN);
|
---|
191 | else
|
---|
192 | icb_vis(group, grp, ICB_MAXGRPLEN, VIS_SP);
|
---|
193 | LIST_FOREACH(ig, &groups, entry) {
|
---|
194 | if (strcmp(ig->name, group) == 0)
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | if (ig == NULL) {
|
---|
198 | if (!creategroups) {
|
---|
199 | icb_error(is, "Can't create new groups", group);
|
---|
200 | icbd_drop(is, NULL);
|
---|
201 | return (1);
|
---|
202 | } else {
|
---|
203 | if ((ig = icb_addgroup(is, group)) == NULL) {
|
---|
204 | icb_error(is, "Can't create group %s", group);
|
---|
205 | return (0);
|
---|
206 | }
|
---|
207 | icbd_log(NULL, LOG_DEBUG, "%s created group %s",
|
---|
208 | is->nick, group);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
212 | if (strcmp(s->nick, is->nick) == 0) {
|
---|
213 | icb_error(is, "Nick is already in use");
|
---|
214 | icbd_drop(is, NULL);
|
---|
215 | return (1);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (client && strlen(client) > 0)
|
---|
220 | icb_vis(is->client, client, sizeof is->client, VIS_SP);
|
---|
221 | else
|
---|
222 | strlcpy(is->client, is->nick, sizeof is->client);
|
---|
223 | is->group = ig;
|
---|
224 | is->login = time(NULL);
|
---|
225 | is->last = getmonotime();
|
---|
226 |
|
---|
227 | /* notify group */
|
---|
228 | icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
|
---|
229 | is->nick, is->client, is->host);
|
---|
230 |
|
---|
231 | CLRF(is->flags, ICB_SF_PROTOSENT);
|
---|
232 | SETF(is->flags, ICB_SF_LOGGEDIN);
|
---|
233 |
|
---|
234 | LIST_INSERT_HEAD(&ig->sess, is, entry);
|
---|
235 |
|
---|
236 | /* acknowledge successful login */
|
---|
237 | icb_sendfmt(is, "%c", ICB_M_LOGIN);
|
---|
238 |
|
---|
239 | /* notify user */
|
---|
240 | icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
|
---|
241 | icb_ismod(ig, is) ? " as moderator" : "");
|
---|
242 |
|
---|
243 | /* send user a topic name */
|
---|
244 | if (strlen(ig->topic) > 0)
|
---|
245 | icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
|
---|
246 | ig->name, ig->topic);
|
---|
247 | return (0);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * icb_groupmsg: handles open message ('b') packets
|
---|
252 | */
|
---|
253 | void
|
---|
254 | icb_groupmsg(struct icb_session *is, char *msg)
|
---|
255 | {
|
---|
256 | char buf[ICB_MSGSIZE];
|
---|
257 | struct icb_group *ig = is->group;
|
---|
258 | struct icb_session *s;
|
---|
259 | int res, buflen;
|
---|
260 |
|
---|
261 | if (strlen(msg) == 0) {
|
---|
262 | icb_error(is, "Empty message");
|
---|
263 | return;
|
---|
264 | }
|
---|
265 |
|
---|
266 | do {
|
---|
267 | res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
|
---|
268 | is->nick, ICB_M_SEP, msg);
|
---|
269 | if (res < 0) {
|
---|
270 | icbd_log(is, LOG_ERR, "Format error in %s", __func__);
|
---|
271 | return;
|
---|
272 | }
|
---|
273 | /* res doesn't include the terminating NUL */
|
---|
274 | buflen = MIN((size_t)res + 1, sizeof buf - 1);
|
---|
275 | buf[0] = buflen;
|
---|
276 |
|
---|
277 | logger(ig->name, is->nick, msg);
|
---|
278 |
|
---|
279 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
280 | if (s == is)
|
---|
281 | continue;
|
---|
282 | icbd_send(s, buf, buflen + 1);
|
---|
283 | }
|
---|
284 |
|
---|
285 | msg += buflen - 1;
|
---|
286 | } while (res > buflen - 1);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * icb_privmsg: handles personal message ('c') packets
|
---|
291 | */
|
---|
292 | void
|
---|
293 | icb_privmsg(struct icb_session *is, char *to, char *msg)
|
---|
294 | {
|
---|
295 | struct icb_group *ig = is->group;
|
---|
296 | struct icb_session *s;
|
---|
297 | char buf[ICB_MSGSIZE];
|
---|
298 | char whom[ICB_MAXNICKLEN];
|
---|
299 | int res, buflen;
|
---|
300 |
|
---|
301 | icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
|
---|
302 |
|
---|
303 | /* try home group first */
|
---|
304 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
305 | if (strcmp(s->nick, whom) == 0)
|
---|
306 | break;
|
---|
307 | }
|
---|
308 | if (!s) {
|
---|
309 | /* try all groups until the first match */
|
---|
310 | LIST_FOREACH(ig, &groups, entry) {
|
---|
311 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
312 | if (strcmp(s->nick, whom) == 0)
|
---|
313 | break;
|
---|
314 | }
|
---|
315 | if (s)
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | if (!s) {
|
---|
319 | icb_error(is, "No such user %s", whom);
|
---|
320 | return;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | do {
|
---|
325 | res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s",
|
---|
326 | ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
|
---|
327 | if (res < 0) {
|
---|
328 | icbd_log(is, LOG_ERR, "Format error in %s", __func__);
|
---|
329 | return;
|
---|
330 | }
|
---|
331 | /* res doesn't include the terminating NUL */
|
---|
332 | buflen = MIN((size_t)res + 1, sizeof buf - 1);
|
---|
333 | buf[0] = buflen;
|
---|
334 |
|
---|
335 | icbd_send(s, buf, buflen + 1);
|
---|
336 |
|
---|
337 | msg += buflen - 1;
|
---|
338 | } while (res > buflen - 1);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * icb_command: handles command ('h') packets
|
---|
343 | */
|
---|
344 | void
|
---|
345 | icb_command(struct icb_session *is, char *cmd, char *arg)
|
---|
346 | {
|
---|
347 | void (*handler)(struct icb_session *, char *);
|
---|
348 | char command[ICB_MAXCMDLEN];
|
---|
349 |
|
---|
350 | icb_vis(command, cmd, sizeof command, VIS_SP);
|
---|
351 |
|
---|
352 | if ((handler = icb_cmd_lookup(command)) == NULL) {
|
---|
353 | icb_error(is, "Unsupported command: %s", command);
|
---|
354 | return;
|
---|
355 | }
|
---|
356 | handler(is, arg);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * icb_cmdout: sends out command output ('i') packets, called from the
|
---|
361 | * command handlers
|
---|
362 | */
|
---|
363 | void
|
---|
364 | icb_cmdout(struct icb_session *is, int type, char *outmsg)
|
---|
365 | {
|
---|
366 | char *otype = NULL;
|
---|
367 |
|
---|
368 | switch (type) {
|
---|
369 | case CMDOUT_CO:
|
---|
370 | otype = "co";
|
---|
371 | break;
|
---|
372 | case CMDOUT_EC:
|
---|
373 | otype = "ec";
|
---|
374 | break;
|
---|
375 | case CMDOUT_WG:
|
---|
376 | otype = "wg";
|
---|
377 | break;
|
---|
378 | case CMDOUT_WH:
|
---|
379 | otype = "wh";
|
---|
380 | break;
|
---|
381 | case CMDOUT_WL:
|
---|
382 | otype = "wl";
|
---|
383 | break;
|
---|
384 | default:
|
---|
385 | icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
|
---|
386 | return;
|
---|
387 | }
|
---|
388 | if (outmsg)
|
---|
389 | icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
|
---|
390 | outmsg);
|
---|
391 | else
|
---|
392 | icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * icb_status: sends a status message ('d') to the client
|
---|
397 | */
|
---|
398 | void
|
---|
399 | icb_status(struct icb_session *is, int type, const char *fmt, ...)
|
---|
400 | {
|
---|
401 | va_list ap;
|
---|
402 | char buf[ICB_MSGSIZE];
|
---|
403 | int res, buflen = 1;
|
---|
404 | static const struct {
|
---|
405 | int type;
|
---|
406 | const char *msg;
|
---|
407 | } msgtab[] = {
|
---|
408 | { STATUS_ARRIVE, "Arrive" },
|
---|
409 | { STATUS_BOOT, "Boot" },
|
---|
410 | { STATUS_DEPART, "Depart" },
|
---|
411 | { STATUS_HELP, "Help" },
|
---|
412 | { STATUS_NAME, "Name" },
|
---|
413 | { STATUS_NOBEEP, "No-Beep" },
|
---|
414 | { STATUS_NOTIFY, "Notify" },
|
---|
415 | { STATUS_SIGNON, "Sign-on" },
|
---|
416 | { STATUS_SIGNOFF, "Sign-off" },
|
---|
417 | { STATUS_STATUS, "Status" },
|
---|
418 | { STATUS_TOPIC, "Topic" },
|
---|
419 | { STATUS_WARNING, "Warning" },
|
---|
420 | { 0, NULL }
|
---|
421 | };
|
---|
422 |
|
---|
423 | if (type < 0 || type > (int)nitems(msgtab) - 1)
|
---|
424 | return;
|
---|
425 | res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
|
---|
426 | msgtab[type].msg, ICB_M_SEP);
|
---|
427 | if (res < 0) {
|
---|
428 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
429 | return;
|
---|
430 | }
|
---|
431 | buflen += MIN((size_t)res, sizeof buf - 1);
|
---|
432 | if ((size_t)buflen >= sizeof buf) {
|
---|
433 | icbd_log(NULL, LOG_ERR, "Status buffer too small");
|
---|
434 | return;
|
---|
435 | }
|
---|
436 | va_start(ap, fmt);
|
---|
437 | res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
|
---|
438 | va_end(ap);
|
---|
439 | if (res < 0) {
|
---|
440 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
441 | return;
|
---|
442 | }
|
---|
443 | buflen += MIN((size_t)res, sizeof buf - buflen);
|
---|
444 | buf[0] = buflen;
|
---|
445 | icbd_send(is, buf, buflen + 1);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * icb_status: sends a status message ('d') to the group except of the
|
---|
450 | * "ex" if it's not NULL
|
---|
451 | */
|
---|
452 | void
|
---|
453 | icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
|
---|
454 | const char *fmt, ...)
|
---|
455 | {
|
---|
456 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
457 | va_list ap;
|
---|
458 | struct icb_session *s;
|
---|
459 |
|
---|
460 | va_start(ap, fmt);
|
---|
461 | (void)vsnprintf(buf, sizeof buf, fmt, ap);
|
---|
462 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
463 | if (ex && s == ex)
|
---|
464 | continue;
|
---|
465 | icb_status(s, type, buf);
|
---|
466 | }
|
---|
467 | logger(ig->name, "", buf);
|
---|
468 | icbd_log(NULL, LOG_DEBUG, "%s", buf);
|
---|
469 | va_end(ap);
|
---|
470 | }
|
---|
471 |
|
---|
472 | /*
|
---|
473 | * icb_error: sends an error message ('e') to the client
|
---|
474 | */
|
---|
475 | void
|
---|
476 | icb_error(struct icb_session *is, const char *fmt, ...)
|
---|
477 | {
|
---|
478 | char buf[ICB_MSGSIZE];
|
---|
479 | va_list ap;
|
---|
480 | int res, buflen = 1;
|
---|
481 |
|
---|
482 | va_start(ap, fmt);
|
---|
483 | res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
|
---|
484 | va_end(ap);
|
---|
485 | if (res < 0) {
|
---|
486 | icbd_log(NULL, LOG_ERR, "Format error");
|
---|
487 | return;
|
---|
488 | }
|
---|
489 | buflen += MIN((size_t)res, sizeof buf - 2);
|
---|
490 | buf[0] = ++buflen; /* account for ICB_M_ERROR */
|
---|
491 | buf[1] = ICB_M_ERROR;
|
---|
492 | icbd_send(is, buf, buflen + 1);
|
---|
493 | icbd_log(is, LOG_DEBUG, "%s", buf + 2);
|
---|
494 | }
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * icb_remove: removes a session from the associated group
|
---|
498 | */
|
---|
499 | void
|
---|
500 | icb_remove(struct icb_session *is, char *reason)
|
---|
501 | {
|
---|
502 | if (is->group) {
|
---|
503 | if (icb_ismod(is->group, is))
|
---|
504 | (void)icb_pass(is->group, is, NULL);
|
---|
505 | LIST_REMOVE(is, entry);
|
---|
506 | if (reason)
|
---|
507 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
508 | "%s (%s@%s) just left: %s", is->nick, is->client,
|
---|
509 | is->host, reason);
|
---|
510 | else
|
---|
511 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
512 | "%s (%s@%s) just left", is->nick, is->client,
|
---|
513 | is->host);
|
---|
514 | is->group = NULL;
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * icb_addgroup: adds a new group to the list
|
---|
520 | */
|
---|
521 | struct icb_group *
|
---|
522 | icb_addgroup(struct icb_session *is, char *name)
|
---|
523 | {
|
---|
524 | struct icb_group *ig;
|
---|
525 |
|
---|
526 | if ((ig = calloc(1, sizeof *ig)) == NULL)
|
---|
527 | return (NULL);
|
---|
528 | strlcpy(ig->name, name, sizeof ig->name);
|
---|
529 | if (is)
|
---|
530 | ig->mod = is;
|
---|
531 | LIST_INIT(&ig->sess);
|
---|
532 | LIST_INSERT_HEAD(&groups, ig, entry);
|
---|
533 | return (ig);
|
---|
534 | }
|
---|
535 |
|
---|
536 | #ifdef notused
|
---|
537 | /*
|
---|
538 | * icb_delgroup: removes a group from the list
|
---|
539 | */
|
---|
540 | void
|
---|
541 | icb_delgroup(struct icb_group *ig)
|
---|
542 | {
|
---|
543 | struct icb_session *s;
|
---|
544 |
|
---|
545 | /* well, i guess we should kick out participants! ;-) */
|
---|
546 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
547 | icb_status(s, STATUS_WARNING, "Group dismissed");
|
---|
548 | s->group = NULL;
|
---|
549 | }
|
---|
550 | LIST_REMOVE(ig, entry);
|
---|
551 | bzero(ig, sizeof ig); /* paranoic thing, obviously */
|
---|
552 | free(ig);
|
---|
553 | }
|
---|
554 | #endif
|
---|
555 |
|
---|
556 | /*
|
---|
557 | * icb_dowho: a helper function that sends out a group header as a command
|
---|
558 | * output and user information in the "wl" format
|
---|
559 | */
|
---|
560 | int
|
---|
561 | icb_dowho(struct icb_session *is, struct icb_group *ig)
|
---|
562 | {
|
---|
563 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
564 | struct icb_session *s;
|
---|
565 | time_t now;
|
---|
566 | int nusers = 0;
|
---|
567 |
|
---|
568 | now = getmonotime();
|
---|
569 | icb_cmdout(is, CMDOUT_CO, " ");
|
---|
570 | snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
|
---|
571 | ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
|
---|
572 | strlen(ig->topic) > 0 ? ig->topic : "(None)");
|
---|
573 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
574 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
575 | (void)snprintf(buf, sizeof buf,
|
---|
576 | "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
|
---|
577 | icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
|
---|
578 | s->nick, ICB_M_SEP, now - s->last,
|
---|
579 | ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
|
---|
580 | s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
|
---|
581 | icb_cmdout(is, CMDOUT_WL, buf);
|
---|
582 | nusers++;
|
---|
583 | }
|
---|
584 | return (nusers);
|
---|
585 | }
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * icb_who: sends a list of users of either the specified group or all
|
---|
589 | * groups found on the server
|
---|
590 | */
|
---|
591 | void
|
---|
592 | icb_who(struct icb_session *is, struct icb_group *ig)
|
---|
593 | {
|
---|
594 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
595 | struct icb_group *g;
|
---|
596 |
|
---|
597 | if (!ig) {
|
---|
598 | int nusers = 0, ngroups = 0;
|
---|
599 |
|
---|
600 | LIST_FOREACH(g, &groups, entry) {
|
---|
601 | nusers += icb_dowho(is, g);
|
---|
602 | ngroups++;
|
---|
603 | }
|
---|
604 | if (nusers > 0) {
|
---|
605 | (void)snprintf(buf, sizeof buf,
|
---|
606 | "Total: %d %s in %d %s",
|
---|
607 | nusers, nusers > 1 ? "users" : "user",
|
---|
608 | ngroups, ngroups > 1 ? "groups" : "group");
|
---|
609 | } else
|
---|
610 | (void)snprintf(buf, sizeof buf, "No users found.");
|
---|
611 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
612 | } else
|
---|
613 | (void)icb_dowho(is, ig);
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * icb_ismod: checks whether group is moderated by "is"
|
---|
618 | */
|
---|
619 | int
|
---|
620 | icb_ismod(struct icb_group *ig, struct icb_session *is)
|
---|
621 | {
|
---|
622 | return (ig->mod == is);
|
---|
623 | }
|
---|
624 |
|
---|
625 | /*
|
---|
626 | * icb_modpermit: checks user against the moderators table if it has
|
---|
627 | * been populated
|
---|
628 | */
|
---|
629 | int
|
---|
630 | icb_modpermit(struct icb_session *is, int enforce)
|
---|
631 | {
|
---|
632 | extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
|
---|
633 | extern int modtabcnt;
|
---|
634 |
|
---|
635 | icbd_modupdate();
|
---|
636 | if ((enforce ? 0 : modtabcnt == 0) ||
|
---|
637 | bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
|
---|
638 | (int (*)(const void *, const void *))strcmp))
|
---|
639 | return (1);
|
---|
640 | return (0);
|
---|
641 | }
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * icb_pass: passes moderation of group "ig" from "from" to "to",
|
---|
645 | * returns -1 if "from" is not a moderator, 1 if passed
|
---|
646 | * to "to" and 0 otherwise (no moderator or passed to the
|
---|
647 | * internal bot)
|
---|
648 | */
|
---|
649 | int
|
---|
650 | icb_pass(struct icb_group *ig, struct icb_session *from,
|
---|
651 | struct icb_session *to)
|
---|
652 | {
|
---|
653 | if (ig->mod && ig->mod != from)
|
---|
654 | return (-1);
|
---|
655 | if (!from && !to)
|
---|
656 | return (-1);
|
---|
657 | ig->mod = to;
|
---|
658 | if (to)
|
---|
659 | icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
|
---|
660 | " of %s", from ? from->nick : "server", ig->name);
|
---|
661 | icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
|
---|
662 | "to %s", from ? from->nick : "server", to ? to->nick : "server");
|
---|
663 | return (1);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * icb_sendfmt: formats a string and sends it over
|
---|
668 | */
|
---|
669 | void
|
---|
670 | icb_sendfmt(struct icb_session *is, const char *fmt, ...)
|
---|
671 | {
|
---|
672 | char buf[ICB_MSGSIZE];
|
---|
673 | va_list ap;
|
---|
674 | int res, buflen;
|
---|
675 |
|
---|
676 | va_start(ap, fmt);
|
---|
677 | res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
|
---|
678 | va_end(ap);
|
---|
679 | if (res < 0) {
|
---|
680 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
681 | return;
|
---|
682 | }
|
---|
683 | buflen = MIN((size_t)res + 1, sizeof buf - 1);
|
---|
684 | buf[0] = buflen;
|
---|
685 | icbd_send(is, buf, buflen + 1);
|
---|
686 | }
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * icb_token: copies a sequence of characters delimited by the 'sep' character
|
---|
690 | * from the source buffer 'buf' at offset indicated by 'bufptr' to
|
---|
691 | * the destination buffer 'dst' and sets 'bufptr' to the next byte
|
---|
692 | * after 'sep'.
|
---|
693 | */
|
---|
694 | int
|
---|
695 | icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
|
---|
696 | int trim)
|
---|
697 | {
|
---|
698 | char *start;
|
---|
699 | int i, ret;
|
---|
700 |
|
---|
701 | if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
|
---|
702 | return (0);
|
---|
703 | if (*bufptr == NULL)
|
---|
704 | *bufptr = buf;
|
---|
705 | start = *bufptr;
|
---|
706 | for (i = *bufptr - buf; i < len; i++, (*bufptr)++) {
|
---|
707 | if (**bufptr == sep || **bufptr == '\0') {
|
---|
708 | /* copy and null terminate the token */
|
---|
709 | ret = strlcpy(dst, start,
|
---|
710 | MIN(*bufptr - start + 1, dstlen));
|
---|
711 | if (**bufptr != '\0')
|
---|
712 | (*bufptr)++;
|
---|
713 | if (ret > 0 && trim)
|
---|
714 | ret = icb_trim(dst, dstlen);
|
---|
715 | return (ret);
|
---|
716 | }
|
---|
717 | }
|
---|
718 | /*
|
---|
719 | * Reached the end of the buffer without finding a field separator
|
---|
720 | * nor the end of line character. If we have advanced our pointer
|
---|
721 | * we should copy the resulting single field out.
|
---|
722 | */
|
---|
723 | if (*bufptr - start > 0) {
|
---|
724 | ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
|
---|
725 | if (ret > 0 && trim)
|
---|
726 | ret = icb_trim(dst, dstlen);
|
---|
727 | return (ret);
|
---|
728 | }
|
---|
729 | return (0);
|
---|
730 | }
|
---|
731 |
|
---|
732 | /*
|
---|
733 | * icb_trim: trims trailing whitespace
|
---|
734 | */
|
---|
735 | int
|
---|
736 | icb_trim(char *buf, int len)
|
---|
737 | {
|
---|
738 | char *p = buf;
|
---|
739 | int i;
|
---|
740 |
|
---|
741 | for (i = 0; i < len && *p != '\0'; i++)
|
---|
742 | p++;
|
---|
743 | if (*p == '\0' && p - buf > 0)
|
---|
744 | p--;
|
---|
745 | while (p >= buf && isspace(*p)) {
|
---|
746 | *p = '\0';
|
---|
747 | i--;
|
---|
748 | if (p > buf)
|
---|
749 | p--;
|
---|
750 | }
|
---|
751 | return (i);
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * icb_vis: strnvis-like function that escapes percentages as well
|
---|
756 | */
|
---|
757 | int
|
---|
758 | icb_vis(char *dst, const char *src, size_t dstsize, int flags)
|
---|
759 | {
|
---|
760 | int si, di, td;
|
---|
761 |
|
---|
762 | for (si = 0, di = 0; (size_t)di < dstsize - 1 && src[si] != '\0';
|
---|
763 | si++, di++) {
|
---|
764 | if (src[si] == '%') {
|
---|
765 | if ((size_t)di + 1 >= dstsize - 1)
|
---|
766 | break;
|
---|
767 | dst[di++] = '%';
|
---|
768 | dst[di] = '%';
|
---|
769 | } else if (src[si] == ' ' && flags & VIS_SP)
|
---|
770 | dst[di] = '_';
|
---|
771 | else if (isgraph(src[si]) || src[si] == ' ')
|
---|
772 | dst[di] = src[si];
|
---|
773 | else {
|
---|
774 | td = snprintf(&dst[di], dstsize - di,
|
---|
775 | "\\%03o", (unsigned char)src[si]);
|
---|
776 | if (td == -1 || (size_t)td >= dstsize - di)
|
---|
777 | break;
|
---|
778 | di += td - 1;
|
---|
779 | }
|
---|
780 | }
|
---|
781 | dst[MIN((size_t)di, dstsize - 1)] = '\0';
|
---|
782 | return (0);
|
---|
783 | }
|
---|