source: code/icb.c@ 1c6061c

Last change on this file since 1c6061c was 96a2e31, checked in by Mike Belopuhov <mike@…>, 11 years ago

Reset is->group pointer in icb_remove so that we won't try to LIST_REMOVE
multiple times (happens when free is delayed by the DNS resolution).

  • Property mode set to 100644
File size: 15.0 KB
Line 
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/param.h>
18#include <sys/queue.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <syslog.h>
24#include <unistd.h>
25#include <ctype.h>
26#include <event.h>
27#include <vis.h>
28
29#include "icb.h"
30#include "icbd.h"
31
32extern int creategroups;
33extern char srvname[MAXHOSTNAMELEN];
34
35void icb_command(struct icb_session *, char *, char *);
36void icb_groupmsg(struct icb_session *, char *);
37int icb_login(struct icb_session *, char *, char *, char *);
38int icb_dowho(struct icb_session *, struct icb_group *);
39char *icb_nextfield(char **, int);
40
41/*
42 * icb_init: initializes pointers to callbacks
43 */
44void
45icb_init(void)
46{
47 LIST_INIT(&groups);
48
49 if (strlen(srvname) == 0)
50 (void)gethostname(srvname, sizeof srvname);
51 /*
52 * MAXHOSTNAMELEN is usually greater than what we
53 * can actually send, hence truncation:
54 */
55 if (strlen(srvname) > 200)
56 srvname[200] = '\0';
57}
58
59/*
60 * icb_start: called upon accepting a new connection, greets new client
61 */
62void
63icb_start(struct icb_session *is)
64{
65 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
66 ICB_M_SEP, "icbd");
67 SETF(is->flags, ICB_SF_PROTOSENT);
68}
69
70/*
71 * icb_input: main input processing routine
72 */
73int
74icb_input(struct icb_session *is)
75{
76 char *msg = is->buffer;
77 unsigned char type;
78 int res = 0;
79
80 is->last = getmonotime();
81 type = msg[0];
82 msg++;
83 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
84 icb_error(is, "Not logged in");
85 return (0);
86 }
87 switch (type) {
88 case ICB_M_LOGIN: {
89 char *nick, *group, *client, *cmd;
90
91 client = icb_nextfield(&msg, 1);
92 nick = icb_nextfield(&msg, 1);
93 group = icb_nextfield(&msg, 1);
94 cmd = icb_nextfield(&msg, 1);
95 if (strlen(cmd) > 0 && cmd[0] == 'w') {
96 icb_error(is, "Command not implemented");
97 icbd_drop(is, NULL);
98 return (1);
99 }
100 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
101 icb_error(is, "Malformed login packet");
102 icbd_drop(is, NULL);
103 return (1);
104 }
105 res = icb_login(is, group, nick, client);
106 break;
107 }
108 case ICB_M_OPEN: {
109 char *grpmsg;
110
111 grpmsg = icb_nextfield(&msg, 0);
112 icb_groupmsg(is, grpmsg);
113 break;
114 }
115 case ICB_M_COMMAND: {
116 char *cmd, *arg;
117
118 cmd = icb_nextfield(&msg, 1);
119 arg = icb_nextfield(&msg, 0);
120 icb_command(is, cmd, arg);
121 break;
122 }
123 case ICB_M_PONG: {
124 icb_sendfmt(is, "%c", ICB_M_PING);
125 break;
126 }
127 case ICB_M_PROTO:
128 case ICB_M_NOOP:
129 /* ignore */
130 break;
131 default:
132 /* everything else is not valid */
133 icb_error(is, "Undefined message type %u", type);
134 }
135 return (res);
136}
137
138/*
139 * icb_login: handles login ('a') packets
140 */
141int
142icb_login(struct icb_session *is, char *grp, char *nick, char *client)
143{
144 char *defgrp = "1";
145 struct icb_group *ig;
146 struct icb_session *s;
147 char group[ICB_MAXGRPLEN];
148
149 if (!nick || strlen(nick) == 0 ||
150 icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
151 icb_error(is, "Invalid nick");
152 icbd_drop(is, NULL);
153 return (1);
154 }
155 if (!grp || strlen(grp) == 0)
156 strlcpy(group, defgrp, ICB_MAXGRPLEN);
157 else
158 icb_vis(group, grp, ICB_MAXNICKLEN, VIS_SP);
159 LIST_FOREACH(ig, &groups, entry) {
160 if (strcmp(ig->name, group) == 0)
161 break;
162 }
163 if (ig == NULL) {
164 if (!creategroups) {
165 icb_error(is, "Invalid group %s", group);
166 icbd_drop(is, NULL);
167 return (1);
168 } else {
169 if ((ig = icb_addgroup(is, group, NULL)) == NULL) {
170 icb_error(is, "Can't create group %s", group);
171 return (0);
172 }
173 icbd_log(NULL, LOG_DEBUG, "%s created group %s",
174 is->nick, group);
175 }
176 }
177 LIST_FOREACH(s, &ig->sess, entry) {
178 if (strcmp(s->nick, is->nick) == 0) {
179 icb_error(is, "Nick is already in use");
180 icbd_drop(is, NULL);
181 return (1);
182 }
183 }
184
185 if (client && strlen(client) > 0)
186 icb_vis(is->client, client, sizeof is->client, VIS_SP);
187 is->group = ig;
188 is->login = time(NULL);
189 is->last = getmonotime();
190
191 /* notify group */
192 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
193 is->nick, is->client, is->host);
194
195 CLRF(is->flags, ICB_SF_PROTOSENT);
196 SETF(is->flags, ICB_SF_LOGGEDIN);
197
198 LIST_INSERT_HEAD(&ig->sess, is, entry);
199
200 /* acknowledge successful login */
201 icb_sendfmt(is, "%c", ICB_M_LOGIN);
202
203 /* notify user */
204 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
205 icb_ismod(ig, is) ? " as moderator" : "");
206
207 /* send user a topic name */
208 if (strlen(ig->topic) > 0)
209 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
210 ig->name, ig->topic);
211 return (0);
212}
213
214/*
215 * icb_groupmsg: handles open message ('b') packets
216 */
217void
218icb_groupmsg(struct icb_session *is, char *msg)
219{
220 char buf[ICB_MSGSIZE];
221 struct icb_group *ig = is->group;
222 struct icb_session *s;
223 int buflen = 1;
224
225 if (strlen(msg) == 0) {
226 icb_error(is, "Empty message");
227 return;
228 }
229
230 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
231 is->nick, ICB_M_SEP, msg);
232 buf[0] = buflen;
233
234 logger(ig->name, is->nick, msg);
235
236 LIST_FOREACH(s, &ig->sess, entry) {
237 if (s == is)
238 continue;
239 icbd_send(s, buf, buflen + 1);
240 }
241}
242
243/*
244 * icb_privmsg: handles personal message ('c') packets
245 */
246void
247icb_privmsg(struct icb_session *is, char *to, char *msg)
248{
249 struct icb_group *ig = is->group;
250 struct icb_session *s;
251 char whom[ICB_MAXNICKLEN];
252
253 icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
254
255 /* try home group first */
256 LIST_FOREACH(s, &ig->sess, entry) {
257 if (strcmp(s->nick, whom) == 0)
258 break;
259 }
260 if (!s) {
261 /* try all groups until the first match */
262 LIST_FOREACH(ig, &groups, entry) {
263 LIST_FOREACH(s, &ig->sess, entry) {
264 if (strcmp(s->nick, whom) == 0)
265 break;
266 }
267 if (s)
268 break;
269 }
270 if (!s) {
271 icb_error(is, "No such user %s", whom);
272 return;
273 }
274 }
275 icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
276}
277
278/*
279 * icb_command: handles command ('h') packets
280 */
281void
282icb_command(struct icb_session *is, char *cmd, char *arg)
283{
284 void (*handler)(struct icb_session *, char *);
285 char command[32]; /* XXX */
286
287 icb_vis(command, cmd, sizeof command, VIS_SP);
288
289 if ((handler = icb_cmd_lookup(command)) == NULL) {
290 icb_error(is, "Unsupported command: %s", command);
291 return;
292 }
293 handler(is, arg);
294}
295
296/*
297 * icb_cmdout: sends out command output ('i') packets, called from the
298 * command handlers
299 */
300void
301icb_cmdout(struct icb_session *is, int type, char *outmsg)
302{
303 char *otype = NULL;
304
305 switch (type) {
306 case CMDOUT_CO:
307 otype = "co";
308 break;
309 case CMDOUT_EC:
310 otype = "ec";
311 break;
312 case CMDOUT_WG:
313 otype = "wg";
314 break;
315 case CMDOUT_WH:
316 otype = "wh";
317 break;
318 case CMDOUT_WL:
319 otype = "wl";
320 break;
321 default:
322 icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
323 return;
324 }
325 if (outmsg)
326 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
327 outmsg);
328 else
329 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
330}
331
332/*
333 * icb_status: sends a status message ('d') to the client
334 */
335void
336icb_status(struct icb_session *is, int type, const char *fmt, ...)
337{
338 va_list ap;
339 char buf[ICB_MSGSIZE];
340 int buflen = 1;
341 static const struct {
342 int type;
343 const char *msg;
344 } msgtab[] = {
345 { STATUS_ARRIVE, "Arrive" },
346 { STATUS_BOOT, "Boot" },
347 { STATUS_DEPART, "Depart" },
348 { STATUS_HELP, "Help" },
349 { STATUS_NAME, "Name" },
350 { STATUS_NOBEEP, "No-Beep" },
351 { STATUS_NOTIFY, "Notify" },
352 { STATUS_SIGNON, "Sign-on" },
353 { STATUS_SIGNOFF, "Sign-off" },
354 { STATUS_STATUS, "Status" },
355 { STATUS_TOPIC, "Topic" },
356 { STATUS_WARNING, "Warning" },
357 { 0, NULL }
358 };
359
360 if (type < 0 || type > (int)nitems(msgtab) - 1)
361 return;
362 va_start(ap, fmt);
363 buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
364 msgtab[type].msg, ICB_M_SEP);
365 buflen += vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
366 buf[0] = buflen;
367 va_end(ap);
368 icbd_send(is, buf, buflen + 1);
369}
370
371/*
372 * icb_status: sends a status message ('d') to the group except of the
373 * "ex" if it's not NULL
374 */
375void
376icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
377 const char *fmt, ...)
378{
379 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
380 va_list ap;
381 struct icb_session *s;
382
383 va_start(ap, fmt);
384 (void)vsnprintf(buf, sizeof buf, fmt, ap);
385 LIST_FOREACH(s, &ig->sess, entry) {
386 if (ex && s == ex)
387 continue;
388 icb_status(s, type, buf);
389 }
390 logger(ig->name, "", buf);
391 icbd_log(NULL, LOG_DEBUG, "%s", buf);
392 va_end(ap);
393}
394
395/*
396 * icb_error: sends an error message ('e') to the client
397 */
398void
399icb_error(struct icb_session *is, const char *fmt, ...)
400{
401 char buf[ICB_MSGSIZE];
402 va_list ap;
403 int buflen = 1;
404
405 va_start(ap, fmt);
406 buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
407 va_end(ap);
408 buf[0] = ++buflen; /* account for ICB_M_ERROR */
409 buf[1] = ICB_M_ERROR;
410 icbd_send(is, buf, buflen + 1);
411 icbd_log(is, LOG_DEBUG, "%s", buf + 2);
412}
413
414/*
415 * icb_remove: removes a session from the associated group
416 */
417void
418icb_remove(struct icb_session *is, char *reason)
419{
420 if (is->group) {
421 if (icb_ismod(is->group, is))
422 (void)icb_pass(is->group, is, NULL);
423 LIST_REMOVE(is, entry);
424 if (reason)
425 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
426 "%s (%s@%s) just left: %s", is->nick, is->client,
427 is->host, reason);
428 else
429 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
430 "%s (%s@%s) just left", is->nick, is->client,
431 is->host);
432 is->group = NULL;
433 }
434}
435
436/*
437 * icb_addgroup: adds a new group to the list
438 */
439struct icb_group *
440icb_addgroup(struct icb_session *is, char *name, char *mpass)
441{
442 struct icb_group *ig;
443
444 if ((ig = calloc(1, sizeof *ig)) == NULL)
445 return (NULL);
446 strlcpy(ig->name, name, sizeof ig->name);
447 if (mpass)
448 strlcpy(ig->mpass, mpass, sizeof ig->mpass);
449 if (is)
450 ig->mod = is;
451 LIST_INIT(&ig->sess);
452 LIST_INSERT_HEAD(&groups, ig, entry);
453 return (ig);
454}
455
456#ifdef notused
457/*
458 * icb_delgroup: removes a group from the list
459 */
460void
461icb_delgroup(struct icb_group *ig)
462{
463 struct icb_session *s;
464
465 /* well, i guess we should kick out participants! ;-) */
466 LIST_FOREACH(s, &ig->sess, entry) {
467 icb_status(s, STATUS_WARNING, "Group dismissed");
468 s->group = NULL;
469 }
470 LIST_REMOVE(ig, entry);
471 bzero(ig, sizeof ig); /* paranoic thing, obviously */
472 free(ig);
473}
474#endif
475
476/*
477 * icb_dowho: a helper function that sends out a group header as a command
478 * output and user information in the "wl" format
479 */
480int
481icb_dowho(struct icb_session *is, struct icb_group *ig)
482{
483 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
484 struct icb_session *s;
485 int nusers = 0;
486
487 icb_cmdout(is, CMDOUT_CO, " ");
488 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
489 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
490 strlen(ig->topic) > 0 ? ig->topic : "(None)");
491 icb_cmdout(is, CMDOUT_CO, buf);
492 LIST_FOREACH(s, &ig->sess, entry) {
493 (void)snprintf(buf, sizeof buf,
494 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
495 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
496 s->nick, ICB_M_SEP, getmonotime() - s->last,
497 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
498 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
499 icb_cmdout(is, CMDOUT_WL, buf);
500 nusers++;
501 }
502 return (nusers);
503}
504
505/*
506 * icb_who: sends a list of users of either the specified group or all
507 * groups found on the server
508 */
509void
510icb_who(struct icb_session *is, struct icb_group *ig)
511{
512 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
513 struct icb_group *g;
514
515 if (!ig) {
516 int nusers = 0, ngroups = 0;
517
518 LIST_FOREACH(g, &groups, entry) {
519 nusers += icb_dowho(is, g);
520 ngroups++;
521 }
522 if (nusers > 0) {
523 (void)snprintf(buf, sizeof buf,
524 "Total: %d %s in %d %s",
525 nusers, nusers > 1 ? "users" : "user",
526 ngroups, ngroups > 1 ? "groups" : "group");
527 } else
528 (void)snprintf(buf, sizeof buf, "No users found.");
529 icb_cmdout(is, CMDOUT_CO, buf);
530 } else
531 (void)icb_dowho(is, ig);
532}
533
534/*
535 * icb_ismod: checks whether group is moderated by "is"
536 */
537inline int
538icb_ismod(struct icb_group *ig, struct icb_session *is)
539{
540 return (ig->mod == is);
541}
542
543/*
544 * icb_modpermit: checks user against the moderators table if it has
545 * been populated
546 */
547int
548icb_modpermit(struct icb_session *is, int enforce)
549{
550 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
551 extern int modtabcnt;
552
553 icbd_modupdate();
554 if ((enforce ? 0 : modtabcnt == 0) ||
555 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
556 (int (*)(const void *, const void *))strcmp))
557 return (1);
558 return (0);
559}
560
561/*
562 * icb_pass: passes moderation of group "ig" from "from" to "to",
563 * returns -1 if "from" is not a moderator, 1 if passed
564 * to "to" and 0 otherwise (no moderator or passed to the
565 * internal bot)
566 */
567int
568icb_pass(struct icb_group *ig, struct icb_session *from,
569 struct icb_session *to)
570{
571 if (ig->mod && ig->mod != from)
572 return (-1);
573 if (!from && !to)
574 return (-1);
575 ig->mod = to;
576 if (to)
577 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
578 " of %s", from ? from->nick : "server", ig->name);
579 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
580 "to %s", from ? from->nick : "server", to ? to->nick : "server");
581 return (1);
582}
583
584/*
585 * icb_nextfield: advances through a given buffer returning pointer to the
586 * beginning of the icb field or an empty string otherwise;
587 * cleans up trailing spaces
588 */
589char *
590icb_nextfield(char **buf, int notrspace)
591{
592 char *start = *buf;
593 char *end = NULL;
594
595 while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
596 (*buf)++;
597 if (*buf && **buf == ICB_M_SEP) {
598 **buf = '\0';
599 end = *buf;
600 (*buf)++;
601 } else
602 end = *buf;
603 while (notrspace && end && *(--end) == ' ' && end > start)
604 *end = '\0';
605 return (start);
606}
607
608/*
609 * icb_sendfmt: formats a string and sends it over
610 */
611void
612icb_sendfmt(struct icb_session *is, const char *fmt, ...)
613{
614 char buf[ICB_MSGSIZE];
615 va_list ap;
616 int buflen = 1;
617
618 va_start(ap, fmt);
619 buflen += vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
620 va_end(ap);
621 buf[0] = buflen;
622 icbd_send(is, buf, buflen + 1);
623}
624
625/*
626 * icb_vis: strnvis-like function that escapes percentages as well
627 */
628int
629icb_vis(char *dst, const char *src, size_t dstsize, int flags)
630{
631 int si = 0, di = 0, td;
632
633 while ((size_t)di < dstsize && src[si] != '\0') {
634 if (src[si] == '%')
635 dst[di++] = '%', dst[di] = '%';
636 else if (src[si] == ' ' && flags & VIS_SP)
637 dst[di] = '_';
638 else if (isgraph(src[si]) || src[si] == ' ')
639 dst[di] = src[si];
640 else {
641 td = snprintf(&dst[di], dstsize - di,
642 "\\%03o", (unsigned char)src[si]);
643 di += td - 1;
644 }
645 si++, di++;
646 }
647 dst[MIN((size_t)di, dstsize)] = '\0';
648 return (0);
649}
Note: See TracBrowser for help on using the repository browser.