source: code/icb.c@ ee0e95f

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

Change the "Bummer" message to the more informative one

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