4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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.
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
24 #include "special-vars.h"
28 #include "channels-setup.h"
31 GSList *channels; /* List of all channels */
33 static char *get_join_data(CHANNEL_REC *channel)
35 return g_strdup(channel->name);
38 void channel_init(CHANNEL_REC *channel, int automatic)
40 g_return_if_fail(channel != NULL);
41 g_return_if_fail(channel->name != NULL);
43 channels = g_slist_append(channels, channel);
44 if (channel->server != NULL) {
45 channel->server->channels =
46 g_slist_append(channel->server->channels, channel);
49 MODULE_DATA_INIT(channel);
50 channel->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL");
51 channel->mode = g_strdup("");
52 channel->createtime = time(NULL);
53 channel->get_join_data = get_join_data;
55 signal_emit("channel created", 2, channel, GINT_TO_POINTER(automatic));
58 void channel_destroy(CHANNEL_REC *channel)
60 g_return_if_fail(IS_CHANNEL(channel));
62 if (channel->destroying) return;
63 channel->destroying = TRUE;
65 channels = g_slist_remove(channels, channel);
66 if (channel->server != NULL)
67 channel->server->channels = g_slist_remove(channel->server->channels, channel);
68 signal_emit("channel destroyed", 1, channel);
70 MODULE_DATA_DEINIT(channel);
71 g_free_not_null(channel->hilight_color);
72 g_free_not_null(channel->topic);
73 g_free_not_null(channel->topic_by);
74 g_free_not_null(channel->key);
75 g_free(channel->mode);
76 g_free(channel->name);
80 static CHANNEL_REC *channel_find_server(SERVER_REC *server,
85 g_return_val_if_fail(IS_SERVER(server), NULL);
87 if (server->channel_find_func != NULL) {
88 /* use the server specific channel find function */
89 return server->channel_find_func(server, name);
92 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
93 CHANNEL_REC *rec = tmp->data;
95 if (g_strcasecmp(name, rec->name) == 0)
102 CHANNEL_REC *channel_find(SERVER_REC *server, const char *name)
104 g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
105 g_return_val_if_fail(name != NULL, NULL);
108 return channel_find_server(server, name);
110 /* find from any server */
111 return gslist_foreach_find(servers,
112 (FOREACH_FIND_FUNC) channel_find_server,
116 /* connected to server, autojoin to channels. */
117 static void event_connected(SERVER_REC *server)
122 g_return_if_fail(SERVER(server));
124 if (server->connrec->reconnection)
127 /* join to the channels marked with autojoin in setup */
128 chans = g_string_new(NULL);
129 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
130 CHANNEL_SETUP_REC *rec = tmp->data;
132 if (!rec->autojoin ||
133 !channel_chatnet_match(rec->chatnet,
134 server->connrec->chatnet))
137 g_string_sprintfa(chans, "%s,", rec->name);
140 if (chans->len > 0) {
141 g_string_truncate(chans, chans->len-1);
142 server->channels_join(server, chans->str, TRUE);
145 g_string_free(chans, TRUE);
148 static int match_nick_flags(SERVER_REC *server, NICK_REC *nick, char flag)
150 const char *flags = server->get_nick_flags();
152 return strchr(flags, flag) == NULL ||
153 (flag == flags[0] && nick->op) ||
154 (flag == flags[1] && (nick->voice || nick->halfop ||
156 (flag == flags[2] && (nick->halfop || nick->op));
159 /* Send the auto send command to channel */
160 void channel_send_autocommands(CHANNEL_REC *channel)
162 CHANNEL_SETUP_REC *rec;
166 g_return_if_fail(IS_CHANNEL(channel));
168 rec = channel_setup_find(channel->name, channel->server->connrec->chatnet);
169 if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
172 if (rec->botmasks == NULL || !*rec->botmasks) {
173 /* just send the command. */
174 eval_special_string(rec->autosendcmd, "", channel->server, channel);
178 /* find first available bot.. */
179 bots = g_strsplit(rec->botmasks, " ", -1);
180 for (bot = bots; *bot != NULL; bot++) {
181 const char *botnick = *bot;
183 nick = nicklist_find_mask(channel,
184 channel->server->isnickflag(*botnick) ?
185 botnick+1 : botnick);
187 match_nick_flags(channel->server, nick, *botnick)) {
188 eval_special_string(rec->autosendcmd, nick->nick,
189 channel->server, channel);
196 void channels_init(void)
198 channels_setup_init();
200 signal_add("event connected", (SIGNAL_FUNC) event_connected);
203 void channels_deinit(void)
205 channels_setup_deinit();
207 signal_remove("event connected", (SIGNAL_FUNC) event_connected);
208 module_uniq_destroy("CHANNEL");