Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / core / chatnets.c
1 /*
2  chatnets.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 "network.h"
23 #include "signals.h"
24 #include "special-vars.h"
25 #include "lib-config/iconfig.h"
26 #include "settings.h"
27
28 #include "chat-protocols.h"
29 #include "chatnets.h"
30 #include "servers.h"
31
32 GSList *chatnets; /* list of available chat networks */
33
34 static void chatnet_config_save(CHATNET_REC *chatnet)
35 {
36         CONFIG_NODE *node;
37
38         node = iconfig_node_traverse("chatnets", TRUE);
39         node = config_node_section(node, chatnet->name, NODE_TYPE_BLOCK);
40         iconfig_node_clear(node);
41
42         iconfig_node_set_str(node, "type", chat_protocol_find_id(chatnet->chat_type)->name);
43         iconfig_node_set_str(node, "nick", chatnet->nick);
44         iconfig_node_set_str(node, "username", chatnet->username);
45         iconfig_node_set_str(node, "realname", chatnet->realname);
46         iconfig_node_set_str(node, "host", chatnet->own_host);
47         iconfig_node_set_str(node, "autosendcmd", chatnet->autosendcmd);
48
49         signal_emit("chatnet saved", 2, chatnet, node);
50 }
51
52 static void chatnet_config_remove(CHATNET_REC *chatnet)
53 {
54         CONFIG_NODE *node;
55
56         node = iconfig_node_traverse("chatnets", FALSE);
57         if (node != NULL) iconfig_node_set_str(node, chatnet->name, NULL);
58 }
59
60 void chatnet_create(CHATNET_REC *chatnet)
61 {
62         g_return_if_fail(chatnet != NULL);
63
64         chatnet->type = module_get_uniq_id("CHATNET", 0);
65         if (g_slist_find(chatnets, chatnet) == NULL)
66                 chatnets = g_slist_append(chatnets, chatnet);
67
68         chatnet_config_save(chatnet);
69         signal_emit("chatnet created", 1, chatnet);
70 }
71
72 void chatnet_remove(CHATNET_REC *chatnet)
73 {
74         g_return_if_fail(IS_CHATNET(chatnet));
75
76         signal_emit("chatnet removed", 1, chatnet);
77
78         chatnet_config_remove(chatnet);
79         chatnet_destroy(chatnet);
80 }
81
82 void chatnet_destroy(CHATNET_REC *chatnet)
83 {
84         g_return_if_fail(IS_CHATNET(chatnet));
85
86         chatnets = g_slist_remove(chatnets, chatnet);
87         signal_emit("chatnet destroyed", 1, chatnet);
88
89         g_free_not_null(chatnet->nick);
90         g_free_not_null(chatnet->username);
91         g_free_not_null(chatnet->realname);
92         g_free_not_null(chatnet->own_host);
93         g_free_not_null(chatnet->autosendcmd);
94         g_free(chatnet->name);
95         g_free(chatnet);
96 }
97
98 /* Find the chat network by name */
99 CHATNET_REC *chatnet_find(const char *name)
100 {
101         GSList *tmp;
102
103         g_return_val_if_fail(name != NULL, NULL);
104
105         for (tmp = chatnets; tmp != NULL; tmp = tmp->next) {
106                 CHATNET_REC *rec = tmp->data;
107
108                 if (g_strcasecmp(rec->name, name) == 0)
109                         return rec;
110         }
111
112         return NULL;
113 }
114
115 static void sig_connected(SERVER_REC *server)
116 {
117         CHATNET_REC *rec;
118
119         g_return_if_fail(IS_SERVER(server));
120
121         if (server->connrec->chatnet == NULL || server->session_reconnect)
122                 return;
123
124         rec = chatnet_find(server->connrec->chatnet);
125         if (!server->connrec->no_autosendcmd && rec != NULL && rec->autosendcmd)
126                 eval_special_string(rec->autosendcmd, "", server, NULL);
127 }
128
129 static void chatnet_read(CONFIG_NODE *node)
130 {
131         CHAT_PROTOCOL_REC *proto;
132         CHATNET_REC *rec;
133         char *type;
134
135         if (node == NULL || node->key == NULL)
136                 return;
137
138         type = config_node_get_str(node, "type", NULL);
139         proto = type == NULL ? NULL : chat_protocol_find(type);
140         if (proto == NULL) {
141                 proto = type == NULL ? chat_protocol_get_default() :
142                         chat_protocol_get_unknown(type);
143         }
144
145         if (type == NULL)
146                 iconfig_node_set_str(node, "type", proto->name);
147
148         rec = proto->create_chatnet();
149         rec->type = module_get_uniq_id("CHATNET", 0);
150         rec->chat_type = proto->id;
151         rec->name = g_strdup(node->key);
152         rec->nick = g_strdup(config_node_get_str(node, "nick", NULL));
153         rec->username = g_strdup(config_node_get_str(node, "username", NULL));
154         rec->realname = g_strdup(config_node_get_str(node, "realname", NULL));
155         rec->own_host = g_strdup(config_node_get_str(node, "host", NULL));
156         rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
157
158         chatnets = g_slist_append(chatnets, rec);
159         signal_emit("chatnet read", 2, rec, node);
160 }
161
162 static void read_chatnets(void)
163 {
164         CONFIG_NODE *node;
165         GSList *tmp;
166
167         while (chatnets != NULL)
168                 chatnet_destroy(chatnets->data);
169
170         node = iconfig_node_traverse("chatnets", FALSE);
171         if (node != NULL) {
172                 tmp = config_node_first(node->value);
173                 for (; tmp != NULL; tmp = config_node_next(tmp))
174                         chatnet_read(tmp->data);
175         }
176 }
177
178 void chatnets_init(void)
179 {
180         chatnets = NULL;
181
182         signal_add_first("event connected", (SIGNAL_FUNC) sig_connected);
183         signal_add("setup reread", (SIGNAL_FUNC) read_chatnets);
184         signal_add_first("irssi init read settings", (SIGNAL_FUNC) read_chatnets);
185 }
186
187 void chatnets_deinit(void)
188 {
189         module_uniq_destroy("CHATNET");
190
191         signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
192         signal_remove("setup reread", (SIGNAL_FUNC) read_chatnets);
193         signal_remove("irssi init read settings", (SIGNAL_FUNC) read_chatnets);
194 }