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