summaryrefslogblamecommitdiff
path: root/src/map/channel.c
blob: 6ebe4650df47ce1d4a6378fcca841248e8485d6e (plain) (tree)
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









































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                             
// 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/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;

void channel_create(struct channel_data *chan, char *name, char *pass, unsigned char color)
{
	chan->users = idb_alloc(DB_OPT_BASE);
	if (name)
		safestrncpy(chan->name, name, HCS_NAME_LENGTH);
	chan->color = color;
	if (!pass)
		chan->pass[0] = '\0';
	else
		safestrncpy(chan->pass, pass, HCS_NAME_LENGTH);

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

	chan->msg_delay = 0;

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

/**
 * 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();
	}
}

void channel_join(struct channel_data *chan, struct map_session_data *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 (sd->stealth) {
		sd->stealth = false;
	} else if (chan->opt & 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);
	}

}

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;

		CREATE(map->list[sd->bl.m].channel, struct channel_data , 1);
		safestrncpy(map->list[sd->bl.m].channel->name, channel->config->local_name, HCS_NAME_LENGTH);
		map->list[sd->bl.m].channel->type = HCS_TYPE_MAP;
		map->list[sd->bl.m].channel->m = sd->bl.m;

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

	if( map->list[sd->bl.m].channel->banned && idb_exists(map->list[sd->bl.m].channel->banned, sd->status.account_id) ) {
		return;
	}

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

	if( !( map->list[sd->bl.m].channel->opt & HCS_OPT_ANNOUNCE_JOIN ) ) {
		char mout[60];
		sprintf(mout, msg_txt(1435), channel->config->local_name, map->list[sd->bl.m].name); // You're now in the '#%s' channel for '%s'
		clif->colormes(sd->fd, COLOR_DEFAULT, mout);
	}
}

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->opt & 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_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->opt & 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 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->opt & 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;
}

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);
}

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(chan,sd);
			}
		}
	}

	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(chan,sd);
			}
		}
	}
}

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 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) {
			struct channel_data *chd;
			CREATE(chd, struct channel_data, 1);

			safestrncpy(chd->name, channel->config->irc_name, HCS_NAME_LENGTH);
			chd->type = HCS_TYPE_IRC;

			channel->create(chd, NULL, NULL, channel->config->irc_color);
			ircbot->channel = chd;
		}

		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);
				struct channel_data *chd;

				for (k = 0; k < channel->config->colors_count; k++) {
					if (strcmpi(channel->config->colors_name[k],color) == 0)
						break;
				}
				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;

				}
				CREATE( chd, struct channel_data, 1 );

				safestrncpy(chd->name, name, HCS_NAME_LENGTH);
				chd->type = HCS_TYPE_PUBLIC;

				channel->create(chd,NULL,NULL,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;
	/* core */
	channel->init = do_init_channel;
	channel->final = do_final_channel;
	channel->config = &channel_config;

	channel->create = channel_create;
	channel->send = channel_send;
	channel->join = channel_join;
	channel->leave = channel_leave;
	channel->delete = channel_delete;
	channel->map_join = channel_map_join;
	channel->quit = channel_quit;
	channel->quit_guild = channel_quit_guild;
	channel->guild_join = channel_guild_join;
	channel->guild_leave = channel_guild_leave;
	channel->config_read = read_channels_config;
}