Added SILC Thread Queue API
[crypto.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
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 "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             server->connrec->no_autojoin_channels)
123                 return;
124
125         rec = chatnet_find(server->connrec->chatnet);
126         if (rec != NULL && rec->autosendcmd)
127                 eval_special_string(rec->autosendcmd, "", server, NULL);
128 }
129
130 static void chatnet_read(CONFIG_NODE *node)
131 {
132         CHAT_PROTOCOL_REC *proto;
133         CHATNET_REC *rec;
134         char *type;
135
136         if (node == NULL || node->key == NULL)
137                 return;
138
139         type = config_node_get_str(node, "type", NULL);
140         proto = type == NULL ? NULL : chat_protocol_find(type);
141         if (proto == NULL) {
142                 proto = type == NULL ? chat_protocol_get_default() :
143                         chat_protocol_get_unknown(type);
144         }
145
146         if (type == NULL)
147                 iconfig_node_set_str(node, "type", proto->name);
148
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));
158
159         chatnets = g_slist_append(chatnets, rec);
160         signal_emit("chatnet read", 2, rec, node);
161 }
162
163 static void read_chatnets(void)
164 {
165         CONFIG_NODE *node;
166         GSList *tmp;
167
168         while (chatnets != NULL)
169                 chatnet_destroy(chatnets->data);
170
171         node = iconfig_node_traverse("chatnets", FALSE);
172         if (node != NULL) {
173                 tmp = config_node_first(node->value);
174                 for (; tmp != NULL; tmp = config_node_next(tmp))
175                         chatnet_read(tmp->data);
176         }
177 }
178
179 void chatnets_init(void)
180 {
181         chatnets = NULL;
182
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);
186 }
187
188 void chatnets_deinit(void)
189 {
190         module_uniq_destroy("CHATNET");
191
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);
195 }