source: code/icb.c@ 4e66b3a

Last change on this file since 4e66b3a was 45bd56a, checked in by Mike Belopuhov <mike@…>, 11 years ago

logger will do its own time accounting; from the discussion with florian

  • Property mode set to 100644
File size: 14.8 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 *);
39int icb_modpermit(struct icb_session *);
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);
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';
62}
63
64/*
65 * icb_start: called upon accepting a new connection, greets new client
66 */
67void
68icb_start(struct icb_session *is)
69{
70 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
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 }
104 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
105 icb_error(is, "Malformed login packet");
106 icb_drop(is, NULL);
107 return;
108 }
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 }
127 case ICB_M_PONG: {
128 icb_sendfmt(is, "%c", ICB_M_PING);
129 break;
130 }
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
145icb_login(struct icb_session *is, char *grp, char *nick, char *client)
146{
147 char *defgrp = "1";
148 struct icb_group *ig;
149 struct icb_session *s;
150 char group[ICB_MAXGRPLEN];
151
152 if (!nick || strlen(nick) == 0 ||
153 icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
154 icb_error(is, "Invalid nick");
155 icb_drop(is, NULL);
156 return;
157 }
158 if (!grp || strlen(grp) == 0)
159 strlcpy(group, defgrp, ICB_MAXGRPLEN);
160 else
161 icb_vis(group, grp, ICB_MAXNICKLEN, VIS_SP);
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",
177 is->nick, group);
178 }
179 }
180 LIST_FOREACH(s, &ig->sess, entry) {
181 if (strcmp(s->nick, is->nick) == 0) {
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)
189 icb_vis(is->client, client, sizeof is->client, VIS_SP);
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,
209 icb_ismod(ig, is) ? " as moderator" : "");
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 logger(ig->name, is->nick, msg);
238
239 LIST_FOREACH(s, &ig->sess, entry) {
240 if (s == is)
241 continue;
242 icb_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 icb_log(is, LOG_ERR, "unknown cmdout 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 icb_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];
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 icb_log(NULL, LOG_DEBUG, "%s", buf);
394 va_end(ap);
395}
396
397/*
398 * icb_error: sends an error message ('e') to the client
399 */
400void
401icb_error(struct icb_session *is, const char *fmt, ...)
402{
403 char buf[ICB_MSGSIZE];
404 va_list ap;
405 int buflen = 1;
406
407 va_start(ap, fmt);
408 buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
409 va_end(ap);
410 buf[0] = ++buflen; /* account for ICB_M_ERROR */
411 buf[1] = ICB_M_ERROR;
412 icb_send(is, buf, buflen + 1);
413 icb_log(is, LOG_DEBUG, "%s", buf + 2);
414}
415
416/*
417 * icb_remove: removes a session from the associated group
418 */
419void
420icb_remove(struct icb_session *is, char *reason)
421{
422 if (is->group) {
423 if (icb_ismod(is->group, is))
424 (void)icb_pass(is->group, is, NULL);
425 LIST_REMOVE(is, entry);
426 if (reason)
427 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
428 "%s (%s@%s) just left: %s", is->nick, is->client,
429 is->host, reason);
430 else
431 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
432 "%s (%s@%s) just left", is->nick, is->client,
433 is->host);
434 }
435}
436
437/*
438 * icb_addgroup: adds a new group to the list
439 */
440struct icb_group *
441icb_addgroup(struct icb_session *is, char *name, char *mpass)
442{
443 struct icb_group *ig;
444
445 if ((ig = calloc(1, sizeof *ig)) == NULL)
446 return (NULL);
447 strlcpy(ig->name, name, sizeof ig->name);
448 if (mpass)
449 strlcpy(ig->mpass, mpass, sizeof ig->mpass);
450 if (is)
451 ig->mod = is;
452 LIST_INIT(&ig->sess);
453 LIST_INSERT_HEAD(&groups, ig, entry);
454 return (ig);
455}
456
457#ifdef notused
458/*
459 * icb_delgroup: removes a group from the list
460 */
461void
462icb_delgroup(struct icb_group *ig)
463{
464 struct icb_session *s;
465
466 /* well, i guess we should kick out participants! ;-) */
467 LIST_FOREACH(s, &ig->sess, entry) {
468 icb_status(s, STATUS_WARNING, "Group dismissed");
469 s->group = NULL;
470 }
471 LIST_REMOVE(ig, entry);
472 bzero(ig, sizeof ig); /* paranoic thing, obviously */
473 free(ig);
474}
475#endif
476
477/*
478 * icb_dowho: a helper function that sends out a group header as a command
479 * output and user information in the "wl" format
480 */
481int
482icb_dowho(struct icb_session *is, struct icb_group *ig)
483{
484 char buf[ICB_MSGSIZE];
485 struct icb_session *s;
486 int nusers = 0;
487
488 icb_cmdout(is, CMDOUT_CO, " ");
489 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
490 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
491 strlen(ig->topic) > 0 ? ig->topic : "(None)");
492 icb_cmdout(is, CMDOUT_CO, buf);
493 LIST_FOREACH(s, &ig->sess, entry) {
494 (void)snprintf(buf, sizeof buf,
495 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
496 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
497 s->nick, ICB_M_SEP, getmonotime() - s->last,
498 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
499 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
500 icb_cmdout(is, CMDOUT_WL, buf);
501 nusers++;
502 }
503 return (nusers);
504}
505
506/*
507 * icb_who: sends a list of users of either the specified group or all
508 * groups found on the server
509 */
510void
511icb_who(struct icb_session *is, struct icb_group *ig)
512{
513 char buf[ICB_MSGSIZE];
514 struct icb_group *g;
515
516 if (!ig) {
517 int nusers = 0, ngroups = 0;
518
519 LIST_FOREACH(g, &groups, entry) {
520 nusers += icb_dowho(is, g);
521 ngroups++;
522 }
523 if (nusers > 0) {
524 (void)snprintf(buf, sizeof buf,
525 "Total: %d %s in %d %s",
526 nusers, nusers > 1 ? "users" : "user",
527 ngroups, ngroups > 1 ? "groups" : "group");
528 } else
529 (void)snprintf(buf, sizeof buf, "No users found.");
530 icb_cmdout(is, CMDOUT_CO, buf);
531 } else
532 (void)icb_dowho(is, ig);
533}
534
535/*
536 * icb_ismod: checks whether group is moderated by "is"
537 */
538inline int
539icb_ismod(struct icb_group *ig, struct icb_session *is)
540{
541 return (ig->mod == is);
542}
543
544/*
545 * icb_modpermit: checks user against the moderators table if it has
546 * been populated
547 */
548int
549icb_modpermit(struct icb_session *is)
550{
551 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
552 extern int modtabcnt;
553
554 if (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 if (to && !icb_modpermit(to))
576 return (-1);
577 ig->mod = to;
578 if (to)
579 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
580 " of %s", from ? from->nick : "server", ig->name);
581 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
582 "to %s", from ? from->nick : "server", to ? to->nick : "server");
583 return (1);
584}
585
586/*
587 * icb_nextfield: advances through a given buffer returning pointer to the
588 * beginning of the icb field or an empty string otherwise;
589 * cleans up trailing spaces
590 */
591char *
592icb_nextfield(char **buf)
593{
594 char *start = *buf;
595 char *end = NULL;
596
597 while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
598 (*buf)++;
599 if (*buf && **buf == ICB_M_SEP) {
600 **buf = '\0';
601 end = *buf;
602 (*buf)++;
603 } else
604 end = *buf;
605 while (end && *(--end) == ' ' && end > start)
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 icb_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 && src[si] != '\0') {
636 if (src[si] == '%')
637 dst[di++] = '%', dst[di] = '%';
638 else if (src[si] == ' ' && flags & VIS_SP)
639 dst[di] = '_';
640 else if (isgraph(src[si]) || src[si] == ' ')
641 dst[di] = src[si];
642 else {
643 td = snprintf(&dst[di], dstsize - di,
644 "\\%03o", (unsigned char)src[si]);
645 di += td - 1;
646 }
647 si++, di++;
648 }
649 dst[MIN((size_t)di, dstsize)] = '\0';
650 return (0);
651}
Note: See TracBrowser for help on using the repository browser.