[cd7b81d] | 1 | /*
|
---|
[4284008] | 2 | * Copyright (c) 2009, 2010, 2013 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 <stdio.h>
|
---|
| 21 | #include <stdlib.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <syslog.h>
|
---|
| 24 | #include <unistd.h>
|
---|
[626f420] | 25 | #include <vis.h>
|
---|
[cd7b81d] | 26 | #include <event.h>
|
---|
| 27 |
|
---|
| 28 | #include "icb.h"
|
---|
[7882a6f] | 29 | #include "icbd.h"
|
---|
[cd7b81d] | 30 |
|
---|
| 31 | extern int creategroups;
|
---|
| 32 |
|
---|
[9195a6a] | 33 | void icb_cmd_help(struct icb_session *, char *);
|
---|
[bf02a60] | 34 | void icb_cmd_beep(struct icb_session *, char *);
|
---|
[cd7b81d] | 35 | void icb_cmd_boot(struct icb_session *, char *);
|
---|
| 36 | void icb_cmd_change(struct icb_session *, char *);
|
---|
| 37 | void icb_cmd_name(struct icb_session *, char *);
|
---|
[bf02a60] | 38 | void icb_cmd_nobeep(struct icb_session *, char *);
|
---|
[cd7b81d] | 39 | void icb_cmd_personal(struct icb_session *, char *);
|
---|
| 40 | void icb_cmd_pass(struct icb_session *, char *);
|
---|
| 41 | void icb_cmd_topic(struct icb_session *, char *);
|
---|
| 42 | void icb_cmd_who(struct icb_session *, char *);
|
---|
| 43 |
|
---|
| 44 | void *
|
---|
| 45 | icb_cmd_lookup(char *cmd)
|
---|
| 46 | {
|
---|
| 47 | struct {
|
---|
| 48 | const char *cmd;
|
---|
| 49 | void (*handler)(struct icb_session *, char *);
|
---|
| 50 | } cmdtab[] = {
|
---|
[9195a6a] | 51 | { "?", icb_cmd_help },
|
---|
[bf02a60] | 52 | { "beep", icb_cmd_beep },
|
---|
[cd7b81d] | 53 | { "boot", icb_cmd_boot },
|
---|
| 54 | { "g", icb_cmd_change },
|
---|
| 55 | { "m", icb_cmd_personal },
|
---|
| 56 | { "msg", icb_cmd_personal },
|
---|
| 57 | { "name", icb_cmd_name },
|
---|
[bf02a60] | 58 | { "nobeep", icb_cmd_nobeep },
|
---|
[cd7b81d] | 59 | { "pass", icb_cmd_pass },
|
---|
| 60 | { "topic", icb_cmd_topic },
|
---|
| 61 | { "w", icb_cmd_who },
|
---|
| 62 | { NULL, NULL }
|
---|
| 63 | };
|
---|
| 64 | int i;
|
---|
| 65 |
|
---|
| 66 | for (i = 0; cmdtab[i].cmd != NULL; i++)
|
---|
| 67 | if (strcasecmp(cmdtab[i].cmd, cmd) == 0)
|
---|
| 68 | return (cmdtab[i].handler);
|
---|
| 69 | return (NULL);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[9195a6a] | 72 | void
|
---|
| 73 | icb_cmd_help(struct icb_session *is, char *arg __attribute__((unused)))
|
---|
| 74 | {
|
---|
| 75 | icb_status(is, STATUS_HELP, "Server supports following commands:");
|
---|
| 76 | icb_status(is, STATUS_HELP, "beep boot g m name nobeep pass topic");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[bf02a60] | 79 | void
|
---|
| 80 | icb_cmd_beep(struct icb_session *is, char *arg)
|
---|
| 81 | {
|
---|
| 82 | struct icb_group *ig = is->group;
|
---|
| 83 | struct icb_session *s;
|
---|
[626f420] | 84 | char whom[ICB_MAXNICKLEN];
|
---|
[bf02a60] | 85 |
|
---|
| 86 | if (strlen(arg) == 0) {
|
---|
| 87 | icb_error(is, "Invalid user");
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[b7bc432] | 91 | icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
|
---|
[626f420] | 92 |
|
---|
[bf02a60] | 93 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
[626f420] | 94 | if (strcmp(s->nick, whom) == 0)
|
---|
[bf02a60] | 95 | break;
|
---|
| 96 | }
|
---|
| 97 | if (s == NULL) {
|
---|
[626f420] | 98 | icb_status(is, STATUS_NOTIFY, "%s is not signed on", whom);
|
---|
[bf02a60] | 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (ISSETF(s->flags, ICB_SF_NOBEEP | ICB_SF_NOBEEP2)) {
|
---|
| 103 | icb_error(is, "User has nobeep enabled");
|
---|
| 104 | if (ISSETF(s->flags, ICB_SF_NOBEEP2))
|
---|
| 105 | icb_status(s, STATUS_NOBEEP,
|
---|
| 106 | "%s attempted to beep you", is->nick);
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | icb_sendfmt(s, "%c%s", ICB_M_BEEP, is->nick);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[cd7b81d] | 113 | void
|
---|
| 114 | icb_cmd_boot(struct icb_session *is, char *arg)
|
---|
| 115 | {
|
---|
| 116 | struct icb_group *ig;
|
---|
| 117 | struct icb_session *s;
|
---|
[626f420] | 118 | char whom[ICB_MAXNICKLEN];
|
---|
[cd7b81d] | 119 |
|
---|
| 120 | /* to boot or not to boot, that is the question */
|
---|
| 121 | ig = is->group;
|
---|
[4284008] | 122 | if (!icb_ismod(ig, is)) {
|
---|
[cd7b81d] | 123 | icb_status(is, STATUS_NOTIFY, "Sorry, booting is a privilege "
|
---|
| 124 | "you don't possess");
|
---|
| 125 | return;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[fdbbc45] | 128 | if (strlen(arg) == 0) {
|
---|
[626f420] | 129 | icb_error(is, "Invalid user");
|
---|
| 130 | return;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[b7bc432] | 133 | icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
|
---|
[626f420] | 134 |
|
---|
[cd7b81d] | 135 | /* who would be a target then? */
|
---|
| 136 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
[626f420] | 137 | if (strcmp(s->nick, whom) == 0)
|
---|
[cd7b81d] | 138 | break;
|
---|
| 139 | }
|
---|
| 140 | if (s == NULL) {
|
---|
| 141 | icb_status(is, STATUS_NOTIFY, "No such user");
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
[383d37b] | 144 | if (s == is) {
|
---|
| 145 | icb_error(is, "Just quit, would you?");
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
[cd7b81d] | 148 |
|
---|
| 149 | /* okay, here we go, but first, be polite and notify a user */
|
---|
| 150 | icb_status(s, STATUS_BOOT, "%s booted you", is->nick);
|
---|
| 151 | icb_status_group(s->group, s, STATUS_BOOT, "%s was booted", s->nick);
|
---|
[7882a6f] | 152 | icbd_drop(s, "booted");
|
---|
[cd7b81d] | 153 | }
|
---|
| 154 |
|
---|
| 155 | void
|
---|
| 156 | icb_cmd_change(struct icb_session *is, char *arg)
|
---|
| 157 | {
|
---|
| 158 | struct icb_group *ig;
|
---|
| 159 | struct icb_session *s;
|
---|
[626f420] | 160 | char group[ICB_MAXGRPLEN];
|
---|
[cd7b81d] | 161 | int changing = 0;
|
---|
| 162 |
|
---|
| 163 | if (strlen(arg) == 0) {
|
---|
[7ff6405] | 164 | icb_error(is, "Invalid group name");
|
---|
[cd7b81d] | 165 | return;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[b7bc432] | 168 | icb_vis(group, arg, ICB_MAXGRPLEN, VIS_SP);
|
---|
[626f420] | 169 |
|
---|
[cd7b81d] | 170 | LIST_FOREACH(ig, &groups, entry) {
|
---|
[626f420] | 171 | if (strcmp(ig->name, group) == 0)
|
---|
[cd7b81d] | 172 | break;
|
---|
| 173 | }
|
---|
| 174 | if (ig == NULL) {
|
---|
| 175 | if (!creategroups) {
|
---|
[7ff6405] | 176 | icb_error(is, "Can't create new groups");
|
---|
[cd7b81d] | 177 | return;
|
---|
| 178 | } else {
|
---|
[677a45b] | 179 | if ((ig = icb_addgroup(is, group)) == NULL) {
|
---|
[7ff6405] | 180 | icb_error(is, "Can't create group %s", group);
|
---|
[cd7b81d] | 181 | return;
|
---|
| 182 | }
|
---|
[7882a6f] | 183 | icbd_log(NULL, LOG_DEBUG, "%s created group %s",
|
---|
[626f420] | 184 | is->nick, group);
|
---|
[cd7b81d] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[4d92f03] | 188 | /* see if we're changing to the same group */
|
---|
[cd7b81d] | 189 | if (is->group && is->group == ig) {
|
---|
[4d92f03] | 190 | icb_error(is, "You are already in that group");
|
---|
[cd7b81d] | 191 | return;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
| 195 | if (strcmp(s->nick, is->nick) == 0) {
|
---|
| 196 | icb_error(is, "Nick is already in use");
|
---|
| 197 | return;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | if (is->group) {
|
---|
| 202 | changing = 1;
|
---|
[4284008] | 203 | if (icb_ismod(is->group, is))
|
---|
[cd7b81d] | 204 | (void)icb_pass(is->group, is, NULL);
|
---|
| 205 | LIST_REMOVE(is, entry);
|
---|
| 206 | icb_status_group(is->group, NULL, STATUS_DEPART,
|
---|
| 207 | "%s (%s@%s) just left", is->nick, is->client, is->host);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | is->group = ig;
|
---|
| 211 | LIST_INSERT_HEAD(&ig->sess, is, entry);
|
---|
| 212 |
|
---|
| 213 | /* notify group */
|
---|
| 214 | icb_status_group(ig, is, changing ? STATUS_ARRIVE : STATUS_SIGNON,
|
---|
| 215 | "%s (%s@%s) entered group", is->nick, is->client, is->host);
|
---|
| 216 |
|
---|
| 217 | /* acknowledge successful join */
|
---|
| 218 | icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
|
---|
[4284008] | 219 | icb_ismod(ig, is) ? " as moderator" : "");
|
---|
[cd7b81d] | 220 |
|
---|
| 221 | /* send user a topic name */
|
---|
| 222 | if (strlen(ig->topic) > 0)
|
---|
| 223 | icb_status(is, STATUS_TOPIC, "The topic is: %s", ig->topic);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void
|
---|
| 227 | icb_cmd_name(struct icb_session *is, char *arg)
|
---|
| 228 | {
|
---|
| 229 | struct icb_group *ig = is->group;
|
---|
| 230 | struct icb_session *s;
|
---|
[626f420] | 231 | char nick[ICB_MAXNICKLEN];
|
---|
[cd7b81d] | 232 |
|
---|
| 233 | if (strlen(arg) == 0) {
|
---|
| 234 | icb_status(is, STATUS_NAME, "Your nickname is %s",
|
---|
| 235 | is->nick);
|
---|
| 236 | return;
|
---|
| 237 | }
|
---|
| 238 | if (strcasecmp(arg, "admin") == 0) {
|
---|
| 239 | icb_error(is, "Wuff wuff!");
|
---|
| 240 | return;
|
---|
| 241 | }
|
---|
| 242 | /* sanitize user input */
|
---|
| 243 | if (strlen(arg) > ICB_MAXNICKLEN)
|
---|
| 244 | arg[ICB_MAXNICKLEN - 1] = '\0';
|
---|
[b7bc432] | 245 | icb_vis(nick, arg, ICB_MAXNICKLEN, VIS_SP);
|
---|
[cd7b81d] | 246 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
[626f420] | 247 | if (strcmp(s->nick, nick) == 0) {
|
---|
[cd7b81d] | 248 | icb_error(is, "Nick is already in use");
|
---|
| 249 | return;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | icb_status_group(ig, NULL, STATUS_NAME,
|
---|
[626f420] | 253 | "%s changed nickname to %s", is->nick, nick);
|
---|
| 254 | strlcpy(is->nick, nick, sizeof is->nick);
|
---|
[cd7b81d] | 255 | }
|
---|
| 256 |
|
---|
[bf02a60] | 257 | void
|
---|
| 258 | icb_cmd_nobeep(struct icb_session *is, char *arg)
|
---|
| 259 | {
|
---|
| 260 | if (strlen(arg) == 0) {
|
---|
| 261 | /* fail if we have verbose turned on */
|
---|
| 262 | if (ISSETF(is->flags, ICB_SF_NOBEEP2)) {
|
---|
| 263 | icb_error(is, "Can't toggle your nobeep status");
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 | /* otherwise toggle the status */
|
---|
| 267 | if (ISSETF(is->flags, ICB_SF_NOBEEP))
|
---|
| 268 | CLRF(is->flags, ICB_SF_NOBEEP);
|
---|
| 269 | else
|
---|
| 270 | SETF(is->flags, ICB_SF_NOBEEP);
|
---|
| 271 | icb_status(is, STATUS_NOBEEP, "No-Beep %s",
|
---|
| 272 | ISSETF(is->flags, ICB_SF_NOBEEP) ? "on" : "off");
|
---|
| 273 | return;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | if (strcmp(arg, "on") == 0) {
|
---|
| 277 | SETF(is->flags, ICB_SF_NOBEEP);
|
---|
| 278 | CLRF(is->flags, ICB_SF_NOBEEP2); /* can't be on and verbose */
|
---|
| 279 | icb_status(is, STATUS_NOBEEP, "No-Beep on");
|
---|
| 280 | } else if (strcmp(arg, "verbose") == 0) {
|
---|
| 281 | SETF(is->flags, ICB_SF_NOBEEP2);
|
---|
| 282 | CLRF(is->flags, ICB_SF_NOBEEP); /* can't be on and verbose */
|
---|
| 283 | icb_status(is, STATUS_NOBEEP, "No-Beep on (verbose)");
|
---|
| 284 | } else if (strcmp(arg, "off") == 0) {
|
---|
| 285 | CLRF(is->flags, ICB_SF_NOBEEP | ICB_SF_NOBEEP2);
|
---|
| 286 | icb_status(is, STATUS_NOBEEP, "No-Beep off");
|
---|
| 287 | } else
|
---|
| 288 | icb_error(is, "Invalid nobeep mode");
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[cd7b81d] | 291 | void
|
---|
| 292 | icb_cmd_personal(struct icb_session *is, char *arg)
|
---|
| 293 | {
|
---|
| 294 | char *p;
|
---|
| 295 |
|
---|
| 296 | if ((p = strchr(arg, ' ')) == 0) {
|
---|
| 297 | icb_error(is, "Empty message");
|
---|
| 298 | return;
|
---|
| 299 | }
|
---|
| 300 | *p = '\0';
|
---|
| 301 | icb_privmsg(is, arg, ++p);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | void
|
---|
[7eb46d4] | 305 | icb_cmd_pass(struct icb_session *is, char *arg)
|
---|
[cd7b81d] | 306 | {
|
---|
[7eb46d4] | 307 | struct icb_group *ig = is->group;
|
---|
| 308 | struct icb_session *s;
|
---|
[626f420] | 309 | char whom[ICB_MAXNICKLEN];
|
---|
[7eb46d4] | 310 |
|
---|
[f3c60e6] | 311 | if (!ig->mod) { /* if there is no mod, try grabbing it */
|
---|
| 312 | if (icb_modpermit(is, 0) && icb_pass(ig, ig->mod, is) < 0)
|
---|
[e2fcbc7] | 313 | icb_error(is, "Acquiring group moderation failed.");
|
---|
| 314 | } else if (icb_ismod(ig, is)) {
|
---|
[a203cdb] | 315 | if (strlen(arg) == 0) {
|
---|
| 316 | /* no argument: relinquish moderator */
|
---|
| 317 | (void)icb_pass(ig, ig->mod, NULL);
|
---|
| 318 | return;
|
---|
| 319 | }
|
---|
[b7bc432] | 320 | icb_vis(whom, arg, ICB_MAXNICKLEN, VIS_SP);
|
---|
[7eb46d4] | 321 | LIST_FOREACH(s, &ig->sess, entry) {
|
---|
[626f420] | 322 | if (strcmp(s->nick, whom) == 0)
|
---|
[7eb46d4] | 323 | break;
|
---|
| 324 | }
|
---|
| 325 | if (s == NULL) {
|
---|
| 326 | icb_status(is, STATUS_NOTIFY, "No such user");
|
---|
| 327 | return;
|
---|
| 328 | }
|
---|
[f3c60e6] | 329 | if (icb_modpermit(s, 0) && icb_pass(ig, ig->mod, s) < 0)
|
---|
[e2fcbc7] | 330 | icb_error(s, "Acquiring group moderation failed.");
|
---|
[f3c60e6] | 331 | } else {
|
---|
| 332 | /*
|
---|
| 333 | * if group is moderated and we're not the moderator,
|
---|
| 334 | * but modtab is enabled, then check the permission
|
---|
| 335 | * and pass moderation if successful.
|
---|
| 336 | */
|
---|
| 337 | if (icb_modpermit(is, 1) && icb_pass(ig, ig->mod, is) < 0)
|
---|
| 338 | icb_error(is, "Acquiring group moderation failed.");
|
---|
[7eb46d4] | 339 | }
|
---|
[cd7b81d] | 340 | }
|
---|
| 341 |
|
---|
| 342 | void
|
---|
| 343 | icb_cmd_topic(struct icb_session *is, char *arg)
|
---|
| 344 | {
|
---|
| 345 | struct icb_group *ig = is->group;
|
---|
[626f420] | 346 | char topic[ICB_MAXTOPICLEN];
|
---|
[cd7b81d] | 347 |
|
---|
| 348 | if (strlen(arg) == 0) { /* querying the topic */
|
---|
| 349 | if (strlen(ig->topic) > 0)
|
---|
| 350 | icb_status(is, STATUS_TOPIC, "The topic is: %s",
|
---|
| 351 | ig->topic);
|
---|
| 352 | else
|
---|
| 353 | icb_status(is, STATUS_TOPIC, "The topic is not set.");
|
---|
| 354 | } else { /* setting the topic */
|
---|
[4284008] | 355 | if (!icb_ismod(ig, is)) {
|
---|
[8886035] | 356 | icb_status(is, STATUS_NOTIFY, "Setting the topic is "
|
---|
| 357 | "only for moderators.");
|
---|
| 358 | return;
|
---|
| 359 | }
|
---|
[b7bc432] | 360 | icb_vis(topic, arg, ICB_MAXTOPICLEN, 0);
|
---|
[626f420] | 361 | strlcpy(ig->topic, topic, sizeof ig->topic);
|
---|
[cd7b81d] | 362 | icb_status_group(ig, NULL, STATUS_TOPIC,
|
---|
| 363 | "%s changed the topic to \"%s\"", is->nick, ig->topic);
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | void
|
---|
[4284008] | 368 | icb_cmd_who(struct icb_session *is, char *arg)
|
---|
[cd7b81d] | 369 | {
|
---|
[4284008] | 370 | struct icb_group *ig;
|
---|
[626f420] | 371 | char group[ICB_MAXGRPLEN];
|
---|
[4284008] | 372 |
|
---|
[b28dd0e] | 373 | while (strlen(arg) && arg[0] == '-') { /* ignore options, for now */
|
---|
| 374 | /* ircII "set SHOW_CHANNEL_NAMES ON" uses /w -s */
|
---|
| 375 | while(arg[0] != ' ' && arg[0] != 0)
|
---|
| 376 | arg++;
|
---|
| 377 | if(arg[0] == ' ')
|
---|
| 378 | arg++;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[4284008] | 381 | if (strlen(arg) == 0)
|
---|
| 382 | return icb_who(is, NULL);
|
---|
| 383 |
|
---|
[c102bbf] | 384 | /* pidgin-icb treats '.' as the current group */
|
---|
| 385 | if (strlen(arg) == 1 && arg[0] == '.') {
|
---|
| 386 | icb_who(is, is->group);
|
---|
| 387 | return;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[b7bc432] | 390 | icb_vis(group, arg, ICB_MAXGRPLEN, VIS_SP);
|
---|
[4284008] | 391 | LIST_FOREACH(ig, &groups, entry) {
|
---|
[626f420] | 392 | if (strcmp(ig->name, group) == 0)
|
---|
[4284008] | 393 | break;
|
---|
| 394 | }
|
---|
| 395 | if (ig == NULL) {
|
---|
[626f420] | 396 | icb_error(is, "The group %s doesn't exist.", group);
|
---|
[4284008] | 397 | return;
|
---|
| 398 | }
|
---|
| 399 | icb_who(is, ig);
|
---|
[cd7b81d] | 400 | }
|
---|