source: code/icb.c@ cba908b

Last change on this file since cba908b was cba908b, checked in by Mike Belopuhov <mike@…>, 10 years ago

defgrp is const

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