| 1 | /* |
| 2 | * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected]) |
| 3 | * Copyright (C) 2000,2001 Marco Ziech ([email protected]) |
| 4 | * Copyright (C) 2002,2003,2004 Dizzy |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #define ACCOUNT_INTERNAL_ACCESS |
| 21 | #include "common/setup_before.h" |
| 22 | #include "account.h" |
| 23 | |
| 24 | #include <cstddef> |
| 25 | #include <cassert> |
| 26 | #include <cctype> |
| 27 | #include <chrono> |
| 28 | |
| 29 | #include "compat/strdup.h" |
| 30 | #include "compat/strcasecmp.h" |
| 31 | #include "compat/strncasecmp.h" |
| 32 | #include "compat/pdir.h" |
| 33 | #include "common/list.h" |
| 34 | #include "common/elist.h" |
| 35 | #include "common/eventlog.h" |
| 36 | #include "common/util.h" |
| 37 | #include "common/field_sizes.h" |
| 38 | #include "common/bnethash.h" |
| 39 | #include "common/introtate.h" |
| 40 | |
| 41 | #include "prefs.h" |
| 42 | #include "account_wrap.h" |
| 43 | #include "common/hashtable.h" |
| 44 | #include "connection.h" |
| 45 | #include "watch.h" |
| 46 | #include "friends.h" |
| 47 | #include "team.h" |
| 48 | #include "common/tag.h" |
| 49 | #include "ladder.h" |
| 50 | #include "clan.h" |
| 51 | #include "server.h" |
| 52 | #include "attrgroup.h" |
| 53 | #include "attrlayer.h" |
| 54 | #include "storage.h" |
| 55 | #include "common/flags.h" |
| 56 | #include "common/xalloc.h" |
| 57 | #include "common/xstring.h" |
| 58 | #include "common/setup_after.h" |
| 59 | |
| 60 | namespace pvpgn |
| 61 | { |
| 62 | |
| 63 | namespace bnetd |
| 64 | { |
| 65 | |
| 66 | static t_hashtable * accountlist_head = NULL; |
| 67 | static t_hashtable * accountlist_uid_head = NULL; |
| 68 | |
| 69 | unsigned int maxuserid = 0; |
| 70 | |
| 71 | /* This is to force the creation of all accounts when we initially load the accountlist. */ |
| 72 | static int force_account_add = 0; |
| 73 | |
| 74 | static unsigned int account_hash(char const * username); |
| 75 | static t_account * account_load(t_attrgroup *); |
| 76 | static int account_load_friends(t_account * account); |
| 77 | static int account_unload_friends(t_account * account); |
| 78 | static void account_destroy(t_account * account); |
| 79 | static t_account * accountlist_add_account(t_account * account); |
| 80 | |
| 81 | static unsigned int account_hash(char const *username) |
| 82 | { |
| 83 | register unsigned int h; |
| 84 | register unsigned int len = std::strlen(username); |
| 85 | |
| 86 | int c; |
| 87 | for (h = 5381; len > 0; --len, ++username) { |
| 88 | h += h << 5; |
| 89 | |
| 90 | c = (int)*username; |
| 91 | // FIXME: (HarpyWar) I add this condition because if we call connlist_find_connection_by_accountname |
| 92 | // with wrong account name, then it fails on std::isupper(c) |
| 93 | if (c < -1 || c > 255) |
| 94 | break; |
| 95 | |
| 96 | if (std::isupper(c) == 0) |
| 97 | h ^= *username; |
| 98 | else |
| 99 | h ^= std::tolower(c); |
| 100 | } |
| 101 | return h; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static t_account * account_create(char const * username, char const * passhash1) |
| 106 | { |
| 107 | t_account * account; |
| 108 | |
| 109 | if (username && !passhash1) { |
| 110 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL passhash1"); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | if (username && account_check_name(username)) { |
| 115 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid chars in username"); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | account = (t_account*)xmalloc(sizeof(t_account)); |
| 120 | |
| 121 | account->name = NULL; |
| 122 | account->clanmember = NULL; |
| 123 | account->attrgroup = NULL; |
| 124 | account->friends = NULL; |
| 125 | account->teams = NULL; |
| 126 | account->conn = NULL; |
| 127 | FLAG_ZERO(&account->flags); |
| 128 | |
| 129 | account->namehash = 0; /* hash it later before inserting */ |
| 130 | account->uid = 0; /* hash it later before inserting */ |
| 131 | |
| 132 | if (username) |
| 133 | { |
| 134 | /* actually making a new account */ |
| 135 | /* first check if such a username already owns an account. |
| 136 | * we search in the memory hash mainly for non-indexed storage types. |
| 137 | * indexed storage types check themselves if the username exists already |
| 138 | * in the storage (see storage_sql.c) */ |
| 139 | if (accountlist_find_account(username)) { |
| 140 | eventlog(eventlog_level_debug, __FUNCTION__, "user \"{}\" already has an account", username); |
| 141 | goto err; |
| 142 | } |
| 143 | |
| 144 | account->attrgroup = attrgroup_create_newuser(username); |
| 145 | if (!account->attrgroup) { |
| 146 | eventlog(eventlog_level_error, __FUNCTION__, "failed to add user"); |
| 147 | goto err; |
| 148 | } |
| 149 | |
| 150 | account->name = xstrdup(username); |
| 151 | |
| 152 | if (account_set_strattr(account, "BNET\\acct\\username", username) < 0) { |
| 153 | eventlog(eventlog_level_error, __FUNCTION__, "could not set username"); |
| 154 | goto err; |
| 155 | } |
| 156 | |
| 157 | if (account_set_numattr(account, "BNET\\acct\\userid", maxuserid + 1) < 0) { |
| 158 | eventlog(eventlog_level_error, __FUNCTION__, "could not set userid"); |
| 159 | goto err; |
| 160 | } |
| 161 | |
| 162 | if (account_set_strattr(account, "BNET\\acct\\passhash1", passhash1) < 0) { |
| 163 | eventlog(eventlog_level_error, __FUNCTION__, "could not set passhash1"); |
| 164 | goto err; |
| 165 | } |
| 166 | |
| 167 | if (account_set_numattr(account, "BNET\\acct\\ctime", (unsigned int)now)) { |
| 168 | eventlog(eventlog_level_error, __FUNCTION__, "could not set ctime"); |
| 169 | goto err; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return account; |
| 174 | |
| 175 | err: |
| 176 | account_destroy(account); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | static void account_destroy(t_account * account) |
| 181 | { |
| 182 | assert(account); |
| 183 | |
| 184 | friendlist_close(account->friends); |
| 185 | teams_destroy(account->teams); |
| 186 | if (account->attrgroup) |
| 187 | attrgroup_destroy(account->attrgroup); |
| 188 | if (account->name) |
| 189 | xfree(account->name); |
| 190 | |
| 191 | xfree(account); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | extern unsigned int account_get_uid_real(t_account const * account, char const * fn, unsigned int ln) |
| 196 | { |
| 197 | if (!account) { |
| 198 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account (from {}:{})", fn, ln); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | return account->uid; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | extern int account_match(t_account * account, char const * username) |
| 207 | { |
| 208 | unsigned int userid = 0; |
| 209 | unsigned int namehash; |
| 210 | char const * tname; |
| 211 | |
| 212 | if (!account) |
| 213 | { |
| 214 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 215 | return -1; |
| 216 | } |
| 217 | if (!username) |
| 218 | { |
| 219 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL username"); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | if (username[0] == '#') |
| 224 | if (str_to_uint(&username[1], &userid) < 0) |
| 225 | userid = 0; |
| 226 | |
| 227 | if (userid) |
| 228 | { |
| 229 | if (account->uid == userid) |
| 230 | return 1; |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | namehash = account_hash(username); |
| 235 | if (account->namehash == namehash && |
| 236 | (tname = account_get_name(account))) |
| 237 | { |
| 238 | if (strcasecmp(tname, username) == 0) |
| 239 | return 1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | extern int account_save(t_account * account, unsigned flags) |
| 248 | { |
| 249 | assert(account); |
| 250 | |
| 251 | return attrgroup_save(account->attrgroup, flags); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | extern int account_flush(t_account * account, unsigned flags) |
| 256 | { |
| 257 | int res; |
| 258 | |
| 259 | assert(account); |
| 260 | |
| 261 | res = attrgroup_flush(account->attrgroup, flags); |
| 262 | if (res < 0) return res; |
| 263 | |
| 264 | account_unload_friends(account); |
| 265 | |
| 266 | return res; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | extern char const * account_get_strattr_real(t_account * account, char const * key, char const * fn, unsigned int ln) |
| 271 | { |
| 272 | if (!account) { |
| 273 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account (from {}:{})", fn, ln); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | if (!key) { |
| 278 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL key (from {}:{})", fn, ln); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | return attrgroup_get_attr(account->attrgroup, key); |
| 283 | } |
| 284 | |
| 285 | extern int account_set_strattr(t_account * account, char const * key, char const * val) |
| 286 | { |
| 287 | if (!account) { |
| 288 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | if (!key) { |
| 293 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL key"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | return attrgroup_set_attr(account->attrgroup, key, val); |
| 298 | } |
| 299 | |
| 300 | static t_account * account_load(t_attrgroup *attrgroup) |
| 301 | { |
| 302 | t_account * account; |
| 303 | |
| 304 | assert(attrgroup); |
| 305 | |
| 306 | if (!(account = account_create(NULL, NULL))) { |
| 307 | eventlog(eventlog_level_error, __FUNCTION__, "could not create account"); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | account->attrgroup = attrgroup; |
| 312 | |
| 313 | return account; |
| 314 | } |
| 315 | |
| 316 | static t_account * account_load_new(char const * name, unsigned uid) |
| 317 | { |
| 318 | t_account *account; |
| 319 | t_attrgroup *attrgroup; |
| 320 | |
| 321 | if (name && account_check_name(name)) return NULL; |
| 322 | |
| 323 | force_account_add = 1; /* disable the protection */ |
| 324 | |
| 325 | attrgroup = attrgroup_create_nameuid(name, uid); |
| 326 | if (!attrgroup) { |
| 327 | force_account_add = 0; |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | if (!(account = account_load(attrgroup))) { |
| 332 | eventlog(eventlog_level_error, __FUNCTION__, "could not load account"); |
| 333 | attrgroup_destroy(attrgroup); |
| 334 | force_account_add = 0; |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | if (!accountlist_add_account(account)) { |
| 339 | eventlog(eventlog_level_error, __FUNCTION__, "could not add account to list"); |
| 340 | account_destroy(account); |
| 341 | force_account_add = 0; |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | force_account_add = 0; |
| 346 | |
| 347 | return account; |
| 348 | } |
| 349 | |
| 350 | static int _cb_read_accounts(t_attrgroup *attrgroup, void *data) |
| 351 | { |
| 352 | unsigned int *count = (unsigned int *)data; |
| 353 | t_account *account; |
| 354 | |
| 355 | if (!(account = account_load(attrgroup))) { |
| 356 | eventlog(eventlog_level_error, __FUNCTION__, "could not load account"); |
| 357 | attrgroup_destroy(attrgroup); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | if (!accountlist_add_account(account)) { |
| 362 | eventlog(eventlog_level_error, __FUNCTION__, "could not add account to list"); |
| 363 | account_destroy(account); |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | /* might as well free up the memory since we probably won't need it */ |
| 368 | account_flush(account, FS_FORCE); /* force unload */ |
| 369 | |
| 370 | (*count)++; |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | extern int accountlist_load_all(int flag) |
| 376 | { |
| 377 | static bool loaded = false; // all accounts already loaded ? |
| 378 | |
| 379 | if (loaded) |
| 380 | { |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | auto t0 = std::chrono::steady_clock::now(); |
| 385 | |
| 386 | unsigned int count = 0; |
| 387 | force_account_add = 1; // disable the protection |
| 388 | int ret = attrgroup_read_accounts(flag, _cb_read_accounts, &count); |
| 389 | force_account_add = 0; // enable the protection |
| 390 | |
| 391 | auto t1 = std::chrono::steady_clock::now(); |
| 392 | |
| 393 | if (ret == 0) |
| 394 | { |
| 395 | loaded = true; |
| 396 | eventlog(eventlog_level_info, __FUNCTION__, "Successfully loaded {} user accounts in {} milliseconds", count, std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | else if (ret == -1) |
| 401 | { |
| 402 | eventlog(eventlog_level_error, __FUNCTION__, "got error reading user accounts"); |
| 403 | |
| 404 | return -1; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | return 0; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | extern int accountlist_create(void) |
| 413 | { |
| 414 | eventlog(eventlog_level_info, __FUNCTION__, "started creating accountlist"); |
| 415 | |
| 416 | if (!(accountlist_head = hashtable_create(prefs_get_hashtable_size()))) |
| 417 | { |
| 418 | eventlog(eventlog_level_error, __FUNCTION__, "could not create accountlist_head"); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | if (!(accountlist_uid_head = hashtable_create(prefs_get_hashtable_size()))) |
| 423 | { |
| 424 | eventlog(eventlog_level_error, __FUNCTION__, "could not create accountlist_uid_head"); |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | /* load accounts without force, indexed storage types wont be loading */ |
| 429 | accountlist_load_all(ST_NONE); |
| 430 | maxuserid = storage->read_maxuserid(); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | extern int accountlist_destroy(void) |
| 437 | { |
| 438 | t_entry * curr; |
| 439 | t_account * account; |
| 440 | |
| 441 | HASHTABLE_TRAVERSE(accountlist_head, curr) |
| 442 | { |
| 443 | if (!(account = (t_account*)entry_get_data(curr))) |
| 444 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL account in list"); |
| 445 | else |
| 446 | { |
| 447 | if (account_flush(account, FS_FORCE) < 0) |
| 448 | eventlog(eventlog_level_error, __FUNCTION__, "could not save account"); |
| 449 | |
| 450 | account_destroy(account); |
| 451 | } |
| 452 | hashtable_remove_entry(accountlist_head, curr); |
| 453 | } |
| 454 | |
| 455 | HASHTABLE_TRAVERSE(accountlist_uid_head, curr) |
| 456 | { |
| 457 | hashtable_remove_entry(accountlist_head, curr); |
| 458 | } |
| 459 | |
| 460 | if (hashtable_destroy(accountlist_head) < 0) |
| 461 | return -1; |
| 462 | accountlist_head = NULL; |
| 463 | if (hashtable_destroy(accountlist_uid_head) < 0) |
| 464 | return -1; |
| 465 | accountlist_uid_head = NULL; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | extern t_hashtable * accountlist(void) |
| 471 | { |
| 472 | return accountlist_head; |
| 473 | } |
| 474 | |
| 475 | extern t_hashtable * accountlist_uid(void) |
| 476 | { |
| 477 | return accountlist_uid_head; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | extern unsigned int accountlist_get_length(void) |
| 482 | { |
| 483 | return hashtable_get_length(accountlist_head); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | extern int accountlist_save(unsigned flags) |
| 488 | { |
| 489 | return attrlayer_save(flags); |
| 490 | } |
| 491 | |
| 492 | extern int accountlist_flush(unsigned flags) |
| 493 | { |
| 494 | return attrlayer_flush(flags); |
| 495 | } |
| 496 | |
| 497 | extern t_account * accountlist_find_account(char const * username) |
| 498 | { |
| 499 | unsigned int userid = 0; |
| 500 | t_entry * curr; |
| 501 | t_account * account; |
| 502 | |
| 503 | if (!username) |
| 504 | { |
| 505 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL username"); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | if (username[0] == '#') { |
| 510 | if (str_to_uint(&username[1], &userid) < 0) |
| 511 | userid = 0; |
| 512 | } |
| 513 | else if (!(prefs_get_savebyname())) |
| 514 | if (str_to_uint(username, &userid) < 0) |
| 515 | userid = 0; |
| 516 | |
| 517 | /* all accounts in list must be hashed already, no need to check */ |
| 518 | |
| 519 | if (userid) { |
| 520 | account = accountlist_find_account_by_uid(userid); |
| 521 | if (account) return account; |
| 522 | } |
| 523 | |
| 524 | if ((!(userid)) || (userid && ((username[0] == '#') || (std::isdigit((int)username[0]))))) |
| 525 | { |
| 526 | unsigned int namehash; |
| 527 | char const * tname; |
| 528 | |
| 529 | namehash = account_hash(username); |
| 530 | HASHTABLE_TRAVERSE_MATCHING(accountlist_head, curr, namehash) |
| 531 | { |
| 532 | account = (t_account*)entry_get_data(curr); |
| 533 | if ((tname = account_get_name(account))) |
| 534 | { |
| 535 | if (strcasecmp(tname, username) == 0) |
| 536 | { |
| 537 | hashtable_entry_release(curr); |
| 538 | return account; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return account_load_new(username, 0); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | extern t_account * accountlist_find_account_by_uid(unsigned int uid) |
| 549 | { |
| 550 | t_entry * curr; |
| 551 | t_account * account; |
| 552 | |
| 553 | if (uid) { |
| 554 | HASHTABLE_TRAVERSE_MATCHING(accountlist_uid_head, curr, uid) |
| 555 | { |
| 556 | account = (t_account*)entry_get_data(curr); |
| 557 | if (account->uid == uid) { |
| 558 | hashtable_entry_release(curr); |
| 559 | return account; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | return account_load_new(NULL, uid); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | extern char const *accountlist_find_vague_account(t_account * account, char const *vague_username) |
| 568 | { |
| 569 | char const *tname; |
| 570 | |
| 571 | if (!vague_username) { |
| 572 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL vague_username"); |
| 573 | return NULL; |
| 574 | } |
| 575 | if (!account) { |
| 576 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | if (tname = account_get_name(account)) { |
| 582 | if (find_substr((char*)tname, vague_username)) { |
| 583 | return tname; |
| 584 | } |
| 585 | return NULL; |
| 586 | } |
| 587 | return NULL; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | extern int accountlist_allow_add(void) |
| 592 | { |
| 593 | if (force_account_add) |
| 594 | return 1; /* the permission was forced */ |
| 595 | |
| 596 | if (prefs_get_max_accounts() == 0) |
| 597 | return 1; /* allow infinite accounts */ |
| 598 | |
| 599 | if (prefs_get_max_accounts() <= hashtable_get_length(accountlist_head)) |
| 600 | return 0; /* maximum account limit reached */ |
| 601 | |
| 602 | return 1; /* otherwise let them proceed */ |
| 603 | } |
| 604 | |
| 605 | static t_account * accountlist_add_account(t_account * account) |
| 606 | { |
| 607 | unsigned int uid; |
| 608 | char const * username; |
| 609 | |
| 610 | if (!account) { |
| 611 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | username = account_get_name(account); |
| 616 | uid = account_get_numattr(account, "BNET\\acct\\userid"); |
| 617 | |
| 618 | if (!username || std::strlen(username) < 1) { |
| 619 | eventlog(eventlog_level_error, __FUNCTION__, "got bad account (empty username)"); |
| 620 | return NULL; |
| 621 | } |
| 622 | if (uid < 1) { |
| 623 | ERROR2("got bad account (bad uid: {}) for \"{}\", fix it!", uid, username); |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | /* check whether the account limit was reached */ |
| 628 | if (!accountlist_allow_add()) { |
| 629 | eventlog(eventlog_level_warn, __FUNCTION__, "account limit reached (current is {}, storing {})", prefs_get_max_accounts(), hashtable_get_length(accountlist_head)); |
| 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | /* delayed hash, do it before inserting account into the list */ |
| 634 | account->namehash = account_hash(username); |
| 635 | account->uid = uid; |
| 636 | |
| 637 | /* FIXME: this check actually (with the new attr system) happens too late |
| 638 | * we already have created the attrgroup here which is "dirty" and refusing |
| 639 | * an account here will trigger attrgroup_destroy which will "sync" the |
| 640 | * bad data to the storage! The codes should make sure we don't fail here */ |
| 641 | /* mini version of accountlist_find_account(username) || accountlist_find_account(uid) */ |
| 642 | { |
| 643 | t_entry * curr; |
| 644 | t_account * curraccount; |
| 645 | char const * tname; |
| 646 | |
| 647 | if (uid <= maxuserid) |
| 648 | HASHTABLE_TRAVERSE_MATCHING(accountlist_uid_head, curr, uid) |
| 649 | { |
| 650 | curraccount = (t_account*)entry_get_data(curr); |
| 651 | if (curraccount->uid == uid) |
| 652 | { |
| 653 | eventlog(eventlog_level_debug, __FUNCTION__, "BUG: user \"{}\":" UID_FORMAT " already has an account (\"{}\":" UID_FORMAT ")", username, uid, account_get_name(curraccount), curraccount->uid); |
| 654 | hashtable_entry_release(curr); |
| 655 | return NULL; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | HASHTABLE_TRAVERSE_MATCHING(accountlist_head, curr, account->namehash) |
| 660 | { |
| 661 | curraccount = (t_account*)entry_get_data(curr); |
| 662 | if ((tname = account_get_name(curraccount))) |
| 663 | { |
| 664 | if (strcasecmp(tname, username) == 0) |
| 665 | { |
| 666 | eventlog(eventlog_level_debug, __FUNCTION__, "BUG: user \"{}\":" UID_FORMAT " already has an account (\"{}\":" UID_FORMAT ")", username, uid, tname, curraccount->uid); |
| 667 | hashtable_entry_release(curr); |
| 668 | return NULL; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (hashtable_insert_data(accountlist_head, account, account->namehash) < 0) { |
| 675 | eventlog(eventlog_level_error, __FUNCTION__, "could not add account to list"); |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | if (hashtable_insert_data(accountlist_uid_head, account, uid)<0) { |
| 680 | eventlog(eventlog_level_error, __FUNCTION__, "could not add account to list"); |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | /* hashtable_stats(accountlist_head); */ |
| 685 | |
| 686 | if (uid>maxuserid) |
| 687 | maxuserid = uid; |
| 688 | |
| 689 | return account; |
| 690 | } |
| 691 | |
| 692 | extern t_account * accountlist_create_account(const char *username, const char *passhash1) |
| 693 | { |
| 694 | t_account *res; |
| 695 | |
| 696 | assert(username != NULL); |
| 697 | assert(passhash1 != NULL); |
| 698 | |
| 699 | res = account_create(username, passhash1); |
| 700 | if (!res) return NULL; /* eventlog reported ealier */ |
| 701 | |
| 702 | if (!accountlist_add_account(res)) { |
| 703 | account_destroy(res); |
| 704 | return NULL; /* eventlog reported earlier */ |
| 705 | } |
| 706 | |
| 707 | account_save(res, FS_FORCE); /* sync new account to storage */ |
| 708 | |
| 709 | return res; |
| 710 | } |
| 711 | |
| 712 | extern int account_check_name(char const * name) |
| 713 | { |
| 714 | unsigned int i; |
| 715 | char ch; |
| 716 | |
| 717 | if (!name) { |
| 718 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL name"); |
| 719 | return -1; |
| 720 | } |
| 721 | |
| 722 | size_t namelen = std::strlen(name); |
| 723 | for (i = 0; i < namelen; i++) |
| 724 | { |
| 725 | /* These are the Battle.net rules but they are too strict. |
| 726 | * We want to allow any characters that wouldn't cause |
| 727 | * problems so this should test for what is _not_ allowed |
| 728 | * instead of what is. |
| 729 | */ |
| 730 | ch = name[i]; |
| 731 | /* hardcoded safety checks */ |
| 732 | if (ch == '/' || ch == '\\') return -1; |
| 733 | if (std::isalnum((unsigned char)ch)) continue; |
| 734 | if (std::strchr(prefs_get_account_allowed_symbols(), ch)) continue; |
| 735 | return -1; |
| 736 | } |
| 737 | if (i < MIN_USERNAME_LEN || i >= MAX_USERNAME_LEN) |
| 738 | return -1; |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | extern char const * account_get_name_real(t_account * account, char const * fn, unsigned int ln) |
| 743 | { |
| 744 | char const * temp; |
| 745 | |
| 746 | if (!account) |
| 747 | { |
| 748 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account (from {}:{})", fn, ln); |
| 749 | return NULL; /* FIXME: places assume this can't fail */ |
| 750 | } |
| 751 | |
| 752 | if (account->name) /* we have a cached username so return it */ |
| 753 | return account->name; |
| 754 | |
| 755 | /* we dont have a cached username so lets get it from attributes */ |
| 756 | if (!(temp = account_get_strattr(account, "BNET\\acct\\username"))) |
| 757 | eventlog(eventlog_level_error, __FUNCTION__, "account has no username"); |
| 758 | else |
| 759 | account->name = xstrdup(temp); |
| 760 | return account->name; |
| 761 | } |
| 762 | |
| 763 | |
| 764 | extern int account_check_mutual(t_account * account, int myuserid) |
| 765 | { |
| 766 | if (account == NULL) { |
| 767 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | if (!myuserid) { |
| 772 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL userid"); |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | if (account->friends != NULL) |
| 777 | { |
| 778 | t_friend * fr; |
| 779 | if ((fr = friendlist_find_uid(account->friends, myuserid)) != NULL) |
| 780 | { |
| 781 | friend_set_mutual(fr, FRIEND_ISMUTUAL); |
| 782 | return 0; |
| 783 | } |
| 784 | } |
| 785 | else |
| 786 | { |
| 787 | int i; |
| 788 | int n = account_get_friendcount(account); |
| 789 | int frienduid; |
| 790 | for (i = 0; i < n; i++) |
| 791 | { |
| 792 | frienduid = account_get_friend(account, i); |
| 793 | if (!frienduid) { |
| 794 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL friend"); |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | if (myuserid == frienduid) |
| 799 | return 0; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | // If friend isnt in list return -1 to tell func NO |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | extern t_list * account_get_friends(t_account * account) |
| 808 | { |
| 809 | if (!account) |
| 810 | { |
| 811 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | if (!FLAG_ISSET(account->flags, ACCOUNT_FLAG_FLOADED)) |
| 816 | if (account_load_friends(account) < 0) |
| 817 | { |
| 818 | eventlog(eventlog_level_error, __FUNCTION__, "could not load friend list"); |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | return account->friends; |
| 823 | } |
| 824 | |
| 825 | static int account_load_friends(t_account * account) |
| 826 | { |
| 827 | int i; |
| 828 | int n; |
| 829 | int frienduid; |
| 830 | t_account * acc; |
| 831 | t_friend * fr; |
| 832 | |
| 833 | int newlist = 0; |
| 834 | if (!account) |
| 835 | { |
| 836 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 837 | return -1; |
| 838 | } |
| 839 | |
| 840 | if (FLAG_ISSET(account->flags, ACCOUNT_FLAG_FLOADED)) |
| 841 | return 0; |
| 842 | |
| 843 | if (account->friends == NULL) |
| 844 | { |
| 845 | account->friends = list_create(); |
| 846 | newlist = 1; |
| 847 | } |
| 848 | |
| 849 | n = account_get_friendcount(account); |
| 850 | for (i = 0; i < n; i++) |
| 851 | { |
| 852 | frienduid = account_get_friend(account, i); |
| 853 | if (!frienduid) { |
| 854 | account_remove_friend(account, i); |
| 855 | continue; |
| 856 | } |
| 857 | fr = NULL; |
| 858 | if (newlist || (fr = friendlist_find_uid(account->friends, frienduid)) == NULL) |
| 859 | { |
| 860 | if ((acc = accountlist_find_account_by_uid(frienduid)) == NULL) |
| 861 | { |
| 862 | if (account_remove_friend(account, i) == 0) |
| 863 | { |
| 864 | i--; |
| 865 | n--; |
| 866 | } |
| 867 | continue; |
| 868 | } |
| 869 | if (account_check_mutual(acc, account_get_uid(account)) == 0) |
| 870 | friendlist_add_account(account->friends, acc, FRIEND_ISMUTUAL); |
| 871 | else |
| 872 | friendlist_add_account(account->friends, acc, FRIEND_NOTMUTUAL); |
| 873 | } |
| 874 | else { |
| 875 | if ((acc = friend_get_account(fr)) == NULL) |
| 876 | { |
| 877 | account_remove_friend(account, i); |
| 878 | continue; |
| 879 | } |
| 880 | if (account_check_mutual(acc, account_get_uid(account)) == 0) |
| 881 | friend_set_mutual(fr, FRIEND_ISMUTUAL); |
| 882 | else |
| 883 | friend_set_mutual(fr, FRIEND_NOTMUTUAL); |
| 884 | } |
| 885 | } |
| 886 | if (!newlist) |
| 887 | friendlist_purge(account->friends); |
| 888 | FLAG_SET(&account->flags, ACCOUNT_FLAG_FLOADED); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static int account_unload_friends(t_account * account) |
| 893 | { |
| 894 | if (account->friends != nullptr && friendlist_unload(account->friends) < 0) |
| 895 | return -1; |
| 896 | FLAG_CLEAR(&account->flags, ACCOUNT_FLAG_FLOADED); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | extern int account_set_clanmember(t_account * account, t_clanmember * clanmember) |
| 901 | { |
| 902 | if (account == NULL) |
| 903 | { |
| 904 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | account->clanmember = clanmember; |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | extern t_clanmember * account_get_clanmember(t_account * account) |
| 913 | { |
| 914 | t_clanmember * member; |
| 915 | |
| 916 | if (account == NULL) |
| 917 | { |
| 918 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 919 | return NULL; |
| 920 | } |
| 921 | |
| 922 | if ((member = account->clanmember) && (clanmember_get_clan(member)) && (clan_get_created(clanmember_get_clan(member)) > 0) && (clanmember_get_fullmember(member) == 1)) |
| 923 | return member; |
| 924 | else |
| 925 | return NULL; |
| 926 | } |
| 927 | |
| 928 | extern t_clanmember * account_get_clanmember_forced(t_account * account) |
| 929 | { |
| 930 | if (account == NULL) |
| 931 | { |
| 932 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 933 | return NULL; |
| 934 | } |
| 935 | |
| 936 | return account->clanmember; |
| 937 | } |
| 938 | |
| 939 | extern t_clan * account_get_clan(t_account * account) |
| 940 | { |
| 941 | if (account == NULL) |
| 942 | { |
| 943 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | if (account->clanmember && (clanmember_get_clan(account->clanmember) != NULL) && (clan_get_created(clanmember_get_clan(account->clanmember)) > 0) && (clanmember_get_fullmember(account->clanmember) == 1)) |
| 948 | return clanmember_get_clan(account->clanmember); |
| 949 | else |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | extern t_clan * account_get_creating_clan(t_account * account) |
| 954 | { |
| 955 | if (account == NULL) |
| 956 | { |
| 957 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | if (account->clanmember && (clanmember_get_clan(account->clanmember) != NULL) && ((clan_get_created(clanmember_get_clan(account->clanmember)) <= 0) || (clanmember_get_fullmember(account->clanmember) == 0))) |
| 962 | |
| 963 | return clanmember_get_clan(account->clanmember); |
| 964 | else |
| 965 | return NULL; |
| 966 | } |
| 967 | |
| 968 | int account_set_conn(t_account * account, t_connection * conn) |
| 969 | { |
| 970 | if (!(account)) |
| 971 | { |
| 972 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 973 | return -1; |
| 974 | } |
| 975 | |
| 976 | account->conn = conn; |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | t_connection * account_get_conn(t_account * account) |
| 982 | { |
| 983 | if (!(account)) |
| 984 | { |
| 985 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | return account->conn; |
| 990 | } |
| 991 | |
| 992 | void account_add_team(t_account * account, t_team * team) |
| 993 | { |
| 994 | assert(account); |
| 995 | assert(team); |
| 996 | |
| 997 | if (!(account->teams)) |
| 998 | account->teams = list_create(); |
| 999 | |
| 1000 | list_append_data(account->teams, team); |
| 1001 | } |
| 1002 | |
| 1003 | t_team * account_find_team_by_accounts(t_account * account, t_account **accounts, t_clienttag clienttag) |
| 1004 | { |
| 1005 | if ((account->teams)) |
| 1006 | return _list_find_team_by_accounts(accounts, clienttag, account->teams); |
| 1007 | else |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | |
| 1011 | t_team * account_find_team_by_teamid(t_account * account, unsigned int teamid) |
| 1012 | { |
| 1013 | if ((account->teams)) |
| 1014 | return _list_find_team_by_teamid(teamid, account->teams); |
| 1015 | else |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | t_list * account_get_teams(t_account * account) |
| 1020 | { |
| 1021 | assert(account); |
| 1022 | |
| 1023 | return account->teams; |
| 1024 | } |
| 1025 | |
| 1026 | } |
| 1027 | |
| 1028 | } |