source: code/icb.c@ b7bc432

Last change on this file since b7bc432 was b7bc432, checked in by Stuart Henderson <stu@…>, 11 years ago

permit whitespace in topic, otherwise sanitize to _

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