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