2 channels-setup.c : irssi
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
23 #include "lib-config/iconfig.h"
26 #include "chat-protocols.h"
28 #include "servers-setup.h"
29 #include "channels-setup.h"
31 GSList *setupchannels;
33 static void channel_setup_save(CHANNEL_SETUP_REC *channel)
35 CONFIG_NODE *parentnode, *node;
38 index = g_slist_index(setupchannels, channel);
40 parentnode = iconfig_node_traverse("(channels", TRUE);
41 node = config_node_nth(parentnode, index);
43 node = config_node_section(parentnode, NULL, NODE_TYPE_BLOCK);
45 iconfig_node_clear(node);
46 iconfig_node_set_str(node, "name", channel->name);
47 iconfig_node_set_str(node, "chatnet", channel->chatnet);
48 if (channel->autojoin)
49 iconfig_node_set_bool(node, "autojoin", TRUE);
50 iconfig_node_set_str(node, "password", channel->password);
51 iconfig_node_set_str(node, "botmasks", channel->botmasks);
52 iconfig_node_set_str(node, "autosendcmd", channel->autosendcmd);
55 void channel_setup_create(CHANNEL_SETUP_REC *channel)
57 channel->type = module_get_uniq_id("CHANNEL SETUP", 0);
59 if (g_slist_find(setupchannels, channel) == NULL)
60 setupchannels = g_slist_append(setupchannels, channel);
61 channel_setup_save(channel);
63 signal_emit("channel setup created", 1, channel);
66 static void channel_config_remove(CHANNEL_SETUP_REC *channel)
70 node = iconfig_node_traverse("channels", FALSE);
71 if (node != NULL) iconfig_node_list_remove(node, g_slist_index(setupchannels, channel));
74 static void channel_setup_destroy(CHANNEL_SETUP_REC *channel)
76 g_return_if_fail(channel != NULL);
78 setupchannels = g_slist_remove(setupchannels, channel);
79 signal_emit("channel setup destroyed", 1, channel);
81 g_free_not_null(channel->chatnet);
82 g_free_not_null(channel->password);
83 g_free_not_null(channel->botmasks);
84 g_free_not_null(channel->autosendcmd);
85 g_free(channel->name);
89 void channel_setup_remove(CHANNEL_SETUP_REC *channel)
91 channel_config_remove(channel);
92 channel_setup_destroy(channel);
95 CHANNEL_SETUP_REC *channel_setup_find(const char *channel,
100 g_return_val_if_fail(channel != NULL, NULL);
102 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
103 CHANNEL_SETUP_REC *rec = tmp->data;
105 if (g_strcasecmp(rec->name, channel) == 0 &&
106 channel_chatnet_match(rec->chatnet, chatnet))
113 static CHANNEL_SETUP_REC *channel_setup_read(CONFIG_NODE *node)
115 CHANNEL_SETUP_REC *rec;
116 CHATNET_REC *chatnetrec;
117 char *channel, *chatnet;
119 g_return_val_if_fail(node != NULL, NULL);
121 channel = config_node_get_str(node, "name", NULL);
122 chatnet = config_node_get_str(node, "chatnet", NULL);
124 chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
125 if (channel == NULL || chatnetrec == NULL) {
126 /* missing information.. */
130 rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
131 rec->type = module_get_uniq_id("CHANNEL SETUP", 0);
132 rec->chat_type = CHAT_PROTOCOL(chatnetrec)->id;
133 rec->autojoin = config_node_get_bool(node, "autojoin", FALSE);
134 rec->name = g_strdup(channel);
135 rec->chatnet = g_strdup(chatnetrec != NULL ? chatnetrec->name : chatnet);
136 rec->password = g_strdup(config_node_get_str(node, "password", NULL));
137 rec->botmasks = g_strdup(config_node_get_str(node, "botmasks", NULL));
138 rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
140 setupchannels = g_slist_append(setupchannels, rec);
141 signal_emit("channel setup created", 2, rec, node);
145 static void channels_read_config(void)
150 while (setupchannels != NULL)
151 channel_setup_destroy(setupchannels->data);
154 node = iconfig_node_traverse("channels", FALSE);
156 tmp = config_node_first(node->value);
157 for (; tmp != NULL; tmp = config_node_next(tmp))
158 channel_setup_read(tmp->data);
162 void channels_setup_init(void)
164 setupchannels = NULL;
165 source_host_ok = FALSE;
167 signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
168 signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
171 void channels_setup_deinit(void)
173 while (setupchannels != NULL)
174 channel_setup_destroy(setupchannels->data);
176 signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
177 signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);