1 /*
2 channels-setup.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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "lib-config/iconfig.h"
24 #include "settings.h"
25
26 #include "chat-protocols.h"
27 #include "chatnets.h"
28 #include "servers-setup.h"
29 #include "channels-setup.h"
30
31 GSList *setupchannels;
32
compare_channel_setup(CONFIG_NODE * node,CHANNEL_SETUP_REC * channel)33 static int compare_channel_setup (CONFIG_NODE *node, CHANNEL_SETUP_REC *channel)
34 {
35 char *name, *chatnet;
36
37 name = config_node_get_str(node, "name", NULL);
38 chatnet = config_node_get_str(node, "chatnet", NULL);
39
40 if (name == NULL || chatnet == NULL) {
41 return 0;
42 }
43
44 if (g_ascii_strcasecmp(name, channel->name) != 0 ||
45 g_ascii_strcasecmp(chatnet, channel->chatnet) != 0) {
46 return 1;
47 }
48
49 return 0;
50 }
51
channel_setup_save(CHANNEL_SETUP_REC * channel)52 static void channel_setup_save(CHANNEL_SETUP_REC *channel)
53 {
54 CONFIG_NODE *parent_node, *node;
55 GSList *config_node;
56
57 parent_node = iconfig_node_traverse("(channels", TRUE);
58
59 /* Try to find this channel in the configuration */
60 config_node = g_slist_find_custom(parent_node->value, channel,
61 (GCompareFunc)compare_channel_setup);
62 if (config_node != NULL)
63 /* Let's update this channel record */
64 node = config_node->data;
65 else
66 /* Create a brand-new channel record */
67 node = iconfig_node_section(parent_node, NULL, NODE_TYPE_BLOCK);
68
69 iconfig_node_clear(node);
70 iconfig_node_set_str(node, "name", channel->name);
71 iconfig_node_set_str(node, "chatnet", channel->chatnet);
72 if (channel->autojoin)
73 iconfig_node_set_bool(node, "autojoin", TRUE);
74 iconfig_node_set_str(node, "password", channel->password);
75 iconfig_node_set_str(node, "botmasks", channel->botmasks);
76 iconfig_node_set_str(node, "autosendcmd", channel->autosendcmd);
77 }
78
channel_setup_create(CHANNEL_SETUP_REC * channel)79 void channel_setup_create(CHANNEL_SETUP_REC *channel)
80 {
81 channel->type = module_get_uniq_id("CHANNEL SETUP", 0);
82
83 if (g_slist_find(setupchannels, channel) == NULL)
84 setupchannels = g_slist_append(setupchannels, channel);
85 channel_setup_save(channel);
86
87 signal_emit("channel setup created", 1, channel);
88 }
89
channel_config_remove(CHANNEL_SETUP_REC * channel)90 static void channel_config_remove(CHANNEL_SETUP_REC *channel)
91 {
92 CONFIG_NODE *parent_node;
93 GSList *config_node;
94
95 parent_node = iconfig_node_traverse("channels", FALSE);
96
97 if (parent_node == NULL)
98 return;
99
100 /* Try to find this channel in the configuration */
101 config_node = g_slist_find_custom(parent_node->value, channel,
102 (GCompareFunc)compare_channel_setup);
103
104 if (config_node != NULL)
105 /* Delete the channel from the configuration */
106 iconfig_node_remove(parent_node, config_node->data);
107 }
108
channel_setup_destroy(CHANNEL_SETUP_REC * channel)109 static void channel_setup_destroy(CHANNEL_SETUP_REC *channel)
110 {
111 g_return_if_fail(channel != NULL);
112
113 setupchannels = g_slist_remove(setupchannels, channel);
114 signal_emit("channel setup destroyed", 1, channel);
115
116 g_free_not_null(channel->chatnet);
117 g_free_not_null(channel->password);
118 g_free_not_null(channel->botmasks);
119 g_free_not_null(channel->autosendcmd);
120 g_free(channel->name);
121 g_free(channel);
122 }
123
channel_setup_remove_chatnet(const char * chatnet)124 void channel_setup_remove_chatnet(const char *chatnet)
125 {
126 GSList *tmp, *next;
127
128 g_return_if_fail(chatnet != NULL);
129
130 for (tmp = setupchannels; tmp != NULL; tmp = next) {
131 CHANNEL_SETUP_REC *rec = tmp->data;
132
133 next = tmp->next;
134 if (g_ascii_strcasecmp(rec->chatnet, chatnet) == 0)
135 channel_setup_remove(rec);
136 }
137 }
138
channel_setup_remove(CHANNEL_SETUP_REC * channel)139 void channel_setup_remove(CHANNEL_SETUP_REC *channel)
140 {
141 channel_config_remove(channel);
142 channel_setup_destroy(channel);
143 }
144
channel_setup_find(const char * channel,const char * chatnet)145 CHANNEL_SETUP_REC *channel_setup_find(const char *channel,
146 const char *chatnet)
147 {
148 GSList *tmp;
149
150 g_return_val_if_fail(channel != NULL, NULL);
151
152 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
153 CHANNEL_SETUP_REC *rec = tmp->data;
154
155 if (g_ascii_strcasecmp(rec->name, channel) == 0 &&
156 channel_chatnet_match(rec->chatnet, chatnet))
157 return rec;
158 }
159
160 return NULL;
161 }
162
channel_setup_read(CONFIG_NODE * node)163 static CHANNEL_SETUP_REC *channel_setup_read(CONFIG_NODE *node)
164 {
165 CHANNEL_SETUP_REC *rec;
166 CHATNET_REC *chatnetrec;
167 char *channel, *chatnet;
168
169 g_return_val_if_fail(node != NULL, NULL);
170
171 channel = config_node_get_str(node, "name", NULL);
172 chatnet = config_node_get_str(node, "chatnet", NULL);
173
174 chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
175 if (channel == NULL || chatnetrec == NULL) {
176 /* missing information.. */
177 return NULL;
178 }
179
180 rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
181 rec->type = module_get_uniq_id("CHANNEL SETUP", 0);
182 rec->chat_type = CHAT_PROTOCOL(chatnetrec)->id;
183 rec->autojoin = config_node_get_bool(node, "autojoin", FALSE);
184 rec->name = g_strdup(channel);
185 rec->chatnet = g_strdup(chatnetrec != NULL ? chatnetrec->name : chatnet);
186 rec->password = g_strdup(config_node_get_str(node, "password", NULL));
187 rec->botmasks = g_strdup(config_node_get_str(node, "botmasks", NULL));
188 rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
189
190 setupchannels = g_slist_append(setupchannels, rec);
191 signal_emit("channel setup created", 2, rec, node);
192 return rec;
193 }
194
channels_read_config(void)195 static void channels_read_config(void)
196 {
197 CONFIG_NODE *node;
198 GSList *tmp;
199
200 while (setupchannels != NULL)
201 channel_setup_destroy(setupchannels->data);
202
203 /* Read channels */
204 node = iconfig_node_traverse("channels", FALSE);
205 if (node != NULL) {
206 tmp = config_node_first(node->value);
207 for (; tmp != NULL; tmp = config_node_next(tmp))
208 channel_setup_read(tmp->data);
209 }
210 }
211
channels_setup_init(void)212 void channels_setup_init(void)
213 {
214 setupchannels = NULL;
215 source_host_ok = FALSE;
216
217 signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
218 signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
219 }
220
channels_setup_deinit(void)221 void channels_setup_deinit(void)
222 {
223 while (setupchannels != NULL)
224 channel_setup_destroy(setupchannels->data);
225
226 signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
227 signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
228 }
229