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