source: code/icb.c

Last change on this file was 4a74b5b, checked in by Izuru Yakumo <eternal-servant@…>, 7 weeks ago

Nitori Engineering

  • Property mode set to 100644
File size: 18.4 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
[0519a87]17#include <sys/types.h>
[cd7b81d]18#include <sys/queue.h>
[0519a87]19#include <netdb.h>
[cd7b81d]20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <syslog.h>
25#include <unistd.h>
[626f420]26#include <ctype.h>
[cd7b81d]27#include <event.h>
[b7bc432]28#include <vis.h>
[cd7b81d]29
30#include "icb.h"
31#include "icbd.h"
32
[e22d747]33struct icb_grplist groups;
34
[cd7b81d]35extern int creategroups;
[0519a87]36extern char srvname[NI_MAXHOST];
[cd7b81d]37
38void icb_command(struct icb_session *, char *, char *);
39void icb_groupmsg(struct icb_session *, char *);
[d45051e]40int icb_login(struct icb_session *, char *, char *, char *);
[4284008]41int icb_dowho(struct icb_session *, struct icb_group *);
[cd7b81d]42
43/*
[b8da4b3]44 * icb_init: initialize server name and a list of groups
[cd7b81d]45 */
46void
[7882a6f]47icb_init(void)
[cd7b81d]48{
49 LIST_INIT(&groups);
[a785c27]50
51 if (strlen(srvname) == 0)
52 (void)gethostname(srvname, sizeof srvname);
53 /*
[0519a87]54 * NI_MAXHOST is usually greater than what we
[a785c27]55 * can actually send, hence truncation:
56 */
57 if (strlen(srvname) > 200)
58 srvname[200] = '\0';
[cd7b81d]59}
60
61/*
62 * icb_start: called upon accepting a new connection, greets new client
63 */
64void
65icb_start(struct icb_session *is)
66{
[a785c27]67 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
[cd7b81d]68 ICB_M_SEP, "icbd");
69 SETF(is->flags, ICB_SF_PROTOSENT);
70}
71
72/*
73 * icb_input: main input processing routine
74 */
[d45051e]75int
[cd7b81d]76icb_input(struct icb_session *is)
77{
78 char *msg = is->buffer;
[176b6ef]79 int msglen = is->length;
[1bcb666]80 unsigned char type;
[176b6ef]81 char *wptr = NULL;
[d45051e]82 int res = 0;
[cd7b81d]83
84 is->last = getmonotime();
[a2fadb4]85 type = msg[0];
86 msg++;
[cd7b81d]87 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
88 icb_error(is, "Not logged in");
[d45051e]89 return (0);
[cd7b81d]90 }
91 switch (type) {
92 case ICB_M_LOGIN: {
[176b6ef]93 char client[ICB_MAXNICKLEN];
94 char nick[ICB_MAXNICKLEN];
95 char group[ICB_MAXGRPLEN];
96 char cmd[ICB_MAXCMDLEN];
97
[8b1683e]98 memset(client, 0, sizeof client);
[176b6ef]99 if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN,
100 ICB_M_SEP, 1) < 0) {
101 icb_error(is, "Invalid client");
102 icbd_drop(is, NULL);
103 return (1);
104 }
[8b1683e]105 memset(nick, 0, sizeof nick);
[176b6ef]106 if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN,
107 ICB_M_SEP, 1) <= 0) {
108 icb_error(is, "Invalid nick");
109 icbd_drop(is, NULL);
110 return (1);
111 }
[8b1683e]112 memset(group, 0, sizeof group);
[176b6ef]113 if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN,
114 ICB_M_SEP, 1) < 0) {
[7ff6405]115 icb_error(is, "Invalid login group");
[176b6ef]116 icbd_drop(is, NULL);
117 return (1);
118 }
[8b1683e]119 memset(cmd, 0, sizeof cmd);
[176b6ef]120 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
121 ICB_M_SEP, 1) < 0) {
122 icb_error(is, "Invalid command");
123 icbd_drop(is, NULL);
124 return (1);
125 }
[cd7b81d]126 if (strlen(cmd) > 0 && cmd[0] == 'w') {
127 icb_error(is, "Command not implemented");
[7882a6f]128 icbd_drop(is, NULL);
[d45051e]129 return (1);
[cd7b81d]130 }
[1da9ee5]131 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
132 icb_error(is, "Malformed login packet");
[7882a6f]133 icbd_drop(is, NULL);
[d45051e]134 return (1);
[1da9ee5]135 }
[d45051e]136 res = icb_login(is, group, nick, client);
[cd7b81d]137 break;
138 }
139 case ICB_M_OPEN: {
[176b6ef]140 icb_groupmsg(is, msg);
[cd7b81d]141 break;
142 }
143 case ICB_M_COMMAND: {
[176b6ef]144 char cmd[ICB_MAXCMDLEN];
[cd7b81d]145
[8b1683e]146 memset(cmd, 0, sizeof cmd);
[176b6ef]147 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
148 ICB_M_SEP, 1) <= 0) {
149 icb_error(is, "Invalid command");
150 icbd_drop(is, NULL);
151 return (1);
152 }
[a56efea]153 msg = wptr;
154 icb_command(is, cmd, msg);
[cd7b81d]155 break;
156 }
[1fb1bbe]157 case ICB_M_PONG: {
158 icb_sendfmt(is, "%c", ICB_M_PING);
159 break;
160 }
[cd7b81d]161 case ICB_M_PROTO:
162 case ICB_M_NOOP:
163 /* ignore */
164 break;
165 default:
166 /* everything else is not valid */
[1bcb666]167 icb_error(is, "Undefined message type %u", type);
[cd7b81d]168 }
[d45051e]169 return (res);
[cd7b81d]170}
171
172/*
173 * icb_login: handles login ('a') packets
174 */
[d45051e]175int
[626f420]176icb_login(struct icb_session *is, char *grp, char *nick, char *client)
[cd7b81d]177{
[cba908b]178 const char *defgrp = "1";
[cd7b81d]179 struct icb_group *ig;
180 struct icb_session *s;
[626f420]181 char group[ICB_MAXGRPLEN];
[cd7b81d]182
[626f420]183 if (!nick || strlen(nick) == 0 ||
[b7bc432]184 icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
[cd7b81d]185 icb_error(is, "Invalid nick");
[7882a6f]186 icbd_drop(is, NULL);
[d45051e]187 return (1);
[cd7b81d]188 }
[626f420]189 if (!grp || strlen(grp) == 0)
190 strlcpy(group, defgrp, ICB_MAXGRPLEN);
191 else
[cb7c494]192 icb_vis(group, grp, ICB_MAXGRPLEN, VIS_SP);
[cd7b81d]193 LIST_FOREACH(ig, &groups, entry) {
194 if (strcmp(ig->name, group) == 0)
195 break;
196 }
197 if (ig == NULL) {
198 if (!creategroups) {
[7ff6405]199 icb_error(is, "Can't create new groups", group);
[7882a6f]200 icbd_drop(is, NULL);
[d45051e]201 return (1);
[cd7b81d]202 } else {
[677a45b]203 if ((ig = icb_addgroup(is, group)) == NULL) {
[cd7b81d]204 icb_error(is, "Can't create group %s", group);
[d45051e]205 return (0);
[cd7b81d]206 }
[7882a6f]207 icbd_log(NULL, LOG_DEBUG, "%s created group %s",
[626f420]208 is->nick, group);
[cd7b81d]209 }
210 }
211 LIST_FOREACH(s, &ig->sess, entry) {
[626f420]212 if (strcmp(s->nick, is->nick) == 0) {
[cd7b81d]213 icb_error(is, "Nick is already in use");
[7882a6f]214 icbd_drop(is, NULL);
[d45051e]215 return (1);
[cd7b81d]216 }
217 }
218
219 if (client && strlen(client) > 0)
[b7bc432]220 icb_vis(is->client, client, sizeof is->client, VIS_SP);
[0519a87]221 else
222 strlcpy(is->client, is->nick, sizeof is->client);
[cd7b81d]223 is->group = ig;
224 is->login = time(NULL);
225 is->last = getmonotime();
226
227 /* notify group */
228 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
229 is->nick, is->client, is->host);
230
231 CLRF(is->flags, ICB_SF_PROTOSENT);
232 SETF(is->flags, ICB_SF_LOGGEDIN);
233
234 LIST_INSERT_HEAD(&ig->sess, is, entry);
235
236 /* acknowledge successful login */
237 icb_sendfmt(is, "%c", ICB_M_LOGIN);
238
239 /* notify user */
240 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
[4284008]241 icb_ismod(ig, is) ? " as moderator" : "");
[cd7b81d]242
243 /* send user a topic name */
244 if (strlen(ig->topic) > 0)
245 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
246 ig->name, ig->topic);
[d45051e]247 return (0);
[cd7b81d]248}
249
250/*
251 * icb_groupmsg: handles open message ('b') packets
252 */
253void
254icb_groupmsg(struct icb_session *is, char *msg)
255{
256 char buf[ICB_MSGSIZE];
257 struct icb_group *ig = is->group;
258 struct icb_session *s;
[c6e00b9]259 int res, buflen;
[cd7b81d]260
261 if (strlen(msg) == 0) {
262 icb_error(is, "Empty message");
263 return;
264 }
265
[c6e00b9]266 do {
267 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
268 is->nick, ICB_M_SEP, msg);
269 if (res < 0) {
270 icbd_log(is, LOG_ERR, "Format error in %s", __func__);
271 return;
272 }
[e26e899]273 /* res doesn't include the terminating NUL */
[c6e00b9]274 buflen = MIN((size_t)res + 1, sizeof buf - 1);
275 buf[0] = buflen;
[cd7b81d]276
[c6e00b9]277 logger(ig->name, is->nick, msg);
[55923b7]278
[c6e00b9]279 LIST_FOREACH(s, &ig->sess, entry) {
280 if (s == is)
281 continue;
282 icbd_send(s, buf, buflen + 1);
283 }
284
285 msg += buflen - 1;
286 } while (res > buflen - 1);
[cd7b81d]287}
288
289/*
290 * icb_privmsg: handles personal message ('c') packets
291 */
292void
[626f420]293icb_privmsg(struct icb_session *is, char *to, char *msg)
[cd7b81d]294{
295 struct icb_group *ig = is->group;
296 struct icb_session *s;
[c6e00b9]297 char buf[ICB_MSGSIZE];
[626f420]298 char whom[ICB_MAXNICKLEN];
[c6e00b9]299 int res, buflen;
[626f420]300
[b7bc432]301 icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
[cd7b81d]302
[efa8586]303 /* try home group first */
[cd7b81d]304 LIST_FOREACH(s, &ig->sess, entry) {
305 if (strcmp(s->nick, whom) == 0)
306 break;
307 }
308 if (!s) {
[efa8586]309 /* try all groups until the first match */
310 LIST_FOREACH(ig, &groups, entry) {
311 LIST_FOREACH(s, &ig->sess, entry) {
312 if (strcmp(s->nick, whom) == 0)
313 break;
314 }
315 if (s)
316 break;
317 }
318 if (!s) {
319 icb_error(is, "No such user %s", whom);
320 return;
321 }
[cd7b81d]322 }
[c6e00b9]323
324 do {
325 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s",
326 ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
327 if (res < 0) {
328 icbd_log(is, LOG_ERR, "Format error in %s", __func__);
329 return;
330 }
[e26e899]331 /* res doesn't include the terminating NUL */
[c6e00b9]332 buflen = MIN((size_t)res + 1, sizeof buf - 1);
333 buf[0] = buflen;
334
335 icbd_send(s, buf, buflen + 1);
336
337 msg += buflen - 1;
338 } while (res > buflen - 1);
[cd7b81d]339}
340
341/*
342 * icb_command: handles command ('h') packets
343 */
344void
345icb_command(struct icb_session *is, char *cmd, char *arg)
346{
347 void (*handler)(struct icb_session *, char *);
[176b6ef]348 char command[ICB_MAXCMDLEN];
[cd7b81d]349
[b7bc432]350 icb_vis(command, cmd, sizeof command, VIS_SP);
[626f420]351
352 if ((handler = icb_cmd_lookup(command)) == NULL) {
353 icb_error(is, "Unsupported command: %s", command);
[cd7b81d]354 return;
355 }
356 handler(is, arg);
357}
358
359/*
360 * icb_cmdout: sends out command output ('i') packets, called from the
361 * command handlers
362 */
363void
364icb_cmdout(struct icb_session *is, int type, char *outmsg)
365{
366 char *otype = NULL;
367
368 switch (type) {
369 case CMDOUT_CO:
370 otype = "co";
371 break;
372 case CMDOUT_EC:
373 otype = "ec";
374 break;
375 case CMDOUT_WG:
376 otype = "wg";
377 break;
[4284008]378 case CMDOUT_WH:
379 otype = "wh";
380 break;
381 case CMDOUT_WL:
382 otype = "wl";
383 break;
[cd7b81d]384 default:
[7882a6f]385 icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
[cd7b81d]386 return;
387 }
[4284008]388 if (outmsg)
389 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
390 outmsg);
391 else
392 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
[cd7b81d]393}
394
395/*
396 * icb_status: sends a status message ('d') to the client
397 */
398void
399icb_status(struct icb_session *is, int type, const char *fmt, ...)
400{
401 va_list ap;
402 char buf[ICB_MSGSIZE];
[124a071]403 int res, buflen;
[cd7b81d]404 static const struct {
405 int type;
406 const char *msg;
407 } msgtab[] = {
408 { STATUS_ARRIVE, "Arrive" },
409 { STATUS_BOOT, "Boot" },
410 { STATUS_DEPART, "Depart" },
[9195a6a]411 { STATUS_HELP, "Help" },
[cd7b81d]412 { STATUS_NAME, "Name" },
[bf02a60]413 { STATUS_NOBEEP, "No-Beep" },
[cd7b81d]414 { STATUS_NOTIFY, "Notify" },
415 { STATUS_SIGNON, "Sign-on" },
416 { STATUS_SIGNOFF, "Sign-off" },
417 { STATUS_STATUS, "Status" },
418 { STATUS_TOPIC, "Topic" },
419 { STATUS_WARNING, "Warning" },
[1022119]420 { 0, NULL }
[cd7b81d]421 };
422
423 if (type < 0 || type > (int)nitems(msgtab) - 1)
424 return;
[65b48ee]425 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
[cd7b81d]426 msgtab[type].msg, ICB_M_SEP);
[65b48ee]427 if (res < 0) {
428 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
429 return;
430 }
[124a071]431 if ((size_t)res >= sizeof buf) {
[65b48ee]432 icbd_log(NULL, LOG_ERR, "Status buffer too small");
433 return;
434 }
[124a071]435 buflen = MIN((size_t)res + 1, sizeof buf - 1);
[65b48ee]436 va_start(ap, fmt);
[124a071]437 res = vsnprintf(&buf[buflen], sizeof buf - buflen - 1, fmt, ap);
[cd7b81d]438 va_end(ap);
[124a071]439 buflen--; /* buf[buflen] was overwritten */
[65b48ee]440 if (res < 0) {
[124a071]441 icbd_log(NULL, LOG_ERR, "Message format error in %s", __func__);
[65b48ee]442 return;
443 }
[124a071]444 if ((size_t)res + buflen >= sizeof buf) {
445 icbd_log(NULL, LOG_ERR, "Status message too long");
446 return;
447 }
448 buflen += MIN((size_t)res + 1, sizeof buf - buflen - 1);
[65b48ee]449 buf[0] = buflen;
[7882a6f]450 icbd_send(is, buf, buflen + 1);
[cd7b81d]451}
452
453/*
454 * icb_status: sends a status message ('d') to the group except of the
455 * "ex" if it's not NULL
456 */
457void
458icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
459 const char *fmt, ...)
460{
[a2fadb4]461 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
[cd7b81d]462 va_list ap;
463 struct icb_session *s;
464
465 va_start(ap, fmt);
466 (void)vsnprintf(buf, sizeof buf, fmt, ap);
467 LIST_FOREACH(s, &ig->sess, entry) {
468 if (ex && s == ex)
469 continue;
470 icb_status(s, type, buf);
471 }
[23ca6f1]472 logger(ig->name, "", buf);
[7882a6f]473 icbd_log(NULL, LOG_DEBUG, "%s", buf);
[cd7b81d]474 va_end(ap);
475}
476
477/*
478 * icb_error: sends an error message ('e') to the client
479 */
480void
481icb_error(struct icb_session *is, const char *fmt, ...)
482{
483 char buf[ICB_MSGSIZE];
484 va_list ap;
[124a071]485 int res, buflen;
[cd7b81d]486
487 va_start(ap, fmt);
[65b48ee]488 res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
[cd7b81d]489 va_end(ap);
[65b48ee]490 if (res < 0) {
491 icbd_log(NULL, LOG_ERR, "Format error");
492 return;
493 }
[124a071]494 buflen = MIN((size_t)res + 1, sizeof buf - 2);
[cd7b81d]495 buf[0] = ++buflen; /* account for ICB_M_ERROR */
496 buf[1] = ICB_M_ERROR;
[7882a6f]497 icbd_send(is, buf, buflen + 1);
498 icbd_log(is, LOG_DEBUG, "%s", buf + 2);
[cd7b81d]499}
500
501/*
502 * icb_remove: removes a session from the associated group
503 */
504void
505icb_remove(struct icb_session *is, char *reason)
506{
507 if (is->group) {
[4284008]508 if (icb_ismod(is->group, is))
[cd7b81d]509 (void)icb_pass(is->group, is, NULL);
510 LIST_REMOVE(is, entry);
511 if (reason)
512 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
513 "%s (%s@%s) just left: %s", is->nick, is->client,
514 is->host, reason);
515 else
516 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
517 "%s (%s@%s) just left", is->nick, is->client,
518 is->host);
[96a2e31]519 is->group = NULL;
[cd7b81d]520 }
521}
522
523/*
524 * icb_addgroup: adds a new group to the list
525 */
526struct icb_group *
[677a45b]527icb_addgroup(struct icb_session *is, char *name)
[cd7b81d]528{
529 struct icb_group *ig;
530
531 if ((ig = calloc(1, sizeof *ig)) == NULL)
532 return (NULL);
533 strlcpy(ig->name, name, sizeof ig->name);
534 if (is)
[4284008]535 ig->mod = is;
[cd7b81d]536 LIST_INIT(&ig->sess);
537 LIST_INSERT_HEAD(&groups, ig, entry);
538 return (ig);
539}
540
541#ifdef notused
542/*
543 * icb_delgroup: removes a group from the list
544 */
545void
546icb_delgroup(struct icb_group *ig)
547{
548 struct icb_session *s;
549
550 /* well, i guess we should kick out participants! ;-) */
551 LIST_FOREACH(s, &ig->sess, entry) {
552 icb_status(s, STATUS_WARNING, "Group dismissed");
553 s->group = NULL;
554 }
555 LIST_REMOVE(ig, entry);
556 bzero(ig, sizeof ig); /* paranoic thing, obviously */
557 free(ig);
558}
559#endif
560
561/*
[4284008]562 * icb_dowho: a helper function that sends out a group header as a command
563 * output and user information in the "wl" format
[cd7b81d]564 */
[4284008]565int
566icb_dowho(struct icb_session *is, struct icb_group *ig)
[cd7b81d]567{
[a2fadb4]568 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
[cd7b81d]569 struct icb_session *s;
[5dfeb4c]570 time_t now;
[4284008]571 int nusers = 0;
[cd7b81d]572
[5dfeb4c]573 now = getmonotime();
[4284008]574 icb_cmdout(is, CMDOUT_CO, " ");
575 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
576 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
577 strlen(ig->topic) > 0 ? ig->topic : "(None)");
578 icb_cmdout(is, CMDOUT_CO, buf);
[cd7b81d]579 LIST_FOREACH(s, &ig->sess, entry) {
580 (void)snprintf(buf, sizeof buf,
[4a74b5b]581#ifdef __NetBSD__
582 "%c%c%s%c%ld%c0%c%ld%c%s%c%s%c%s",
583#else
[bf02a60]584 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
[4a74b5b]585#endif
[4284008]586 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
[5dfeb4c]587 s->nick, ICB_M_SEP, now - s->last,
[cd7b81d]588 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
[c102bbf]589 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
[cd7b81d]590 icb_cmdout(is, CMDOUT_WL, buf);
[4284008]591 nusers++;
[cd7b81d]592 }
[4284008]593 return (nusers);
594}
595
596/*
597 * icb_who: sends a list of users of either the specified group or all
598 * groups found on the server
599 */
600void
601icb_who(struct icb_session *is, struct icb_group *ig)
602{
[a2fadb4]603 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
[4284008]604 struct icb_group *g;
605
606 if (!ig) {
607 int nusers = 0, ngroups = 0;
608
609 LIST_FOREACH(g, &groups, entry) {
610 nusers += icb_dowho(is, g);
611 ngroups++;
612 }
613 if (nusers > 0) {
614 (void)snprintf(buf, sizeof buf,
615 "Total: %d %s in %d %s",
616 nusers, nusers > 1 ? "users" : "user",
617 ngroups, ngroups > 1 ? "groups" : "group");
618 } else
619 (void)snprintf(buf, sizeof buf, "No users found.");
620 icb_cmdout(is, CMDOUT_CO, buf);
621 } else
622 (void)icb_dowho(is, ig);
[cd7b81d]623}
624
625/*
[4284008]626 * icb_ismod: checks whether group is moderated by "is"
[cd7b81d]627 */
[1c7657c]628int
[4284008]629icb_ismod(struct icb_group *ig, struct icb_session *is)
[cd7b81d]630{
[626f420]631 return (ig->mod == is);
[cd7b81d]632}
633
[1d2125a]634/*
635 * icb_modpermit: checks user against the moderators table if it has
636 * been populated
637 */
638int
[f3c60e6]639icb_modpermit(struct icb_session *is, int enforce)
[1d2125a]640{
641 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
642 extern int modtabcnt;
643
[82d3c1f]644 icbd_modupdate();
[f3c60e6]645 if ((enforce ? 0 : modtabcnt == 0) ||
[1d2125a]646 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
647 (int (*)(const void *, const void *))strcmp))
648 return (1);
649 return (0);
650}
651
[cd7b81d]652/*
653 * icb_pass: passes moderation of group "ig" from "from" to "to",
654 * returns -1 if "from" is not a moderator, 1 if passed
655 * to "to" and 0 otherwise (no moderator or passed to the
656 * internal bot)
657 */
658int
659icb_pass(struct icb_group *ig, struct icb_session *from,
660 struct icb_session *to)
661{
[4284008]662 if (ig->mod && ig->mod != from)
[cd7b81d]663 return (-1);
664 if (!from && !to)
665 return (-1);
[4284008]666 ig->mod = to;
[cd7b81d]667 if (to)
668 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
669 " of %s", from ? from->nick : "server", ig->name);
670 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
671 "to %s", from ? from->nick : "server", to ? to->nick : "server");
672 return (1);
673}
674
675/*
676 * icb_sendfmt: formats a string and sends it over
677 */
678void
679icb_sendfmt(struct icb_session *is, const char *fmt, ...)
680{
681 char buf[ICB_MSGSIZE];
682 va_list ap;
[d8c3967]683 int res, buflen;
[cd7b81d]684
685 va_start(ap, fmt);
[65b48ee]686 res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
[cd7b81d]687 va_end(ap);
[65b48ee]688 if (res < 0) {
689 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
690 return;
691 }
[d8c3967]692 buflen = MIN((size_t)res + 1, sizeof buf - 1);
[cd7b81d]693 buf[0] = buflen;
[7882a6f]694 icbd_send(is, buf, buflen + 1);
[cd7b81d]695}
[626f420]696
[677a45b]697/*
698 * icb_token: copies a sequence of characters delimited by the 'sep' character
699 * from the source buffer 'buf' at offset indicated by 'bufptr' to
700 * the destination buffer 'dst' and sets 'bufptr' to the next byte
701 * after 'sep'.
702 */
703int
[176b6ef]704icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
705 int trim)
[677a45b]706{
707 char *start;
708 int i, ret;
709
[176b6ef]710 if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
[677a45b]711 return (0);
712 if (*bufptr == NULL)
713 *bufptr = buf;
714 start = *bufptr;
715 for (i = *bufptr - buf; i < len; i++, (*bufptr)++) {
716 if (**bufptr == sep || **bufptr == '\0') {
717 /* copy and null terminate the token */
718 ret = strlcpy(dst, start,
[176b6ef]719 MIN(*bufptr - start + 1, dstlen));
[677a45b]720 if (**bufptr != '\0')
721 (*bufptr)++;
[176b6ef]722 if (ret > 0 && trim)
723 ret = icb_trim(dst, dstlen);
[677a45b]724 return (ret);
725 }
726 }
727 /*
728 * Reached the end of the buffer without finding a field separator
729 * nor the end of line character. If we have advanced our pointer
730 * we should copy the resulting single field out.
731 */
732 if (*bufptr - start > 0) {
[176b6ef]733 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
734 if (ret > 0 && trim)
735 ret = icb_trim(dst, dstlen);
[677a45b]736 return (ret);
737 }
738 return (0);
739}
740
[176b6ef]741/*
742 * icb_trim: trims trailing whitespace
743 */
744int
745icb_trim(char *buf, int len)
746{
747 char *p = buf;
748 int i;
749
750 for (i = 0; i < len && *p != '\0'; i++)
751 p++;
752 if (*p == '\0' && p - buf > 0)
753 p--;
754 while (p >= buf && isspace(*p)) {
755 *p = '\0';
756 i--;
757 if (p > buf)
758 p--;
759 }
760 return (i);
761}
762
[626f420]763/*
764 * icb_vis: strnvis-like function that escapes percentages as well
765 */
766int
[b7bc432]767icb_vis(char *dst, const char *src, size_t dstsize, int flags)
[626f420]768{
[ab3bb32]769 int si, di, td;
[626f420]770
[ab3bb32]771 for (si = 0, di = 0; (size_t)di < dstsize - 1 && src[si] != '\0';
772 si++, di++) {
[f73b386]773 if (src[si] == '%') {
774 if ((size_t)di + 1 >= dstsize - 1)
[8d30e02]775 break;
[6755e15]776 dst[di++] = '%';
777 dst[di] = '%';
[f73b386]778 } else if (src[si] == ' ' && flags & VIS_SP)
[b7bc432]779 dst[di] = '_';
780 else if (isgraph(src[si]) || src[si] == ' ')
[626f420]781 dst[di] = src[si];
782 else {
783 td = snprintf(&dst[di], dstsize - di,
784 "\\%03o", (unsigned char)src[si]);
[f73b386]785 if (td == -1 || (size_t)td >= dstsize - di)
[8d30e02]786 break;
[626f420]787 di += td - 1;
788 }
789 }
[8f5ba64]790 dst[MIN((size_t)di, dstsize - 1)] = '\0';
[626f420]791 return (0);
792}
Note: See TracBrowser for help on using the repository browser.