imported.
[crypto.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 void channel_init(CHANNEL_REC *channel, int automatic)
39 {
40         g_return_if_fail(channel != NULL);
41         g_return_if_fail(channel->name != NULL);
42
43         channels = g_slist_append(channels, channel);
44         if (channel->server != NULL) {
45                 channel->server->channels =
46                         g_slist_append(channel->server->channels, channel);
47         }
48
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;
54
55         signal_emit("channel created", 2, channel, GINT_TO_POINTER(automatic));
56 }
57
58 void channel_destroy(CHANNEL_REC *channel)
59 {
60         g_return_if_fail(IS_CHANNEL(channel));
61
62         if (channel->destroying) return;
63         channel->destroying = TRUE;
64
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);
69
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);
77         g_free(channel);
78 }
79
80 static CHANNEL_REC *channel_find_server(SERVER_REC *server,
81                                         const char *name)
82 {
83         GSList *tmp;
84
85         g_return_val_if_fail(IS_SERVER(server), NULL);
86
87         if (server->channel_find_func != NULL) {
88                 /* use the server specific channel find function */
89                 return server->channel_find_func(server, name);
90         }
91
92         for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
93                 CHANNEL_REC *rec = tmp->data;
94
95                 if (g_strcasecmp(name, rec->name) == 0)
96                         return rec;
97         }
98
99         return NULL;
100 }
101
102 CHANNEL_REC *channel_find(SERVER_REC *server, const char *name)
103 {
104         g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
105         g_return_val_if_fail(name != NULL, NULL);
106
107         if (server != NULL)
108                 return channel_find_server(server, name);
109
110         /* find from any server */
111         return gslist_foreach_find(servers,
112                                    (FOREACH_FIND_FUNC) channel_find_server,
113                                    (void *) name);
114 }
115
116 /* connected to server, autojoin to channels. */
117 static void event_connected(SERVER_REC *server)
118 {
119         GString *chans;
120         GSList *tmp;
121
122         g_return_if_fail(SERVER(server));
123
124         if (server->connrec->reconnection)
125                 return;
126
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;
131
132                 if (!rec->autojoin ||
133                     !channel_chatnet_match(rec->chatnet,
134                                            server->connrec->chatnet))
135                         continue;
136
137                 g_string_sprintfa(chans, "%s,", rec->name);
138         }
139
140         if (chans->len > 0) {
141                 g_string_truncate(chans, chans->len-1);
142                 server->channels_join(server, chans->str, TRUE);
143         }
144
145         g_string_free(chans, TRUE);
146 }
147
148 static int match_nick_flags(SERVER_REC *server, NICK_REC *nick, char flag)
149 {
150         const char *flags = server->get_nick_flags();
151
152         return strchr(flags, flag) == NULL ||
153                 (flag == flags[0] && nick->op) ||
154                 (flag == flags[1] && (nick->voice || nick->halfop ||
155                                       nick->op)) ||
156                 (flag == flags[2] && (nick->halfop || nick->op));
157 }
158
159 /* Send the auto send command to channel */
160 void channel_send_autocommands(CHANNEL_REC *channel)
161 {
162         CHANNEL_SETUP_REC *rec;
163         NICK_REC *nick;
164         char **bots, **bot;
165
166         g_return_if_fail(IS_CHANNEL(channel));
167
168         rec = channel_setup_find(channel->name, channel->server->connrec->chatnet);
169         if (rec == NULL || rec->autosendcmd == NULL || !*rec->autosendcmd)
170                 return;
171
172         if (rec->botmasks == NULL || !*rec->botmasks) {
173                 /* just send the command. */
174                 eval_special_string(rec->autosendcmd, "", channel->server, channel);
175                 return;
176         }
177
178         /* find first available bot.. */
179         bots = g_strsplit(rec->botmasks, " ", -1);
180         for (bot = bots; *bot != NULL; bot++) {
181                 const char *botnick = *bot;
182
183                 nick = nicklist_find_mask(channel,
184                                           channel->server->isnickflag(*botnick) ?
185                                           botnick+1 : botnick);
186                 if (nick != NULL &&
187                     match_nick_flags(channel->server, nick, *botnick)) {
188                         eval_special_string(rec->autosendcmd, nick->nick,
189                                             channel->server, channel);
190                         break;
191                 }
192         }
193         g_strfreev(bots);
194 }
195
196 void channels_init(void)
197 {
198         channels_setup_init();
199
200         signal_add("event connected", (SIGNAL_FUNC) event_connected);
201 }
202
203 void channels_deinit(void)
204 {
205         channels_setup_deinit();
206
207         signal_remove("event connected", (SIGNAL_FUNC) event_connected);
208         module_uniq_destroy("CHANNEL");
209 }