[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] | 33 | struct icb_grplist groups;
|
---|
| 34 |
|
---|
[cd7b81d] | 35 | extern int creategroups;
|
---|
[0519a87] | 36 | extern char srvname[NI_MAXHOST];
|
---|
[cd7b81d] | 37 |
|
---|
| 38 | void icb_command(struct icb_session *, char *, char *);
|
---|
| 39 | void icb_groupmsg(struct icb_session *, char *);
|
---|
[d45051e] | 40 | int icb_login(struct icb_session *, char *, char *, char *);
|
---|
[4284008] | 41 | int icb_dowho(struct icb_session *, struct icb_group *);
|
---|
[cd7b81d] | 42 |
|
---|
| 43 | /*
|
---|
| 44 | * icb_init: initializes pointers to callbacks
|
---|
| 45 | */
|
---|
| 46 | void
|
---|
[7882a6f] | 47 | icb_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 | */
|
---|
| 64 | void
|
---|
| 65 | icb_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] | 75 | int
|
---|
[cd7b81d] | 76 | icb_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] | 175 | int
|
---|
[626f420] | 176 | icb_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 | */
|
---|
| 253 | void
|
---|
| 254 | icb_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;
|
---|
[65b48ee] | 259 | int res, buflen = 1;
|
---|
[cd7b81d] | 260 |
|
---|
| 261 | if (strlen(msg) == 0) {
|
---|
| 262 | icb_error(is, "Empty message");
|
---|
| 263 | return;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[65b48ee] | 266 | res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
|
---|
[cd7b81d] | 267 | is->nick, ICB_M_SEP, msg);
|
---|
[65b48ee] | 268 | if (res < 0) {
|
---|
| 269 | icb_error(is, "Format error");
|
---|
| 270 | return;
|
---|
| 271 | }
|
---|
| 272 | buflen += MIN((size_t)res, sizeof buf - 1);
|
---|
[cd7b81d] | 273 | buf[0] = buflen;
|
---|
| 274 |
|
---|
[45bd56a] | 275 | logger(ig->name, is->nick, msg);
|
---|
[55923b7] | 276 |
|
---|
[cd7b81d] | 277 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 278 | if (s == is)
|
---|
| 279 | continue;
|
---|
[7882a6f] | 280 | icbd_send(s, buf, buflen + 1);
|
---|
[cd7b81d] | 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /*
|
---|
| 285 | * icb_privmsg: handles personal message ('c') packets
|
---|
| 286 | */
|
---|
| 287 | void
|
---|
[626f420] | 288 | icb_privmsg(struct icb_session *is, char *to, char *msg)
|
---|
[cd7b81d] | 289 | {
|
---|
| 290 | struct icb_group *ig = is->group;
|
---|
| 291 | struct icb_session *s;
|
---|
[626f420] | 292 | char whom[ICB_MAXNICKLEN];
|
---|
| 293 |
|
---|
[b7bc432] | 294 | icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
|
---|
[cd7b81d] | 295 |
|
---|
[efa8586] | 296 | /* try home group first */
|
---|
[cd7b81d] | 297 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 298 | if (strcmp(s->nick, whom) == 0)
|
---|
| 299 | break;
|
---|
| 300 | }
|
---|
| 301 | if (!s) {
|
---|
[efa8586] | 302 | /* try all groups until the first match */
|
---|
| 303 | LIST_FOREACH(ig, &groups, entry) {
|
---|
| 304 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 305 | if (strcmp(s->nick, whom) == 0)
|
---|
| 306 | break;
|
---|
| 307 | }
|
---|
| 308 | if (s)
|
---|
| 309 | break;
|
---|
| 310 | }
|
---|
| 311 | if (!s) {
|
---|
| 312 | icb_error(is, "No such user %s", whom);
|
---|
| 313 | return;
|
---|
| 314 | }
|
---|
[cd7b81d] | 315 | }
|
---|
| 316 | icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /*
|
---|
| 320 | * icb_command: handles command ('h') packets
|
---|
| 321 | */
|
---|
| 322 | void
|
---|
| 323 | icb_command(struct icb_session *is, char *cmd, char *arg)
|
---|
| 324 | {
|
---|
| 325 | void (*handler)(struct icb_session *, char *);
|
---|
[176b6ef] | 326 | char command[ICB_MAXCMDLEN];
|
---|
[cd7b81d] | 327 |
|
---|
[b7bc432] | 328 | icb_vis(command, cmd, sizeof command, VIS_SP);
|
---|
[626f420] | 329 |
|
---|
| 330 | if ((handler = icb_cmd_lookup(command)) == NULL) {
|
---|
| 331 | icb_error(is, "Unsupported command: %s", command);
|
---|
[cd7b81d] | 332 | return;
|
---|
| 333 | }
|
---|
| 334 | handler(is, arg);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /*
|
---|
| 338 | * icb_cmdout: sends out command output ('i') packets, called from the
|
---|
| 339 | * command handlers
|
---|
| 340 | */
|
---|
| 341 | void
|
---|
| 342 | icb_cmdout(struct icb_session *is, int type, char *outmsg)
|
---|
| 343 | {
|
---|
| 344 | char *otype = NULL;
|
---|
| 345 |
|
---|
| 346 | switch (type) {
|
---|
| 347 | case CMDOUT_CO:
|
---|
| 348 | otype = "co";
|
---|
| 349 | break;
|
---|
| 350 | case CMDOUT_EC:
|
---|
| 351 | otype = "ec";
|
---|
| 352 | break;
|
---|
| 353 | case CMDOUT_WG:
|
---|
| 354 | otype = "wg";
|
---|
| 355 | break;
|
---|
[4284008] | 356 | case CMDOUT_WH:
|
---|
| 357 | otype = "wh";
|
---|
| 358 | break;
|
---|
| 359 | case CMDOUT_WL:
|
---|
| 360 | otype = "wl";
|
---|
| 361 | break;
|
---|
[cd7b81d] | 362 | default:
|
---|
[7882a6f] | 363 | icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
|
---|
[cd7b81d] | 364 | return;
|
---|
| 365 | }
|
---|
[4284008] | 366 | if (outmsg)
|
---|
| 367 | icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
|
---|
| 368 | outmsg);
|
---|
| 369 | else
|
---|
| 370 | icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
|
---|
[cd7b81d] | 371 | }
|
---|
| 372 |
|
---|
| 373 | /*
|
---|
| 374 | * icb_status: sends a status message ('d') to the client
|
---|
| 375 | */
|
---|
| 376 | void
|
---|
| 377 | icb_status(struct icb_session *is, int type, const char *fmt, ...)
|
---|
| 378 | {
|
---|
| 379 | va_list ap;
|
---|
| 380 | char buf[ICB_MSGSIZE];
|
---|
[65b48ee] | 381 | int res, buflen = 1;
|
---|
[cd7b81d] | 382 | static const struct {
|
---|
| 383 | int type;
|
---|
| 384 | const char *msg;
|
---|
| 385 | } msgtab[] = {
|
---|
| 386 | { STATUS_ARRIVE, "Arrive" },
|
---|
| 387 | { STATUS_BOOT, "Boot" },
|
---|
| 388 | { STATUS_DEPART, "Depart" },
|
---|
[9195a6a] | 389 | { STATUS_HELP, "Help" },
|
---|
[cd7b81d] | 390 | { STATUS_NAME, "Name" },
|
---|
[bf02a60] | 391 | { STATUS_NOBEEP, "No-Beep" },
|
---|
[cd7b81d] | 392 | { STATUS_NOTIFY, "Notify" },
|
---|
| 393 | { STATUS_SIGNON, "Sign-on" },
|
---|
| 394 | { STATUS_SIGNOFF, "Sign-off" },
|
---|
| 395 | { STATUS_STATUS, "Status" },
|
---|
| 396 | { STATUS_TOPIC, "Topic" },
|
---|
| 397 | { STATUS_WARNING, "Warning" },
|
---|
[1022119] | 398 | { 0, NULL }
|
---|
[cd7b81d] | 399 | };
|
---|
| 400 |
|
---|
| 401 | if (type < 0 || type > (int)nitems(msgtab) - 1)
|
---|
| 402 | return;
|
---|
[65b48ee] | 403 | res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
|
---|
[cd7b81d] | 404 | msgtab[type].msg, ICB_M_SEP);
|
---|
[65b48ee] | 405 | if (res < 0) {
|
---|
| 406 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
| 407 | return;
|
---|
| 408 | }
|
---|
| 409 | buflen += MIN((size_t)res, sizeof buf - 1);
|
---|
| 410 | if ((size_t)buflen >= sizeof buf) {
|
---|
| 411 | icbd_log(NULL, LOG_ERR, "Status buffer too small");
|
---|
| 412 | return;
|
---|
| 413 | }
|
---|
| 414 | va_start(ap, fmt);
|
---|
| 415 | res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
|
---|
[cd7b81d] | 416 | va_end(ap);
|
---|
[65b48ee] | 417 | if (res < 0) {
|
---|
| 418 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
| 419 | return;
|
---|
| 420 | }
|
---|
| 421 | buflen += MIN((size_t)res, sizeof buf - buflen);
|
---|
| 422 | buf[0] = buflen;
|
---|
[7882a6f] | 423 | icbd_send(is, buf, buflen + 1);
|
---|
[cd7b81d] | 424 | }
|
---|
| 425 |
|
---|
| 426 | /*
|
---|
| 427 | * icb_status: sends a status message ('d') to the group except of the
|
---|
| 428 | * "ex" if it's not NULL
|
---|
| 429 | */
|
---|
| 430 | void
|
---|
| 431 | icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
|
---|
| 432 | const char *fmt, ...)
|
---|
| 433 | {
|
---|
[a2fadb4] | 434 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
[cd7b81d] | 435 | va_list ap;
|
---|
| 436 | struct icb_session *s;
|
---|
| 437 |
|
---|
| 438 | va_start(ap, fmt);
|
---|
| 439 | (void)vsnprintf(buf, sizeof buf, fmt, ap);
|
---|
| 440 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 441 | if (ex && s == ex)
|
---|
| 442 | continue;
|
---|
| 443 | icb_status(s, type, buf);
|
---|
| 444 | }
|
---|
[23ca6f1] | 445 | logger(ig->name, "", buf);
|
---|
[7882a6f] | 446 | icbd_log(NULL, LOG_DEBUG, "%s", buf);
|
---|
[cd7b81d] | 447 | va_end(ap);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | /*
|
---|
| 451 | * icb_error: sends an error message ('e') to the client
|
---|
| 452 | */
|
---|
| 453 | void
|
---|
| 454 | icb_error(struct icb_session *is, const char *fmt, ...)
|
---|
| 455 | {
|
---|
| 456 | char buf[ICB_MSGSIZE];
|
---|
| 457 | va_list ap;
|
---|
[65b48ee] | 458 | int res, buflen = 1;
|
---|
[cd7b81d] | 459 |
|
---|
| 460 | va_start(ap, fmt);
|
---|
[65b48ee] | 461 | res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
|
---|
[cd7b81d] | 462 | va_end(ap);
|
---|
[65b48ee] | 463 | if (res < 0) {
|
---|
| 464 | icbd_log(NULL, LOG_ERR, "Format error");
|
---|
| 465 | return;
|
---|
| 466 | }
|
---|
| 467 | buflen += MIN((size_t)res, sizeof buf - 2);
|
---|
[cd7b81d] | 468 | buf[0] = ++buflen; /* account for ICB_M_ERROR */
|
---|
| 469 | buf[1] = ICB_M_ERROR;
|
---|
[7882a6f] | 470 | icbd_send(is, buf, buflen + 1);
|
---|
| 471 | icbd_log(is, LOG_DEBUG, "%s", buf + 2);
|
---|
[cd7b81d] | 472 | }
|
---|
| 473 |
|
---|
| 474 | /*
|
---|
| 475 | * icb_remove: removes a session from the associated group
|
---|
| 476 | */
|
---|
| 477 | void
|
---|
| 478 | icb_remove(struct icb_session *is, char *reason)
|
---|
| 479 | {
|
---|
| 480 | if (is->group) {
|
---|
[4284008] | 481 | if (icb_ismod(is->group, is))
|
---|
[cd7b81d] | 482 | (void)icb_pass(is->group, is, NULL);
|
---|
| 483 | LIST_REMOVE(is, entry);
|
---|
| 484 | if (reason)
|
---|
| 485 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
| 486 | "%s (%s@%s) just left: %s", is->nick, is->client,
|
---|
| 487 | is->host, reason);
|
---|
| 488 | else
|
---|
| 489 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
| 490 | "%s (%s@%s) just left", is->nick, is->client,
|
---|
| 491 | is->host);
|
---|
[96a2e31] | 492 | is->group = NULL;
|
---|
[cd7b81d] | 493 | }
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | /*
|
---|
| 497 | * icb_addgroup: adds a new group to the list
|
---|
| 498 | */
|
---|
| 499 | struct icb_group *
|
---|
[677a45b] | 500 | icb_addgroup(struct icb_session *is, char *name)
|
---|
[cd7b81d] | 501 | {
|
---|
| 502 | struct icb_group *ig;
|
---|
| 503 |
|
---|
| 504 | if ((ig = calloc(1, sizeof *ig)) == NULL)
|
---|
| 505 | return (NULL);
|
---|
| 506 | strlcpy(ig->name, name, sizeof ig->name);
|
---|
| 507 | if (is)
|
---|
[4284008] | 508 | ig->mod = is;
|
---|
[cd7b81d] | 509 | LIST_INIT(&ig->sess);
|
---|
| 510 | LIST_INSERT_HEAD(&groups, ig, entry);
|
---|
| 511 | return (ig);
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | #ifdef notused
|
---|
| 515 | /*
|
---|
| 516 | * icb_delgroup: removes a group from the list
|
---|
| 517 | */
|
---|
| 518 | void
|
---|
| 519 | icb_delgroup(struct icb_group *ig)
|
---|
| 520 | {
|
---|
| 521 | struct icb_session *s;
|
---|
| 522 |
|
---|
| 523 | /* well, i guess we should kick out participants! ;-) */
|
---|
| 524 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 525 | icb_status(s, STATUS_WARNING, "Group dismissed");
|
---|
| 526 | s->group = NULL;
|
---|
| 527 | }
|
---|
| 528 | LIST_REMOVE(ig, entry);
|
---|
| 529 | bzero(ig, sizeof ig); /* paranoic thing, obviously */
|
---|
| 530 | free(ig);
|
---|
| 531 | }
|
---|
| 532 | #endif
|
---|
| 533 |
|
---|
| 534 | /*
|
---|
[4284008] | 535 | * icb_dowho: a helper function that sends out a group header as a command
|
---|
| 536 | * output and user information in the "wl" format
|
---|
[cd7b81d] | 537 | */
|
---|
[4284008] | 538 | int
|
---|
| 539 | icb_dowho(struct icb_session *is, struct icb_group *ig)
|
---|
[cd7b81d] | 540 | {
|
---|
[a2fadb4] | 541 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
[cd7b81d] | 542 | struct icb_session *s;
|
---|
[5dfeb4c] | 543 | time_t now;
|
---|
[4284008] | 544 | int nusers = 0;
|
---|
[cd7b81d] | 545 |
|
---|
[5dfeb4c] | 546 | now = getmonotime();
|
---|
[4284008] | 547 | icb_cmdout(is, CMDOUT_CO, " ");
|
---|
| 548 | snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
|
---|
| 549 | ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
|
---|
| 550 | strlen(ig->topic) > 0 ? ig->topic : "(None)");
|
---|
| 551 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
[cd7b81d] | 552 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 553 | (void)snprintf(buf, sizeof buf,
|
---|
[bf02a60] | 554 | "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
|
---|
[4284008] | 555 | icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
|
---|
[5dfeb4c] | 556 | s->nick, ICB_M_SEP, now - s->last,
|
---|
[cd7b81d] | 557 | ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
|
---|
[c102bbf] | 558 | s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
|
---|
[cd7b81d] | 559 | icb_cmdout(is, CMDOUT_WL, buf);
|
---|
[4284008] | 560 | nusers++;
|
---|
[cd7b81d] | 561 | }
|
---|
[4284008] | 562 | return (nusers);
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | /*
|
---|
| 566 | * icb_who: sends a list of users of either the specified group or all
|
---|
| 567 | * groups found on the server
|
---|
| 568 | */
|
---|
| 569 | void
|
---|
| 570 | icb_who(struct icb_session *is, struct icb_group *ig)
|
---|
| 571 | {
|
---|
[a2fadb4] | 572 | char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
|
---|
[4284008] | 573 | struct icb_group *g;
|
---|
| 574 |
|
---|
| 575 | if (!ig) {
|
---|
| 576 | int nusers = 0, ngroups = 0;
|
---|
| 577 |
|
---|
| 578 | LIST_FOREACH(g, &groups, entry) {
|
---|
| 579 | nusers += icb_dowho(is, g);
|
---|
| 580 | ngroups++;
|
---|
| 581 | }
|
---|
| 582 | if (nusers > 0) {
|
---|
| 583 | (void)snprintf(buf, sizeof buf,
|
---|
| 584 | "Total: %d %s in %d %s",
|
---|
| 585 | nusers, nusers > 1 ? "users" : "user",
|
---|
| 586 | ngroups, ngroups > 1 ? "groups" : "group");
|
---|
| 587 | } else
|
---|
| 588 | (void)snprintf(buf, sizeof buf, "No users found.");
|
---|
| 589 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
| 590 | } else
|
---|
| 591 | (void)icb_dowho(is, ig);
|
---|
[cd7b81d] | 592 | }
|
---|
| 593 |
|
---|
| 594 | /*
|
---|
[4284008] | 595 | * icb_ismod: checks whether group is moderated by "is"
|
---|
[cd7b81d] | 596 | */
|
---|
[626f420] | 597 | inline int
|
---|
[4284008] | 598 | icb_ismod(struct icb_group *ig, struct icb_session *is)
|
---|
[cd7b81d] | 599 | {
|
---|
[626f420] | 600 | return (ig->mod == is);
|
---|
[cd7b81d] | 601 | }
|
---|
| 602 |
|
---|
[1d2125a] | 603 | /*
|
---|
| 604 | * icb_modpermit: checks user against the moderators table if it has
|
---|
| 605 | * been populated
|
---|
| 606 | */
|
---|
| 607 | int
|
---|
[f3c60e6] | 608 | icb_modpermit(struct icb_session *is, int enforce)
|
---|
[1d2125a] | 609 | {
|
---|
| 610 | extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
|
---|
| 611 | extern int modtabcnt;
|
---|
| 612 |
|
---|
[82d3c1f] | 613 | icbd_modupdate();
|
---|
[f3c60e6] | 614 | if ((enforce ? 0 : modtabcnt == 0) ||
|
---|
[1d2125a] | 615 | bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
|
---|
| 616 | (int (*)(const void *, const void *))strcmp))
|
---|
| 617 | return (1);
|
---|
| 618 | return (0);
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[cd7b81d] | 621 | /*
|
---|
| 622 | * icb_pass: passes moderation of group "ig" from "from" to "to",
|
---|
| 623 | * returns -1 if "from" is not a moderator, 1 if passed
|
---|
| 624 | * to "to" and 0 otherwise (no moderator or passed to the
|
---|
| 625 | * internal bot)
|
---|
| 626 | */
|
---|
| 627 | int
|
---|
| 628 | icb_pass(struct icb_group *ig, struct icb_session *from,
|
---|
| 629 | struct icb_session *to)
|
---|
| 630 | {
|
---|
[4284008] | 631 | if (ig->mod && ig->mod != from)
|
---|
[cd7b81d] | 632 | return (-1);
|
---|
| 633 | if (!from && !to)
|
---|
| 634 | return (-1);
|
---|
[4284008] | 635 | ig->mod = to;
|
---|
[cd7b81d] | 636 | if (to)
|
---|
| 637 | icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
|
---|
| 638 | " of %s", from ? from->nick : "server", ig->name);
|
---|
| 639 | icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
|
---|
| 640 | "to %s", from ? from->nick : "server", to ? to->nick : "server");
|
---|
| 641 | return (1);
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | /*
|
---|
| 645 | * icb_sendfmt: formats a string and sends it over
|
---|
| 646 | */
|
---|
| 647 | void
|
---|
| 648 | icb_sendfmt(struct icb_session *is, const char *fmt, ...)
|
---|
| 649 | {
|
---|
| 650 | char buf[ICB_MSGSIZE];
|
---|
| 651 | va_list ap;
|
---|
[65b48ee] | 652 | int res, buflen = 1;
|
---|
[cd7b81d] | 653 |
|
---|
| 654 | va_start(ap, fmt);
|
---|
[65b48ee] | 655 | res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
|
---|
[cd7b81d] | 656 | va_end(ap);
|
---|
[65b48ee] | 657 | if (res < 0) {
|
---|
| 658 | icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
|
---|
| 659 | return;
|
---|
| 660 | }
|
---|
| 661 | buflen += MIN((size_t)res, sizeof buf - 1);
|
---|
[cd7b81d] | 662 | buf[0] = buflen;
|
---|
[7882a6f] | 663 | icbd_send(is, buf, buflen + 1);
|
---|
[cd7b81d] | 664 | }
|
---|
[626f420] | 665 |
|
---|
[677a45b] | 666 | /*
|
---|
| 667 | * icb_token: copies a sequence of characters delimited by the 'sep' character
|
---|
| 668 | * from the source buffer 'buf' at offset indicated by 'bufptr' to
|
---|
| 669 | * the destination buffer 'dst' and sets 'bufptr' to the next byte
|
---|
| 670 | * after 'sep'.
|
---|
| 671 | */
|
---|
| 672 | int
|
---|
[176b6ef] | 673 | icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
|
---|
| 674 | int trim)
|
---|
[677a45b] | 675 | {
|
---|
| 676 | char *start;
|
---|
| 677 | int i, ret;
|
---|
| 678 |
|
---|
[176b6ef] | 679 | if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
|
---|
[677a45b] | 680 | return (0);
|
---|
| 681 | if (*bufptr == NULL)
|
---|
| 682 | *bufptr = buf;
|
---|
| 683 | start = *bufptr;
|
---|
| 684 | for (i = *bufptr - buf; i < len; i++, (*bufptr)++) {
|
---|
| 685 | if (**bufptr == sep || **bufptr == '\0') {
|
---|
| 686 | /* copy and null terminate the token */
|
---|
| 687 | ret = strlcpy(dst, start,
|
---|
[176b6ef] | 688 | MIN(*bufptr - start + 1, dstlen));
|
---|
[677a45b] | 689 | if (**bufptr != '\0')
|
---|
| 690 | (*bufptr)++;
|
---|
[176b6ef] | 691 | if (ret > 0 && trim)
|
---|
| 692 | ret = icb_trim(dst, dstlen);
|
---|
[677a45b] | 693 | return (ret);
|
---|
| 694 | }
|
---|
| 695 | }
|
---|
| 696 | /*
|
---|
| 697 | * Reached the end of the buffer without finding a field separator
|
---|
| 698 | * nor the end of line character. If we have advanced our pointer
|
---|
| 699 | * we should copy the resulting single field out.
|
---|
| 700 | */
|
---|
| 701 | if (*bufptr - start > 0) {
|
---|
[176b6ef] | 702 | ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
|
---|
| 703 | if (ret > 0 && trim)
|
---|
| 704 | ret = icb_trim(dst, dstlen);
|
---|
[677a45b] | 705 | return (ret);
|
---|
| 706 | }
|
---|
| 707 | return (0);
|
---|
| 708 | }
|
---|
| 709 |
|
---|
[176b6ef] | 710 | /*
|
---|
| 711 | * icb_trim: trims trailing whitespace
|
---|
| 712 | */
|
---|
| 713 | int
|
---|
| 714 | icb_trim(char *buf, int len)
|
---|
| 715 | {
|
---|
| 716 | char *p = buf;
|
---|
| 717 | int i;
|
---|
| 718 |
|
---|
| 719 | for (i = 0; i < len && *p != '\0'; i++)
|
---|
| 720 | p++;
|
---|
| 721 | if (*p == '\0' && p - buf > 0)
|
---|
| 722 | p--;
|
---|
| 723 | while (p >= buf && isspace(*p)) {
|
---|
| 724 | *p = '\0';
|
---|
| 725 | i--;
|
---|
| 726 | if (p > buf)
|
---|
| 727 | p--;
|
---|
| 728 | }
|
---|
| 729 | return (i);
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[626f420] | 732 | /*
|
---|
| 733 | * icb_vis: strnvis-like function that escapes percentages as well
|
---|
| 734 | */
|
---|
| 735 | int
|
---|
[b7bc432] | 736 | icb_vis(char *dst, const char *src, size_t dstsize, int flags)
|
---|
[626f420] | 737 | {
|
---|
| 738 | int si = 0, di = 0, td;
|
---|
| 739 |
|
---|
[8f5ba64] | 740 | while ((size_t)di < dstsize - 1 && src[si] != '\0') {
|
---|
[f73b386] | 741 | if (src[si] == '%') {
|
---|
| 742 | if ((size_t)di + 1 >= dstsize - 1)
|
---|
[8d30e02] | 743 | break;
|
---|
[626f420] | 744 | dst[di++] = '%', dst[di] = '%';
|
---|
[f73b386] | 745 | } else if (src[si] == ' ' && flags & VIS_SP)
|
---|
[b7bc432] | 746 | dst[di] = '_';
|
---|
| 747 | else if (isgraph(src[si]) || src[si] == ' ')
|
---|
[626f420] | 748 | dst[di] = src[si];
|
---|
| 749 | else {
|
---|
| 750 | td = snprintf(&dst[di], dstsize - di,
|
---|
| 751 | "\\%03o", (unsigned char)src[si]);
|
---|
[f73b386] | 752 | if (td == -1 || (size_t)td >= dstsize - di)
|
---|
[8d30e02] | 753 | break;
|
---|
[626f420] | 754 | di += td - 1;
|
---|
| 755 | }
|
---|
| 756 | si++, di++;
|
---|
| 757 | }
|
---|
[8f5ba64] | 758 | dst[MIN((size_t)di, dstsize - 1)] = '\0';
|
---|
[626f420] | 759 | return (0);
|
---|
| 760 | }
|
---|