Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / chat-protocols.c
1 /*
2  chat-protocol.c : irssi
3
4     Copyright (C) 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 "modules.h"
23 #include "signals.h"
24 #include "chat-protocols.h"
25
26 #include "chatnets.h"
27 #include "servers.h"
28 #include "servers-setup.h"
29 #include "channels-setup.h"
30
31 GSList *chat_protocols;
32
33 static CHAT_PROTOCOL_REC *default_proto;
34
35 void *chat_protocol_check_cast(void *object, int type_pos, const char *id)
36 {
37         return object == NULL ||
38                 chat_protocol_lookup(id) !=
39                 G_STRUCT_MEMBER(int, object, type_pos) ? NULL : object;
40 }
41
42 /* Return the ID for the specified chat protocol. */
43 int chat_protocol_lookup(const char *name)
44 {
45         CHAT_PROTOCOL_REC *rec;
46
47         g_return_val_if_fail(name != NULL, -1);
48
49         rec = chat_protocol_find(name);
50         return rec == NULL ? -1 : rec->id;
51 }
52
53 CHAT_PROTOCOL_REC *chat_protocol_find(const char *name)
54 {
55         GSList *tmp;
56
57         g_return_val_if_fail(name != NULL, NULL);
58
59         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
60                 CHAT_PROTOCOL_REC *rec = tmp->data;
61
62                 if (g_strcasecmp(rec->name, name) == 0)
63                         return rec;
64         }
65
66         return NULL;
67 }
68
69 CHAT_PROTOCOL_REC *chat_protocol_find_id(int id)
70 {
71         GSList *tmp;
72
73         g_return_val_if_fail(id > 0, NULL);
74
75         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
76                 CHAT_PROTOCOL_REC *rec = tmp->data;
77
78                 if (rec->id == id)
79                         return rec;
80         }
81
82         return NULL;
83 }
84
85 CHAT_PROTOCOL_REC *chat_protocol_find_net(GHashTable *optlist)
86 {
87         GSList *tmp;
88
89         g_return_val_if_fail(optlist != NULL, NULL);
90
91         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
92                 CHAT_PROTOCOL_REC *rec = tmp->data;
93
94                 if (rec->chatnet != NULL &&
95                     g_hash_table_lookup(optlist, rec->chatnet) != NULL)
96                         return rec;
97         }
98
99         return NULL;
100 }
101
102 /* Register new chat protocol. */
103 CHAT_PROTOCOL_REC *chat_protocol_register(CHAT_PROTOCOL_REC *rec)
104 {
105         CHAT_PROTOCOL_REC *newrec;
106         int created;
107
108         g_return_val_if_fail(rec != NULL, NULL);
109
110         newrec = chat_protocol_find(rec->name);
111         created = newrec == NULL;
112         if (newrec == NULL) {
113                 newrec = g_new0(CHAT_PROTOCOL_REC, 1);
114                 chat_protocols = g_slist_append(chat_protocols, newrec);
115         } else {
116                 /* updating existing protocol */
117                 g_free(newrec->name);
118         }
119
120         memcpy(newrec, rec, sizeof(CHAT_PROTOCOL_REC));
121         newrec->id = module_get_uniq_id_str("PROTOCOL", rec->name);
122         newrec->name = g_strdup(rec->name);
123
124         if (default_proto == NULL)
125                 chat_protocol_set_default(newrec);
126
127         if (created)
128                 signal_emit("chat protocol created", 1, newrec);
129         else
130                 signal_emit("chat protocol updated", 1, newrec);
131         return newrec;
132 }
133
134 static void chat_protocol_destroy(CHAT_PROTOCOL_REC *rec)
135 {
136         g_return_if_fail(rec != NULL);
137
138         chat_protocols = g_slist_remove(chat_protocols, rec);
139
140         if (default_proto == rec) {
141                 chat_protocol_set_default(chat_protocols == NULL ? NULL :
142                                           chat_protocols->data);
143         }
144
145         signal_emit("chat protocol destroyed", 1, rec);
146
147         g_free(rec->name);
148         g_free(rec);
149 }
150
151 /* Unregister chat protocol. */
152 void chat_protocol_unregister(const char *name)
153 {
154         CHAT_PROTOCOL_REC *rec;
155
156         g_return_if_fail(name != NULL);
157
158         rec = chat_protocol_find(name);
159         if (rec != NULL) {
160                 chat_protocol_destroy(rec);
161
162                 /* there might still be references to this chat protocol -
163                    recreate it as a dummy protocol */
164                 chat_protocol_get_unknown(name);
165         }
166 }
167
168 /* Default chat protocol to use */
169 void chat_protocol_set_default(CHAT_PROTOCOL_REC *rec)
170 {
171         default_proto = rec;
172 }
173
174 CHAT_PROTOCOL_REC *chat_protocol_get_default(void)
175 {
176         return default_proto;
177 }
178
179 static CHATNET_REC *create_chatnet(void)
180 {
181         return g_new0(CHATNET_REC, 1);
182 }
183
184 static SERVER_SETUP_REC *create_server_setup(void)
185 {
186         return g_new0(SERVER_SETUP_REC, 1);
187 }
188
189 static CHANNEL_SETUP_REC *create_channel_setup(void)
190 {
191         return g_new0(CHANNEL_SETUP_REC, 1);
192 }
193
194 static SERVER_CONNECT_REC *create_server_connect(void)
195 {
196         return g_new0(SERVER_CONNECT_REC, 1);
197 }
198
199 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
200 {
201 }
202
203 /* Return "unknown chat protocol" record. Used when protocol name is
204    specified but it isn't registered yet. */
205 CHAT_PROTOCOL_REC *chat_protocol_get_unknown(const char *name)
206 {
207         CHAT_PROTOCOL_REC *rec, *newrec;
208
209         g_return_val_if_fail(name != NULL, NULL);
210
211         rec = chat_protocol_find(name);
212         if (rec != NULL)
213                 return rec;
214
215         rec = g_new0(CHAT_PROTOCOL_REC, 1);
216         rec->not_initialized = TRUE;
217         rec->name = (char *) name;
218         rec->create_chatnet = create_chatnet;
219         rec->create_server_setup = create_server_setup;
220         rec->create_channel_setup = create_channel_setup;
221         rec->create_server_connect = create_server_connect;
222         rec->destroy_server_connect = destroy_server_connect;
223
224         newrec = chat_protocol_register(rec);
225         g_free(rec);
226         return newrec;
227 }
228
229 void chat_protocols_init(void)
230 {
231         default_proto = NULL;
232         chat_protocols = NULL;
233 }
234
235 void chat_protocols_deinit(void)
236 {
237         while (chat_protocols != NULL)
238                 chat_protocol_destroy(chat_protocols->data);
239 }