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
24 #include "special-vars.h"
25 #include "lib-config/iconfig.h"
28 #include "chat-protocols.h"
32 GSList *chatnets; /* list of available chat networks */
34 static void chatnet_config_save(CHATNET_REC *chatnet)
38 node = iconfig_node_traverse("chatnets", TRUE);
39 node = config_node_section(node, chatnet->name, NODE_TYPE_BLOCK);
40 iconfig_node_clear(node);
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);
49 signal_emit("chatnet saved", 2, chatnet, node);
52 static void chatnet_config_remove(CHATNET_REC *chatnet)
56 node = iconfig_node_traverse("chatnets", FALSE);
57 if (node != NULL) iconfig_node_set_str(node, chatnet->name, NULL);
60 void chatnet_create(CHATNET_REC *chatnet)
62 g_return_if_fail(chatnet != NULL);
64 chatnet->type = module_get_uniq_id("CHATNET", 0);
65 if (g_slist_find(chatnets, chatnet) == NULL)
66 chatnets = g_slist_append(chatnets, chatnet);
68 chatnet_config_save(chatnet);
69 signal_emit("chatnet created", 1, chatnet);
72 void chatnet_remove(CHATNET_REC *chatnet)
74 g_return_if_fail(IS_CHATNET(chatnet));
76 signal_emit("chatnet removed", 1, chatnet);
78 chatnet_config_remove(chatnet);
79 chatnet_destroy(chatnet);
82 void chatnet_destroy(CHATNET_REC *chatnet)
84 g_return_if_fail(IS_CHATNET(chatnet));
86 chatnets = g_slist_remove(chatnets, chatnet);
87 signal_emit("chatnet destroyed", 1, chatnet);
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);
98 /* Find the chat network by name */
99 CHATNET_REC *chatnet_find(const char *name)
103 g_return_val_if_fail(name != NULL, NULL);
105 for (tmp = chatnets; tmp != NULL; tmp = tmp->next) {
106 CHATNET_REC *rec = tmp->data;
108 if (g_strcasecmp(rec->name, name) == 0)
115 static void sig_connected(SERVER_REC *server)
119 g_return_if_fail(IS_SERVER(server));
121 if (server->connrec->chatnet == NULL || server->session_reconnect ||
122 server->connrec->no_autojoin_channels)
125 rec = chatnet_find(server->connrec->chatnet);
126 if (rec != NULL && rec->autosendcmd)
127 eval_special_string(rec->autosendcmd, "", server, NULL);
130 static void chatnet_read(CONFIG_NODE *node)
132 CHAT_PROTOCOL_REC *proto;
136 if (node == NULL || node->key == NULL)
139 type = config_node_get_str(node, "type", NULL);
140 proto = type == NULL ? NULL : chat_protocol_find(type);
142 proto = type == NULL ? chat_protocol_get_default() :
143 chat_protocol_get_unknown(type);
147 iconfig_node_set_str(node, "type", proto->name);
149 rec = proto->create_chatnet();
150 rec->type = module_get_uniq_id("CHATNET", 0);
151 rec->chat_type = proto->id;
152 rec->name = g_strdup(node->key);
153 rec->nick = g_strdup(config_node_get_str(node, "nick", NULL));
154 rec->username = g_strdup(config_node_get_str(node, "username", NULL));
155 rec->realname = g_strdup(config_node_get_str(node, "realname", NULL));
156 rec->own_host = g_strdup(config_node_get_str(node, "host", NULL));
157 rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL));
159 chatnets = g_slist_append(chatnets, rec);
160 signal_emit("chatnet read", 2, rec, node);
163 static void read_chatnets(void)
168 while (chatnets != NULL)
169 chatnet_destroy(chatnets->data);
171 node = iconfig_node_traverse("chatnets", FALSE);
173 tmp = config_node_first(node->value);
174 for (; tmp != NULL; tmp = config_node_next(tmp))
175 chatnet_read(tmp->data);
179 void chatnets_init(void)
183 signal_add_first("event connected", (SIGNAL_FUNC) sig_connected);
184 signal_add("setup reread", (SIGNAL_FUNC) read_chatnets);
185 signal_add_first("irssi init read settings", (SIGNAL_FUNC) read_chatnets);
188 void chatnets_deinit(void)
190 module_uniq_destroy("CHATNET");
192 signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
193 signal_remove("setup reread", (SIGNAL_FUNC) read_chatnets);
194 signal_remove("irssi init read settings", (SIGNAL_FUNC) read_chatnets);