9a92b896caac2bc2dd6692aae625c2b8ef704474
[silc.git] / apps / irssi / src / core / channels.c
1 /*
2  channel.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "misc.h"
24 #include "special-vars.h"
25
26 #include "servers.h"
27 #include "channels.h"
28 #include "channels-setup.h"
29 #include "nicklist.h"
30
31 GSList *channels; /* List of all channels */
32
33 static char *get_join_data(CHANNEL_REC *channel)
34 {
35         return g_strdup(channel->name);
36 }
37
38 static const char *channel_get_target(WI_ITEM_REC *item)
39 {
40         return ((CHANNEL_REC *) item)->name;
41 }
42
43 void channel_init(CHANNEL_REC *channel, SERVER_REC *server, const char *name,
44                   const char *visible_name, int automatic)
45 {
46         g_return_if_fail(channel != NULL);
47         g_return_if_fail(name != NULL);
48         g_return_if_fail(server != NULL);
49
50         if (visible_name == NULL)
51                 visible_name = name;
52
53         MODULE_DATA_INIT(channel);
54         channel->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "CHANNEL");
55         channel->destroy = (void (*) (WI_ITEM_REC *)) channel_destroy;
56         channel->get_target = channel_get_target;
57         channel->get_join_data = get_join_data;
58
59         channel->chat_type = server->chat_type;
60         channel->server = server;
61         channel->name = g_strdup(name);
62         channel->visible_name = g_strdup(visible_name);
63         channel->mode = g_strdup("");
64         channel->createtime = time(NULL);
65
66         channels = g_slist_append(channels, channel);
67         server->channels = g_slist_append(server->channels, channel);
68
69         signal_emit("channel created", 2, channel, GINT_TO_POINTER(automatic));
70 }
71
72 void channel_destroy(CHANNEL_REC *channel)
73 {
74         g_return_if_fail(IS_CHANNEL(channel));
75
76         if (channel->destroying) return;
77         channel->destroying = TRUE;
78
79         channels = g_slist_remove(channels, channel);
80         channel->server->channels =
81                 g_slist_remove(channel->server->channels, channel);
82
83         signal_emit("channel destroyed", 1, channel);
84
85         MODULE_DATA_DEINIT(channel);
86         g_free_not_null(channel->hilight_color);
87         g_free_not_null(channel->topic);
88         g_free_not_null(channel->topic_by);
89         g_free_not_null(channel->key);
90         g_free(channel->mode);
91         g_free(channel->name);
92
93         channel->type = 0;
94         g_free(channel);
95 }
96
97 static CHANNEL_REC *channel_find_server(SERVER_REC *server,
98                                         const char *name)
99 {
100         GSList *tmp;
101
102         g_return_val_if_fail(IS_SERVER(server), NULL);
103
104         if (server->channel_find_func != NULL) {
105                 /* use the server specific channel find function */
106                 return server->channel_find_func(server, name);
107         }
108
109         for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
110                 CHANNEL_REC *rec = tmp->data;
111
112                 if (g_strcasecmp(name, rec->name) == 0)
113                         return rec;
114         }
115
116         return NULL;
117 }
118
119 CHANNEL_REC *channel_find(SERVER_REC *server, const char *name)
120 {
121         g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
122         g_return_val_if_fail(name != NULL, NULL);
123
124         if (server != NULL)
125                 return channel_find_server(server, name);
126
127         /* find from any server */
128         return gslist_foreach_find(servers,
129                                    (FOREACH_FIND_FUNC) channel_find_server,
130                                    (void *) name);
131 }
132
133 void channel_change_name(CHANNEL_REC *channel, const char *name)
134 {
135         g_return_if_fail(IS_CHANNEL(channel));
136
137         g_free(channel->name);
138         channel->name = g_strdup(name);
139
140         signal_emit("channel name changed", 1, channel);
141 }
142
143 void channel_change_visible_name(CHANNEL_REC *channel, const char *name)
144 {
145         g_return_if_fail(IS_CHANNEL(channel));
146
147         g_free(channel->visible_name);
148         channel->visible_name = g_strdup(name);
149
150         signal_emit("window item name changed", 1, channel);
151 }
152
153 static CHANNEL_REC *channel_find_servers(GSList *servers, const char *name)
154 {
155         return gslist_foreach_find(servers,
156                                    (FOREACH_FIND_FUNC) channel_find_server,
157                                    (void *) name);
158 }
159
160 static GSList *servers_find_chatnet_except(SERVER_REC *server)
161 {
162         GSList *tmp, *list;
163
164         list = NULL;
165         for (tmp = servers; tmp != NULL; tmp = tmp->next) {
166                 SERVER_REC *rec = tmp->data;
167
168                 if (server != rec && rec->connrec->chatnet != NULL &&
169                     strcmp(server->connrec->chatnet,
170                            rec->connrec->chatnet) == 0) {
171                         /* chatnets match */
172                         list = g_slist_append(list, rec);
173                 }
174         }
175
176         return list;
177 }
178
179 /* connected to server, autojoin to channels. */
180 static void event_connected(SERVER_REC *server)
181 {
182         GString *chans;
183         GSList *tmp, *chatnet_servers;
184
185         g_return_if_fail(SERVER(server));
186
187         if (server->connrec->reconnection ||
188             server->connrec->no_autojoin_channels)
189                 return;
190
191         /* get list of servers in same chat network */
192         chatnet_servers = server->connrec->chatnet == NULL ? NULL:
193                 servers_find_chatnet_except(server);
194
195         /* join to the channels marked with autojoin in setup */
196         chans = g_string_new(NULL);
197         for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
198                 CHANNEL_SETUP_REC *rec = tmp->data;
199
200                 if (!rec->autojoin ||
201                     !channel_chatnet_match(rec->chatnet,
202                                            server->connrec->chatnet))
203                         continue;
204
205                 /* check that we haven't already joined this channel in
206                    same chat network connection.. */
207                 if (channel_find_servers(chatnet_servers, rec->name) == NULL)
208                         g_string_sprintfa(chans, "%s,", rec->name);
209         }
210         g_slist_free(chatnet_servers);
211
212         if (chans->len > 0) {
213                 g_string_truncate(chans, chans->len-1);
214                 server->channels_join(server, chans->str, TRUE);
215         }
216
217         g_string_free(chans, TRUE);
218 }
219
220 static int match_nick_flags(SERVER_REC *server, NICK_REC *nick, char flag)
221 {
222         const char *flags = server->get_nick_flags();
223
224         return strchr(flags, flag) == NULL ||
225                 (flag == flags[0] && nick->op) ||
226                 (flag == flags[1] && (nick->voice || nick->halfop ||
227                                       nick->op)) ||
228                 (flag == flags[2] && (nick->halfop || nick->op));
229 }
230
231 /* Send the auto send command to channel */
232 void channel_send_autocommands(CHANNEL_REC *channel)
233 {
234         CHANNEL_SETUP_REC *rec;
235         NICK_REC *nick;
236         char **bots, **bot;
237
238         g_return_if_fail(IS_CHANNEL(channel));
239
240         if (channel->session_rejoin)
241                 return;
242
243         rec = channel_setup_find(channel->name, channel->server->connrec->chatnet);
244         if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
245                 return;
246
247         if (rec->botmasks == NULL || !*rec->botmasks) {
248                 /* just send the command. */
249                 eval_special_string(rec->autosendcmd, "", channel->server, channel);
250                 return;
251         }
252
253         /* find first available bot.. */
254         bots = g_strsplit(rec->botmasks, " ", -1);
255         for (bot = bots; *bot != NULL; bot++) {
256                 const char *botnick = *bot;
257
258                 if (*botnick == '\0')
259                         continue;
260
261                 nick = nicklist_find_mask(channel,
262                                           channel->server->isnickflag(*botnick) ?
263                                           botnick+1 : botnick);
264                 if (nick != NULL &&
265                     match_nick_flags(channel->server, nick, *botnick)) {
266                         eval_special_string(rec->autosendcmd, nick->nick,
267                                             channel->server, channel);
268                         break;
269                 }
270         }
271         g_strfreev(bots);
272 }
273
274 void channels_init(void)
275 {
276         channels_setup_init();
277
278         signal_add("event connected", (SIGNAL_FUNC) event_connected);
279 }
280
281 void channels_deinit(void)
282 {
283         channels_setup_deinit();
284
285         signal_remove("event connected", (SIGNAL_FUNC) event_connected);
286 }