[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 |
|
---|
| 17 | #include <sys/param.h>
|
---|
| 18 | #include <sys/queue.h>
|
---|
| 19 | #include <stdarg.h>
|
---|
| 20 | #include <stdio.h>
|
---|
| 21 | #include <stdlib.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <syslog.h>
|
---|
| 24 | #include <unistd.h>
|
---|
[626f420] | 25 | #include <ctype.h>
|
---|
[cd7b81d] | 26 | #include <event.h>
|
---|
[b7bc432] | 27 | #include <vis.h>
|
---|
[cd7b81d] | 28 |
|
---|
| 29 | #include "icb.h"
|
---|
| 30 | #include "icbd.h"
|
---|
| 31 |
|
---|
| 32 | extern int creategroups;
|
---|
[a785c27] | 33 | extern char srvname[MAXHOSTNAMELEN];
|
---|
[cd7b81d] | 34 |
|
---|
| 35 | void icb_command(struct icb_session *, char *, char *);
|
---|
| 36 | void icb_groupmsg(struct icb_session *, char *);
|
---|
| 37 | void icb_login(struct icb_session *, char *, char *, char *);
|
---|
[4284008] | 38 | int icb_dowho(struct icb_session *, struct icb_group *);
|
---|
[1d2125a] | 39 | int icb_modpermit(struct icb_session *);
|
---|
[cd7b81d] | 40 | char *icb_nextfield(char **);
|
---|
| 41 |
|
---|
| 42 | /*
|
---|
| 43 | * icb_init: initializes pointers to callbacks
|
---|
| 44 | */
|
---|
| 45 | void
|
---|
| 46 | icb_init(struct icbd_callbacks *ic)
|
---|
| 47 | {
|
---|
| 48 | icb_drop = ic->drop;
|
---|
| 49 | icb_log = ic->log;
|
---|
| 50 | icb_send = ic->send;
|
---|
| 51 |
|
---|
| 52 | LIST_INIT(&groups);
|
---|
[a785c27] | 53 |
|
---|
| 54 | if (strlen(srvname) == 0)
|
---|
| 55 | (void)gethostname(srvname, sizeof srvname);
|
---|
| 56 | /*
|
---|
| 57 | * MAXHOSTNAMELEN is usually greater than what we
|
---|
| 58 | * can actually send, hence truncation:
|
---|
| 59 | */
|
---|
| 60 | if (strlen(srvname) > 200)
|
---|
| 61 | srvname[200] = '\0';
|
---|
[cd7b81d] | 62 | }
|
---|
| 63 |
|
---|
| 64 | /*
|
---|
| 65 | * icb_start: called upon accepting a new connection, greets new client
|
---|
| 66 | */
|
---|
| 67 | void
|
---|
| 68 | icb_start(struct icb_session *is)
|
---|
| 69 | {
|
---|
[a785c27] | 70 | icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
|
---|
[cd7b81d] | 71 | ICB_M_SEP, "icbd");
|
---|
| 72 | SETF(is->flags, ICB_SF_PROTOSENT);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /*
|
---|
| 76 | * icb_input: main input processing routine
|
---|
| 77 | */
|
---|
| 78 | void
|
---|
| 79 | icb_input(struct icb_session *is)
|
---|
| 80 | {
|
---|
| 81 | char *msg = is->buffer;
|
---|
| 82 | char type;
|
---|
| 83 |
|
---|
| 84 | is->last = getmonotime();
|
---|
| 85 | type = msg[1];
|
---|
| 86 | msg += 2;
|
---|
| 87 | if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
|
---|
| 88 | icb_error(is, "Not logged in");
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 | switch (type) {
|
---|
| 92 | case ICB_M_LOGIN: {
|
---|
| 93 | char *nick, *group, *client, *cmd;
|
---|
| 94 |
|
---|
| 95 | client = icb_nextfield(&msg);
|
---|
| 96 | nick = icb_nextfield(&msg);
|
---|
| 97 | group = icb_nextfield(&msg);
|
---|
| 98 | cmd = icb_nextfield(&msg);
|
---|
| 99 | if (strlen(cmd) > 0 && cmd[0] == 'w') {
|
---|
| 100 | icb_error(is, "Command not implemented");
|
---|
| 101 | icb_drop(is, NULL);
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
[1da9ee5] | 104 | if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
|
---|
| 105 | icb_error(is, "Malformed login packet");
|
---|
| 106 | icb_drop(is, NULL);
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
[cd7b81d] | 109 | icb_login(is, group, nick, client);
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 | case ICB_M_OPEN: {
|
---|
| 113 | char *grpmsg;
|
---|
| 114 |
|
---|
| 115 | grpmsg = icb_nextfield(&msg);
|
---|
| 116 | icb_groupmsg(is, grpmsg);
|
---|
| 117 | break;
|
---|
| 118 | }
|
---|
| 119 | case ICB_M_COMMAND: {
|
---|
| 120 | char *cmd, *arg;
|
---|
| 121 |
|
---|
| 122 | cmd = icb_nextfield(&msg);
|
---|
| 123 | arg = icb_nextfield(&msg);
|
---|
| 124 | icb_command(is, cmd, arg);
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
[1fb1bbe] | 127 | case ICB_M_PONG: {
|
---|
| 128 | icb_sendfmt(is, "%c", ICB_M_PING);
|
---|
| 129 | break;
|
---|
| 130 | }
|
---|
[cd7b81d] | 131 | case ICB_M_PROTO:
|
---|
| 132 | case ICB_M_NOOP:
|
---|
| 133 | /* ignore */
|
---|
| 134 | break;
|
---|
| 135 | default:
|
---|
| 136 | /* everything else is not valid */
|
---|
| 137 | icb_error(is, "Bummer. This is a bummer, man.");
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /*
|
---|
| 142 | * icb_login: handles login ('a') packets
|
---|
| 143 | */
|
---|
| 144 | void
|
---|
[626f420] | 145 | icb_login(struct icb_session *is, char *grp, char *nick, char *client)
|
---|
[cd7b81d] | 146 | {
|
---|
| 147 | char *defgrp = "1";
|
---|
| 148 | struct icb_group *ig;
|
---|
| 149 | struct icb_session *s;
|
---|
[626f420] | 150 | char group[ICB_MAXGRPLEN];
|
---|
[cd7b81d] | 151 |
|
---|
[626f420] | 152 | if (!nick || strlen(nick) == 0 ||
|
---|
[b7bc432] | 153 | icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
|
---|
[cd7b81d] | 154 | icb_error(is, "Invalid nick");
|
---|
| 155 | icb_drop(is, NULL);
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
[626f420] | 158 | if (!grp || strlen(grp) == 0)
|
---|
| 159 | strlcpy(group, defgrp, ICB_MAXGRPLEN);
|
---|
| 160 | else
|
---|
[b7bc432] | 161 | icb_vis(group, grp, ICB_MAXNICKLEN, VIS_SP);
|
---|
[cd7b81d] | 162 | LIST_FOREACH(ig, &groups, entry) {
|
---|
| 163 | if (strcmp(ig->name, group) == 0)
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | if (ig == NULL) {
|
---|
| 167 | if (!creategroups) {
|
---|
| 168 | icb_error(is, "Invalid group %s", group);
|
---|
| 169 | icb_drop(is, NULL);
|
---|
| 170 | return;
|
---|
| 171 | } else {
|
---|
| 172 | if ((ig = icb_addgroup(is, group, NULL)) == NULL) {
|
---|
| 173 | icb_error(is, "Can't create group %s", group);
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 | icb_log(NULL, LOG_DEBUG, "%s created group %s",
|
---|
[626f420] | 177 | is->nick, group);
|
---|
[cd7b81d] | 178 | }
|
---|
| 179 | }
|
---|
| 180 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
[626f420] | 181 | if (strcmp(s->nick, is->nick) == 0) {
|
---|
[cd7b81d] | 182 | icb_error(is, "Nick is already in use");
|
---|
| 183 | icb_drop(is, NULL);
|
---|
| 184 | return;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (client && strlen(client) > 0)
|
---|
[b7bc432] | 189 | icb_vis(is->client, client, sizeof is->client, VIS_SP);
|
---|
[cd7b81d] | 190 | is->group = ig;
|
---|
| 191 | is->login = time(NULL);
|
---|
| 192 | is->last = getmonotime();
|
---|
| 193 |
|
---|
| 194 | /* notify group */
|
---|
| 195 | icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
|
---|
| 196 | is->nick, is->client, is->host);
|
---|
| 197 |
|
---|
| 198 | CLRF(is->flags, ICB_SF_PROTOSENT);
|
---|
| 199 | SETF(is->flags, ICB_SF_LOGGEDIN);
|
---|
| 200 |
|
---|
| 201 | LIST_INSERT_HEAD(&ig->sess, is, entry);
|
---|
| 202 |
|
---|
| 203 | /* acknowledge successful login */
|
---|
| 204 | icb_sendfmt(is, "%c", ICB_M_LOGIN);
|
---|
| 205 |
|
---|
| 206 | /* notify user */
|
---|
| 207 | icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
|
---|
[4284008] | 208 | icb_ismod(ig, is) ? " as moderator" : "");
|
---|
[cd7b81d] | 209 |
|
---|
| 210 | /* send user a topic name */
|
---|
| 211 | if (strlen(ig->topic) > 0)
|
---|
| 212 | icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
|
---|
| 213 | ig->name, ig->topic);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /*
|
---|
| 217 | * icb_groupmsg: handles open message ('b') packets
|
---|
| 218 | */
|
---|
| 219 | void
|
---|
| 220 | icb_groupmsg(struct icb_session *is, char *msg)
|
---|
| 221 | {
|
---|
| 222 | char buf[ICB_MSGSIZE];
|
---|
| 223 | struct icb_group *ig = is->group;
|
---|
| 224 | struct icb_session *s;
|
---|
| 225 | int buflen = 1;
|
---|
| 226 |
|
---|
| 227 | if (strlen(msg) == 0) {
|
---|
| 228 | icb_error(is, "Empty message");
|
---|
| 229 | return;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
|
---|
| 233 | is->nick, ICB_M_SEP, msg);
|
---|
| 234 | buf[0] = buflen;
|
---|
| 235 |
|
---|
[45bd56a] | 236 | logger(ig->name, is->nick, msg);
|
---|
[55923b7] | 237 |
|
---|
[cd7b81d] | 238 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 239 | if (s == is)
|
---|
| 240 | continue;
|
---|
| 241 | icb_send(s, buf, buflen + 1);
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /*
|
---|
| 246 | * icb_privmsg: handles personal message ('c') packets
|
---|
| 247 | */
|
---|
| 248 | void
|
---|
[626f420] | 249 | icb_privmsg(struct icb_session *is, char *to, char *msg)
|
---|
[cd7b81d] | 250 | {
|
---|
| 251 | struct icb_group *ig = is->group;
|
---|
| 252 | struct icb_session *s;
|
---|
[626f420] | 253 | char whom[ICB_MAXNICKLEN];
|
---|
| 254 |
|
---|
[b7bc432] | 255 | icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
|
---|
[cd7b81d] | 256 |
|
---|
[efa8586] | 257 | /* try home group first */
|
---|
[cd7b81d] | 258 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 259 | if (strcmp(s->nick, whom) == 0)
|
---|
| 260 | break;
|
---|
| 261 | }
|
---|
| 262 | if (!s) {
|
---|
[efa8586] | 263 | /* try all groups until the first match */
|
---|
| 264 | LIST_FOREACH(ig, &groups, entry) {
|
---|
| 265 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 266 | if (strcmp(s->nick, whom) == 0)
|
---|
| 267 | break;
|
---|
| 268 | }
|
---|
| 269 | if (s)
|
---|
| 270 | break;
|
---|
| 271 | }
|
---|
| 272 | if (!s) {
|
---|
| 273 | icb_error(is, "No such user %s", whom);
|
---|
| 274 | return;
|
---|
| 275 | }
|
---|
[cd7b81d] | 276 | }
|
---|
| 277 | icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * icb_command: handles command ('h') packets
|
---|
| 282 | */
|
---|
| 283 | void
|
---|
| 284 | icb_command(struct icb_session *is, char *cmd, char *arg)
|
---|
| 285 | {
|
---|
| 286 | void (*handler)(struct icb_session *, char *);
|
---|
[626f420] | 287 | char command[32]; /* XXX */
|
---|
[cd7b81d] | 288 |
|
---|
[b7bc432] | 289 | icb_vis(command, cmd, sizeof command, VIS_SP);
|
---|
[626f420] | 290 |
|
---|
| 291 | if ((handler = icb_cmd_lookup(command)) == NULL) {
|
---|
| 292 | icb_error(is, "Unsupported command: %s", command);
|
---|
[cd7b81d] | 293 | return;
|
---|
| 294 | }
|
---|
| 295 | handler(is, arg);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /*
|
---|
| 299 | * icb_cmdout: sends out command output ('i') packets, called from the
|
---|
| 300 | * command handlers
|
---|
| 301 | */
|
---|
| 302 | void
|
---|
| 303 | icb_cmdout(struct icb_session *is, int type, char *outmsg)
|
---|
| 304 | {
|
---|
| 305 | char *otype = NULL;
|
---|
| 306 |
|
---|
| 307 | switch (type) {
|
---|
| 308 | case CMDOUT_CO:
|
---|
| 309 | otype = "co";
|
---|
| 310 | break;
|
---|
| 311 | case CMDOUT_EC:
|
---|
| 312 | otype = "ec";
|
---|
| 313 | break;
|
---|
| 314 | case CMDOUT_WG:
|
---|
| 315 | otype = "wg";
|
---|
| 316 | break;
|
---|
[4284008] | 317 | case CMDOUT_WH:
|
---|
| 318 | otype = "wh";
|
---|
| 319 | break;
|
---|
| 320 | case CMDOUT_WL:
|
---|
| 321 | otype = "wl";
|
---|
| 322 | break;
|
---|
[cd7b81d] | 323 | default:
|
---|
| 324 | icb_log(is, LOG_ERR, "unknown cmdout type");
|
---|
| 325 | return;
|
---|
| 326 | }
|
---|
[4284008] | 327 | if (outmsg)
|
---|
| 328 | icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
|
---|
| 329 | outmsg);
|
---|
| 330 | else
|
---|
| 331 | icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
|
---|
[cd7b81d] | 332 | }
|
---|
| 333 |
|
---|
| 334 | /*
|
---|
| 335 | * icb_status: sends a status message ('d') to the client
|
---|
| 336 | */
|
---|
| 337 | void
|
---|
| 338 | icb_status(struct icb_session *is, int type, const char *fmt, ...)
|
---|
| 339 | {
|
---|
| 340 | va_list ap;
|
---|
| 341 | char buf[ICB_MSGSIZE];
|
---|
| 342 | int buflen = 1;
|
---|
| 343 | static const struct {
|
---|
| 344 | int type;
|
---|
| 345 | const char *msg;
|
---|
| 346 | } msgtab[] = {
|
---|
| 347 | { STATUS_ARRIVE, "Arrive" },
|
---|
| 348 | { STATUS_BOOT, "Boot" },
|
---|
| 349 | { STATUS_DEPART, "Depart" },
|
---|
[9195a6a] | 350 | { STATUS_HELP, "Help" },
|
---|
[cd7b81d] | 351 | { STATUS_NAME, "Name" },
|
---|
[bf02a60] | 352 | { STATUS_NOBEEP, "No-Beep" },
|
---|
[cd7b81d] | 353 | { STATUS_NOTIFY, "Notify" },
|
---|
| 354 | { STATUS_SIGNON, "Sign-on" },
|
---|
| 355 | { STATUS_SIGNOFF, "Sign-off" },
|
---|
| 356 | { STATUS_STATUS, "Status" },
|
---|
| 357 | { STATUS_TOPIC, "Topic" },
|
---|
| 358 | { STATUS_WARNING, "Warning" },
|
---|
[1022119] | 359 | { 0, NULL }
|
---|
[cd7b81d] | 360 | };
|
---|
| 361 |
|
---|
| 362 | if (type < 0 || type > (int)nitems(msgtab) - 1)
|
---|
| 363 | return;
|
---|
| 364 | va_start(ap, fmt);
|
---|
| 365 | buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
|
---|
| 366 | msgtab[type].msg, ICB_M_SEP);
|
---|
| 367 | buflen += vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
|
---|
| 368 | buf[0] = buflen;
|
---|
| 369 | va_end(ap);
|
---|
| 370 | icb_send(is, buf, buflen + 1);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /*
|
---|
| 374 | * icb_status: sends a status message ('d') to the group except of the
|
---|
| 375 | * "ex" if it's not NULL
|
---|
| 376 | */
|
---|
| 377 | void
|
---|
| 378 | icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
|
---|
| 379 | const char *fmt, ...)
|
---|
| 380 | {
|
---|
| 381 | char buf[ICB_MSGSIZE];
|
---|
| 382 | va_list ap;
|
---|
| 383 | struct icb_session *s;
|
---|
| 384 |
|
---|
| 385 | va_start(ap, fmt);
|
---|
| 386 | (void)vsnprintf(buf, sizeof buf, fmt, ap);
|
---|
| 387 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 388 | if (ex && s == ex)
|
---|
| 389 | continue;
|
---|
| 390 | icb_status(s, type, buf);
|
---|
| 391 | }
|
---|
[23ca6f1] | 392 | logger(ig->name, "", buf);
|
---|
[cd7b81d] | 393 | icb_log(NULL, LOG_DEBUG, "%s", buf);
|
---|
| 394 | va_end(ap);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | /*
|
---|
| 398 | * icb_error: sends an error message ('e') to the client
|
---|
| 399 | */
|
---|
| 400 | void
|
---|
| 401 | icb_error(struct icb_session *is, const char *fmt, ...)
|
---|
| 402 | {
|
---|
| 403 | char buf[ICB_MSGSIZE];
|
---|
| 404 | va_list ap;
|
---|
| 405 | int buflen = 1;
|
---|
| 406 |
|
---|
| 407 | va_start(ap, fmt);
|
---|
| 408 | buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
|
---|
| 409 | va_end(ap);
|
---|
| 410 | buf[0] = ++buflen; /* account for ICB_M_ERROR */
|
---|
| 411 | buf[1] = ICB_M_ERROR;
|
---|
| 412 | icb_send(is, buf, buflen + 1);
|
---|
| 413 | icb_log(is, LOG_DEBUG, "%s", buf + 2);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /*
|
---|
| 417 | * icb_remove: removes a session from the associated group
|
---|
| 418 | */
|
---|
| 419 | void
|
---|
| 420 | icb_remove(struct icb_session *is, char *reason)
|
---|
| 421 | {
|
---|
| 422 | if (is->group) {
|
---|
[4284008] | 423 | if (icb_ismod(is->group, is))
|
---|
[cd7b81d] | 424 | (void)icb_pass(is->group, is, NULL);
|
---|
| 425 | LIST_REMOVE(is, entry);
|
---|
| 426 | if (reason)
|
---|
| 427 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
| 428 | "%s (%s@%s) just left: %s", is->nick, is->client,
|
---|
| 429 | is->host, reason);
|
---|
| 430 | else
|
---|
| 431 | icb_status_group(is->group, NULL, STATUS_SIGNOFF,
|
---|
| 432 | "%s (%s@%s) just left", is->nick, is->client,
|
---|
| 433 | is->host);
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /*
|
---|
| 438 | * icb_addgroup: adds a new group to the list
|
---|
| 439 | */
|
---|
| 440 | struct icb_group *
|
---|
| 441 | icb_addgroup(struct icb_session *is, char *name, char *mpass)
|
---|
| 442 | {
|
---|
| 443 | struct icb_group *ig;
|
---|
| 444 |
|
---|
| 445 | if ((ig = calloc(1, sizeof *ig)) == NULL)
|
---|
| 446 | return (NULL);
|
---|
| 447 | strlcpy(ig->name, name, sizeof ig->name);
|
---|
| 448 | if (mpass)
|
---|
| 449 | strlcpy(ig->mpass, mpass, sizeof ig->mpass);
|
---|
| 450 | if (is)
|
---|
[4284008] | 451 | ig->mod = is;
|
---|
[cd7b81d] | 452 | LIST_INIT(&ig->sess);
|
---|
| 453 | LIST_INSERT_HEAD(&groups, ig, entry);
|
---|
| 454 | return (ig);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | #ifdef notused
|
---|
| 458 | /*
|
---|
| 459 | * icb_delgroup: removes a group from the list
|
---|
| 460 | */
|
---|
| 461 | void
|
---|
| 462 | icb_delgroup(struct icb_group *ig)
|
---|
| 463 | {
|
---|
| 464 | struct icb_session *s;
|
---|
| 465 |
|
---|
| 466 | /* well, i guess we should kick out participants! ;-) */
|
---|
| 467 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 468 | icb_status(s, STATUS_WARNING, "Group dismissed");
|
---|
| 469 | s->group = NULL;
|
---|
| 470 | }
|
---|
| 471 | LIST_REMOVE(ig, entry);
|
---|
| 472 | bzero(ig, sizeof ig); /* paranoic thing, obviously */
|
---|
| 473 | free(ig);
|
---|
| 474 | }
|
---|
| 475 | #endif
|
---|
| 476 |
|
---|
| 477 | /*
|
---|
[4284008] | 478 | * icb_dowho: a helper function that sends out a group header as a command
|
---|
| 479 | * output and user information in the "wl" format
|
---|
[cd7b81d] | 480 | */
|
---|
[4284008] | 481 | int
|
---|
| 482 | icb_dowho(struct icb_session *is, struct icb_group *ig)
|
---|
[cd7b81d] | 483 | {
|
---|
| 484 | char buf[ICB_MSGSIZE];
|
---|
| 485 | struct icb_session *s;
|
---|
[4284008] | 486 | int nusers = 0;
|
---|
[cd7b81d] | 487 |
|
---|
[4284008] | 488 | icb_cmdout(is, CMDOUT_CO, " ");
|
---|
| 489 | snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
|
---|
| 490 | ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
|
---|
| 491 | strlen(ig->topic) > 0 ? ig->topic : "(None)");
|
---|
| 492 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
[cd7b81d] | 493 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 494 | (void)snprintf(buf, sizeof buf,
|
---|
[bf02a60] | 495 | "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
|
---|
[4284008] | 496 | icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
|
---|
[cd7b81d] | 497 | s->nick, ICB_M_SEP, getmonotime() - s->last,
|
---|
| 498 | ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
|
---|
| 499 | s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
|
---|
| 500 | icb_cmdout(is, CMDOUT_WL, buf);
|
---|
[4284008] | 501 | nusers++;
|
---|
[cd7b81d] | 502 | }
|
---|
[4284008] | 503 | return (nusers);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | /*
|
---|
| 507 | * icb_who: sends a list of users of either the specified group or all
|
---|
| 508 | * groups found on the server
|
---|
| 509 | */
|
---|
| 510 | void
|
---|
| 511 | icb_who(struct icb_session *is, struct icb_group *ig)
|
---|
| 512 | {
|
---|
| 513 | char buf[ICB_MSGSIZE];
|
---|
| 514 | struct icb_group *g;
|
---|
| 515 |
|
---|
| 516 | if (!ig) {
|
---|
| 517 | int nusers = 0, ngroups = 0;
|
---|
| 518 |
|
---|
| 519 | LIST_FOREACH(g, &groups, entry) {
|
---|
| 520 | nusers += icb_dowho(is, g);
|
---|
| 521 | ngroups++;
|
---|
| 522 | }
|
---|
| 523 | if (nusers > 0) {
|
---|
| 524 | (void)snprintf(buf, sizeof buf,
|
---|
| 525 | "Total: %d %s in %d %s",
|
---|
| 526 | nusers, nusers > 1 ? "users" : "user",
|
---|
| 527 | ngroups, ngroups > 1 ? "groups" : "group");
|
---|
| 528 | } else
|
---|
| 529 | (void)snprintf(buf, sizeof buf, "No users found.");
|
---|
| 530 | icb_cmdout(is, CMDOUT_CO, buf);
|
---|
| 531 | } else
|
---|
| 532 | (void)icb_dowho(is, ig);
|
---|
[cd7b81d] | 533 | }
|
---|
| 534 |
|
---|
| 535 | /*
|
---|
[4284008] | 536 | * icb_ismod: checks whether group is moderated by "is"
|
---|
[cd7b81d] | 537 | */
|
---|
[626f420] | 538 | inline int
|
---|
[4284008] | 539 | icb_ismod(struct icb_group *ig, struct icb_session *is)
|
---|
[cd7b81d] | 540 | {
|
---|
[626f420] | 541 | return (ig->mod == is);
|
---|
[cd7b81d] | 542 | }
|
---|
| 543 |
|
---|
[1d2125a] | 544 | /*
|
---|
| 545 | * icb_modpermit: checks user against the moderators table if it has
|
---|
| 546 | * been populated
|
---|
| 547 | */
|
---|
| 548 | int
|
---|
| 549 | icb_modpermit(struct icb_session *is)
|
---|
| 550 | {
|
---|
| 551 | extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
|
---|
| 552 | extern int modtabcnt;
|
---|
| 553 |
|
---|
| 554 | if (modtabcnt == 0 ||
|
---|
| 555 | bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
|
---|
| 556 | (int (*)(const void *, const void *))strcmp))
|
---|
| 557 | return (1);
|
---|
| 558 | return (0);
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[cd7b81d] | 561 | /*
|
---|
| 562 | * icb_pass: passes moderation of group "ig" from "from" to "to",
|
---|
| 563 | * returns -1 if "from" is not a moderator, 1 if passed
|
---|
| 564 | * to "to" and 0 otherwise (no moderator or passed to the
|
---|
| 565 | * internal bot)
|
---|
| 566 | */
|
---|
| 567 | int
|
---|
| 568 | icb_pass(struct icb_group *ig, struct icb_session *from,
|
---|
| 569 | struct icb_session *to)
|
---|
| 570 | {
|
---|
[4284008] | 571 | if (ig->mod && ig->mod != from)
|
---|
[cd7b81d] | 572 | return (-1);
|
---|
| 573 | if (!from && !to)
|
---|
| 574 | return (-1);
|
---|
[1d2125a] | 575 | if (to && !icb_modpermit(to))
|
---|
| 576 | return (-1);
|
---|
[4284008] | 577 | ig->mod = to;
|
---|
[cd7b81d] | 578 | if (to)
|
---|
| 579 | icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
|
---|
| 580 | " of %s", from ? from->nick : "server", ig->name);
|
---|
| 581 | icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
|
---|
| 582 | "to %s", from ? from->nick : "server", to ? to->nick : "server");
|
---|
| 583 | return (1);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | /*
|
---|
[4284008] | 587 | * icb_nextfield: advances through a given buffer returning pointer to the
|
---|
[a202ff1] | 588 | * beginning of the icb field or an empty string otherwise;
|
---|
| 589 | * cleans up trailing spaces
|
---|
[cd7b81d] | 590 | */
|
---|
| 591 | char *
|
---|
| 592 | icb_nextfield(char **buf)
|
---|
| 593 | {
|
---|
| 594 | char *start = *buf;
|
---|
[a202ff1] | 595 | char *end = NULL;
|
---|
[cd7b81d] | 596 |
|
---|
| 597 | while (*buf && **buf != '\0' && **buf != ICB_M_SEP)
|
---|
| 598 | (*buf)++;
|
---|
| 599 | if (*buf && **buf == ICB_M_SEP) {
|
---|
| 600 | **buf = '\0';
|
---|
[a202ff1] | 601 | end = *buf;
|
---|
[cd7b81d] | 602 | (*buf)++;
|
---|
[a202ff1] | 603 | } else
|
---|
| 604 | end = *buf;
|
---|
| 605 | while (end && *(--end) == ' ' && end > start)
|
---|
| 606 | *end = '\0';
|
---|
[cd7b81d] | 607 | return (start);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | /*
|
---|
| 611 | * icb_sendfmt: formats a string and sends it over
|
---|
| 612 | */
|
---|
| 613 | void
|
---|
| 614 | icb_sendfmt(struct icb_session *is, const char *fmt, ...)
|
---|
| 615 | {
|
---|
| 616 | char buf[ICB_MSGSIZE];
|
---|
| 617 | va_list ap;
|
---|
| 618 | int buflen = 1;
|
---|
| 619 |
|
---|
| 620 | va_start(ap, fmt);
|
---|
| 621 | buflen += vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
|
---|
| 622 | va_end(ap);
|
---|
| 623 | buf[0] = buflen;
|
---|
| 624 | icb_send(is, buf, buflen + 1);
|
---|
| 625 | }
|
---|
[626f420] | 626 |
|
---|
| 627 | /*
|
---|
| 628 | * icb_vis: strnvis-like function that escapes percentages as well
|
---|
| 629 | */
|
---|
| 630 | int
|
---|
[b7bc432] | 631 | icb_vis(char *dst, const char *src, size_t dstsize, int flags)
|
---|
[626f420] | 632 | {
|
---|
| 633 | int si = 0, di = 0, td;
|
---|
| 634 |
|
---|
| 635 | while ((size_t)di < dstsize && src[si] != '\0') {
|
---|
| 636 | if (src[si] == '%')
|
---|
| 637 | dst[di++] = '%', dst[di] = '%';
|
---|
[b7bc432] | 638 | else if (src[si] == ' ' && flags & VIS_SP)
|
---|
| 639 | dst[di] = '_';
|
---|
| 640 | else if (isgraph(src[si]) || src[si] == ' ')
|
---|
[626f420] | 641 | dst[di] = src[si];
|
---|
| 642 | else {
|
---|
| 643 | td = snprintf(&dst[di], dstsize - di,
|
---|
| 644 | "\\%03o", (unsigned char)src[si]);
|
---|
| 645 | di += td - 1;
|
---|
| 646 | }
|
---|
| 647 | si++, di++;
|
---|
| 648 | }
|
---|
| 649 | dst[MIN((size_t)di, dstsize)] = '\0';
|
---|
| 650 | return (0);
|
---|
| 651 | }
|
---|