| 1 | /* |
| 2 | * Copyright (C) 2000 Onlyer ([email protected]) |
| 3 | * Copyright (C) 2002 Ross Combs ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #define ALIAS_COMMAND_INTERNAL_ACCESS |
| 20 | #include "common/setup_before.h" |
| 21 | |
| 22 | #include <cerrno> |
| 23 | #include <cstring> |
| 24 | #include <cctype> |
| 25 | |
| 26 | #include "alias_command.h" |
| 27 | #include "compat/strcasecmp.h" |
| 28 | #include "common/field_sizes.h" |
| 29 | #include "common/util.h" |
| 30 | #include "common/eventlog.h" |
| 31 | #include "common/list.h" |
| 32 | #include "common/xalloc.h" |
| 33 | #include "message.h" |
| 34 | #include "connection.h" |
| 35 | #include "i18n.h" |
| 36 | #include "common/setup_after.h" |
| 37 | |
| 38 | |
| 39 | namespace pvpgn |
| 40 | { |
| 41 | |
| 42 | namespace bnetd |
| 43 | { |
| 44 | |
| 45 | static t_list * aliaslist_head = NULL; |
| 46 | |
| 47 | #define MAX_ALIAS_LEN 32 |
| 48 | |
| 49 | |
| 50 | static int list_aliases(t_connection * c); |
| 51 | static char * replace_args(char const * in, unsigned int * offsets, int numargs, char const * text); |
| 52 | static int do_alias(t_connection * c, char const * cmd, char const * args); |
| 53 | |
| 54 | |
| 55 | static int list_aliases(t_connection * c) |
| 56 | { |
| 57 | t_elem const * elem1; |
| 58 | t_elem const * elem2; |
| 59 | t_alias const * alias; |
| 60 | t_output * output; |
| 61 | char temp[MAX_MESSAGE_LEN]; |
| 62 | |
| 63 | message_send_text(c, message_type_info, c, localize(c, "Alias list:")); |
| 64 | LIST_TRAVERSE_CONST(aliaslist_head, elem1) |
| 65 | { |
| 66 | if (!(alias = (t_alias*)elem_get_data(elem1))) |
| 67 | continue; |
| 68 | |
| 69 | std::sprintf(temp, "@%.128s", alias->alias); |
| 70 | message_send_text(c, message_type_info, c, temp); |
| 71 | LIST_TRAVERSE_CONST(alias->output, elem2) |
| 72 | { |
| 73 | if (!(output = (t_output*)elem_get_data(elem2))) |
| 74 | continue; |
| 75 | |
| 76 | /* |
| 77 | * FIXME: need a more user-friendly way to express this... maybe |
| 78 | * add a help line to the file format? |
| 79 | */ |
| 80 | if (output->min == -1) |
| 81 | std::sprintf(temp, "[*]%.128s", output->line); |
| 82 | else if (output->max == -1) |
| 83 | std::sprintf(temp, "[%d+]%.128s", output->min, output->line); |
| 84 | else if (output->min == output->max) |
| 85 | std::sprintf(temp, "[%d]%.128s", output->min, output->line); |
| 86 | else |
| 87 | std::sprintf(temp, "[%d-%d]%.128s", output->min, output->max, output->line); |
| 88 | message_send_text(c, message_type_info, c, temp); |
| 89 | } |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static char * replace_args(char const * in, unsigned int * offsets, int numargs, char const * text) |
| 96 | { |
| 97 | char * out; |
| 98 | unsigned int inpos; |
| 99 | unsigned int outpos; |
| 100 | unsigned int off1; |
| 101 | unsigned int off2; |
| 102 | unsigned int size; |
| 103 | |
| 104 | off1 = off2 = 0; |
| 105 | |
| 106 | out = xstrdup(in); |
| 107 | size = std::strlen(out); |
| 108 | size_t textlen = std::strlen(text); |
| 109 | |
| 110 | for (inpos = outpos = 0; inpos < std::strlen(in); inpos++) |
| 111 | { |
| 112 | if (in[inpos] != '$') |
| 113 | { |
| 114 | out[outpos] = in[inpos]; |
| 115 | outpos++; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | inpos++; |
| 120 | |
| 121 | if (in[inpos] == '*') |
| 122 | { |
| 123 | off1 = offsets[0]; |
| 124 | off2 = textlen; |
| 125 | } |
| 126 | else if (in[inpos] == '{') |
| 127 | { |
| 128 | int arg1, arg2; |
| 129 | char symbol; |
| 130 | |
| 131 | if (std::sscanf(&in[inpos], "{%d-%d}", &arg1, &arg2) != 2) |
| 132 | { |
| 133 | if (std::sscanf(&in[inpos], "{%d%c", &arg2, &symbol) != 2) |
| 134 | { |
| 135 | while (in[inpos] != '\0' && in[inpos] != '}') |
| 136 | inpos++; |
| 137 | continue; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | if (symbol == '}') |
| 142 | { |
| 143 | if (arg2 < 0) |
| 144 | { |
| 145 | arg1 = 0; |
| 146 | arg2 = -arg2; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | arg1 = arg2; |
| 151 | } |
| 152 | } |
| 153 | else if (symbol == '-') |
| 154 | { |
| 155 | arg1 = arg2; |
| 156 | arg2 = numargs - 1; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | while (in[inpos] != '\0' && in[inpos] != '}') inpos++; |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (arg2 >= numargs) |
| 168 | arg2 = numargs - 1; |
| 169 | if (arg1 > arg2) |
| 170 | { |
| 171 | while (in[inpos] != '\0' && in[inpos] != '}') |
| 172 | inpos++; |
| 173 | continue; |
| 174 | } |
| 175 | off1 = offsets[arg1]; |
| 176 | if (arg2 + 1 == numargs) |
| 177 | off2 = textlen; |
| 178 | else |
| 179 | off2 = offsets[arg2 + 1] - 1; |
| 180 | |
| 181 | while (in[inpos] != '\0' && in[inpos] != '}') |
| 182 | inpos++; |
| 183 | } |
| 184 | else if (std::isdigit((int)in[inpos])) |
| 185 | { |
| 186 | int arg; |
| 187 | |
| 188 | if (in[inpos] == '0') arg = 0; |
| 189 | else if (in[inpos] == '1') arg = 1; |
| 190 | else if (in[inpos] == '2') arg = 2; |
| 191 | else if (in[inpos] == '3') arg = 3; |
| 192 | else if (in[inpos] == '4') arg = 4; |
| 193 | else if (in[inpos] == '5') arg = 5; |
| 194 | else if (in[inpos] == '6') arg = 6; |
| 195 | else if (in[inpos] == '7') arg = 7; |
| 196 | else if (in[inpos] == '8') arg = 8; |
| 197 | else arg = 9; |
| 198 | |
| 199 | if (arg >= numargs) |
| 200 | continue; |
| 201 | for (off1 = off2 = offsets[arg]; text[off2] != '\0' && text[off2] != ' ' && text[off2] != '\t'; off2++); |
| 202 | } |
| 203 | |
| 204 | { |
| 205 | char * newout; |
| 206 | |
| 207 | newout = (char*)xmalloc(size + (off2 - off1) + 1); /* curr + new + nul */ |
| 208 | size = size + (off2 - off1) + 1; |
| 209 | std::memmove(newout, out, outpos); |
| 210 | xfree(out); |
| 211 | out = newout; |
| 212 | |
| 213 | while (off1 < off2) |
| 214 | out[outpos++] = text[off1++]; |
| 215 | } |
| 216 | } |
| 217 | out[outpos] = '\0'; |
| 218 | |
| 219 | return out; |
| 220 | } |
| 221 | |
| 222 | int count_args(char const * text) |
| 223 | { |
| 224 | int args = 1; |
| 225 | char * pointer = (char *)text; |
| 226 | int not_ws = 1; |
| 227 | |
| 228 | for (; *pointer != '\0'; pointer++) |
| 229 | { |
| 230 | if ((not_ws) && (*pointer == ' ')) not_ws = 0; |
| 231 | if ((not_ws == 0) && (*pointer != ' ')) { not_ws = 1; args++; } |
| 232 | } |
| 233 | |
| 234 | return args; |
| 235 | } |
| 236 | |
| 237 | void get_offsets(char const * text, unsigned int * offsets) |
| 238 | { |
| 239 | char * pointer = (char *)text; |
| 240 | int counter = 1; |
| 241 | int not_ws = 1; |
| 242 | |
| 243 | offsets[0] = 0; |
| 244 | for (; *pointer != '\0'; pointer++) |
| 245 | { |
| 246 | if ((not_ws) && (*pointer == ' ')) not_ws = 0; |
| 247 | if ((not_ws == 0) && (*pointer != ' ')) |
| 248 | { |
| 249 | not_ws = 1; |
| 250 | offsets[counter] = (unsigned int)(pointer - text); |
| 251 | counter++; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | static int do_alias(t_connection * c, char const * cmd, char const * text) |
| 258 | { |
| 259 | t_elem const * elem1; |
| 260 | t_elem const * elem2; |
| 261 | t_alias const * alias; |
| 262 | t_output * output; |
| 263 | unsigned int * offsets; |
| 264 | int numargs; |
| 265 | int match = -1; |
| 266 | |
| 267 | numargs = count_args(text) - 1; |
| 268 | offsets = (unsigned int*)xmalloc(sizeof(unsigned int)*(numargs + 1)); |
| 269 | get_offsets(text, offsets); |
| 270 | |
| 271 | LIST_TRAVERSE_CONST(aliaslist_head, elem1) |
| 272 | { |
| 273 | if (!(alias = (t_alias*)elem_get_data(elem1))) |
| 274 | continue; |
| 275 | |
| 276 | if (std::strstr(alias->alias, cmd) == NULL) |
| 277 | continue; |
| 278 | |
| 279 | LIST_TRAVERSE_CONST(alias->output, elem2) |
| 280 | { |
| 281 | if (!(output = (t_output*)elem_get_data(elem2))) |
| 282 | continue; |
| 283 | |
| 284 | if (!output->line) |
| 285 | continue; |
| 286 | |
| 287 | if ((output->min == -1) || |
| 288 | ((output->min <= numargs) && (output->max == -1)) || |
| 289 | ((output->min <= numargs) && (output->max >= numargs))) |
| 290 | |
| 291 | //TODO: initialize offsets |
| 292 | |
| 293 | { |
| 294 | char * msgtmp; |
| 295 | char * tmp2; |
| 296 | const char * cmd = "%C"; |
| 297 | match = 1; |
| 298 | |
| 299 | if ((msgtmp = replace_args(output->line, offsets, numargs + 1, text))) |
| 300 | { |
| 301 | if (msgtmp[0] != '\0') |
| 302 | { |
| 303 | if ((msgtmp[0] == '/') && (msgtmp[1] != '/')) // to make sure we don't get endless aliasing loop |
| 304 | { |
| 305 | tmp2 = (char*)xmalloc(std::strlen(msgtmp) + 3); |
| 306 | std::sprintf(tmp2, "%s%s", cmd, msgtmp); |
| 307 | xfree((void *)msgtmp); |
| 308 | msgtmp = tmp2; |
| 309 | |
| 310 | } |
| 311 | if (std::strlen(msgtmp) > MAX_MESSAGE_LEN) |
| 312 | { |
| 313 | msgtmp[MAX_MESSAGE_LEN] = '\0'; |
| 314 | eventlog(eventlog_level_info, __FUNCTION__, "message line after alias expansion was too long, truncating it"); |
| 315 | } |
| 316 | message_send_formatted(c, msgtmp); |
| 317 | } |
| 318 | xfree((void *)msgtmp); /* avoid warning */ |
| 319 | } |
| 320 | else |
| 321 | eventlog(eventlog_level_error, __FUNCTION__, "could not perform argument replacement"); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | xfree(offsets); |
| 326 | return match; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | extern int aliasfile_load(char const * filename) |
| 331 | { |
| 332 | std::FILE * afp; |
| 333 | char * buff; |
| 334 | char * temp; |
| 335 | unsigned int line; |
| 336 | unsigned int pos; |
| 337 | int inalias; |
| 338 | t_alias * alias = NULL; |
| 339 | |
| 340 | |
| 341 | if (!filename) |
| 342 | { |
| 343 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 344 | return -1; |
| 345 | } |
| 346 | if (!(afp = std::fopen(filename, "r"))) |
| 347 | { |
| 348 | eventlog(eventlog_level_error, __FUNCTION__, "unable to open alias file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | aliaslist_head = list_create(); |
| 353 | |
| 354 | inalias = 0; |
| 355 | for (line = 1; (buff = file_get_line(afp)); line++) |
| 356 | { |
| 357 | for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++); |
| 358 | if (buff[pos] == '\0' || buff[pos] == '#') |
| 359 | { |
| 360 | continue; |
| 361 | } |
| 362 | if (!(temp = std::strrchr(buff, '"'))) /* FIXME: assumes comments don't contain " */ |
| 363 | temp = buff; |
| 364 | if ((temp = std::strrchr(temp, '#'))) |
| 365 | { |
| 366 | unsigned int len; |
| 367 | unsigned int endpos; |
| 368 | |
| 369 | *temp = '\0'; |
| 370 | len = std::strlen(buff) + 1; |
| 371 | for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--); |
| 372 | buff[endpos + 1] = '\0'; |
| 373 | } |
| 374 | |
| 375 | switch (inalias) |
| 376 | { |
| 377 | case 0: |
| 378 | if (buff[pos] != '@') /* not start of alias */ |
| 379 | { |
| 380 | eventlog(eventlog_level_error, __FUNCTION__, "expected start of alias stanza on line {} of alias file \"{}\" but found \"{}\"", line, filename, &buff[pos]); |
| 381 | break; |
| 382 | } |
| 383 | inalias = 1; |
| 384 | break; |
| 385 | |
| 386 | case 1: |
| 387 | { |
| 388 | if (buff[pos] == '\0') break; |
| 389 | |
| 390 | inalias = 2; |
| 391 | alias = (t_alias*)xmalloc(sizeof(t_alias)); |
| 392 | alias->output = 0; |
| 393 | alias->alias = xstrdup(&buff[pos]); |
| 394 | } |
| 395 | break; |
| 396 | |
| 397 | case 2: |
| 398 | { |
| 399 | char * dummy; |
| 400 | char * out = NULL; |
| 401 | int min, max; |
| 402 | t_output * output; |
| 403 | |
| 404 | min = max = 0; |
| 405 | if (buff[pos] != '[') |
| 406 | { |
| 407 | eventlog(eventlog_level_error, __FUNCTION__, "expected output entry on line {} of alias file \"{}\" but found \"{}\"", line, filename, &buff[pos]); |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | if ((dummy = std::strchr(&buff[pos], ']'))) |
| 412 | { |
| 413 | if (dummy[1] != '\0') out = xstrdup(&dummy[1]); |
| 414 | } |
| 415 | |
| 416 | if (buff[pos + 1] == '*') |
| 417 | { |
| 418 | min = max = -1; |
| 419 | } |
| 420 | else if (buff[pos + 1] == '+') |
| 421 | { |
| 422 | min = 1; |
| 423 | max = -1; |
| 424 | } |
| 425 | else if (std::sscanf(&buff[pos], "[%i", &min) == 1) |
| 426 | { |
| 427 | if (*(dummy - 1) == '+') |
| 428 | max = -1; |
| 429 | else max = min; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | eventlog(eventlog_level_error, __FUNCTION__, "error parsing min/max value for alias output"); |
| 434 | } |
| 435 | |
| 436 | if (out != NULL) |
| 437 | { |
| 438 | output = (t_output*)xmalloc(sizeof(t_output)); |
| 439 | output->min = min; |
| 440 | output->max = max; |
| 441 | output->line = out; |
| 442 | alias->output = list_create(); |
| 443 | list_append_data(alias->output, output); |
| 444 | inalias = 3; |
| 445 | } |
| 446 | } |
| 447 | break; |
| 448 | case 3: |
| 449 | { |
| 450 | if (buff[pos] == '@') |
| 451 | { |
| 452 | inalias = 1; |
| 453 | if (alias != NULL) list_append_data(aliaslist_head, alias); |
| 454 | alias = NULL; |
| 455 | break; |
| 456 | } |
| 457 | else if (buff[pos] == '[') |
| 458 | { |
| 459 | char * dummy; |
| 460 | char * out = NULL; |
| 461 | int min, max; |
| 462 | t_output * output; |
| 463 | |
| 464 | min = max = 0; |
| 465 | if ((dummy = std::strchr(&buff[pos], ']'))) |
| 466 | { |
| 467 | if (dummy[1] != '\0') out = xstrdup(&dummy[1]); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | if (buff[pos + 1] == '*') |
| 472 | { |
| 473 | min = max = -1; |
| 474 | } |
| 475 | else if (buff[pos + 1] == '+') |
| 476 | { |
| 477 | min = 1; |
| 478 | max = -1; |
| 479 | } |
| 480 | else if (std::sscanf(&buff[pos], "[%i", &min) == 1) |
| 481 | { |
| 482 | if (*(dummy - 1) == '+') |
| 483 | max = -1; |
| 484 | else max = min; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | eventlog(eventlog_level_error, __FUNCTION__, "error parsing min/max value for alias output"); |
| 489 | } |
| 490 | |
| 491 | if (out != NULL) |
| 492 | { |
| 493 | output = (t_output*)xmalloc(sizeof(t_output)); |
| 494 | output->min = min; |
| 495 | output->max = max; |
| 496 | output->line = out; |
| 497 | |
| 498 | list_append_data(alias->output, output); |
| 499 | } |
| 500 | } |
| 501 | else |
| 502 | eventlog(eventlog_level_error, __FUNCTION__, "expected output entry or next alias stanza on line {} of file \"{}\"i but found \"{}\"", line, filename, &buff[pos]); |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | if (alias != NULL) list_append_data(aliaslist_head, alias); |
| 508 | |
| 509 | file_get_line(NULL); // clear file_get_line buffer |
| 510 | std::fclose(afp); |
| 511 | eventlog(eventlog_level_info, __FUNCTION__, "done loading aliases"); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | extern int aliasfile_unload(void) |
| 517 | { |
| 518 | t_elem * elem1; |
| 519 | t_elem * elem2; |
| 520 | t_alias const * alias; |
| 521 | t_output * output; |
| 522 | |
| 523 | if (aliaslist_head) |
| 524 | { |
| 525 | LIST_TRAVERSE(aliaslist_head, elem1) |
| 526 | { |
| 527 | if (!(alias = (t_alias*)elem_get_data(elem1))) /* should not happen */ |
| 528 | { |
| 529 | eventlog(eventlog_level_error, __FUNCTION__, "alias list contains NULL item"); |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | if (list_remove_elem(aliaslist_head, &elem1) < 0) |
| 534 | { |
| 535 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove alias"); |
| 536 | continue; |
| 537 | } |
| 538 | if (alias->output) |
| 539 | { |
| 540 | LIST_TRAVERSE(alias->output, elem2) |
| 541 | { |
| 542 | if (!(output = (t_output*)elem_get_data(elem2))) |
| 543 | { |
| 544 | eventlog(eventlog_level_error, __FUNCTION__, "output list contains NULL item"); |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | if (list_remove_elem(alias->output, &elem2) < 0) |
| 549 | { |
| 550 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove output"); |
| 551 | continue; |
| 552 | } |
| 553 | xfree((void *)output->line); /* avoid warning */ |
| 554 | xfree((void *)output); |
| 555 | } |
| 556 | list_destroy(alias->output); |
| 557 | } |
| 558 | if (alias->alias) xfree((void *)alias->alias); |
| 559 | xfree((void *)alias); |
| 560 | } |
| 561 | |
| 562 | if (list_destroy(aliaslist_head) < 0) |
| 563 | return -1; |
| 564 | aliaslist_head = NULL; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | |
| 571 | extern int handle_alias_command(t_connection * c, char const * text) |
| 572 | { |
| 573 | unsigned int i, j; |
| 574 | char cmd[MAX_COMMAND_LEN]; |
| 575 | |
| 576 | for (i = j = 0; text[i] != ' ' && text[i] != '\0'; i++) /* get command */ |
| 577 | if (j < sizeof(cmd)-1) cmd[j++] = text[i]; |
| 578 | cmd[j] = '\0'; |
| 579 | |
| 580 | if (cmd[2] == '\0') |
| 581 | return list_aliases(c); |
| 582 | |
| 583 | if (do_alias(c, cmd, text) < 0) |
| 584 | { |
| 585 | message_send_text(c, message_type_info, c, localize(c, "No such alias. Use // to show the list.")); |
| 586 | return -1; |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | } |
| 592 | |
| 593 | } |