File: peerKeys.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (999 lines) | stat: -rw-r--r-- 20,882 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/*
GameSpy Peer SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

/*************
** INCLUDES **
*************/
#include <stdlib.h>
#include "peerKeys.h"
#include "peerCallbacks.h"
#include "peerRooms.h"
#include "peerGlobalCallbacks.h"
#include "peerMangle.h"

/************
** DEFINES **
************/
#define PI_WATCH_KEYS_NUM_BUCKETS      16
#define PI_WATCH_CACHE_NUM_BUCKETS     128

/**********
** TYPES **
**********/
// Stored in the *WatckKey tables.
//////////////////////////////////
typedef struct piWatchKey
{
	char * key;
} piWatchKey;

// Stored in the *WatchCache tables.
////////////////////////////////////
typedef struct piCacheKey
{
	char * nick;
	char * key;
	char * value;
#ifdef GSI_UNICODE
	unsigned short * value_W;
#endif
} piCacheKey;

typedef struct piClearWatchKeyCacheData
{
	const char * key;
	HashTable watchCache;
} piClearWatchKeyCacheData;

typedef struct piRemoveExistingKeysData
{
	int num;
	const char ** keys;
	HashTable watchKeys;
} piRemoveExistingKeysData;

typedef struct piPlayerChangedNickMapData
{
	const char * oldNick;
	const char * newNick;
} piPlayerChangedNickMapData;

typedef struct piSetupKeysMapData
{
	int next;
	char ** keys;
} piSetupKeysMapData;

typedef struct piCleanseRoomCacheMapData
{
	PEER peer;
	RoomType roomType;
} piCleanseRoomCacheMapData;

/**************
** FUNCTIONS **
**************/
#ifndef GSI_UNICODE // NOT GSI_UNICODE, these have to coincide with the chatsdk build
#define piGetRoomKeysCallback		piGetRoomKeysCallbackA
#define piGetGlobalKeysCallback		piGetGlobalKeysCallbackA
#else
#define piGetRoomKeysCallback		piGetRoomKeysCallbackW
#define piGetGlobalKeysCallback		piGetGlobalKeysCallbackW
#endif


static int WatchKeysHash
(
	const void * elem,
	int numBuckets
)
{
	piWatchKey * key = (piWatchKey *)elem;
	int c;
	const char * str;
	unsigned int hash;

	assert(key->key && key->key[0]);

	// Get the hash.
	////////////////
	str = key->key;
	hash = 0;
	while((c = *str++) != '\0')
		hash += (unsigned int)tolower(c);
	hash %= (unsigned int)numBuckets;

	return (int)hash;
}

static int GS_STATIC_CALLBACK WatchKeysCompare
(
	const void * elem1,
	const void * elem2
)
{
	piWatchKey * key1 = (piWatchKey *)elem1;
	piWatchKey * key2 = (piWatchKey *)elem2;

	assert(key1->key && key1->key[0] && key2->key && key2->key[0]);

	return strcasecmp(key1->key, key2->key);
}

static void WatchKeysFree
(
	void * elem
)
{
	piWatchKey * key = (piWatchKey *)elem;

	gsifree(key->key);
}

static int WatchCacheHash
(
	const void * elem,
	int numBuckets
)
{
	piCacheKey * key = (piCacheKey *)elem;
	int c;
	const char * str;
	unsigned int hash;

	assert(key);
	assert(key->nick[0]);

	// Get the hash.
	////////////////
	str = key->key;
	hash = 0;
	while((c = *str++) != '\0')
		hash += (unsigned int)tolower(c);
	hash %= (unsigned int)numBuckets;

	return (int)hash;
}

static int GS_STATIC_CALLBACK WatchCacheCompare
(
	const void * elem1,
	const void * elem2
)
{
	int rcode;

	piCacheKey * key1 = (piCacheKey *)elem1;
	piCacheKey * key2 = (piCacheKey *)elem2;

	rcode = strcasecmp(key1->nick, key2->nick);
	if(rcode)
		return rcode;

	return strcasecmp(key1->key, key2->key);
}

static void WatchCacheFree
(
	void * elem
)
{
	piCacheKey * key = (piCacheKey *)elem;

	gsifree(key->nick);
	gsifree(key->key);
	gsifree(key->value);
#ifdef GSI_UNICODE
	gsifree(key->value_W);
#endif
}

PEERBool piKeysInit
(
	PEER peer
)
{
	int roomType;
	PEER_CONNECTION;

	memset(connection->globalWatchKeys, 0, sizeof(HashTable) * NumRooms);
	memset(connection->roomWatchKeys, 0, sizeof(HashTable) * NumRooms);
	memset(connection->roomWatchCache, 0, sizeof(HashTable) * NumRooms);

	// Create all the tables.
	/////////////////////////
	connection->globalWatchCache = TableNew(sizeof(piCacheKey), PI_WATCH_CACHE_NUM_BUCKETS,
			WatchCacheHash, WatchCacheCompare, WatchCacheFree);
	if(!connection->globalWatchCache)
		return PEERFalse;
	for(roomType = 0 ; roomType < NumRooms ; roomType++)
	{
		connection->globalWatchKeys[roomType] = TableNew(sizeof(piWatchKey), PI_WATCH_KEYS_NUM_BUCKETS,
			WatchKeysHash, WatchKeysCompare, WatchKeysFree);
		connection->roomWatchKeys[roomType] = TableNew(sizeof(piWatchKey), PI_WATCH_KEYS_NUM_BUCKETS,
			WatchKeysHash, WatchKeysCompare, WatchKeysFree);
		connection->roomWatchCache[roomType] = TableNew(sizeof(piCacheKey), PI_WATCH_CACHE_NUM_BUCKETS,
			WatchCacheHash, WatchCacheCompare, WatchCacheFree);

		if(!connection->globalWatchKeys[roomType] || !connection->roomWatchKeys[roomType] ||
			!connection->roomWatchCache[roomType])
		{
			return PEERFalse;
		}
	}

	return PEERTrue;
}

void piKeysCleanup
(
	PEER peer
)
{
	int roomType;

	PEER_CONNECTION;

	if(connection->globalWatchCache)
		TableFree(connection->globalWatchCache);

	for(roomType = 0 ; roomType < NumRooms ; roomType++)
	{
		if(connection->globalWatchKeys[roomType])
			TableFree(connection->globalWatchKeys[roomType]);
		if(connection->roomWatchKeys[roomType])
			TableFree(connection->roomWatchKeys[roomType]);
		if(connection->roomWatchCache[roomType])
			TableFree(connection->roomWatchCache[roomType]);
	}
}

static const char * piGetWatchKeyA
(
	const char * nick,
	const char * key,
	HashTable watchCache
)
{
	piCacheKey keyTemp;
	piCacheKey * cacheKey;

	keyTemp.nick = (char *)nick;
	keyTemp.key = (char *)key;
	cacheKey = (piCacheKey *)TableLookup(watchCache, &keyTemp);

	if(!cacheKey)
		return NULL;

	if(cacheKey->value)
		return cacheKey->value;
	return "";
}
static const unsigned short * piGetWatchKeyW
(
	const unsigned short * nick,
	const unsigned short * key,
	HashTable watchCache
)
{
#ifndef GSI_UNICODE
	GSI_UNUSED(nick);
	GSI_UNUSED(key);
	GSI_UNUSED(watchCache);
	return NULL; // can't use this function unless in GSI_UNICODE mode
#else
	piCacheKey keyTemp;
	piCacheKey * cacheKey;

	char* nick_A = UCS2ToUTF8StringAlloc(nick);
	char* key_A = UCS2ToUTF8StringAlloc(key);

	keyTemp.nick = (char *)nick_A;
	keyTemp.key = (char *)key_A;
	cacheKey = (piCacheKey *)TableLookup(watchCache, &keyTemp);

	gsifree(nick_A);
	gsifree(key_A);

	if (!cacheKey)
		return NULL;

	if(cacheKey->value_W)
		return cacheKey->value_W;
	return L"";
#endif
}

const char * piGetGlobalWatchKeyA
(
	PEER peer,
	const char * nick,
	const char * key
)
{
	PEER_CONNECTION;

	return piGetWatchKeyA(nick, key, connection->globalWatchCache);
}
const unsigned short * piGetGlobalWatchKeyW
(
	PEER peer,
	const unsigned short * nick,
	const unsigned short * key
)
{
	PEER_CONNECTION;

	return piGetWatchKeyW(nick, key, connection->globalWatchCache);
}

const char * piGetRoomWatchKeyA
(
	PEER peer,
	RoomType roomType,
	const char * nick,
	const char * key
)
{
	PEER_CONNECTION;

	return piGetWatchKeyA(nick, key, connection->roomWatchCache[roomType]);
}
const unsigned short * piGetRoomWatchKeyW
(
	PEER peer,
	RoomType roomType,
	const unsigned short * nick,
	const unsigned short * key
)
{
	PEER_CONNECTION;

	return piGetWatchKeyW(nick, key, connection->roomWatchCache[roomType]);
}

static void piCleanseGlobalCacheMap
(
	void * elem,
	void * clientData
)
{
	piPlayer * player;
	int roomType;
	piCacheKey * cacheKey = (piCacheKey *)elem;
	PEER peer = (PEER)clientData;
	piConnection * connection = (piConnection *)peer;
	piWatchKey watchKeyTemp;

	watchKeyTemp.key = cacheKey->key;

	player = piGetPlayer(peer, cacheKey->nick);
	if(player)
	{
		for(roomType = 0 ; roomType < NumRooms ; roomType++)
		{
			if(player->inRoom[roomType])
			{
				if(TableLookup(connection->globalWatchKeys[roomType], &watchKeyTemp))
					return;
			}
		}
	}

	TableRemove(connection->globalWatchCache, cacheKey);
}

static void piCleanseRoomCacheMap
(
	void * elem,
	void * clientData
)
{
	piPlayer * player;
	piCacheKey * cacheKey = (piCacheKey *)elem;
	piCleanseRoomCacheMapData * data = (piCleanseRoomCacheMapData *)clientData;
	piConnection * connection = (piConnection *)data->peer;
	piWatchKey watchKeyTemp;

	watchKeyTemp.key = cacheKey->key;

	player = piGetPlayer(data->peer, cacheKey->nick);
	if(player && player->inRoom[data->roomType])
	{
		if(TableLookup(connection->roomWatchKeys[data->roomType], &watchKeyTemp))
			return;
	}

	TableRemove(connection->globalWatchCache, cacheKey);
}

void piKeyCacheCleanse
(
	PEER peer
)
{
	int roomType;
	piCleanseRoomCacheMapData data;

	PEER_CONNECTION;

	TableMapSafe(connection->globalWatchCache, piCleanseGlobalCacheMap, peer);

	data.peer = peer;
	for(roomType = 0 ; roomType < NumRooms ; roomType++)
	{
		if(IN_ROOM || ENTERING_ROOM)
		{
			data.roomType = (RoomType)roomType;
			TableMapSafe(connection->roomWatchCache[roomType], piCleanseRoomCacheMap, &data);
		}
		else
		{
			TableClear(connection->roomWatchCache[roomType]);
		}
	}
}

static void piGetGlobalKeysCallbackA
(
	CHAT chat,
	CHATBool success,
	const char * user,
	int num,
	const char ** keys,
	const char ** values,
	void * param
)
{
	PEER peer = (PEER)param;

	if(success && user)
	{
		int i;

		for(i = 0 ; i < num ; i++)
			piGlobalKeyChanged(peer, user, keys[i], values[i]);
	}
	
	GSI_UNUSED(chat);
}
#ifdef GSI_UNICODE
static void piGetGlobalKeysCallbackW
(
	CHAT chat,
	CHATBool success,
	const unsigned short * user,
	int num,
	const unsigned short ** keys,
	const unsigned short ** values,
	void * param
)
{
	char* user_A = UCS2ToUTF8StringAlloc(user);
	char** keys_A = UCS2ToUTF8StringArrayAlloc(keys, num);
	char** values_A = UCS2ToUTF8StringArrayAlloc(values, num);
	int i;
	piGetGlobalKeysCallbackA(chat, success, user_A, num, (const char**)keys_A, (const char**)values_A, param);
	gsifree(user_A);
	for (i=0; i<num; i++)
	{
		if (keys_A != NULL)	gsifree(keys_A[i]);
		if (values_A != NULL) gsifree(values_A[i]);
	}
	gsifree(keys_A);
	gsifree(values_A);
}
#endif

static void piGetRoomKeysCallbackA
(
	CHAT chat,
	CHATBool success,
	const char * channel,
	const char * user,
	int num,
	const char ** keys,
	const char ** values,
	void * param
)
{
	PEER peer = (PEER)param;

	if(!user && success)
	{
		//
		// If <user> is NULL, then this has been called by the
		// GETCKEY list-end handler (ciRplEndGetCKeyHandler),
		// and the PlayerInfo callback should be called one final
		// time with NULL as the user.
		//
		// Alter the username and proceed so that all of the
		// subsequent handlers and callbacks have a real name to
		// work with. Note "(END)" is illegal as an IRC nickname
		// (09mar01/bgw).
		//
		int i;
		RoomType roomType;
		static const char * szEndName = "(END)";

		if(!piRoomToType(peer, channel, &roomType))
			return;

		for(i = 0 ; i < num ; i++)
			piRoomKeyChanged(peer, roomType, szEndName, keys[i], NULL);
		return;
	}

	if(success /*&& user*/)
	{
		int i;
		RoomType roomType;

		if(!piRoomToType(peer, channel, &roomType))
			return;

		for(i = 0 ; i < num ; i++)
			piRoomKeyChanged(peer, roomType, user, keys[i], values[i]);
	}
	
	GSI_UNUSED(chat);
}
#ifdef GSI_UNICODE
static void piGetRoomKeysCallbackW
(
	CHAT chat,
	CHATBool success,
	const unsigned short * channel,
	const unsigned short * user,
	int num,
	const unsigned short ** keys,
	const unsigned short ** values,
	void * param
)
{
	char* channel_A = UCS2ToUTF8StringAlloc(channel);
	char* user_A = UCS2ToUTF8StringAlloc(user);
	char** keys_A = UCS2ToUTF8StringArrayAlloc(keys, num);
	char** values_A = UCS2ToUTF8StringArrayAlloc(values, num);
	int i;
	piGetRoomKeysCallbackA(chat, success, channel_A, user_A, num, (const char**)keys_A, (const char**)values_A, param);
	gsifree(channel_A);
	gsifree(user_A);
	for(i=0; i<num; i++)
	{
		if (keys_A != NULL) gsifree(keys_A[i]);
		if (values_A != NULL) gsifree(values_A[i]);
	}
	gsifree(keys_A);
	gsifree(values_A);
}
#endif

static void piRemoveExistingKeysMap
(
	void * elem,
	void * clientData
)
{
	int i;

	piWatchKey * key = (piWatchKey *)elem;
	piRemoveExistingKeysData * data = (piRemoveExistingKeysData *)clientData;

	for(i = 0 ; i < data->num ; i++)
		if(strcasecmp(key->key, data->keys[i]) == 0)
			return;

	TableRemove(data->watchKeys, key);
}

void piSetGlobalWatchKeys
(
	PEER peer,
	RoomType roomType,
	int num,
	const char ** keys,
	PEERBool addKeys
)
{
	piWatchKey watchKey;
	int i;

	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);
	assert(num >= 0);

	// If no keys.
	//////////////
	if(num == 0)
	{
		// Nothing to add.
		//////////////////
		if(addKeys)
			return;

		// Clear the list and cache.
		////////////////////////////
		TableClear(connection->globalWatchKeys[roomType]);
		piKeyCacheCleanse(peer);

		return;
	}

	assert(keys);

	if(!addKeys)
	{
		piRemoveExistingKeysData data;
		data.num = num;
		data.keys = keys;
		data.watchKeys = connection->globalWatchKeys[roomType];
		TableMapSafe(connection->globalWatchKeys[roomType], piRemoveExistingKeysMap, &data);
	}

	for(i = 0 ; i < num ; i++)
	{
		watchKey.key = goastrdup(keys[i]);
		TableEnter(connection->globalWatchKeys[roomType], &watchKey);
	}

	// Update them all.
	///////////////////
	if(ENTERING_ROOM || IN_ROOM)
		chatGetGlobalKeysA(connection->chat, ROOM, num, keys, piGetGlobalKeysCallback, peer, CHATFalse);
}

void piSetRoomWatchKeys
(
	PEER peer,
	RoomType roomType,
	int num,
	const char ** keys,
	PEERBool addKeys
)
{
	piWatchKey watchKey;
	int i;

	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);
	assert(num >= 0);

	// If no keys.
	//////////////
	if(num == 0)
	{
		// Nothing to add.
		//////////////////
		if(addKeys)
			return;

		// Clear the list and cache.
		////////////////////////////
		TableClear(connection->roomWatchKeys[roomType]);
		TableClear(connection->roomWatchCache[roomType]);

		return;
	}

	assert(keys);

	if(!addKeys)
	{
		piRemoveExistingKeysData data;
		data.num = num;
		data.keys = keys;
		data.watchKeys = connection->roomWatchKeys[roomType];
		TableMapSafe(connection->roomWatchKeys[roomType], piRemoveExistingKeysMap, &data);
	}

	for(i = 0 ; i < num ; i++)
	{
		watchKey.key = goastrdup(keys[i]);
		TableEnter(connection->roomWatchKeys[roomType], &watchKey);
	}

	// Update them all.
	///////////////////
	if(ENTERING_ROOM || IN_ROOM)
		chatGetChannelKeysA(connection->chat, ROOM, "*", num, keys, piGetRoomKeysCallback, peer, CHATFalse);
}

static PEERBool piKeyChanged
(
	PEER peer,
	const char * nick,
	const char * key,
	const char * value,
	HashTable watchKeys,
	HashTable watchCache,
	PEERBool inRoom,
	RoomType roomType
)
{
	piWatchKey watchKeyTemp;
	piCacheKey cacheKey;

	assert(key && key[0]);

	// We don't track anything for rooms.
	/////////////////////////////////////
	if(!nick || !nick[0])
		return PEERTrue;

	// No NULL values.
	//////////////////
	if(!value)
		value = "";

	// Is this a username (IP and Profile ID)?
	//////////////////////////////////////////
	if(strcasecmp(key, "username") == 0)
	{
		piPlayer * player;

		if(strcmp(nick, "(END)") == 0)
		{
			//
			// If <nick> is "(END)", this is the final message from
			// a GETCKEYS list. Call straight into the PlayerInfo
			// callback with zeroed arguments (09mar01/bgw).
			//
			assert(inRoom);
			piAddPlayerInfoCallback(peer, roomType, NULL, 0, 0);
			return PEERFalse;
		}

		// Get the player.
		//////////////////
		player = piGetPlayer(peer, nick);
		if(player && !player->gotIPAndProfileID)
		{
			int profileID;
			unsigned int IP;
			
			// Get the info.
			////////////////
			if(piDemangleUser(value, &IP, &profileID))
			{
				// Cache the info.
				//////////////////
				piSetPlayerIPAndProfileID(peer, nick, IP, profileID);
			}
		}

		// If this was in a room, call the callback.
		////////////////////////////////////////////
		if(inRoom)
		{
			if(player && player->gotIPAndProfileID)
				piAddPlayerInfoCallback(peer, roomType, nick, player->IP, player->profileID);
			else
				piAddPlayerInfoCallback(peer, roomType, nick, 0, 0);
		}
	}

	// Is this flags?
	/////////////////
	if(inRoom && strcasecmp(key, "b_flags") == 0)
		piSetPlayerRoomFlags(peer, nick, roomType, value);

	// Check if this is a watch key.
	////////////////////////////////
	watchKeyTemp.key = (char *)key;
	if(!TableLookup(watchKeys, &watchKeyTemp))
	{
		// Is it a broadcast key?
		/////////////////////////
		if(inRoom && (strncmp(key, "b_", 2) == 0))
			return PEERTrue;
		return PEERFalse;
	}

	// Cache it.
	////////////
	memset(&cacheKey, 0, sizeof(piCacheKey));
	cacheKey.nick = goastrdup(nick);
	cacheKey.key = goastrdup(key);
	cacheKey.value = goastrdup(value);
#ifdef GSI_UNICODE
	cacheKey.value_W = UTF8ToUCS2StringAlloc(cacheKey.value);
#endif
	TableEnter(watchCache, &cacheKey);

	return PEERTrue;
}

void piGlobalKeyChanged
(
	PEER peer,
	const char * nick,
	const char * key,
	const char * value
)
{
	PEER_CONNECTION;

	if(piKeyChanged(peer, nick, key, value, connection->globalWatchKeys[TitleRoom], connection->globalWatchCache, PEERFalse, (RoomType)0) ||
		piKeyChanged(peer, nick, key, value, connection->globalWatchKeys[GroupRoom], connection->globalWatchCache, PEERFalse, (RoomType)0) ||
		piKeyChanged(peer, nick, key, value, connection->globalWatchKeys[StagingRoom], connection->globalWatchCache, PEERFalse, (RoomType)0))
		piAddGlobalKeyChangedCallback(peer, nick, key, value);
}

void piRoomKeyChanged
(
	PEER peer,
	RoomType roomType,
	const char * nick,
	const char * key,
	const char * value
)
{
	PEER_CONNECTION;

	if(piKeyChanged(peer, nick, key, value, connection->roomWatchKeys[roomType], connection->roomWatchCache[roomType], PEERTrue, roomType))
		piAddRoomKeyChangedCallback(peer, roomType, nick, key, value);
}

static void piPlayerChangedNickMap
(
	void * elem,
	void * clientData
)
{
	piCacheKey * key = (piCacheKey *)elem;
	piPlayerChangedNickMapData * data = (piPlayerChangedNickMapData *)clientData;

	if(strcasecmp(key->nick, data->oldNick) == 0)
	{
		gsifree(key->nick);
		key->nick = goastrdup(data->newNick);
	}
}

void piKeyCachePlayerChangedNick
(
	PEER peer,
	const char * oldNick,
	const char * newNick
)
{
	piPlayerChangedNickMapData data;

	PEER_CONNECTION;

	data.oldNick = oldNick;
	data.newNick = newNick;

	TableMap(connection->globalWatchCache, piPlayerChangedNickMap, &data);
	TableMap(connection->roomWatchCache[0], piPlayerChangedNickMap, &data);
	TableMap(connection->roomWatchCache[1], piPlayerChangedNickMap, &data);
	TableMap(connection->roomWatchCache[2], piPlayerChangedNickMap, &data);
}

static void piSetupKeysMap
(
	void * elem,
	void * clientData
)
{
	piWatchKey * key = (piWatchKey *)elem;
	piSetupKeysMapData * data = (piSetupKeysMapData *)clientData;

	// Add the key to the list.
	///////////////////////////
	data->keys[data->next++] = key->key;
}

static void piKeyCacheRefresh
(
	PEER peer,
	RoomType roomType,
	const char * nick
)
{
	int num;
	piSetupKeysMapData data;
	PEERBool getIP;
	PEERBool getFlags;
	piWatchKey watchKey;

	PEER_CONNECTION;

	assert(IN_ROOM || ENTERING_ROOM);
	if(!IN_ROOM && !ENTERING_ROOM)
		return;

	// Get the IP if we're pinging this room.
	/////////////////////////////////////////
	if(!nick)
		getIP = (connection->pingRoom[roomType] || connection->alwaysRequestPlayerInfo)?PEERTrue:PEERFalse;
	else
		getIP = PEERFalse;

	// Check if we're already getting the IP.
	/////////////////////////////////////////
	if(getIP)
	{
		watchKey.key = "username";
		getIP = (PEERBool)(!TableLookup(connection->roomWatchKeys[roomType], &watchKey));
	}

	// Don't get flags on a player - he just joined, and will set them.
	///////////////////////////////////////////////////////////////////
	getFlags = (PEERBool)(nick == NULL);

	// Check for a b_flags watch key.
	/////////////////////////////////
	if(getFlags)
	{
		watchKey.key = "b_flags";
		getFlags = (PEERBool)(!(TableLookup(connection->globalWatchKeys[roomType], &watchKey) ||
			TableLookup(connection->roomWatchKeys[roomType], &watchKey)));
	}

	data.next = 0;
	num = TableCount(connection->globalWatchKeys[roomType]);
	if(num)
	{
		data.keys = (char **)gsimalloc(sizeof(char *) * num);
		if(!data.keys)
			return;
		TableMap(connection->globalWatchKeys[roomType], piSetupKeysMap, &data);
		assert(data.next == num);
		chatGetGlobalKeysA(connection->chat, nick?nick:ROOM, num, (const char **)data.keys, piGetGlobalKeysCallback, peer, CHATFalse);
		gsifree(data.keys);
	}

	if(!nick)
	{
		data.next = 0;
		num = TableCount(connection->roomWatchKeys[roomType]);
		if(getIP)
			num++;
		if(getFlags)
			num++;
		if(num)
		{
			data.keys = (char **)gsimalloc(sizeof(char *) * num);
			if(!data.keys)
				return;
			TableMap(connection->roomWatchKeys[roomType], piSetupKeysMap, &data);
			if(getIP)
				data.keys[data.next++] = "username";
			if(getFlags)
				data.keys[data.next++] = "b_flags";
			assert(data.next == num);
			chatGetChannelKeysA(connection->chat, ROOM, nick?nick:"*", num, (const char **)data.keys, piGetRoomKeysCallback, peer, CHATFalse);
			gsifree(data.keys);
		}
	}
}

void piKeyCacheRefreshPlayer
(
	PEER peer,
	RoomType roomType,
	const char * nick
)
{
	piKeyCacheRefresh(peer, roomType, nick);
}

void piKeyCacheRefreshRoom
(
	PEER peer,
	RoomType roomType
)
{
	piKeyCacheRefresh(peer, roomType, NULL);
}