summaryrefslogtreecommitdiff
path: root/src/map/channel.c
blob: aa7b10ba3107d337ef19a190ae49dddebed6d8fb (plain) (blame)
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
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file

#define HERCULES_CORE

#include "channel.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "atcommand.h"
#include "guild.h"
#include "instance.h"
#include "irc-bot.h"
#include "map.h"
#include "pc.h"
#include "../common/cbasetypes.h"
#include "../common/conf.h"
#include "../common/db.h"
#include "../common/malloc.h"
#include "../common/nullpo.h"
#include "../common/random.h"
#include "../common/showmsg.h"
#include "../common/socket.h"
#include "../common/strlib.h"
#include "../common/timer.h"
#include "../common/utils.h"

struct channel_interface channel_s;

static struct Channel_Config channel_config;

/**
 * Returns the named channel.
 *
 * @param name The channel name
 * @param sd   The issuer character, for character-specific channels (i.e. map, ally)
 * @return a pointer to the channel, or NULL.
 */
struct channel_data *channel_search(const char *name, struct map_session_data *sd)
{
	const char *realname = name;
	if (!realname || !*realname)
		return NULL;
	if (*realname == '#') {
		realname++;
		if (!*realname)
			return NULL;
	}

	if (channel->config->local && strcmpi(realname, channel->config->local_name) == 0) {
		if (!sd)
			return NULL;
		if (!map->list[sd->bl.m].channel) {
			channel->map_join(sd);
		}
		return map->list[sd->bl.m].channel;
	}

	if (channel->config->ally && strcmpi(realname, channel->config->ally_name) == 0) {
		if (!sd || !sd->status.guild_id || !sd->guild)
			return NULL;
		return sd->guild->channel;
	}

	return strdb_get(channel->db, realname);
}

/**
 * Creates a chat channel.
 *
 * If the channel type isn't HCS_TYPE_MAP or HCS_TYPE_ALLY, the channel is added to the channel->db.
 *
 * @param type The channel type.
 * @param name The channel name.
 * @param color The channel chat color.
 * @return A pointer to the created channel.
 */
struct channel_data *channel_create(enum channel_types type, const char *name, unsigned char color)
{
	struct channel_data *chan;
	CREATE(chan, struct channel_data, 1);
	chan->users = idb_alloc(DB_OPT_BASE);
	if (name)
		safestrncpy(chan->name, name, HCS_NAME_LENGTH);
	chan->color = color;

	chan->options = HCS_OPT_BASE;
	chan->banned = NULL;

	chan->msg_delay = 0;

	chan->type = type;
	if (chan->type != HCS_TYPE_MAP && chan->type != HCS_TYPE_ALLY)
		strdb_put(channel->db, chan->name, chan);
	return chan;
}

void channel_delete(struct channel_data *chan)
{
	if (db_size(chan->users) && !channel->config->closing) {
		DBIterator *iter;
		struct map_session_data *sd;
		unsigned char i;
		iter = db_iterator(chan->users);
		for( sd = dbi_first(iter); dbi_exists(iter); sd = dbi_next(iter) ) {
			for( i = 0; i < sd->channel_count; i++ ) {
				if( sd->channels[i] == chan ) {
					sd->channels[i] = NULL;
					break;
				}
			}
			if( i < sd->channel_count ) {
				unsigned char cursor = 0;
				for( i = 0; i < sd->channel_count; i++ ) {
					if( sd->channels[i] == NULL )
						continue;
					if( cursor != i ) {
						sd->channels[cursor] = sd->channels[i];
					}
					cursor++;
				}
				if ( !(sd->channel_count = cursor) ) {
					aFree(sd->channels);
					sd->channels = NULL;
				}
			}
		}
		dbi_destroy(iter);
	}
	if( chan->banned ) {
		db_destroy(chan->banned);
		chan->banned = NULL;
	}
	db_destroy(chan->users);
	if( chan->m ) {
		map->list[chan->m].channel = NULL;
		aFree(chan);
	} else if ( chan->type == HCS_TYPE_ALLY )
		aFree(chan);
	else if (!channel->config->closing)
		strdb_remove(channel->db, chan->name);
}

/**
 * Sets a chat channel password.
 *
 * @param chan The channel to edit.
 * @param pass The password to set. Pass NULL to remove existing passwords.
 */
void channel_set_password(struct channel_data *chan, const char *password)
{
	nullpo_retv(chan);
	if (password)
		safestrncpy(chan->password, password, HCS_NAME_LENGTH);
	else
		chan->password[0] = '\0';
}

/**
 * Bans a character from the given channel.
 *
 * @param chan The channel.
 * @param ssd  The source character, if any.
 * @param tsd  The target character.
 * @retval HCS_STATUS_OK if the operation succeeded.
 * @retval HCS_STATUS_ALREADY if the target character is already banned.
 * @retval HCS_STATUS_NOPERM if the source character doesn't have enough permissions.
 * @retval HCS_STATUS_FAIL in case of generic failure.
 */
enum channel_operation_status channel_ban(struct channel_data *chan, const struct map_session_data *ssd, struct map_session_data *tsd)
{
	struct channel_ban_entry *entry = NULL;

	nullpo_retr(HCS_STATUS_FAIL, chan);
	nullpo_retr(HCS_STATUS_FAIL, tsd);

	if (ssd && chan->owner != ssd->status.char_id && !pc_has_permission(ssd, PC_PERM_HCHSYS_ADMIN))
		return HCS_STATUS_NOPERM;

	if (pc_has_permission(tsd, PC_PERM_HCHSYS_ADMIN))
		return HCS_STATUS_FAIL;

	if (chan->banned && idb_exists(chan->banned, tsd->status.account_id))
		return HCS_STATUS_ALREADY;

	if (!chan->banned)
		chan->banned = idb_alloc(DB_OPT_BASE|DB_OPT_ALLOW_NULL_DATA|DB_OPT_RELEASE_DATA);

	CREATE(entry, struct channel_ban_entry, 1);
	safestrncpy(entry->name, tsd->status.name, NAME_LENGTH);
	idb_put(chan->banned, tsd->status.account_id, entry);

	channel->leave(chan, tsd);

	return HCS_STATUS_OK;
}

/**
 * Unbans a character from the given channel.
 *
 * @param chan The channel.
 * @param ssd  The source character, if any.
 * @param tsd  The target character. If no target character is specified, all characters are unbanned.
 * @retval HCS_STATUS_OK if the operation succeeded.
 * @retval HCS_STATUS_ALREADY if the target character is not banned.
 * @retval HCS_STATUS_NOPERM if the source character doesn't have enough permissions.
 * @retval HCS_STATUS_FAIL in case of generic failure.
 */
enum channel_operation_status channel_unban(struct channel_data *chan, const struct map_session_data *ssd, struct map_session_data *tsd)
{
	nullpo_retr(HCS_STATUS_FAIL, chan);

	if (ssd && chan->owner != ssd->status.char_id && !pc_has_permission(ssd, PC_PERM_HCHSYS_ADMIN))
		return HCS_STATUS_NOPERM;

	if (!chan->banned)
		return HCS_STATUS_ALREADY;

	if (!tsd) {
		// Unban all
		db_destroy(chan->banned);
		chan->banned = NULL;
		return HCS_STATUS_OK;
	}

	// Unban one
	if (!idb_exists(chan->banned, tsd->status.account_id))
		return HCS_STATUS_ALREADY;

	idb_remove(chan->banned, tsd->status.account_id);
	if (!db_size(chan->banned)) {
		db_destroy(chan->banned);
		chan->banned = NULL;
	}

	return HCS_STATUS_OK;
}

/**
 * Sets or edits a channel's options.
 *
 * @param chan The channel.
 * @param options The new options set to apply.
 */
void channel_set_options(struct channel_data *chan, unsigned int options)
{
	nullpo_retv(chan);

	chan->options = options;
}

/**
 * Sends a message to a channel.
 *
 * @param chan The destination channel.
 * @param sd   The source character.
 * @param msg  The message to send.
 */
void channel_send(struct channel_data *chan, struct map_session_data *sd, const char *msg)
{
	nullpo_retv(chan);
	nullpo_retv(sd);

	if (chan->msg_delay != 0 && DIFF_TICK(sd->hchsysch_tick + chan->msg_delay*1000, timer->gettick()) > 0
	 && !pc_has_permission(sd, PC_PERM_HCHSYS_ADMIN)) {
		clif->colormes(sd->fd,COLOR_RED,msg_txt(1455));
		return;
	} else {
		char message[150];
		snprintf(message, 150, "[ #%s ] %s : %s",chan->name,sd->status.name, msg);
		clif->channel_msg(chan,sd,message);
		if (chan->type == HCS_TYPE_IRC)
			ircbot->relay(sd->status.name,msg);
		if (chan->msg_delay != 0)
			sd->hchsysch_tick = timer->gettick();
	}
}

/**
 * Joins a channel, without any permission checks.
 *
 * @param chan    The channel to join
 * @param sd      The character
 * @param stealth If true, hide join announcements.
 */
void channel_join_sub(struct channel_data *chan, struct map_session_data *sd, bool stealth)
{
	nullpo_retv(chan);
	nullpo_retv(sd);

	if (idb_put(chan->users, sd->status.char_id, sd))
		return;

	RECREATE(sd->channels, struct channel_data *, ++sd->channel_count);
	sd->channels[ sd->channel_count - 1 ] = chan;

	if (!stealth && (chan->options&HCS_OPT_ANNOUNCE_JOIN)) {
		char message[60];
		sprintf(message, "#%s '%s' joined",chan->name,sd->status.name);
		clif->channel_msg(chan,sd,message);
	}

	/* someone is cheating, we kindly disconnect the bastard */
	if (sd->channel_count > 200) {
		set_eof(sd->fd);
	}

}

/**
 * Joins a channel.
 *
 * Ban and password checks are performed before joining the channel.
 * If the channel is an HCS_TYPE_ALLY channel, alliance channels are automatically joined.
 *
 * @param chan     The channel to join
 * @param sd       The character
 * @param password The specified join password, if any
 * @param silent   If true, suppress the "You're now in the <x> channel" greeting message
 * @retval HCS_STATUS_OK      if the operation succeeded
 * @retval HCS_STATUS_ALREADY if the character is already in the channel
 * @retval HCS_STATUS_NOPERM  if the specified password doesn't match
 * @retval HCS_STATUS_BANNED  if the character is in the channel's ban list
 * @retval HCS_STATUS_FAIL    in case of generic error
 */
enum channel_operation_status channel_join(struct channel_data *chan, struct map_session_data *sd, const char *password, bool silent)
{
	bool stealth = false;

	nullpo_retr(HCS_STATUS_FAIL, chan);
	nullpo_retr(HCS_STATUS_FAIL, sd);

	if (idb_exists(chan->users, sd->status.char_id)) {
		return HCS_STATUS_ALREADY;
	}

	if (chan->password[0] != '\0' && strcmp(chan->password, password) != 0) {
		if (pc_has_permission(sd, PC_PERM_HCHSYS_ADMIN)) {
			stealth = true;
		} else {
			return HCS_STATUS_NOPERM;
		}
	}

	if (chan->banned && idb_exists(chan->banned, sd->status.account_id)) {
		return HCS_STATUS_BANNED;
	}

	if (!silent && !(chan->options&HCS_OPT_ANNOUNCE_JOIN)) {
		char output[CHAT_SIZE_MAX];
		if (chan->type == HCS_TYPE_MAP) {
			sprintf(output, msg_txt(1435), chan->name, map->list[sd->bl.m].name); // You're now in the '#%s' channel for '%s'
		} else {
			sprintf(output, msg_txt(1403), chan->name); // You're now in the '%s' channel
		}
		clif->colormes(sd->fd, COLOR_DEFAULT, output);
	}

	if (chan->type == HCS_TYPE_ALLY) {
		struct guild *g = sd->guild;
		int i;
		for (i = 0; i < MAX_GUILDALLIANCE; i++) {
			struct guild *sg = NULL;
			if (g->alliance[i].opposition == 0 && g->alliance[i].guild_id && (sg = guild->search(g->alliance[i].guild_id))) {
				if (!(sg->channel->banned && idb_exists(sg->channel->banned, sd->status.account_id))) {
					channel->join_sub(sg->channel, sd, stealth);
				}
			}
		}
	}

	channel->join_sub(chan, sd, stealth);

	return HCS_STATUS_OK;
}

void channel_leave(struct channel_data *chan, struct map_session_data *sd)
{
	unsigned char i;

	if ( !idb_remove(chan->users,sd->status.char_id) )
		return;

	if( chan == sd->gcbind )
		sd->gcbind = NULL;

	if( !db_size(chan->users) && chan->type == HCS_TYPE_PRIVATE ) {
		channel->delete(chan);
	} else if( !channel->config->closing && (chan->options & HCS_OPT_ANNOUNCE_JOIN) ) {
		char message[60];
		sprintf(message, "#%s '%s' left",chan->name,sd->status.name);
		clif->channel_msg(chan,sd,message);
	}

	for( i = 0; i < sd->channel_count; i++ ) {
		if( sd->channels[i] == chan ) {
			sd->channels[i] = NULL;
			break;
		}
	}

	if( i < sd->channel_count ) {
		unsigned char cursor = 0;
		for( i = 0; i < sd->channel_count; i++ ) {
			if( sd->channels[i] == NULL )
				continue;
			if( cursor != i ) {
				sd->channels[cursor] = sd->channels[i];
			}
			cursor++;
		}
		if ( !(sd->channel_count = cursor) ) {
			aFree(sd->channels);
			sd->channels = NULL;
		}
	}

}

void channel_quit(struct map_session_data *sd)
{
	unsigned char i;

	for (i = 0; i < sd->channel_count; i++) {
		struct channel_data *chan = sd->channels[i];
		if (chan != NULL) {
			idb_remove(chan->users,sd->status.char_id);

			if( chan == sd->gcbind )
				sd->gcbind = NULL;

			if (!db_size(chan->users) && chan->type == HCS_TYPE_PRIVATE) {
				channel->delete(chan);
			} else if (!channel->config->closing && (chan->options & HCS_OPT_ANNOUNCE_JOIN)) {
				char message[60];
				sprintf(message, "#%s '%s' left",chan->name,sd->status.name);
				clif->channel_msg(chan,sd,message);
			}

		}
	}

	sd->channel_count = 0;
	aFree(sd->channels);
	sd->channels = NULL;
}

/**
 * Joins the local map channel.
 *
 * @param sd The target character
 */
void channel_map_join(struct map_session_data *sd)
{
	if (sd->state.autotrade || sd->state.standalone)
		return;
	if (!map->list[sd->bl.m].channel) {
		if (map->list[sd->bl.m].flag.chsysnolocalaj || (map->list[sd->bl.m].instance_id >= 0 && instance->list[map->list[sd->bl.m].instance_id].owner_type != IOT_NONE))
			return;

		map->list[sd->bl.m].channel = channel->create(HCS_TYPE_MAP, channel->config->local_name, channel->config->local_color);
		map->list[sd->bl.m].channel->m = sd->bl.m;
	}

	channel->join(map->list[sd->bl.m].channel, sd, NULL, false);
}

void channel_guild_join(struct guild *g1,struct guild *g2)
{
	struct map_session_data *sd;
	struct channel_data *chan;
	int j;

	if( (chan = g1->channel) ) {
		for(j = 0; j < g2->max_member; j++) {
			if( (sd = g2->member[j].sd) != NULL ) {
				if( !(g1->channel->banned && idb_exists(g1->channel->banned, sd->status.account_id)))
					channel->join_sub(chan,sd, false);
			}
		}
	}

	if( (chan = g2->channel) ) {
		for(j = 0; j < g1->max_member; j++) {
			if( (sd = g1->member[j].sd) != NULL ) {
				if( !(g2->channel->banned && idb_exists(g2->channel->banned, sd->status.account_id)))
				channel->join_sub(chan,sd, false);
			}
		}
	}
}

void channel_guild_leave(struct guild *g1,struct guild *g2)
{
	struct map_session_data *sd;
	struct channel_data *chan;
	int j;

	if( (chan = g1->channel) ) {
		for(j = 0; j < g2->max_member; j++) {
			if( (sd = g2->member[j].sd) != NULL ) {
				channel->leave(chan,sd);
			}
		}
	}

	if( (chan = g2->channel) ) {
		for(j = 0; j < g1->max_member; j++) {
			if( (sd = g1->member[j].sd) != NULL ) {
				channel->leave(chan,sd);
			}
		}
	}
}

void channel_quit_guild(struct map_session_data *sd)
{
	unsigned char i;

	for( i = 0; i < sd->channel_count; i++ ) {
		struct channel_data *chan = sd->channels[i];
		if (chan != NULL && chan->type == HCS_TYPE_ALLY) {
			if (!idb_remove(chan->users,sd->status.char_id))
				continue;

			if( chan == sd->gcbind )
				sd->gcbind = NULL;

			if (!db_size(chan->users) && chan->type == HCS_TYPE_PRIVATE) {
				channel->delete(chan);
			} else if (!channel->config->closing && (chan->options & HCS_OPT_ANNOUNCE_JOIN)) {
				char message[60];
				sprintf(message, "#%s '%s' left",chan->name,sd->status.name);
				clif->channel_msg(chan,sd,message);
			}
			sd->channels[i] = NULL;
		}
	}

	if( i < sd->channel_count ) {
		unsigned char cursor = 0;
		for( i = 0; i < sd->channel_count; i++ ) {
			if( sd->channels[i] == NULL )
				continue;
			if( cursor != i ) {
				sd->channels[cursor] = sd->channels[i];
			}
			cursor++;
		}
		if ( !(sd->channel_count = cursor) ) {
			aFree(sd->channels);
			sd->channels = NULL;
		}
	}

}

void read_channels_config(void)
{
	config_t channels_conf;
	config_setting_t *chsys = NULL;
	const char *config_filename = "conf/channels.conf"; // FIXME hardcoded name

	if (libconfig->read_file(&channels_conf, config_filename))
		return;

	chsys = libconfig->lookup(&channels_conf, "chsys");

	if (chsys != NULL) {
		config_setting_t *settings = libconfig->setting_get_elem(chsys, 0);
		config_setting_t *channels;
		config_setting_t *colors;
		int i,k;
		const char *local_name, *ally_name,
					*local_color, *ally_color,
					*irc_name, *irc_color;
		int ally_enabled = 0, local_enabled = 0,
			local_autojoin = 0, ally_autojoin = 0,
			allow_user_channel_creation = 0,
			irc_enabled = 0;

		if( !libconfig->setting_lookup_string(settings, "map_local_channel_name", &local_name) )
			local_name = "map";
		safestrncpy(channel->config->local_name, local_name, HCS_NAME_LENGTH);

		if( !libconfig->setting_lookup_string(settings, "ally_channel_name", &ally_name) )
			ally_name = "ally";
		safestrncpy(channel->config->ally_name, ally_name, HCS_NAME_LENGTH);

		if( !libconfig->setting_lookup_string(settings, "irc_channel_name", &irc_name) )
			irc_name = "irc";
		safestrncpy(channel->config->irc_name, irc_name, HCS_NAME_LENGTH);

		libconfig->setting_lookup_bool(settings, "map_local_channel", &local_enabled);
		libconfig->setting_lookup_bool(settings, "ally_channel_enabled", &ally_enabled);
		libconfig->setting_lookup_bool(settings, "irc_channel_enabled", &irc_enabled);

		if (local_enabled)
			channel->config->local = true;
		if (ally_enabled)
			channel->config->ally = true;
		if (irc_enabled)
			channel->config->irc = true;

		channel->config->irc_server[0] = channel->config->irc_channel[0] = channel->config->irc_nick[0] = channel->config->irc_nick_pw[0] = '\0';

		if (channel->config->irc) {
			const char *irc_server, *irc_channel,
				 *irc_nick, *irc_nick_pw;
			int irc_use_ghost = 0;
			if( libconfig->setting_lookup_string(settings, "irc_channel_network", &irc_server) ) {
				if( !strstr(irc_server,":") ) {
					channel->config->irc = false;
					ShowWarning("channels.conf : network port wasn't found in 'irc_channel_network', disabling irc channel...\n");
				} else {
					unsigned char d = 0, dlen = strlen(irc_server);
					char server[40];
					if (dlen > 39)
						dlen = 39;
					memset(server, '\0', sizeof(server));

					for(d = 0; d < dlen; d++) {
						if(irc_server[d] == ':') {
							memcpy(server, irc_server, d);
							safestrncpy(channel->config->irc_server, server, 40);
							memcpy(server, &irc_server[d+1], dlen - d - 1);
							channel->config->irc_server_port = atoi(server);
							break;
						}
					}
				}
			} else {
				channel->config->irc = false;
				ShowWarning("channels.conf : irc channel enabled but irc_channel_network wasn't found, disabling irc channel...\n");
			}
			if( libconfig->setting_lookup_string(settings, "irc_channel_channel", &irc_channel) )
				safestrncpy(channel->config->irc_channel, irc_channel, 50);
			else {
				channel->config->irc = false;
				ShowWarning("channels.conf : irc channel enabled but irc_channel_channel wasn't found, disabling irc channel...\n");
			}
			if( libconfig->setting_lookup_string(settings, "irc_channel_nick", &irc_nick) ) {
				if( strcmpi(irc_nick,"Hercules_chSysBot") == 0 ) {
					sprintf(channel->config->irc_nick, "Hercules_chSysBot%d",rnd()%777);
				} else
					safestrncpy(channel->config->irc_nick, irc_nick, 40);
			} else {
				channel->config->irc = false;
				ShowWarning("channels.conf : irc channel enabled but irc_channel_nick wasn't found, disabling irc channel...\n");
			}
			if( libconfig->setting_lookup_string(settings, "irc_channel_nick_pw", &irc_nick_pw) ) {
				safestrncpy(channel->config->irc_nick_pw, irc_nick_pw, 30);
				config_setting_lookup_bool(settings, "irc_channel_use_ghost", &irc_use_ghost);
				channel->config->irc_use_ghost = irc_use_ghost;
			}

		}

		libconfig->setting_lookup_bool(settings, "map_local_channel_autojoin", &local_autojoin);
		libconfig->setting_lookup_bool(settings, "ally_channel_autojoin", &ally_autojoin);

		if (local_autojoin)
			channel->config->local_autojoin = true;
		if (ally_autojoin)
			channel->config->ally_autojoin = true;

		libconfig->setting_lookup_bool(settings, "allow_user_channel_creation", &allow_user_channel_creation);

		if( allow_user_channel_creation )
			channel->config->allow_user_channel_creation = true;

		if( (colors = libconfig->setting_get_member(settings, "colors")) != NULL ) {
			int color_count = libconfig->setting_length(colors);
			CREATE(channel->config->colors, unsigned int, color_count);
			CREATE(channel->config->colors_name, char *, color_count);
			for(i = 0; i < color_count; i++) {
				config_setting_t *color = libconfig->setting_get_elem(colors, i);

				CREATE(channel->config->colors_name[i], char, HCS_NAME_LENGTH);

				safestrncpy(channel->config->colors_name[i], config_setting_name(color), HCS_NAME_LENGTH);

				channel->config->colors[i] = (unsigned int)strtoul(libconfig->setting_get_string_elem(colors,i),NULL,0);
				channel->config->colors[i] = (channel->config->colors[i] & 0x0000FF) << 16 | (channel->config->colors[i] & 0x00FF00) | (channel->config->colors[i] & 0xFF0000) >> 16;//RGB to BGR
			}
			channel->config->colors_count = color_count;
		}

		libconfig->setting_lookup_string(settings, "map_local_channel_color", &local_color);

		for (k = 0; k < channel->config->colors_count; k++) {
			if (strcmpi(channel->config->colors_name[k], local_color) == 0)
				break;
		}

		if (k < channel->config->colors_count) {
			channel->config->local_color = k;
		} else {
			ShowError("channels.conf: unknown color '%s' for 'map_local_channel_color', disabling '#%s'...\n",local_color,local_name);
			channel->config->local = false;
		}

		libconfig->setting_lookup_string(settings, "ally_channel_color", &ally_color);

		for (k = 0; k < channel->config->colors_count; k++) {
			if (strcmpi(channel->config->colors_name[k], ally_color) == 0)
				break;
		}

		if( k < channel->config->colors_count ) {
			channel->config->ally_color = k;
		} else {
			ShowError("channels.conf: unknown color '%s' for 'ally_channel_color', disabling '#%s'...\n",ally_color,ally_name);
			channel->config->ally = false;
		}

		libconfig->setting_lookup_string(settings, "irc_channel_color", &irc_color);

		for (k = 0; k < channel->config->colors_count; k++) {
			if (strcmpi(channel->config->colors_name[k], irc_color) == 0)
				break;
		}

		if (k < channel->config->colors_count) {
			channel->config->irc_color = k;
		} else {
			ShowError("channels.conf: unknown color '%s' for 'irc_channel_color', disabling '#%s'...\n",irc_color,irc_name);
			channel->config->irc = false;
		}

		if (channel->config->irc) {
			ircbot->channel = channel->create(HCS_TYPE_IRC, channel->config->irc_name, channel->config->irc_color);
		}

		if( (channels = libconfig->setting_get_member(settings, "default_channels")) != NULL ) {
			int channel_count = libconfig->setting_length(channels);

			for(i = 0; i < channel_count; i++) {
				config_setting_t *chan = libconfig->setting_get_elem(channels, i);
				const char *name = config_setting_name(chan);
				const char *color = libconfig->setting_get_string_elem(channels,i);

				ARR_FIND(0, channel->config->colors_count, k, strcmpi(channel->config->colors_name[k],color) == 0);
				if (k == channel->config->colors_count) {
					ShowError("channels.conf: unknown color '%s' for channel '%s', skipping channel...\n",color,name);
					continue;
				}
				if (strcmpi(name, channel->config->local_name) == 0
				 || strcmpi(name, channel->config->ally_name) == 0
				 || strcmpi(name, channel->config->irc_name) == 0
				 || strdb_exists(channel->db, name)) {
					ShowError("channels.conf: duplicate channel '%s', skipping channel...\n",name);
					continue;

				}
				channel->create(HCS_TYPE_PUBLIC, name, k);
			}
		}

		ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' channels in '"CL_WHITE"%s"CL_RESET"'.\n", db_size(channel->db), config_filename);
		libconfig->destroy(&channels_conf);
	}
}

/*==========================================
 *
 *------------------------------------------*/
int do_init_channel(bool minimal)
{
	if (minimal)
		return 0;

	channel->db = stridb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, HCS_NAME_LENGTH);
	channel->config->ally = channel->config->local = channel->config->irc = channel->config->ally_autojoin = channel->config->local_autojoin = false;
	channel->config_read();

	return 0;
}

void do_final_channel(void)
{
	DBIterator *iter = db_iterator(channel->db);
	struct channel_data *chan;
	unsigned char i;

	for( chan = dbi_first(iter); dbi_exists(iter); chan = dbi_next(iter) ) {
		channel->delete(chan);
	}

	dbi_destroy(iter);

	for(i = 0; i < channel->config->colors_count; i++) {
		aFree(channel->config->colors_name[i]);
	}

	if (channel->config->colors_count) {
		aFree(channel->config->colors_name);
		aFree(channel->config->colors);
	}

	db_destroy(channel->db);
}

void channel_defaults(void)
{
	channel = &channel_s;

	channel->init = do_init_channel;
	channel->final = do_final_channel;
	channel->config = &channel_config;

	channel->search = channel_search;
	channel->create = channel_create;
	channel->delete = channel_delete;

	channel->set_password = channel_set_password;
	channel->ban = channel_ban;
	channel->unban = channel_unban;
	channel->set_options = channel_set_options;

	channel->send = channel_send;
	channel->join_sub = channel_join_sub;
	channel->join = channel_join;
	channel->leave = channel_leave;
	channel->quit = channel_quit;

	channel->map_join = channel_map_join;
	channel->guild_join = channel_guild_join;
	channel->guild_leave = channel_guild_leave;
	channel->quit_guild = channel_quit_guild;

	channel->config_read = read_channels_config;
}