source: code/icb.c@ e22d747

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

Improve groups list declaration

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