addition of silc.css
[silc.git] / apps / irssi / src / core / channels-setup.c
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
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 "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
33 static void channel_setup_save(CHANNEL_SETUP_REC *channel)
34 {
35         CONFIG_NODE *parentnode, *node;
36         int index;
37
38         index = g_slist_index(setupchannels, channel);
39
40         parentnode = iconfig_node_traverse("(channels", TRUE);
41         node = config_node_index(parentnode, index);
42         if (node == NULL)
43                 node = config_node_section(parentnode, NULL, NODE_TYPE_BLOCK);
44
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);
53 }
54
55 void channel_setup_create(CHANNEL_SETUP_REC *channel)
56 {
57         if (g_slist_find(setupchannels, channel) == NULL)
58                 setupchannels = g_slist_append(setupchannels, channel);
59         channel_setup_save(channel);
60
61         signal_emit("channel setup created", 1, channel);
62 }
63
64 static void channel_config_remove(CHANNEL_SETUP_REC *channel)
65 {
66         CONFIG_NODE *node;
67
68         node = iconfig_node_traverse("channels", FALSE);
69         if (node != NULL) iconfig_node_list_remove(node, g_slist_index(setupchannels, channel));
70 }
71
72 static void channel_setup_destroy(CHANNEL_SETUP_REC *channel)
73 {
74         g_return_if_fail(channel != NULL);
75
76         setupchannels = g_slist_remove(setupchannels, channel);
77         signal_emit("channel setup destroyed", 1, channel);
78
79         g_free_not_null(channel->chatnet);
80         g_free_not_null(channel->password);
81         g_free_not_null(channel->botmasks);
82         g_free_not_null(channel->autosendcmd);
83         g_free(channel->name);
84         g_free(channel);
85 }
86
87 void channel_setup_remove(CHANNEL_SETUP_REC *channel)
88 {
89         channel_config_remove(channel);
90         channel_setup_destroy(channel);
91 }
92
93 CHANNEL_SETUP_REC *channel_setup_find(const char *channel,
94                                       const char *chatnet)
95 {
96         GSList *tmp;
97
98         g_return_val_if_fail(channel != NULL, NULL);
99
100         for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
101                 CHANNEL_SETUP_REC *rec = tmp->data;
102
103                 if (g_strcasecmp(rec->name, channel) == 0 &&
104                     channel_chatnet_match(rec->chatnet, chatnet))
105                         return rec;
106         }
107
108         return NULL;
109 }
110
111 static CHANNEL_SETUP_REC *channel_setup_read(CONFIG_NODE *node)
112 {
113         CHANNEL_SETUP_REC *rec;
114         CHATNET_REC *chatnetrec;
115         char *channel, *chatnet;
116
117         g_return_val_if_fail(node != NULL, NULL);
118
119         channel = config_node_get_str(node, "name", NULL);
120         chatnet = config_node_get_str(node, "chatnet", NULL);
121         if (chatnet == NULL) /* FIXME: remove this after .98... */ {
122                 chatnet = g_strdup(config_node_get_str(node, "ircnet", NULL));
123                 if (chatnet != NULL) {
124                         iconfig_node_set_str(node, "chatnet", chatnet);
125                         iconfig_node_set_str(node, "ircnet", NULL);
126                 }
127         }
128
129         chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
130         if (channel == NULL || chatnetrec == NULL) {
131                 /* missing information.. */
132                 return NULL;
133         }
134
135         rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
136         rec->type = module_get_uniq_id("CHANNEL SETUP", 0);
137         rec->chat_type = CHAT_PROTOCOL(chatnetrec)->id;
138         rec->autojoin = config_node_get_bool(node, "autojoin", FALSE);
139         rec->name = g_strdup(channel);
140         rec->chatnet = g_strdup(chatnetrec != NULL ? chatnetrec->name : chatnet);
141         rec->password = g_strdup(config_node_get_str(node, "password", NULL));
142         rec->botmasks = g_strdup(config_node_get_str(node, "botmasks", NULL));
143         rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
144
145         setupchannels = g_slist_append(setupchannels, rec);
146         signal_emit("channel setup created", 2, rec, node);
147         return rec;
148 }
149
150 static void channels_read_config(void)
151 {
152         CONFIG_NODE *node;
153         GSList *tmp;
154
155         while (setupchannels != NULL)
156                 channel_setup_destroy(setupchannels->data);
157
158         /* Read channels */
159         node = iconfig_node_traverse("channels", FALSE);
160         if (node != NULL) {
161                 for (tmp = node->value; tmp != NULL; tmp = tmp->next)
162                         channel_setup_read(tmp->data);
163         }
164 }
165
166 void channels_setup_init(void)
167 {
168         setupchannels = NULL;
169         source_host_ok = FALSE;
170
171         signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
172         signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
173 }
174
175 void channels_setup_deinit(void)
176 {
177         while (setupchannels != NULL)
178                 channel_setup_destroy(setupchannels->data);
179
180         signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
181         signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
182 }