Added SILC Thread Queue API
[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_nth(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         channel->type = module_get_uniq_id("CHANNEL SETUP", 0);
58
59         if (g_slist_find(setupchannels, channel) == NULL)
60                 setupchannels = g_slist_append(setupchannels, channel);
61         channel_setup_save(channel);
62
63         signal_emit("channel setup created", 1, channel);
64 }
65
66 static void channel_config_remove(CHANNEL_SETUP_REC *channel)
67 {
68         CONFIG_NODE *node;
69
70         node = iconfig_node_traverse("channels", FALSE);
71         if (node != NULL) iconfig_node_list_remove(node, g_slist_index(setupchannels, channel));
72 }
73
74 static void channel_setup_destroy(CHANNEL_SETUP_REC *channel)
75 {
76         g_return_if_fail(channel != NULL);
77
78         setupchannels = g_slist_remove(setupchannels, channel);
79         signal_emit("channel setup destroyed", 1, channel);
80
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);
86         g_free(channel);
87 }
88
89 void channel_setup_remove(CHANNEL_SETUP_REC *channel)
90 {
91         channel_config_remove(channel);
92         channel_setup_destroy(channel);
93 }
94
95 CHANNEL_SETUP_REC *channel_setup_find(const char *channel,
96                                       const char *chatnet)
97 {
98         GSList *tmp;
99
100         g_return_val_if_fail(channel != NULL, NULL);
101
102         for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
103                 CHANNEL_SETUP_REC *rec = tmp->data;
104
105                 if (g_strcasecmp(rec->name, channel) == 0 &&
106                     channel_chatnet_match(rec->chatnet, chatnet))
107                         return rec;
108         }
109
110         return NULL;
111 }
112
113 static CHANNEL_SETUP_REC *channel_setup_read(CONFIG_NODE *node)
114 {
115         CHANNEL_SETUP_REC *rec;
116         CHATNET_REC *chatnetrec;
117         char *channel, *chatnet;
118
119         g_return_val_if_fail(node != NULL, NULL);
120
121         channel = config_node_get_str(node, "name", NULL);
122         chatnet = config_node_get_str(node, "chatnet", NULL);
123
124         chatnetrec = chatnet == NULL ? NULL : chatnet_find(chatnet);
125         if (channel == NULL || chatnetrec == NULL) {
126                 /* missing information.. */
127                 return NULL;
128         }
129
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));
139
140         setupchannels = g_slist_append(setupchannels, rec);
141         signal_emit("channel setup created", 2, rec, node);
142         return rec;
143 }
144
145 static void channels_read_config(void)
146 {
147         CONFIG_NODE *node;
148         GSList *tmp;
149
150         while (setupchannels != NULL)
151                 channel_setup_destroy(setupchannels->data);
152
153         /* Read channels */
154         node = iconfig_node_traverse("channels", FALSE);
155         if (node != NULL) {
156                 tmp = config_node_first(node->value);
157                 for (; tmp != NULL; tmp = config_node_next(tmp))
158                         channel_setup_read(tmp->data);
159         }
160 }
161
162 void channels_setup_init(void)
163 {
164         setupchannels = NULL;
165         source_host_ok = FALSE;
166
167         signal_add("setup reread", (SIGNAL_FUNC) channels_read_config);
168         signal_add("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
169 }
170
171 void channels_setup_deinit(void)
172 {
173         while (setupchannels != NULL)
174                 channel_setup_destroy(setupchannels->data);
175
176         signal_remove("setup reread", (SIGNAL_FUNC) channels_read_config);
177         signal_remove("irssi init read settings", (SIGNAL_FUNC) channels_read_config);
178 }