Merged 0.7.99 irssi.
[runtime.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
122         chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
123         if (channel == NULL || chatnetrec == NULL) {
124                 /* missing information.. */
125                 return NULL;
126         }
127
128         rec = CHAT_PROTOCOL(chatnetrec)->create_channel_setup();
129         rec->type = module_get_uniq_id("CHANNEL SETUP", 0);
130         rec->chat_type = CHAT_PROTOCOL(chatnetrec)->id;
131         rec->autojoin = config_node_get_bool(node, "autojoin", FALSE);
132         rec->name = g_strdup(channel);
133         rec->chatnet = g_strdup(chatnetrec != NULL ? chatnetrec->name : chatnet);
134         rec->password = g_strdup(config_node_get_str(node, "password", NULL));
135         rec->botmasks = g_strdup(config_node_get_str(node, "botmasks", NULL));
136         rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
137
138         setupchannels = g_slist_append(setupchannels, rec);
139         signal_emit("channel setup created", 2, rec, node);
140         return rec;
141 }
142
143 static void channels_read_config(void)
144 {
145         CONFIG_NODE *node;
146         GSList *tmp;
147
148         while (setupchannels != NULL)
149                 channel_setup_destroy(setupchannels->data);
150
151         /* Read channels */
152         node = iconfig_node_traverse("channels", FALSE);
153         if (node != NULL) {
154                 tmp = config_node_first(node->value);
155                 for (; tmp != NULL; tmp = config_node_next(tmp))
156                         channel_setup_read(tmp->data);
157         }
158 }
159
160 void channels_setup_init(void)
161 {
162         setupchannels = NULL;
163         source_host_ok = FALSE;
164
165         signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
166         signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
167 }
168
169 void channels_setup_deinit(void)
170 {
171         while (setupchannels != NULL)
172                 channel_setup_destroy(setupchannels->data);
173
174         signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
175         signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
176 }