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