source: code/icb.c@ 65b48ee

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

Improve [v]snprintf return value handling

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