summaryrefslogtreecommitdiff
path: root/src/irc/core/irc-channels-setup.c
blob: 52949783ee52f5162a88b704b37467947ecc531c (plain)
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
/*
 irc-channels-setup.c : irssi

    Copyright (C) 1999-2000 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "nicklist.h"
#include "servers.h"
#include "special-vars.h"

#include "servers-setup.h"
#include "channels-setup.h"

#include "irc.h"
#include "irc-chatnets.h"
#include "irc-servers.h"
#include "irc-channels.h"

/* connected to server, autojoin to channels. */
static void event_connected(IRC_SERVER_REC *server)
{
	GString *chans;
	GSList *tmp;

	g_return_if_fail(server != NULL);

	if (server->connrec->reconnection)
		return;

	/* join to the channels marked with autojoin in setup */
	chans = g_string_new(NULL);
	for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
		CHANNEL_SETUP_REC *rec = tmp->data;

		if (!rec->autojoin ||
		    !channel_chatnet_match(rec->chatnet,
					   server->connrec->chatnet))
			continue;

		g_string_sprintfa(chans, "%s,", rec->name);
	}

	if (chans->len > 0) {
		g_string_truncate(chans, chans->len-1);
		irc_channels_join(server, chans->str, TRUE);
	}

	g_string_free(chans, TRUE);
}

/* channel wholist received: send the auto send command */
static void channel_wholist(CHANNEL_REC *channel)
{
	CHANNEL_SETUP_REC *rec;
	NICK_REC *nick;
	char **bots, **bot;

	g_return_if_fail(channel != NULL);

	rec = channels_setup_find(channel->name, channel->server->connrec->chatnet);
	if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
		return;

	if (rec->botmasks == NULL || !*rec->botmasks) {
		/* just send the command. */
		eval_special_string(rec->autosendcmd, "", channel->server, channel);
		return;
	}

	/* find first available bot.. */
	bots = g_strsplit(rec->botmasks, " ", -1);
	for (bot = bots; *bot != NULL; bot++) {
		const char *botnick = *bot;

		nick = nicklist_find(channel, isnickflag(*botnick) ?
				     botnick+1 : botnick);
		if (nick == NULL)
			continue;
		if ((*botnick == '@' && !nick->op) ||
		    (*botnick == '+' && !nick->voice && !nick->op))
			continue;

		/* got one! */
		eval_special_string(rec->autosendcmd, nick->nick, channel->server, channel);
		break;
	}
	g_strfreev(bots);
}

void irc_channels_setup_init(void)
{
	signal_add("event connected", (SIGNAL_FUNC) event_connected);
	signal_add("channel wholist", (SIGNAL_FUNC) channel_wholist);
}

void irc_channels_setup_deinit(void)
{
	signal_remove("event connected", (SIGNAL_FUNC) event_connected);
	signal_remove("channel wholist", (SIGNAL_FUNC) channel_wholist);
}